Fixes up a left join with dictionary -> language items, adds another couple of unit tests adds another method to the ILocalizationService

This commit is contained in:
Shannon
2015-01-12 19:39:30 +11:00
parent ffd4e9507b
commit fb3b99a49a
6 changed files with 110 additions and 29 deletions

View File

@@ -109,7 +109,7 @@ namespace Umbraco.Core.Persistence.Repositories
{
sql.Select("*")
.From<DictionaryDto>()
.InnerJoin<LanguageTextDto>()
.LeftJoin<LanguageTextDto>()
.On<DictionaryDto, LanguageTextDto>(left => left.UniqueId, right => right.UniqueId);
}
return sql;

View File

@@ -15,6 +15,15 @@ namespace Umbraco.Core.Services
//Add/Set Text (Insert/Update)
//Remove Text (in translation)
/// <summary>
/// Adds or updates a translation for a dictionary item and language
/// </summary>
/// <param name="item"></param>
/// <param name="language"></param>
/// <param name="value"></param>
/// <returns></returns>
void AddOrUpdateDictionaryValue(IDictionaryItem item, ILanguage language, string value);
/// <summary>
/// Creates and saves a new dictionary item and assigns a value to all languages if defaultValue is specified.
/// </summary>

View File

@@ -44,6 +44,35 @@ namespace Umbraco.Core.Services
_uowProvider = provider;
}
/// <summary>
/// Adds or updates a translation for a dictionary item and language
/// </summary>
/// <param name="item"></param>
/// <param name="language"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <remarks>
/// This does not save the item, that needs to be done explicitly
/// </remarks>
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<IDictionaryTranslation>(item.Translations)
{
new DictionaryTranslation(language, value)
};
}
}
/// <summary>
/// Creates and saves a new dictionary item and assigns a value to all languages if defaultValue is specified.
/// </summary>

View File

@@ -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<IDictionaryTranslation>
{
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);
}
}

View File

@@ -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<IDictionaryTranslation>
{
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<IDictionaryTranslation>(item.Translations)
item.Translations = new List<IDictionaryTranslation>
{
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<IDictionaryTranslation>(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]

View File

@@ -17,11 +17,7 @@ using Language = umbraco.cms.businesslogic.language.Language;
namespace umbraco.cms.businesslogic
{
/// <summary>
/// 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.
/// </summary>
[Obsolete("Obsolete, Umbraco.Core.Services.ILocalizationService")]
public class Dictionary
{
@@ -30,6 +26,7 @@ namespace umbraco.cms.businesslogic
//private static readonly ConcurrentDictionary<string, DictionaryItem> DictionaryItems = new ConcurrentDictionary<string, DictionaryItem>();
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