Code cleanup, added unit tests

This commit is contained in:
Benjamin Carleski
2020-02-08 11:05:14 -08:00
parent e7248ea48f
commit 408ee452e1
5 changed files with 343 additions and 170 deletions
@@ -0,0 +1,232 @@
using System;
using System.Globalization;
using Moq;
using Newtonsoft.Json;
using NUnit.Framework;
using Newtonsoft.Json.Linq;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.PropertyEditors.ValueConverters;
using Umbraco.Core.Services;
using Umbraco.Tests.Components;
using Umbraco.Tests.TestHelpers;
using Umbraco.Web.Models;
using Umbraco.Web;
using Umbraco.Web.PropertyEditors;
using System.Text;
namespace Umbraco.Tests.Models
{
[TestFixture]
public class ImageProcessorImageUrlGeneratorTest
{
private const string MediaPath = "/media/1005/img_0671.jpg";
private static readonly ImageUrlGenerationOptions.CropCoordinates Crop = new ImageUrlGenerationOptions.CropCoordinates { X1 = 0.58729977382575338m, Y1 = 0.055768992440203169m, X2 = 0m, Y2 = 0.32457553600198386m };
private static readonly ImageUrlGenerationOptions.FocalPointPosition Focus1 = new ImageUrlGenerationOptions.FocalPointPosition { Top = 0.80827067669172936m, Left = 0.96m };
private static readonly ImageUrlGenerationOptions.FocalPointPosition Focus2 = new ImageUrlGenerationOptions.FocalPointPosition { Top = 0.41m, Left = 0.4275m };
private static readonly ImageProcessorImageUrlGenerator Generator = new ImageProcessorImageUrlGenerator();
[Test]
public void GetCropUrl_CropAliasTest()
{
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, Crop = Crop, Width = 100, Height = 100 });
Assert.AreEqual(MediaPath + "?crop=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&cropmode=percentage&width=100&height=100", urlString);
}
[Test]
public void GetCropUrl_WidthHeightTest()
{
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, FocalPoint = Focus1, Width = 200, Height = 300 });
Assert.AreEqual(MediaPath + "?center=0.80827067669172936,0.96&mode=crop&width=200&height=300", urlString);
}
[Test]
public void GetCropUrl_FocalPointTest()
{
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, FocalPoint = Focus1, Width = 100, Height = 100 });
Assert.AreEqual(MediaPath + "?center=0.80827067669172936,0.96&mode=crop&width=100&height=100", urlString);
}
[Test]
public void GetCropUrlFurtherOptionsTest()
{
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, FocalPoint = Focus1, Width = 200, Height = 300, FurtherOptions = "&filter=comic&roundedcorners=radius-26|bgcolor-fff" });
Assert.AreEqual(MediaPath + "?center=0.80827067669172936,0.96&mode=crop&width=200&height=300&filter=comic&roundedcorners=radius-26|bgcolor-fff", urlString);
}
/// <summary>
/// Test that if a crop alias has been specified that doesn't exist the method returns null
/// </summary>
[Test]
public void GetCropUrlNullTest()
{
var urlString = Generator.GetImageUrl(null);
Assert.AreEqual(null, urlString);
}
/// <summary>
/// Test that if a crop alias has been specified that doesn't exist the method returns null
/// </summary>
[Test]
public void GetCropUrlEmptyTest()
{
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions());
Assert.AreEqual("?mode=crop", urlString);
}
/// <summary>
/// Test the GetCropUrl method on the ImageCropDataSet Model
/// </summary>
[Test]
public void GetBaseCropUrlFromModelTest()
{
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions { Crop = Crop, Width = 100, Height = 100 });
Assert.AreEqual("?crop=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&cropmode=percentage&width=100&height=100", urlString);
}
/// <summary>
/// Test the height ratio mode with predefined crop dimensions
/// </summary>
[Test]
public void GetCropUrl_CropAliasHeightRatioModeTest()
{
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, Crop = Crop, Width = 100, HeightRatio = 1 });
Assert.AreEqual(MediaPath + "?crop=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&cropmode=percentage&heightratio=1&width=100", urlString);
}
/// <summary>
/// Test the height ratio mode with manual width/height dimensions
/// </summary>
[Test]
public void GetCropUrl_WidthHeightRatioModeTest()
{
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, FocalPoint = Focus1, Width = 300, HeightRatio = 0.5m });
Assert.AreEqual(MediaPath + "?center=0.80827067669172936,0.96&mode=crop&heightratio=0.5&width=300", urlString);
}
/// <summary>
/// Test the height ratio mode with width/height dimensions
/// </summary>
[Test]
public void GetCropUrl_HeightWidthRatioModeTest()
{
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, FocalPoint = Focus1, Height = 150, WidthRatio = 2 });
Assert.AreEqual(MediaPath + "?center=0.80827067669172936,0.96&mode=crop&widthratio=2&height=150", urlString);
}
/// <summary>
/// Test that if Crop mode is specified as anything other than Crop the image doesn't use the crop
/// </summary>
[Test]
public void GetCropUrl_SpecifiedCropModeTest()
{
var urlStringMin = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, ImageCropMode = "Min", Width = 300, Height = 150 });
var urlStringBoxPad = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, ImageCropMode = "BoxPad", Width = 300, Height = 150 });
var urlStringPad = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, ImageCropMode = "Pad", Width = 300, Height = 150 });
var urlStringMax = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, ImageCropMode = "Max", Width = 300, Height = 150 });
var urlStringStretch = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, ImageCropMode = "Stretch", Width = 300, Height = 150 });
Assert.AreEqual(MediaPath + "?mode=min&width=300&height=150", urlStringMin);
Assert.AreEqual(MediaPath + "?mode=boxpad&width=300&height=150", urlStringBoxPad);
Assert.AreEqual(MediaPath + "?mode=pad&width=300&height=150", urlStringPad);
Assert.AreEqual(MediaPath + "?mode=max&width=300&height=150", urlStringMax);
Assert.AreEqual(MediaPath + "?mode=stretch&width=300&height=150", urlStringStretch);
}
/// <summary>
/// Test for upload property type
/// </summary>
[Test]
public void GetCropUrl_UploadTypeTest()
{
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, ImageCropMode = "Crop", ImageCropAnchor = "Center", Width = 100, Height = 270 });
Assert.AreEqual(MediaPath + "?mode=crop&anchor=center&width=100&height=270", urlString);
}
/// <summary>
/// Test for preferFocalPoint when focal point is centered
/// </summary>
[Test]
public void GetCropUrl_PreferFocalPointCenter()
{
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, DefaultCrop = true, Width = 300, Height = 150 });
Assert.AreEqual(MediaPath + "?anchor=center&mode=crop&width=300&height=150", urlString);
}
/// <summary>
/// Test to check if height ratio is returned for a predefined crop without coordinates and focal point in centre when a width parameter is passed
/// </summary>
[Test]
public void GetCropUrl_PreDefinedCropNoCoordinatesWithWidth()
{
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, DefaultCrop = true, Width = 200, HeightRatio = 0.5962962962962962962962962963m });
Assert.AreEqual(MediaPath + "?anchor=center&mode=crop&heightratio=0.5962962962962962962962962963&width=200", urlString);
}
/// <summary>
/// Test to check if height ratio is returned for a predefined crop without coordinates and focal point is custom when a width parameter is passed
/// </summary>
[Test]
public void GetCropUrl_PreDefinedCropNoCoordinatesWithWidthAndFocalPoint()
{
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, FocalPoint = Focus2, Width = 200, HeightRatio = 0.5962962962962962962962962963m });
Assert.AreEqual(MediaPath + "?center=0.41,0.4275&mode=crop&heightratio=0.5962962962962962962962962963&width=200", urlString);
}
/// <summary>
/// Test to check if crop ratio is ignored if useCropDimensions is true
/// </summary>
[Test]
public void GetCropUrl_PreDefinedCropNoCoordinatesWithWidthAndFocalPointIgnore()
{
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, FocalPoint = Focus2, Width = 270, Height = 161 });
Assert.AreEqual(MediaPath + "?center=0.41,0.4275&mode=crop&width=270&height=161", urlString);
}
/// <summary>
/// Test to check if width ratio is returned for a predefined crop without coordinates and focal point in centre when a height parameter is passed
/// </summary>
[Test]
public void GetCropUrl_PreDefinedCropNoCoordinatesWithHeight()
{
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, DefaultCrop = true, Height = 200, WidthRatio = 1.6770186335403726708074534161m });
Assert.AreEqual(MediaPath + "?anchor=center&mode=crop&widthratio=1.6770186335403726708074534161&height=200", urlString);
}
/// <summary>
/// Test to check result when only a width parameter is passed, effectivly a resize only
/// </summary>
[Test]
public void GetCropUrl_WidthOnlyParameter()
{
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, DefaultCrop = true, Width = 200 });
Assert.AreEqual(MediaPath + "?anchor=center&mode=crop&width=200", urlString);
}
/// <summary>
/// Test to check result when only a height parameter is passed, effectivly a resize only
/// </summary>
[Test]
public void GetCropUrl_HeightOnlyParameter()
{
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, DefaultCrop = true, Height = 200 });
Assert.AreEqual(MediaPath + "?anchor=center&mode=crop&height=200", urlString);
}
/// <summary>
/// Test to check result when using a background color with padding
/// </summary>
[Test]
public void GetCropUrl_BackgroundColorParameter()
{
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = MediaPath, ImageCropMode = "Pad", Width = 400, Height = 400, FurtherOptions = "&bgcolor=fff" });
Assert.AreEqual(MediaPath + "?mode=pad&width=400&height=400&bgcolor=fff", urlString);
}
}
}
@@ -5,9 +5,7 @@ using Newtonsoft.Json;
using NUnit.Framework;
using Newtonsoft.Json.Linq;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
@@ -112,7 +110,7 @@ namespace Umbraco.Tests.PropertyEditors
public void GetCropUrl_CropAliasTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, cropAlias: "Thumb", useCropDimensions: true);
Assert.AreEqual(MediaPath + "?crop=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&cropmode=percentage&width=100&height=100", urlString);
Assert.AreEqual(MediaPath + "?c=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&w=100&h=100", urlString);
}
/// <summary>
@@ -122,28 +120,28 @@ namespace Umbraco.Tests.PropertyEditors
public void GetCropUrl_CropAliasIgnoreWidthHeightTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, cropAlias: "Thumb", useCropDimensions: true, width: 50, height: 50);
Assert.AreEqual(MediaPath + "?crop=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&cropmode=percentage&width=100&height=100", urlString);
Assert.AreEqual(MediaPath + "?c=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&w=100&h=100", urlString);
}
[Test]
public void GetCropUrl_WidthHeightTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 200, height: 300);
Assert.AreEqual(MediaPath + "?center=0.80827067669172936,0.96&mode=crop&width=200&height=300", urlString);
Assert.AreEqual(MediaPath + "?f=0.80827067669172936x0.96&w=200&h=300", urlString);
}
[Test]
public void GetCropUrl_FocalPointTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, cropAlias: "thumb", preferFocalPoint: true, useCropDimensions: true);
Assert.AreEqual(MediaPath + "?center=0.80827067669172936,0.96&mode=crop&width=100&height=100", urlString);
Assert.AreEqual(MediaPath + "?f=0.80827067669172936x0.96&w=100&h=100", urlString);
}
[Test]
public void GetCropUrlFurtherOptionsTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 200, height: 300, furtherOptions: "&filter=comic&roundedcorners=radius-26|bgcolor-fff");
Assert.AreEqual(MediaPath + "?center=0.80827067669172936,0.96&mode=crop&width=200&height=300&filter=comic&roundedcorners=radius-26|bgcolor-fff", urlString);
Assert.AreEqual(MediaPath + "?f=0.80827067669172936x0.96&w=200&h=300&filter=comic&roundedcorners=radius-26|bgcolor-fff", urlString);
}
/// <summary>
@@ -164,7 +162,7 @@ namespace Umbraco.Tests.PropertyEditors
{
var cropDataSet = CropperJson1.DeserializeImageCropperValue();
var urlString = cropDataSet.GetCropUrl("thumb", new TestImageUrlGenerator());
Assert.AreEqual("?crop=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&cropmode=percentage&width=100&height=100", urlString);
Assert.AreEqual("?c=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&w=100&h=100", urlString);
}
/// <summary>
@@ -174,7 +172,7 @@ namespace Umbraco.Tests.PropertyEditors
public void GetCropUrl_CropAliasHeightRatioModeTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, cropAlias: "Thumb", useCropDimensions: true, ratioMode:ImageCropRatioMode.Height);
Assert.AreEqual(MediaPath + "?crop=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&cropmode=percentage&heightratio=1&width=100", urlString);
Assert.AreEqual(MediaPath + "?c=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&hr=1&w=100", urlString);
}
/// <summary>
@@ -184,7 +182,7 @@ namespace Umbraco.Tests.PropertyEditors
public void GetCropUrl_WidthHeightRatioModeTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 300, height: 150, ratioMode:ImageCropRatioMode.Height);
Assert.AreEqual(MediaPath + "?center=0.80827067669172936,0.96&mode=crop&heightratio=0.5&width=300", urlString);
Assert.AreEqual(MediaPath + "?f=0.80827067669172936x0.96&hr=0.5&w=300", urlString);
}
/// <summary>
@@ -194,7 +192,7 @@ namespace Umbraco.Tests.PropertyEditors
public void GetCropUrl_HeightWidthRatioModeTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 300, height: 150, ratioMode: ImageCropRatioMode.Width);
Assert.AreEqual(MediaPath + "?center=0.80827067669172936,0.96&mode=crop&widthratio=2&height=150", urlString);
Assert.AreEqual(MediaPath + "?f=0.80827067669172936x0.96&wr=2&h=150", urlString);
}
/// <summary>
@@ -209,11 +207,11 @@ namespace Umbraco.Tests.PropertyEditors
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 300, height: 150, imageCropMode:ImageCropMode.Max);
var urlStringStretch = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 300, height: 150, imageCropMode: ImageCropMode.Stretch);
Assert.AreEqual(MediaPath + "?mode=min&width=300&height=150", urlStringMin);
Assert.AreEqual(MediaPath + "?mode=boxpad&width=300&height=150", urlStringBoxPad);
Assert.AreEqual(MediaPath + "?mode=pad&width=300&height=150", urlStringPad);
Assert.AreEqual(MediaPath + "?mode=max&width=300&height=150", urlString);
Assert.AreEqual(MediaPath + "?mode=stretch&width=300&height=150", urlStringStretch);
Assert.AreEqual(MediaPath + "?m=min&w=300&h=150", urlStringMin);
Assert.AreEqual(MediaPath + "?m=boxpad&w=300&h=150", urlStringBoxPad);
Assert.AreEqual(MediaPath + "?m=pad&w=300&h=150", urlStringPad);
Assert.AreEqual(MediaPath + "?m=max&w=300&h=150", urlString);
Assert.AreEqual(MediaPath + "?m=stretch&w=300&h=150", urlStringStretch);
}
/// <summary>
@@ -223,7 +221,7 @@ namespace Umbraco.Tests.PropertyEditors
public void GetCropUrl_UploadTypeTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), width: 100, height: 270, imageCropMode: ImageCropMode.Crop, imageCropAnchor: ImageCropAnchor.Center);
Assert.AreEqual(MediaPath + "?mode=crop&anchor=center&width=100&height=270", urlString);
Assert.AreEqual(MediaPath + "?m=crop&a=center&w=100&h=270", urlString);
}
/// <summary>
@@ -235,7 +233,7 @@ namespace Umbraco.Tests.PropertyEditors
const string cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\":\"thumb\",\"width\": 100,\"height\": 100,\"coordinates\": {\"x1\": 0.58729977382575338,\"y1\": 0.055768992440203169,\"x2\": 0,\"y2\": 0.32457553600198386}}]}";
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: cropperJson, width: 300, height: 150, preferFocalPoint:true);
Assert.AreEqual(MediaPath + "?anchor=center&mode=crop&width=300&height=150", urlString);
Assert.AreEqual(MediaPath + "?m=defaultcrop&w=300&h=150", urlString);
}
/// <summary>
@@ -247,7 +245,7 @@ namespace Umbraco.Tests.PropertyEditors
const string cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}";
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: cropperJson, cropAlias: "home", width: 200);
Assert.AreEqual(MediaPath + "?anchor=center&mode=crop&heightratio=0.5962962962962962962962962963&width=200", urlString);
Assert.AreEqual(MediaPath + "?m=defaultcrop&hr=0.5962962962962962962962962963&w=200", urlString);
}
/// <summary>
@@ -259,7 +257,7 @@ namespace Umbraco.Tests.PropertyEditors
const string cropperJson = "{\"focalPoint\": {\"left\": 0.4275,\"top\": 0.41},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}";
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: cropperJson, cropAlias: "home", width: 200);
Assert.AreEqual(MediaPath + "?center=0.41,0.4275&mode=crop&heightratio=0.5962962962962962962962962963&width=200", urlString);
Assert.AreEqual(MediaPath + "?f=0.41x0.4275&hr=0.5962962962962962962962962963&w=200", urlString);
}
/// <summary>
@@ -271,7 +269,7 @@ namespace Umbraco.Tests.PropertyEditors
const string cropperJson = "{\"focalPoint\": {\"left\": 0.4275,\"top\": 0.41},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}";
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: cropperJson, cropAlias: "home", width: 200, useCropDimensions: true);
Assert.AreEqual(MediaPath + "?center=0.41,0.4275&mode=crop&width=270&height=161", urlString);
Assert.AreEqual(MediaPath + "?f=0.41x0.4275&w=270&h=161", urlString);
}
/// <summary>
@@ -283,7 +281,7 @@ namespace Umbraco.Tests.PropertyEditors
const string cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}";
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: cropperJson, cropAlias: "home", height: 200);
Assert.AreEqual(MediaPath + "?anchor=center&mode=crop&widthratio=1.6770186335403726708074534161&height=200", urlString);
Assert.AreEqual(MediaPath + "?m=defaultcrop&wr=1.6770186335403726708074534161&h=200", urlString);
}
/// <summary>
@@ -295,7 +293,7 @@ namespace Umbraco.Tests.PropertyEditors
const string cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}";
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: cropperJson, width: 200);
Assert.AreEqual(MediaPath + "?anchor=center&mode=crop&width=200", urlString);
Assert.AreEqual(MediaPath + "?m=defaultcrop&w=200", urlString);
}
/// <summary>
@@ -307,7 +305,7 @@ namespace Umbraco.Tests.PropertyEditors
const string cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}";
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: cropperJson, height: 200);
Assert.AreEqual(MediaPath + "?anchor=center&mode=crop&height=200", urlString);
Assert.AreEqual(MediaPath + "?m=defaultcrop&h=200", urlString);
}
/// <summary>
@@ -319,10 +317,10 @@ namespace Umbraco.Tests.PropertyEditors
var cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"" + MediaPath + "\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}";
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), 400, 400, cropperJson, imageCropMode: ImageCropMode.Pad, furtherOptions: "&bgcolor=fff");
Assert.AreEqual(MediaPath + "?mode=pad&width=400&height=400&bgcolor=fff", urlString);
Assert.AreEqual(MediaPath + "?m=pad&w=400&h=400&bgcolor=fff", urlString);
}
private class TestImageUrlGenerator : IImageUrlGenerator
internal class TestImageUrlGenerator : IImageUrlGenerator
{
public string GetImageUrl(ImageUrlGenerationOptions options)
{
@@ -330,42 +328,40 @@ namespace Umbraco.Tests.PropertyEditors
if (options.FocalPoint != null)
{
imageProcessorUrl.Append("?center=");
imageProcessorUrl.Append("?f=");
imageProcessorUrl.Append(options.FocalPoint.Top.ToString(CultureInfo.InvariantCulture));
imageProcessorUrl.Append(",");
imageProcessorUrl.Append("x");
imageProcessorUrl.Append(options.FocalPoint.Left.ToString(CultureInfo.InvariantCulture));
imageProcessorUrl.Append("&mode=crop");
}
else if (options.Crop != null)
{
imageProcessorUrl.Append("?crop=");
imageProcessorUrl.Append("?c=");
imageProcessorUrl.Append(options.Crop.X1.ToString(CultureInfo.InvariantCulture)).Append(",");
imageProcessorUrl.Append(options.Crop.Y1.ToString(CultureInfo.InvariantCulture)).Append(",");
imageProcessorUrl.Append(options.Crop.X2.ToString(CultureInfo.InvariantCulture)).Append(",");
imageProcessorUrl.Append(options.Crop.Y2.ToString(CultureInfo.InvariantCulture));
imageProcessorUrl.Append("&cropmode=percentage");
}
else if (options.DefaultCrop)
{
imageProcessorUrl.Append("?anchor=center&mode=crop");
imageProcessorUrl.Append("?m=defaultcrop");
}
else
{
imageProcessorUrl.Append("?mode=" + options.ImageCropMode.ToString().ToLower());
if (options.ImageCropAnchor != null)imageProcessorUrl.Append("&anchor=" + options.ImageCropAnchor.ToString().ToLower());
imageProcessorUrl.Append("?m=" + options.ImageCropMode.ToString().ToLower());
if (options.ImageCropAnchor != null)imageProcessorUrl.Append("&a=" + options.ImageCropAnchor.ToString().ToLower());
}
var hasFormat = options.FurtherOptions != null && options.FurtherOptions.InvariantContains("&format=");
if (options.Quality != null && hasFormat == false) imageProcessorUrl.Append("&quality=" + options.Quality);
if (options.HeightRatio != null) imageProcessorUrl.Append("&heightratio=" + options.HeightRatio.Value.ToString(CultureInfo.InvariantCulture));
if (options.WidthRatio != null) imageProcessorUrl.Append("&widthratio=" + options.WidthRatio.Value.ToString(CultureInfo.InvariantCulture));
if (options.Width != null) imageProcessorUrl.Append("&width=" + options.Width);
if (options.Height != null) imageProcessorUrl.Append("&height=" + options.Height);
if (options.UpScale == false) imageProcessorUrl.Append("&upscale=false");
if (options.AnimationProcessMode != null) imageProcessorUrl.Append("&animationprocessmode=" + options.AnimationProcessMode);
var hasFormat = options.FurtherOptions != null && options.FurtherOptions.InvariantContains("&f=");
if (options.Quality != null && hasFormat == false) imageProcessorUrl.Append("&q=" + options.Quality);
if (options.HeightRatio != null) imageProcessorUrl.Append("&hr=" + options.HeightRatio.Value.ToString(CultureInfo.InvariantCulture));
if (options.WidthRatio != null) imageProcessorUrl.Append("&wr=" + options.WidthRatio.Value.ToString(CultureInfo.InvariantCulture));
if (options.Width != null) imageProcessorUrl.Append("&w=" + options.Width);
if (options.Height != null) imageProcessorUrl.Append("&h=" + options.Height);
if (options.UpScale == false) imageProcessorUrl.Append("&u=no");
if (options.AnimationProcessMode != null) imageProcessorUrl.Append("&apm=" + options.AnimationProcessMode);
if (options.FurtherOptions != null) imageProcessorUrl.Append(options.FurtherOptions);
if (options.Quality != null && hasFormat) imageProcessorUrl.Append("&quality=" + options.Quality);
if (options.CacheBusterValue != null) imageProcessorUrl.Append("&rnd=").Append(options.CacheBusterValue);
if (options.Quality != null && hasFormat) imageProcessorUrl.Append("&q=" + options.Quality);
if (options.CacheBusterValue != null) imageProcessorUrl.Append("&r=").Append(options.CacheBusterValue);
return imageProcessorUrl.ToString();
}
+1
View File
@@ -140,6 +140,7 @@
<Compile Include="ModelsBuilder\UmbracoApplicationTests.cs" />
<Compile Include="Models\ContentScheduleTests.cs" />
<Compile Include="Models\CultureImpactTests.cs" />
<Compile Include="Models\ImageProcessorImageUrlGeneratorTest.cs" />
<Compile Include="Models\PathValidationTests.cs" />
<Compile Include="Models\VariationTests.cs" />
<Compile Include="Persistence\Mappers\MapperTestBase.cs" />
@@ -308,82 +308,71 @@ namespace Umbraco.Web
ImageCropRatioMode? ratioMode = null,
bool upScale = true)
{
if (string.IsNullOrEmpty(imageUrl) == false)
if (string.IsNullOrEmpty(imageUrl)) return string.Empty;
ImageUrlGenerationOptions options;
if (cropDataSet != null && (imageCropMode == ImageCropMode.Crop || imageCropMode == null))
{
ImageUrlGenerationOptions options;
var crop = cropDataSet.GetCrop(cropAlias);
if (cropDataSet != null && (imageCropMode == ImageCropMode.Crop || imageCropMode == null))
// if a crop was specified, but not found, return null
if (crop == null && !string.IsNullOrWhiteSpace(cropAlias))
return null;
options = cropDataSet.GetCropBaseOptions(imageUrl, crop, string.IsNullOrWhiteSpace(cropAlias), preferFocalPoint);
if (crop != null & useCropDimensions)
{
var crop = cropDataSet.GetCrop(cropAlias);
// if a crop was specified, but not found, return null
if (crop == null && !string.IsNullOrWhiteSpace(cropAlias))
return null;
options = cropDataSet.GetCropBaseOptions(imageUrl, crop, string.IsNullOrWhiteSpace(cropAlias), preferFocalPoint);
if (crop != null & useCropDimensions)
{
width = crop.Width;
height = crop.Height;
}
// If a predefined crop has been specified & there are no coordinates & no ratio mode, but a width parameter has been passed we can get the crop ratio for the height
if (crop != null && string.IsNullOrEmpty(cropAlias) == false && crop.Coordinates == null && ratioMode == null && width != null && height == null)
{
options.HeightRatio = (decimal)crop.Height / crop.Width;
}
// If a predefined crop has been specified & there are no coordinates & no ratio mode, but a height parameter has been passed we can get the crop ratio for the width
if (crop != null && string.IsNullOrEmpty(cropAlias) == false && crop.Coordinates == null && ratioMode == null && width == null && height != null)
{
options.WidthRatio = (decimal)crop.Width / crop.Height;
}
}
else
{
options = new ImageUrlGenerationOptions
{
ImageUrl = imageUrl,
ImageCropMode = (imageCropMode ?? ImageCropMode.Pad).ToString().ToLowerInvariant(),
ImageCropAnchor = imageCropAnchor?.ToString().ToLowerInvariant()
};
width = crop.Width;
height = crop.Height;
}
options.Quality = quality;
options.Width = ratioMode != null && ratioMode.Value == ImageCropRatioMode.Width ? null : width;
options.Height = ratioMode != null && ratioMode.Value == ImageCropRatioMode.Height ? null : height;
if (ratioMode == ImageCropRatioMode.Width && height != null)
// If a predefined crop has been specified & there are no coordinates & no ratio mode, but a width parameter has been passed we can get the crop ratio for the height
if (crop != null && string.IsNullOrEmpty(cropAlias) == false && crop.Coordinates == null && ratioMode == null && width != null && height == null)
{
// if only height specified then assume a square
if (width == null)
{
width = height;
}
options.WidthRatio = (decimal)width / (decimal)height;
options.HeightRatio = (decimal)crop.Height / crop.Width;
}
if (ratioMode == ImageCropRatioMode.Height && width != null)
// If a predefined crop has been specified & there are no coordinates & no ratio mode, but a height parameter has been passed we can get the crop ratio for the width
if (crop != null && string.IsNullOrEmpty(cropAlias) == false && crop.Coordinates == null && ratioMode == null && width == null && height != null)
{
// if only width specified then assume a square
if (height == null)
{
height = width;
}
options.HeightRatio = (decimal)height / (decimal)width;
options.WidthRatio = (decimal)crop.Width / crop.Height;
}
options.UpScale = upScale;
options.FurtherOptions = furtherOptions;
options.CacheBusterValue = cacheBusterValue;
return imageUrlGenerator.GetImageUrl(options);
}
else
{
options = new ImageUrlGenerationOptions
{
ImageUrl = imageUrl,
ImageCropMode = (imageCropMode ?? ImageCropMode.Pad).ToString().ToLowerInvariant(),
ImageCropAnchor = imageCropAnchor?.ToString().ToLowerInvariant()
};
}
return string.Empty;
options.Quality = quality;
options.Width = ratioMode != null && ratioMode.Value == ImageCropRatioMode.Width ? null : width;
options.Height = ratioMode != null && ratioMode.Value == ImageCropRatioMode.Height ? null : height;
if (ratioMode == ImageCropRatioMode.Width && height != null)
{
// if only height specified then assume a square
if (width == null) width = height;
options.WidthRatio = (decimal)width / (decimal)height;
}
if (ratioMode == ImageCropRatioMode.Height && width != null)
{
// if only width specified then assume a square
if (height == null) height = width;
options.HeightRatio = (decimal)height / (decimal)width;
}
options.UpScale = upScale;
options.FurtherOptions = furtherOptions;
options.CacheBusterValue = cacheBusterValue;
return imageUrlGenerator.GetImageUrl(options);
}
}
}
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Globalization;
using System.Text;
using System.Threading.Tasks;
using Umbraco.Core;
using Umbraco.Core.Models;
@@ -13,13 +9,14 @@ namespace Umbraco.Web.Models
{
public string GetImageUrl(ImageUrlGenerationOptions options)
{
if (options == null) return null;
var imageProcessorUrl = new StringBuilder(options.ImageUrl ?? string.Empty);
if (options.FocalPoint != null)
{
imageProcessorUrl.Append("?center=");
imageProcessorUrl.Append(options.FocalPoint.Top.ToString(CultureInfo.InvariantCulture));
imageProcessorUrl.Append(",");
imageProcessorUrl.Append(options.FocalPoint.Top.ToString(CultureInfo.InvariantCulture)).Append(",");
imageProcessorUrl.Append(options.FocalPoint.Left.ToString(CultureInfo.InvariantCulture));
imageProcessorUrl.Append("&mode=crop");
}
@@ -34,73 +31,31 @@ namespace Umbraco.Web.Models
}
else if (options.DefaultCrop)
{
imageProcessorUrl.Append("?anchor=center");
imageProcessorUrl.Append("&mode=crop");
imageProcessorUrl.Append("?anchor=center&mode=crop");
}
else
{
imageProcessorUrl.Append("?mode=" + options.ImageCropMode.ToString().ToLower());
imageProcessorUrl.Append("?mode=").Append((options.ImageCropMode ?? "crop").ToLower());
if (options.ImageCropAnchor != null)
{
imageProcessorUrl.Append("&anchor=" + options.ImageCropAnchor.ToString().ToLower());
}
if (options.ImageCropAnchor != null) imageProcessorUrl.Append("&anchor=").Append(options.ImageCropAnchor.ToLower());
}
var hasFormat = options.FurtherOptions != null && options.FurtherOptions.InvariantContains("&format=");
//Only put quality here, if we don't have a format specified.
//Otherwise we need to put quality at the end to avoid it being overridden by the format.
if (options.Quality != null && hasFormat == false)
{
imageProcessorUrl.Append("&quality=" + options.Quality);
}
if (options.HeightRatio != null)
{
imageProcessorUrl.Append("&heightratio=" + options.HeightRatio.Value.ToString(CultureInfo.InvariantCulture));
}
if (options.WidthRatio != null)
{
imageProcessorUrl.Append("&widthratio=" + options.WidthRatio.Value.ToString(CultureInfo.InvariantCulture));
}
if (options.Width != null)
{
imageProcessorUrl.Append("&width=" + options.Width);
}
if (options.Height != null)
{
imageProcessorUrl.Append("&height=" + options.Height);
}
if (options.UpScale == false)
{
imageProcessorUrl.Append("&upscale=false");
}
if (options.AnimationProcessMode != null)
{
imageProcessorUrl.Append("&animationprocessmode=" + options.AnimationProcessMode);
}
if (options.FurtherOptions != null)
{
imageProcessorUrl.Append(options.FurtherOptions);
}
if (options.Quality != null && hasFormat == false) imageProcessorUrl.Append("&quality=").Append(options.Quality);
if (options.HeightRatio != null) imageProcessorUrl.Append("&heightratio=").Append(options.HeightRatio.Value.ToString(CultureInfo.InvariantCulture));
if (options.WidthRatio != null) imageProcessorUrl.Append("&widthratio=").Append(options.WidthRatio.Value.ToString(CultureInfo.InvariantCulture));
if (options.Width != null) imageProcessorUrl.Append("&width=").Append(options.Width);
if (options.Height != null) imageProcessorUrl.Append("&height=").Append(options.Height);
if (options.UpScale == false) imageProcessorUrl.Append("&upscale=false");
if (options.AnimationProcessMode != null) imageProcessorUrl.Append("&animationprocessmode=").Append(options.AnimationProcessMode);
if (options.FurtherOptions != null) imageProcessorUrl.Append(options.FurtherOptions);
//If furtherOptions contains a format, we need to put the quality after the format.
if (options.Quality != null && hasFormat)
{
imageProcessorUrl.Append("&quality=" + options.Quality);
}
if (options.CacheBusterValue != null)
{
imageProcessorUrl.Append("&rnd=").Append(options.CacheBusterValue);
}
if (options.Quality != null && hasFormat) imageProcessorUrl.Append("&quality=").Append(options.Quality);
if (options.CacheBusterValue != null) imageProcessorUrl.Append("&rnd=").Append(options.CacheBusterValue);
return imageProcessorUrl.ToString();
}