Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Umbraco.Core/Routing/PublishedUrlInfoProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@
public async Task<ISet<UrlInfo>> GetAllAsync(IContent content)
{
HashSet<UrlInfo> urlInfos = [];
IEnumerable<string> cultures = await _languageService.GetAllIsoCodesAsync();

// For invariant content, only return the URL for the default language.
// Invariant content doesn't vary by culture, so it only has one URL.
IEnumerable<string> cultures = content.ContentType.VariesByCulture()
? await _languageService.GetAllIsoCodesAsync()
: [(await _languageService.GetDefaultIsoCodeAsync())];

Check warning on line 53 in src/Umbraco.Core/Routing/PublishedUrlInfoProvider.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (main)

❌ New issue: Complex Method

GetAllAsync has a cyclomatic complexity of 9, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

// First we get the urls of all cultures, using the published router, meaning we respect any extensions.
foreach (var culture in cultures)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Tests.Common.Builders;
using Umbraco.Cms.Tests.Common.Builders.Extensions;

namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;

internal sealed class PublishedUrlInfoProviderTests : PublishedUrlInfoProviderTestsBase
{
private ILanguageService LanguageService => GetRequiredService<ILanguageService>();

[Test]
public async Task Invariant_content_returns_only_default_language_url()
{
// Arrange: Add a second language (Danish) alongside the default English
var danishLanguage = new LanguageBuilder()
.WithCultureInfo("da-DK")
.WithCultureName("Danish")
.Build();
await LanguageService.CreateAsync(danishLanguage, Constants.Security.SuperUserKey);

// The base class creates invariant content (ContentType doesn't vary by culture)
// Publish the root content
ContentService.PublishBranch(Textpage, PublishBranchFilter.IncludeUnpublished, ["*"]);

// Act: Get all URLs for the invariant content
var urls = await PublishedUrlInfoProvider.GetAllAsync(Textpage);

// Assert: Invariant content should only return ONE URL (for the default language)
// not multiple URLs for each configured language
Assert.AreEqual(1, urls.Count, "Invariant content should only return one URL for the default language, not URLs for all configured languages");
Assert.IsNotNull(urls.First().Url);
Assert.AreEqual("en-US", urls.First().Culture, "The URL should be for the default language (en-US)");
}

[Test]
public async Task Two_items_in_level_1_with_same_name_will_have_conflicting_routes()
Expand Down
Loading