Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Examine/DeliveryApiContentIndexHelperTests.cs
yv01p 0e395cc56f refactor(core): clean up remaining internal methods in ContentService
Remove or simplify internal helper methods that are no longer needed
after service extraction.

Removed methods:
- GetAllPublished(): Was only used in tests, replaced with public query methods
- QueryNotTrashed field and property: No longer needed after GetAllPublished removal
- HasUnsavedChanges(): Duplicated in ContentPublishOperationService where it's used
- TryGetParentKey(): Duplicated in ContentMoveOperationService where it's used

Kept methods:
- Audit() and AuditAsync(): Still used by MoveToRecycleBin and DeleteOfTypes

Test refactoring:
- DeliveryApiContentIndexHelperTests.GetExpectedNumberOfContentItems() now uses
  public GetPagedDescendants method instead of internal GetAllPublished

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-24 20:20:24 +00:00

124 lines
4.6 KiB
C#

using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Infrastructure.Examine;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Tests.Common.Builders;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Examine;
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
[TestFixture]
public class DeliveryApiContentIndexHelperTests : UmbracoIntegrationTestWithContent
{
public override void CreateTestData()
{
base.CreateTestData();
// Save an extra, published content item of a different type to those created via the base class,
// that we'll use to test filtering out disallowed content types.
var template = TemplateBuilder.CreateTextPageTemplate("textPage2");
FileService.SaveTemplate(template);
var contentType = ContentTypeBuilder.CreateSimpleContentType("umbTextpage2", "Textpage2", defaultTemplateId: template.Id);
contentType.Key = Guid.NewGuid();
ContentTypeService.Save(contentType);
ContentType.AllowedContentTypes =
[
new ContentTypeSort(ContentType.Key, 0, "umbTextpage"),
new ContentTypeSort(contentType.Key, 1, "umbTextpage2"),
];
ContentTypeService.Save(ContentType);
var subpage = ContentBuilder.CreateSimpleContent(contentType, "Alternate Text Page 4", Textpage.Id);
ContentService.Save(subpage);
// And then add some more of the first type, so the one we'll filter out in tests isn't in the last page.
for (int i = 0; i < 5; i++)
{
subpage = ContentBuilder.CreateSimpleContent(ContentType, $"Text Page {5 + i}", Textpage.Id);
ContentService.Save(subpage);
}
}
[Test]
public void Can_Enumerate_Descendants_For_Content_Index()
{
var sut = CreateDeliveryApiContentIndexHelper();
var expectedNumberOfContentItems = GetExpectedNumberOfContentItems();
var contentEnumerated = 0;
Action<IContent[]> actionToPerform = content =>
{
contentEnumerated += content.Length;
};
const int pageSize = 3;
sut.EnumerateApplicableDescendantsForContentIndex(
Cms.Core.Constants.System.Root,
actionToPerform,
pageSize);
Assert.AreEqual(expectedNumberOfContentItems, contentEnumerated);
}
[Test]
public void Can_Enumerate_Descendants_For_Content_Index_With_Disallowed_Content_Type()
{
var sut = CreateDeliveryApiContentIndexHelper(["umbTextPage2"]);
var expectedNumberOfContentItems = GetExpectedNumberOfContentItems();
var contentEnumerated = 0;
Action<IContent[]> actionToPerform = content =>
{
contentEnumerated += content.Length;
};
const int pageSize = 3;
sut.EnumerateApplicableDescendantsForContentIndex(
Cms.Core.Constants.System.Root,
actionToPerform,
pageSize);
Assert.AreEqual(expectedNumberOfContentItems - 1, contentEnumerated);
}
private DeliveryApiContentIndexHelper CreateDeliveryApiContentIndexHelper(HashSet<string>? disallowedContentTypeAliases = null)
{
return new DeliveryApiContentIndexHelper(
ContentService,
GetRequiredService<IUmbracoDatabaseFactory>(),
GetDeliveryApiSettings(disallowedContentTypeAliases ?? []));
}
private IOptionsMonitor<DeliveryApiSettings> GetDeliveryApiSettings(HashSet<string> disallowedContentTypeAliases)
{
var deliveryApiSettings = new DeliveryApiSettings
{
DisallowedContentTypeAliases = disallowedContentTypeAliases,
};
var optionsMonitorMock = new Mock<IOptionsMonitor<DeliveryApiSettings>>();
optionsMonitorMock.Setup(o => o.CurrentValue).Returns(deliveryApiSettings);
return optionsMonitorMock.Object;
}
private int GetExpectedNumberOfContentItems()
{
// Count all non-trashed content items - matching the behavior of the old GetAllPublished method
// which was actually getting all non-trashed content, not just published content
var allContent = ContentService.GetPagedDescendants(Cms.Core.Constants.System.Root, 0, int.MaxValue, out var total);
var nonTrashedCount = allContent.Count(c => !c.Trashed);
Assert.AreEqual(10, nonTrashedCount);
return nonTrashedCount;
}
}