Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Core/Services/MediaEditingServiceTests.cs
T
Chris HoustonandGitHub 0d4f24300a fix(code-quality): resolve CS0628 warnings - change protected to private in sealed classes. (#21193)
* fix(code-quality): resolve CS0628 warnings - change protected to private in sealed classes

Changed protected members to private in sealed classes across 26 files. Protected members in sealed classes serve no purpose since sealed classes cannot be inherited. This eliminates 57 CS0628 compiler warnings.

* fix: use public for NUnit SetUp methods per Copilot review

NUnit requires SetUp methods to be at least protected. Using public
avoids CS0628 while allowing NUnit to discover and execute the methods.

* Clean up

- Renames internal fields to clearer names and aligns with conventions
- Changes internal cache holder to use auto-properties for state ( fixes another build warning )
- Removes an unused exception type from the loader and cleans up unused usings
- Adds "Umbraco" to the list of known spellings in .vsCode settings file, amazed this wasn't already there :)

* Improvements to fix CodeScene Code Health Review issues.

- Introduces debug-only logging helpers and routes all log messages through them for consistency
- Centralizes retrieval of discoverable types and scanning logic to simplify paths
- Aligns logging of cached vs non-cached and slow paths with new helpers
- Documents data-holding structure used to store type lists for clarity

* Refactoring to make CodeScene happy, removing code duplication :)
2025-12-18 19:28:13 +01:00

119 lines
4.6 KiB
C#

using System.Text.Json.Nodes;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.OperationStatus;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
internal sealed class MediaEditingServiceTests : UmbracoIntegrationTest
{
private IMediaTypeService MediaTypeService => GetRequiredService<IMediaTypeService>();
private IMediaEditingService MediaEditingService => GetRequiredService<IMediaEditingService>();
private IMediaType ImageMediaType { get; set; }
private IMediaType ArticleMediaType { get; set; }
[SetUp]
public async Task Setup()
{
ImageMediaType = MediaTypeService.Get(Constants.Conventions.MediaTypes.Image);
ArticleMediaType = MediaTypeService.Get(Constants.Conventions.MediaTypes.ArticleAlias);
}
[Test]
public async Task Cannot_Create_Media_With_Mandatory_File_Property_Without_Providing_File()
{
var imageModel = CreateMediaCreateModel("Image", Guid.NewGuid(), ImageMediaType.Key);
var imageCreateAttempt = await MediaEditingService.CreateAsync(imageModel, Constants.Security.SuperUserKey);
Assert.IsFalse(imageCreateAttempt.Success);
Assert.AreEqual(ContentEditingOperationStatus.PropertyValidationError, imageCreateAttempt.Status);
}
[Test]
public async Task Can_Create_Media_With_Mandatory_File_Property_With_File_Provided()
{
var imageModel = CreateMediaCreateModelWithFile("Image", Guid.NewGuid(), ArticleMediaType.Key);
var imageCreateAttempt = await MediaEditingService.CreateAsync(imageModel, Constants.Security.SuperUserKey);
Assert.IsTrue(imageCreateAttempt.Success);
Assert.AreEqual(ContentEditingOperationStatus.Success, imageCreateAttempt.Status);
}
[Test]
public async Task Can_Create_Media_With_Optional_File_Property_Without_Providing_File()
{
ImageMediaType.PropertyTypes.First(x => x.Alias == Constants.Conventions.Media.File).Mandatory = false;
MediaTypeService.Save(ImageMediaType);
var imageModel = CreateMediaCreateModel("Image", Guid.NewGuid(), ImageMediaType.Key);
var imageCreateAttempt = await MediaEditingService.CreateAsync(imageModel, Constants.Security.SuperUserKey);
// Assert
Assert.IsTrue(imageCreateAttempt.Success);
Assert.AreEqual(ContentEditingOperationStatus.Success, imageCreateAttempt.Status);
}
[Test]
public async Task Can_Update_Media_With_Mandatory_File_Property_With_File_Provided()
{
Guid articleKey = Guid.NewGuid();
var articleModel = CreateMediaCreateModelWithFile("Article", articleKey, ArticleMediaType.Key);
var articleCreateAttempt = await MediaEditingService.CreateAsync(articleModel, Constants.Security.SuperUserKey);
Assert.IsTrue(articleCreateAttempt.Success);
var updateModel = new MediaUpdateModel
{
Properties =
[
new PropertyValueModel
{
Alias = Constants.Conventions.Media.File,
Value = new JsonObject
{
{ "src", string.Empty },
},
}
],
Variants = articleModel.Variants,
};
var articleUpdateAttempt = await MediaEditingService.ValidateUpdateAsync(articleKey, updateModel);
Assert.IsFalse(articleUpdateAttempt.Success);
Assert.AreEqual(ContentEditingOperationStatus.PropertyValidationError, articleUpdateAttempt.Status);
}
private static MediaCreateModel CreateMediaCreateModel(string name, Guid key, Guid mediaTypeKey)
=> new()
{
ContentTypeKey = mediaTypeKey,
ParentKey = Constants.System.RootKey,
Variants = [new() { Name = name }],
Key = key,
};
private static MediaCreateModel CreateMediaCreateModelWithFile(string name, Guid key, Guid mediaTypeKey)
{
var model = CreateMediaCreateModel(name, key, mediaTypeKey);
model.Properties = [
new PropertyValueModel
{
Alias = Constants.Conventions.Media.File,
Value = new JsonObject
{
{ "src", "reference-to-file" },
},
}
];
return model;
}
}