Migrated stylesheet and template tests into new project and builder pattern.
This commit is contained in:
@@ -76,6 +76,9 @@
|
||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
|
||||
<_Parameter1>Umbraco.Tests.Integration</_Parameter1>
|
||||
</AssemblyAttribute>
|
||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
|
||||
<_Parameter1>Umbraco.Tests.Common</_Parameter1>
|
||||
</AssemblyAttribute>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
34
src/Umbraco.Tests.Common/Builders/StylesheetBuilder.cs
Normal file
34
src/Umbraco.Tests.Common/Builders/StylesheetBuilder.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using Umbraco.Core.Models;
|
||||
|
||||
namespace Umbraco.Tests.Common.Builders
|
||||
{
|
||||
public class StylesheetBuilder
|
||||
: BuilderBase<Stylesheet>
|
||||
{
|
||||
private string _path;
|
||||
private string _content;
|
||||
|
||||
public StylesheetBuilder WithPath(string path)
|
||||
{
|
||||
_path = path;
|
||||
return this;
|
||||
}
|
||||
|
||||
public StylesheetBuilder WithContent(string content)
|
||||
{
|
||||
_content = content;
|
||||
return this;
|
||||
}
|
||||
|
||||
public override Stylesheet Build()
|
||||
{
|
||||
var path = _path ?? string.Empty;
|
||||
var content = _content ?? string.Empty;
|
||||
|
||||
return new Stylesheet(path)
|
||||
{
|
||||
Content = content,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
116
src/Umbraco.Tests.Common/Builders/TemplateBuilder.cs
Normal file
116
src/Umbraco.Tests.Common/Builders/TemplateBuilder.cs
Normal file
@@ -0,0 +1,116 @@
|
||||
using System;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Strings;
|
||||
using Umbraco.Tests.Common.Builders.Extensions;
|
||||
using Umbraco.Tests.Common.Builders.Interfaces;
|
||||
|
||||
namespace Umbraco.Tests.Common.Builders
|
||||
{
|
||||
public class TemplateBuilder
|
||||
: BuilderBase<Template>,
|
||||
IWithIdBuilder,
|
||||
IWithKeyBuilder,
|
||||
IWithAliasBuilder,
|
||||
IWithNameBuilder,
|
||||
IWithCreateDateBuilder,
|
||||
IWithUpdateDateBuilder,
|
||||
IWithPathBuilder
|
||||
{
|
||||
private int? _id;
|
||||
private Guid? _key;
|
||||
private string _alias;
|
||||
private string _name;
|
||||
private DateTime? _createDate;
|
||||
private DateTime? _updateDate;
|
||||
private string _path;
|
||||
private string _content;
|
||||
private bool? _isMasterTemplate;
|
||||
private string _masterTemplateAlias;
|
||||
private Lazy<int> _masterTemplateId;
|
||||
|
||||
public TemplateBuilder WithContent(string content)
|
||||
{
|
||||
_content = content;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TemplateBuilder AsMasterTemplate(string masterTemplateAlias, int masterTemplateId)
|
||||
{
|
||||
_isMasterTemplate = true;
|
||||
_masterTemplateAlias = masterTemplateAlias;
|
||||
_masterTemplateId = new Lazy<int>(() => masterTemplateId);
|
||||
return this;
|
||||
}
|
||||
|
||||
public override Template Build()
|
||||
{
|
||||
var id = _id ?? 0;
|
||||
var key = _key ?? Guid.NewGuid();
|
||||
var name = _name ?? Guid.NewGuid().ToString();
|
||||
var alias = _alias ?? name.ToCamelCase();
|
||||
var createDate = _createDate ?? DateTime.Now;
|
||||
var updateDate = _updateDate ?? DateTime.Now;
|
||||
var path = _path ?? string.Empty;
|
||||
var content = _content;
|
||||
var isMasterTemplate = _isMasterTemplate ?? false;
|
||||
var masterTemplateAlias = _masterTemplateAlias ?? string.Empty;
|
||||
var masterTemplateId = _masterTemplateId ?? null;
|
||||
|
||||
var shortStringHelper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig());
|
||||
return new Template(shortStringHelper, name, alias)
|
||||
{
|
||||
Id = id,
|
||||
Key = key,
|
||||
CreateDate = createDate,
|
||||
UpdateDate = updateDate,
|
||||
Path = path,
|
||||
Content = content,
|
||||
IsMasterTemplate = isMasterTemplate,
|
||||
MasterTemplateAlias = masterTemplateAlias,
|
||||
MasterTemplateId = masterTemplateId,
|
||||
};
|
||||
}
|
||||
|
||||
int? IWithIdBuilder.Id
|
||||
{
|
||||
get => _id;
|
||||
set => _id = value;
|
||||
}
|
||||
|
||||
Guid? IWithKeyBuilder.Key
|
||||
{
|
||||
get => _key;
|
||||
set => _key = value;
|
||||
}
|
||||
|
||||
string IWithAliasBuilder.Alias
|
||||
{
|
||||
get => _alias;
|
||||
set => _alias = value;
|
||||
}
|
||||
|
||||
string IWithNameBuilder.Name
|
||||
{
|
||||
get => _name;
|
||||
set => _name = value;
|
||||
}
|
||||
|
||||
DateTime? IWithCreateDateBuilder.CreateDate
|
||||
{
|
||||
get => _createDate;
|
||||
set => _createDate = value;
|
||||
}
|
||||
|
||||
DateTime? IWithUpdateDateBuilder.UpdateDate
|
||||
{
|
||||
get => _updateDate;
|
||||
set => _updateDate = value;
|
||||
}
|
||||
|
||||
string IWithPathBuilder.Path
|
||||
{
|
||||
get => _path;
|
||||
set => _path = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,26 +3,24 @@ using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Serialization;
|
||||
using Umbraco.Tests.TestHelpers;
|
||||
using Umbraco.Tests.Common.Builders;
|
||||
|
||||
namespace Umbraco.Tests.Models
|
||||
namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models
|
||||
{
|
||||
[TestFixture]
|
||||
public class StylesheetTests
|
||||
{
|
||||
[SetUp]
|
||||
public virtual void Initialize()
|
||||
{
|
||||
SettingsForTests.Reset();
|
||||
}
|
||||
private readonly StylesheetBuilder _builder = new StylesheetBuilder();
|
||||
|
||||
[Test]
|
||||
public void Can_Create_Stylesheet()
|
||||
{
|
||||
// Arrange
|
||||
var stylesheet = new Stylesheet("/css/styles.css");
|
||||
stylesheet.Content = @"body { color:#000; } .bold {font-weight:bold;}";
|
||||
// Act
|
||||
var stylesheet = _builder
|
||||
.WithPath("/css/styles.css")
|
||||
.WithContent(@"body { color:#000; } .bold {font-weight:bold;}")
|
||||
.Build();
|
||||
|
||||
// Assert
|
||||
Assert.That(stylesheet.Name, Is.EqualTo("styles.css"));
|
||||
@@ -33,8 +31,12 @@ namespace Umbraco.Tests.Models
|
||||
public void Can_Add_Property()
|
||||
{
|
||||
// Arrange
|
||||
var stylesheet = new Stylesheet("/css/styles.css") {Content = @"body { color:#000; } .bold {font-weight:bold;}"};
|
||||
var stylesheet = _builder
|
||||
.WithPath("/css/styles.css")
|
||||
.WithContent(@"body { color:#000; } .bold {font-weight:bold;}")
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
stylesheet.AddProperty(new StylesheetProperty("Test", "p", "font-weight:bold; font-family:Arial;"));
|
||||
|
||||
// Assert
|
||||
@@ -48,13 +50,16 @@ namespace Umbraco.Tests.Models
|
||||
public void Can_Remove_Property()
|
||||
{
|
||||
// Arrange
|
||||
var stylesheet = new Stylesheet("/css/styles.css") { Content = @"body { color:#000; } /**umb_name:Hello*/p{font-size:2em;} .bold {font-weight:bold;}" };
|
||||
|
||||
|
||||
var stylesheet = _builder
|
||||
.WithPath("/css/styles.css")
|
||||
.WithContent(@"body { color:#000; } /**umb_name:Hello*/p{font-size:2em;} .bold {font-weight:bold;}")
|
||||
.Build();
|
||||
Assert.AreEqual(1, stylesheet.Properties.Count());
|
||||
|
||||
// Act
|
||||
stylesheet.RemoveProperty("Hello");
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(0, stylesheet.Properties.Count());
|
||||
Assert.AreEqual(@"body { color:#000; } .bold {font-weight:bold;}", stylesheet.Content);
|
||||
}
|
||||
@@ -63,17 +68,20 @@ namespace Umbraco.Tests.Models
|
||||
public void Can_Update_Property()
|
||||
{
|
||||
// Arrange
|
||||
var stylesheet = new Stylesheet("/css/styles.css")
|
||||
{
|
||||
Content = @"body { color:#000; } /**umb_name:Hello*/p{font-size:2em;} .bold {font-weight:bold;}"
|
||||
};
|
||||
var stylesheet = _builder
|
||||
.WithPath("/css/styles.css")
|
||||
.WithContent(@"body { color:#000; } /**umb_name:Hello*/p{font-size:2em;} .bold {font-weight:bold;}")
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var prop = stylesheet.Properties.Single();
|
||||
prop.Alias = "li";
|
||||
prop.Value = "font-size:5em;";
|
||||
|
||||
//re-get
|
||||
// - re-get
|
||||
prop = stylesheet.Properties.Single();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual("li", prop.Alias);
|
||||
Assert.AreEqual("font-size:5em;", prop.Value);
|
||||
Assert.AreEqual("body { color:#000; } /**umb_name:Hello*/\r\nli {\r\n\tfont-size:5em;\r\n} .bold {font-weight:bold;}", stylesheet.Content);
|
||||
@@ -83,12 +91,15 @@ namespace Umbraco.Tests.Models
|
||||
public void Can_Get_Properties_From_Css()
|
||||
{
|
||||
// Arrange
|
||||
var stylesheet = new Stylesheet("/css/styles.css");
|
||||
stylesheet.Content = @"body { color:#000; } .bold {font-weight:bold;} /**umb_name:Hello */ p { font-size: 1em; } /**umb_name:testing123*/ li:first-child {padding:0px;}";
|
||||
var stylesheet = _builder
|
||||
.WithPath("/css/styles.css")
|
||||
.WithContent(@"body { color:#000; } .bold {font-weight:bold;} /**umb_name:Hello */ p { font-size: 1em; } /**umb_name:testing123*/ li:first-child {padding:0px;}")
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var properties = stylesheet.Properties;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(2, properties.Count());
|
||||
Assert.AreEqual("Hello", properties.First().Name);
|
||||
Assert.AreEqual("font-size: 1em;", properties.First().Value);
|
||||
@@ -102,13 +113,17 @@ namespace Umbraco.Tests.Models
|
||||
[Test]
|
||||
public void Can_Serialize_Without_Error()
|
||||
{
|
||||
var stylesheet = new Stylesheet("/css/styles.css");
|
||||
stylesheet.Content = @"@media screen and (min-width: 600px) and (min-width: 900px) {
|
||||
.class {
|
||||
background: #666;
|
||||
}
|
||||
}";
|
||||
// Arrange
|
||||
var stylesheet = _builder
|
||||
.WithPath("/css/styles.css")
|
||||
.WithContent(@"@media screen and (min-width: 600px) and (min-width: 900px) {
|
||||
.class {
|
||||
background: #666;
|
||||
}
|
||||
}")
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var json = JsonConvert.SerializeObject(stylesheet);
|
||||
Debug.Print(json);
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Newtonsoft.Json;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Tests.Common.Builders;
|
||||
|
||||
using Umbraco.Tests.Common.Builders.Extensions;
|
||||
|
||||
namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Models
|
||||
{
|
||||
[TestFixture]
|
||||
public class TemplateTests
|
||||
{
|
||||
private readonly TemplateBuilder _builder = new TemplateBuilder();
|
||||
|
||||
[Test]
|
||||
public void Can_Deep_Clone()
|
||||
{
|
||||
var template = BuildTemplate();
|
||||
|
||||
var clone = (Template)template.DeepClone();
|
||||
|
||||
Assert.AreNotSame(clone, template);
|
||||
Assert.AreEqual(clone, template);
|
||||
Assert.AreEqual(clone.Path, template.Path);
|
||||
Assert.AreEqual(clone.IsMasterTemplate, template.IsMasterTemplate);
|
||||
Assert.AreEqual(clone.CreateDate, template.CreateDate);
|
||||
Assert.AreEqual(clone.Alias, template.Alias);
|
||||
Assert.AreEqual(clone.Id, template.Id);
|
||||
Assert.AreEqual(clone.Key, template.Key);
|
||||
Assert.AreEqual(clone.MasterTemplateAlias, template.MasterTemplateAlias);
|
||||
Assert.AreEqual(clone.MasterTemplateId.Value, template.MasterTemplateId.Value);
|
||||
Assert.AreEqual(clone.Name, template.Name);
|
||||
Assert.AreEqual(clone.UpdateDate, template.UpdateDate);
|
||||
|
||||
// clone.Content should be null but getting it would lazy-load
|
||||
var type = clone.GetType();
|
||||
var contentField = type.BaseType.GetField("_content", BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
var value = contentField.GetValue(clone);
|
||||
Assert.IsNull(value);
|
||||
|
||||
// this double verifies by reflection
|
||||
// need to exclude content else it would lazy-load
|
||||
var allProps = clone.GetType().GetProperties();
|
||||
foreach (var propertyInfo in allProps.Where(x => x.Name != "Content"))
|
||||
{
|
||||
Assert.AreEqual(propertyInfo.GetValue(clone, null), propertyInfo.GetValue(template, null));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Can_Serialize_Without_Error()
|
||||
{
|
||||
var template = BuildTemplate();
|
||||
|
||||
var json = JsonConvert.SerializeObject(template);
|
||||
Debug.Print(json);
|
||||
}
|
||||
|
||||
private Template BuildTemplate()
|
||||
{
|
||||
return _builder
|
||||
.WithId(3)
|
||||
.WithAlias("test")
|
||||
.WithName("Test")
|
||||
.WithCreateDate(DateTime.Now)
|
||||
.WithUpdateDate(DateTime.Now)
|
||||
.WithKey(Guid.NewGuid())
|
||||
.WithPath("-1,3")
|
||||
.WithContent("blah")
|
||||
.AsMasterTemplate("master", 88)
|
||||
.Build();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Tests.Common.Builders;
|
||||
|
||||
namespace Umbraco.Tests.UnitTests.Umbraco.Tests.Common.Builders
|
||||
{
|
||||
[TestFixture]
|
||||
public class StylesheetBuilderTests
|
||||
{
|
||||
[Test]
|
||||
public void Is_Built_Correctly()
|
||||
{
|
||||
// Arrange
|
||||
const string path = "/css/styles.css";
|
||||
const string content = @"body { color:#000; } .bold {font-weight:bold;}";
|
||||
|
||||
var builder = new StylesheetBuilder();
|
||||
|
||||
// Act
|
||||
var stylesheet = builder
|
||||
.WithPath(path)
|
||||
.WithContent(content)
|
||||
.Build();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual("\\css\\styles.css", stylesheet.Path);
|
||||
Assert.AreEqual(content, stylesheet.Content);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Tests.Common.Builders;
|
||||
using Umbraco.Tests.Common.Builders.Extensions;
|
||||
|
||||
namespace Umbraco.Tests.UnitTests.Umbraco.Tests.Common.Builders
|
||||
{
|
||||
[TestFixture]
|
||||
public class TemplateBuilderTests
|
||||
{
|
||||
[Test]
|
||||
public void Is_Built_Correctly()
|
||||
{
|
||||
// Arrange
|
||||
const int id = 3;
|
||||
const string alias = "test";
|
||||
const string name = "Test";
|
||||
var key = Guid.NewGuid();
|
||||
var createDate = DateTime.Now.AddHours(-1);
|
||||
var updateDate = DateTime.Now;
|
||||
const string path = "-1,3";
|
||||
const string content = "blah";
|
||||
const string masterTemplateAlias = "master";
|
||||
const int masterTemplateId = 88;
|
||||
|
||||
var builder = new TemplateBuilder();
|
||||
|
||||
// Act
|
||||
var template = builder
|
||||
.WithId(3)
|
||||
.WithAlias(alias)
|
||||
.WithName(name)
|
||||
.WithCreateDate(createDate)
|
||||
.WithUpdateDate(updateDate)
|
||||
.WithKey(key)
|
||||
.WithPath(path)
|
||||
.WithContent(content)
|
||||
.AsMasterTemplate(masterTemplateAlias, masterTemplateId)
|
||||
.Build();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(id, template.Id);
|
||||
Assert.AreEqual(alias, template.Alias);
|
||||
Assert.AreEqual(name, template.Name);
|
||||
Assert.AreEqual(createDate, template.CreateDate);
|
||||
Assert.AreEqual(updateDate, template.UpdateDate);
|
||||
Assert.AreEqual(key, template.Key);
|
||||
Assert.AreEqual(path, template.Path);
|
||||
Assert.AreEqual(content, template.Content);
|
||||
Assert.IsTrue(template.IsMasterTemplate);
|
||||
Assert.AreEqual(masterTemplateAlias, template.MasterTemplateAlias);
|
||||
Assert.AreEqual(masterTemplateId, template.MasterTemplateId.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Newtonsoft.Json;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Serialization;
|
||||
using Umbraco.Tests.Testing;
|
||||
|
||||
namespace Umbraco.Tests.Models
|
||||
{
|
||||
[TestFixture]
|
||||
public class TemplateTests : UmbracoTestBase
|
||||
{
|
||||
[Test]
|
||||
public void Can_Deep_Clone()
|
||||
{
|
||||
var item = new Template(ShortStringHelper, "Test", "test")
|
||||
{
|
||||
Id = 3,
|
||||
CreateDate = DateTime.Now,
|
||||
Key = Guid.NewGuid(),
|
||||
UpdateDate = DateTime.Now,
|
||||
Content = "blah",
|
||||
Path = "-1,3",
|
||||
IsMasterTemplate = true,
|
||||
MasterTemplateAlias = "master",
|
||||
MasterTemplateId = new Lazy<int>(() => 88)
|
||||
};
|
||||
|
||||
var clone = (Template)item.DeepClone();
|
||||
|
||||
Assert.AreNotSame(clone, item);
|
||||
Assert.AreEqual(clone, item);
|
||||
Assert.AreEqual(clone.Path, item.Path);
|
||||
Assert.AreEqual(clone.IsMasterTemplate, item.IsMasterTemplate);
|
||||
Assert.AreEqual(clone.CreateDate, item.CreateDate);
|
||||
Assert.AreEqual(clone.Alias, item.Alias);
|
||||
Assert.AreEqual(clone.Id, item.Id);
|
||||
Assert.AreEqual(clone.Key, item.Key);
|
||||
Assert.AreEqual(clone.MasterTemplateAlias, item.MasterTemplateAlias);
|
||||
Assert.AreEqual(clone.MasterTemplateId.Value, item.MasterTemplateId.Value);
|
||||
Assert.AreEqual(clone.Name, item.Name);
|
||||
Assert.AreEqual(clone.UpdateDate, item.UpdateDate);
|
||||
|
||||
// clone.Content should be null but getting it would lazy-load
|
||||
var type = clone.GetType();
|
||||
var contentField = type.BaseType.GetField("_content", BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
var value = contentField.GetValue(clone);
|
||||
Assert.IsNull(value);
|
||||
|
||||
// this double verifies by reflection
|
||||
// need to exclude content else it would lazy-load
|
||||
var allProps = clone.GetType().GetProperties();
|
||||
foreach (var propertyInfo in allProps.Where(x => x.Name != "Content"))
|
||||
{
|
||||
Assert.AreEqual(propertyInfo.GetValue(clone, null), propertyInfo.GetValue(item, null));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Can_Serialize_Without_Error()
|
||||
{
|
||||
var item = new Template(ShortStringHelper, "Test", "test")
|
||||
{
|
||||
Id = 3,
|
||||
CreateDate = DateTime.Now,
|
||||
Key = Guid.NewGuid(),
|
||||
UpdateDate = DateTime.Now,
|
||||
Content = "blah",
|
||||
MasterTemplateAlias = "master",
|
||||
MasterTemplateId = new Lazy<int>(() => 88)
|
||||
};
|
||||
|
||||
var json = JsonConvert.SerializeObject(item);
|
||||
Debug.Print(json);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -300,7 +300,6 @@
|
||||
<Compile Include="Models\PropertyGroupTests.cs" />
|
||||
<Compile Include="Models\PropertyTypeTests.cs" />
|
||||
<Compile Include="Models\RelationTests.cs" />
|
||||
<Compile Include="Models\TemplateTests.cs" />
|
||||
<Compile Include="Models\LightEntityTest.cs" />
|
||||
<Compile Include="Models\UserTests.cs" />
|
||||
<Compile Include="Web\Mvc\RenderModelBinderTests.cs" />
|
||||
@@ -402,7 +401,6 @@
|
||||
<Compile Include="PropertyEditors\PropertyEditorValueConverterTests.cs" />
|
||||
<Compile Include="Models\ContentTests.cs" />
|
||||
<Compile Include="Models\ContentXmlTest.cs" />
|
||||
<Compile Include="Models\StylesheetTests.cs" />
|
||||
<Compile Include="Persistence\SqlCeTableByTableTest.cs" />
|
||||
<Compile Include="Persistence\DatabaseContextTests.cs" />
|
||||
<Compile Include="Persistence\Mappers\ContentMapperTest.cs" />
|
||||
|
||||
Reference in New Issue
Block a user