Renames/moves some stuff and adds test to show auto relation tracking

This commit is contained in:
Shannon
2019-10-25 15:08:56 +11:00
parent 25c2eed888
commit 70ccee302d
9 changed files with 78 additions and 50 deletions

View File

@@ -1522,7 +1522,7 @@
<Compile Include="TypeExtensions.cs" />
<Compile Include="Udi.cs" />
<Compile Include="UdiDefinitionAttribute.cs" />
<Compile Include="UdiEntityType.cs" />
<Compile Include="Contants-UdiEntityType.cs" />
<Compile Include="UdiGetterExtensions.cs" />
<Compile Include="UdiRange.cs" />
<Compile Include="UdiType.cs" />

View File

@@ -448,6 +448,47 @@ namespace Umbraco.Tests.Services
Assert.That(content.HasIdentity, Is.False);
}
[Test]
public void Automatically_Track_Relations()
{
var mt = MockedContentTypes.CreateSimpleMediaType("testMediaType", "Test Media Type");
ServiceContext.MediaTypeService.Save(mt);
var m1 = MockedMedia.CreateSimpleMedia(mt, "hello 1", -1);
var m2 = MockedMedia.CreateSimpleMedia(mt, "hello 1", -1);
ServiceContext.MediaService.Save(m1);
ServiceContext.MediaService.Save(m2);
var ct = MockedContentTypes.CreateTextPageContentType("richTextTest");
ct.AllowedTemplates = Enumerable.Empty<ITemplate>();
ServiceContext.ContentTypeService.Save(ct);
var c1 = MockedContent.CreateTextpageContent(ct, "my content 1", -1);
ServiceContext.ContentService.Save(c1);
var c2 = MockedContent.CreateTextpageContent(ct, "my content 2", -1);
//'bodyText' is a property with a RTE property editor which we knows tracks relations
c2.Properties["bodyText"].SetValue(@"<p>
<img src='/media/12312.jpg' data-udi='umb://media/" + m1.Key.ToString("N") + @"' />
</p><p><img src='/media/234234.jpg' data-udi=""umb://media/" + m2.Key.ToString("N") + @""" />
</p>
<p>
<a href=""{locallink:umb://document/" + c1.Key.ToString("N") + @"}"">hello</a>
</p>");
ServiceContext.ContentService.Save(c2);
var relations = ServiceContext.RelationService.GetByParentId(c2.Id).ToList();
Assert.AreEqual(3, relations.Count);
Assert.AreEqual(Constants.Conventions.RelationTypes.RelatedMediaAlias, relations[0].RelationType.Alias);
Assert.AreEqual(m1.Id, relations[0].ChildId);
Assert.AreEqual(Constants.Conventions.RelationTypes.RelatedMediaAlias, relations[1].RelationType.Alias);
Assert.AreEqual(m2.Id, relations[1].ChildId);
Assert.AreEqual(Constants.Conventions.RelationTypes.RelatedDocumentAlias, relations[2].RelationType.Alias);
Assert.AreEqual(c1.Id, relations[2].ChildId);
}
[Test]
public void Can_Create_Content_Without_Explicitly_Set_User()
{

View File

@@ -20,7 +20,7 @@ namespace Umbraco.Tests.Templates
[TestFixture]
public class ImageSourceParserTests
public class HtmlImageSourceParserTests
{
[Test]
public void Returns_Udis_From_Data_Udi_Html_Attributes()

View File

@@ -1,26 +1,44 @@
using System;
using Moq;
using NUnit.Framework;
using System;
using System.Linq;
using System.Web;
using Moq;
using NUnit.Framework;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.Services;
using Umbraco.Tests.TestHelpers;
using Umbraco.Tests.Testing.Objects;
using Umbraco.Tests.Testing.Objects.Accessors;
using Umbraco.Web;
using Umbraco.Web.PublishedCache;
using Umbraco.Web.Routing;
using Umbraco.Web.Templates;
namespace Umbraco.Tests.Web
namespace Umbraco.Tests.Templates
{
[TestFixture]
public class HtmlLocalLinkParserTests
{
[Test]
public void Returns_Udis_From_LocalLinks()
{
var input = @"<p>
<div>
<img src='/media/12312.jpg' data-udi='umb://media/D4B18427A1544721B09AC7692F35C264' />
<a href=""{locallink:umb://document/C093961595094900AAF9170DDE6AD442}"">hello</a>
</div>
</p><p><img src='/media/234234.jpg' data-udi=""umb://media-type/B726D735E4C446D58F703F3FBCFC97A5"" />
<a href=""{locallink:umb://document-type/2D692FCB070B4CDA92FB6883FDBFD6E2}"">hello</a>
</p>";
var umbracoContextAccessor = new TestUmbracoContextAccessor();
var parser = new HtmlLocalLinkParser(umbracoContextAccessor);
var result = parser.FindUdisFromLocalLinks(input).ToList();
Assert.AreEqual(2, result.Count);
Assert.AreEqual(Udi.Parse("umb://document/C093961595094900AAF9170DDE6AD442"), result[0]);
Assert.AreEqual(Udi.Parse("umb://document-type/2D692FCB070B4CDA92FB6883FDBFD6E2"), result[1]);
}
[TestCase("", "")]
[TestCase("hello href=\"{localLink:1234}\" world ", "hello href=\"/my-test-url\" world ")]
[TestCase("hello href=\"{localLink:umb://document/9931BDE0-AAC3-4BAB-B838-909A7B47570E}\" world ", "hello href=\"/my-test-url\" world ")]
@@ -40,7 +58,7 @@ namespace Umbraco.Tests.Web
var publishedContent = new Mock<IPublishedContent>();
publishedContent.Setup(x => x.Id).Returns(1234);
publishedContent.Setup(x => x.ContentType).Returns(contentType);
var mediaType = new PublishedContentType(777, "image", PublishedItemType.Media, Enumerable.Empty<string>(), Enumerable.Empty<PublishedPropertyType>(), ContentVariation.Nothing);
var media = new Mock<IPublishedContent>();
media.Setup(x => x.ContentType).Returns(mediaType);

View File

@@ -1,34 +0,0 @@
using NUnit.Framework;
using System.Linq;
using Umbraco.Core;
using Umbraco.Tests.Testing.Objects.Accessors;
using Umbraco.Web.Templates;
namespace Umbraco.Tests.Templates
{
[TestFixture]
public class LocalLinkParserTests
{
[Test]
public void Returns_Udis_From_LocalLinks()
{
var input = @"<p>
<div>
<img src='/media/12312.jpg' data-udi='umb://media/D4B18427A1544721B09AC7692F35C264' />
<a href=""{locallink:umb://document/C093961595094900AAF9170DDE6AD442}"">hello</a>
</div>
</p><p><img src='/media/234234.jpg' data-udi=""umb://media-type/B726D735E4C446D58F703F3FBCFC97A5"" />
<a href=""{locallink:umb://document-type/2D692FCB070B4CDA92FB6883FDBFD6E2}"">hello</a>
</p>";
var umbracoContextAccessor = new TestUmbracoContextAccessor();
var parser = new HtmlLocalLinkParser(umbracoContextAccessor);
var result = parser.FindUdisFromLocalLinks(input).ToList();
Assert.AreEqual(2, result.Count);
Assert.AreEqual(Udi.Parse("umb://document/C093961595094900AAF9170DDE6AD442"), result[0]);
Assert.AreEqual(Udi.Parse("umb://document-type/2D692FCB070B4CDA92FB6883FDBFD6E2"), result[1]);
}
}
}

View File

@@ -159,7 +159,7 @@
<Compile Include="Services\MemberGroupServiceTests.cs" />
<Compile Include="Services\MediaTypeServiceTests.cs" />
<Compile Include="Services\PropertyValidationServiceTests.cs" />
<Compile Include="Templates\LocalLinkParserTests.cs" />
<Compile Include="Templates\HtmlLocalLinkParserTests.cs" />
<Compile Include="TestHelpers\RandomIdRamDirectory.cs" />
<Compile Include="Testing\Objects\TestDataSource.cs" />
<Compile Include="Published\PublishedSnapshotTestObjects.cs" />
@@ -255,8 +255,7 @@
<Compile Include="Web\Controllers\UsersControllerTests.cs" />
<Compile Include="Web\HealthChecks\HealthCheckResultsTests.cs" />
<Compile Include="Web\HttpCookieExtensionsTests.cs" />
<Compile Include="Web\HtmlLocalLinkParserTests.cs" />
<Compile Include="Templates\ImageSourceParserTests.cs" />
<Compile Include="Templates\HtmlImageSourceParserTests.cs" />
<Compile Include="Web\Mvc\HtmlStringUtilitiesTests.cs" />
<Compile Include="Web\ModelStateExtensionsTests.cs" />
<Compile Include="Web\Mvc\RenderIndexActionSelectorAttributeTests.cs" />

View File

@@ -140,9 +140,13 @@ namespace Umbraco.Web.PropertyEditors
{
var asString = value == null ? string.Empty : value is string str ? str : value.ToString();
//TODO: FindUdisFromDataAttributes will return UDIs of any type found, typically these will always be "media" but
// if the text is modified to be another type it will still be returned but we will always be relating these with the RelatedMediaAlias
foreach (var udi in _imageSourceParser.FindUdisFromDataAttributes(asString))
yield return new UmbracoEntityReference(udi, Constants.Conventions.RelationTypes.RelatedMediaAlias);
//TODO: FindUdisFromLocalLinks will return UDIs of any type found, typically these will always be "document" but
// if the text is modified to be another type it will still be returned but we will always be relating these with the RelatedDocumentAlias
foreach (var udi in _localLinkParser.FindUdisFromLocalLinks(asString))
yield return new UmbracoEntityReference(udi, Constants.Conventions.RelationTypes.RelatedDocumentAlias);

View File

@@ -20,7 +20,7 @@ namespace Umbraco.Web.Templates
RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
/// <summary>
/// Parses out UDIs from an html string based on 'data-udi' html attributes
/// Parses out media UDIs from an html string based on 'data-udi' html attributes
/// </summary>
/// <param name="text"></param>
/// <returns></returns>