Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Web.Common/Extensions/ImageCropperTemplateCoreExtensionsTests.cs
Paul Johnson 00133e880d Move test projects from src/ to tests/ (#11357)
* Update gitignore

* Move csproj

* Update project references

* Update solutions

* Update build scripts

* Tests used to share editorconfig with projects in src

* Fix broken tests.

* Stop copying around .editorconfig

merged root one with linting

* csharp_style_expression_bodied -> suggestion

* Move StyleCop rulesets to matching directories and update shared build properties

* Remove legacy build files, update NuGet.cofig and solution files

* Restore myget source

* Clean up .gitignore

* Update .gitignore

* Move new test classes to tests after merge

* Gitignore + nuget config

* Move new test

Co-authored-by: Ronald Barendse <ronald@barend.se>
2021-10-18 08:14:04 +01:00

115 lines
4.4 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Collections.Generic;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.Media;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.PropertyEditors.ValueConverters;
using Umbraco.Extensions;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Extensions
{
[TestFixture]
public class ImageCropperTemplateCoreExtensionsTests
{
[Test]
public void GetCropUrl_WithCropSpecifiedButNotFound_ReturnsNull()
{
var imageUrl = "/test.jpg";
Mock<IImageUrlGenerator> imageUrlGenerator = CreateMockImageUrlGenerator();
var result = imageUrl.GetCropUrl(
imageUrlGenerator.Object,
new ImageCropperValue { },
imageCropMode: ImageCropMode.Crop,
cropAlias: "Missing");
Assert.IsNull(result);
}
[Test]
public void GetCropUrl_WithCropSpecifiedAndUsingCropDimensions_CallsImageGeneratorWithCorrectParameters()
{
var imageUrl = "/test.jpg";
Mock<IImageUrlGenerator> imageUrlGenerator = CreateMockImageUrlGenerator();
var result = imageUrl.GetCropUrl(
imageUrlGenerator.Object,
CreateImageCropperValueWithCrops(),
imageCropMode: ImageCropMode.Crop,
cropAlias: "TestCrop",
useCropDimensions: true);
imageUrlGenerator
.Verify(x => x.GetImageUrl(
It.Is<ImageUrlGenerationOptions>(y => y.Width == 100 &&
y.Height == 200)));
}
[Test]
public void GetCropUrl_WithCropSpecifiedAndWidthAndHeightProvided_CallsImageGeneratorWithCorrectParameters()
{
var imageUrl = "/test.jpg";
Mock<IImageUrlGenerator> imageUrlGenerator = CreateMockImageUrlGenerator();
var result = imageUrl.GetCropUrl(
imageUrlGenerator.Object,
CreateImageCropperValueWithCrops(),
imageCropMode: ImageCropMode.Crop,
cropAlias: "TestCrop",
width: 50,
height: 80);
imageUrlGenerator
.Verify(x => x.GetImageUrl(
It.Is<ImageUrlGenerationOptions>(y => y.Width == 50 &&
y.Height == 80)));
}
[Test]
public void GetCropUrl_WithCropSpecifiedAndWidthOnlyProvided_CallsImageGeneratorWithCorrectParameters()
{
var imageUrl = "/test.jpg";
Mock<IImageUrlGenerator> imageUrlGenerator = CreateMockImageUrlGenerator();
var result = imageUrl.GetCropUrl(
imageUrlGenerator.Object,
CreateImageCropperValueWithCrops(),
imageCropMode: ImageCropMode.Crop,
cropAlias: "TestCrop",
width: 50);
imageUrlGenerator
.Verify(x => x.GetImageUrl(
It.Is<ImageUrlGenerationOptions>(y => y.Width == 50 &&
y.Height == 100)));
}
[Test]
public void GetCropUrl_WithCropSpecifiedAndHeightOnlyProvided_CallsImageGeneratorWithCorrectParameters()
{
var imageUrl = "/test.jpg";
Mock<IImageUrlGenerator> imageUrlGenerator = CreateMockImageUrlGenerator();
var result = imageUrl.GetCropUrl(
imageUrlGenerator.Object,
CreateImageCropperValueWithCrops(),
imageCropMode: ImageCropMode.Crop,
cropAlias: "TestCrop",
height: 50);
imageUrlGenerator
.Verify(x => x.GetImageUrl(
It.Is<ImageUrlGenerationOptions>(y => y.Width == 25 &&
y.Height == 50)));
}
private static Mock<IImageUrlGenerator> CreateMockImageUrlGenerator() => new Mock<IImageUrlGenerator>();
private static ImageCropperValue CreateImageCropperValueWithCrops() => new ImageCropperValue
{
Crops = new List<ImageCropperValue.ImageCropperCrop>
{
new ImageCropperValue.ImageCropperCrop { Alias = "TestCrop", Width = 100, Height = 200 },
}
};
}
}