|
| 1 | +using Microsoft.Extensions.Logging; |
| 2 | +using Microsoft.Extensions.Options; |
| 3 | +using Moq; |
| 4 | +using NUnit.Framework; |
| 5 | +using Umbraco.Cms.Core.Configuration.Models; |
| 6 | +using Umbraco.Cms.Core.Models; |
| 7 | +using Umbraco.Cms.Core.PropertyEditors; |
| 8 | +using Umbraco.Cms.Core.Serialization; |
| 9 | + |
| 10 | +namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.PropertyEditors; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Tests for <see cref="RichTextPropertyIndexValueFactory"/> to ensure it correctly creates index values from rich text properties. |
| 14 | +/// </summary> |
| 15 | +public class RichTextPropertyIndexValueFactoryTests |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Tests that the factory can create index values from a rich text property with valid content |
| 19 | + /// </summary> |
| 20 | + /// <param name="testContent"></param> |
| 21 | + /// <param name="expected"></param> |
| 22 | + [TestCase("<p>Sample text</p>", "Sample text")] |
| 23 | + [TestCase("<p>John Smith<br>Company ABC<br>London</p>", "John Smith Company ABC London")] |
| 24 | + [TestCase("<p>John Smith<break>Company ABC<break>London</p>", "John SmithCompany ABCLondon")] |
| 25 | + [TestCase("<p>John Smith<br>Company ABC<branything>London</p>", "John Smith Company ABCLondon")] |
| 26 | + [TestCase("<p>Another sample text with <strong>bold</strong> content</p>", "Another sample text with bold content")] |
| 27 | + [TestCase("<p>Text with <a href=\"https://example.com\">link</a></p>", "Text with link")] |
| 28 | + [TestCase("<p>Text with <img src=\"image.jpg\" alt=\"image\" /></p>", "Text with")] |
| 29 | + [TestCase("<p>Text with <span style=\"color: red;\">styled text</span></p>", "Text with styled text")] |
| 30 | + [TestCase("<p>Text with <em>emphasized</em> content</p>", "Text with emphasized content")] |
| 31 | + [TestCase("<p>Text with <u>underlined</u> content</p>", "Text with underlined content")] |
| 32 | + [TestCase("<p>Text with <code>inline code</code></p>", "Text with inline code")] |
| 33 | + [TestCase("<p>Text with <pre><code>code block</code></pre></p>", "Text with code block")] |
| 34 | + [TestCase("<p>Text with <blockquote>quoted text</blockquote></p>", "Text with quoted text")] |
| 35 | + [TestCase("<p>Text with <ul><li>list item 1</li><li>list item 2</li></ul></p>", |
| 36 | + "Text with list item 1list item 2")] |
| 37 | + [TestCase("<p>Text with <ol><li>ordered item 1</li><li>ordered item 2</li></ol></p>", |
| 38 | + "Text with ordered item 1ordered item 2")] |
| 39 | + [TestCase("<p>Text with <div class=\"class-name\">div content</div></p>", "Text with div content")] |
| 40 | + [TestCase("<p>Text with <span class=\"class-name\">span content</span></p>", "Text with span content")] |
| 41 | + [TestCase("<p>Text with <strong>bold</strong> and <em>italic</em> content</p>", |
| 42 | + "Text with bold and italic content")] |
| 43 | + [TestCase("<p>Text with <a href=\"https://example.com\" target=\"_blank\">external link</a></p>", |
| 44 | + "Text with external link")] |
| 45 | + [TestCase("<p>John Smith<br class=\"test\">Company ABC<br>London</p>", "John Smith Company ABC London")] |
| 46 | + [TestCase("<p>John Smith<br \r\n />Company ABC<br>London</p>", "John Smith Company ABC London")] |
| 47 | + public void Can_Create_Index_Values_From_RichText_Property(string testContent, string expected) |
| 48 | + { |
| 49 | + var propertyEditorCollection = new PropertyEditorCollection(new DataEditorCollection(() => null)); |
| 50 | + var jsonSerializer = Mock.Of<IJsonSerializer>(); |
| 51 | + var indexingSettings = Mock.Of<IOptionsMonitor<IndexingSettings>>(); |
| 52 | + Mock.Get(indexingSettings).Setup(x => x.CurrentValue).Returns(new IndexingSettings { }); |
| 53 | + var logger = Mock.Of<ILogger<RichTextPropertyIndexValueFactory>>(); |
| 54 | + string alias = "richText"; |
| 55 | + |
| 56 | + var factory = new RichTextPropertyIndexValueFactory( |
| 57 | + propertyEditorCollection, |
| 58 | + jsonSerializer, |
| 59 | + indexingSettings, |
| 60 | + logger); |
| 61 | + |
| 62 | + // create a mock property with the rich text value |
| 63 | + var property = Mock.Of<IProperty>(p => p.Alias == alias |
| 64 | + && (string)p.GetValue(It.IsAny<string>(), It.IsAny<string>(), |
| 65 | + It.IsAny<bool>()) == testContent); |
| 66 | + |
| 67 | + // get the index value for the property |
| 68 | + var indexValue = factory |
| 69 | + .GetIndexValues(property, null, null, true, [], new Dictionary<Guid, IContentType>()) |
| 70 | + .FirstOrDefault(kvp => kvp.FieldName == alias); |
| 71 | + Assert.IsNotNull(indexValue); |
| 72 | + |
| 73 | + // assert that index the value is created correctly (it might contain a trailing whitespace, but that's OK) |
| 74 | + var expectedIndexValue = indexValue.Values.SingleOrDefault() as string; |
| 75 | + Assert.IsNotNull(expectedIndexValue); |
| 76 | + Assert.AreEqual(expected, expectedIndexValue.TrimEnd()); |
| 77 | + } |
| 78 | +} |
0 commit comments