public void Get_Media_Url_Can_Resolve_Variant_Property_Url() { var umbracoContext = GetUmbracoContext("http://localhost"); var umbracoFilePropertyType = CreatePropertyType(Constants.PropertyEditors.Aliases.UploadField, null, ContentVariation.Culture); const string enMediaUrl = "/media/rfeiw584/en.jpg"; const string daMediaUrl = "/media/uf8ewud2/da.jpg"; var property = new SolidPublishedPropertyWithLanguageVariants { Alias = "umbracoFile", PropertyType = umbracoFilePropertyType, }; property.SetSourceValue("en", enMediaUrl, true); property.SetSourceValue("da", daMediaUrl); var contentType = new PublishedContentType(Guid.NewGuid(), 666, "alias", PublishedItemType.Content, Enumerable.Empty <string>(), new [] { umbracoFilePropertyType }, ContentVariation.Culture); var publishedContent = new SolidPublishedContent(contentType) { Properties = new[] { property } }; var resolvedUrl = GetPublishedUrlProvider(umbracoContext).GetMediaUrl(publishedContent, UrlMode.Auto, "da"); Assert.AreEqual(daMediaUrl, resolvedUrl); }
public void SingleNestedTest() { (var contentType1, _) = CreateContentTypes(); // nested single converter returns the proper value clr type TestModel, and cache level Assert.AreEqual(typeof(TestElementModel), contentType1.GetPropertyType("property1").ClrType); Assert.AreEqual(PropertyCacheLevel.Element, contentType1.GetPropertyType("property1").CacheLevel); var key = Guid.NewGuid(); var keyA = Guid.NewGuid(); var content = new SolidPublishedContent(contentType1) { Key = key, Properties = new [] { new TestPublishedProperty(contentType1.GetPropertyType("property1"), $@"[ {{ ""key"": ""{keyA}"", ""propertyN1"": ""foo"", ""ncContentTypeAlias"": ""contentN1"" }} ]") } }; var value = content.Value("property1"); // nested single converter returns proper TestModel value Assert.IsInstanceOf <TestElementModel>(value); var valueM = (TestElementModel)value; Assert.AreEqual("foo", valueM.PropValue); Assert.AreEqual(keyA, valueM.Key); }
public void ManyNestedTest() { (_, var contentType2) = CreateContentTypes(); // nested many converter returns the proper value clr type IEnumerable<TestModel>, and cache level Assert.AreEqual(typeof(IEnumerable <TestElementModel>), contentType2.GetPropertyType("property2").ClrType); Assert.AreEqual(PropertyCacheLevel.Element, contentType2.GetPropertyType("property2").CacheLevel); var key = Guid.NewGuid(); var keyA = Guid.NewGuid(); var keyB = Guid.NewGuid(); var content = new SolidPublishedContent(contentType2) { Key = key, Properties = new[] { new TestPublishedProperty(contentType2.GetPropertyType("property2"), $@"[ {{ ""key"": ""{keyA}"", ""propertyN1"": ""foo"", ""ncContentTypeAlias"": ""contentN1"" }}, {{ ""key"": ""{keyB}"", ""propertyN1"": ""bar"", ""ncContentTypeAlias"": ""contentN1"" }} ]") } }; var value = content.Value("property2"); // nested many converter returns proper IEnumerable<TestModel> value Assert.IsInstanceOf <IEnumerable <IPublishedElement> >(value); Assert.IsInstanceOf <IEnumerable <TestElementModel> >(value); var valueM = ((IEnumerable <TestElementModel>)value).ToArray(); Assert.AreEqual("foo", valueM[0].PropValue); Assert.AreEqual(keyA, valueM[0].Key); Assert.AreEqual("bar", valueM[1].PropValue); Assert.AreEqual(keyB, valueM[1].Key); }
public void Get_Url_For_Culture_Variant_Non_Current_Url() { const string currentUri = "http://example.us/test"; var globalSettings = Mock.Get(Factory.GetInstance <IGlobalSettings>()); //this will modify the IGlobalSettings instance stored in the container globalSettings.Setup(x => x.HideTopLevelNodeFromPath).Returns(false); var umbracoSettings = Current.Configs.Settings(); var contentType = new PublishedContentType(Guid.NewGuid(), 666, "alias", PublishedItemType.Content, Enumerable.Empty <string>(), Enumerable.Empty <PublishedPropertyType>(), ContentVariation.Culture); var publishedContent = new SolidPublishedContent(contentType) { Id = 1234 }; var publishedContentCache = new Mock <IPublishedContentCache>(); publishedContentCache.Setup(x => x.GetRouteById(1234, "fr-FR")) .Returns("9876/home/test-fr"); //prefix with the root id node with the domain assigned as per the umbraco standard publishedContentCache.Setup(x => x.GetById(It.IsAny <int>())) .Returns <int>(id => id == 1234 ? publishedContent : null); var domainCache = new Mock <IDomainCache>(); domainCache.Setup(x => x.GetAssigned(It.IsAny <int>(), false)) .Returns((int contentId, bool includeWildcards) => { if (contentId != 9876) { return(Enumerable.Empty <Domain>()); } return(new[] { new Domain(2, "example.us", 9876, CultureInfo.GetCultureInfo("en-US"), false), //default new Domain(3, "example.fr", 9876, CultureInfo.GetCultureInfo("fr-FR"), false) }); }); domainCache.Setup(x => x.DefaultCulture).Returns(CultureInfo.GetCultureInfo("en-US").Name); var snapshot = Mock.Of <IPublishedSnapshot>(x => x.Content == publishedContentCache.Object && x.Domains == domainCache.Object); var snapshotService = new Mock <IPublishedSnapshotService>(); snapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny <string>())) .Returns(snapshot); var umbracoContext = GetUmbracoContext(currentUri, umbracoSettings: umbracoSettings, urlProviders: new[] { new DefaultUrlProvider(umbracoSettings.RequestHandler, Logger, globalSettings.Object, new SiteDomainHelper()) }, globalSettings: globalSettings.Object, snapshotService: snapshotService.Object); var url = umbracoContext.UrlProvider.GetUrl(1234, culture: "fr-FR"); //the current uri is not the culture specific domain we want, so the result is an absolute path to the culture specific domain Assert.AreEqual("http://example.fr/home/test-fr/", url); }
public void Get_Url_For_Culture_Variant_With_Current_Url() { const string currentUri = "http://example.fr/test"; var requestHandlerSettings = new RequestHandlerSettings { AddTrailingSlash = true }; var contentType = new PublishedContentType(Guid.NewGuid(), 666, "alias", PublishedItemType.Content, Enumerable.Empty <string>(), Enumerable.Empty <PublishedPropertyType>(), ContentVariation.Culture); var publishedContent = new SolidPublishedContent(contentType) { Id = 1234 }; var publishedContentCache = new Mock <IPublishedContentCache>(); publishedContentCache.Setup(x => x.GetRouteById(1234, "fr-FR")) .Returns("9876/home/test-fr"); //prefix with the root id node with the domain assigned as per the umbraco standard publishedContentCache.Setup(x => x.GetById(It.IsAny <int>())) .Returns <int>(id => id == 1234 ? publishedContent : null); var domainCache = new Mock <IDomainCache>(); domainCache.Setup(x => x.GetAssigned(It.IsAny <int>(), false)) .Returns((int contentId, bool includeWildcards) => { if (contentId != 9876) { return(Enumerable.Empty <Domain>()); } return(new[] { new Domain(2, "example.us", 9876, "en-US", false), //default new Domain(3, "example.fr", 9876, "fr-FR", false) }); }); domainCache.Setup(x => x.DefaultCulture).Returns(CultureInfo.GetCultureInfo("en-US").Name); var snapshot = Mock.Of <IPublishedSnapshot>(x => x.Content == publishedContentCache.Object && x.Domains == domainCache.Object); var snapshotService = new Mock <IPublishedSnapshotService>(); snapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny <string>())) .Returns(snapshot); var umbracoContext = GetUmbracoContext(currentUri, globalSettings: _globalSettings, snapshotService: snapshotService.Object); var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext); var urlProvider = new DefaultUrlProvider( Microsoft.Extensions.Options.Options.Create(requestHandlerSettings), LoggerFactory.CreateLogger <DefaultUrlProvider>(), new SiteDomainMapper(), umbracoContextAccessor, UriUtility); var publishedUrlProvider = GetPublishedUrlProvider(umbracoContext, urlProvider); var url = publishedUrlProvider.GetUrl(1234, culture: "fr-FR"); Assert.AreEqual("/home/test-fr/", url); }
public void Get_Url_For_Culture_Variant_Without_Domains_Non_Current_Url() { const string currentUri = "http://example.us/test"; var globalSettings = Mock.Get(Factory.GetInstance <IGlobalSettings>()); //this will modify the IGlobalSettings instance stored in the container globalSettings.Setup(x => x.HideTopLevelNodeFromPath).Returns(false); var umbracoSettings = Current.Configs.Settings(); var contentType = new PublishedContentType(Guid.NewGuid(), 666, "alias", PublishedItemType.Content, Enumerable.Empty <string>(), Enumerable.Empty <PublishedPropertyType>(), ContentVariation.Culture); var publishedContent = new SolidPublishedContent(contentType) { Id = 1234 }; var publishedContentCache = new Mock <IPublishedContentCache>(); publishedContentCache.Setup(x => x.GetRouteById(1234, "fr-FR")) .Returns("9876/home/test-fr"); //prefix with the root id node with the domain assigned as per the umbraco standard publishedContentCache.Setup(x => x.GetById(It.IsAny <int>())) .Returns <int>(id => id == 1234 ? publishedContent : null); var domainCache = new Mock <IDomainCache>(); domainCache.Setup(x => x.GetAssigned(It.IsAny <int>(), false)) .Returns((int contentId, bool includeWildcards) => Enumerable.Empty <Domain>()); var snapshot = Mock.Of <IPublishedSnapshot>(x => x.Content == publishedContentCache.Object && x.Domains == domainCache.Object); var snapshotService = new Mock <IPublishedSnapshotService>(); snapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny <string>())) .Returns(snapshot); var umbracoContext = GetUmbracoContext(currentUri, umbracoSettings: umbracoSettings, urlProviders: new[] { new DefaultUrlProvider(umbracoSettings.RequestHandler, Logger, globalSettings.Object, new SiteDomainHelper()) }, globalSettings: globalSettings.Object, snapshotService: snapshotService.Object); //even though we are asking for a specific culture URL, there are no domains assigned so all that can be returned is a normal relative URL. var url = umbracoContext.UrlProvider.GetUrl(1234, culture: "fr-FR"); Assert.AreEqual("/home/test-fr/", url); }
public void SimpleConverter2Test() { var cacheMock = new Mock <IPublishedContentCache>(); var cacheContent = new Dictionary <int, IPublishedContent>(); cacheMock.Setup(x => x.GetById(It.IsAny <int>())).Returns <int>(id => cacheContent.TryGetValue(id, out IPublishedContent content) ? content : null); var publishedSnapshotMock = new Mock <IPublishedSnapshot>(); publishedSnapshotMock.Setup(x => x.Content).Returns(cacheMock.Object); var publishedSnapshotAccessorMock = new Mock <IPublishedSnapshotAccessor>(); publishedSnapshotAccessorMock.Setup(x => x.PublishedSnapshot).Returns(publishedSnapshotMock.Object); var publishedSnapshotAccessor = publishedSnapshotAccessorMock.Object; var converters = new PropertyValueConverterCollection(new IPropertyValueConverter[] { new SimpleConverter2(publishedSnapshotAccessor), }); var dataTypeService = new TestObjects.TestDataTypeService( new DataType(new VoidEditor(Mock.Of <ILogger>())) { Id = 1 }); var contentTypeFactory = new PublishedContentTypeFactory(Mock.Of <IPublishedModelFactory>(), converters, dataTypeService); IEnumerable <IPublishedPropertyType> CreatePropertyTypes(IPublishedContentType contentType) { yield return(contentTypeFactory.CreatePropertyType(contentType, "prop1", 1)); } var elementType1 = contentTypeFactory.CreateContentType(1000, "element1", CreatePropertyTypes); var element1 = new PublishedElement(elementType1, Guid.NewGuid(), new Dictionary <string, object> { { "prop1", "1234" } }, false); var cntType1 = contentTypeFactory.CreateContentType(1001, "cnt1", t => Enumerable.Empty <PublishedPropertyType>()); var cnt1 = new SolidPublishedContent(cntType1) { Id = 1234 }; cacheContent[cnt1.Id] = cnt1; Assert.AreSame(cnt1, element1.Value("prop1")); }
public void SimpleConverter3Test() { Current.Reset(); var register = RegisterFactory.Create(); var composition = new Composition(register, new TypeLoader(), Mock.Of <IProfilingLogger>(), ComponentTests.MockRuntimeState(RuntimeLevel.Run)); composition.WithCollectionBuilder <PropertyValueConverterCollectionBuilder>() .Append <SimpleConverter3A>() .Append <SimpleConverter3B>(); IPublishedModelFactory factory = new PublishedModelFactory(new[] { typeof(PublishedSnapshotTestObjects.TestElementModel1), typeof(PublishedSnapshotTestObjects.TestElementModel2), typeof(PublishedSnapshotTestObjects.TestContentModel1), typeof(PublishedSnapshotTestObjects.TestContentModel2), }); register.Register(f => factory); Current.Factory = composition.CreateFactory(); var cacheMock = new Mock <IPublishedContentCache>(); var cacheContent = new Dictionary <int, IPublishedContent>(); cacheMock.Setup(x => x.GetById(It.IsAny <int>())).Returns <int>(id => cacheContent.TryGetValue(id, out IPublishedContent content) ? content : null); var publishedSnapshotMock = new Mock <IPublishedSnapshot>(); publishedSnapshotMock.Setup(x => x.Content).Returns(cacheMock.Object); var publishedSnapshotAccessorMock = new Mock <IPublishedSnapshotAccessor>(); publishedSnapshotAccessorMock.Setup(x => x.PublishedSnapshot).Returns(publishedSnapshotMock.Object); register.Register(f => publishedSnapshotAccessorMock.Object); var converters = Current.Factory.GetInstance <PropertyValueConverterCollection>(); var dataTypeService = new TestObjects.TestDataTypeService( new DataType(new VoidEditor(Mock.Of <ILogger>())) { Id = 1 }, new DataType(new VoidEditor("2", Mock.Of <ILogger>())) { Id = 2 }); var contentTypeFactory = new PublishedContentTypeFactory(factory, converters, dataTypeService); IEnumerable <IPublishedPropertyType> CreatePropertyTypes(IPublishedContentType contentType, int i) { yield return(contentTypeFactory.CreatePropertyType(contentType, "prop" + i, i)); } var elementType1 = contentTypeFactory.CreateContentType(1000, "element1", t => CreatePropertyTypes(t, 1)); var elementType2 = contentTypeFactory.CreateContentType(1001, "element2", t => CreatePropertyTypes(t, 2)); var contentType1 = contentTypeFactory.CreateContentType(1002, "content1", t => CreatePropertyTypes(t, 1)); var contentType2 = contentTypeFactory.CreateContentType(1003, "content2", t => CreatePropertyTypes(t, 2)); var element1 = new PublishedElement(elementType1, Guid.NewGuid(), new Dictionary <string, object> { { "prop1", "val1" } }, false); var element2 = new PublishedElement(elementType2, Guid.NewGuid(), new Dictionary <string, object> { { "prop2", "1003" } }, false); var cnt1 = new SolidPublishedContent(contentType1) { Id = 1003, Properties = new[] { new SolidPublishedProperty { Alias = "prop1", SolidHasValue = true, SolidValue = "val1" } } }; var cnt2 = new SolidPublishedContent(contentType1) { Id = 1004, Properties = new[] { new SolidPublishedProperty { Alias = "prop2", SolidHasValue = true, SolidValue = "1003" } } }; cacheContent[cnt1.Id] = cnt1.CreateModel(); cacheContent[cnt2.Id] = cnt2.CreateModel(); // can get the actual property Clr type // ie ModelType gets properly mapped by IPublishedContentModelFactory // must test ModelClrType with special equals 'cos they are not ref-equals Assert.IsTrue(ModelType.Equals(typeof(IEnumerable <>).MakeGenericType(ModelType.For("content1")), contentType2.GetPropertyType("prop2").ModelClrType)); Assert.AreEqual(typeof(IEnumerable <PublishedSnapshotTestObjects.TestContentModel1>), contentType2.GetPropertyType("prop2").ClrType); // can create a model for an element var model1 = factory.CreateModel(element1); Assert.IsInstanceOf <PublishedSnapshotTestObjects.TestElementModel1>(model1); Assert.AreEqual("val1", ((PublishedSnapshotTestObjects.TestElementModel1)model1).Prop1); // can create a model for a published content var model2 = factory.CreateModel(element2); Assert.IsInstanceOf <PublishedSnapshotTestObjects.TestElementModel2>(model2); var mmodel2 = (PublishedSnapshotTestObjects.TestElementModel2)model2; // and get direct property Assert.IsInstanceOf <PublishedSnapshotTestObjects.TestContentModel1[]>(model2.Value("prop2")); Assert.AreEqual(1, ((PublishedSnapshotTestObjects.TestContentModel1[])model2.Value("prop2")).Length); // and get model property Assert.IsInstanceOf <IEnumerable <PublishedSnapshotTestObjects.TestContentModel1> >(mmodel2.Prop2); Assert.IsInstanceOf <PublishedSnapshotTestObjects.TestContentModel1[]>(mmodel2.Prop2); var mmodel1 = mmodel2.Prop2.First(); // and we get what we want Assert.AreSame(cacheContent[mmodel1.Id], mmodel1); }