Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentVersionCleanupServiceTest.cs
Kenn Jacobsen 012b43a1c2 Publishing in the Management API (#14774)
* make CoreScopeProvider available for derived classes

* Create publish controller

* Add publish functionality

* Remove unneeded using

* Implement publish for multiple cultures

* support multiple cultures in controler

* Dont validate properties

* Refactor to use PublishingOperationStatus

* refactor to use proper publish async methods

* Refactor publish logic into own service

* Commit some demo code

* Add notes about what errors can happen when publishing

* Rework ContentPublishingService and introduce explicit Publish and PublishBranch methods in ContentService

* Fix merge

* Allow the publishing strategy to do its job

* Improved check for unsaved changes

* Make the old content controller work (as best possible)

* Remove SaveAndPublish (SaveAndPublishBranch) from all tests

* Proper guards for invalid cultures when publishing

* Fix edge cases for property validation and content unpublishing + add unpublishing to ContentPublishingService

* Clear out a few TODOs - we'll accept the behavior for now

* Unpublish controller

* Fix merge

* Fix branch publish notifications

* Added extra test for publishing unpublished cultures and added FIXME comments for when we fix the state of published cultures in content

---------

Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>
Co-authored-by: Zeegaan <nge@umbraco.dk>
2023-11-22 12:52:08 +01:00

123 lines
4.4 KiB
C#

using System.Diagnostics;
using NUnit.Framework;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
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.Services;
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
internal class ContentVersionCleanupServiceTest : UmbracoIntegrationTest
{
public IFileService FileService => GetRequiredService<IFileService>();
public IContentTypeService ContentTypeService => GetRequiredService<IContentTypeService>();
public IContentService ContentService => GetRequiredService<IContentService>();
public IContentVersionService ContentVersionService => GetRequiredService<IContentVersionService>();
/// <remarks>
/// This is covered by the unit tests, but nice to know it deletes on infra.
/// And proves implementation is compatible with SQL CE.
/// </remarks>
[Test]
public void PerformContentVersionCleanup_WithNoKeepPeriods_DeletesEverythingExceptActive()
{
// For reference, Our currently has
// 5000 Documents
// With 200K Versions
// With 11M Property data
var template = TemplateBuilder.CreateTextPageTemplate();
FileService.SaveTemplate(template);
var contentTypeA =
ContentTypeBuilder.CreateSimpleContentType("contentTypeA", "contentTypeA", defaultTemplateId: template.Id);
// Kill all historic
contentTypeA.HistoryCleanup.PreventCleanup = false;
contentTypeA.HistoryCleanup.KeepAllVersionsNewerThanDays = 0;
contentTypeA.HistoryCleanup.KeepLatestVersionPerDayForDays = 0;
ContentTypeService.Save(contentTypeA);
var content = ContentBuilder.CreateSimpleContent(contentTypeA);
ContentService.Save(content);
ContentService.Publish(content, Array.Empty<string>());
for (var i = 0; i < 10; i++)
{
ContentService.Publish(content, Array.Empty<string>());
}
var before = GetReport();
Debug.Assert(before.ContentVersions == 12); // 10 historic + current draft + current published
Debug.Assert(before.PropertyData == 12 * 3); // CreateSimpleContentType = 3 props
ContentVersionService.PerformContentVersionCleanup(DateTime.Now.AddHours(1));
var after = GetReport();
Assert.Multiple(() =>
{
Assert.AreEqual(2, after.ContentVersions); // current draft, current published
Assert.AreEqual(2, after.DocumentVersions);
Assert.AreEqual(6, after.PropertyData); // CreateSimpleContentType = 3 props
});
}
private Report GetReport()
{
using (var scope = ScopeProvider.CreateScope(autoComplete: true))
{
// SQL CE is fun!
var contentVersions =
ScopeAccessor.AmbientScope.Database.Single<int>(@"select count(1) from umbracoContentVersion");
var documentVersions =
ScopeAccessor.AmbientScope.Database.Single<int>(@"select count(1) from umbracoDocumentVersion");
var propertyData =
ScopeAccessor.AmbientScope.Database.Single<int>(@"select count(1) from umbracoPropertyData");
return new Report
{
ContentVersions = contentVersions,
DocumentVersions = documentVersions,
PropertyData = propertyData
};
}
}
private void InsertCleanupPolicy(IContentType contentType, int daysToKeepAll, int daysToRollupAll,
bool preventCleanup = false)
{
using (var scope = ScopeProvider.CreateScope(autoComplete: true))
{
var entity = new ContentVersionCleanupPolicyDto
{
ContentTypeId = contentType.Id,
KeepAllVersionsNewerThanDays = daysToKeepAll,
KeepLatestVersionPerDayForDays = daysToRollupAll,
PreventCleanup = preventCleanup,
Updated = DateTime.Today
};
ScopeAccessor.AmbientScope.Database.Insert(entity);
}
}
private class Report
{
public int ContentVersions { get; set; }
public int DocumentVersions { get; set; }
public int PropertyData { get; set; }
}
}