Skip to content

Commit

Permalink
Fixes to templates: Do not allow deletion of masters and ensure file …
Browse files Browse the repository at this point in the history
…names are pascal case (#17539)

* Make template file names start with capical character

* #16443

Also ensure master templates cannot be deleted

* Fix test

* Do not check breaking changes in integration tests

---------

Co-authored-by: nikolajlauridsen <[email protected]>
  • Loading branch information
bergmania and nikolajlauridsen authored Nov 16, 2024
1 parent a8705be commit c846633
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 8 deletions.
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
Expand Up @@ -195,7 +195,7 @@ public async Task Can_Delete_Template()
}

[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 @@ public async Task Deleting_Master_Template_Also_Deletes_Children()
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

0 comments on commit c846633

Please sign in to comment.