From fb3b99a49a1d5984aa00f0bbb022fe523f8d6264 Mon Sep 17 00:00:00 2001 From: Shannon Date: Mon, 12 Jan 2015 19:39:30 +1100 Subject: [PATCH] Fixes up a left join with dictionary -> language items, adds another couple of unit tests adds another method to the ILocalizationService --- .../Repositories/DictionaryRepository.cs | 2 +- .../Services/ILocalizationService.cs | 9 ++++ .../Services/LocalizationService.cs | 29 ++++++++++++ .../Repositories/DictionaryRepositoryTest.cs | 47 +++++++++++++++++-- .../Services/LocalizationServiceTests.cs | 35 +++++++++----- src/umbraco.cms/businesslogic/Dictionary.cs | 17 +++---- 6 files changed, 110 insertions(+), 29 deletions(-) diff --git a/src/Umbraco.Core/Persistence/Repositories/DictionaryRepository.cs b/src/Umbraco.Core/Persistence/Repositories/DictionaryRepository.cs index 390982b11c..c77bc9624a 100644 --- a/src/Umbraco.Core/Persistence/Repositories/DictionaryRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/DictionaryRepository.cs @@ -109,7 +109,7 @@ namespace Umbraco.Core.Persistence.Repositories { sql.Select("*") .From() - .InnerJoin() + .LeftJoin() .On(left => left.UniqueId, right => right.UniqueId); } return sql; diff --git a/src/Umbraco.Core/Services/ILocalizationService.cs b/src/Umbraco.Core/Services/ILocalizationService.cs index afac56f6f8..49eb8f266d 100644 --- a/src/Umbraco.Core/Services/ILocalizationService.cs +++ b/src/Umbraco.Core/Services/ILocalizationService.cs @@ -15,6 +15,15 @@ namespace Umbraco.Core.Services //Add/Set Text (Insert/Update) //Remove Text (in translation) + /// + /// Adds or updates a translation for a dictionary item and language + /// + /// + /// + /// + /// + void AddOrUpdateDictionaryValue(IDictionaryItem item, ILanguage language, string value); + /// /// Creates and saves a new dictionary item and assigns a value to all languages if defaultValue is specified. /// diff --git a/src/Umbraco.Core/Services/LocalizationService.cs b/src/Umbraco.Core/Services/LocalizationService.cs index dafbc6191f..65229dcb74 100644 --- a/src/Umbraco.Core/Services/LocalizationService.cs +++ b/src/Umbraco.Core/Services/LocalizationService.cs @@ -44,6 +44,35 @@ namespace Umbraco.Core.Services _uowProvider = provider; } + /// + /// Adds or updates a translation for a dictionary item and language + /// + /// + /// + /// + /// + /// + /// This does not save the item, that needs to be done explicitly + /// + public void AddOrUpdateDictionaryValue(IDictionaryItem item, ILanguage language, string value) + { + if (item == null) throw new ArgumentNullException("item"); + if (language == null) throw new ArgumentNullException("language"); + + var existing = item.Translations.FirstOrDefault(x => x.Language.Id == language.Id); + if (existing != null) + { + existing.Value = value; + } + else + { + item.Translations = new List(item.Translations) + { + new DictionaryTranslation(language, value) + }; + } + } + /// /// Creates and saves a new dictionary item and assigns a value to all languages if defaultValue is specified. /// diff --git a/src/Umbraco.Tests/Persistence/Repositories/DictionaryRepositoryTest.cs b/src/Umbraco.Tests/Persistence/Repositories/DictionaryRepositoryTest.cs index 5ee689c9a4..aa6bbdcfe2 100644 --- a/src/Umbraco.Tests/Persistence/Repositories/DictionaryRepositoryTest.cs +++ b/src/Umbraco.Tests/Persistence/Repositories/DictionaryRepositoryTest.cs @@ -55,16 +55,53 @@ namespace Umbraco.Tests.Persistence.Repositories LanguageRepository languageRepository; using (var repository = CreateRepository(unitOfWork, out languageRepository)) { - // Act - var dictionaryItem = repository.Get(1); + var dictionaryItem = (IDictionaryItem)new DictionaryItem("Testing1235") + { + Translations = new List + { + new DictionaryTranslation(ServiceContext.LocalizationService.GetLanguageByCultureCode("en-US"), "Hello world") + } + }; + + repository.AddOrUpdate(dictionaryItem); + unitOfWork.Commit(); + + //re-get + dictionaryItem = repository.Get(dictionaryItem.Id); + // Assert Assert.That(dictionaryItem, Is.Not.Null); - Assert.That(dictionaryItem.ItemKey, Is.EqualTo("Read More")); + Assert.That(dictionaryItem.ItemKey, Is.EqualTo("Testing1235")); Assert.That(dictionaryItem.Translations.Any(), Is.True); Assert.That(dictionaryItem.Translations.Any(x => x == null), Is.False); - Assert.That(dictionaryItem.Translations.First().Value, Is.EqualTo("Read More")); - Assert.That(dictionaryItem.Translations.Last().Value, Is.EqualTo("Læs mere")); + Assert.That(dictionaryItem.Translations.First().Value, Is.EqualTo("Hello world")); + } + + } + + [Test] + public void Can_Perform_Get_On_DictionaryRepository_When_No_Language_Assigned() + { + // Arrange + var provider = new PetaPocoUnitOfWorkProvider(Logger); + var unitOfWork = provider.GetUnitOfWork(); + LanguageRepository languageRepository; + using (var repository = CreateRepository(unitOfWork, out languageRepository)) + { + var dictionaryItem = (IDictionaryItem) new DictionaryItem("Testing1235"); + + repository.AddOrUpdate(dictionaryItem); + unitOfWork.Commit(); + + //re-get + dictionaryItem = repository.Get(dictionaryItem.Id); + + + // Assert + Assert.That(dictionaryItem, Is.Not.Null); + Assert.That(dictionaryItem.ItemKey, Is.EqualTo("Testing1235")); + Assert.That(dictionaryItem.Translations.Any(), Is.False); } } diff --git a/src/Umbraco.Tests/Services/LocalizationServiceTests.cs b/src/Umbraco.Tests/Services/LocalizationServiceTests.cs index 32ffb7a305..a4dcaff25b 100644 --- a/src/Umbraco.Tests/Services/LocalizationServiceTests.cs +++ b/src/Umbraco.Tests/Services/LocalizationServiceTests.cs @@ -190,6 +190,10 @@ namespace Umbraco.Tests.Services var item = ServiceContext.LocalizationService.CreateDictionaryItemWithIdentity( "Testing12345", null, "Hellooooo"); + //re-get + item = ServiceContext.LocalizationService.GetDictionaryItemById(item.Id); + + Assert.IsNotNull(item); Assert.Greater(item.Id, 0); Assert.IsTrue(item.HasIdentity); Assert.AreEqual(new Guid(Constants.Conventions.Localization.DictionaryItemRootId), item.ParentId); @@ -208,35 +212,40 @@ namespace Umbraco.Tests.Services { var english = ServiceContext.LocalizationService.GetLanguageByIsoCode("en-US"); - var item = (IDictionaryItem)new DictionaryItem("Testing123") - { - Translations = new List - { - new DictionaryTranslation(english, "Hello world") - } - }; + var item = (IDictionaryItem) new DictionaryItem("Testing123"); ServiceContext.LocalizationService.Save(item); //re-get item = ServiceContext.LocalizationService.GetDictionaryItemById(item.Id); - var newTranslations = new List(item.Translations) + item.Translations = new List + { + new DictionaryTranslation(english, "Hello world") + }; + + ServiceContext.LocalizationService.Save(item); + + Assert.AreEqual(1, item.Translations.Count()); + foreach (var translation in item.Translations) + { + Assert.AreEqual("Hello world", translation.Value); + } + + item.Translations = new List(item.Translations) { new DictionaryTranslation( ServiceContext.LocalizationService.GetLanguageByIsoCode("en-GB"), "My new value") }; - item.Translations = newTranslations; ServiceContext.LocalizationService.Save(item); + //re-get item = ServiceContext.LocalizationService.GetDictionaryItemById(item.Id); Assert.AreEqual(2, item.Translations.Count()); - foreach (var translation in item.Translations) - { - Assert.AreEqual(translation.Language.CultureName == "en-US" ? "Hello world" : "My new value", translation.Value); - } + Assert.AreEqual("Hello world", item.Translations.First().Value); + Assert.AreEqual("My new value", item.Translations.Last().Value); } [Test] diff --git a/src/umbraco.cms/businesslogic/Dictionary.cs b/src/umbraco.cms/businesslogic/Dictionary.cs index 745d38de4a..225d49bfc7 100644 --- a/src/umbraco.cms/businesslogic/Dictionary.cs +++ b/src/umbraco.cms/businesslogic/Dictionary.cs @@ -17,11 +17,7 @@ using Language = umbraco.cms.businesslogic.language.Language; namespace umbraco.cms.businesslogic { - /// - /// The Dictionary is used for storing and retrieving language translated textpieces in Umbraco. It uses - /// umbraco.cms.businesslogic.language.Item class as storage and can be used from the public website of umbraco - /// all text are cached in memory. - /// + [Obsolete("Obsolete, Umbraco.Core.Services.ILocalizationService")] public class Dictionary { @@ -30,6 +26,7 @@ namespace umbraco.cms.businesslogic //private static readonly ConcurrentDictionary DictionaryItems = new ConcurrentDictionary(); private static readonly Guid TopLevelParent = new Guid(Constants.Conventions.Localization.DictionaryItemRootId); + [Obsolete("Obsolete, For querying the database use the new UmbracoDatabase object ApplicationContext.Current.DatabaseContext.Database")] protected static ISqlHelper SqlHelper { get { return Application.SqlHelper; } @@ -334,11 +331,11 @@ namespace umbraco.cms.businesslogic public void setValue(int languageId, string value) { - foreach (var translation in _dictionaryItem.Translations.Where(x => x.Language.Id == languageId)) - { - translation.Value = value; - } - + ApplicationContext.Current.Services.LocalizationService.AddOrUpdateDictionaryValue( + _dictionaryItem, + ApplicationContext.Current.Services.LocalizationService.GetLanguageById(languageId), + value); + //if (Item.hasText(_dictionaryItem.Key, languageId)) // Item.setText(languageId, _dictionaryItem.Key, value); //else