From 10563577d9b51970c0f602a66923581d84019a50 Mon Sep 17 00:00:00 2001 From: elitsa Date: Tue, 25 Feb 2020 13:29:03 +0100 Subject: [PATCH 1/6] Eliminating WebForms Controls from MacroContent and migrating the file from Web to Abstractions/Core project --- src/Umbraco.Core/Macros/MacroContent.cs | 20 ++++++++++ src/Umbraco.Web/Macros/MacroContent.cs | 53 ------------------------- 2 files changed, 20 insertions(+), 53 deletions(-) create mode 100644 src/Umbraco.Core/Macros/MacroContent.cs delete mode 100644 src/Umbraco.Web/Macros/MacroContent.cs diff --git a/src/Umbraco.Core/Macros/MacroContent.cs b/src/Umbraco.Core/Macros/MacroContent.cs new file mode 100644 index 0000000000..f0ca8d6e9a --- /dev/null +++ b/src/Umbraco.Core/Macros/MacroContent.cs @@ -0,0 +1,20 @@ +using System; + +namespace Umbraco.Web.Macros +{ + // represents the content of a macro + public class MacroContent + { + // gets or sets the text content + public string Text { get; set; } + + // gets or sets the date the content was generated + public DateTime Date { get; set; } = DateTime.Now; + + // a value indicating whether the content is empty + public bool IsEmpty => Text == null; + + // gets an empty macro content + public static MacroContent Empty { get; } = new MacroContent(); + } +} diff --git a/src/Umbraco.Web/Macros/MacroContent.cs b/src/Umbraco.Web/Macros/MacroContent.cs deleted file mode 100644 index 2e7680ebe0..0000000000 --- a/src/Umbraco.Web/Macros/MacroContent.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using System.IO; -using System.Web.UI; - -namespace Umbraco.Web.Macros -{ - // represents the content of a macro - public class MacroContent - { - // gets or sets the text content - public string Text { get; set; } - - // gets or sets the control content - public Control Control { get; set; } - public string ControlId { get; set; } - - // gets or sets the date the content was generated - public DateTime Date { get; set; } = DateTime.Now; - - // a value indicating whether the content is empty - public bool IsEmpty => Text == null && Control == null; - - // a value indicating whether the content is pure text (no control) - public bool IsText => Control == null; - - // a value indicating whether the content is a control - public bool IsControl => Control != null; - - // gets an empty macro content - public static MacroContent Empty { get; } = new MacroContent(); - - // gets the macro content as a string - // ie executes the control if any - public string GetAsText() - { - if (Control == null) return Text ?? string.Empty; - - using (var textWriter = new StringWriter()) - using (var htmlWriter = new HtmlTextWriter(textWriter)) - { - Control.RenderControl(htmlWriter); - return textWriter.ToString(); - } - } - - // gets the macro content as a control - // ie wraps the text in a control if needed - public Control GetAsControl() - { - return Control ?? new LiteralControl(Text ?? string.Empty); - } - } -} From c3d3e6e27d3ebc40776c479cb7193552606e1100 Mon Sep 17 00:00:00 2001 From: elitsa Date: Tue, 25 Feb 2020 13:33:22 +0100 Subject: [PATCH 2/6] As GetAsText() had no other purpose when WebForm Controls were removed, it is now replaced with the populated Text property --- .../ValueConverters/RteMacroRenderingValueConverter.cs | 2 +- src/Umbraco.Web/UmbracoComponentRenderer.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Web/PropertyEditors/ValueConverters/RteMacroRenderingValueConverter.cs b/src/Umbraco.Web/PropertyEditors/ValueConverters/RteMacroRenderingValueConverter.cs index 60857fd76c..0016d4c88a 100644 --- a/src/Umbraco.Web/PropertyEditors/ValueConverters/RteMacroRenderingValueConverter.cs +++ b/src/Umbraco.Web/PropertyEditors/ValueConverters/RteMacroRenderingValueConverter.cs @@ -61,7 +61,7 @@ namespace Umbraco.Web.PropertyEditors.ValueConverters macroAlias, umbracoContext.PublishedRequest?.PublishedContent, //needs to be explicitly casted to Dictionary - macroAttributes.ConvertTo(x => (string)x, x => x)).GetAsText())); + macroAttributes.ConvertTo(x => (string)x, x => x)).Text)); return sb.ToString(); } diff --git a/src/Umbraco.Web/UmbracoComponentRenderer.cs b/src/Umbraco.Web/UmbracoComponentRenderer.cs index 60ee14ff21..7a338cca59 100644 --- a/src/Umbraco.Web/UmbracoComponentRenderer.cs +++ b/src/Umbraco.Web/UmbracoComponentRenderer.cs @@ -119,7 +119,7 @@ namespace Umbraco.Web x => x.Key.ToLowerInvariant(), i => (i.Value is string) ? HttpUtility.HtmlDecode(i.Value.ToString()) : i.Value); - var html = _macroRenderer.Render(alias, content, macroProps).GetAsText(); + var html = _macroRenderer.Render(alias, content, macroProps).Text; return new HtmlString(html); } From 0a130d6bbd31bc2aaa0030ca6cfc92a7bdf95460 Mon Sep 17 00:00:00 2001 From: elitsa Date: Tue, 25 Feb 2020 13:38:36 +0100 Subject: [PATCH 3/6] Removing MacroType enum as we only have one type - PartialView. Refactoring all its references and fixing tests --- .../Macros/MacroModel.cs | 5 ----- src/Umbraco.Core/Models/IMacro.cs | 12 ----------- src/Umbraco.Core/Models/MacroTypes.cs | 18 ----------------- .../Upgrade/V_8_0_0/RefactorMacroColumns.cs | 11 +++++----- src/Umbraco.Infrastructure/Models/Macro.cs | 16 +-------------- .../Packaging/PackageDataInstallation.cs | 3 +-- .../Persistence/Factories/MacroFactory.cs | 4 ++-- .../Persistence/Mappers/MacroMapper.cs | 1 - .../Services/Implement/EntityXmlSerializer.cs | 1 - src/Umbraco.Tests/Macros/MacroTests.cs | 10 ++++------ src/Umbraco.Tests/Models/MacroTests.cs | 2 +- .../Repositories/MacroRepositoryTest.cs | 16 +++++++-------- .../Services/EntityXmlSerializerTests.cs | 2 +- .../Services/MacroServiceTests.cs | 20 +++++++++---------- .../Editors/MacroRenderingController.cs | 3 +-- src/Umbraco.Web/Editors/MacrosController.cs | 4 +--- 16 files changed, 36 insertions(+), 92 deletions(-) rename src/{Umbraco.Web => Umbraco.Core}/Macros/MacroModel.cs (91%) delete mode 100644 src/Umbraco.Core/Models/MacroTypes.cs diff --git a/src/Umbraco.Web/Macros/MacroModel.cs b/src/Umbraco.Core/Macros/MacroModel.cs similarity index 91% rename from src/Umbraco.Web/Macros/MacroModel.cs rename to src/Umbraco.Core/Macros/MacroModel.cs index dd9a73b147..6b5013115a 100644 --- a/src/Umbraco.Web/Macros/MacroModel.cs +++ b/src/Umbraco.Core/Macros/MacroModel.cs @@ -20,8 +20,6 @@ namespace Umbraco.Web.Macros /// public string Alias { get; set; } - public MacroTypes MacroType { get; set; } - public string MacroSource { get; set; } public int CacheDuration { get; set; } @@ -46,7 +44,6 @@ namespace Umbraco.Web.Macros Id = macro.Id; Name = macro.Name; Alias = macro.Alias; - MacroType = macro.MacroType; MacroSource = macro.MacroSource; CacheDuration = macro.CacheDuration; CacheByPage = macro.CacheByPage; @@ -55,8 +52,6 @@ namespace Umbraco.Web.Macros foreach (var prop in macro.Properties) Properties.Add(new MacroPropertyModel(prop.Alias, string.Empty, prop.EditorAlias)); - - MacroType = macro.MacroType; } } } diff --git a/src/Umbraco.Core/Models/IMacro.cs b/src/Umbraco.Core/Models/IMacro.cs index c3d37b0700..9d1c47154c 100644 --- a/src/Umbraco.Core/Models/IMacro.cs +++ b/src/Umbraco.Core/Models/IMacro.cs @@ -58,22 +58,10 @@ namespace Umbraco.Core.Models [DataMember] string MacroSource { get; set; } - /// - /// Gets or set the macro type - /// - [DataMember] - MacroTypes MacroType { get; set; } - /// /// Gets or sets a list of Macro Properties /// [DataMember] MacroPropertyCollection Properties { get; } - - ///// - ///// Returns an enum based on the properties on the Macro - ///// - ///// - //MacroTypes MacroType(); } } diff --git a/src/Umbraco.Core/Models/MacroTypes.cs b/src/Umbraco.Core/Models/MacroTypes.cs deleted file mode 100644 index 5f8440845d..0000000000 --- a/src/Umbraco.Core/Models/MacroTypes.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Runtime.Serialization; - -namespace Umbraco.Core.Models -{ - /// - /// Enum for the various types of Macros - /// - [Serializable] - [DataContract(IsReference = true)] - public enum MacroTypes - { - [EnumMember] - Unknown = 4, - [EnumMember] - PartialView = 7 - } -} diff --git a/src/Umbraco.Infrastructure/Migrations/Upgrade/V_8_0_0/RefactorMacroColumns.cs b/src/Umbraco.Infrastructure/Migrations/Upgrade/V_8_0_0/RefactorMacroColumns.cs index 58ec0e30c2..bc3df6f584 100644 --- a/src/Umbraco.Infrastructure/Migrations/Upgrade/V_8_0_0/RefactorMacroColumns.cs +++ b/src/Umbraco.Infrastructure/Migrations/Upgrade/V_8_0_0/RefactorMacroColumns.cs @@ -18,11 +18,12 @@ namespace Umbraco.Core.Migrations.Upgrade.V_8_0_0 AddColumn("macroSource", out var sqls2); //populate the new columns with legacy data - Execute.Sql($"UPDATE {Constants.DatabaseSchema.Tables.Macro} SET macroSource = '', macroType = {(int)MacroTypes.Unknown}").Do(); - Execute.Sql($"UPDATE {Constants.DatabaseSchema.Tables.Macro} SET macroSource = macroXSLT, macroType = {(int)MacroTypes.Unknown} WHERE macroXSLT != '' AND macroXSLT IS NOT NULL").Do(); - Execute.Sql($"UPDATE {Constants.DatabaseSchema.Tables.Macro} SET macroSource = macroScriptAssembly, macroType = {(int)MacroTypes.Unknown} WHERE macroScriptAssembly != '' AND macroScriptAssembly IS NOT NULL").Do(); - Execute.Sql($"UPDATE {Constants.DatabaseSchema.Tables.Macro} SET macroSource = macroScriptType, macroType = {(int)MacroTypes.Unknown} WHERE macroScriptType != '' AND macroScriptType IS NOT NULL").Do(); - Execute.Sql($"UPDATE {Constants.DatabaseSchema.Tables.Macro} SET macroSource = macroPython, macroType = {(int)MacroTypes.PartialView} WHERE macroPython != '' AND macroPython IS NOT NULL").Do(); + //when the macro type is PartialView, it corresponds to 7, else it is 4 for Unknown + Execute.Sql($"UPDATE {Constants.DatabaseSchema.Tables.Macro} SET macroSource = '', macroType = 4").Do(); + Execute.Sql($"UPDATE {Constants.DatabaseSchema.Tables.Macro} SET macroSource = macroXSLT, macroType = 4 WHERE macroXSLT != '' AND macroXSLT IS NOT NULL").Do(); + Execute.Sql($"UPDATE {Constants.DatabaseSchema.Tables.Macro} SET macroSource = macroScriptAssembly, macroType = 4 WHERE macroScriptAssembly != '' AND macroScriptAssembly IS NOT NULL").Do(); + Execute.Sql($"UPDATE {Constants.DatabaseSchema.Tables.Macro} SET macroSource = macroScriptType, macroType = 4 WHERE macroScriptType != '' AND macroScriptType IS NOT NULL").Do(); + Execute.Sql($"UPDATE {Constants.DatabaseSchema.Tables.Macro} SET macroSource = macroPython, macroType = 7 WHERE macroPython != '' AND macroPython IS NOT NULL").Do(); //now apply constraints (NOT NULL) to new table foreach (var sql in sqls1) Execute.Sql(sql).Do(); diff --git a/src/Umbraco.Infrastructure/Models/Macro.cs b/src/Umbraco.Infrastructure/Models/Macro.cs index 95e3477212..083c288e09 100644 --- a/src/Umbraco.Infrastructure/Models/Macro.cs +++ b/src/Umbraco.Infrastructure/Models/Macro.cs @@ -40,7 +40,7 @@ namespace Umbraco.Core.Models /// /// /// - public Macro(IShortStringHelper shortStringHelper, int id, Guid key, bool useInEditor, int cacheDuration, string @alias, string name, bool cacheByPage, bool cacheByMember, bool dontRender, string macroSource, MacroTypes macroType) + public Macro(IShortStringHelper shortStringHelper, int id, Guid key, bool useInEditor, int cacheDuration, string @alias, string name, bool cacheByPage, bool cacheByMember, bool dontRender, string macroSource) : this(shortStringHelper) { Id = id; @@ -53,7 +53,6 @@ namespace Umbraco.Core.Models CacheByMember = cacheByMember; DontRender = dontRender; MacroSource = macroSource; - MacroType = macroType; } /// @@ -69,7 +68,6 @@ namespace Umbraco.Core.Models /// public Macro(IShortStringHelper shortStringHelper, string @alias, string name, string macroSource, - MacroTypes macroType, bool cacheByPage = false, bool cacheByMember = false, bool dontRender = true, @@ -85,7 +83,6 @@ namespace Umbraco.Core.Models CacheByMember = cacheByMember; DontRender = dontRender; MacroSource = macroSource; - MacroType = macroType; } private string _alias; @@ -96,7 +93,6 @@ namespace Umbraco.Core.Models private bool _cacheByMember; private bool _dontRender; private string _macroSource; - private MacroTypes _macroType = MacroTypes.Unknown; private MacroPropertyCollection _properties; private List _addedProperties; private List _removedProperties; @@ -247,16 +243,6 @@ namespace Umbraco.Core.Models set => SetPropertyValueAndDetectChanges(value, ref _macroSource, nameof(MacroSource)); } - /// - /// Gets or set the path to the Partial View to render - /// - [DataMember] - public MacroTypes MacroType - { - get => _macroType; - set => SetPropertyValueAndDetectChanges(value, ref _macroType, nameof(MacroType)); - } - /// /// Gets or sets a list of Macro Properties /// diff --git a/src/Umbraco.Infrastructure/Packaging/PackageDataInstallation.cs b/src/Umbraco.Infrastructure/Packaging/PackageDataInstallation.cs index 1699365dfd..6b1aa96e69 100644 --- a/src/Umbraco.Infrastructure/Packaging/PackageDataInstallation.cs +++ b/src/Umbraco.Infrastructure/Packaging/PackageDataInstallation.cs @@ -1133,7 +1133,6 @@ namespace Umbraco.Core.Packaging { var macroName = macroElement.Element("name").Value; var macroAlias = macroElement.Element("alias").Value; - var macroType = Enum.Parse(macroElement.Element("macroType").Value); var macroSource = macroElement.Element("macroSource").Value; //Following xml elements are treated as nullable properties @@ -1169,7 +1168,7 @@ namespace Umbraco.Core.Packaging } var existingMacro = _macroService.GetByAlias(macroAlias) as Macro; - var macro = existingMacro ?? new Macro(_shortStringHelper, macroAlias, macroName, macroSource, macroType, + var macro = existingMacro ?? new Macro(_shortStringHelper, macroAlias, macroName, macroSource, cacheByPage, cacheByMember, dontRender, useInEditor, cacheDuration); var properties = macroElement.Element("properties"); diff --git a/src/Umbraco.Infrastructure/Persistence/Factories/MacroFactory.cs b/src/Umbraco.Infrastructure/Persistence/Factories/MacroFactory.cs index 25cde62c75..5d0cbc9c34 100644 --- a/src/Umbraco.Infrastructure/Persistence/Factories/MacroFactory.cs +++ b/src/Umbraco.Infrastructure/Persistence/Factories/MacroFactory.cs @@ -10,7 +10,7 @@ namespace Umbraco.Core.Persistence.Factories { public static IMacro BuildEntity(IShortStringHelper shortStringHelper, MacroDto dto) { - var model = new Macro(shortStringHelper, dto.Id, dto.UniqueId, dto.UseInEditor, dto.RefreshRate, dto.Alias, dto.Name, dto.CacheByPage, dto.CachePersonalized, dto.DontRender, dto.MacroSource, (MacroTypes)dto.MacroType); + var model = new Macro(shortStringHelper, dto.Id, dto.UniqueId, dto.UseInEditor, dto.RefreshRate, dto.Alias, dto.Name, dto.CacheByPage, dto.CachePersonalized, dto.DontRender, dto.MacroSource); try { @@ -45,7 +45,7 @@ namespace Umbraco.Core.Persistence.Factories RefreshRate = entity.CacheDuration, UseInEditor = entity.UseInEditor, MacroPropertyDtos = BuildPropertyDtos(entity), - MacroType = (int)entity.MacroType + MacroType = 7 //PartialView }; if (entity.HasIdentity) diff --git a/src/Umbraco.Infrastructure/Persistence/Mappers/MacroMapper.cs b/src/Umbraco.Infrastructure/Persistence/Mappers/MacroMapper.cs index 6a0d803052..f6f7de8347 100644 --- a/src/Umbraco.Infrastructure/Persistence/Mappers/MacroMapper.cs +++ b/src/Umbraco.Infrastructure/Persistence/Mappers/MacroMapper.cs @@ -19,7 +19,6 @@ namespace Umbraco.Core.Persistence.Mappers DefineMap(nameof(Macro.Alias), nameof(MacroDto.Alias)); DefineMap(nameof(Macro.CacheByPage), nameof(MacroDto.CacheByPage)); DefineMap(nameof(Macro.CacheByMember), nameof(MacroDto.CachePersonalized)); - DefineMap(nameof(Macro.MacroType), nameof(MacroDto.MacroType)); DefineMap(nameof(Macro.DontRender), nameof(MacroDto.DontRender)); DefineMap(nameof(Macro.Name), nameof(MacroDto.Name)); DefineMap(nameof(Macro.CacheDuration), nameof(MacroDto.RefreshRate)); diff --git a/src/Umbraco.Infrastructure/Services/Implement/EntityXmlSerializer.cs b/src/Umbraco.Infrastructure/Services/Implement/EntityXmlSerializer.cs index e8fea4c0e1..d69297eb57 100644 --- a/src/Umbraco.Infrastructure/Services/Implement/EntityXmlSerializer.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/EntityXmlSerializer.cs @@ -415,7 +415,6 @@ namespace Umbraco.Core.Services.Implement var xml = new XElement("macro"); xml.Add(new XElement("name", macro.Name)); xml.Add(new XElement("alias", macro.Alias)); - xml.Add(new XElement("macroType", macro.MacroType)); xml.Add(new XElement("macroSource", macro.MacroSource)); xml.Add(new XElement("useInEditor", macro.UseInEditor.ToString())); xml.Add(new XElement("dontRender", macro.DontRender.ToString())); diff --git a/src/Umbraco.Tests/Macros/MacroTests.cs b/src/Umbraco.Tests/Macros/MacroTests.cs index 2b2a8051e7..66adfc4a97 100644 --- a/src/Umbraco.Tests/Macros/MacroTests.cs +++ b/src/Umbraco.Tests/Macros/MacroTests.cs @@ -25,15 +25,13 @@ namespace Umbraco.Tests.Macros new IsolatedCaches(type => new ObjectCacheAppCache(typeFinder))); } - [TestCase("PartialView", true)] - [TestCase("Unknown", false)] - public void Macro_Is_File_Based(string macroTypeString, bool expectedNonNull) + [TestCase("anything", true)] + [TestCase("", false)] + public void Macro_Is_File_Based(string macroSource, bool expectedNonNull) { - var macroType = Enum.Parse(macroTypeString); var model = new MacroModel { - MacroType = macroType, - MacroSource = "anything" + MacroSource = macroSource }; var filename = MacroRenderer.GetMacroFileName(model); if (expectedNonNull) diff --git a/src/Umbraco.Tests/Models/MacroTests.cs b/src/Umbraco.Tests/Models/MacroTests.cs index b14092955f..9ebe57a847 100644 --- a/src/Umbraco.Tests/Models/MacroTests.cs +++ b/src/Umbraco.Tests/Models/MacroTests.cs @@ -13,7 +13,7 @@ namespace Umbraco.Tests.Models [Test] public void Can_Deep_Clone() { - var macro = new Macro(TestHelper.ShortStringHelper, 1, Guid.NewGuid(), true, 3, "test", "Test", false, true, true, "~/script.cshtml", MacroTypes.PartialView); + var macro = new Macro(TestHelper.ShortStringHelper, 1, Guid.NewGuid(), true, 3, "test", "Test", false, true, true, "~/script.cshtml"); macro.Properties.Add(new MacroProperty(6, Guid.NewGuid(), "rewq", "REWQ", 1, "asdfasdf")); var clone = (Macro)macro.DeepClone(); diff --git a/src/Umbraco.Tests/Persistence/Repositories/MacroRepositoryTest.cs b/src/Umbraco.Tests/Persistence/Repositories/MacroRepositoryTest.cs index 4b9d0436ec..facfb9c012 100644 --- a/src/Umbraco.Tests/Persistence/Repositories/MacroRepositoryTest.cs +++ b/src/Umbraco.Tests/Persistence/Repositories/MacroRepositoryTest.cs @@ -37,7 +37,7 @@ namespace Umbraco.Tests.Persistence.Repositories { var repository = new MacroRepository((IScopeAccessor) provider, AppCaches.Disabled, Mock.Of(), ShortStringHelper); - var macro = new Macro(ShortStringHelper, "test1", "Test", "~/views/macropartials/test.cshtml", MacroTypes.PartialView); + var macro = new Macro(ShortStringHelper, "test1", "Test", "~/views/macropartials/test.cshtml"); ; Assert.Throws(() => repository.Save(macro)); @@ -168,7 +168,7 @@ namespace Umbraco.Tests.Persistence.Repositories var repository = new MacroRepository((IScopeAccessor) provider, AppCaches.Disabled, Mock.Of(), ShortStringHelper); // Act - var macro = new Macro(ShortStringHelper, "test", "Test", "~/views/macropartials/test.cshtml", MacroTypes.PartialView); + var macro = new Macro(ShortStringHelper, "test", "Test", "~/views/macropartials/test.cshtml"); macro.Properties.Add(new MacroProperty("test", "Test", 0, "test")); repository.Save(macro); @@ -289,7 +289,7 @@ namespace Umbraco.Tests.Persistence.Repositories { var repository = new MacroRepository((IScopeAccessor) provider, AppCaches.Disabled, Mock.Of(), ShortStringHelper); - var macro = new Macro(ShortStringHelper, "newmacro", "A new macro", "~/views/macropartials/test1.cshtml", MacroTypes.PartialView); + var macro = new Macro(ShortStringHelper, "newmacro", "A new macro", "~/views/macropartials/test1.cshtml"); macro.Properties.Add(new MacroProperty("blah1", "New1", 4, "test.editor")); repository.Save(macro); @@ -314,7 +314,7 @@ namespace Umbraco.Tests.Persistence.Repositories { var repository = new MacroRepository((IScopeAccessor) provider, AppCaches.Disabled, Mock.Of(), ShortStringHelper); - var macro = new Macro(ShortStringHelper, "newmacro", "A new macro", "~/views/macropartials/test1.cshtml", MacroTypes.PartialView); + var macro = new Macro(ShortStringHelper, "newmacro", "A new macro", "~/views/macropartials/test1.cshtml"); macro.Properties.Add(new MacroProperty("blah1", "New1", 4, "test.editor")); repository.Save(macro); @@ -338,7 +338,7 @@ namespace Umbraco.Tests.Persistence.Repositories { var repository = new MacroRepository((IScopeAccessor) provider, AppCaches.Disabled, Mock.Of(), ShortStringHelper); - var macro = new Macro(ShortStringHelper, "newmacro", "A new macro", "~/views/macropartials/test1.cshtml", MacroTypes.PartialView); + var macro = new Macro(ShortStringHelper, "newmacro", "A new macro", "~/views/macropartials/test1.cshtml"); var prop1 = new MacroProperty("blah1", "New1", 4, "test.editor"); var prop2 = new MacroProperty("blah2", "New2", 3, "test.editor"); @@ -424,9 +424,9 @@ namespace Umbraco.Tests.Persistence.Repositories { var repository = new MacroRepository((IScopeAccessor) provider, AppCaches.Disabled, Mock.Of(), ShortStringHelper); - repository.Save(new Macro(ShortStringHelper, "test1", "Test1", "~/views/macropartials/test1.cshtml", MacroTypes.PartialView)); - repository.Save(new Macro(ShortStringHelper, "test2", "Test2", "~/views/macropartials/test2.cshtml", MacroTypes.PartialView)); - repository.Save(new Macro(ShortStringHelper, "test3", "Tet3", "~/views/macropartials/test3.cshtml", MacroTypes.PartialView)); + repository.Save(new Macro(ShortStringHelper, "test1", "Test1", "~/views/macropartials/test1.cshtml")); + repository.Save(new Macro(ShortStringHelper, "test2", "Test2", "~/views/macropartials/test2.cshtml")); + repository.Save(new Macro(ShortStringHelper, "test3", "Tet3", "~/views/macropartials/test3.cshtml")); scope.Complete(); } diff --git a/src/Umbraco.Tests/Services/EntityXmlSerializerTests.cs b/src/Umbraco.Tests/Services/EntityXmlSerializerTests.cs index cf1a974742..e10dd99482 100644 --- a/src/Umbraco.Tests/Services/EntityXmlSerializerTests.cs +++ b/src/Umbraco.Tests/Services/EntityXmlSerializerTests.cs @@ -22,7 +22,7 @@ namespace Umbraco.Tests.Services public void Can_Export_Macro() { // Arrange - var macro = new Macro(ShortStringHelper, "test1", "Test", "~/views/macropartials/test.cshtml", MacroTypes.PartialView); + var macro = new Macro(ShortStringHelper, "test1", "Test", "~/views/macropartials/test.cshtml"); ServiceContext.MacroService.Save(macro); // Act diff --git a/src/Umbraco.Tests/Services/MacroServiceTests.cs b/src/Umbraco.Tests/Services/MacroServiceTests.cs index 1d8642b402..ceecb72156 100644 --- a/src/Umbraco.Tests/Services/MacroServiceTests.cs +++ b/src/Umbraco.Tests/Services/MacroServiceTests.cs @@ -28,9 +28,9 @@ namespace Umbraco.Tests.Services { var repository = new MacroRepository((IScopeAccessor) provider, AppCaches.Disabled, Mock.Of(), ShortStringHelper); - repository.Save(new Macro(ShortStringHelper, "test1", "Test1", "~/views/macropartials/test1.cshtml", MacroTypes.PartialView)); - repository.Save(new Macro(ShortStringHelper, "test2", "Test2", "~/views/macropartials/test2.cshtml", MacroTypes.PartialView)); - repository.Save(new Macro(ShortStringHelper, "test3", "Tet3", "~/views/macropartials/test3.cshtml", MacroTypes.PartialView)); + repository.Save(new Macro(ShortStringHelper, "test1", "Test1", "~/views/macropartials/test1.cshtml")); + repository.Save(new Macro(ShortStringHelper, "test2", "Test2", "~/views/macropartials/test2.cshtml")); + repository.Save(new Macro(ShortStringHelper, "test3", "Tet3", "~/views/macropartials/test3.cshtml")); scope.Complete(); } } @@ -75,7 +75,7 @@ namespace Umbraco.Tests.Services var macroService = ServiceContext.MacroService; // Act - var macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", MacroTypes.PartialView, cacheDuration: 1234); + var macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); macroService.Save(macro); //assert @@ -100,7 +100,7 @@ namespace Umbraco.Tests.Services { // Arrange var macroService = ServiceContext.MacroService; - var macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", MacroTypes.PartialView, cacheDuration: 1234); + var macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); macroService.Save(macro); // Act @@ -119,7 +119,7 @@ namespace Umbraco.Tests.Services { // Arrange var macroService = ServiceContext.MacroService; - IMacro macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", MacroTypes.PartialView, cacheDuration: 1234); + IMacro macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); macroService.Save(macro); // Act @@ -143,7 +143,7 @@ namespace Umbraco.Tests.Services { // Arrange var macroService = ServiceContext.MacroService; - IMacro macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", MacroTypes.PartialView, cacheDuration: 1234); + IMacro macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); macro.Properties.Add(new MacroProperty("blah", "Blah", 0, "blah")); macroService.Save(macro); @@ -174,7 +174,7 @@ namespace Umbraco.Tests.Services { // Arrange var macroService = ServiceContext.MacroService; - IMacro macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", MacroTypes.PartialView, cacheDuration: 1234); + IMacro macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); macro.Properties.Add(new MacroProperty("blah1", "Blah1", 0, "blah1")); macro.Properties.Add(new MacroProperty("blah2", "Blah2", 1, "blah2")); macro.Properties.Add(new MacroProperty("blah3", "Blah3", 2, "blah3")); @@ -218,7 +218,7 @@ namespace Umbraco.Tests.Services public void Can_Add_And_Remove_Properties() { var macroService = ServiceContext.MacroService; - var macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", MacroTypes.PartialView, cacheDuration: 1234); + var macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); //adds some properties macro.Properties.Add(new MacroProperty("blah1", "Blah1", 0, "blah1")); @@ -253,7 +253,7 @@ namespace Umbraco.Tests.Services { // Arrange var macroService = ServiceContext.MacroService; - var macro = new Macro(ShortStringHelper, "test", string.Empty, "~/Views/MacroPartials/Test.cshtml", MacroTypes.PartialView, cacheDuration: 1234); + var macro = new Macro(ShortStringHelper, "test", string.Empty, "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); // Act & Assert Assert.Throws(() => macroService.Save(macro)); diff --git a/src/Umbraco.Web/Editors/MacroRenderingController.cs b/src/Umbraco.Web/Editors/MacroRenderingController.cs index 950a7cb0ef..06cd15dcb4 100644 --- a/src/Umbraco.Web/Editors/MacroRenderingController.cs +++ b/src/Umbraco.Web/Editors/MacroRenderingController.cs @@ -195,8 +195,7 @@ namespace Umbraco.Web.Editors { Alias = macroName.ToSafeAlias(ShortStringHelper), Name = macroName, - MacroSource = model.VirtualPath.EnsureStartsWith("~"), - MacroType = MacroTypes.PartialView + MacroSource = model.VirtualPath.EnsureStartsWith("~") }; _macroService.Save(macro); // may throw diff --git a/src/Umbraco.Web/Editors/MacrosController.cs b/src/Umbraco.Web/Editors/MacrosController.cs index 67e75f17c2..0ac75df477 100644 --- a/src/Umbraco.Web/Editors/MacrosController.cs +++ b/src/Umbraco.Web/Editors/MacrosController.cs @@ -108,8 +108,7 @@ namespace Umbraco.Web.Editors { Alias = alias, Name = name, - MacroSource = string.Empty, - MacroType = MacroTypes.PartialView + MacroSource = string.Empty }; _macroService.Save(macro, this.Security.CurrentUser.Id); @@ -223,7 +222,6 @@ namespace Umbraco.Web.Editors macro.DontRender = !macroDisplay.RenderInEditor; macro.UseInEditor = macroDisplay.UseInEditor; macro.MacroSource = macroDisplay.View; - macro.MacroType = MacroTypes.PartialView; macro.Properties.ReplaceAll(macroDisplay.Parameters.Select((x,i) => new MacroProperty(x.Key, x.Label, i, x.Editor))); try From 65e719cfa6a1e8adecde5846079e17f61b8f8c17 Mon Sep 17 00:00:00 2001 From: elitsa Date: Tue, 25 Feb 2020 13:40:22 +0100 Subject: [PATCH 4/6] Migrating more Macro files to Abstractions or Infrastructure --- .../Macros/IMacroRenderer.cs | 0 .../Macros/MacroPropertyModel.cs | 0 .../Macros/MacroTagParser.cs | 8 ++++---- src/Umbraco.Infrastructure/Umbraco.Infrastructure.csproj | 1 + src/Umbraco.Web/Umbraco.Web.csproj | 6 ------ 5 files changed, 5 insertions(+), 10 deletions(-) rename src/{Umbraco.Web => Umbraco.Core}/Macros/IMacroRenderer.cs (100%) rename src/{Umbraco.Web => Umbraco.Core}/Macros/MacroPropertyModel.cs (100%) rename src/{Umbraco.Web => Umbraco.Infrastructure}/Macros/MacroTagParser.cs (96%) diff --git a/src/Umbraco.Web/Macros/IMacroRenderer.cs b/src/Umbraco.Core/Macros/IMacroRenderer.cs similarity index 100% rename from src/Umbraco.Web/Macros/IMacroRenderer.cs rename to src/Umbraco.Core/Macros/IMacroRenderer.cs diff --git a/src/Umbraco.Web/Macros/MacroPropertyModel.cs b/src/Umbraco.Core/Macros/MacroPropertyModel.cs similarity index 100% rename from src/Umbraco.Web/Macros/MacroPropertyModel.cs rename to src/Umbraco.Core/Macros/MacroPropertyModel.cs diff --git a/src/Umbraco.Web/Macros/MacroTagParser.cs b/src/Umbraco.Infrastructure/Macros/MacroTagParser.cs similarity index 96% rename from src/Umbraco.Web/Macros/MacroTagParser.cs rename to src/Umbraco.Infrastructure/Macros/MacroTagParser.cs index 7dea3674b0..2cbd84e20a 100644 --- a/src/Umbraco.Web/Macros/MacroTagParser.cs +++ b/src/Umbraco.Infrastructure/Macros/MacroTagParser.cs @@ -10,7 +10,7 @@ namespace Umbraco.Web.Macros /// /// Parses the macro syntax in a string and renders out it's contents /// - internal class MacroTagParser + public class MacroTagParser { private static readonly Regex MacroRteContent = new Regex(@"()", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Singleline); @@ -35,7 +35,7 @@ namespace Umbraco.Web.Macros /// {/div} /// /// - internal static string FormatRichTextPersistedDataForEditor(string persistedContent, IDictionary htmlAttributes) + public static string FormatRichTextPersistedDataForEditor(string persistedContent, IDictionary htmlAttributes) { return MacroPersistedFormat.Replace(persistedContent, match => { @@ -92,7 +92,7 @@ namespace Umbraco.Web.Macros /// since this is exactly how we need to persist it to the db. /// /// - internal static string FormatRichTextContentForPersistence(string rteContent) + public static string FormatRichTextContentForPersistence(string rteContent) { if (string.IsNullOrEmpty(rteContent)) { @@ -145,7 +145,7 @@ namespace Umbraco.Web.Macros /// This method simply parses the macro contents, it does not create a string or result, /// this is up to the developer calling this method to implement this with the callbacks. /// - internal static void ParseMacros( + public static void ParseMacros( string text, Action textFoundCallback, Action> macroFoundCallback ) diff --git a/src/Umbraco.Infrastructure/Umbraco.Infrastructure.csproj b/src/Umbraco.Infrastructure/Umbraco.Infrastructure.csproj index f73e5a548e..27dae62e72 100644 --- a/src/Umbraco.Infrastructure/Umbraco.Infrastructure.csproj +++ b/src/Umbraco.Infrastructure/Umbraco.Infrastructure.csproj @@ -6,6 +6,7 @@ + diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 5e7654e896..a66ddf8922 100755 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -167,8 +167,6 @@ - - @@ -238,7 +236,6 @@ - @@ -282,10 +279,7 @@ - - - From 0bdf03bda2c159d7c163e794b19555035e920714 Mon Sep 17 00:00:00 2001 From: elitsa Date: Tue, 25 Feb 2020 13:43:08 +0100 Subject: [PATCH 5/6] Removing the last references to MacroTypes and removing the references to pageElements coming from old legacy (PublishedContentHashtableConverter), which is deleted as well --- src/Umbraco.Web/Macros/MacroRenderer.cs | 92 +----- .../PublishedContentHashtableConverter.cs | 301 ------------------ 2 files changed, 15 insertions(+), 378 deletions(-) delete mode 100644 src/Umbraco.Web/Macros/PublishedContentHashtableConverter.cs diff --git a/src/Umbraco.Web/Macros/MacroRenderer.cs b/src/Umbraco.Web/Macros/MacroRenderer.cs index 013f54c5fc..85861a1496 100755 --- a/src/Umbraco.Web/Macros/MacroRenderer.cs +++ b/src/Umbraco.Web/Macros/MacroRenderer.cs @@ -112,10 +112,6 @@ namespace Umbraco.Web.Macros } } - // this is legacy and I'm not sure what exactly it is supposed to do - if (macroContent.Control != null) - macroContent.Control.ID = macroContent.ControlId; - return macroContent; } @@ -137,10 +133,6 @@ namespace Umbraco.Web.Macros if (key == null) return; } - // this is legacy and I'm not sure what exactly it is supposed to do - if (macroContent.Control != null) - macroContent.ControlId = macroContent.Control.ID; - // remember when we cache the content macroContent.Date = DateTime.Now; @@ -155,23 +147,12 @@ namespace Umbraco.Web.Macros } // gets the macro source file name - // null if the macro is not file-based + // null if the macro is not file-based, or not supported internal static string GetMacroFileName(MacroModel model) { - string filename; + string filename = model.MacroSource; // partial views are saved with their full virtual path - switch (model.MacroType) - { - case MacroTypes.PartialView: - filename = model.MacroSource; // partial views are saved with their full virtual path - break; - default: - // not file-based, or not supported - filename = null; - break; - } - - return filename; + return string.IsNullOrEmpty(filename) ? null : filename; } // gets the macro source file @@ -210,24 +191,22 @@ namespace Umbraco.Web.Macros if (m == null) throw new InvalidOperationException("No macro found by alias " + macroAlias); - var page = new PublishedContentHashtableConverter(content, _userService); - var macro = new MacroModel(m); UpdateMacroModelProperties(macro, macroParams); - return Render(macro, content, page.Elements); + return Render(macro, content); } - private MacroContent Render(MacroModel macro, IPublishedContent content, IDictionary pageElements) + private MacroContent Render(MacroModel macro, IPublishedContent content) { if (content == null) throw new ArgumentNullException(nameof(content)); - var macroInfo = $"Render Macro: {macro.Name}, type: {macro.MacroType}, cache: {macro.CacheDuration}"; + var macroInfo = $"Render Macro: {macro.Name}, cache: {macro.CacheDuration}"; using (_plogger.DebugDuration(macroInfo, "Rendered Macro.")) { // parse macro parameters ie replace the special [#key], [$key], etc. syntaxes foreach (var prop in macro.Properties) - prop.Value = ParseAttribute(pageElements, prop.Value); + prop.Value = ParseAttribute(prop.Value); macro.CacheIdentifier = GetContentCacheIdentifier(macro, content.Id); @@ -334,22 +313,11 @@ namespace Umbraco.Web.Macros var textService = _textService; - switch (model.MacroType) - { - case MacroTypes.PartialView: - return ExecuteMacroWithErrorWrapper(model, - $"Executing PartialView: MacroSource=\"{model.MacroSource}\".", - "Executed PartialView.", - () => ExecutePartialView(model, content), - () => textService.Localize("errors/macroErrorLoadingPartialView", new[] { model.MacroSource })); - - default: - return ExecuteMacroWithErrorWrapper(model, - $"Execute macro with unsupported type \"{model.MacroType}\".", - "Executed.", - () => { throw new Exception("Unsupported macro type."); }, - () => textService.Localize("errors/macroErrorUnsupportedType")); - } + return ExecuteMacroWithErrorWrapper(model, + $"Executing PartialView: MacroSource=\"{model.MacroSource}\".", + "Executed PartialView.", + () => ExecutePartialView(model, content), + () => textService.Localize("errors/macroErrorLoadingPartialView", new[] { model.MacroSource })); } @@ -371,12 +339,10 @@ namespace Umbraco.Web.Macros #region Execution helpers - // parses attribute value looking for [@requestKey], [%sessionKey], [#pageElement], [$recursiveValue] + // parses attribute value looking for [@requestKey], [%sessionKey] // supports fallbacks eg "[@requestKey],[%sessionKey],1234" - private string ParseAttribute(IDictionary pageElements, string attributeValue) + private string ParseAttribute(string attributeValue) { - if (pageElements == null) throw new ArgumentNullException(nameof(pageElements)); - // check for potential querystring/cookie variables attributeValue = attributeValue.Trim(); if (attributeValue.StartsWith("[") == false) @@ -388,7 +354,7 @@ namespace Umbraco.Web.Macros // like [1,2,3] which we don't want to parse - however the last one can be a literal, so // don't check on the last one which can be just anything - check all previous tokens - char[] validTypes = { '@', '%', '#', '$' }; + char[] validTypes = { '@', '%' }; if (tokens.Take(tokens.Length - 1).Any(x => x.Length < 4 // ie "[?x]".Length - too short || x[0] != '[' // starts with [ @@ -424,14 +390,6 @@ namespace Umbraco.Web.Macros if (string.IsNullOrEmpty(attributeValue)) attributeValue = _cookieManager.GetCookieValue(name); break; - case '#': - attributeValue = pageElements[name]?.ToString(); - break; - case '$': - attributeValue = pageElements[name]?.ToString(); - if (string.IsNullOrEmpty(attributeValue)) - attributeValue = ParseAttributeOnParents(pageElements, name); - break; } attributeValue = attributeValue?.Trim(); @@ -442,26 +400,6 @@ namespace Umbraco.Web.Macros return attributeValue; } - private string ParseAttributeOnParents(IDictionary pageElements, string name) - { - if (pageElements == null) throw new ArgumentNullException(nameof(pageElements)); - // this was, and still is, an ugly piece of nonsense - - var value = string.Empty; - var cache = _umbracoContextAccessor.UmbracoContext.Content; - - var splitpath = (string[])pageElements["splitpath"]; - for (var i = splitpath.Length - 1; i > 0; i--) // at 0 we have root (-1) - { - var content = cache.GetById(int.Parse(splitpath[i])); - if (content == null) continue; - value = content.Value(name)?.ToString(); - if (string.IsNullOrEmpty(value) == false) break; - } - - return value; - } - #endregion } diff --git a/src/Umbraco.Web/Macros/PublishedContentHashtableConverter.cs b/src/Umbraco.Web/Macros/PublishedContentHashtableConverter.cs deleted file mode 100644 index 29c03f7cfa..0000000000 --- a/src/Umbraco.Web/Macros/PublishedContentHashtableConverter.cs +++ /dev/null @@ -1,301 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using Umbraco.Core; -using Umbraco.Core.Models; -using Umbraco.Core.Models.PublishedContent; -using Umbraco.Core.PropertyEditors; -using Umbraco.Core.Services; -using Umbraco.Core.Strings; -using Umbraco.Web.Editors; -using Umbraco.Web.Routing; - -namespace Umbraco.Web.Macros -{ - /// - /// Legacy class used by macros which converts a published content item into a hashset of values - /// - internal class PublishedContentHashtableConverter - { - #region Constructors - - /// - /// Initializes a new instance of the class for a published document request. - /// - /// The pointing to the document. - /// The . - /// - /// The difference between creating the page with PublishedRequest vs an IPublishedContent item is - /// that the PublishedRequest takes into account how a template is assigned during the routing process whereas - /// with an IPublishedContent item, the template id is assigned purely based on the default. - /// - internal PublishedContentHashtableConverter(IPublishedRequest frequest, IUserService userService) - { - if (!frequest.HasPublishedContent) - throw new ArgumentException("Document request has no node.", nameof(frequest)); - - PopulatePageData(frequest.PublishedContent.Id, - frequest.PublishedContent.Name, frequest.PublishedContent.ContentType.Id, frequest.PublishedContent.ContentType.Alias, - frequest.PublishedContent.GetWriterName(userService), frequest.PublishedContent.GetCreatorName(userService), frequest.PublishedContent.CreateDate, frequest.PublishedContent.UpdateDate, - frequest.PublishedContent.Path, frequest.PublishedContent.Parent?.Id ?? -1); - - if (frequest.HasTemplate) - { - Elements["template"] = frequest.TemplateModel.Id.ToString(); - } - - PopulateElementData(frequest.PublishedContent); - - } - - /// - /// Initializes a new instance of the page for a published document - /// - /// - internal PublishedContentHashtableConverter(IPublishedContent doc, IUserService userService) - { - if (doc == null) throw new ArgumentNullException(nameof(doc)); - - PopulatePageData(doc.Id, - doc.Name, doc.ContentType.Id, doc.ContentType.Alias, - doc.GetWriterName(userService), doc.GetCreatorName(userService), doc.CreateDate, doc.UpdateDate, - doc.Path, doc.Parent?.Id ?? -1); - - if (doc.TemplateId.HasValue) - { - //set the template to whatever is assigned to the doc - Elements["template"] = doc.TemplateId.Value.ToString(); - } - - PopulateElementData(doc); - } - - /// - /// Initializes a new instance of the page for a content. - /// - /// The content. - /// - /// This is for usage only. - internal PublishedContentHashtableConverter(IContent content, IVariationContextAccessor variationContextAccessor, IUserService userService, IShortStringHelper shortStringHelper, IContentTypeBaseServiceProvider contentTypeBaseServiceProvider, IPublishedContentTypeFactory publishedContentTypeFactory, UrlSegmentProviderCollection urlSegmentProviders) - : this(new PagePublishedContent(content, variationContextAccessor, userService, shortStringHelper, contentTypeBaseServiceProvider, publishedContentTypeFactory, urlSegmentProviders), userService) - { } - - #endregion - - #region Initialize - - private void PopulatePageData(int pageId, - string pageName, int nodeType, string nodeTypeAlias, - string writerName, string creatorName, DateTime createDate, DateTime updateDate, - string path, int parentId) - { - // Update the elements hashtable - Elements.Add("pageID", pageId); - Elements.Add("parentID", parentId); - Elements.Add("pageName", pageName); - Elements.Add("nodeType", nodeType); - Elements.Add("nodeTypeAlias", nodeTypeAlias); - Elements.Add("writerName", writerName); - Elements.Add("creatorName", creatorName); - Elements.Add("createDate", createDate); - Elements.Add("updateDate", updateDate); - Elements.Add("path", path); - Elements.Add("splitpath", path.Split(',')); - } - - /// - /// Puts the properties of the node into the elements table - /// - /// - private void PopulateElementData(IPublishedElement node) - { - foreach (var p in node.Properties) - { - if (Elements.ContainsKey(p.Alias) == false) - { - // note: legacy used the raw value (see populating from an Xml node below) - // so we're doing the same here, using DataValue. If we use Value then every - // value will be converted NOW - including RTEs that may contain macros that - // require that the 'page' is already initialized = catch-22. - - // to properly fix this, we'd need to turn the elements collection into some - // sort of collection of lazy values. - - Elements[p.Alias] = p.GetSourceValue(); - } - } - } - #endregion - - /// - /// Returns a Hashtable of data for a published content item - /// - public Hashtable Elements { get; } = new Hashtable(); - - - #region PublishedContent - - private class PagePublishedProperty : PublishedPropertyBase - { - private readonly object _sourceValue; - private readonly IPublishedContent _content; - - public PagePublishedProperty(IPublishedPropertyType propertyType, IPublishedContent content) - : base(propertyType, PropertyCacheLevel.Unknown) // cache level is ignored - { - _sourceValue = null; - _content = content; - } - - public PagePublishedProperty(IPublishedPropertyType propertyType, IPublishedContent content, IProperty property) - : base(propertyType, PropertyCacheLevel.Unknown) // cache level is ignored - { - _sourceValue = property.GetValue(); - _content = content; - } - - public override bool HasValue(string culture = null, string segment = null) - { - return _sourceValue != null && ((_sourceValue is string) == false || string.IsNullOrWhiteSpace((string)_sourceValue) == false); - } - - public override object GetSourceValue(string culture = null, string segment = null) - { - return _sourceValue; - } - - public override object GetValue(string culture = null, string segment = null) - { - // isPreviewing is true here since we want to preview anyway... - const bool isPreviewing = true; - var source = PropertyType.ConvertSourceToInter(_content, _sourceValue, isPreviewing); - return PropertyType.ConvertInterToObject(_content, PropertyCacheLevel.Unknown, source, isPreviewing); - } - - public override object GetXPathValue(string culture = null, string segment = null) - { - throw new NotImplementedException(); - } - } - - private class PagePublishedContent : IPublishedContent - { - private readonly IContent _inner; - private readonly IPublishedProperty[] _properties; - private IReadOnlyDictionary _cultureInfos; - private readonly IVariationContextAccessor _variationContextAccessor; - private readonly IShortStringHelper _shortStringHelper; - private readonly UrlSegmentProviderCollection _urlSegmentProviders; - - private static readonly IReadOnlyDictionary NoCultureInfos = new Dictionary(); - - private PagePublishedContent(int id) - { - Id = id; - } - - public PagePublishedContent(IContent inner, IVariationContextAccessor variationContextAccessor, IUserService userService, IShortStringHelper shortStringHelper, IContentTypeBaseServiceProvider contentTypeBaseServiceProvider, IPublishedContentTypeFactory publishedContentTypeFactory, UrlSegmentProviderCollection urlSegmentProviders) - { - _inner = inner ?? throw new ArgumentNullException(nameof(inner)); - _variationContextAccessor = variationContextAccessor; - _shortStringHelper = shortStringHelper ?? throw new ArgumentNullException(nameof(shortStringHelper)); - _urlSegmentProviders = urlSegmentProviders ?? throw new ArgumentNullException(nameof(urlSegmentProviders)); - - Id = _inner.Id; - Key = _inner.Key; - - CreatorName = _inner.GetCreatorProfile(userService)?.Name; - WriterName = _inner.GetWriterProfile(userService)?.Name; - - var contentType = contentTypeBaseServiceProvider.GetContentTypeOf(_inner); - ContentType = publishedContentTypeFactory.CreateContentType(contentType); - - _properties = ContentType.PropertyTypes - .Select(x => - { - var p = _inner.Properties.SingleOrDefault(xx => xx.Alias == x.Alias); - return p == null ? new PagePublishedProperty(x, this) : new PagePublishedProperty(x, this, p); - }) - .Cast() - .ToArray(); - - Parent = new PagePublishedContent(_inner.ParentId); - } - - public IPublishedContentType ContentType { get; } - - public int Id { get; } - - public Guid Key { get; } - - public int? TemplateId => _inner.TemplateId; - - public int SortOrder => _inner.SortOrder; - - public string Name => _inner.Name; - - public IReadOnlyDictionary Cultures - { - get - { - if (!_inner.ContentType.VariesByCulture()) - return NoCultureInfos; - - if (_cultureInfos != null) - return _cultureInfos; - - return _cultureInfos = _inner.PublishCultureInfos.Values - .ToDictionary(x => x.Culture, x => new PublishedCultureInfo(x.Culture, x.Name, _inner.GetUrlSegment(_shortStringHelper, _urlSegmentProviders, x.Culture), x.Date)); - } - } - - public string UrlSegment => throw new NotImplementedException(); - - public string WriterName { get; } - - public string CreatorName { get; } - - public int WriterId => _inner.WriterId; - - public int CreatorId => _inner.CreatorId; - - public string Path => _inner.Path; - - public DateTime CreateDate => _inner.CreateDate; - - public DateTime UpdateDate => _inner.UpdateDate; - - public int Level => _inner.Level; - - public string Url => throw new NotImplementedException(); - - public PublishedItemType ItemType => PublishedItemType.Content; - - public bool IsDraft(string culture = null) - { - throw new NotImplementedException(); - } - - public bool IsPublished(string culture = null) - { - throw new NotImplementedException(); - } - - public IPublishedContent Parent { get; } - - public IEnumerable Children => throw new NotImplementedException(); - - public IEnumerable ChildrenForAllCultures => throw new NotImplementedException(); - - public IEnumerable Properties => _properties; - - public IPublishedProperty GetProperty(string alias) - { - throw new NotImplementedException(); - } - } - - #endregion - } -} From e0b921952848e87cabe2bdd55b05b7a8bb709891 Mon Sep 17 00:00:00 2001 From: Elitsa Marinovska <21998037+elit0451@users.noreply.github.com> Date: Tue, 25 Feb 2020 14:27:58 +0100 Subject: [PATCH 6/6] Update MacroContent.cs to use `is` operator for comparison to null Co-Authored-By: Bjarke Berg --- src/Umbraco.Core/Macros/MacroContent.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Umbraco.Core/Macros/MacroContent.cs b/src/Umbraco.Core/Macros/MacroContent.cs index f0ca8d6e9a..60dfb9210d 100644 --- a/src/Umbraco.Core/Macros/MacroContent.cs +++ b/src/Umbraco.Core/Macros/MacroContent.cs @@ -12,7 +12,7 @@ namespace Umbraco.Web.Macros public DateTime Date { get; set; } = DateTime.Now; // a value indicating whether the content is empty - public bool IsEmpty => Text == null; + public bool IsEmpty => Text is null; // gets an empty macro content public static MacroContent Empty { get; } = new MacroContent();