2016-11-07 19:12:09 +01:00
|
|
|
|
using System;
|
2018-04-24 13:07:18 +10:00
|
|
|
|
using System.Globalization;
|
2018-04-29 20:02:38 +02:00
|
|
|
|
using System.Linq;
|
2020-02-10 13:09:26 +01:00
|
|
|
|
using System.Web;
|
2016-11-07 19:12:09 +01:00
|
|
|
|
using System.Web.Security;
|
|
|
|
|
|
using Moq;
|
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.Cache;
|
2018-07-20 16:39:39 +02:00
|
|
|
|
using Umbraco.Core.Composing;
|
2019-10-18 14:27:55 +02:00
|
|
|
|
using Umbraco.Core.Configuration;
|
2016-11-07 19:12:09 +01:00
|
|
|
|
using Umbraco.Core.Dictionary;
|
2018-06-28 23:28:22 +02:00
|
|
|
|
using Umbraco.Core.Logging;
|
2019-10-18 14:27:55 +02:00
|
|
|
|
using Umbraco.Core.Mapping;
|
2018-04-29 20:02:38 +02:00
|
|
|
|
using Umbraco.Core.Models;
|
2016-11-07 19:12:09 +01:00
|
|
|
|
using Umbraco.Core.Models.PublishedContent;
|
2020-06-11 11:21:07 +02:00
|
|
|
|
using Umbraco.Core.Templates;
|
2019-10-18 14:27:55 +02:00
|
|
|
|
using Umbraco.Core.Persistence;
|
2016-11-07 19:12:09 +01:00
|
|
|
|
using Umbraco.Core.Services;
|
2020-03-13 12:44:42 +11:00
|
|
|
|
using Umbraco.Tests.Common;
|
2016-11-07 19:12:09 +01:00
|
|
|
|
using Umbraco.Tests.TestHelpers;
|
|
|
|
|
|
using Umbraco.Tests.TestHelpers.Stubs;
|
|
|
|
|
|
using Umbraco.Web;
|
2019-02-15 11:43:56 +11:00
|
|
|
|
using Umbraco.Web.PublishedCache;
|
2016-11-07 19:12:09 +01:00
|
|
|
|
using Umbraco.Web.Routing;
|
|
|
|
|
|
using Umbraco.Web.Security;
|
2019-11-25 21:20:00 +11:00
|
|
|
|
using Umbraco.Web.Security.Providers;
|
2019-10-18 14:27:55 +02:00
|
|
|
|
using Umbraco.Web.WebApi;
|
2017-05-30 18:13:11 +02:00
|
|
|
|
using Current = Umbraco.Web.Composing.Current;
|
2016-11-07 19:12:09 +01:00
|
|
|
|
|
2016-12-16 10:40:14 +01:00
|
|
|
|
namespace Umbraco.Tests.Testing.TestingTests
|
2016-11-07 19:12:09 +01:00
|
|
|
|
{
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
|
public class MockTests : UmbracoTestBase
|
|
|
|
|
|
{
|
|
|
|
|
|
public override void SetUp()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.SetUp();
|
|
|
|
|
|
|
|
|
|
|
|
Current.UmbracoContextAccessor = new TestUmbracoContextAccessor();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void Can_Mock_Service_Context()
|
|
|
|
|
|
{
|
|
|
|
|
|
// ReSharper disable once UnusedVariable
|
|
|
|
|
|
var serviceContext = TestObjects.GetServiceContextMock();
|
|
|
|
|
|
Assert.Pass();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void Can_Mock_Umbraco_Context()
|
|
|
|
|
|
{
|
|
|
|
|
|
var umbracoContext = TestObjects.GetUmbracoContextMock(Current.UmbracoContextAccessor);
|
2019-02-14 09:49:45 +01:00
|
|
|
|
Assert.AreEqual(umbracoContext, Current.UmbracoContext);
|
2016-11-07 19:12:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void Can_Mock_Umbraco_Helper()
|
|
|
|
|
|
{
|
|
|
|
|
|
// unless we can inject them in MembershipHelper, we need need this
|
2018-11-28 12:59:40 +01:00
|
|
|
|
Composition.Register(_ => Mock.Of<IMemberService>());
|
|
|
|
|
|
Composition.Register(_ => Mock.Of<IMemberTypeService>());
|
|
|
|
|
|
Composition.Register(_ => Mock.Of<IUserService>());
|
2019-01-17 08:34:29 +01:00
|
|
|
|
Composition.Register(_ => AppCaches.Disabled);
|
2018-11-28 12:59:40 +01:00
|
|
|
|
Composition.Register<ServiceContext>();
|
2016-11-07 19:12:09 +01:00
|
|
|
|
|
|
|
|
|
|
// ReSharper disable once UnusedVariable
|
2019-02-15 11:43:56 +11:00
|
|
|
|
var helper = new UmbracoHelper(Mock.Of<IPublishedContent>(),
|
2019-01-31 15:09:31 +11:00
|
|
|
|
Mock.Of<ICultureDictionaryFactory>(),
|
2016-11-07 19:12:09 +01:00
|
|
|
|
Mock.Of<IUmbracoComponentRenderer>(),
|
2020-02-27 09:56:35 +01:00
|
|
|
|
Mock.Of<IPublishedContentQuery>());
|
2016-11-07 19:12:09 +01:00
|
|
|
|
Assert.Pass();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void Can_Mock_UrlProvider()
|
|
|
|
|
|
{
|
|
|
|
|
|
var umbracoContext = TestObjects.GetUmbracoContextMock();
|
|
|
|
|
|
|
2020-02-14 13:04:49 +01:00
|
|
|
|
|
2016-11-07 19:12:09 +01:00
|
|
|
|
var urlProviderMock = new Mock<IUrlProvider>();
|
2020-02-14 13:04:49 +01:00
|
|
|
|
urlProviderMock.Setup(provider => provider.GetUrl(It.IsAny<IPublishedContent>(), It.IsAny<UrlMode>(), It.IsAny<string>(), It.IsAny<Uri>()))
|
2018-12-18 22:02:39 +11:00
|
|
|
|
.Returns(UrlInfo.Url("/hello/world/1234"));
|
2016-11-07 19:12:09 +01:00
|
|
|
|
var urlProvider = urlProviderMock.Object;
|
|
|
|
|
|
|
2020-02-14 13:04:49 +01:00
|
|
|
|
var theUrlProvider = new UrlProvider(
|
2020-02-17 10:07:18 +01:00
|
|
|
|
new TestUmbracoContextAccessor(umbracoContext),
|
2020-03-12 09:52:34 +01:00
|
|
|
|
TestHelper.WebRoutingSettings,
|
2020-02-14 13:04:49 +01:00
|
|
|
|
new UrlProviderCollection(new [] { urlProvider }),
|
|
|
|
|
|
new MediaUrlProviderCollection( Enumerable.Empty<IMediaUrlProvider>())
|
|
|
|
|
|
, umbracoContext.VariationContextAccessor);
|
2016-11-07 19:12:09 +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.ContentType).Returns(contentType);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual("/hello/world/1234", theUrlProvider.GetUrl(publishedContent));
|
2016-11-07 19:12:09 +01:00
|
|
|
|
}
|
2019-10-18 14:27:55 +02:00
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void Can_Mock_UmbracoApiController_Dependencies_With_Injected_UmbracoMapper()
|
|
|
|
|
|
{
|
2019-11-26 12:49:57 +11:00
|
|
|
|
var logger = Mock.Of<IProfilingLogger>();
|
|
|
|
|
|
var memberService = Mock.Of<IMemberService>();
|
|
|
|
|
|
var memberTypeService = Mock.Of<IMemberTypeService>();
|
2019-11-26 12:47:48 +01:00
|
|
|
|
var membershipProvider = new MembersMembershipProvider(memberService, memberTypeService, Mock.Of<IUmbracoVersion>(), TestHelper.GetHostingEnvironment(), TestHelper.GetIpResolver());
|
2020-02-19 12:56:55 +01:00
|
|
|
|
var membershipHelper = new MembershipHelper(Mock.Of<IHttpContextAccessor>(), Mock.Of<IPublishedMemberCache>(), membershipProvider, Mock.Of<RoleProvider>(), memberService, memberTypeService, Mock.Of<IPublicAccessService>(), AppCaches.Disabled, logger, ShortStringHelper, Mock.Of<IEntityService>());
|
2019-10-18 14:27:55 +02:00
|
|
|
|
var umbracoMapper = new UmbracoMapper(new MapDefinitionCollection(new[] { Mock.Of<IMapDefinition>() }));
|
2019-11-26 12:49:57 +11:00
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
var umbracoApiController = new FakeUmbracoApiController(Mock.Of<IGlobalSettings>(), Mock.Of<IUmbracoContextAccessor>(), Mock.Of<ISqlContext>(), ServiceContext.CreatePartial(), AppCaches.NoCache, logger, Mock.Of<IRuntimeState>(), umbracoMapper, Mock.Of<IPublishedUrlProvider>());
|
2019-10-18 14:27:55 +02:00
|
|
|
|
|
|
|
|
|
|
Assert.Pass();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal class FakeUmbracoApiController : UmbracoApiController
|
|
|
|
|
|
{
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public FakeUmbracoApiController(IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoMapper umbracoMapper, IPublishedUrlProvider publishedUrlProvider)
|
|
|
|
|
|
: base(globalSettings, umbracoContextAccessor, sqlContext, services, appCaches, logger, runtimeState, umbracoMapper, publishedUrlProvider) { }
|
2016-11-07 19:12:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|