Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V15: Dont create invalid media #17534

Open
wants to merge 6 commits into
base: v15/dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public async Task<IActionResult> Create(

return result.Success
? CreatedAtId<ByKeyMediaController>(controller => nameof(controller.ByKey), result.Result.Content!.Key)
: ContentEditingOperationStatusResult(result.Status);
: MediaEditingOperationStatusResult(result.Status, requestModel, result.Result.ValidationResult);
});
}
8 changes: 7 additions & 1 deletion src/Umbraco.Core/Services/MediaEditingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,18 @@ public async Task<Attempt<MediaCreateResult, ContentEditingOperationStatus>> Cre
ContentEditingOperationStatus validationStatus = result.Status;
ContentValidationResult validationResult = result.Result.ValidationResult;

// If we have property validation errors, don't allow saving, as media only supports "published" status.
if (result.Status == ContentEditingOperationStatus.PropertyValidationError)
{
return Attempt.FailWithStatus(validationStatus, new MediaCreateResult { ValidationResult = validationResult });
}

IMedia media = result.Result.Content!;

var currentUserId = await GetUserIdAsync(userKey);
ContentEditingOperationStatus operationStatus = Save(media, currentUserId);
return operationStatus == ContentEditingOperationStatus.Success
? Attempt.SucceedWithStatus(validationStatus, new MediaCreateResult { Content = media, ValidationResult = validationResult })
? Attempt.SucceedWithStatus(validationStatus, new MediaCreateResult { Content = media })
: Attempt.FailWithStatus(operationStatus, new MediaCreateResult { Content = media });
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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)]
public class MediaEditingServiceTests : UmbracoIntegrationTest
{
protected IMediaTypeService MediaTypeService => GetRequiredService<IMediaTypeService>();

protected IMediaEditingService MediaEditingService => GetRequiredService<IMediaEditingService>();

protected IMediaType ImageMediaType { get; set; }

[SetUp]
public async Task Setup()
{
ImageMediaType = MediaTypeService.Get(Constants.Conventions.MediaTypes.Image);
}

[Test]
public async Task Cannot_Create_Media_With_Mandatory_Property()
{
var imageModel = CreateMediaCreateModel("Image", new Guid(), ImageMediaType.Key);
var imageCreateAttempt = await MediaEditingService.CreateAsync(imageModel, Constants.Security.SuperUserKey);

// Assert
Assert.IsFalse(imageCreateAttempt.Success);
Assert.AreEqual(ContentEditingOperationStatus.PropertyValidationError, imageCreateAttempt.Status);
}

[Test]
public async Task Can_Create_Media_Without_Mandatory_Property()
{
ImageMediaType.PropertyTypes.First(x => x.Alias == "umbracoFile").Mandatory = false;
MediaTypeService.Save(ImageMediaType);
var imageModel = CreateMediaCreateModel("Image", new Guid(), ImageMediaType.Key);
var imageCreateAttempt = await MediaEditingService.CreateAsync(imageModel, Constants.Security.SuperUserKey);

// Assert
Assert.IsTrue(imageCreateAttempt.Success);
Assert.AreEqual(ContentEditingOperationStatus.Success, imageCreateAttempt.Status);
}

private MediaCreateModel CreateMediaCreateModel(string name, Guid key, Guid mediaTypeKey)
=> new()
{
ContentTypeKey = mediaTypeKey,
ParentKey = Constants.System.RootKey,
InvariantName = name,
Key = key,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public async Task Setup()
// Media Types
FolderMediaType = MediaTypeService.Get(Constants.Conventions.MediaTypes.Folder);
ImageMediaType = MediaTypeService.Get(Constants.Conventions.MediaTypes.Image);
ImageMediaType.PropertyTypes.First(x => x.Alias == "umbracoFile").Mandatory = false;
MediaTypeService.Save(ImageMediaType);

// Media
var albumModel = CreateMediaCreateModel("Album", new Guid("1CD97C02-8534-4B72-AE9E-AE52EC94CF31"), FolderMediaType.Key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ private void CreateTenMediaItemsFromTwoMediaTypesAtRoot()
private async Task<IMedia> CreateRootMediaWithFiveChildrenAsListViewItems(Guid? listViewDataTypeKey = null)
{
var childImageMediaType = MediaTypeService.Get(Constants.Conventions.MediaTypes.Image);
childImageMediaType.PropertyTypes.First(x => x.Alias == "umbracoFile").Mandatory = false;
MediaTypeService.Save(childImageMediaType);

var mediaTypeWithListView = new MediaTypeBuilder()
.WithAlias("album")
Expand Down
Loading