2020-12-20 08:36:11 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
using NUnit.Framework;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Models;
|
2022-09-27 14:22:34 +02:00
|
|
|
using Umbraco.Cms.Imaging.ImageSharp.Media;
|
2020-02-08 11:05:14 -08:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Media;
|
|
|
|
|
|
2023-03-23 10:38:18 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Contains tests for all parameters for image generation options.
|
|
|
|
|
/// </summary>
|
2022-06-21 08:09:38 +02:00
|
|
|
[TestFixture]
|
|
|
|
|
public class ImageSharpImageUrlGeneratorTests
|
2020-02-08 11:05:14 -08:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
private const string MediaPath = "/media/1005/img_0671.jpg";
|
|
|
|
|
|
2023-03-23 10:38:18 +01:00
|
|
|
private static readonly ImageUrlGenerationOptions.CropCoordinates _sCrop = new(0.58729977382575338m, 0.055768992440203169m, 0m, 0.32457553600198386m);
|
|
|
|
|
private static readonly ImageUrlGenerationOptions.FocalPointPosition _sFocus = new(0.96m, 0.80827067669172936m);
|
|
|
|
|
private static readonly ImageSharpImageUrlGenerator _sGenerator = new(Array.Empty<string>());
|
2020-02-08 11:05:14 -08:00
|
|
|
|
2023-03-23 10:38:18 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Tests that the media path is returned if no options are provided.
|
|
|
|
|
/// </summary>
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
2023-03-23 10:38:18 +01:00
|
|
|
public void GivenMediaPath_AndNoOptions_ReturnsMediaPath()
|
2022-06-21 08:09:38 +02:00
|
|
|
{
|
2023-03-23 10:38:18 +01:00
|
|
|
var actual = _sGenerator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath));
|
|
|
|
|
Assert.AreEqual(MediaPath, actual);
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2020-02-08 11:05:14 -08:00
|
|
|
|
2023-03-23 10:38:18 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Test that if options is null, the generated image URL is also null.
|
|
|
|
|
/// </summary>
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
2023-03-23 10:38:18 +01:00
|
|
|
public void GivenNullOptions_ReturnsNull()
|
2022-06-21 08:09:38 +02:00
|
|
|
{
|
2023-03-23 10:38:18 +01:00
|
|
|
var actual = _sGenerator.GetImageUrl(null);
|
|
|
|
|
Assert.IsNull(actual);
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2020-02-08 11:05:14 -08:00
|
|
|
|
2023-03-23 10:38:18 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Test that if a null image url is given, null is returned.
|
|
|
|
|
/// </summary>
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
2023-03-23 10:38:18 +01:00
|
|
|
public void GivenNullImageUrl_ReturnsNull()
|
2022-06-21 08:09:38 +02:00
|
|
|
{
|
2023-03-23 10:38:18 +01:00
|
|
|
var actual = _sGenerator.GetImageUrl(new ImageUrlGenerationOptions(null));
|
|
|
|
|
Assert.IsNull(actual);
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-10 13:56:29 +01:00
|
|
|
[Test]
|
|
|
|
|
public void GetImageUrlFurtherOptionsModeAndQualityTest()
|
|
|
|
|
{
|
2023-03-23 10:38:18 +01:00
|
|
|
var urlString = _sGenerator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath)
|
2022-11-10 13:56:29 +01:00
|
|
|
{
|
|
|
|
|
Quality = 10,
|
|
|
|
|
FurtherOptions = "format=webp",
|
|
|
|
|
});
|
|
|
|
|
Assert.AreEqual(
|
|
|
|
|
MediaPath +
|
|
|
|
|
"?format=webp&quality=10",
|
|
|
|
|
urlString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void GetImageUrlFurtherOptionsWithModeAndQualityTest()
|
|
|
|
|
{
|
2023-03-23 10:38:18 +01:00
|
|
|
var urlString = _sGenerator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath)
|
2022-11-10 13:56:29 +01:00
|
|
|
{
|
|
|
|
|
FurtherOptions = "quality=10&format=webp",
|
|
|
|
|
});
|
|
|
|
|
Assert.AreEqual(
|
|
|
|
|
MediaPath +
|
|
|
|
|
"?format=webp&quality=10",
|
|
|
|
|
urlString);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
/// <summary>
|
2023-03-23 10:38:18 +01:00
|
|
|
/// Test that if an empty string image url is given, null is returned.
|
2022-06-21 08:09:38 +02:00
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
2023-03-23 10:38:18 +01:00
|
|
|
public void GivenEmptyStringImageUrl_ReturnsEmptyString()
|
2022-06-21 08:09:38 +02:00
|
|
|
{
|
2023-03-23 10:38:18 +01:00
|
|
|
var actual = _sGenerator.GetImageUrl(new ImageUrlGenerationOptions(string.Empty));
|
|
|
|
|
Assert.AreEqual(actual, string.Empty);
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-03-23 10:38:18 +01:00
|
|
|
/// Tests the correct query string is returned when given a crop.
|
2022-06-21 08:09:38 +02:00
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
2023-03-23 10:38:18 +01:00
|
|
|
public void GivenCrop_ReturnsExpectedQueryString()
|
2022-06-21 08:09:38 +02:00
|
|
|
{
|
2023-03-23 10:38:18 +01:00
|
|
|
const string expected = "?cc=0.58729977382575338,0.055768992440203169,0,0.32457553600198386";
|
|
|
|
|
var actual = _sGenerator.GetImageUrl(new ImageUrlGenerationOptions(string.Empty) { Crop = _sCrop });
|
|
|
|
|
Assert.AreEqual(expected, actual);
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-03-23 10:38:18 +01:00
|
|
|
/// Tests the correct query string is returned when given a width.
|
2022-06-21 08:09:38 +02:00
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
2023-03-23 10:38:18 +01:00
|
|
|
public void GivenWidth_ReturnsExpectedQueryString()
|
2022-06-21 08:09:38 +02:00
|
|
|
{
|
2023-03-23 10:38:18 +01:00
|
|
|
const string expected = "?width=200";
|
|
|
|
|
var actual = _sGenerator.GetImageUrl(new ImageUrlGenerationOptions(string.Empty) { Width = 200 });
|
|
|
|
|
Assert.AreEqual(expected, actual);
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-03-23 10:38:18 +01:00
|
|
|
/// Tests the correct query string is returned when given a height.
|
2022-06-21 08:09:38 +02:00
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
2023-03-23 10:38:18 +01:00
|
|
|
public void GivenHeight_ReturnsExpectedQueryString()
|
2022-06-21 08:09:38 +02:00
|
|
|
{
|
2023-03-23 10:38:18 +01:00
|
|
|
const string expected = "?height=200";
|
|
|
|
|
var actual = _sGenerator.GetImageUrl(new ImageUrlGenerationOptions(string.Empty) { Height = 200 });
|
|
|
|
|
Assert.AreEqual(expected, actual);
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-03-23 10:38:18 +01:00
|
|
|
/// Tests the correct query string is returned when provided a focal point.
|
2022-06-21 08:09:38 +02:00
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
2023-03-23 10:38:18 +01:00
|
|
|
public void GivenFocalPoint_ReturnsExpectedQueryString()
|
2022-06-21 08:09:38 +02:00
|
|
|
{
|
2023-03-23 10:38:18 +01:00
|
|
|
const string expected = "?rxy=0.96,0.80827067669172936";
|
|
|
|
|
var actual = _sGenerator.GetImageUrl(new ImageUrlGenerationOptions(string.Empty) { FocalPoint = _sFocus });
|
|
|
|
|
Assert.AreEqual(expected, actual);
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-03-23 10:38:18 +01:00
|
|
|
/// Tests the correct query string is returned when given further options.
|
|
|
|
|
/// There are a few edge case inputs here to ensure thorough testing in future versions.
|
2022-06-21 08:09:38 +02:00
|
|
|
/// </summary>
|
2023-03-23 10:38:18 +01:00
|
|
|
[TestCase("&filter=comic&roundedcorners=radius-26%7Cbgcolor-fff", "?filter=comic&roundedcorners=radius-26%7Cbgcolor-fff")]
|
|
|
|
|
[TestCase("testoptions", "?testoptions=")]
|
|
|
|
|
[TestCase("&&&should=strip", "?should=strip")]
|
|
|
|
|
[TestCase("should=encode&$^%()", "?should=encode&$%5E%25()=")]
|
|
|
|
|
public void GivenFurtherOptions_ReturnsExpectedQueryString(string input, string expected)
|
2022-06-21 08:09:38 +02:00
|
|
|
{
|
2023-03-23 10:38:18 +01:00
|
|
|
var actual = _sGenerator.GetImageUrl(new ImageUrlGenerationOptions(string.Empty)
|
2020-02-08 11:05:14 -08:00
|
|
|
{
|
2023-03-23 10:38:18 +01:00
|
|
|
FurtherOptions = input,
|
2022-06-21 08:09:38 +02:00
|
|
|
});
|
2023-03-23 10:38:18 +01:00
|
|
|
Assert.AreEqual(expected, actual);
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-03-23 10:38:18 +01:00
|
|
|
/// Test that the correct query string is returned for all image crop modes.
|
2022-06-21 08:09:38 +02:00
|
|
|
/// </summary>
|
2023-03-23 10:38:18 +01:00
|
|
|
[TestCase(ImageCropMode.Min, "?rmode=min")]
|
|
|
|
|
[TestCase(ImageCropMode.BoxPad, "?rmode=boxpad")]
|
|
|
|
|
[TestCase(ImageCropMode.Pad, "?rmode=pad")]
|
|
|
|
|
[TestCase(ImageCropMode.Max, "?rmode=max")]
|
|
|
|
|
[TestCase(ImageCropMode.Stretch, "?rmode=stretch")]
|
|
|
|
|
public void GivenCropMode_ReturnsExpectedQueryString(ImageCropMode cropMode, string expectedQueryString)
|
|
|
|
|
{
|
|
|
|
|
var cropUrl = _sGenerator.GetImageUrl(new ImageUrlGenerationOptions(string.Empty)
|
|
|
|
|
{
|
|
|
|
|
ImageCropMode = cropMode,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(expectedQueryString, cropUrl);
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-03-23 10:38:18 +01:00
|
|
|
/// Test that the correct query string is returned for all image crop anchors.
|
2022-06-21 08:09:38 +02:00
|
|
|
/// </summary>
|
2023-03-23 10:38:18 +01:00
|
|
|
[TestCase(ImageCropAnchor.Bottom, "?ranchor=bottom")]
|
|
|
|
|
[TestCase(ImageCropAnchor.BottomLeft, "?ranchor=bottomleft")]
|
|
|
|
|
[TestCase(ImageCropAnchor.BottomRight, "?ranchor=bottomright")]
|
|
|
|
|
[TestCase(ImageCropAnchor.Center, "?ranchor=center")]
|
|
|
|
|
[TestCase(ImageCropAnchor.Left, "?ranchor=left")]
|
|
|
|
|
[TestCase(ImageCropAnchor.Right, "?ranchor=right")]
|
|
|
|
|
[TestCase(ImageCropAnchor.Top, "?ranchor=top")]
|
|
|
|
|
[TestCase(ImageCropAnchor.TopLeft, "?ranchor=topleft")]
|
|
|
|
|
[TestCase(ImageCropAnchor.TopRight, "?ranchor=topright")]
|
|
|
|
|
public void GivenCropAnchor_ReturnsExpectedQueryString(ImageCropAnchor imageCropAnchor, string expectedQueryString)
|
|
|
|
|
{
|
|
|
|
|
var actual = _sGenerator.GetImageUrl(new ImageUrlGenerationOptions(string.Empty)
|
|
|
|
|
{
|
|
|
|
|
ImageCropAnchor = imageCropAnchor,
|
|
|
|
|
});
|
|
|
|
|
Assert.AreEqual(expectedQueryString, actual);
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-03-23 10:38:18 +01:00
|
|
|
/// Tests that the quality query string always returns the input number regardless of value.
|
2022-06-21 08:09:38 +02:00
|
|
|
/// </summary>
|
2023-03-23 10:38:18 +01:00
|
|
|
[TestCase(int.MinValue)]
|
|
|
|
|
[TestCase(-50)]
|
|
|
|
|
[TestCase(0)]
|
|
|
|
|
[TestCase(50)]
|
|
|
|
|
[TestCase(int.MaxValue)]
|
|
|
|
|
public void GivenQuality_ReturnsExpectedQueryString(int quality)
|
|
|
|
|
{
|
|
|
|
|
var expected = "?quality=" + quality;
|
|
|
|
|
var actual = _sGenerator.GetImageUrl(new ImageUrlGenerationOptions(string.Empty)
|
|
|
|
|
{
|
|
|
|
|
Quality = quality,
|
|
|
|
|
});
|
|
|
|
|
Assert.AreEqual(expected, actual);
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-03-23 10:38:18 +01:00
|
|
|
/// Tests that the correct query string is returned for cache buster.
|
|
|
|
|
/// There are some edge case tests here to ensure thorough testing in future versions.
|
2022-06-21 08:09:38 +02:00
|
|
|
/// </summary>
|
2023-03-23 10:38:18 +01:00
|
|
|
[TestCase("test-buster", "?rnd=test-buster")]
|
|
|
|
|
[TestCase("test-buster&&^-value", "?rnd=test-buster%26%26%5E-value")]
|
|
|
|
|
public void GivenCacheBusterValue_ReturnsExpectedQueryString(string input, string expected)
|
2022-06-21 08:09:38 +02:00
|
|
|
{
|
2023-03-23 10:38:18 +01:00
|
|
|
var actual = _sGenerator.GetImageUrl(new ImageUrlGenerationOptions(string.Empty)
|
|
|
|
|
{
|
|
|
|
|
CacheBusterValue = input,
|
|
|
|
|
});
|
|
|
|
|
Assert.AreEqual(expected, actual);
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-03-23 10:38:18 +01:00
|
|
|
/// Tests that an expected query string is returned when all options are given.
|
|
|
|
|
/// This will be a good test to see if something breaks with ordering of query string parameters.
|
2022-06-21 08:09:38 +02:00
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
2023-03-23 10:38:18 +01:00
|
|
|
public void GivenAllOptions_ReturnsExpectedQueryString()
|
2022-06-21 08:09:38 +02:00
|
|
|
{
|
2023-03-23 10:38:18 +01:00
|
|
|
const string expected =
|
|
|
|
|
"/media/1005/img_0671.jpg?cc=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&rxy=0.96,0.80827067669172936&rmode=stretch&ranchor=right&width=200&height=200&quality=50&more=options&rnd=buster";
|
|
|
|
|
|
|
|
|
|
var actual = _sGenerator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath)
|
2020-02-08 11:05:14 -08:00
|
|
|
{
|
2023-03-23 10:38:18 +01:00
|
|
|
Quality = 50,
|
|
|
|
|
Crop = _sCrop,
|
|
|
|
|
FocalPoint = _sFocus,
|
|
|
|
|
CacheBusterValue = "buster",
|
|
|
|
|
FurtherOptions = "more=options",
|
|
|
|
|
Height = 200,
|
|
|
|
|
Width = 200,
|
|
|
|
|
ImageCropAnchor = ImageCropAnchor.Right,
|
|
|
|
|
ImageCropMode = ImageCropMode.Stretch,
|
2022-06-21 08:09:38 +02:00
|
|
|
});
|
2023-03-23 10:38:18 +01:00
|
|
|
|
|
|
|
|
Assert.AreEqual(expected, actual);
|
2020-02-08 11:05:14 -08:00
|
|
|
}
|
|
|
|
|
}
|