Adds tests for MediaParser and simplifies UmbracoContext mocking
This commit is contained in:
@@ -24,6 +24,7 @@ using Umbraco.Web.PropertyEditors;
|
||||
|
||||
namespace Umbraco.Tests.PropertyEditors
|
||||
{
|
||||
|
||||
[TestFixture]
|
||||
public class ImageCropperTest
|
||||
{
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Umbraco.Core.Models.PublishedContent;
|
||||
using Umbraco.Core.Scoping;
|
||||
using Umbraco.Web;
|
||||
using Umbraco.Web.PublishedCache.NuCache;
|
||||
using Umbraco.Web.PublishedCache.NuCache.DataSource;
|
||||
|
||||
namespace Umbraco.Tests.Testing.Objects
|
||||
{
|
||||
|
||||
internal class TestDataSource : IDataSource
|
||||
{
|
||||
public TestDataSource(params ContentNodeKit[] kits)
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
using Moq;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Core.Configuration.UmbracoSettings;
|
||||
using Umbraco.Core.Services;
|
||||
using Umbraco.Tests.TestHelpers;
|
||||
using Umbraco.Tests.Testing.Objects.Accessors;
|
||||
using Umbraco.Web;
|
||||
using Umbraco.Web.PublishedCache;
|
||||
using Umbraco.Web.Routing;
|
||||
|
||||
namespace Umbraco.Tests.Testing.Objects
|
||||
{
|
||||
/// <summary>
|
||||
/// Simplify creating test UmbracoContext's
|
||||
/// </summary>
|
||||
public class TestUmbracoContextFactory
|
||||
{
|
||||
public static IUmbracoContextFactory Create(IGlobalSettings globalSettings = null, IUrlProvider urlProvider = null,
|
||||
IMediaUrlProvider mediaUrlProvider = null,
|
||||
IUmbracoContextAccessor umbracoContextAccessor = null)
|
||||
{
|
||||
if (globalSettings == null) globalSettings = SettingsForTests.GenerateMockGlobalSettings();
|
||||
if (urlProvider == null) urlProvider = Mock.Of<IUrlProvider>();
|
||||
if (mediaUrlProvider == null) mediaUrlProvider = Mock.Of<IMediaUrlProvider>();
|
||||
if (umbracoContextAccessor == null) umbracoContextAccessor = new TestUmbracoContextAccessor();
|
||||
|
||||
var contentCache = new Mock<IPublishedContentCache>();
|
||||
var mediaCache = new Mock<IPublishedMediaCache>();
|
||||
var snapshot = new Mock<IPublishedSnapshot>();
|
||||
snapshot.Setup(x => x.Content).Returns(contentCache.Object);
|
||||
snapshot.Setup(x => x.Media).Returns(mediaCache.Object);
|
||||
var snapshotService = new Mock<IPublishedSnapshotService>();
|
||||
snapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny<string>())).Returns(snapshot.Object);
|
||||
|
||||
var umbracoContextFactory = new UmbracoContextFactory(
|
||||
umbracoContextAccessor,
|
||||
snapshotService.Object,
|
||||
new TestVariationContextAccessor(),
|
||||
new TestDefaultCultureAccessor(),
|
||||
Mock.Of<IUmbracoSettingsSection>(section => section.WebRouting == Mock.Of<IWebRoutingSection>(routingSection => routingSection.UrlProviderMode == "Auto")),
|
||||
globalSettings,
|
||||
new UrlProviderCollection(new[] { urlProvider }),
|
||||
new MediaUrlProviderCollection(new[] { mediaUrlProvider }),
|
||||
Mock.Of<IUserService>());
|
||||
|
||||
return umbracoContextFactory;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -212,6 +212,7 @@
|
||||
<Compile Include="Testing\Objects\Accessors\TestVariationContextAccessor.cs" />
|
||||
<Compile Include="Testing\ContentBaseExtensions.cs" />
|
||||
<Compile Include="Testing\Objects\Accessors\TestDefaultCultureAccessor.cs" />
|
||||
<Compile Include="Testing\Objects\TestUmbracoContextFactory.cs" />
|
||||
<Compile Include="Testing\TestDatabase.cs" />
|
||||
<Compile Include="Testing\TestingTests\NUnitTests.cs" />
|
||||
<Compile Include="Persistence\LocksTests.cs" />
|
||||
@@ -254,6 +255,7 @@
|
||||
<Compile Include="Web\HealthChecks\HealthCheckResultsTests.cs" />
|
||||
<Compile Include="Web\HttpCookieExtensionsTests.cs" />
|
||||
<Compile Include="Web\InternalLinkParserTests.cs" />
|
||||
<Compile Include="Web\MediaParserTests.cs" />
|
||||
<Compile Include="Web\Mvc\HtmlStringUtilitiesTests.cs" />
|
||||
<Compile Include="Web\ModelStateExtensionsTests.cs" />
|
||||
<Compile Include="Web\Mvc\RenderIndexActionSelectorAttributeTests.cs" />
|
||||
|
||||
@@ -8,6 +8,7 @@ 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;
|
||||
@@ -16,6 +17,7 @@ using Umbraco.Web.Templates;
|
||||
|
||||
namespace Umbraco.Tests.Web
|
||||
{
|
||||
|
||||
[TestFixture]
|
||||
public class InternalLinkParserTests
|
||||
{
|
||||
@@ -29,54 +31,40 @@ namespace Umbraco.Tests.Web
|
||||
[TestCase("hello href=\"{localLink:umb://document-type/9931BDE0-AAC3-4BAB-B838-909A7B47570E}\" world ", "hello href=\"#\" world ")]
|
||||
public void ParseLocalLinks(string input, string result)
|
||||
{
|
||||
var serviceCtxMock = new TestObjects(null).GetServiceContextMock();
|
||||
|
||||
//setup a mock url provider which we'll use for testing
|
||||
var testUrlProvider = new Mock<IUrlProvider>();
|
||||
testUrlProvider
|
||||
var contentUrlProvider = new Mock<IUrlProvider>();
|
||||
contentUrlProvider
|
||||
.Setup(x => x.GetUrl(It.IsAny<UmbracoContext>(), It.IsAny<IPublishedContent>(), It.IsAny<UrlMode>(), It.IsAny<string>(), It.IsAny<Uri>()))
|
||||
.Returns(UrlInfo.Url("/my-test-url"));
|
||||
|
||||
var globalSettings = SettingsForTests.GenerateMockGlobalSettings();
|
||||
|
||||
var contentType = new PublishedContentType(666, "alias", PublishedItemType.Content, Enumerable.Empty<string>(), Enumerable.Empty<PublishedPropertyType>(), ContentVariation.Nothing);
|
||||
var publishedContent = new Mock<IPublishedContent>();
|
||||
publishedContent.Setup(x => x.Id).Returns(1234);
|
||||
publishedContent.Setup(x => x.ContentType).Returns(contentType);
|
||||
var contentCache = new Mock<IPublishedContentCache>();
|
||||
contentCache.Setup(x => x.GetById(It.IsAny<int>())).Returns(publishedContent.Object);
|
||||
contentCache.Setup(x => x.GetById(It.IsAny<Guid>())).Returns(publishedContent.Object);
|
||||
|
||||
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.Url).Returns("/media/1001/my-image.jpg");
|
||||
media.Setup(x => x.ContentType).Returns(mediaType);
|
||||
var mediaCache = new Mock<IPublishedMediaCache>();
|
||||
mediaCache.Setup(x => x.GetById(It.IsAny<int>())).Returns(media.Object);
|
||||
mediaCache.Setup(x => x.GetById(It.IsAny<Guid>())).Returns(media.Object);
|
||||
var snapshot = new Mock<IPublishedSnapshot>();
|
||||
snapshot.Setup(x => x.Content).Returns(contentCache.Object);
|
||||
snapshot.Setup(x => x.Media).Returns(mediaCache.Object);
|
||||
var snapshotService = new Mock<IPublishedSnapshotService>();
|
||||
snapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny<string>())).Returns(snapshot.Object);
|
||||
var mediaUrlProvider = new Mock<IMediaUrlProvider>();
|
||||
mediaUrlProvider.Setup(x => x.GetMediaUrl(It.IsAny<UmbracoContext>(), It.IsAny<IPublishedContent>(), It.IsAny<string>(), It.IsAny<UrlMode>(), It.IsAny<string>(), It.IsAny<Uri>()))
|
||||
.Returns(UrlInfo.Url("/media/1001/my-image.jpg"));
|
||||
|
||||
var umbracoContextAccessor = new TestUmbracoContextAccessor();
|
||||
|
||||
var umbracoContextFactory = new UmbracoContextFactory(
|
||||
umbracoContextAccessor,
|
||||
snapshotService.Object,
|
||||
new TestVariationContextAccessor(),
|
||||
new TestDefaultCultureAccessor(),
|
||||
Mock.Of<IUmbracoSettingsSection>(section => section.WebRouting == Mock.Of<IWebRoutingSection>(routingSection => routingSection.UrlProviderMode == "Auto")),
|
||||
globalSettings,
|
||||
new UrlProviderCollection(new[] { testUrlProvider.Object }),
|
||||
new MediaUrlProviderCollection(new[] { mediaUrlProvider.Object }),
|
||||
Mock.Of<IUserService>());
|
||||
var umbracoContextFactory = TestUmbracoContextFactory.Create(
|
||||
urlProvider: contentUrlProvider.Object,
|
||||
mediaUrlProvider: mediaUrlProvider.Object,
|
||||
umbracoContextAccessor: umbracoContextAccessor);
|
||||
|
||||
using (var reference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of<HttpContextBase>()))
|
||||
{
|
||||
var contentCache = Mock.Get(reference.UmbracoContext.Content);
|
||||
contentCache.Setup(x => x.GetById(It.IsAny<int>())).Returns(publishedContent.Object);
|
||||
contentCache.Setup(x => x.GetById(It.IsAny<Guid>())).Returns(publishedContent.Object);
|
||||
|
||||
var mediaCache = Mock.Get(reference.UmbracoContext.Media);
|
||||
mediaCache.Setup(x => x.GetById(It.IsAny<int>())).Returns(media.Object);
|
||||
mediaCache.Setup(x => x.GetById(It.IsAny<Guid>())).Returns(media.Object);
|
||||
|
||||
var linkParser = new InternalLinkParser(umbracoContextAccessor);
|
||||
|
||||
var output = linkParser.EnsureInternalLinks(input);
|
||||
|
||||
97
src/Umbraco.Tests/Web/MediaParserTests.cs
Normal file
97
src/Umbraco.Tests/Web/MediaParserTests.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using Umbraco.Core.Logging;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core.Services;
|
||||
using Umbraco.Tests.Testing.Objects.Accessors;
|
||||
using Umbraco.Web.Templates;
|
||||
using Umbraco.Web;
|
||||
using Umbraco.Core.Models.PublishedContent;
|
||||
using Umbraco.Web.Routing;
|
||||
using Umbraco.Tests.Testing.Objects;
|
||||
using System.Web;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Umbraco.Core.Models;
|
||||
|
||||
namespace Umbraco.Tests.Web
|
||||
{
|
||||
[TestFixture]
|
||||
public class MediaParserTests
|
||||
{
|
||||
[Test]
|
||||
public void Remove_Image_Sources()
|
||||
{
|
||||
var logger = Mock.Of<ILogger>();
|
||||
var umbracoContextAccessor = new TestUmbracoContextAccessor();
|
||||
var mediaParser = new MediaParser(umbracoContextAccessor, logger, Mock.Of<IMediaService>(), Mock.Of<IContentTypeBaseServiceProvider>());
|
||||
|
||||
var result = mediaParser.RemoveImageSources(@"<p>
|
||||
<div>
|
||||
<img src=""/media/12354/test.jpg"" />
|
||||
</div></p>
|
||||
<p>
|
||||
<div><img src=""/media/987645/test.jpg"" data-udi=""umb://media/81BB2036-034F-418B-B61F-C7160D68DCD4"" /></div>
|
||||
</p>");
|
||||
|
||||
Assert.AreEqual(@"<p>
|
||||
<div>
|
||||
<img src=""/media/12354/test.jpg"" />
|
||||
</div></p>
|
||||
<p>
|
||||
<div><img src="""" data-udi=""umb://media/81BB2036-034F-418B-B61F-C7160D68DCD4"" /></div>
|
||||
</p>", result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Ensure_Image_Sources()
|
||||
{
|
||||
//setup a mock url provider which we'll use for testing
|
||||
|
||||
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);
|
||||
var mediaUrlProvider = new Mock<IMediaUrlProvider>();
|
||||
mediaUrlProvider.Setup(x => x.GetMediaUrl(It.IsAny<UmbracoContext>(), It.IsAny<IPublishedContent>(), It.IsAny<string>(), It.IsAny<UrlMode>(), It.IsAny<string>(), It.IsAny<Uri>()))
|
||||
.Returns(UrlInfo.Url("/media/1001/my-image.jpg"));
|
||||
|
||||
var umbracoContextAccessor = new TestUmbracoContextAccessor();
|
||||
|
||||
var umbracoContextFactory = TestUmbracoContextFactory.Create(
|
||||
mediaUrlProvider: mediaUrlProvider.Object,
|
||||
umbracoContextAccessor: umbracoContextAccessor);
|
||||
|
||||
using (var reference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of<HttpContextBase>()))
|
||||
{
|
||||
var mediaCache = Mock.Get(reference.UmbracoContext.Media);
|
||||
mediaCache.Setup(x => x.GetById(It.IsAny<Guid>())).Returns(media.Object);
|
||||
|
||||
var mediaParser = new MediaParser(umbracoContextAccessor, Mock.Of<ILogger>(), Mock.Of<IMediaService>(), Mock.Of<IContentTypeBaseServiceProvider>());
|
||||
|
||||
var result = mediaParser.EnsureImageSources(@"<p>
|
||||
<div>
|
||||
<img src="""" />
|
||||
</div></p>
|
||||
<p>
|
||||
<div><img src="""" data-udi=""umb://media/81BB2036-034F-418B-B61F-C7160D68DCD4"" /></div>
|
||||
</p>
|
||||
<p>
|
||||
<div><img src=""?width=100"" data-udi=""umb://media/81BB2036-034F-418B-B61F-C7160D68DCD4"" /></div>
|
||||
</p>");
|
||||
|
||||
Assert.AreEqual(@"<p>
|
||||
<div>
|
||||
<img src="""" />
|
||||
</div></p>
|
||||
<p>
|
||||
<div><img src=""/media/1001/my-image.jpg"" data-udi=""umb://media/81BB2036-034F-418B-B61F-C7160D68DCD4"" /></div>
|
||||
</p>
|
||||
<p>
|
||||
<div><img src=""/media/1001/my-image.jpg?width=100"" data-udi=""umb://media/81BB2036-034F-418B-B61F-C7160D68DCD4"" /></div>
|
||||
</p>", result);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ using Umbraco.Core.IO;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Services;
|
||||
using Umbraco.Web.Routing;
|
||||
|
||||
namespace Umbraco.Web.Templates
|
||||
{
|
||||
@@ -39,11 +40,13 @@ namespace Umbraco.Web.Templates
|
||||
public string EnsureImageSources(string text)
|
||||
{
|
||||
// don't attempt to proceed without a context
|
||||
if (_umbracoContextAccessor?.UmbracoContext?.Media == null)
|
||||
if (_umbracoContextAccessor?.UmbracoContext?.UrlProvider == null)
|
||||
{
|
||||
return text;
|
||||
}
|
||||
|
||||
var urlProvider = _umbracoContextAccessor.UmbracoContext.UrlProvider;
|
||||
|
||||
return ResolveImgPattern.Replace(text, match =>
|
||||
{
|
||||
// match groups:
|
||||
@@ -57,16 +60,15 @@ namespace Umbraco.Web.Templates
|
||||
{
|
||||
return match.Value;
|
||||
}
|
||||
var media = _umbracoContextAccessor?.UmbracoContext?.Media.GetById(guidUdi.Guid);
|
||||
if (media == null)
|
||||
var mediaUrl = urlProvider.GetMediaUrl(guidUdi.Guid);
|
||||
if (mediaUrl == null)
|
||||
{
|
||||
// image does not exist - we could choose to remove the image entirely here (return empty string),
|
||||
// but that would leave the editors completely in the dark as to why the image doesn't show
|
||||
return match.Value;
|
||||
}
|
||||
|
||||
var url = media.Url;
|
||||
return $"{match.Groups[1].Value}{url}{match.Groups[3].Value}{udi}{match.Groups[5].Value}";
|
||||
return $"{match.Groups[1].Value}{mediaUrl}{match.Groups[3].Value}{udi}{match.Groups[5].Value}";
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user