Skip to content

Commit

Permalink
Cleanup and fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bergmania committed Nov 2, 2021
1 parent 926c74d commit b7d5fd4
Show file tree
Hide file tree
Showing 20 changed files with 183 additions and 91 deletions.
2 changes: 1 addition & 1 deletion src/Umbraco.Core/Models/ContentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Umbraco.Cms.Core.Models
/// </summary>
[Serializable]
[DataContract(IsReference = true)]
public class ContentType : ContentTypeCompositionBase, IContentType
public class ContentType : ContentTypeCompositionBase, IContentTypeWithHistoryCleanup
{
public const bool SupportsPublishingConst = true;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Umbraco.Core.Models
namespace Umbraco.Cms.Core.Models
{
public class ContentVersionCleanupPolicySettings
{
Expand Down
18 changes: 12 additions & 6 deletions src/Umbraco.Core/Models/IContentType.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Umbraco.Cms.Core.Models.ContentEditing;

namespace Umbraco.Cms.Core.Models
{
[Obsolete("This will be merged into IContentType in Umbraco 10")]
public interface IContentTypeWithHistoryCleanup : IContentType
{
/// <summary>
/// Gets or Sets the history cleanup configuration
/// </summary>
HistoryCleanup HistoryCleanup { get; set; }
}


/// <summary>
/// Defines a ContentType, which Content is based on
/// </summary>
Expand All @@ -23,11 +34,6 @@ public interface IContentType : IContentTypeComposition
/// </summary>
IEnumerable<ITemplate> AllowedTemplates { get; set; }

/// <summary>
/// Gets or Sets the history cleanup configuration
/// </summary>
HistoryCleanup HistoryCleanup { get; set; }

/// <summary>
/// Determines if AllowedTemplates contains templateId
/// </summary>
Expand Down
34 changes: 23 additions & 11 deletions src/Umbraco.Core/Models/Mapping/ContentTypeMapDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ public ContentTypeMapDefinition(CommonMapper commonMapper, PropertyEditorCollect
IMemberTypeService memberTypeService,
ILoggerFactory loggerFactory, IShortStringHelper shortStringHelper, IOptions<GlobalSettings> globalSettings,
IHostingEnvironment hostingEnvironment)
: this(commonMapper, propertyEditors, dataTypeService, fileService, contentTypeService, mediaTypeService, memberTypeService, loggerFactory, shortStringHelper, globalSettings, hostingEnvironment, StaticServiceProvider.Instance.GetRequiredService<IOptionsMonitor<ContentSettings>>())
: this(commonMapper, propertyEditors, dataTypeService, fileService, contentTypeService, mediaTypeService,
memberTypeService, loggerFactory, shortStringHelper, globalSettings, hostingEnvironment,
StaticServiceProvider.Instance.GetRequiredService<IOptionsMonitor<ContentSettings>>())
{

}

public ContentTypeMapDefinition(CommonMapper commonMapper, PropertyEditorCollection propertyEditors,
Expand Down Expand Up @@ -130,7 +131,10 @@ private void Map(DocumentTypeSave source, IContentType target, MapperContext con
MapSaveToTypeBase<DocumentTypeSave, PropertyTypeBasic>(source, target, context);
MapComposition(source, target, alias => _contentTypeService.Get(alias));

target.HistoryCleanup = source.HistoryCleanup;
if (target is IContentTypeWithHistoryCleanup targetWithHistoryCleanup)
{
targetWithHistoryCleanup.HistoryCleanup = source.HistoryCleanup;
}

target.AllowedTemplates = source.AllowedTemplates
.Where(x => x != null)
Expand Down Expand Up @@ -177,15 +181,23 @@ private void Map(IContentType source, DocumentTypeDisplay target, MapperContext
{
MapTypeToDisplayBase<DocumentTypeDisplay, PropertyTypeDisplay>(source, target);

target.HistoryCleanup = new HistoryCleanupViewModel
if (source is IContentTypeWithHistoryCleanup sourceWithHistoryCleanup)
{
PreventCleanup = source.HistoryCleanup.PreventCleanup,
KeepAllVersionsNewerThanDays = source.HistoryCleanup.KeepAllVersionsNewerThanDays,
KeepLatestVersionPerDayForDays = source.HistoryCleanup.KeepLatestVersionPerDayForDays,
GlobalKeepAllVersionsNewerThanDays = _contentSettings.ContentVersionCleanupPolicy.KeepAllVersionsNewerThanDays,
GlobalKeepLatestVersionPerDayForDays = _contentSettings.ContentVersionCleanupPolicy.KeepLatestVersionPerDayForDays,
GlobalEnableCleanup = _contentSettings.ContentVersionCleanupPolicy.EnableCleanup
};
target.HistoryCleanup = new HistoryCleanupViewModel
{
PreventCleanup = sourceWithHistoryCleanup.HistoryCleanup.PreventCleanup,
KeepAllVersionsNewerThanDays =
sourceWithHistoryCleanup.HistoryCleanup.KeepAllVersionsNewerThanDays,
KeepLatestVersionPerDayForDays =
sourceWithHistoryCleanup.HistoryCleanup.KeepLatestVersionPerDayForDays,
GlobalKeepAllVersionsNewerThanDays =
_contentSettings.ContentVersionCleanupPolicy.KeepAllVersionsNewerThanDays,
GlobalKeepLatestVersionPerDayForDays =
_contentSettings.ContentVersionCleanupPolicy.KeepLatestVersionPerDayForDays,
GlobalEnableCleanup = _contentSettings.ContentVersionCleanupPolicy.EnableCleanup
};
}


target.AllowCultureVariant = source.VariesByCulture();
target.AllowSegmentVariant = source.VariesBySegment();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Collections.Generic;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Persistence;
using Umbraco.Core.Models;

namespace Umbraco.Core.Persistence.Repositories
namespace Umbraco.Cms.Core.Persistence.Repositories
{
public interface IDocumentVersionRepository : IRepository
{
Expand Down
3 changes: 1 addition & 2 deletions src/Umbraco.Core/Services/IContentVersionCleanupPolicy.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Collections.Generic;
using Umbraco.Cms.Core.Models;
using Umbraco.Core.Models;

namespace Umbraco.Core.Services
namespace Umbraco.Cms.Core.Services
{
/// <summary>
/// Used to filter historic content versions for cleanup.
Expand Down
1 change: 0 additions & 1 deletion src/Umbraco.Core/Services/IContentVersionService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using Umbraco.Cms.Core.Models;
using Umbraco.Core.Models;

namespace Umbraco.Cms.Core.Services
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ internal static IUmbracoBuilder AddRepositories(this IUmbracoBuilder builder)
builder.Services.AddUnique<IDictionaryRepository, DictionaryRepository>();
builder.Services.AddUnique<IDocumentBlueprintRepository, DocumentBlueprintRepository>();
builder.Services.AddUnique<IDocumentRepository, DocumentRepository>();
builder.Services.AddUnique<IDocumentVersionRepository, DocumentVersionRepository>();
builder.Services.AddUnique<IDocumentTypeContainerRepository, DocumentTypeContainerRepository>();
builder.Services.AddUnique<IDomainRepository, DomainRepository>();
builder.Services.AddUnique<IEntityRepository, EntityRepository>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Configuration;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Hosting;
Expand All @@ -16,6 +15,7 @@
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.Implement;
using Umbraco.Cms.Infrastructure.Packaging;
using Umbraco.Cms.Infrastructure.Services.Implement;
using Umbraco.Extensions;

namespace Umbraco.Cms.Infrastructure.DependencyInjection
Expand Down Expand Up @@ -44,6 +44,7 @@ internal static IUmbracoBuilder AddServices(this IUmbracoBuilder builder)
builder.Services.AddUnique<ITagService, TagService>();
builder.Services.AddUnique<IContentService, ContentService>();
builder.Services.AddUnique<IContentVersionService, ContentVersionService>();
builder.Services.AddUnique<IContentVersionCleanupPolicy, DefaultContentVersionCleanupPolicy>();
builder.Services.AddUnique<IUserService, UserService>();
builder.Services.AddUnique<IMemberService, MemberService>();
builder.Services.AddUnique<IMediaService, MediaService>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Umbraco.Cms.Core.Runtime;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Sync;
using Umbraco.Core.Services;

namespace Umbraco.Cms.Infrastructure.HostedServices
{
Expand All @@ -18,38 +17,36 @@ public class ContentVersionCleanup : RecurringHostedServiceBase
{
private readonly IRuntimeState _runtimeState;
private readonly ILogger<ContentVersionCleanup> _logger;
private readonly IOptionsMonitor<ContentSettings> _settingsMonitor;
private readonly IContentVersionService _service;
private readonly IMainDom _mainDom;
private readonly IServerRoleAccessor _serverRoleAccessor;
private ContentVersionCleanupPolicySettings _settings;

/// <summary>
/// Initializes a new instance of the <see cref="ContentVersionCleanup"/> class.
/// </summary>
public ContentVersionCleanup(
IRuntimeState runtimeState,
ILogger<ContentVersionCleanup> logger,
IOptionsMonitor<ContentSettings> settings,
IOptionsMonitor<ContentSettings> settingsMonitor,
IContentVersionService service,
IMainDom mainDom,
IServerRoleAccessor serverRoleAccessor)
: base(TimeSpan.FromDays(1), TimeSpan.FromMinutes(1))
{
_runtimeState = runtimeState;
_logger = logger;
_settings = settings.CurrentValue.ContentVersionCleanupPolicy;
_settingsMonitor = settingsMonitor;
_service = service;
_mainDom = mainDom;
_serverRoleAccessor = serverRoleAccessor;

settings.OnChange(x => _settings = x.ContentVersionCleanupPolicy);
}

/// <inheritdoc />
public override Task PerformExecuteAsync(object state)
{
// Globally disabled by feature flag
if (!_settings.EnableCleanup)
if (!_settingsMonitor.CurrentValue.ContentVersionCleanupPolicy.EnableCleanup)
{
_logger.LogInformation("ContentVersionCleanup task will not run as it has been globally disabled via configuration");
return Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_8_7_0;
using Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_8_9_0;
using Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_9_0_0;
using Umbraco.Core.Migrations.Upgrade.V_9_1_0;
using Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_9_1_0;
using Umbraco.Extensions;

namespace Umbraco.Cms.Infrastructure.Migrations.Upgrade
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
using Umbraco.Extensions;

namespace Umbraco.Core.Migrations.Upgrade.V_9_1_0
namespace Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_9_1_0
{
class AddContentVersionCleanupFeature : MigrationBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,19 @@ protected override void PersistUpdatedItem(IContentType entity)

private void PersistHistoryCleanup(IContentType entity)
{
ContentVersionCleanupPolicyDto dto = new ContentVersionCleanupPolicyDto()
if (entity is IContentTypeWithHistoryCleanup entityWithHistoryCleanup)
{
ContentTypeId = entity.Id,
Updated = DateTime.Now,
PreventCleanup = entity.HistoryCleanup.PreventCleanup,
KeepAllVersionsNewerThanDays = entity.HistoryCleanup.KeepAllVersionsNewerThanDays,
KeepLatestVersionPerDayForDays = entity.HistoryCleanup.KeepLatestVersionPerDayForDays,
};
Database.InsertOrUpdate(dto);
ContentVersionCleanupPolicyDto dto = new ContentVersionCleanupPolicyDto()
{
ContentTypeId = entity.Id,
Updated = DateTime.Now,
PreventCleanup = entityWithHistoryCleanup.HistoryCleanup.PreventCleanup,
KeepAllVersionsNewerThanDays = entityWithHistoryCleanup.HistoryCleanup.KeepAllVersionsNewerThanDays,
KeepLatestVersionPerDayForDays = entityWithHistoryCleanup.HistoryCleanup.KeepLatestVersionPerDayForDays,
};
Database.InsertOrUpdate(dto);
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
using System.Linq;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence.Repositories;
using Umbraco.Extensions;

namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Core.Persistence.Repositories;
using Umbraco.Core.Services;
using Umbraco.Extensions;

namespace Umbraco.Cms.Core.Services.Implement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence.Repositories;
using Umbraco.Core.Services;
using ContentVersionCleanupPolicySettings = Umbraco.Core.Models.ContentVersionCleanupPolicySettings;
using Umbraco.Cms.Core.Services;
using ContentVersionCleanupPolicySettings = Umbraco.Cms.Core.Models.ContentVersionCleanupPolicySettings;

namespace Umbraco.Cms.Infrastructure.Services.Implement
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics;
using System.Linq;
using NUnit.Framework;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
using Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement;
Expand All @@ -25,8 +26,11 @@ public class DocumentVersionRepositoryTest : UmbracoIntegrationTest
[Test]
public void GetDocumentVersionsEligibleForCleanup_Always_ExcludesActiveVersions()
{
var contentType = ContentTypeBuilder.CreateSimpleContentType("umbTextpage", "Textpage");
FileService.SaveTemplate(contentType.DefaultTemplate);
Template template = TemplateBuilder.CreateTextPageTemplate();
FileService.SaveTemplate(template);

var contentType = ContentTypeBuilder.CreateSimpleContentType("umbTextpage", "Textpage", defaultTemplateId: template.Id);
ContentTypeService.Save(contentType);
ContentTypeService.Save(contentType);

var content = ContentBuilder.CreateSimpleContent(contentType);
Expand All @@ -53,8 +57,11 @@ public void GetDocumentVersionsEligibleForCleanup_Always_ExcludesActiveVersions(
[Test]
public void GetDocumentVersionsEligibleForCleanup_Always_ExcludesPinnedVersions()
{
var contentType = ContentTypeBuilder.CreateSimpleContentType("umbTextpage", "Textpage");
FileService.SaveTemplate(contentType.DefaultTemplate);
Template template = TemplateBuilder.CreateTextPageTemplate();
FileService.SaveTemplate(template);

var contentType = ContentTypeBuilder.CreateSimpleContentType("umbTextpage", "Textpage", defaultTemplateId: template.Id);
ContentTypeService.Save(contentType);
ContentTypeService.Save(contentType);

var content = ContentBuilder.CreateSimpleContent(contentType);
Expand Down Expand Up @@ -92,8 +99,10 @@ public void GetDocumentVersionsEligibleForCleanup_Always_ExcludesPinnedVersions(
[Test]
public void DeleteVersions_Always_DeletesSpecifiedVersions()
{
var contentType = ContentTypeBuilder.CreateSimpleContentType("umbTextpage", "Textpage");
FileService.SaveTemplate(contentType.DefaultTemplate);
Template template = TemplateBuilder.CreateTextPageTemplate();
FileService.SaveTemplate(template);

var contentType = ContentTypeBuilder.CreateSimpleContentType("umbTextpage", "Textpage", defaultTemplateId: template.Id);
ContentTypeService.Save(contentType);

var content = ContentBuilder.CreateSimpleContent(contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal class ContentVersionCleanupServiceTest : UmbracoIntegrationTest
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.
Expand All @@ -33,8 +34,10 @@ public void PerformContentVersionCleanup_WithNoKeepPeriods_DeletesEverythingExce
// With 200K Versions
// With 11M Property data

ContentType contentTypeA = ContentTypeBuilder.CreateSimpleContentType("contentTypeA", "contentTypeA");
FileService.SaveTemplate(contentTypeA.DefaultTemplate);
Template template = TemplateBuilder.CreateTextPageTemplate();
FileService.SaveTemplate(template);

ContentType contentTypeA = ContentTypeBuilder.CreateSimpleContentType("contentTypeA", "contentTypeA", defaultTemplateId: template.Id);
ContentTypeService.Save(contentTypeA);

Content content = ContentBuilder.CreateSimpleContent(contentTypeA);
Expand All @@ -53,7 +56,7 @@ public void PerformContentVersionCleanup_WithNoKeepPeriods_DeletesEverythingExce
// Kill all historic
InsertCleanupPolicy(contentTypeA, 0, 0);

((IContentVersionService)ContentService).PerformContentVersionCleanup(DateTime.Now.AddHours(1));
ContentVersionService.PerformContentVersionCleanup(DateTime.Now.AddHours(1));

Report after = GetReport();

Expand Down
Loading

0 comments on commit b7d5fd4

Please sign in to comment.