Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/FileServiceTests.cs
Kenn Jacobsen 01224fce89 Template API for new backoffice (#13642)
* Template query builder API

* Create a dedicated template service (copy template operations from file service)

* CRUD API for templates

* Make file service consume the template service (remove duplicated code)

* Use the template service in the old template controller so we can track changes better (note: this is breaking, but it doesn't matter as the controller will be deleted)

* Add scaffolding to the template API

* Make the route differ between query settings and execution

* Get rid of ugly string constants

* Refactor query execution a little to improve code health

* Fix build checks (compat)

* Deduce the master template from the template contents

* Make template service async, move master template parsing into template service

* Fix open API test

* Make sure the unit tests use new template parsing

* Add FIXME for SetMasterTemplate

* added obsolete attributes

Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2023-01-11 14:40:41 +01:00

136 lines
5.2 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Linq;
using NUnit.Framework;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services;
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
public class FileServiceTests : UmbracoIntegrationTest
{
private IFileService FileService => GetRequiredService<IFileService>();
[SetUp]
public void SetUp()
{
var fileSystems = GetRequiredService<FileSystems>();
var viewFileSystem = fileSystems.MvcViewsFileSystem!;
foreach (var file in viewFileSystem.GetFiles(string.Empty).ToArray())
{
viewFileSystem.DeleteFile(file);
}
}
[Test]
public void Create_Template_Then_Assign_Child()
{
var child = FileService.CreateTemplateWithIdentity("Child", "child", "test");
var parent = FileService.CreateTemplateWithIdentity("Parent", "parent", "test");
child.Content = "Layout = \"Parent.cshtml\";";
FileService.SaveTemplate(child);
child = FileService.GetTemplate(child.Id);
Assert.AreEqual(parent.Alias, child.MasterTemplateAlias);
}
[Test]
public void Create_Template_With_Child_Then_Unassign()
{
var parent = FileService.CreateTemplateWithIdentity("Parent", "parent", "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 = "test";
FileService.SaveTemplate(child);
child = FileService.GetTemplate(child.Id);
Assert.NotNull(child);
Assert.AreEqual(null, child.MasterTemplateAlias);
}
[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);
}
[Test]
public void Can_Query_Template_Children()
{
var parent = FileService.CreateTemplateWithIdentity("Parent", "parent", "test");
var child1 = FileService.CreateTemplateWithIdentity("Child1", "child1", "Layout = \"Parent.cshtml\";");
var child2 = FileService.CreateTemplateWithIdentity("Child2", "child2", "Layout = \"Parent.cshtml\";");
var children = FileService.GetTemplates(parent.Id).Select(x => x.Id).ToArray();
Assert.IsTrue(children.Contains(child1.Id));
Assert.IsTrue(children.Contains(child2.Id));
}
[Test]
public void Create_Template_With_Custom_Alias()
{
var template = FileService.CreateTemplateWithIdentity("Test template", "customTemplateAlias", "test");
FileService.SaveTemplate(template);
template = FileService.GetTemplate(template.Id);
Assert.AreEqual("Test template", template.Name);
Assert.AreEqual("customTemplateAlias", template.Alias);
}
}