Skip to content

Commit fee3d07

Browse files
authored
Serializes key for templates in package definition and imports if ava… (#11163)
1 parent e5ce9de commit fee3d07

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

src/Umbraco.Infrastructure/Packaging/PackageDataInstallation.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,15 @@ public IReadOnlyList<ITemplate> ImportTemplates(IReadOnlyCollection<XElement> te
15581558
var masterElement = templateElement.Element("Master");
15591559

15601560
var existingTemplate = _fileService.GetTemplate(alias) as Template;
1561+
15611562
var template = existingTemplate ?? new Template(_shortStringHelper, templateName, alias);
1563+
1564+
// For new templates, use the serialized key if avaialble.
1565+
if (existingTemplate == null && Guid.TryParse(templateElement.Element("Key")?.Value, out var key))
1566+
{
1567+
template.Key = key;
1568+
}
1569+
15621570
template.Content = design;
15631571

15641572
if (masterElement != null && string.IsNullOrEmpty((string)masterElement) == false)

src/Umbraco.Infrastructure/Services/Implement/EntityXmlSerializer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Globalization;
4-
using System.IO;
54
using System.Linq;
65
using System.Net;
76
using System.Xml.Linq;
8-
using Umbraco.Cms.Core.IO;
97
using Umbraco.Cms.Core.Models;
108
using Umbraco.Cms.Core.PropertyEditors;
119
using Umbraco.Cms.Core.Serialization;
@@ -319,6 +317,7 @@ public XElement Serialize(ITemplate template)
319317
{
320318
var xml = new XElement("Template");
321319
xml.Add(new XElement("Name", template.Name));
320+
xml.Add(new XElement("Key", template.Key));
322321
xml.Add(new XElement("Alias", template.Alias));
323322
xml.Add(new XElement("Design", new XCData(template.Content)));
324323

src/Umbraco.Tests.Integration/Umbraco.Core/Packaging/CreatedPackagesRepositoryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public void Export_Xml()
327327
Assert.Multiple(() =>
328328
{
329329
Assert.AreEqual("umbPackage", xml.Root.Name.ToString());
330-
Assert.AreEqual($"<Templates><Template><Name>Text page</Name><Alias>textPage</Alias><Design><![CDATA[@using Umbraco.Cms.Web.Common.PublishedModels;{Environment.NewLine}@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage{Environment.NewLine}@{{{Environment.NewLine}\tLayout = null;{Environment.NewLine}}}]]></Design></Template></Templates>", xml.Element("umbPackage").Element("Templates").ToString(SaveOptions.DisableFormatting));
330+
Assert.AreEqual($"<Templates><Template><Name>Text page</Name><Key>{template.Key}</Key><Alias>textPage</Alias><Design><![CDATA[@using Umbraco.Cms.Web.Common.PublishedModels;{Environment.NewLine}@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage{Environment.NewLine}@{{{Environment.NewLine}\tLayout = null;{Environment.NewLine}}}]]></Design></Template></Templates>", xml.Element("umbPackage").Element("Templates").ToString(SaveOptions.DisableFormatting));
331331
Assert.IsNull(xml.DocumentType);
332332
Assert.IsNull(xml.Parent);
333333
Assert.IsNull(xml.NextNode);

src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Packaging/PackageDataInstallationTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,34 @@ public void Can_Import_Single_Template()
233233
Assert.That(templates, Is.Not.Null);
234234
Assert.That(templates.Any(), Is.True);
235235
Assert.That(templates.Count(), Is.EqualTo(1));
236+
237+
var template = templates.First();
238+
Assert.AreEqual(template.Name, "Articles");
239+
}
240+
241+
[Test]
242+
public void Can_Import_Single_Template_With_Key()
243+
{
244+
// Arrange
245+
string strXml = ImportResources.StandardMvc_Package;
246+
var xml = XElement.Parse(strXml);
247+
XElement element = xml.Descendants("Templates").First();
248+
249+
var firstTemplateElement = element.Elements("Template").First();
250+
var key = Guid.NewGuid();
251+
firstTemplateElement.Add(new XElement("Key", key));
252+
253+
// Act
254+
IEnumerable<ITemplate> templates = PackageDataInstallation.ImportTemplate(firstTemplateElement, 0);
255+
256+
// Assert
257+
Assert.That(templates, Is.Not.Null);
258+
Assert.That(templates.Any(), Is.True);
259+
Assert.That(templates.Count(), Is.EqualTo(1));
260+
261+
var template = templates.First();
262+
Assert.AreEqual(template.Name, "Articles");
263+
Assert.AreEqual(template.Key, key);
236264
}
237265

238266
[Test]

0 commit comments

Comments
 (0)