|
| 1 | +using System.Linq; |
| 2 | +using NUnit.Framework; |
| 3 | +using Umbraco.Cms.Core.Configuration.Models; |
| 4 | +using Umbraco.Cms.Core.Configuration.UmbracoSettings; |
| 5 | + |
| 6 | +namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Configuration.Models |
| 7 | +{ |
| 8 | + [TestFixture] |
| 9 | + public class RequestHandlerSettingsTests |
| 10 | + { |
| 11 | + [Test] |
| 12 | + public void Given_CharCollection_With_DefaultEnabled_MergesCollection() |
| 13 | + { |
| 14 | + var userCollection = new CharacterReplacement[] |
| 15 | + { |
| 16 | + new() { Char = "test", Replacement = "replace" }, |
| 17 | + new() { Char = "test2", Replacement = "replace2" } |
| 18 | + }; |
| 19 | + |
| 20 | + |
| 21 | + var settings = new RequestHandlerSettings { CharCollection = userCollection }; |
| 22 | + var actual = settings.GetCharReplacements().ToList(); |
| 23 | + |
| 24 | + var expectedCollection = RequestHandlerSettings.DefaultCharCollection.ToList(); |
| 25 | + expectedCollection.AddRange(userCollection); |
| 26 | + |
| 27 | + Assert.AreEqual(expectedCollection.Count, actual.Count); |
| 28 | + Assert.That(actual, Is.EquivalentTo(expectedCollection)); |
| 29 | + } |
| 30 | + |
| 31 | + [Test] |
| 32 | + public void Given_CharCollection_With_DefaultDisabled_ReturnsUserCollection() |
| 33 | + { |
| 34 | + var userCollection = new CharacterReplacement[] |
| 35 | + { |
| 36 | + new() { Char = "test", Replacement = "replace" }, |
| 37 | + new() { Char = "test2", Replacement = "replace2" } |
| 38 | + }; |
| 39 | + |
| 40 | + var settings = new RequestHandlerSettings { CharCollection = userCollection, EnableDefaultCharReplacements = false }; |
| 41 | + var actual = settings.GetCharReplacements().ToList(); |
| 42 | + |
| 43 | + Assert.AreEqual(userCollection.Length, actual.Count); |
| 44 | + Assert.That(actual, Is.EquivalentTo(userCollection)); |
| 45 | + } |
| 46 | + |
| 47 | + [Test] |
| 48 | + public void Given_CharCollection_That_OverridesDefaultValues_ReturnsReplacements() |
| 49 | + { |
| 50 | + var userCollection = new CharacterReplacement[] |
| 51 | + { |
| 52 | + new() { Char = "%", Replacement = "percent" }, |
| 53 | + new() { Char = ".", Replacement = "dot" } |
| 54 | + }; |
| 55 | + |
| 56 | + var settings = new RequestHandlerSettings { CharCollection = userCollection }; |
| 57 | + var actual = settings.GetCharReplacements().ToList(); |
| 58 | + |
| 59 | + Assert.AreEqual(RequestHandlerSettings.DefaultCharCollection.Length, actual.Count); |
| 60 | + |
| 61 | + Assert.That(actual, Has.Exactly(1).Matches<CharacterReplacement>(x => x.Char == "%" && x.Replacement == "percent")); |
| 62 | + Assert.That(actual, Has.Exactly(1).Matches<CharacterReplacement>(x => x.Char == "." && x.Replacement == "dot")); |
| 63 | + Assert.That(actual, Has.Exactly(0).Matches<CharacterReplacement>(x => x.Char == "%" && x.Replacement == string.Empty)); |
| 64 | + Assert.That(actual, Has.Exactly(0).Matches<CharacterReplacement>(x => x.Char == "." && x.Replacement == string.Empty)); |
| 65 | + } |
| 66 | + |
| 67 | + [Test] |
| 68 | + public void Given_CharCollection_That_OverridesDefaultValues_And_ContainsNew_ReturnsMergedWithReplacements() |
| 69 | + { |
| 70 | + var userCollection = new CharacterReplacement[] |
| 71 | + { |
| 72 | + new() { Char = "%", Replacement = "percent" }, |
| 73 | + new() { Char = ".", Replacement = "dot" }, |
| 74 | + new() {Char = "new", Replacement = "new"} |
| 75 | + }; |
| 76 | + |
| 77 | + var settings = new RequestHandlerSettings { CharCollection = userCollection }; |
| 78 | + var actual = settings.GetCharReplacements().ToList(); |
| 79 | + |
| 80 | + // Add 1 to the length, because we're expecting to only add one new one |
| 81 | + Assert.AreEqual(RequestHandlerSettings.DefaultCharCollection.Length + 1, actual.Count); |
| 82 | + |
| 83 | + Assert.That(actual, Has.Exactly(1).Matches<CharacterReplacement>(x => x.Char == "%" && x.Replacement == "percent")); |
| 84 | + Assert.That(actual, Has.Exactly(1).Matches<CharacterReplacement>(x => x.Char == "." && x.Replacement == "dot")); |
| 85 | + Assert.That(actual, Has.Exactly(1).Matches<CharacterReplacement>(x => x.Char == "new" && x.Replacement == "new")); |
| 86 | + Assert.That(actual, Has.Exactly(0).Matches<CharacterReplacement>(x => x.Char == "%" && x.Replacement == string.Empty)); |
| 87 | + Assert.That(actual, Has.Exactly(0).Matches<CharacterReplacement>(x => x.Char == "." && x.Replacement == string.Empty)); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments