Routing: Added method to IDocumentUrlService for retrieving document key from URI (closes #20666) (#20673)

Added method to IDocumentUrlService for retrieving document key from URI.
This commit is contained in:
Andy Butland
2025-10-29 09:47:17 +01:00
committed by GitHub
parent 8733230762
commit e7ccfaaaac
5 changed files with 151 additions and 6 deletions

View File

@@ -1,11 +1,13 @@
using NUnit.Framework;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Core.Sync;
using Umbraco.Cms.Tests.Common.Builders;
using Umbraco.Cms.Tests.Common.Builders.Extensions;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
using Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Scoping;
@@ -23,6 +25,8 @@ internal sealed class DocumentUrlServiceTests : UmbracoIntegrationTestWithConten
protected ILanguageService LanguageService => GetRequiredService<ILanguageService>();
protected IDomainService DomainService => GetRequiredService<IDomainService>();
protected override void CustomTestSetup(IUmbracoBuilder builder)
{
builder.Services.AddUnique<IServerMessenger, ScopedRepositoryTests.LocalServerMessenger>();
@@ -148,6 +152,64 @@ internal sealed class DocumentUrlServiceTests : UmbracoIntegrationTestWithConten
Assert.IsNull(actual);
}
[TestCase("/", ExpectedResult = TextpageKey)]
[TestCase("/text-page-1", ExpectedResult = SubPageKey)]
public string? GetDocumentKeyByUri_Without_Domains_Returns_Expected_DocumentKey(string path)
{
ContentService.PublishBranch(Textpage, PublishBranchFilter.IncludeUnpublished, ["*"]);
var uri = new Uri("http://example.com" + path);
return DocumentUrlService.GetDocumentKeyByUri(uri, false)?.ToString()?.ToUpper();
}
private const string VariantRootPageKey = "1D3283C7-64FD-4F4D-A741-442BDA487B71";
private const string VariantChildPageKey = "1D3283C7-64FD-4F4D-A741-442BDA487B72";
[TestCase("/", "/en", "http://example.com/en/", ExpectedResult = VariantRootPageKey)]
[TestCase("/child-page", "/en", "http://example.com/en/", ExpectedResult = VariantChildPageKey)]
[TestCase("/", "example.com", "http://example.com/", ExpectedResult = VariantRootPageKey)]
[TestCase("/child-page", "example.com", "http://example.com/", ExpectedResult = VariantChildPageKey)]
public async Task<string?> GetDocumentKeyByUri_With_Domains_Returns_Expected_DocumentKey(string path, string domain, string rootUrl)
{
var template = TemplateBuilder.CreateTextPageTemplate("variantPageTemplate", "Variant Page Template");
FileService.SaveTemplate(template);
var contentType = new ContentTypeBuilder()
.WithAlias("variantPage")
.WithName("Variant Page")
.WithContentVariation(ContentVariation.Culture)
.WithAllowAsRoot(true)
.WithDefaultTemplateId(template.Id)
.Build();
ContentTypeService.Save(contentType);
var rootPage = new ContentBuilder()
.WithKey(Guid.Parse(VariantRootPageKey))
.WithContentType(contentType)
.WithCultureName("en-US", $"Root Page")
.Build();
var childPage = new ContentBuilder()
.WithKey(Guid.Parse(VariantChildPageKey))
.WithContentType(contentType)
.WithCultureName("en-US", $"Child Page")
.WithParent(rootPage)
.Build();
ContentService.Save(rootPage, -1);
ContentService.Save(childPage, -1);
ContentService.PublishBranch(rootPage, PublishBranchFilter.IncludeUnpublished, ["*"]);
var updateDomainResult = await DomainService.UpdateDomainsAsync(
rootPage.Key,
new DomainsUpdateModel
{
Domains = [new DomainModel { DomainName = domain, IsoCode = "en-US" }],
});
Assert.IsTrue(updateDomainResult.Success);
var uri = new Uri(rootUrl + path);
return DocumentUrlService.GetDocumentKeyByUri(uri, false)?.ToString()?.ToUpper();
}
[TestCase("/", "en-US", true, ExpectedResult = TextpageKey)]
[TestCase("/text-page-1", "en-US", true, ExpectedResult = SubPageKey)]
[TestCase("/text-page-1-custom", "en-US", true, ExpectedResult = SubPageKey)] // Uses the segment registered by the custom IIUrlSegmentProvider that allows for more than one segment per document.
@@ -160,7 +222,7 @@ internal sealed class DocumentUrlServiceTests : UmbracoIntegrationTestWithConten
[TestCase("/text-page-2", "en-US", false, ExpectedResult = null)]
[TestCase("/text-page-2-custom", "en-US", false, ExpectedResult = SubPage2Key)] // Uses the segment registered by the custom IIUrlSegmentProvider that does not allow for more than one segment per document.
[TestCase("/text-page-3", "en-US", false, ExpectedResult = SubPage3Key)]
public string? GetDocumentKeyByRoute_Returns_Expected_Route(string route, string isoCode, bool loadDraft)
public string? GetDocumentKeyByRoute_Returns_Expected_DocumentKey(string route, string isoCode, bool loadDraft)
{
if (loadDraft is false)
{