GetCropUrl - further code duplication removal and 2 new unit tests

This commit is contained in:
Jeavon Leopold
2014-03-24 16:32:50 +00:00
parent 550166c41b
commit f51a66b728
2 changed files with 28 additions and 33 deletions

View File

@@ -9,15 +9,29 @@ namespace Umbraco.Tests.PropertyEditors
[TestFixture]
public class ImageCropperTest
{
private const string cropperJson = "{\"focalPoint\": {\"left\": 0.96,\"top\": 0.80827067669172936},\"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}}]}";
private const string mediaPath = "/media/1005/img_0671.jpg";
[Test]
public void GetCropUrl_Test()
public void GetCropUrl_CropAliasTest()
{
var cropperJson =
"{\"src\": \"/media/1001/img_0671.jpg\",\"crops\": [{\"alias\": \"Thumb\",\"width\": 100,\"height\": 100,\"coordinates\": {\"x1\": 0.36664230810213955,\"y1\": 0.25767203967761981,\"x2\": 0.48805391012476662,\"y2\": 0.54858958462492169}}],\"focalPoint\": {\"left\": 0.8075,\"top\": 0.8666666666666667}}";
var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, cropAlias: "Thumb", cacheBuster: false, useCropDimensions: true);
Assert.AreEqual(mediaPath + "?crop=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&cropmode=percentage&width=100&height=100", urlString);
}
var urlString = "/media/1001/img_0671.jpg".GetCropUrl(imageCropperValue:cropperJson, cropAlias:"Thumb", width:100, height:100);
[Test]
public void GetCropUrl_WidthHeightTest()
{
var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, width: 200, height: 300, cacheBuster: false);
Assert.AreEqual(mediaPath + "?center=0.80827067669172936,0.96&mode=crop&width=200&height=300", urlString);
}
Assert.AreEqual(urlString, "/media/1001/img_0671.jpg?crop=0.36664230810213955,0.25767203967761981,0.48805391012476662,0.54858958462492169&cropmode=percentage&width=100&height=100");
[Test]
public void GetCropUrl_FocalPointTest()
{
var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, cropAlias: "thumb", useFocalPoint: true, useCropDimensions: true, cacheBuster: false);
Assert.AreEqual(mediaPath + "?center=0.80827067669172936,0.96&mode=crop&width=100&height=100", urlString);
}
}

View File

@@ -18,31 +18,10 @@ namespace Umbraco.Web
return mediaItem.GetCropUrl(Constants.Conventions.Media.File, cropAlias);
}
//this only takes the crop json into account
// this only takes the crop json into account
public static string GetCropUrl(this IPublishedContent mediaItem, string propertyAlias, string cropAlias)
{
mediaItem.HasProperty(propertyAlias);
var property = mediaItem.GetPropertyValue<string>(propertyAlias);
if (string.IsNullOrEmpty(property))
return string.Empty;
if (property.DetectIsJson())
{
var cropDataSet = property.SerializeToCropDataSet();
var cropUrl = cropDataSet.GetCropUrl(cropAlias);
if (cropUrl != null)
{
return cropDataSet.Src + cropUrl;
}
return null;
}
else
{
return property;
}
return mediaItem.GetCropUrl(propertyAlias: propertyAlias, cropAlias: cropAlias, useCropDimensions: true);
}
public static string GetCropUrl(
@@ -55,6 +34,7 @@ namespace Umbraco.Web
string propertyAlias = Constants.Conventions.Media.File,
string cropAlias = null,
bool useFocalPoint = false,
bool useCropDimensions = false,
bool cacheBuster = true,
string furtherOptions = null)
{
@@ -66,7 +46,7 @@ namespace Umbraco.Web
{
imageCropperValue = mediaItem.GetPropertyValue<string>(propertyAlias);
//get the raw value (this will be json)
// get the raw value (this will be json)
var urlValue = mediaItem.GetPropertyValue<string>(propertyAlias);
mediaItemUrl = urlValue.DetectIsJson()
@@ -79,7 +59,7 @@ namespace Umbraco.Web
}
return mediaItemUrl != null
? GetCropUrl(mediaItemUrl, width, height, quality, imageCropMode, imageCropAnchor, imageCropperValue, cropAlias, useFocalPoint, cacheBuster, furtherOptions)
? GetCropUrl(mediaItemUrl, width, height, quality, imageCropMode, imageCropAnchor, imageCropperValue, cropAlias, useFocalPoint, useCropDimensions, cacheBuster, furtherOptions)
: string.Empty;
}
@@ -93,6 +73,7 @@ namespace Umbraco.Web
string imageCropperValue = null,
string cropAlias = null,
bool useFocalPoint = false,
bool useCropDimensions = false,
bool cacheBuster = true,
string furtherOptions = null)
{
@@ -113,7 +94,7 @@ namespace Umbraco.Web
return null;
}
imageResizerUrl.Append(cropDataSet.Src);
imageResizerUrl.Append(cropDataSet.GetCropUrl(cropAlias, false, useFocalPoint, cacheBuster));
imageResizerUrl.Append(cropDataSet.GetCropUrl(cropAlias, useCropDimensions, useFocalPoint, cacheBuster));
}
}
else
@@ -136,12 +117,12 @@ namespace Umbraco.Web
imageResizerUrl.Append("&quality=" + quality);
}
if (width != null)
if (width != null && useCropDimensions == false)
{
imageResizerUrl.Append("&width=" + width);
}
if (height != null)
if (height != null && useCropDimensions == false)
{
imageResizerUrl.Append("&height=" + height);
}