diff --git a/src/Umbraco.Core/Persistence/Repositories/LanguageRepository.cs b/src/Umbraco.Core/Persistence/Repositories/LanguageRepository.cs index d52a2549f4..0c891d512d 100644 --- a/src/Umbraco.Core/Persistence/Repositories/LanguageRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/LanguageRepository.cs @@ -33,7 +33,7 @@ namespace Umbraco.Core.Persistence.Repositories var sql = GetBaseQuery(false); sql.Where(GetBaseWhereClause(), new { Id = id }); - var languageDto = Database.First(sql); + var languageDto = Database.FirstOrDefault(sql); if (languageDto == null) return null; diff --git a/src/Umbraco.Core/Services/PackagingService.cs b/src/Umbraco.Core/Services/PackagingService.cs index b02b765a37..94360e71ad 100644 --- a/src/Umbraco.Core/Services/PackagingService.cs +++ b/src/Umbraco.Core/Services/PackagingService.cs @@ -773,6 +773,7 @@ namespace Umbraco.Core.Services var items = new List(); foreach (var dictionaryItemElement in dictionaryItemElementList.Elements("DictionaryItem")) items.AddRange(ImportDictionaryItem(dictionaryItemElement, languages, parentId)); + return items; } @@ -788,6 +789,7 @@ namespace Umbraco.Core.Services dictionaryItem = CreateNewDictionaryItem(key, dictionaryItemElement, languages, parentId); _localizationService.Save(dictionaryItem); items.Add(dictionaryItem); + items.AddRange(ImportDictionaryItems(dictionaryItemElement, languages, dictionaryItem.Key)); return items; } @@ -817,7 +819,8 @@ namespace Umbraco.Core.Services private static bool DictionaryValueIsNew(IEnumerable translations, XElement valueElement) { return translations.All(t => - String.Compare(t.Language.IsoCode, valueElement.Attribute("LanguageCultureAlias").Value, StringComparison.InvariantCultureIgnoreCase) != 0 + String.Compare(t.Language.IsoCode, valueElement.Attribute("LanguageCultureAlias").Value, + StringComparison.InvariantCultureIgnoreCase) != 0 ); } diff --git a/src/Umbraco.Tests/Services/LocalizationServiceTests.cs b/src/Umbraco.Tests/Services/LocalizationServiceTests.cs index 6b22da6800..3ce63bc500 100644 --- a/src/Umbraco.Tests/Services/LocalizationServiceTests.cs +++ b/src/Umbraco.Tests/Services/LocalizationServiceTests.cs @@ -1,4 +1,8 @@ -using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using NUnit.Framework; +using Umbraco.Core.Models; using Umbraco.Tests.TestHelpers; namespace Umbraco.Tests.Services @@ -8,10 +12,17 @@ namespace Umbraco.Tests.Services /// This is more of an integration test as it involves multiple layers /// as well as configuration. /// - [DatabaseTestBehavior(DatabaseBehavior.NewDbFileAndSchemaPerFixture)] + [DatabaseTestBehavior(DatabaseBehavior.NewDbFileAndSchemaPerTest)] [TestFixture, RequiresSTA] public class LocalizationServiceTests : BaseServiceTest { + private Guid _parentItemGuidId; + private int _parentItemIntId; + private Guid _childItemGuidId; + private int _childItemIntId; + private int _danishLangId; + private int _englishLangId; + [SetUp] public override void Initialize() { @@ -23,5 +34,194 @@ namespace Umbraco.Tests.Services { base.TearDown(); } + + [Test] + public void LocalizationService_Can_Get_Root_Dictionary_Items() + { + var rootItems = ServiceContext.LocalizationService.GetRootDictionaryItems(); + + Assert.NotNull(rootItems); + Assert.IsTrue(rootItems.Any()); + } + + [Test] + public void LocalizationService_Can_Determint_If_DictionaryItem_Exists() + { + var exists = ServiceContext.LocalizationService.DictionaryItemExists("Parent"); + Assert.IsTrue(exists); + } + + [Test] + public void LocalizationService_Can_Get_All_Languages() + { + var languages = ServiceContext.LocalizationService.GetAllLanguages(); + Assert.NotNull(languages); + Assert.IsTrue(languages.Any()); + Assert.That(languages.Count(), Is.EqualTo(3)); + } + + [Test] + public void LocalizationService_Can_Get_Dictionary_Item_By_Int_Id() + { + var parentItem = ServiceContext.LocalizationService.GetDictionaryItemById(_parentItemIntId); + Assert.NotNull(parentItem); + + var childItem = ServiceContext.LocalizationService.GetDictionaryItemById(_childItemIntId); + Assert.NotNull(childItem); + } + + [Test] + public void LocalizationService_Can_Get_Dictionary_Item_By_Guid_Id() + { + var parentItem = ServiceContext.LocalizationService.GetDictionaryItemById(_parentItemGuidId); + Assert.NotNull(parentItem); + + var childItem = ServiceContext.LocalizationService.GetDictionaryItemById(_childItemGuidId); + Assert.NotNull(childItem); + } + + [Test] + public void LocalizationService_Can_Get_Dictionary_Item_By_Key() + { + var parentItem = ServiceContext.LocalizationService.GetDictionaryItemByKey("Parent"); + Assert.NotNull(parentItem); + + var childItem = ServiceContext.LocalizationService.GetDictionaryItemByKey("Child"); + Assert.NotNull(childItem); + } + + [Test] + public void LocalizationService_Can_Get_Dictionary_Item_Children() + { + var item = ServiceContext.LocalizationService.GetDictionaryItemChildren(_parentItemGuidId); + Assert.NotNull(item); + Assert.That(item.Count(), Is.EqualTo(1)); + + foreach (var dictionaryItem in item) + { + Assert.IsFalse(string.IsNullOrEmpty(dictionaryItem.ItemKey)); + } + } + + [Test] + public void LocalizationService_Can_Get_Language_By_Culture_Code() + { + var danish = ServiceContext.LocalizationService.GetLanguageByCultureCode("Danish"); + var english = ServiceContext.LocalizationService.GetLanguageByCultureCode("English"); + Assert.NotNull(danish); + Assert.NotNull(english); + } + + [Test] + public void LocalizationService_Can_GetLanguageById() + { + var danish = ServiceContext.LocalizationService.GetLanguageById(_danishLangId); + var english = ServiceContext.LocalizationService.GetLanguageById(_englishLangId); + Assert.NotNull(danish); + Assert.NotNull(english); + } + + [Test] + public void LocalizationService_Can_GetLanguageByIsoCode() + { + var danish = ServiceContext.LocalizationService.GetLanguageByIsoCode("da-DK"); + var english = ServiceContext.LocalizationService.GetLanguageByIsoCode("en-GB"); + Assert.NotNull(danish); + Assert.NotNull(english); + } + + [Test] + public void LocalizationService_Does_Not_Fail_When_Language_Doesnt_Exist() + { + var language = ServiceContext.LocalizationService.GetLanguageByIsoCode("sv-SE"); + Assert.Null(language); + } + + [Test] + public void LocalizationService_Does_Not_Fail_When_DictionaryItem_Doesnt_Exist() + { + var item = ServiceContext.LocalizationService.GetDictionaryItemByKey("RandomKey"); + Assert.Null(item); + } + + [Test] + public void LocalizationService_Can_Delete_Language() + { + var norwegian = new Language("nb-NO") { CultureName = "Norwegian" }; + ServiceContext.LocalizationService.Save(norwegian, 0); + Assert.That(norwegian.HasIdentity, Is.True); + var languageId = norwegian.Id; + + ServiceContext.LocalizationService.Delete(norwegian); + + var language = ServiceContext.LocalizationService.GetLanguageById(languageId); + Assert.Null(language); + } + + [Test] + public void LocalizationService_Can_Delete_DictionaryItem() + { + var item = ServiceContext.LocalizationService.GetDictionaryItemByKey("Child"); + Assert.NotNull(item); + + ServiceContext.LocalizationService.Delete(item); + + var deletedItem = ServiceContext.LocalizationService.GetDictionaryItemByKey("Child"); + Assert.Null(deletedItem); + } + + [Test] + public void LocalizationService_Can_Update_Existing_DictionaryItem() + { + var item = ServiceContext.LocalizationService.GetDictionaryItemByKey("Child"); + foreach (var translation in item.Translations) + { + translation.Value = translation.Value + "UPDATED"; + } + + ServiceContext.LocalizationService.Save(item); + + var updatedItem = ServiceContext.LocalizationService.GetDictionaryItemByKey("Child"); + Assert.NotNull(updatedItem); + + foreach (var translation in updatedItem.Translations) + { + Assert.That(translation.Value.EndsWith("UPDATED"), Is.True); + } + } + + public override void CreateTestData() + { + var danish = new Language("da-DK") { CultureName = "Danish" }; + var english = new Language("en-GB") { CultureName = "English" }; + ServiceContext.LocalizationService.Save(danish, 0); + ServiceContext.LocalizationService.Save(english, 0); + _danishLangId = danish.Id; + _englishLangId = english.Id; + + var parentItem = new DictionaryItem("Parent") + { + Translations = new List + { + new DictionaryTranslation(english, "ParentValue"), + new DictionaryTranslation(danish, "ForældreVærdi") + } + }; + ServiceContext.LocalizationService.Save(parentItem); + _parentItemGuidId = parentItem.Key; + _parentItemIntId = parentItem.Id; + + var childItem = new DictionaryItem(parentItem.Key, "Child") + { + Translations = new List + { + new DictionaryTranslation(english, "ChildValue"), + new DictionaryTranslation(danish, "BørnVærdi") + } + }; + ServiceContext.LocalizationService.Save(childItem); + _childItemGuidId = childItem.Key; + _childItemIntId = childItem.Id; + } } } \ No newline at end of file