Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to templates: Do not allow deletion of masters and ensure file names are pascal case #17539

Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ protected IActionResult TemplateOperationStatusResult(TemplateOperationStatus st
.WithTitle("Master template not found")
.WithDetail("The master template referenced in the template was not found.")
.Build()),
TemplateOperationStatus.MasterTemplateCannotBeDeleted => BadRequest(problemDetailsBuilder
.WithTitle("Master template cannot be deleted")
.WithDetail("The master templates cannot be deleted. Please ensure the template is not a master template before you delete.")
.Build()),
_ => StatusCode(StatusCodes.Status500InternalServerError, problemDetailsBuilder
.WithTitle("Unknown template operation status.")
.Build()),
Expand Down
3 changes: 2 additions & 1 deletion src/Umbraco.Core/IO/ViewHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core.DependencyInjection;
Expand Down Expand Up @@ -90,7 +91,7 @@ public string CreateView(ITemplate t, bool overWrite = false)
return t.Content;
}

public string ViewPath(string alias) => _viewFileSystem.GetRelativePath(alias.Replace(" ", string.Empty) + ".cshtml");
public string ViewPath(string alias) => _viewFileSystem.GetRelativePath(CultureInfo.InvariantCulture.TextInfo.ToTitleCase(alias.Replace(" ", string.Empty)) + ".cshtml");

private string SaveTemplateToFile(ITemplate template)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public enum TemplateOperationStatus
DuplicateAlias,
TemplateNotFound,
MasterTemplateNotFound,
CircularMasterTemplateReference
CircularMasterTemplateReference,
MasterTemplateCannotBeDeleted,
}
6 changes: 6 additions & 0 deletions src/Umbraco.Core/Services/TemplateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,12 @@ private void Audit(AuditType type, int userId, int objectId, string? entityType)
return Attempt.FailWithStatus<ITemplate?, TemplateOperationStatus>(TemplateOperationStatus.TemplateNotFound, null);
}

if (template.IsMasterTemplate)
{
scope.Complete();
return Attempt.FailWithStatus<ITemplate?, TemplateOperationStatus>(TemplateOperationStatus.MasterTemplateCannotBeDeleted, null);
}

EventMessages eventMessages = EventMessagesFactory.Get();
var deletingNotification = new TemplateDeletingNotification(template, eventMessages);
if (scope.Notifications.PublishCancelable(deletingNotification))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Umbraco.

Check notice on line 1 in tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/TemplateServiceTests.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

✅ Getting better: Code Duplication

reduced similar code in: Deleting_Master_Template_Also_Deletes_Children. Avoid duplicated, aka copy-pasted, code inside the module. More duplication lowers the code health.
// See LICENSE for more details.

using NUnit.Framework;
Expand Down Expand Up @@ -195,7 +195,7 @@
}

[Test]
public async Task Deleting_Master_Template_Also_Deletes_Children()
public async Task Master_Template_Cannot_Be_Deleted()
{
Attempt<ITemplate, TemplateOperationStatus> result = await TemplateService.CreateAsync("Parent", "parent", "test", Constants.Security.SuperUserKey);
Assert.IsTrue(result.Success);
Expand All @@ -207,10 +207,8 @@
Assert.AreEqual("parent", child.MasterTemplateAlias);

result = await TemplateService.DeleteAsync(parent.Key, Constants.Security.SuperUserKey);
Assert.IsTrue(result.Success);

child = await TemplateService.GetAsync(child.Key);
Assert.Null(child);
Assert.IsFalse(result.Success);
Assert.That(result.Status, Is.EqualTo(TemplateOperationStatus.MasterTemplateCannotBeDeleted));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Description>Contains helper classes for integration tests with Umbraco CMS, including all internal integration tests.</Description>
<RootNamespace>Umbraco.Cms.Tests.Integration</RootNamespace>
<IsPackable>true</IsPackable>
<EnablePackageValidation>$(BaseEnablePackageValidation)</EnablePackageValidation>
<EnablePackageValidation>false</EnablePackageValidation>
<NoWarn>NU5100</NoWarn>
</PropertyGroup>
<PropertyGroup>
Expand Down
Loading