From 147a1daf6d5a41cddcc4338165ddd9e310ea711e Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 17 May 2017 12:09:28 +0200 Subject: [PATCH] U4-9926 - fix partial view macro udis --- src/Umbraco.Core/Models/IPartialView.cs | 2 +- src/Umbraco.Core/Models/PartialView.cs | 17 +++++++++++------ src/Umbraco.Core/Models/PartialViewType.cs | 2 +- .../Repositories/PartialViewRepository.cs | 5 ++--- src/Umbraco.Core/UdiGetterExtensions.cs | 8 +++++++- .../Repositories/PartialViewRepositoryTests.cs | 8 ++++---- src/Umbraco.Web/Editors/CodeFileController.cs | 10 +++++----- .../WebServices/SaveFileController.cs | 2 +- .../umbraco/create/PartialViewTasksBase.cs | 2 +- 9 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/Umbraco.Core/Models/IPartialView.cs b/src/Umbraco.Core/Models/IPartialView.cs index 01127ce22a..40a760427a 100644 --- a/src/Umbraco.Core/Models/IPartialView.cs +++ b/src/Umbraco.Core/Models/IPartialView.cs @@ -2,6 +2,6 @@ { public interface IPartialView : IFile { - + PartialViewType ViewType { get; } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Models/PartialView.cs b/src/Umbraco.Core/Models/PartialView.cs index 75914820f0..3c7a3ee908 100644 --- a/src/Umbraco.Core/Models/PartialView.cs +++ b/src/Umbraco.Core/Models/PartialView.cs @@ -1,10 +1,8 @@ using System; using System.Runtime.Serialization; -using Umbraco.Core.Services; namespace Umbraco.Core.Models { - /// /// Represents a Partial View file /// @@ -12,14 +10,21 @@ namespace Umbraco.Core.Models [DataContract(IsReference = true)] public class PartialView : File, IPartialView { + [Obsolete("Use the ctor that explicitely sets the view type.")] public PartialView(string path) - : this(path, null) + : this(PartialViewType.PartialView, path, null) { } - internal PartialView(string path, Func getFileContent) + public PartialView(PartialViewType viewType, string path) + : this(viewType, path, null) + { } + + internal PartialView(PartialViewType viewType, string path, Func getFileContent) : base(path, getFileContent) - { } + { + ViewType = viewType; + } - internal PartialViewType ViewType { get; set; } + public PartialViewType ViewType { get; set; } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Models/PartialViewType.cs b/src/Umbraco.Core/Models/PartialViewType.cs index 2b45448271..6204b6e165 100644 --- a/src/Umbraco.Core/Models/PartialViewType.cs +++ b/src/Umbraco.Core/Models/PartialViewType.cs @@ -1,6 +1,6 @@ namespace Umbraco.Core.Models { - internal enum PartialViewType : byte + public enum PartialViewType : byte { Unknown = 0, // default PartialView = 1, diff --git a/src/Umbraco.Core/Persistence/Repositories/PartialViewRepository.cs b/src/Umbraco.Core/Persistence/Repositories/PartialViewRepository.cs index 63cfd53833..f3380296df 100644 --- a/src/Umbraco.Core/Persistence/Repositories/PartialViewRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/PartialViewRepository.cs @@ -30,7 +30,7 @@ namespace Umbraco.Core.Persistence.Repositories var updated = FileSystem.GetLastModified(path).UtcDateTime; //var content = GetFileContent(path); - var view = new PartialView(path, file => GetFileContent(file.OriginalPath)) + var view = new PartialView(ViewType, path, file => GetFileContent(file.OriginalPath)) { //id can be the hash Id = path.GetHashCode(), @@ -38,8 +38,7 @@ namespace Umbraco.Core.Persistence.Repositories //Content = content, CreateDate = created, UpdateDate = updated, - VirtualPath = FileSystem.GetUrl(id), - ViewType = ViewType + VirtualPath = FileSystem.GetUrl(id) }; //on initial construction we don't want to have dirty properties tracked diff --git a/src/Umbraco.Core/UdiGetterExtensions.cs b/src/Umbraco.Core/UdiGetterExtensions.cs index 795d48c259..3d829c8a1a 100644 --- a/src/Umbraco.Core/UdiGetterExtensions.cs +++ b/src/Umbraco.Core/UdiGetterExtensions.cs @@ -198,7 +198,13 @@ namespace Umbraco.Core public static StringUdi GetUdi(this IPartialView entity) { if (entity == null) throw new ArgumentNullException("entity"); - return new StringUdi(Constants.UdiEntityType.PartialView, entity.Path.TrimStart('/')).EnsureClosed(); + + // we should throw on Unknown but for the time being, assume it means PartialView + var entityType = entity.ViewType == PartialViewType.PartialViewMacro + ? Constants.UdiEntityType.PartialViewMacro + : Constants.UdiEntityType.PartialView; + + return new StringUdi(entityType, entity.Path.TrimStart('/')).EnsureClosed(); } /// diff --git a/src/Umbraco.Tests/Persistence/Repositories/PartialViewRepositoryTests.cs b/src/Umbraco.Tests/Persistence/Repositories/PartialViewRepositoryTests.cs index 973acea207..5d3015e2b7 100644 --- a/src/Umbraco.Tests/Persistence/Repositories/PartialViewRepositoryTests.cs +++ b/src/Umbraco.Tests/Persistence/Repositories/PartialViewRepositoryTests.cs @@ -37,14 +37,14 @@ namespace Umbraco.Tests.Persistence.Repositories var unitOfWork = provider.GetUnitOfWork(); var repository = new PartialViewRepository(unitOfWork, _fileSystem); - var partialView = new PartialView("test-path-1.cshtml") { Content = "// partialView" }; + var partialView = new PartialView(PartialViewType.PartialView, "test-path-1.cshtml") { Content = "// partialView" }; repository.AddOrUpdate(partialView); unitOfWork.Commit(); Assert.IsTrue(_fileSystem.FileExists("test-path-1.cshtml")); Assert.AreEqual("test-path-1.cshtml", partialView.Path); Assert.AreEqual("/Views/Partials/test-path-1.cshtml", partialView.VirtualPath); - partialView = new PartialView("path-2/test-path-2.cshtml") { Content = "// partialView" }; + partialView = new PartialView(PartialViewType.PartialView, "path-2/test-path-2.cshtml") { Content = "// partialView" }; repository.AddOrUpdate(partialView); unitOfWork.Commit(); Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-2.cshtml")); @@ -56,7 +56,7 @@ namespace Umbraco.Tests.Persistence.Repositories Assert.AreEqual("path-2\\test-path-2.cshtml", partialView.Path); Assert.AreEqual("/Views/Partials/path-2/test-path-2.cshtml", partialView.VirtualPath); - partialView = new PartialView("path-2\\test-path-3.cshtml") { Content = "// partialView" }; + partialView = new PartialView(PartialViewType.PartialView, "path-2\\test-path-3.cshtml") { Content = "// partialView" }; repository.AddOrUpdate(partialView); unitOfWork.Commit(); Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-3.cshtml")); @@ -73,7 +73,7 @@ namespace Umbraco.Tests.Persistence.Repositories Assert.AreEqual("path-2\\test-path-3.cshtml", partialView.Path); Assert.AreEqual("/Views/Partials/path-2/test-path-3.cshtml", partialView.VirtualPath); - partialView = new PartialView("\\test-path-4.cshtml") { Content = "// partialView" }; + partialView = new PartialView(PartialViewType.PartialView, "\\test-path-4.cshtml") { Content = "// partialView" }; Assert.Throws(() => // fixed in 7.3 - 7.2.8 used to strip the \ { repository.AddOrUpdate(partialView); diff --git a/src/Umbraco.Web/Editors/CodeFileController.cs b/src/Umbraco.Web/Editors/CodeFileController.cs index 5f9f719a69..a7f420fc3b 100644 --- a/src/Umbraco.Web/Editors/CodeFileController.cs +++ b/src/Umbraco.Web/Editors/CodeFileController.cs @@ -42,13 +42,13 @@ namespace Umbraco.Web.Editors switch (type) { case Core.Constants.Trees.PartialViews: - var view = new PartialView(display.VirtualPath); + var view = new PartialView(PartialViewType.PartialView, display.VirtualPath); view.Content = display.Content; var result = Services.FileService.CreatePartialView(view, display.Snippet, Security.CurrentUser.Id); return result.Success == true ? Request.CreateResponse(HttpStatusCode.OK) : Request.CreateNotificationValidationErrorResponse(result.Exception.Message); case Core.Constants.Trees.PartialViewMacros: - var viewMacro = new PartialView(display.VirtualPath); + var viewMacro = new PartialView(PartialViewType.PartialViewMacro, display.VirtualPath); viewMacro.Content = display.Content; var resultMacro = Services.FileService.CreatePartialViewMacro(viewMacro, display.Snippet, Security.CurrentUser.Id); return resultMacro.Success == true ? Request.CreateResponse(HttpStatusCode.OK) : Request.CreateNotificationValidationErrorResponse(resultMacro.Exception.Message); @@ -219,13 +219,13 @@ namespace Umbraco.Web.Editors switch (type) { case Core.Constants.Trees.PartialViews: - codeFileDisplay = Mapper.Map(new PartialView(string.Empty)); + codeFileDisplay = Mapper.Map(new PartialView(PartialViewType.PartialView, string.Empty)); codeFileDisplay.VirtualPath = SystemDirectories.PartialViews; if (snippetName.IsNullOrWhiteSpace() == false) codeFileDisplay.Content = Services.FileService.GetPartialViewSnippetContent(snippetName); break; case Core.Constants.Trees.PartialViewMacros: - codeFileDisplay = Mapper.Map(new PartialView(string.Empty)); + codeFileDisplay = Mapper.Map(new PartialView(PartialViewType.PartialViewMacro, string.Empty)); codeFileDisplay.VirtualPath = SystemDirectories.MacroPartials; if (snippetName.IsNullOrWhiteSpace() == false) codeFileDisplay.Content = Services.FileService.GetPartialViewMacroSnippetContent(snippetName); @@ -475,7 +475,7 @@ namespace Umbraco.Web.Editors } else { - view = new PartialView(virtualPath + display.Name); + view = new PartialView(PartialViewType.PartialView, virtualPath + display.Name); view.Content = display.Content; partialViewResult = createView(view, display.Snippet, Security.CurrentUser.Id); } diff --git a/src/Umbraco.Web/WebServices/SaveFileController.cs b/src/Umbraco.Web/WebServices/SaveFileController.cs index 871f1512ef..fe93fb1e06 100644 --- a/src/Umbraco.Web/WebServices/SaveFileController.cs +++ b/src/Umbraco.Web/WebServices/SaveFileController.cs @@ -103,7 +103,7 @@ namespace Umbraco.Web.WebServices : get(svce, oldname); if (currentView == null) - currentView = new PartialView(filename); + currentView = new PartialView(PartialViewType.PartialView, filename); else currentView.Path = filename; currentView.Content = contents; diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/create/PartialViewTasksBase.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/create/PartialViewTasksBase.cs index abb1299507..9f810c01b8 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/create/PartialViewTasksBase.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/create/PartialViewTasksBase.cs @@ -46,7 +46,7 @@ namespace umbraco fileName += ".cshtml"; } - var model = new PartialView(fileName); + var model = new PartialView(IsPartialViewMacro ? PartialViewType.PartialViewMacro : PartialViewType.PartialView, fileName); var fileService = (FileService)ApplicationContext.Current.Services.FileService; var macroService = ApplicationContext.Current.Services.MacroService;