116 lines
4.4 KiB
C#
116 lines
4.4 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using NUnit.Framework;
|
|
using Umbraco.Cms.Core.Models;
|
|
using Umbraco.Cms.Core.Scoping;
|
|
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 currently has
|
|
// 5000 Documents
|
|
// With 200K Versions
|
|
// With 11M Property data
|
|
|
|
Template template = TemplateBuilder.CreateTextPageTemplate();
|
|
FileService.SaveTemplate(template);
|
|
|
|
ContentType contentTypeA = ContentTypeBuilder.CreateSimpleContentType("contentTypeA", "contentTypeA", defaultTemplateId: template.Id);
|
|
ContentTypeService.Save(contentTypeA);
|
|
|
|
Content content = ContentBuilder.CreateSimpleContent(contentTypeA);
|
|
ContentService.SaveAndPublish(content);
|
|
|
|
for (var i = 0; i < 10; i++)
|
|
{
|
|
ContentService.SaveAndPublish(content);
|
|
}
|
|
|
|
Report before = GetReport();
|
|
|
|
Debug.Assert(before.ContentVersions == 12); // 10 historic + current draft + current published
|
|
Debug.Assert(before.PropertyData == 12 * 3); // CreateSimpleContentType = 3 props
|
|
|
|
// Kill all historic
|
|
InsertCleanupPolicy(contentTypeA, 0, 0);
|
|
|
|
ContentVersionService.PerformContentVersionCleanup(DateTime.Now.AddHours(1));
|
|
|
|
Report 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 (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
|
|
{
|
|
// SQL CE is fun!
|
|
var contentVersions = scope.Database.Single<int>(@"select count(1) from umbracoContentVersion");
|
|
var documentVersions = scope.Database.Single<int>(@"select count(1) from umbracoDocumentVersion");
|
|
var propertyData = scope.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 (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
|
|
{
|
|
var entity = new ContentVersionCleanupPolicyDto
|
|
{
|
|
ContentTypeId = contentType.Id,
|
|
KeepAllVersionsNewerThanDays = daysToKeepAll,
|
|
KeepLatestVersionPerDayForDays = daysToRollupAll,
|
|
PreventCleanup = preventCleanup,
|
|
Updated = DateTime.Today
|
|
};
|
|
|
|
scope.Database.Insert(entity);
|
|
}
|
|
}
|
|
|
|
private class Report
|
|
{
|
|
public int ContentVersions { get; set; }
|
|
|
|
public int DocumentVersions { get; set; }
|
|
|
|
public int PropertyData { get; set; }
|
|
}
|
|
}
|
|
}
|