From b01721c2a99837de91d9fbd0f75c8e90fcc93094 Mon Sep 17 00:00:00 2001 From: Morten Christensen Date: Fri, 10 Jan 2014 13:08:33 +0100 Subject: [PATCH] Adding Export of DictionaryItems including test Conflicts: src/Umbraco.Core/Services/PackagingService.cs src/Umbraco.Tests/Services/PackagingServiceTests.cs --- src/Umbraco.Core/Services/PackagingService.cs | 36 ++++++++ .../Services/PackagingServiceTests.cs | 87 +++++++++++++------ 2 files changed, 95 insertions(+), 28 deletions(-) diff --git a/src/Umbraco.Core/Services/PackagingService.cs b/src/Umbraco.Core/Services/PackagingService.cs index 4bd8748aa3..db04a36553 100644 --- a/src/Umbraco.Core/Services/PackagingService.cs +++ b/src/Umbraco.Core/Services/PackagingService.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text.RegularExpressions; +using System.Xml; using System.Xml.Linq; using Umbraco.Core.Configuration; using Umbraco.Core.Events; @@ -946,6 +947,40 @@ namespace Umbraco.Core.Services #endregion #region Dictionary Items + + public XElement Export(IEnumerable dictionaryItem, bool includeChildren = true) + { + var xml = new XElement("DictionaryItems"); + foreach (var item in dictionaryItem) + { + xml.Add(Export(item, includeChildren)); + } + return xml; + } + + private XElement Export(IDictionaryItem dictionaryItem, bool includeChildren) + { + var xml = new XElement("DictionaryItem", new XAttribute("Key", dictionaryItem.ItemKey)); + foreach (var translation in dictionaryItem.Translations) + { + xml.Add(new XElement("Value", + new XAttribute("LanguageId", translation.Language.Id), + new XAttribute("LanguageCultureAlias", translation.Language.IsoCode), + new XCData(translation.Value))); + } + + if (includeChildren) + { + var children = _localizationService.GetDictionaryItemChildren(dictionaryItem.Key); + foreach (var child in children) + { + xml.Add(Export(child, true)); + } + } + + return xml; + } + public IEnumerable ImportDictionaryItems(XElement dictionaryItemElementList) { var languages = _localizationService.GetAllLanguages().ToList(); @@ -1014,6 +1049,7 @@ namespace Umbraco.Core.Services var translation = new DictionaryTranslation(language, valueElement.Value); translations.Add(translation); } + #endregion #region Files diff --git a/src/Umbraco.Tests/Services/PackagingServiceTests.cs b/src/Umbraco.Tests/Services/PackagingServiceTests.cs index 1ed7807403..1d4b2ae09f 100644 --- a/src/Umbraco.Tests/Services/PackagingServiceTests.cs +++ b/src/Umbraco.Tests/Services/PackagingServiceTests.cs @@ -1,41 +1,72 @@ using System; +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Models; +using Umbraco.Tests.Services.Importing; using Umbraco.Tests.TestHelpers.Entities; namespace Umbraco.Tests.Services { - //[TestFixture] - //public class PackagingServiceTests : BaseServiceTest - //{ - // [Test] - // public void Export_Content() - // { - // var yesNo = DataTypesResolver.Current.GetById(new Guid(Constants.PropertyEditors.TrueFalse)); - // var txtField = DataTypesResolver.Current.GetById(new Guid(Constants.PropertyEditors.Textbox)); + [TestFixture] + public class PackagingServiceTests : BaseServiceTest + { + [SetUp] + public override void Initialize() + { + base.Initialize(); + } - // var contentWithDataType = MockedContentTypes.CreateSimpleContentType( - // "test", - // "Test", - // new PropertyTypeCollection( - // new PropertyType[] - // { - // new PropertyType(new DataTypeDefinition(-1, txtField.Id) - // { - // Name = "Testing Textfield", DatabaseType = DataTypeDatabaseType.Ntext - // }), - // new PropertyType(new DataTypeDefinition(-1, yesNo.Id) - // { - // Name = "Testing intfield", DatabaseType = DataTypeDatabaseType.Integer - // }) - // })); + [TearDown] + public override void TearDown() + { + base.TearDown(); + } - // var content = MockedContent.CreateSimpleContent(contentWithDataType); - // content.Name = "Test"; + [Test] + public void PackagingService_Can_Export_DictionaryItems() + { + // Arrange + CreateTestData(); + var dictionaryItem = ServiceContext.LocalizationService.GetDictionaryItemByKey("Parent"); - // var exported = ServiceContext.PackagingService.Export(content); + var newPackageXml = XElement.Parse(ImportResources.Dictionary_Package); + var dictionaryItemsElement = newPackageXml.Elements("DictionaryItems").First(); - // } - //} + // Act + var xml = ServiceContext.PackagingService.Export(new []{dictionaryItem}); + + // Assert + Assert.That(xml.ToString(), Is.EqualTo(dictionaryItemsElement.ToString())); + } + + public void CreateTestData() + { + var languageNbNo = new Language("nb-NO") { CultureName = "nb-NO" }; + ServiceContext.LocalizationService.Save(languageNbNo); + + var languageEnGb = new Language("en-GB") { CultureName = "en-GB" }; + ServiceContext.LocalizationService.Save(languageEnGb); + + var parentItem = new DictionaryItem("Parent"); + var parentTranslations = new List + { + new DictionaryTranslation(languageNbNo, "ForelderVerdi"), + new DictionaryTranslation(languageEnGb, "ParentValue") + }; + parentItem.Translations = parentTranslations; + ServiceContext.LocalizationService.Save(parentItem); + + var childItem = new DictionaryItem(parentItem.Key, "Child"); + var childTranslations = new List + { + new DictionaryTranslation(languageNbNo, "BarnVerdi"), + new DictionaryTranslation(languageEnGb, "ChildValue") + }; + childItem.Translations = childTranslations; + ServiceContext.LocalizationService.Save(childItem); + } + } } \ No newline at end of file