2021-10-15 23:24:21 +01:00
|
|
|
using System.Linq;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
2021-12-22 11:29:04 +01:00
|
|
|
using Umbraco.Extensions;
|
2021-10-15 23:24:21 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Configuration.Models;
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class RequestHandlerSettingsTests
|
2021-10-15 23:24:21 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Given_CharCollection_With_DefaultEnabled_MergesCollection()
|
2021-10-15 23:24:21 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
var userCollection = new CharItem[]
|
2021-10-15 23:24:21 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
new() { Char = "test", Replacement = "replace" },
|
|
|
|
|
new() { Char = "test2", Replacement = "replace2" },
|
|
|
|
|
};
|
2021-10-15 23:24:21 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var settings = new RequestHandlerSettings { UserDefinedCharCollection = userCollection };
|
|
|
|
|
var actual = settings.GetCharReplacements().ToList();
|
2021-10-15 23:24:21 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var expectedCollection = RequestHandlerSettings.DefaultCharCollection.ToList();
|
|
|
|
|
expectedCollection.AddRange(userCollection);
|
2021-10-15 23:24:21 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual(expectedCollection.Count, actual.Count);
|
|
|
|
|
Assert.That(actual, Is.EquivalentTo(expectedCollection));
|
|
|
|
|
}
|
2021-10-15 23:24:21 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Given_CharCollection_With_DefaultDisabled_ReturnsUserCollection()
|
|
|
|
|
{
|
|
|
|
|
var userCollection = new CharItem[]
|
2021-10-15 23:24:21 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
new() { Char = "test", Replacement = "replace" },
|
|
|
|
|
new() { Char = "test2", Replacement = "replace2" },
|
|
|
|
|
};
|
2021-10-15 23:24:21 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var settings = new RequestHandlerSettings
|
|
|
|
|
{
|
|
|
|
|
UserDefinedCharCollection = userCollection,
|
|
|
|
|
EnableDefaultCharReplacements = false,
|
|
|
|
|
};
|
|
|
|
|
var actual = settings.GetCharReplacements().ToList();
|
2021-10-15 23:24:21 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual(userCollection.Length, actual.Count);
|
|
|
|
|
Assert.That(actual, Is.EquivalentTo(userCollection));
|
|
|
|
|
}
|
2021-10-15 23:24:21 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Given_CharCollection_That_OverridesDefaultValues_ReturnsReplacements()
|
|
|
|
|
{
|
|
|
|
|
var userCollection = new CharItem[]
|
2021-10-15 23:24:21 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
new() { Char = "%", Replacement = "percent" },
|
|
|
|
|
new() { Char = ".", Replacement = "dot" },
|
|
|
|
|
};
|
2021-10-15 23:24:21 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var settings = new RequestHandlerSettings { UserDefinedCharCollection = userCollection };
|
|
|
|
|
var actual = settings.GetCharReplacements().ToList();
|
2021-10-15 23:24:21 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual(RequestHandlerSettings.DefaultCharCollection.Length, actual.Count);
|
2021-10-15 23:24:21 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.That(actual, Has.Exactly(1).Matches<CharItem>(x => x.Char == "%" && x.Replacement == "percent"));
|
|
|
|
|
Assert.That(actual, Has.Exactly(1).Matches<CharItem>(x => x.Char == "." && x.Replacement == "dot"));
|
|
|
|
|
Assert.That(actual, Has.Exactly(0).Matches<CharItem>(x => x.Char == "%" && x.Replacement == string.Empty));
|
|
|
|
|
Assert.That(actual, Has.Exactly(0).Matches<CharItem>(x => x.Char == "." && x.Replacement == string.Empty));
|
|
|
|
|
}
|
2021-10-15 23:24:21 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Given_CharCollection_That_OverridesDefaultValues_And_ContainsNew_ReturnsMergedWithReplacements()
|
|
|
|
|
{
|
|
|
|
|
var userCollection = new CharItem[]
|
2021-10-15 23:24:21 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
new() { Char = "%", Replacement = "percent" },
|
|
|
|
|
new() { Char = ".", Replacement = "dot" },
|
|
|
|
|
new() { Char = "new", Replacement = "new" },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var settings = new RequestHandlerSettings { UserDefinedCharCollection = userCollection };
|
|
|
|
|
var actual = settings.GetCharReplacements().ToList();
|
|
|
|
|
|
|
|
|
|
// Add 1 to the length, because we're expecting to only add one new one
|
|
|
|
|
Assert.AreEqual(RequestHandlerSettings.DefaultCharCollection.Length + 1, actual.Count);
|
|
|
|
|
|
|
|
|
|
Assert.That(actual, Has.Exactly(1).Matches<CharItem>(x => x.Char == "%" && x.Replacement == "percent"));
|
|
|
|
|
Assert.That(actual, Has.Exactly(1).Matches<CharItem>(x => x.Char == "." && x.Replacement == "dot"));
|
|
|
|
|
Assert.That(actual, Has.Exactly(1).Matches<CharItem>(x => x.Char == "new" && x.Replacement == "new"));
|
|
|
|
|
Assert.That(actual, Has.Exactly(0).Matches<CharItem>(x => x.Char == "%" && x.Replacement == string.Empty));
|
|
|
|
|
Assert.That(actual, Has.Exactly(0).Matches<CharItem>(x => x.Char == "." && x.Replacement == string.Empty));
|
2021-10-15 23:24:21 +01:00
|
|
|
}
|
|
|
|
|
}
|