Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Core/Packaging/CreatedPackagesRepositoryTests.cs

340 lines
13 KiB
C#
Raw Normal View History

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
2019-01-10 18:55:24 +11:00
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Xml.Linq;
using NUnit.Framework;
2021-08-09 13:19:37 +02:00
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Packaging;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Tests.Common.Builders;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
using Umbraco.Extensions;
using File = System.IO.File;
2019-01-10 18:55:24 +11:00
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Packaging
2019-01-10 18:55:24 +11:00
{
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerFixture)]
public class CreatedPackagesRepositoryTests : UmbracoIntegrationTest
2019-01-10 18:55:24 +11:00
{
private Guid _testBaseFolder;
2020-10-05 15:04:41 +02:00
[SetUp]
public void SetupTestData() => _testBaseFolder = Guid.NewGuid();
2019-01-10 18:55:24 +11:00
2020-10-06 14:16:29 +02:00
[TearDown]
public void DeleteTestFolder() =>
Directory.Delete(HostingEnvironment.MapPathContentRoot("~/" + _testBaseFolder), true);
2021-08-09 06:28:26 +02:00
private IContentService ContentService => GetRequiredService<IContentService>();
private IContentTypeService ContentTypeService => GetRequiredService<IContentTypeService>();
private IDataTypeService DataTypeService => GetRequiredService<IDataTypeService>();
private IFileService FileService => GetRequiredService<IFileService>();
private IMacroService MacroService => GetRequiredService<IMacroService>();
private ILocalizationService LocalizationService => GetRequiredService<ILocalizationService>();
private IEntityXmlSerializer EntityXmlSerializer => GetRequiredService<IEntityXmlSerializer>();
private IHostingEnvironment HostingEnvironment => GetRequiredService<IHostingEnvironment>();
private IMediaService MediaService => GetRequiredService<IMediaService>();
private IMediaTypeService MediaTypeService => GetRequiredService<IMediaTypeService>();
private MediaFileManager MediaFileManager => GetRequiredService<MediaFileManager>();
private FileSystems FileSystems => GetRequiredService<FileSystems>();
public ICreatedPackagesRepository PackageBuilder => new PackagesRepository(
ContentService,
ContentTypeService,
DataTypeService,
FileService,
MacroService,
LocalizationService,
HostingEnvironment,
EntityXmlSerializer,
2020-09-21 21:06:24 +02:00
Microsoft.Extensions.Options.Options.Create(new GlobalSettings()),
MediaService,
MediaTypeService,
MediaFileManager,
FileSystems,
"createdPackages.config",
// temp paths
2019-01-10 18:55:24 +11:00
tempFolderPath: "~/" + _testBaseFolder + "/temp",
packagesFolderPath: "~/" + _testBaseFolder + "/packages",
mediaFolderPath: "~/" + _testBaseFolder + "/media");
[Test]
public void Delete()
{
var def1 = new PackageDefinition
{
Name = "test",
};
bool result = PackageBuilder.SavePackage(def1);
2019-01-10 18:55:24 +11:00
Assert.IsTrue(result);
PackageBuilder.Delete(def1.Id);
def1 = PackageBuilder.GetById(def1.Id);
Assert.IsNull(def1);
}
[Test]
public void Create_New()
{
var def1 = new PackageDefinition
{
Name = "test",
};
bool result = PackageBuilder.SavePackage(def1);
2019-01-10 18:55:24 +11:00
Assert.IsTrue(result);
Assert.AreEqual(1, def1.Id);
Assert.AreNotEqual(default(Guid).ToString(), def1.PackageId);
var def2 = new PackageDefinition
{
Name = "test2",
2019-01-10 18:55:24 +11:00
};
result = PackageBuilder.SavePackage(def2);
Assert.IsTrue(result);
Assert.AreEqual(2, def2.Id);
Assert.AreNotEqual(default(Guid).ToString(), def2.PackageId);
}
[Test]
public void Update_Not_Found()
{
var def = new PackageDefinition
{
Id = 3, // doesn't exist
2019-01-10 18:55:24 +11:00
Name = "test",
};
bool result = PackageBuilder.SavePackage(def);
2019-01-10 18:55:24 +11:00
Assert.IsFalse(result);
}
[Test]
public void Update()
{
var def = new PackageDefinition
{
Name = "test",
};
bool result = PackageBuilder.SavePackage(def);
2021-08-18 14:20:56 +02:00
//Update values and save
2019-01-10 18:55:24 +11:00
def.Name = "updated";
2021-08-18 14:20:56 +02:00
def.ContentNodeId = "test";
def.Languages.Add("Danish");
def.Languages.Add("English");
def.Scripts.Add("TestScript1");
def.Scripts.Add("TestScript2");
2019-01-10 18:55:24 +11:00
result = PackageBuilder.SavePackage(def);
Assert.IsTrue(result);
// re-get
2019-01-10 18:55:24 +11:00
def = PackageBuilder.GetById(def.Id);
Assert.AreEqual("updated", def.Name);
2021-08-18 14:20:56 +02:00
Assert.Multiple(() =>
{
Assert.AreEqual("updated", def.Name);
Assert.AreEqual("test", def.ContentNodeId);
Assert.AreEqual(2, def.Languages.Count());
Assert.AreEqual(2, def.Scripts.Count());
Assert.AreEqual(0, def.DataTypes.Count());
Assert.AreEqual(0, def.DictionaryItems.Count());
Assert.AreEqual(0, def.DocumentTypes.Count());
Assert.AreEqual(0, def.Macros.Count());
Assert.AreEqual(0, def.MediaTypes.Count());
Assert.AreEqual(0, def.MediaUdis.Count());
Assert.AreEqual(0, def.PartialViews.Count());
Assert.AreEqual(0, def.Stylesheets.Count());
Assert.AreEqual(0, def.Templates.Count());
});
2019-01-10 18:55:24 +11:00
}
Fixes packager to ensure the GUIDs are used for all entities where possible (#10477) * Clean up and changes to backoffice for the nuget only packages * temp commit of package logic removal * Lots of package code cleanup and removal * Removes old package data from the test package xml * Updates packaging code to take in XDocument instead of a file since we'll not be dealing with files, starts creating expressions for the package migrations scripting. * fixing tests * Fixes runtime state and boot failed middleware so that it actually runs. Separates out unattended install/upgrade into notification handlers. * Gets unattended package migrations working and running * Gets embedded package.xml resources able to install from package migration. * Implements automatic package migrations for package that just declare an xml data manifest. * fix build * small cleanups * fix build * adds some tests * Fix export test * Fix newlines in test for linux * Typo * removes old todos and updates AutomaticPackgeMigrationPlan to use getter with backing field. * Update dictionary package data to use GUID * Ensures macros are packaged and used with their GUID * Ensures the GUID for doc types and media types remains consistent for package installation based on what is in the xml. * fix automatic migrations to not validate initial state, fixes packaging GUIDs for multiple entities. * Added guids to embedded test packages (Some tests are still failing) * Fix one more test * Fixes up Key vs Id, moves tests to correct namespace, fix tests * Fixes Dictionary packaging to ensure an xml hierarchy * Fixes tests * fixes package xml Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-06-17 16:15:38 +10:00
[Test]
public void GivenNestedDictionaryItems_WhenPackageExported_ThenTheXmlIsNested()
{
var parent = new DictionaryItem("Parent")
{
2021-08-09 06:28:26 +02:00
Key = Guid.NewGuid()
Fixes packager to ensure the GUIDs are used for all entities where possible (#10477) * Clean up and changes to backoffice for the nuget only packages * temp commit of package logic removal * Lots of package code cleanup and removal * Removes old package data from the test package xml * Updates packaging code to take in XDocument instead of a file since we'll not be dealing with files, starts creating expressions for the package migrations scripting. * fixing tests * Fixes runtime state and boot failed middleware so that it actually runs. Separates out unattended install/upgrade into notification handlers. * Gets unattended package migrations working and running * Gets embedded package.xml resources able to install from package migration. * Implements automatic package migrations for package that just declare an xml data manifest. * fix build * small cleanups * fix build * adds some tests * Fix export test * Fix newlines in test for linux * Typo * removes old todos and updates AutomaticPackgeMigrationPlan to use getter with backing field. * Update dictionary package data to use GUID * Ensures macros are packaged and used with their GUID * Ensures the GUID for doc types and media types remains consistent for package installation based on what is in the xml. * fix automatic migrations to not validate initial state, fixes packaging GUIDs for multiple entities. * Added guids to embedded test packages (Some tests are still failing) * Fix one more test * Fixes up Key vs Id, moves tests to correct namespace, fix tests * Fixes Dictionary packaging to ensure an xml hierarchy * Fixes tests * fixes package xml Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-06-17 16:15:38 +10:00
};
LocalizationService.Save(parent);
var child1 = new DictionaryItem(parent.Key, "Child1")
{
Key = Guid.NewGuid()
};
LocalizationService.Save(child1);
var child2 = new DictionaryItem(child1.Key, "Child2")
{
Key = Guid.NewGuid()
};
LocalizationService.Save(child2);
var child3 = new DictionaryItem(child2.Key, "Child3")
{
Key = Guid.NewGuid()
};
LocalizationService.Save(child3);
var child4 = new DictionaryItem(child3.Key, "Child4")
{
Key = Guid.NewGuid()
};
LocalizationService.Save(child4);
var def = new PackageDefinition
{
Name = "test",
// put these out of order to ensure that it doesn't matter.
DictionaryItems = new List<string>
{
child2.Id.ToString(),
child1.Id.ToString(),
// we are missing 3 here so 4 will be orphaned and end up in the root
child4.Id.ToString(),
parent.Id.ToString()
}
};
PackageBuilder.SavePackage(def);
2021-08-09 06:28:26 +02:00
Fixes packager to ensure the GUIDs are used for all entities where possible (#10477) * Clean up and changes to backoffice for the nuget only packages * temp commit of package logic removal * Lots of package code cleanup and removal * Removes old package data from the test package xml * Updates packaging code to take in XDocument instead of a file since we'll not be dealing with files, starts creating expressions for the package migrations scripting. * fixing tests * Fixes runtime state and boot failed middleware so that it actually runs. Separates out unattended install/upgrade into notification handlers. * Gets unattended package migrations working and running * Gets embedded package.xml resources able to install from package migration. * Implements automatic package migrations for package that just declare an xml data manifest. * fix build * small cleanups * fix build * adds some tests * Fix export test * Fix newlines in test for linux * Typo * removes old todos and updates AutomaticPackgeMigrationPlan to use getter with backing field. * Update dictionary package data to use GUID * Ensures macros are packaged and used with their GUID * Ensures the GUID for doc types and media types remains consistent for package installation based on what is in the xml. * fix automatic migrations to not validate initial state, fixes packaging GUIDs for multiple entities. * Added guids to embedded test packages (Some tests are still failing) * Fix one more test * Fixes up Key vs Id, moves tests to correct namespace, fix tests * Fixes Dictionary packaging to ensure an xml hierarchy * Fixes tests * fixes package xml Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-06-17 16:15:38 +10:00
string packageXmlPath = PackageBuilder.ExportPackage(def);
2021-08-09 13:19:37 +02:00
using (var packageXmlStream = File.OpenRead(packageXmlPath))
Fixes packager to ensure the GUIDs are used for all entities where possible (#10477) * Clean up and changes to backoffice for the nuget only packages * temp commit of package logic removal * Lots of package code cleanup and removal * Removes old package data from the test package xml * Updates packaging code to take in XDocument instead of a file since we'll not be dealing with files, starts creating expressions for the package migrations scripting. * fixing tests * Fixes runtime state and boot failed middleware so that it actually runs. Separates out unattended install/upgrade into notification handlers. * Gets unattended package migrations working and running * Gets embedded package.xml resources able to install from package migration. * Implements automatic package migrations for package that just declare an xml data manifest. * fix build * small cleanups * fix build * adds some tests * Fix export test * Fix newlines in test for linux * Typo * removes old todos and updates AutomaticPackgeMigrationPlan to use getter with backing field. * Update dictionary package data to use GUID * Ensures macros are packaged and used with their GUID * Ensures the GUID for doc types and media types remains consistent for package installation based on what is in the xml. * fix automatic migrations to not validate initial state, fixes packaging GUIDs for multiple entities. * Added guids to embedded test packages (Some tests are still failing) * Fix one more test * Fixes up Key vs Id, moves tests to correct namespace, fix tests * Fixes Dictionary packaging to ensure an xml hierarchy * Fixes tests * fixes package xml Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-06-17 16:15:38 +10:00
{
2021-08-09 13:19:37 +02:00
var packageXml = XDocument.Load(packageXmlStream);
2021-07-08 13:58:27 -06:00
var dictionaryItems = packageXml.Root.Element("DictionaryItems");
Fixes packager to ensure the GUIDs are used for all entities where possible (#10477) * Clean up and changes to backoffice for the nuget only packages * temp commit of package logic removal * Lots of package code cleanup and removal * Removes old package data from the test package xml * Updates packaging code to take in XDocument instead of a file since we'll not be dealing with files, starts creating expressions for the package migrations scripting. * fixing tests * Fixes runtime state and boot failed middleware so that it actually runs. Separates out unattended install/upgrade into notification handlers. * Gets unattended package migrations working and running * Gets embedded package.xml resources able to install from package migration. * Implements automatic package migrations for package that just declare an xml data manifest. * fix build * small cleanups * fix build * adds some tests * Fix export test * Fix newlines in test for linux * Typo * removes old todos and updates AutomaticPackgeMigrationPlan to use getter with backing field. * Update dictionary package data to use GUID * Ensures macros are packaged and used with their GUID * Ensures the GUID for doc types and media types remains consistent for package installation based on what is in the xml. * fix automatic migrations to not validate initial state, fixes packaging GUIDs for multiple entities. * Added guids to embedded test packages (Some tests are still failing) * Fix one more test * Fixes up Key vs Id, moves tests to correct namespace, fix tests * Fixes Dictionary packaging to ensure an xml hierarchy * Fixes tests * fixes package xml Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-06-17 16:15:38 +10:00
Assert.IsNotNull(dictionaryItems);
var rootItems = dictionaryItems.Elements("DictionaryItem").ToList();
Assert.AreEqual(2, rootItems.Count);
Assert.AreEqual("Child4", rootItems[0].AttributeValue<string>("Name"));
Assert.AreEqual("Parent", rootItems[1].AttributeValue<string>("Name"));
var children = rootItems[1].Elements("DictionaryItem").ToList();
Assert.AreEqual(1, children.Count);
Assert.AreEqual("Child1", children[0].AttributeValue<string>("Name"));
children = children[0].Elements("DictionaryItem").ToList();
Assert.AreEqual(1, children.Count);
Assert.AreEqual("Child2", children[0].AttributeValue<string>("Name"));
}
}
2019-01-10 18:55:24 +11:00
[Test]
2021-08-09 13:19:37 +02:00
public void Export_Zip()
2019-01-10 18:55:24 +11:00
{
2021-08-09 13:19:37 +02:00
var mt = MediaTypeBuilder.CreateImageMediaType("testImage");
MediaTypeService.Save(mt);
var m1 = MediaBuilder.CreateMediaFile(mt, -1);
MediaService.Save(m1);
//Ensure a file exist
var fullPath = HostingEnvironment.MapPathWebRoot(m1.Properties[Constants.Conventions.Media.File].GetValue().ToString());
using (StreamWriter file1 = File.CreateText(fullPath))
{
file1.WriteLine("hello");
}
2019-01-10 18:55:24 +11:00
var def = new PackageDefinition
{
Name = "test",
2021-08-09 13:19:37 +02:00
MediaUdis = new List<GuidUdi>(){m1.GetUdi()}
2019-01-10 18:55:24 +11:00
};
2021-08-09 13:19:37 +02:00
bool result = PackageBuilder.SavePackage(def);
2019-01-10 18:55:24 +11:00
Assert.IsTrue(result);
Assert.IsTrue(def.PackagePath.IsNullOrWhiteSpace());
string packageXmlPath = PackageBuilder.ExportPackage(def);
2019-01-10 18:55:24 +11:00
def = PackageBuilder.GetById(def.Id); // re-get
2019-01-10 18:55:24 +11:00
Assert.IsNotNull(def.PackagePath);
2021-07-08 13:58:27 -06:00
using (FileStream packageZipStream = File.OpenRead(packageXmlPath))
using (ZipArchive zipArchive = PackageMigrationResource.GetPackageDataManifest(packageZipStream, out XDocument packageXml))
2019-01-10 18:55:24 +11:00
{
2021-08-18 14:20:56 +02:00
string test = "test-file.txt";
Assert.Multiple(() =>
{
2021-08-18 15:45:33 +02:00
var mediaEntry = zipArchive.GetEntry("media/media/test-file.txt");
2021-08-18 14:20:56 +02:00
Assert.AreEqual("umbPackage", packageXml.Root.Name.ToString());
2021-08-18 15:45:33 +02:00
Assert.IsNotNull(mediaEntry);
Assert.AreEqual(test, mediaEntry.Name);
2021-08-18 14:20:56 +02:00
Assert.IsNotNull(zipArchive.GetEntry("package.xml"));
Assert.AreEqual(
$"<MediaItems><MediaSet><testImage id=\"{m1.Id}\" key=\"{m1.Key}\" parentID=\"-1\" level=\"1\" creatorID=\"-1\" sortOrder=\"0\" createDate=\"{m1.CreateDate.ToString("s")}\" updateDate=\"{m1.UpdateDate.ToString("s")}\" nodeName=\"Test File\" urlName=\"test-file\" path=\"{m1.Path}\" isDoc=\"\" nodeType=\"{mt.Id}\" nodeTypeAlias=\"testImage\" writerName=\"\" writerID=\"0\" udi=\"{m1.GetUdi()}\" mediaFilePath=\"/media/test-file.txt\"><umbracoFile><![CDATA[/media/test-file.txt]]></umbracoFile><umbracoBytes><![CDATA[100]]></umbracoBytes><umbracoExtension><![CDATA[png]]></umbracoExtension></testImage></MediaSet></MediaItems>",
packageXml.Element("umbPackage").Element("MediaItems").ToString(SaveOptions.DisableFormatting));
Assert.AreEqual(2, zipArchive.Entries.Count());
Assert.AreEqual(ZipArchiveMode.Read, zipArchive.Mode);
Assert.IsNull(packageXml.DocumentType);
Assert.IsNull(packageXml.NextNode);
Assert.IsNull(packageXml.Parent);
Assert.IsNull(packageXml.PreviousNode);
});
2021-08-09 13:19:37 +02:00
}
}
[Test]
public void Export_Xml()
{
var template = TemplateBuilder.CreateTextPageTemplate();
FileService.SaveTemplate(template);
var def = new PackageDefinition
{
Name = "test",
Templates = new []{template.Id.ToString()}
};
bool result = PackageBuilder.SavePackage(def);
Assert.IsTrue(result);
Assert.IsTrue(def.PackagePath.IsNullOrWhiteSpace());
string packageXmlPath = PackageBuilder.ExportPackage(def); // Get
def = PackageBuilder.GetById(def.Id); // re-get
Assert.IsNotNull(def.PackagePath);
using (var packageXmlStream = File.OpenRead(packageXmlPath))
{
var xml = XDocument.Load(packageXmlStream);
2021-08-18 14:20:56 +02:00
Assert.Multiple(() =>
{
Assert.AreEqual("umbPackage", xml.Root.Name.ToString());
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));
2021-08-18 14:20:56 +02:00
Assert.IsNull(xml.DocumentType);
Assert.IsNull(xml.Parent);
Assert.IsNull(xml.NextNode);
Assert.IsNull(xml.PreviousNode);
});
2019-01-10 18:55:24 +11:00
}
}
}
}