2017-07-20 11:21:28 +02:00
using System ;
2018-10-09 21:19:55 +02:00
using System.Linq ;
2017-05-12 14:49:44 +02:00
using System.Web ;
using Moq ;
using NUnit.Framework ;
using Umbraco.Core ;
using Umbraco.Core.Cache ;
2017-06-23 18:54:42 +02:00
using Umbraco.Core.Composing ;
2017-05-12 14:49:44 +02:00
using Umbraco.Core.Configuration.UmbracoSettings ;
using Umbraco.Core.Logging ;
using Umbraco.Core.Models ;
2018-04-29 20:02:38 +02:00
using Umbraco.Core.Models.PublishedContent ;
2018-10-09 21:19:55 +02:00
using Umbraco.Core.Services ;
2017-05-12 14:49:44 +02:00
using Umbraco.Tests.TestHelpers ;
2018-04-18 19:46:47 +02:00
using Umbraco.Tests.Testing.Objects.Accessors ;
2017-05-12 14:49:44 +02:00
using Umbraco.Web ;
using Umbraco.Web.PublishedCache ;
using Umbraco.Web.Routing ;
using Umbraco.Web.Security ;
using Umbraco.Web.Templates ;
2018-10-19 15:41:28 +11:00
using Umbraco.Core.Configuration ;
2019-02-15 08:46:57 +01:00
using Umbraco.Core.IO ;
2017-05-12 14:49:44 +02:00
namespace Umbraco.Tests.Web
{
[TestFixture]
public class TemplateUtilitiesTests
{
2017-06-23 18:54:42 +02:00
[SetUp]
public void SetUp ( )
{
2017-06-27 16:09:33 +02:00
Current . Reset ( ) ;
2019-01-27 01:17:32 -05:00
// FIXME: now UrlProvider depends on EntityService for GetUrl(guid) - this is bad
2017-06-23 18:54:42 +02:00
// should not depend on more than IdkMap maybe - fix this!
var entityService = new Mock < IEntityService > ( ) ;
2018-01-15 11:32:30 +01:00
entityService . Setup ( x = > x . GetId ( It . IsAny < Guid > ( ) , It . IsAny < UmbracoObjectTypes > ( ) ) ) . Returns ( Attempt < int > . Fail ( ) ) ;
2019-01-04 16:02:10 +01:00
var serviceContext = ServiceContext . CreatePartial ( entityService : entityService . Object ) ;
2017-06-23 18:54:42 +02:00
2019-01-27 01:17:32 -05:00
// FIXME: bad in a unit test - but Udi has a static ctor that wants it?!
2019-01-07 10:43:28 +01:00
var factory = new Mock < IFactory > ( ) ;
factory . Setup ( x = > x . GetInstance ( typeof ( TypeLoader ) ) ) . Returns (
2019-02-15 08:46:57 +01:00
new TypeLoader ( NoAppCache . Instance , IOHelper . MapPath ( "~/App_Data/TEMP" ) , new ProfilingLogger ( Mock . Of < ILogger > ( ) , Mock . Of < IProfiler > ( ) ) ) ) ;
2019-01-07 10:43:28 +01:00
factory . Setup ( x = > x . GetInstance ( typeof ( ServiceContext ) ) ) . Returns ( serviceContext ) ;
var settings = SettingsForTests . GetDefaultUmbracoSettings ( ) ;
factory . Setup ( x = > x . GetInstance ( typeof ( IUmbracoSettingsSection ) ) ) . Returns ( settings ) ;
Current . Factory = factory . Object ;
2017-06-23 18:54:42 +02:00
Umbraco . Web . Composing . Current . UmbracoContextAccessor = new TestUmbracoContextAccessor ( ) ;
2018-04-29 20:02:38 +02:00
2018-03-27 17:59:53 +02:00
Udi . ResetUdiTypes ( ) ;
2017-06-23 18:54:42 +02:00
}
[TearDown]
public void TearDown ( )
{
Current . Reset ( ) ;
}
2017-05-12 14:49:44 +02:00
[TestCase("", "")]
[TestCase("hello href=\"{localLink:1234}\" world ", "hello href=\"/my-test-url\" world ")]
2019-04-09 11:46:13 +02:00
[TestCase("hello href=\"{localLink:umb://document/9931BDE0-AAC3-4BAB-B838-909A7B47570E}\" world ", "hello href=\"/my-test-url\" world ")]
[TestCase("hello href=\"{localLink:umb://document/9931BDE0AAC34BABB838909A7B47570E}\" world ", "hello href=\"/my-test-url\" world ")]
[TestCase("hello href=\"{localLink:umb://media/9931BDE0AAC34BABB838909A7B47570E}\" world ", "hello href=\"/media/1001/my-image.jpg\" world ")]
2017-05-12 14:49:44 +02:00
//this one has an invalid char so won't match
2019-04-09 11:46:13 +02:00
[TestCase("hello href=\"{localLink:umb^://document/9931BDE0-AAC3-4BAB-B838-909A7B47570E}\" world ", "hello href=\"{localLink:umb^://document/9931BDE0-AAC3-4BAB-B838-909A7B47570E}\" world ")]
[TestCase("hello href=\"{localLink:umb://document-type/9931BDE0-AAC3-4BAB-B838-909A7B47570E}\" world ", "hello href=\"#\" world ")]
2017-05-12 14:49:44 +02:00
public void ParseLocalLinks ( string input , string result )
{
var serviceCtxMock = new TestObjects ( null ) . GetServiceContextMock ( ) ;
//setup a mock entity service from the service context to return an integer for a GUID
var entityService = Mock . Get ( serviceCtxMock . EntityService ) ;
2018-04-29 20:02:38 +02:00
//entityService.Setup(x => x.GetId(It.IsAny<Guid>(), It.IsAny<UmbracoObjectTypes>()))
// .Returns((Guid id, UmbracoObjectTypes objType) =>
// {
// return Attempt.Succeed(1234);
// });
2017-05-12 14:49:44 +02:00
2019-04-09 11:46:13 +02:00
//setup a mock url provider which we'll use for testing
2017-05-12 14:49:44 +02:00
var testUrlProvider = new Mock < IUrlProvider > ( ) ;
2018-04-29 20:02:38 +02:00
testUrlProvider
. Setup ( x = > x . GetUrl ( It . IsAny < UmbracoContext > ( ) , It . IsAny < IPublishedContent > ( ) , It . IsAny < UrlProviderMode > ( ) , It . IsAny < string > ( ) , It . IsAny < Uri > ( ) ) )
2018-12-18 22:02:39 +11:00
. Returns ( ( UmbracoContext umbCtx , IPublishedContent content , UrlProviderMode mode , string culture , Uri url ) = > UrlInfo . Url ( "/my-test-url" ) ) ;
2017-05-12 14:49:44 +02:00
2018-04-06 13:51:54 +10:00
var globalSettings = SettingsForTests . GenerateMockGlobalSettings ( ) ;
2018-11-27 13:46:43 +01:00
2018-06-20 14:18:57 +02:00
var contentType = new PublishedContentType ( 666 , "alias" , PublishedItemType . Content , Enumerable . Empty < string > ( ) , Enumerable . Empty < PublishedPropertyType > ( ) , ContentVariation . Nothing ) ;
2018-04-29 20:02:38 +02:00
var publishedContent = Mock . Of < IPublishedContent > ( ) ;
Mock . Get ( publishedContent ) . Setup ( x = > x . Id ) . Returns ( 1234 ) ;
Mock . Get ( publishedContent ) . Setup ( x = > x . ContentType ) . Returns ( contentType ) ;
var contentCache = Mock . Of < IPublishedContentCache > ( ) ;
Mock . Get ( contentCache ) . Setup ( x = > x . GetById ( It . IsAny < int > ( ) ) ) . Returns ( publishedContent ) ;
Mock . Get ( contentCache ) . Setup ( x = > x . GetById ( It . IsAny < Guid > ( ) ) ) . Returns ( publishedContent ) ;
var snapshot = Mock . Of < IPublishedSnapshot > ( ) ;
Mock . Get ( snapshot ) . Setup ( x = > x . Content ) . Returns ( contentCache ) ;
var snapshotService = Mock . Of < IPublishedSnapshotService > ( ) ;
Mock . Get ( snapshotService ) . Setup ( x = > x . CreatePublishedSnapshot ( It . IsAny < string > ( ) ) ) . Returns ( snapshot ) ;
2019-04-09 11:46:13 +02:00
var media = Mock . Of < IPublishedContent > ( ) ;
Mock . Get ( media ) . Setup ( x = > x . Url ) . Returns ( "/media/1001/my-image.jpg" ) ;
var mediaCache = Mock . Of < IPublishedMediaCache > ( ) ;
Mock . Get ( mediaCache ) . Setup ( x = > x . GetById ( It . IsAny < Guid > ( ) ) ) . Returns ( media ) ;
2018-04-29 20:02:38 +02:00
2019-02-14 12:11:06 +01:00
var umbracoContextFactory = new UmbracoContextFactory (
2017-06-23 18:54:42 +02:00
Umbraco . Web . Composing . Current . UmbracoContextAccessor ,
2018-04-29 20:02:38 +02:00
snapshotService ,
2019-02-14 12:11:06 +01:00
new TestVariationContextAccessor ( ) ,
new TestDefaultCultureAccessor ( ) ,
2019-01-31 00:57:10 +11:00
Mock . Of < IUmbracoSettingsSection > ( section = > section . WebRouting = = Mock . Of < IWebRoutingSection > ( routingSection = > routingSection . UrlProviderMode = = "Auto" ) ) ,
2018-04-06 13:51:54 +10:00
globalSettings ,
2019-03-11 15:36:39 +01:00
new UrlProviderCollection ( new [ ] { testUrlProvider . Object } ) ,
2019-04-15 15:57:35 +02:00
new MediaUrlProviderCollection ( Enumerable . Empty < IMediaUrlProvider > ( ) ) ,
2019-02-14 12:11:06 +01:00
Mock . Of < IUserService > ( ) ) ;
using ( var reference = umbracoContextFactory . EnsureUmbracoContext ( Mock . Of < HttpContextBase > ( ) ) )
2017-05-12 14:49:44 +02:00
{
2019-04-09 11:46:13 +02:00
var output = TemplateUtilities . ParseInternalLinks ( input , reference . UmbracoContext . UrlProvider , mediaCache ) ;
2017-05-12 14:49:44 +02:00
Assert . AreEqual ( result , output ) ;
}
}
}
2017-07-20 11:21:28 +02:00
}