Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/TrackedReferencesServiceTests.cs
Bjarke Berg 082d504b33 Fix tracked reference queries for SqlServer (#16020)
* Fixed issue with SqlServer and optimized queries to not do the actual paging if total count is 0

* Cleanup

---------

Co-authored-by: Elitsa <elm@umbraco.dk>
2024-04-10 12:03:32 +02:00

92 lines
2.8 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
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;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services;
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
public class TrackedReferencesServiceTests : UmbracoIntegrationTest
{
private IContentTypeService ContentTypeService => GetRequiredService<IContentTypeService>();
private IContentService ContentService => GetRequiredService<IContentService>();
private Content Root1 { get; set; }
private Content Root2 { get; set; }
private IContentType ContentType { get; set; }
[SetUp]
public void Setup() => CreateTestData();
protected virtual void CreateTestData()
{
ContentType = new ContentTypeBuilder()
.WithName("Page")
.AddPropertyType()
.WithAlias("ContentPicker")
.WithName("contentPicker")
.WithDataTypeId(1046)
.WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.ContentPicker)
.Done()
.Build();
ContentTypeService.Save(ContentType);
Root1 = new ContentBuilder()
.WithContentType(ContentType)
.WithName("Root 1")
.Build();
ContentService.Save(Root1);
ContentService.Publish(Root1, ["*"]);
Root2 = new ContentBuilder()
.WithContentType(ContentType)
.WithName("Root 2")
.WithPropertyValues(new
{
contentPicker = Udi.Create(Constants.UdiEntityType.Document, Root1.Key) // contentPicker is the alias of the property type
})
.Build();
ContentService.Save(Root2);
ContentService.Publish(Root2, ["*"]);
}
[Test]
public async Task Get_Pages_That_Reference_This()
{
var sut = GetRequiredService<ITrackedReferencesService>();
var actual = await sut.GetPagedRelationsForItemAsync(Root1.Key, 0, 10, true);
Assert.Multiple(() =>
{
Assert.AreEqual(1, actual.Total);
var item = actual.Items.FirstOrDefault();
Assert.AreEqual(Root2.ContentType.Alias, item?.ContentTypeAlias);
Assert.AreEqual(Root2.Key, item?.NodeKey);
});
}
[Test]
public async Task Does_not_return_references_if_item_is_not_referenced()
{
var sut = GetRequiredService<ITrackedReferencesService>();
var actual = await sut.GetPagedRelationsForItemAsync(Root2.Key, 0, 10, true);
Assert.AreEqual(0, actual.Total);
}
}