Migrated various service tests into the new integration or unit tests projects as appropriate.

This commit is contained in:
Andy Butland
2020-10-05 21:45:33 +02:00
parent a7c4fa5f2e
commit 30b0f142eb
5 changed files with 53 additions and 160 deletions

View File

@@ -1,4 +1,5 @@
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Configuration.Models;
@@ -7,7 +8,7 @@ using Umbraco.Core.Models;
using Umbraco.Core.Persistence.Repositories.Implement;
using Umbraco.Core.Services;
using Umbraco.Core.Services.Implement;
using Umbraco.Tests.Common.Builders;
using Umbraco.Tests.Integration.Testing;
using Umbraco.Tests.TestHelpers.Entities;
using Umbraco.Tests.Testing;
@@ -18,13 +19,18 @@ namespace Umbraco.Tests.Services
PublishedRepositoryEvents = true,
WithApplication = true,
Logger = UmbracoTestOptions.Logger.Console)]
public class ContentServiceEventTests : TestWithSomeContentBase
public class ContentServiceEventTests : UmbracoIntegrationTest
{
private IContentTypeService _contentTypeService => GetRequiredService<IContentTypeService>();
private IContentService _contentService => GetRequiredService<IContentService>();
private ILocalizationService _localizationService => GetRequiredService<ILocalizationService>();
private IFileService _fileService => GetRequiredService<IFileService>();
private GlobalSettings _globalSettings;
public override void SetUp()
public override async Task Setup()
{
base.SetUp();
await base.Setup();
ContentRepositoryBase.ThrowOnWarning = true;
_globalSettings = new GlobalSettings();
}
@@ -38,28 +44,22 @@ namespace Umbraco.Tests.Services
[Test]
public void Saving_Culture()
{
var languageService = ServiceContext.LocalizationService;
languageService.Save(new Language(_globalSettings, "fr-FR"));
var contentTypeService = ServiceContext.ContentTypeService;
_localizationService.Save(new Language(_globalSettings, "fr-FR"));
var contentType = MockedContentTypes.CreateTextPageContentType();
ServiceContext.FileService.SaveTemplate(contentType.DefaultTemplate);
_fileService.SaveTemplate(contentType.DefaultTemplate);
contentType.Variations = ContentVariation.Culture;
foreach (var propertyType in contentType.PropertyTypes)
propertyType.Variations = ContentVariation.Culture;
contentTypeService.Save(contentType);
var contentService = ServiceContext.ContentService;
_contentTypeService.Save(contentType);
IContent document = new Content("content", -1, contentType);
document.SetCultureName("hello", "en-US");
document.SetCultureName("bonjour", "fr-FR");
contentService.Save(document);
_contentService.Save(document);
//re-get - dirty properties need resetting
document = contentService.GetById(document.Id);
document = _contentService.GetById(document.Id);
// properties: title, bodyText, keywords, description
document.SetValue("title", "title-en", "en-US");
@@ -88,7 +88,7 @@ namespace Umbraco.Tests.Services
ContentService.Saved += OnSaved;
try
{
contentService.Save(document);
_contentService.Save(document);
}
finally
{
@@ -100,13 +100,9 @@ namespace Umbraco.Tests.Services
[Test]
public void Saving_Set_Value()
{
var contentTypeService = ServiceContext.ContentTypeService;
var contentType = MockedContentTypes.CreateTextPageContentType();
ServiceContext.FileService.SaveTemplate(contentType.DefaultTemplate);
contentTypeService.Save(contentType);
var contentService = ServiceContext.ContentService;
_fileService.SaveTemplate(contentType.DefaultTemplate);
_contentTypeService.Save(contentType);
IContent document = new Content("content", -1, contentType);
@@ -136,44 +132,37 @@ namespace Umbraco.Tests.Services
ContentService.Saved += OnSaved;
try
{
contentService.Save(document);
_contentService.Save(document);
}
finally
{
ContentService.Saving -= OnSaving;
ContentService.Saved -= OnSaved;
}
}
[Test]
public void Publishing_Culture()
{
var languageService = ServiceContext.LocalizationService;
languageService.Save(new Language(_globalSettings, "fr-FR"));
var contentTypeService = ServiceContext.ContentTypeService;
_localizationService.Save(new Language(_globalSettings, "fr-FR"));
var contentType = MockedContentTypes.CreateTextPageContentType();
ServiceContext.FileService.SaveTemplate(contentType.DefaultTemplate);
_fileService.SaveTemplate(contentType.DefaultTemplate);
contentType.Variations = ContentVariation.Culture;
foreach (var propertyType in contentType.PropertyTypes)
propertyType.Variations = ContentVariation.Culture;
contentTypeService.Save(contentType);
var contentService = ServiceContext.ContentService;
_contentTypeService.Save(contentType);
IContent document = new Content("content", -1, contentType);
document.SetCultureName("hello", "en-US");
document.SetCultureName("bonjour", "fr-FR");
contentService.Save(document);
_contentService.Save(document);
Assert.IsFalse(document.IsCulturePublished("fr-FR"));
Assert.IsFalse(document.IsCulturePublished("en-US"));
//re-get - dirty properties need resetting
document = contentService.GetById(document.Id);
document = _contentService.GetById(document.Id);
void OnPublishing(IContentService sender, ContentPublishingEventArgs e)
{
@@ -199,7 +188,7 @@ namespace Umbraco.Tests.Services
ContentService.Published += OnPublished;
try
{
contentService.SaveAndPublish(document, "fr-FR");
_contentService.SaveAndPublish(document, "fr-FR");
}
finally
{
@@ -207,7 +196,7 @@ namespace Umbraco.Tests.Services
ContentService.Published -= OnPublished;
}
document = contentService.GetById(document.Id);
document = _contentService.GetById(document.Id);
// ensure it works and does not throw
Assert.IsTrue(document.IsCulturePublished("fr-FR"));
@@ -217,13 +206,9 @@ namespace Umbraco.Tests.Services
[Test]
public void Publishing_Set_Value()
{
var contentTypeService = ServiceContext.ContentTypeService;
var contentType = MockedContentTypes.CreateTextPageContentType();
ServiceContext.FileService.SaveTemplate(contentType.DefaultTemplate);
contentTypeService.Save(contentType);
var contentService = ServiceContext.ContentService;
_fileService.SaveTemplate(contentType.DefaultTemplate);
_contentTypeService.Save(contentType);
IContent document = new Content("content", -1, contentType);
@@ -242,21 +227,21 @@ namespace Umbraco.Tests.Services
Assert.AreSame("title", document.GetValue<string>("title"));
//we're only dealing with invariant here
// We're only dealing with invariant here.
var propValue = saved.Properties["title"].Values.First(x => x.Culture == null && x.Segment == null);
Assert.AreEqual("title", propValue.EditedValue);
Assert.AreEqual("title", propValue.PublishedValue);
}
//We are binding to Saving (not Publishing), because the Publishing event is really just used for cancelling, it should not be
//used for setting values and it won't actually work! This is because the Publishing event is raised AFTER the values on the model
//are published, but Saving is raised BEFORE.
// We are binding to Saving (not Publishing), because the Publishing event is really just used for cancelling, it should not be
// used for setting values and it won't actually work! This is because the Publishing event is raised AFTER the values on the model
// are published, but Saving is raised BEFORE.
ContentService.Saving += OnSaving;
ContentService.Saved += OnSaved;
try
{
contentService.SaveAndPublish(document);
_contentService.SaveAndPublish(document);
}
finally
{
@@ -268,19 +253,15 @@ namespace Umbraco.Tests.Services
[Test]
public void Publishing_Set_Mandatory_Value()
{
var contentTypeService = ServiceContext.ContentTypeService;
var contentType = MockedContentTypes.CreateTextPageContentType();
var titleProperty = contentType.PropertyTypes.First(x => x.Alias == "title");
titleProperty.Mandatory = true; // make this required!
ServiceContext.FileService.SaveTemplate(contentType.DefaultTemplate);
contentTypeService.Save(contentType);
var contentService = ServiceContext.ContentService;
_fileService.SaveTemplate(contentType.DefaultTemplate);
_contentTypeService.Save(contentType);
IContent document = new Content("content", -1, contentType);
var result = contentService.SaveAndPublish(document);
var result = _contentService.SaveAndPublish(document);
Assert.IsFalse(result.Success);
Assert.AreEqual("title", result.InvalidProperties.First().Alias);
@@ -297,13 +278,13 @@ namespace Umbraco.Tests.Services
saved.SetValue("title", "title");
}
//We are binding to Saving (not Publishing), because the Publishing event is really just used for cancelling, it should not be
//used for setting values and it won't actually work! This is because the Publishing event is raised AFTER the values on the model
//are published, but Saving is raised BEFORE.
// We are binding to Saving (not Publishing), because the Publishing event is really just used for cancelling, it should not be
// used for setting values and it won't actually work! This is because the Publishing event is raised AFTER the values on the model
// are published, but Saving is raised BEFORE.
ContentService.Saving += OnSaving;
try
{
result = contentService.SaveAndPublish(document);
result = _contentService.SaveAndPublish(document);
Assert.IsTrue(result.Success); //will succeed now because we were able to specify the required value in the Saving event
}
finally
@@ -315,20 +296,16 @@ namespace Umbraco.Tests.Services
[Test]
public void Unpublishing_Culture()
{
var languageService = ServiceContext.LocalizationService;
languageService.Save(new Language(_globalSettings, "fr-FR"));
var contentTypeService = ServiceContext.ContentTypeService;
_localizationService.Save(new Language(_globalSettings, "fr-FR"));
var contentType = MockedContentTypes.CreateTextPageContentType();
ServiceContext.FileService.SaveTemplate(contentType.DefaultTemplate);
_fileService.SaveTemplate(contentType.DefaultTemplate);
contentType.Variations = ContentVariation.Culture;
foreach (var propertyType in contentType.PropertyTypes)
propertyType.Variations = ContentVariation.Culture;
contentTypeService.Save(contentType);
_contentTypeService.Save(contentType);
var contentService = (ContentService)ServiceContext.ContentService;
var contentService = (ContentService)_contentService;
IContent document = new Content("content", -1, contentType);
document.SetCultureName("hello", "en-US");

View File

@@ -1,12 +1,9 @@
using NUnit.Framework;
using System.Linq;
using System.Linq;
using System.Threading;
using Umbraco.Core;
using Umbraco.Core.Composing;
using NUnit.Framework;
using Umbraco.Core.Configuration.Models;
using Umbraco.Core.Models.Membership;
using Umbraco.Core.Services;
using Umbraco.Tests.Common.Builders;
using Umbraco.Tests.Integration.Testing;
using Umbraco.Tests.Testing;
using Umbraco.Web.Services;
@@ -21,8 +18,8 @@ namespace Umbraco.Tests.Services
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
public class SectionServiceTests : UmbracoIntegrationTest
{
private ISectionService SectionService => GetRequiredService<ISectionService>();
private IUserService UserService => GetRequiredService<IUserService>();
private ISectionService _sectionService => GetRequiredService<ISectionService>();
private IUserService _userService => GetRequiredService<IUserService>();
[Test]
public void SectionService_Can_Get_Allowed_Sections_For_User()
@@ -31,7 +28,7 @@ namespace Umbraco.Tests.Services
var user = CreateTestUser();
// Act
var result = SectionService.GetAllowedSections(user.Id).ToList();
var result = _sectionService.GetAllowedSections(user.Id).ToList();
// Assert
Assert.AreEqual(3, result.Count);
@@ -46,7 +43,7 @@ namespace Umbraco.Tests.Services
Username = "testUser",
Email = "testuser@test.com",
};
UserService.Save(user, false);
_userService.Save(user, false);
var userGroupA = new UserGroup(ShortStringHelper)
{
@@ -56,7 +53,7 @@ namespace Umbraco.Tests.Services
userGroupA.AddAllowedSection("media");
userGroupA.AddAllowedSection("settings");
// TODO: This is failing the test
UserService.Save(userGroupA, new[] { user.Id }, false);
_userService.Save(userGroupA, new[] { user.Id }, false);
var userGroupB = new UserGroup(ShortStringHelper)
{
@@ -65,9 +62,9 @@ namespace Umbraco.Tests.Services
};
userGroupB.AddAllowedSection("settings");
userGroupB.AddAllowedSection("member");
UserService.Save(userGroupB, new[] { user.Id }, false);
_userService.Save(userGroupB, new[] { user.Id }, false);
return UserService.GetUserById(user.Id);
return _userService.GetUserById(user.Id);
}
}
}

View File

@@ -1,78 +0,0 @@
using System;
using System.Reflection;
using System.Text;
using NUnit.Framework;
using Umbraco.Core.Events;
using Umbraco.Core.Services.Implement;
namespace Umbraco.Tests.Services
{
[TestFixture]
public class AmbiguousEventTests
{
[Explicit]
[TestCase(typeof(ContentService))]
[TestCase(typeof(MediaService))]
public void ListAmbiguousEvents(Type serviceType)
{
var typedEventHandler = typeof(TypedEventHandler<,>);
// get all events
var events = serviceType.GetEvents(BindingFlags.Static | BindingFlags.Public);
string TypeName(Type type)
{
if (!type.IsGenericType)
return type.Name;
var sb = new StringBuilder();
TypeNameSb(type, sb);
return sb.ToString();
}
void TypeNameSb(Type type, StringBuilder sb)
{
var name = type.Name;
var pos = name.IndexOf('`');
name = pos > 0 ? name.Substring(0, pos) : name;
sb.Append(name);
if (!type.IsGenericType)
return;
sb.Append("<");
var first = true;
foreach (var arg in type.GetGenericArguments())
{
if (first) first = false;
else sb.Append(", ");
TypeNameSb(arg, sb);
}
sb.Append(">");
}
foreach (var e in events)
{
// only continue if this is a TypedEventHandler
if (!e.EventHandlerType.IsGenericType) continue;
var typeDef = e.EventHandlerType.GetGenericTypeDefinition();
if (typedEventHandler != typeDef) continue;
// get the event args type
var eventArgsType = e.EventHandlerType.GenericTypeArguments[1];
// try to find the event back, based upon sender type + args type
// exclude -ing (eg Saving) events, we don't deal with them in EventDefinitionBase (they always trigger)
var found = EventNameExtractor.FindEvents(serviceType, eventArgsType, EventNameExtractor.MatchIngNames);
if (found.Length == 1) continue;
if (found.Length == 0)
{
Console.WriteLine($"{typeof(ContentService).Name} {e.Name} {TypeName(eventArgsType)} NotFound");
continue;
}
Console.WriteLine($"{typeof(ContentService).Name} {e.Name} {TypeName(eventArgsType)} Ambiguous");
Console.WriteLine("\t" + string.Join(", ", found));
}
}
}
}

View File

@@ -154,8 +154,6 @@
<Compile Include="Routing\RoutableDocumentFilterTests.cs" />
<Compile Include="Runtimes\StandaloneTests.cs" />
<Compile Include="Routing\GetContentUrlsTests.cs" />
<Compile Include="Services\AmbiguousEventTests.cs" />
<Compile Include="Services\ContentServiceEventTests.cs" />
<Compile Include="Services\ContentServicePublishBranchTests.cs" />
<Compile Include="Services\ContentServiceTagsTests.cs" />
<Compile Include="Services\ContentTypeServiceVariantsTests.cs" />

View File

@@ -96,7 +96,6 @@ namespace Umbraco.Web
if (currentUmbracoContext != null)
return new UmbracoContextReference(currentUmbracoContext, false, _umbracoContextAccessor);
var umbracoContext = CreateUmbracoContext();
_umbracoContextAccessor.UmbracoContext = umbracoContext;