2020-12-23 11:35:49 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2020-10-02 14:46:14 +02:00
|
|
|
using System.Linq;
|
|
|
|
|
using NUnit.Framework;
|
2023-01-11 14:40:41 +01:00
|
|
|
using Umbraco.Cms.Core.IO;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
|
using Umbraco.Cms.Tests.Common.Testing;
|
|
|
|
|
using Umbraco.Cms.Tests.Integration.Testing;
|
2020-10-02 14:46:14 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services;
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
|
|
|
|
|
public class FileServiceTests : UmbracoIntegrationTest
|
2020-10-02 14:46:14 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
private IFileService FileService => GetRequiredService<IFileService>();
|
2020-10-02 14:46:14 +02:00
|
|
|
|
2023-01-11 14:40:41 +01:00
|
|
|
[SetUp]
|
|
|
|
|
public void SetUp()
|
|
|
|
|
{
|
|
|
|
|
var fileSystems = GetRequiredService<FileSystems>();
|
|
|
|
|
var viewFileSystem = fileSystems.MvcViewsFileSystem!;
|
|
|
|
|
foreach (var file in viewFileSystem.GetFiles(string.Empty).ToArray())
|
|
|
|
|
{
|
|
|
|
|
viewFileSystem.DeleteFile(file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Create_Template_Then_Assign_Child()
|
|
|
|
|
{
|
|
|
|
|
var child = FileService.CreateTemplateWithIdentity("Child", "child", "test");
|
|
|
|
|
var parent = FileService.CreateTemplateWithIdentity("Parent", "parent", "test");
|
2020-10-02 14:46:14 +02:00
|
|
|
|
2023-01-11 14:40:41 +01:00
|
|
|
child.Content = "Layout = \"Parent.cshtml\";";
|
2022-06-21 08:09:38 +02:00
|
|
|
FileService.SaveTemplate(child);
|
2020-10-02 14:46:14 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
child = FileService.GetTemplate(child.Id);
|
2020-10-02 14:46:14 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual(parent.Alias, child.MasterTemplateAlias);
|
|
|
|
|
}
|
2020-10-02 14:46:14 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Create_Template_With_Child_Then_Unassign()
|
|
|
|
|
{
|
|
|
|
|
var parent = FileService.CreateTemplateWithIdentity("Parent", "parent", "test");
|
2023-01-11 14:40:41 +01:00
|
|
|
var child = FileService.CreateTemplateWithIdentity("Child", "child", "Layout = \"Parent.cshtml\";");
|
2020-10-02 14:46:14 +02:00
|
|
|
|
2023-01-11 14:40:41 +01:00
|
|
|
child = FileService.GetTemplate(child.Id);
|
|
|
|
|
Assert.NotNull(child);
|
|
|
|
|
Assert.AreEqual("parent", child.MasterTemplateAlias);
|
|
|
|
|
|
|
|
|
|
child.Content = "test";
|
2022-06-21 08:09:38 +02:00
|
|
|
FileService.SaveTemplate(child);
|
2020-10-02 14:46:14 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
child = FileService.GetTemplate(child.Id);
|
2023-01-11 14:40:41 +01:00
|
|
|
Assert.NotNull(child);
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual(null, child.MasterTemplateAlias);
|
|
|
|
|
}
|
2020-10-02 14:46:14 +02:00
|
|
|
|
2023-01-11 14:40:41 +01:00
|
|
|
[Test]
|
|
|
|
|
public void Create_Template_With_Child_Then_Reassign()
|
|
|
|
|
{
|
|
|
|
|
var parent = FileService.CreateTemplateWithIdentity("Parent", "parent", "test");
|
|
|
|
|
var parent2 = FileService.CreateTemplateWithIdentity("Parent2", "parent2", "test");
|
|
|
|
|
var child = FileService.CreateTemplateWithIdentity("Child", "child", "Layout = \"Parent.cshtml\";");
|
|
|
|
|
|
|
|
|
|
child = FileService.GetTemplate(child.Id);
|
|
|
|
|
Assert.NotNull(child);
|
|
|
|
|
Assert.AreEqual("parent", child.MasterTemplateAlias);
|
|
|
|
|
|
|
|
|
|
child.Content = "Layout = \"Parent2.cshtml\";";
|
|
|
|
|
FileService.SaveTemplate(child);
|
|
|
|
|
|
|
|
|
|
child = FileService.GetTemplate(child.Id);
|
|
|
|
|
Assert.NotNull(child);
|
|
|
|
|
Assert.AreEqual("parent2", child.MasterTemplateAlias);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Child_Template_Paths_Are_Updated_When_Reassigning_Master()
|
|
|
|
|
{
|
|
|
|
|
var parent = FileService.CreateTemplateWithIdentity("Parent", "parent", "test");
|
|
|
|
|
var parent2 = FileService.CreateTemplateWithIdentity("Parent2", "parent2", "test");
|
|
|
|
|
var child = FileService.CreateTemplateWithIdentity("Child", "child", "Layout = \"Parent.cshtml\";");
|
|
|
|
|
var childOfChild1 = FileService.CreateTemplateWithIdentity("Child1", "child1", "Layout = \"Child.cshtml\";");
|
|
|
|
|
var childOfChild2 = FileService.CreateTemplateWithIdentity("Child2", "child2", "Layout = \"Child.cshtml\";");
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual($"child", childOfChild1.MasterTemplateAlias);
|
|
|
|
|
Assert.AreEqual($"{parent.Path},{child.Id},{childOfChild1.Id}", childOfChild1.Path);
|
|
|
|
|
Assert.AreEqual($"child", childOfChild2.MasterTemplateAlias);
|
|
|
|
|
Assert.AreEqual($"{parent.Path},{child.Id},{childOfChild2.Id}", childOfChild2.Path);
|
|
|
|
|
|
|
|
|
|
child.Content = "Layout = \"Parent2.cshtml\";";
|
|
|
|
|
FileService.SaveTemplate(child);
|
|
|
|
|
|
|
|
|
|
childOfChild1 = FileService.GetTemplate(childOfChild1.Id);
|
|
|
|
|
Assert.NotNull(childOfChild1);
|
|
|
|
|
|
|
|
|
|
childOfChild2 = FileService.GetTemplate(childOfChild2.Id);
|
|
|
|
|
Assert.NotNull(childOfChild2);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual($"child", childOfChild1.MasterTemplateAlias);
|
|
|
|
|
Assert.AreEqual($"{parent2.Path},{child.Id},{childOfChild1.Id}", childOfChild1.Path);
|
|
|
|
|
Assert.AreEqual($"child", childOfChild2.MasterTemplateAlias);
|
|
|
|
|
Assert.AreEqual($"{parent2.Path},{child.Id},{childOfChild2.Id}", childOfChild2.Path);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Can_Query_Template_Children()
|
|
|
|
|
{
|
|
|
|
|
var parent = FileService.CreateTemplateWithIdentity("Parent", "parent", "test");
|
2023-01-11 14:40:41 +01:00
|
|
|
var child1 = FileService.CreateTemplateWithIdentity("Child1", "child1", "Layout = \"Parent.cshtml\";");
|
|
|
|
|
var child2 = FileService.CreateTemplateWithIdentity("Child2", "child2", "Layout = \"Parent.cshtml\";");
|
2020-10-02 14:46:14 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var children = FileService.GetTemplates(parent.Id).Select(x => x.Id).ToArray();
|
2020-10-02 14:46:14 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.IsTrue(children.Contains(child1.Id));
|
|
|
|
|
Assert.IsTrue(children.Contains(child2.Id));
|
|
|
|
|
}
|
2020-10-02 14:46:14 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Create_Template_With_Custom_Alias()
|
|
|
|
|
{
|
|
|
|
|
var template = FileService.CreateTemplateWithIdentity("Test template", "customTemplateAlias", "test");
|
2020-10-02 14:46:14 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
FileService.SaveTemplate(template);
|
2020-10-02 14:46:14 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
template = FileService.GetTemplate(template.Id);
|
2020-10-02 14:46:14 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual("Test template", template.Name);
|
|
|
|
|
Assert.AreEqual("customTemplateAlias", template.Alias);
|
2020-10-02 14:46:14 +02:00
|
|
|
}
|
|
|
|
|
}
|