public void Get_Url_Alternate()
        {
            var settings = SettingsForTests.GenerateMockUmbracoSettings();

            var globalSettings = Mock.Get(Factory.GetInstance <IGlobalSettings>()); //this will modify the IGlobalSettings instance stored in the container

            globalSettings.Setup(x => x.HideTopLevelNodeFromPath).Returns(false);   // ignored w/domains

            var umbracoContext = GetUmbracoContext("http://domain1.com/en/test", 1111, umbracoSettings: settings, urlProviders: new[]
            {
                new DefaultUrlProvider(settings.RequestHandler, Logger, globalSettings.Object, new SiteDomainHelper())
            }, globalSettings: globalSettings.Object);

            SetDomains5();

            var url = umbracoContext.UrlProvider.GetUrl(100111, UrlMode.Absolute);

            Assert.AreEqual("http://domain1.com/en/1001-1-1/", url);

            var result = umbracoContext.UrlProvider.GetOtherUrls(100111).ToArray();

            foreach (var x in result)
            {
                Console.WriteLine(x);
            }

            Assert.AreEqual(2, result.Length);
            Assert.AreEqual(result[0].Text, "http://domain1b.com/en/1001-1-1/");
            Assert.AreEqual(result[1].Text, "http://domain1a.com/en/1001-1-1/");
        }
Exemple #2
0
        public void Get_Url_Relative_Or_Absolute()
        {
            var settings    = SettingsForTests.GenerateMockUmbracoSettings();
            var requestMock = Mock.Get(settings.RequestHandler);

            requestMock.Setup(x => x.UseDomainPrefixes).Returns(false);

            var globalSettings = Mock.Get(Factory.GetInstance <IGlobalSettings>()); //this will modify the IGlobalSettings instance stored in the container

            globalSettings.Setup(x => x.UseDirectoryUrls).Returns(true);
            globalSettings.Setup(x => x.HideTopLevelNodeFromPath).Returns(false); // ignored w/domains

            var umbracoContext = GetUmbracoContext("http://domain1.com/test", 1111, umbracoSettings: settings, urlProviders: new[]
            {
                new DefaultUrlProvider(settings.RequestHandler, Logger, globalSettings.Object, new SiteDomainHelper())
            }, globalSettings: globalSettings.Object);

            SetDomains4();

            Assert.AreEqual("/en/1001-1-1/", umbracoContext.UrlProvider.GetUrl(100111));
            Assert.AreEqual("http://domain3.com/en/1003-1-1/", umbracoContext.UrlProvider.GetUrl(100311));

            requestMock.Setup(x => x.UseDomainPrefixes).Returns(true);

            Assert.AreEqual("http://domain1.com/en/1001-1-1/", umbracoContext.UrlProvider.GetUrl(100111));
            Assert.AreEqual("http://domain3.com/en/1003-1-1/", umbracoContext.UrlProvider.GetUrl(100311));

            requestMock.Setup(x => x.UseDomainPrefixes).Returns(false);
            umbracoContext.UrlProvider.Mode = UrlProviderMode.Absolute;

            Assert.AreEqual("http://domain1.com/en/1001-1-1/", umbracoContext.UrlProvider.GetUrl(100111));
            Assert.AreEqual("http://domain3.com/en/1003-1-1/", umbracoContext.UrlProvider.GetUrl(100311));
        }
        public void U4_4056()
        {
            var settings    = SettingsForTests.GenerateMockUmbracoSettings();
            var contentMock = Mock.Get(settings.RequestHandler);

            contentMock.Setup(x => x.CharCollection).Returns(Enumerable.Empty <IChar>());
            contentMock.Setup(x => x.ConvertUrlsToAscii).Returns(false);

            const string input = "ÆØÅ and æøå and 中文测试 and  אודות האתר and größer БбДдЖж page";

            var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(settings)); // unicode
            var output = helper.CleanStringForUrlSegment(input);

            Assert.AreEqual("æøå-and-æøå-and-中文测试-and-אודות-האתר-and-größer-ббдджж-page", output);

            helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(settings)
                                                  .WithConfig(CleanStringType.UrlSegment, new DefaultShortStringHelperConfig.Config
            {
                IsTerm     = (c, leading) => char.IsLetterOrDigit(c) || c == '_',
                StringType = CleanStringType.LowerCase | CleanStringType.Ascii,     // ascii
                Separator  = '-'
            }));
            output = helper.CleanStringForUrlSegment(input);
            Assert.AreEqual("aeoa-and-aeoa-and-and-and-grosser-bbddzhzh-page", output);
        }
        public void CleanStringDefaultConfig()
        {
            var settings    = SettingsForTests.GenerateMockUmbracoSettings();
            var contentMock = Mock.Get(settings.RequestHandler);

            contentMock.Setup(x => x.CharCollection).Returns(Enumerable.Empty <IChar>());
            contentMock.Setup(x => x.ConvertUrlsToAscii).Returns(false);

            var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(settings));

            const string input = "0123 中文测试 中文测试 léger ZÔRG (2) a?? *x";

            var alias    = helper.CleanStringForSafeAlias(input);
            var filename = helper.CleanStringForSafeFileName(input);
            var segment  = helper.CleanStringForUrlSegment(input);

            // umbraco-cased ascii alias, must begin with a proper letter
            Assert.AreEqual("legerZORG2AX", alias, "alias");

            // lower-cased, utf8 filename, removing illegal filename chars, using dash-separator
            Assert.AreEqual("0123-中文测试-中文测试-léger-zôrg-2-a-x", filename, "filename");

            // lower-cased, utf8 url segment, only letters and digits, using dash-separator
            Assert.AreEqual("0123-中文测试-中文测试-léger-zôrg-2-a-x", segment, "segment");
        }
        public void DoNotPolluteCache()
        {
            var globalSettings = Mock.Get(Factory.GetInstance <IGlobalSettings>()); //this will modify the IGlobalSettings instance stored in the container

            globalSettings.Setup(x => x.UseDirectoryUrls).Returns(true);
            globalSettings.Setup(x => x.HideTopLevelNodeFromPath).Returns(false);

            var settings = SettingsForTests.GenerateMockUmbracoSettings();
            var request  = Mock.Get(settings.RequestHandler);

            request.Setup(x => x.UseDomainPrefixes).Returns(true);

            SetDomains1();

            const string url = "http://domain1.com/1001-1/1001-1-1";

            // get the nice url for 100111
            var umbracoContext = GetUmbracoContext(url, 9999, umbracoSettings: settings, urlProviders: new []
            {
                new DefaultUrlProvider(settings.RequestHandler, Logger, globalSettings.Object, new SiteDomainHelper())
            }, globalSettings: globalSettings.Object);

            Assert.AreEqual("http://domain2.com/1001-1-1/", umbracoContext.UrlProvider.GetUrl(100111, true));

            // check that the proper route has been cached
            var cache = umbracoContext.ContentCache as PublishedContentCache;

            if (cache == null)
            {
                throw new Exception("Unsupported IPublishedContentCache, only the Xml one is supported.");
            }
            var cachedRoutes = cache.RoutesCache.GetCachedRoutes();

            Assert.AreEqual("10011/1001-1-1", cachedRoutes[100111]);

            // route a rogue url
            var publishedRouter = CreatePublishedRouter();
            var frequest        = publishedRouter.CreateRequest(umbracoContext);

            publishedRouter.FindDomain(frequest);
            Assert.IsTrue(frequest.HasDomain);

            // check that it's been routed
            var lookup = new ContentFinderByUrl(Logger);
            var result = lookup.TryFindContent(frequest);

            Assert.IsTrue(result);
            Assert.AreEqual(100111, frequest.PublishedContent.Id);

            // has the cache been polluted?
            cachedRoutes = cache.RoutesCache.GetCachedRoutes();
            Assert.AreEqual("10011/1001-1-1", cachedRoutes[100111]); // no
            //Assert.AreEqual("1001/1001-1/1001-1-1", cachedRoutes[100111]); // yes

            // what's the nice url now?
            Assert.AreEqual("http://domain2.com/1001-1-1/", umbracoContext.UrlProvider.GetUrl(100111)); // good
            //Assert.AreEqual("http://domain1.com/1001-1/1001-1-1", routingContext.NiceUrlProvider.GetNiceUrl(100111, true)); // bad
        }
        public void Uri_From_Umbraco(string sourceUrl, string expectedUrl, bool trailingSlash)
        {
            var globalConfig = Mock.Get(SettingsForTests.GenerateMockGlobalSettings());

            var settings    = SettingsForTests.GenerateMockUmbracoSettings();
            var requestMock = Mock.Get(settings.RequestHandler);

            requestMock.Setup(x => x.AddTrailingSlash).Returns(trailingSlash);

            UriUtility.SetAppDomainAppVirtualPath("/");

            var expectedUri = NewUri(expectedUrl);
            var sourceUri   = NewUri(sourceUrl);
            var resultUri   = UriUtility.UriFromUmbraco(sourceUri, globalConfig.Object, settings.RequestHandler);

            Assert.AreEqual(expectedUri.ToString(), resultUri.ToString());
        }
        public void Get_Url_NestedDomains(int nodeId, string currentUrl, bool absolute, string expected)
        {
            var settings = SettingsForTests.GenerateMockUmbracoSettings();

            var globalSettings = Mock.Get(Factory.GetInstance <IGlobalSettings>()); //this will modify the IGlobalSettings instance stored in the container

            globalSettings.Setup(x => x.HideTopLevelNodeFromPath).Returns(false);   // ignored w/domains

            var umbracoContext = GetUmbracoContext("/test", 1111, umbracoSettings: settings, urlProviders: new[]
            {
                new DefaultUrlProvider(settings.RequestHandler, Logger, globalSettings.Object, new SiteDomainHelper())
            }, globalSettings: globalSettings.Object);

            SetDomains4();

            var currentUri = new Uri(currentUrl);
            var result     = umbracoContext.UrlProvider.GetUrl(nodeId, absolute, current: currentUri);

            Assert.AreEqual(expected, result);
        }
Exemple #8
0
        public void Uri_From_Umbraco(string sourceUrl, string expectedUrl, bool directoryUrls, bool trailingSlash)
        {
            ConfigurationManager.AppSettings.Set("umbracoUseDirectoryUrls", directoryUrls ? "true" : "false");

            var globalConfig = Mock.Get(SettingsForTests.GenerateMockGlobalSettings());

            globalConfig.Setup(x => x.UseDirectoryUrls).Returns(directoryUrls);

            var settings    = SettingsForTests.GenerateMockUmbracoSettings();
            var requestMock = Mock.Get(settings.RequestHandler);

            requestMock.Setup(x => x.AddTrailingSlash).Returns(trailingSlash);

            UriUtility.SetAppDomainAppVirtualPath("/");

            var expectedUri = NewUri(expectedUrl);
            var sourceUri   = NewUri(sourceUrl);
            var resultUri   = UriUtility.UriFromUmbraco(sourceUri, globalConfig.Object, settings.RequestHandler);

            Assert.AreEqual(expectedUri.ToString(), resultUri.ToString());
        }
 protected override void Compose()
 {
     base.Compose();
     Composition.RegisterUnique <IShortStringHelper>(_ => new DefaultShortStringHelper(SettingsForTests.GenerateMockUmbracoSettings()));
 }
        public void Get_Url_DomainsAndCache()
        {
            var settings = SettingsForTests.GenerateMockUmbracoSettings();

            var globalSettings = Mock.Get(Factory.GetInstance <IGlobalSettings>()); //this will modify the IGlobalSettings instance stored in the container

            globalSettings.Setup(x => x.HideTopLevelNodeFromPath).Returns(false);   // ignored w/domains

            var umbracoContext = GetUmbracoContext("/test", 1111, umbracoSettings: settings, urlProviders: new[]
            {
                new DefaultUrlProvider(settings.RequestHandler, Logger, globalSettings.Object, new SiteDomainHelper())
            }, globalSettings: globalSettings.Object);

            SetDomains4();

            string ignore;

            ignore = umbracoContext.UrlProvider.GetUrl(1001, UrlMode.Auto, current: new Uri("http://domain1.com"));
            ignore = umbracoContext.UrlProvider.GetUrl(10011, UrlMode.Auto, current: new Uri("http://domain1.com"));
            ignore = umbracoContext.UrlProvider.GetUrl(100111, UrlMode.Auto, current: new Uri("http://domain1.com"));
            ignore = umbracoContext.UrlProvider.GetUrl(10012, UrlMode.Auto, current: new Uri("http://domain1.com"));
            ignore = umbracoContext.UrlProvider.GetUrl(100121, UrlMode.Auto, current: new Uri("http://domain1.com"));
            ignore = umbracoContext.UrlProvider.GetUrl(10013, UrlMode.Auto, current: new Uri("http://domain1.com"));
            ignore = umbracoContext.UrlProvider.GetUrl(1002, UrlMode.Auto, current: new Uri("http://domain1.com"));
            ignore = umbracoContext.UrlProvider.GetUrl(1001, UrlMode.Auto, current: new Uri("http://domain2.com"));
            ignore = umbracoContext.UrlProvider.GetUrl(10011, UrlMode.Auto, current: new Uri("http://domain2.com"));
            ignore = umbracoContext.UrlProvider.GetUrl(100111, UrlMode.Auto, current: new Uri("http://domain2.com"));
            ignore = umbracoContext.UrlProvider.GetUrl(1002, UrlMode.Auto, current: new Uri("http://domain2.com"));

            var cache = umbracoContext.Content as PublishedContentCache;

            if (cache == null)
            {
                throw new Exception("Unsupported IPublishedContentCache, only the Xml one is supported.");
            }
            var cachedRoutes = cache.RoutesCache.GetCachedRoutes();

            Assert.AreEqual(7, cachedRoutes.Count);

            var cachedIds = cache.RoutesCache.GetCachedIds();

            Assert.AreEqual(0, cachedIds.Count);

            CheckRoute(cachedRoutes, cachedIds, 1001, "1001/");
            CheckRoute(cachedRoutes, cachedIds, 10011, "10011/");
            CheckRoute(cachedRoutes, cachedIds, 100111, "10011/1001-1-1");
            CheckRoute(cachedRoutes, cachedIds, 10012, "10012/");
            CheckRoute(cachedRoutes, cachedIds, 100121, "10012/1001-2-1");
            CheckRoute(cachedRoutes, cachedIds, 10013, "1001/1001-3");
            CheckRoute(cachedRoutes, cachedIds, 1002, "/1002");

            // use the cache
            Assert.AreEqual("/", umbracoContext.UrlProvider.GetUrl(1001, UrlMode.Auto, current: new Uri("http://domain1.com")));
            Assert.AreEqual("/en/", umbracoContext.UrlProvider.GetUrl(10011, UrlMode.Auto, current: new Uri("http://domain1.com")));
            Assert.AreEqual("/en/1001-1-1/", umbracoContext.UrlProvider.GetUrl(100111, UrlMode.Auto, current: new Uri("http://domain1.com")));
            Assert.AreEqual("/fr/", umbracoContext.UrlProvider.GetUrl(10012, UrlMode.Auto, current: new Uri("http://domain1.com")));
            Assert.AreEqual("/fr/1001-2-1/", umbracoContext.UrlProvider.GetUrl(100121, UrlMode.Auto, current: new Uri("http://domain1.com")));
            Assert.AreEqual("/1001-3/", umbracoContext.UrlProvider.GetUrl(10013, UrlMode.Auto, current: new Uri("http://domain1.com")));
            Assert.AreEqual("/1002/", umbracoContext.UrlProvider.GetUrl(1002, UrlMode.Auto, current: new Uri("http://domain1.com")));

            Assert.AreEqual("http://domain1.com/fr/1001-2-1/", umbracoContext.UrlProvider.GetUrl(100121, UrlMode.Auto, current: new Uri("http://domain2.com")));
        }
Exemple #11
0
        public void TestScopeMany(bool complete)
        {
            var umbracoContext = GetUmbracoContext("http://example.com/", setSingleton: true);

            // sanity checks
            Assert.AreSame(umbracoContext, Umbraco.Web.Composing.Current.UmbracoContext);
            Assert.AreSame(XmlStore, ((PublishedContentCache)umbracoContext.ContentCache).XmlStore);

            // settings
            var settings    = SettingsForTests.GenerateMockUmbracoSettings();
            var contentMock = Mock.Get(Factory.GetInstance <IUmbracoSettingsSection>().Content);

            contentMock.Setup(x => x.XmlCacheEnabled).Returns(false);

            // create document type
            var contentType = new ContentType(-1)
            {
                Alias = "CustomDocument", Name = "Custom Document"
            };

            Current.Services.ContentTypeService.Save(contentType);

            // wire cache refresher
            _distributedCacheBinder = new DistributedCacheBinder(new DistributedCache(), Mock.Of <ILogger>());
            _distributedCacheBinder.BindEvents(true);

            // check xml in context = "before"
            var       xml            = XmlInContext;
            var       beforeXml      = xml;
            var       beforeOuterXml = beforeXml.OuterXml;
            var       item           = new Content("name", -1, contentType);
            const int count          = 10;
            var       ids            = new int[count];
            var       clones         = 0;

            SafeXmlReaderWriter.Cloning = () => { clones++; };

            Console.WriteLine("Xml Before:");
            Console.WriteLine(xml.OuterXml);

            using (var scope = ScopeProvider.CreateScope())
            {
                Current.Services.ContentService.SaveAndPublish(item);

                for (var i = 0; i < count; i++)
                {
                    var temp = new Content("content_" + i, -1, contentType);
                    Current.Services.ContentService.SaveAndPublish(temp);
                    ids[i] = temp.Id;
                }

                // this should never change
                Assert.AreEqual(beforeOuterXml, beforeXml.OuterXml);

                xml = XmlMaster;
                Assert.IsNotNull(xml);
                Console.WriteLine("Xml InScope (before complete):");
                Console.WriteLine(xml.OuterXml);
                Assert.AreEqual(beforeOuterXml, xml.OuterXml);

                if (complete)
                {
                    scope.Complete();
                }

                xml = XmlMaster;
                Assert.IsNotNull(xml);
                Console.WriteLine("Xml InScope (after complete):");
                Console.WriteLine(xml.OuterXml);
                Assert.AreEqual(beforeOuterXml, xml.OuterXml);
            }

            var scopeProvider = ScopeProvider;

            Assert.IsNotNull(scopeProvider);
            // ambient scope may be null, or maybe not, depending on whether the code that
            // was called did proper scoped work, or some direct (NoScope) use of the database
            Assert.IsNull(scopeProvider.AmbientContext);

            // limited number of clones!
            Assert.AreEqual(complete ? 1 : 0, clones);

            // this should never change
            Assert.AreEqual(beforeOuterXml, beforeXml.OuterXml);

            xml = XmlMaster;
            Assert.IsNotNull(xml);
            Console.WriteLine("Xml After:");
            Console.WriteLine(xml.OuterXml);
            if (complete)
            {
                var node = xml.GetElementById(item.Id.ToString());
                Assert.IsNotNull(node);

                for (var i = 0; i < 10; i++)
                {
                    node = xml.GetElementById(ids[i].ToString());
                    Assert.IsNotNull(node);
                }
            }
            else
            {
                Assert.AreEqual(beforeOuterXml, xml.OuterXml); // nothing has changed!
            }
        }