// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschränkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using FluentAssertions; using Squidex.Domain.Apps.Core.Assets; using Squidex.Domain.Apps.Core.Schemas; using Squidex.Domain.Apps.Core.ValidateContent; using Squidex.Infrastructure.Collections; using Squidex.Infrastructure.Json.Objects; using Xunit; namespace Squidex.Domain.Apps.Core.Operations.ValidateContent { public class AssetsFieldTests { private readonly List errors = new List(); public sealed class AssetInfo : IAssetInfo { public Guid AssetId { get; set; } public string FileName { get; set; } public string FileHash { get; set; } public string Slug { get; set; } public long FileSize { get; set; } public bool IsImage { get; set; } public int? PixelWidth { get; set; } public int? PixelHeight { get; set; } public AssetMetadata Metadata { get; set; } public AssetType Type { get; set; } } private readonly AssetInfo document = new AssetInfo { AssetId = Guid.NewGuid(), FileName = "MyDocument.pdf", FileSize = 1024 * 4, Type = AssetType.Unknown }; private readonly AssetInfo image1 = new AssetInfo { AssetId = Guid.NewGuid(), FileName = "MyImage.png", FileSize = 1024 * 8, Type = AssetType.Image, Metadata = new AssetMetadata() .SetPixelWidth(800) .SetPixelHeight(600) }; private readonly AssetInfo image2 = new AssetInfo { AssetId = Guid.NewGuid(), FileName = "MyImage.png", FileSize = 1024 * 8, Type = AssetType.Image, Metadata = new AssetMetadata() .SetPixelWidth(800) .SetPixelHeight(600) }; private readonly ValidationContext ctx; public AssetsFieldTests() { ctx = ValidationTestExtensions.Assets(image1, image2, document); } [Fact] public void Should_instantiate_field() { var sut = Field(new AssetsFieldProperties()); Assert.Equal("my-assets", sut.Name); } [Fact] public async Task Should_not_add_error_if_assets_are_valid() { var sut = Field(new AssetsFieldProperties()); await sut.ValidateAsync(CreateValue(document.AssetId), errors, ctx); Assert.Empty(errors); } [Fact] public async Task Should_not_add_error_if_assets_are_null_and_valid() { var sut = Field(new AssetsFieldProperties()); await sut.ValidateAsync(CreateValue(null), errors, ctx); Assert.Empty(errors); } [Fact] public async Task Should_not_add_error_if_number_of_assets_is_equal_to_min_and_max_items() { var sut = Field(new AssetsFieldProperties { MinItems = 2, MaxItems = 2 }); await sut.ValidateAsync(CreateValue(image1.AssetId, image2.AssetId), errors, ctx); Assert.Empty(errors); } [Fact] public async Task Should_not_add_error_if_duplicate_values_are_ignored() { var sut = Field(new AssetsFieldProperties { AllowDuplicates = true }); await sut.ValidateAsync(CreateValue(image1.AssetId, image1.AssetId), errors, ctx); Assert.Empty(errors); } [Fact] public async Task Should_add_error_if_assets_are_required_and_null() { var sut = Field(new AssetsFieldProperties { IsRequired = true }); await sut.ValidateAsync(CreateValue(null), errors, ctx); errors.Should().BeEquivalentTo( new[] { "Field is required." }); } [Fact] public async Task Should_add_error_if_assets_are_required_and_empty() { var sut = Field(new AssetsFieldProperties { IsRequired = true }); await sut.ValidateAsync(CreateValue(), errors, ctx); errors.Should().BeEquivalentTo( new[] { "Field is required." }); } [Fact] public async Task Should_add_error_if_value_is_not_valid() { var sut = Field(new AssetsFieldProperties()); await sut.ValidateAsync(JsonValue.Create("invalid"), errors); errors.Should().BeEquivalentTo( new[] { "Not a valid value." }); } [Fact] public async Task Should_add_error_if_value_has_not_enough_items() { var sut = Field(new AssetsFieldProperties { MinItems = 3 }); await sut.ValidateAsync(CreateValue(image1.AssetId, image2.AssetId), errors, ctx); errors.Should().BeEquivalentTo( new[] { "Must have at least 3 item(s)." }); } [Fact] public async Task Should_add_error_if_value_has_too_much_items() { var sut = Field(new AssetsFieldProperties { MaxItems = 1 }); await sut.ValidateAsync(CreateValue(image1.AssetId, image2.AssetId), errors, ctx); errors.Should().BeEquivalentTo( new[] { "Must not have more than 1 item(s)." }); } [Fact] public async Task Should_add_error_if_asset_are_not_valid() { var assetId = Guid.NewGuid(); var sut = Field(new AssetsFieldProperties()); await sut.ValidateAsync(CreateValue(assetId), errors, ctx); errors.Should().BeEquivalentTo( new[] { $"[1]: Id '{assetId}' not found." }); } [Fact] public async Task Should_not_add_error_if_asset_are_not_valid_but_in_optimized_mode() { var assetId = Guid.NewGuid(); var sut = Field(new AssetsFieldProperties()); await sut.ValidateAsync(CreateValue(assetId), errors, ctx.Optimized()); Assert.Empty(errors); } [Fact] public async Task Should_add_error_if_document_is_too_small() { var sut = Field(new AssetsFieldProperties { MinSize = 5 * 1024 }); await sut.ValidateAsync(CreateValue(document.AssetId, image1.AssetId), errors, ctx); errors.Should().BeEquivalentTo( new[] { "[1]: \'4 kB\' less than minimum of \'5 kB\'." }); } [Fact] public async Task Should_add_error_if_document_is_too_big() { var sut = Field(new AssetsFieldProperties { MaxSize = 5 * 1024 }); await sut.ValidateAsync(CreateValue(document.AssetId, image1.AssetId), errors, ctx); errors.Should().BeEquivalentTo( new[] { "[2]: \'8 kB\' greater than maximum of \'5 kB\'." }); } [Fact] public async Task Should_add_error_if_document_is_not_an_image() { var sut = Field(new AssetsFieldProperties { MustBeImage = true }); await sut.ValidateAsync(CreateValue(document.AssetId, image1.AssetId), errors, ctx); errors.Should().BeEquivalentTo( new[] { "[1]: Not an image." }); } [Fact] public async Task Should_add_error_if_values_contains_duplicate() { var sut = Field(new AssetsFieldProperties { MustBeImage = true }); await sut.ValidateAsync(CreateValue(image1.AssetId, image1.AssetId), errors, ctx); errors.Should().BeEquivalentTo( new[] { "Must not contain duplicate values." }); } [Fact] public async Task Should_add_error_if_image_width_is_too_small() { var sut = Field(new AssetsFieldProperties { MinWidth = 1000 }); await sut.ValidateAsync(CreateValue(document.AssetId, image1.AssetId), errors, ctx); errors.Should().BeEquivalentTo( new[] { "[2]: Width \'800px\' less than minimum of \'1000px\'." }); } [Fact] public async Task Should_add_error_if_image_width_is_too_big() { var sut = Field(new AssetsFieldProperties { MaxWidth = 700 }); await sut.ValidateAsync(CreateValue(document.AssetId, image1.AssetId), errors, ctx); errors.Should().BeEquivalentTo( new[] { "[2]: Width \'800px\' greater than maximum of \'700px\'." }); } [Fact] public async Task Should_add_error_if_image_height_is_too_small() { var sut = Field(new AssetsFieldProperties { MinHeight = 800 }); await sut.ValidateAsync(CreateValue(document.AssetId, image1.AssetId), errors, ctx); errors.Should().BeEquivalentTo( new[] { "[2]: Height \'600px\' less than minimum of \'800px\'." }); } [Fact] public async Task Should_add_error_if_image_height_is_too_big() { var sut = Field(new AssetsFieldProperties { MaxHeight = 500 }); await sut.ValidateAsync(CreateValue(document.AssetId, image1.AssetId), errors, ctx); errors.Should().BeEquivalentTo( new[] { "[2]: Height \'600px\' greater than maximum of \'500px\'." }); } [Fact] public async Task Should_add_error_if_image_has_invalid_aspect_ratio() { var sut = Field(new AssetsFieldProperties { AspectWidth = 1, AspectHeight = 1 }); await sut.ValidateAsync(CreateValue(document.AssetId, image1.AssetId), errors, ctx); errors.Should().BeEquivalentTo( new[] { "[2]: Aspect ratio not '1:1'." }); } [Fact] public async Task Should_add_error_if_image_has_invalid_extension() { var sut = Field(new AssetsFieldProperties { AllowedExtensions = ReadOnlyCollection.Create("mp4") }); await sut.ValidateAsync(CreateValue(document.AssetId, image1.AssetId), errors, ctx); errors.Should().BeEquivalentTo( new[] { "[1]: Invalid file extension.", "[2]: Invalid file extension." }); } private static IJsonValue CreateValue(params Guid[]? ids) { return ids == null ? JsonValue.Null : JsonValue.Array(ids.Select(x => (object)x.ToString()).ToArray()); } private static RootField Field(AssetsFieldProperties properties) { return Fields.Assets(1, "my-assets", Partitioning.Invariant, properties); } } }