Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="autoFixture"></param>
        /// <param name="mockServiceContainer">A service container with mockable services. One is created if it isn't provided. Provide to manually mock all Umbraco services</param>
        /// <param name="EnforceUniqueContentIds"></param>
        public UmbracoUnitTestEngine(Fixture autoFixture = null, MockServiceContext mockServiceContainer = null, bool EnforceUniqueContentIds = true)
        {
            _Fixture = autoFixture ?? new Fixture();

            Content = new List <IPublishedContent>();
            Media   = new List <IPublishedContent>();

            _mocks             = new MockContainer();
            mockServiceContext = mockServiceContainer ?? new MockServiceContext();

            this.ApplicationContext = UmbracoUnitTestHelper.GetApplicationContext(serviceContext: this.ServiceContext,
                                                                                  logger: _mocks.ResolveObject <ILogger>());

            this.UmbracoContext = UmbracoUnitTestHelper.GetUmbracoContext(ApplicationContext,
                                                                          httpContext: _mocks.ResolveObject <HttpContextBase>()
                                                                          , webRoutingSettings: _mocks.ResolveObject <IWebRoutingSection>(),
                                                                          webSecurity: _mocks.ResolveObject <WebSecurity>(null, _mocks.ResolveObject <HttpContextBase>(), null));

            _mocks.Resolve <IWebRoutingSection>().Setup(s => s.UrlProviderMode).Returns(UrlProviderMode.AutoLegacy.ToString()); //needed for currenttemplate, IPublishedContent.UrlAbsolute

            this.EnforceUniqueContentIds = EnforceUniqueContentIds;

            AffectsController(true, GiveRenderMvcControllerPublishedContextRouteData);

            _boot = UmbracoUnitTestHelper.GetCustomBootManager(serviceContext: ServiceContext);
        }
 public static CoreBootManager StartCoreBootManager(CustomBoot bm = null)
 {
     bm = bm ?? GetCustomBootManager();
     if (!bm.Initialized)
     {
         bm.Initialize();
     }
     if (!bm.Started)
     {
         bm.Startup(null);
     }
     if (!bm.Completed)
     {
         bm.Complete(null);
     }
     return(bm);
 }
        public void BasicApiHasPropertyTest()
        {
            //create a mock of the content type service
            var mockContentService = new Mock <IContentTypeService>();
            //this time we will make our own service context, which can take in all of the umbraco services
            //Pass the context the mocked content service object
            //core boot manager requires Services.TextService to not be null (pass in mock of ILocalizedTextService)
            var serviceContext = new ServiceContext(contentTypeService: mockContentService.Object, localizedTextService: Mock.Of <ILocalizedTextService>());

            var appCtx = ApplicationContext.EnsureContext(
                new DatabaseContext(Mock.Of <IDatabaseFactory>(), Mock.Of <ILogger>(), new SqlSyntaxProviders(new[] { Mock.Of <ISqlSyntaxProvider>() })),
                serviceContext,
                CacheHelper.CreateDisabledCacheHelper(),
                new ProfilingLogger(
                    Mock.Of <ILogger>(),
                    Mock.Of <IProfiler>()), true);

            var ctx = UmbracoContext.EnsureContext(
                Mock.Of <HttpContextBase>(),
                appCtx,
                new Mock <WebSecurity>(null, null).Object,
                Mock.Of <IUmbracoSettingsSection>(),
                Enumerable.Empty <IUrlProvider>(), true);

            //Have to use an inherited instance of boot manager to remove methods we can't use
            var bm = new CustomBoot(new UmbracoApplication(), serviceContext);

            bm.Initialize().Startup(null).Complete(null);

            string ctAlias      = "testAlias";
            string propertyName = "testProp";

            //THIS TIME we do need a property type defined.... this is more complicated...
            var mockContentType = new Mock <IContentType>();

            mockContentType.Setup(s => s.Alias).Returns(ctAlias);
            mockContentType.Setup(s => s.CompositionPropertyTypes).Returns(new PropertyType[] { new PropertyType(propertyName, DataTypeDatabaseType.Nvarchar, propertyName) });

            mockContentService.Setup(s => s.GetContentType(ctAlias)).Returns(mockContentType.Object);

            var ContentType = PublishedContentType.Get(PublishedItemType.Content, ctAlias);

            var contentId = 2;
            //get a mocked IPublishedContent
            var contentMock = new Mock <IPublishedContent>();

            contentMock.Setup(s => s.ContentType).Returns(ContentType);

            var mockedTypedQuery = new Mock <ITypedPublishedContentQuery>();

            mockedTypedQuery.Setup(s => s.TypedContent(contentId)).Returns(contentMock.Object);

            //give our dynamic query mock to the longer version of the UmbracoHelper constructor
            var helper = new UmbracoHelper(ctx,
                                           Mock.Of <IPublishedContent>(),
                                           mockedTypedQuery.Object,
                                           Mock.Of <IDynamicPublishedContentQuery>(),
                                           Mock.Of <ITagQuery>(),
                                           Mock.Of <IDataTypeService>(),
                                           new UrlProvider(ctx, Mock.Of <IWebRoutingSection>(section => section.UrlProviderMode == UrlProviderMode.Auto.ToString()), new[] { Mock.Of <IUrlProvider>() }),
                                           Mock.Of <ICultureDictionary>(),
                                           Mock.Of <IUmbracoComponentRenderer>(),
                                           new MembershipHelper(ctx, Mock.Of <MembershipProvider>(), Mock.Of <RoleProvider>()));

            var controller = new BasicUmbracoApiController(ctx, helper);
            var res        = controller.BasicHasPropertyAction(contentId, propertyName);

            Assert.IsTrue(res);

            //clean up resolved so we can use this again...
            appCtx.DisposeIfDisposable();
        }