示例#1
0
        /// <summary>
        /// Gets the URL segment for a specified content and culture.
        /// </summary>
        /// <param name="content">The content.</param>
        /// <param name="shortStringHelper"></param>
        /// <param name="urlSegmentProviders"></param>
        /// <param name="culture">The culture.</param>
        /// <returns>The URL segment.</returns>
        public static string GetUrlSegment(this IContentBase content, IShortStringHelper shortStringHelper, IEnumerable <IUrlSegmentProvider> urlSegmentProviders, string culture = null)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }
            if (urlSegmentProviders == null)
            {
                throw new ArgumentNullException(nameof(urlSegmentProviders));
            }

            var url = urlSegmentProviders.Select(p => p.GetUrlSegment(content, culture)).FirstOrDefault(u => u != null);

            if (url == null)
            {
                if (s_defaultUrlSegmentProvider == null)
                {
                    s_defaultUrlSegmentProvider = new DefaultUrlSegmentProvider(shortStringHelper);
                }

                url = s_defaultUrlSegmentProvider.GetUrlSegment(content, culture); // be safe
            }

            return(url);
        }
示例#2
0
        public void UnpublishedNameChanges()
        {
            var urlSegmentProvider = new DefaultUrlSegmentProvider();

            var contentType = MockedContentTypes.CreateTextPageContentType();

            ServiceContext.FileService.SaveTemplate(contentType.DefaultTemplate);
            ServiceContext.ContentTypeService.Save(contentType);

            var content = MockedContent.CreateTextpageContent(contentType, "hello", Constants.System.Root);

            ServiceContext.ContentService.SaveAndPublish(content);
            var cachedContent = ServiceContext.ContentService.GetById(content.Id);
            var segment       = urlSegmentProvider.GetUrlSegment(cachedContent);

            // Does a new node work?

            Assert.AreEqual("hello", segment);

            content.Name  = "goodbye";
            cachedContent = ServiceContext.ContentService.GetById(content.Id);
            segment       = urlSegmentProvider.GetUrlSegment(cachedContent);

            // We didn't save anything, so all should still be the same

            Assert.AreEqual("hello", segment);

            ServiceContext.ContentService.Save(content);
            cachedContent = ServiceContext.ContentService.GetById(content.Id);
            segment       = urlSegmentProvider.GetUrlSegment(cachedContent);

            // At this point we have saved the new name, but not published. The url should still be the previous name

            Assert.AreEqual("hello", segment);

            PublishedSnapshotService.Rebuild();

            cachedContent = ServiceContext.ContentService.GetById(content.Id);
            segment       = urlSegmentProvider.GetUrlSegment(cachedContent);

            // After a rebuild, the unpublished name should still not be the url.
            // This was previously incorrect, per #11074

            Assert.AreEqual("hello", segment);

            ServiceContext.ContentService.SaveAndPublish(content);
            cachedContent = ServiceContext.ContentService.GetById(content.Id);
            segment       = urlSegmentProvider.GetUrlSegment(cachedContent);

            // The page has now been published, so we should see the new url segment
            Assert.AreEqual("goodbye", segment);

            PublishedSnapshotService.Rebuild();
            cachedContent = ServiceContext.ContentService.GetById(content.Id);
            segment       = urlSegmentProvider.GetUrlSegment(cachedContent);

            // Just double checking that things remain after a rebuild
            Assert.AreEqual("goodbye", segment);
        }