Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/HostedServices/TempFileCleanupTests.cs
Andrew McKaskill 04ac1542aa Refactor hostedServices into background jobs (#14291)
* Refactor jobs from HostedServices into BackgroundJobs

* Clean up generics and DI setup

* Add RecurringBackgroundJob Unit Tests

* Add ServiceCollection helper

* Add Obsolete attributes

* Add Notification Classes

* Add UnitTests for RecurringBackgroundJob HostedService

* Add NotificationEvents

* Add state to notifications

* Update UnitTests

* Add Obsolete Attributes to old hosted service classes

* Updated xmldoc in IRecurringBackgroundJob.cs

* Update Obsolete attribute messages to indicate classes will be removed in Umbraco 14

(cherry picked from commit c30ffa9ac3)
2023-11-13 11:10:58 +01:00

61 lines
2.2 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Runtime;
using Umbraco.Cms.Infrastructure.HostedServices;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.HostedServices
{
[TestFixture]
[Obsolete("Replaced by BackgroundJobs.Jobs.TempFileCleanupTests")]
public class TempFileCleanupTests
{
private Mock<IIOHelper> _mockIOHelper;
private readonly string _testPath = Path.Combine(TestContext.CurrentContext.TestDirectory.Split("bin")[0], "App_Data", "TEMP");
[Test]
public async Task Does_Not_Execute_When_Not_Main_Dom()
{
TempFileCleanup sut = CreateTempFileCleanup(isMainDom: false);
await sut.PerformExecuteAsync(null);
VerifyFilesNotCleaned();
}
[Test]
public async Task Executes_And_Cleans_Files()
{
TempFileCleanup sut = CreateTempFileCleanup();
await sut.PerformExecuteAsync(null);
VerifyFilesCleaned();
}
private TempFileCleanup CreateTempFileCleanup(bool isMainDom = true)
{
var mockMainDom = new Mock<IMainDom>();
mockMainDom.SetupGet(x => x.IsMainDom).Returns(isMainDom);
_mockIOHelper = new Mock<IIOHelper>();
_mockIOHelper.Setup(x => x.GetTempFolders())
.Returns(new DirectoryInfo[] { new(_testPath) });
_mockIOHelper.Setup(x => x.CleanFolder(It.IsAny<DirectoryInfo>(), It.IsAny<TimeSpan>()))
.Returns(CleanFolderResult.Success());
var mockLogger = new Mock<ILogger<TempFileCleanup>>();
return new TempFileCleanup(_mockIOHelper.Object, mockMainDom.Object, mockLogger.Object);
}
private void VerifyFilesNotCleaned() => VerifyFilesCleaned(Times.Never());
private void VerifyFilesCleaned() => VerifyFilesCleaned(Times.Once());
private void VerifyFilesCleaned(Times times) => _mockIOHelper.Verify(x => x.CleanFolder(It.Is<DirectoryInfo>(y => y.FullName == _testPath), It.IsAny<TimeSpan>()), times);
}
}