Fixes U4-2640 LocalizationService throws exception when getting dictionary items and adds tests for the LocalizationService

This commit is contained in:
Morten Christensen
2014-06-08 13:16:32 +02:00
parent ea32776174
commit 02eff33d29
3 changed files with 207 additions and 4 deletions

View File

@@ -33,7 +33,7 @@ namespace Umbraco.Core.Persistence.Repositories
var sql = GetBaseQuery(false); var sql = GetBaseQuery(false);
sql.Where(GetBaseWhereClause(), new { Id = id }); sql.Where(GetBaseWhereClause(), new { Id = id });
var languageDto = Database.First<LanguageDto>(sql); var languageDto = Database.FirstOrDefault<LanguageDto>(sql);
if (languageDto == null) if (languageDto == null)
return null; return null;

View File

@@ -773,6 +773,7 @@ namespace Umbraco.Core.Services
var items = new List<IDictionaryItem>(); var items = new List<IDictionaryItem>();
foreach (var dictionaryItemElement in dictionaryItemElementList.Elements("DictionaryItem")) foreach (var dictionaryItemElement in dictionaryItemElementList.Elements("DictionaryItem"))
items.AddRange(ImportDictionaryItem(dictionaryItemElement, languages, parentId)); items.AddRange(ImportDictionaryItem(dictionaryItemElement, languages, parentId));
return items; return items;
} }
@@ -788,6 +789,7 @@ namespace Umbraco.Core.Services
dictionaryItem = CreateNewDictionaryItem(key, dictionaryItemElement, languages, parentId); dictionaryItem = CreateNewDictionaryItem(key, dictionaryItemElement, languages, parentId);
_localizationService.Save(dictionaryItem); _localizationService.Save(dictionaryItem);
items.Add(dictionaryItem); items.Add(dictionaryItem);
items.AddRange(ImportDictionaryItems(dictionaryItemElement, languages, dictionaryItem.Key)); items.AddRange(ImportDictionaryItems(dictionaryItemElement, languages, dictionaryItem.Key));
return items; return items;
} }
@@ -817,7 +819,8 @@ namespace Umbraco.Core.Services
private static bool DictionaryValueIsNew(IEnumerable<IDictionaryTranslation> translations, XElement valueElement) private static bool DictionaryValueIsNew(IEnumerable<IDictionaryTranslation> translations, XElement valueElement)
{ {
return translations.All(t => 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
); );
} }

View File

@@ -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; using Umbraco.Tests.TestHelpers;
namespace Umbraco.Tests.Services namespace Umbraco.Tests.Services
@@ -8,10 +12,17 @@ namespace Umbraco.Tests.Services
/// This is more of an integration test as it involves multiple layers /// This is more of an integration test as it involves multiple layers
/// as well as configuration. /// as well as configuration.
/// </summary> /// </summary>
[DatabaseTestBehavior(DatabaseBehavior.NewDbFileAndSchemaPerFixture)] [DatabaseTestBehavior(DatabaseBehavior.NewDbFileAndSchemaPerTest)]
[TestFixture, RequiresSTA] [TestFixture, RequiresSTA]
public class LocalizationServiceTests : BaseServiceTest public class LocalizationServiceTests : BaseServiceTest
{ {
private Guid _parentItemGuidId;
private int _parentItemIntId;
private Guid _childItemGuidId;
private int _childItemIntId;
private int _danishLangId;
private int _englishLangId;
[SetUp] [SetUp]
public override void Initialize() public override void Initialize()
{ {
@@ -23,5 +34,194 @@ namespace Umbraco.Tests.Services
{ {
base.TearDown(); 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<IDictionaryTranslation>
{
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<IDictionaryTranslation>
{
new DictionaryTranslation(english, "ChildValue"),
new DictionaryTranslation(danish, "BørnVærdi")
}
};
ServiceContext.LocalizationService.Save(childItem);
_childItemGuidId = childItem.Key;
_childItemIntId = childItem.Id;
}
} }
} }