Removing MacroType enum as we only have one type - PartialView. Refactoring all its references and fixing tests

This commit is contained in:
elitsa
2020-02-25 13:38:36 +01:00
parent c3d3e6e27d
commit 0a130d6bbd
16 changed files with 36 additions and 92 deletions

View File

@@ -20,8 +20,6 @@ namespace Umbraco.Web.Macros
/// </summary>
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;
}
}
}

View File

@@ -58,22 +58,10 @@ namespace Umbraco.Core.Models
[DataMember]
string MacroSource { get; set; }
/// <summary>
/// Gets or set the macro type
/// </summary>
[DataMember]
MacroTypes MacroType { get; set; }
/// <summary>
/// Gets or sets a list of Macro Properties
/// </summary>
[DataMember]
MacroPropertyCollection Properties { get; }
///// <summary>
///// Returns an enum <see cref="MacroTypes"/> based on the properties on the Macro
///// </summary>
///// <returns><see cref="MacroTypes"/></returns>
//MacroTypes MacroType();
}
}

View File

@@ -1,18 +0,0 @@
using System;
using System.Runtime.Serialization;
namespace Umbraco.Core.Models
{
/// <summary>
/// Enum for the various types of Macros
/// </summary>
[Serializable]
[DataContract(IsReference = true)]
public enum MacroTypes
{
[EnumMember]
Unknown = 4,
[EnumMember]
PartialView = 7
}
}

View File

@@ -18,11 +18,12 @@ namespace Umbraco.Core.Migrations.Upgrade.V_8_0_0
AddColumn<MacroDto>("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();

View File

@@ -40,7 +40,7 @@ namespace Umbraco.Core.Models
/// <param name="cacheByMember"></param>
/// <param name="dontRender"></param>
/// <param name="macroSource"></param>
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;
}
/// <summary>
@@ -69,7 +68,6 @@ namespace Umbraco.Core.Models
/// <param name="macroSource"></param>
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<string> _addedProperties;
private List<string> _removedProperties;
@@ -247,16 +243,6 @@ namespace Umbraco.Core.Models
set => SetPropertyValueAndDetectChanges(value, ref _macroSource, nameof(MacroSource));
}
/// <summary>
/// Gets or set the path to the Partial View to render
/// </summary>
[DataMember]
public MacroTypes MacroType
{
get => _macroType;
set => SetPropertyValueAndDetectChanges(value, ref _macroType, nameof(MacroType));
}
/// <summary>
/// Gets or sets a list of Macro Properties
/// </summary>

View File

@@ -1133,7 +1133,6 @@ namespace Umbraco.Core.Packaging
{
var macroName = macroElement.Element("name").Value;
var macroAlias = macroElement.Element("alias").Value;
var macroType = Enum<MacroTypes>.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");

View File

@@ -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)

View File

@@ -19,7 +19,6 @@ namespace Umbraco.Core.Persistence.Mappers
DefineMap<Macro, MacroDto>(nameof(Macro.Alias), nameof(MacroDto.Alias));
DefineMap<Macro, MacroDto>(nameof(Macro.CacheByPage), nameof(MacroDto.CacheByPage));
DefineMap<Macro, MacroDto>(nameof(Macro.CacheByMember), nameof(MacroDto.CachePersonalized));
DefineMap<Macro, MacroDto>(nameof(Macro.MacroType), nameof(MacroDto.MacroType));
DefineMap<Macro, MacroDto>(nameof(Macro.DontRender), nameof(MacroDto.DontRender));
DefineMap<Macro, MacroDto>(nameof(Macro.Name), nameof(MacroDto.Name));
DefineMap<Macro, MacroDto>(nameof(Macro.CacheDuration), nameof(MacroDto.RefreshRate));

View File

@@ -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()));

View File

@@ -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<MacroTypes>.Parse(macroTypeString);
var model = new MacroModel
{
MacroType = macroType,
MacroSource = "anything"
MacroSource = macroSource
};
var filename = MacroRenderer.GetMacroFileName(model);
if (expectedNonNull)

View File

@@ -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();

View File

@@ -37,7 +37,7 @@ namespace Umbraco.Tests.Persistence.Repositories
{
var repository = new MacroRepository((IScopeAccessor) provider, AppCaches.Disabled, Mock.Of<ILogger>(), 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<SqlCeException>(() => repository.Save(macro));
@@ -168,7 +168,7 @@ namespace Umbraco.Tests.Persistence.Repositories
var repository = new MacroRepository((IScopeAccessor) provider, AppCaches.Disabled, Mock.Of<ILogger>(), 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<ILogger>(), 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<ILogger>(), 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<ILogger>(), 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<ILogger>(), 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();
}

View File

@@ -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

View File

@@ -28,9 +28,9 @@ namespace Umbraco.Tests.Services
{
var repository = new MacroRepository((IScopeAccessor) provider, AppCaches.Disabled, Mock.Of<ILogger>(), 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<ArgumentException>(() => macroService.Save(macro));

View File

@@ -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

View File

@@ -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