U4-6479 - fix GetCulture for unpublished content + add tests

This commit is contained in:
Stephan
2015-04-01 15:40:34 +02:00
parent 40d3f2ffb9
commit 22ac571c7d
6 changed files with 329 additions and 57 deletions

View File

@@ -20,7 +20,9 @@ namespace Umbraco.Web.Models
public static CultureInfo GetCulture(this IContent content, Uri current = null)
{
return GetCulture(UmbracoContext.Current,
ApplicationContext.Current.Services.DomainService, ApplicationContext.Current.Services.LocalizationService,
ApplicationContext.Current.Services.DomainService,
ApplicationContext.Current.Services.LocalizationService,
ApplicationContext.Current.Services.ContentService,
content.Id, content.Path,
current);
}
@@ -32,32 +34,64 @@ namespace Umbraco.Web.Models
/// <param name="umbracoContext">An <see cref="UmbracoContext"/> instance.</param>
/// <param name="domainService">An <see cref="IDomainService"/> implementation.</param>
/// <param name="localizationService">An <see cref="ILocalizationService"/> implementation.</param>
/// <param name="contentService">An <see cref="IContentService"/> implementation.</param>
/// <param name="contentId">The content identifier.</param>
/// <param name="contentPath">The content path.</param>
/// <param name="current">The request Uri.</param>
/// <returns>The culture that would be selected to render the content.</returns>
internal static CultureInfo GetCulture(UmbracoContext umbracoContext, IDomainService domainService, ILocalizationService localizationService,
internal static CultureInfo GetCulture(UmbracoContext umbracoContext,
IDomainService domainService, ILocalizationService localizationService, IContentService contentService,
int contentId, string contentPath, Uri current)
{
var route = umbracoContext.ContentCache.GetRouteById(contentId); // cached
var pos = route.IndexOf('/');
var route = umbracoContext == null
? null // for tests only
: umbracoContext.ContentCache.GetRouteById(contentId); // cached
var domainHelper = new DomainHelper(domainService);
IDomain domain;
var domain = pos == 0
? null
: domainHelper.DomainForNode(int.Parse(route.Substring(0, pos)), current).UmbracoDomain;
if (route == null)
{
// if content is not published then route is null and we have to work
// on non-published content (note: could optimize by checking routes?)
var content = contentService.GetById(contentId);
if (content == null)
return GetDefaultCulture(localizationService);
var hasDomain = domainHelper.NodeHasDomains(content.Id);
while (hasDomain == false && content != null)
{
content = content.Parent();
hasDomain = content != null && domainHelper.NodeHasDomains(content.Id);
}
domain = hasDomain ? domainHelper.DomainForNode(content.Id, current).UmbracoDomain : null;
}
else
{
// if content is published then we have a (cached) route
// from which we can figure out the domain
var pos = route.IndexOf('/');
domain = pos == 0
? null
: domainHelper.DomainForNode(int.Parse(route.Substring(0, pos)), current).UmbracoDomain;
}
if (domain == null)
{
var defaultLanguage = localizationService.GetAllLanguages().FirstOrDefault();
return defaultLanguage == null ? CultureInfo.CurrentUICulture : new CultureInfo(defaultLanguage.IsoCode);
}
return GetDefaultCulture(localizationService);
var wcDomain = DomainHelper.FindWildcardDomainInPath(domainService.GetAll(true), contentPath, domain.RootContent.Id);
return wcDomain == null
? new CultureInfo(domain.Language.IsoCode)
: new CultureInfo(wcDomain.Language.IsoCode);
}
private static CultureInfo GetDefaultCulture(ILocalizationService localizationService)
{
var defaultLanguage = localizationService.GetAllLanguages().FirstOrDefault();
return defaultLanguage == null ? CultureInfo.CurrentUICulture : new CultureInfo(defaultLanguage.IsoCode);
}
}
}