private List <Crop> GetAllCropsFromDataTypeServiceAndUpdateCache() { List <Crop> allCrops = new List <Crop>(); //We are bypassing the cache so we have to get everything again from the db. IEnumerable <IDataType> imageCropperDataTypes = GetImageCropperDataTypes(true); foreach (IDataType dataType in imageCropperDataTypes) { ImageCropperConfiguration valueList = (ImageCropperConfiguration)dataType.Configuration; var crops = valueList.Crops ?? Enumerable.Empty <ImageCropperConfiguration.Crop>(); if (crops.Any()) { IEnumerable <Crop> cropsFromDb = crops.Select(x => new Crop() { alias = x.Alias, width = x.Width, height = x.Height }); allCrops.AddRange(cropsFromDb); } } cacheManager.UpdateCache(ImageCropperCacheKeys.AllCrops, allCrops); return(allCrops); }
public void CanConvertImageCropperPropertyEditor(string val1, string val2, bool expected) { try { var container = RegisterFactory.Create(); var composition = new Composition(container, new TypeLoader(), Mock.Of <IProfilingLogger>(), ComponentTests.MockRuntimeState(RuntimeLevel.Run)); composition.WithCollectionBuilder <PropertyValueConverterCollectionBuilder>(); Current.Factory = composition.CreateFactory(); var logger = Mock.Of <ILogger>(); var scheme = Mock.Of <IMediaPathScheme>(); var config = Mock.Of <IContentSection>(); var mediaFileSystem = new MediaFileSystem(Mock.Of <IFileSystem>(), config, scheme, logger); var imageCropperConfiguration = new ImageCropperConfiguration() { Crops = new[] { new ImageCropperConfiguration.Crop() { Alias = "thumb", Width = 100, Height = 100 } } }; var dataTypeService = new TestObjects.TestDataTypeService( new DataType(new ImageCropperPropertyEditor(Mock.Of <ILogger>(), mediaFileSystem, Mock.Of <IContentSection>(), Mock.Of <IDataTypeService>())) { Id = 1, Configuration = imageCropperConfiguration }); var factory = new PublishedContentTypeFactory(Mock.Of <IPublishedModelFactory>(), new PropertyValueConverterCollection(Array.Empty <IPropertyValueConverter>()), dataTypeService); var converter = new ImageCropperValueConverter(); var result = converter.ConvertSourceToIntermediate(null, factory.CreatePropertyType("test", 1), val1, false); // does not use type for conversion var resultShouldMatch = val2.DeserializeImageCropperValue(); if (expected) { Assert.AreEqual(resultShouldMatch, result); } else { Assert.AreNotEqual(resultShouldMatch, result); } } finally { Current.Reset(); } }
public void Get_Media_Url_Resolves_Url_From_Image_Cropper_Property_Editor() { const string expected = "/media/rfeiw584/test.jpg"; var configuration = new ImageCropperConfiguration(); var imageCropperValue = JsonConvert.SerializeObject(new ImageCropperValue { Src = expected }); var umbracoContext = GetUmbracoContext("/"); var publishedContent = CreatePublishedContent(Constants.PropertyEditors.Aliases.ImageCropper, imageCropperValue, configuration); var resolvedUrl = GetPublishedUrlProvider(umbracoContext).GetMediaUrl(publishedContent, UrlMode.Auto); Assert.AreEqual(expected, resolvedUrl); }
/// <summary> /// Applies a configuration. /// </summary> /// <remarks>Ensures that all crops defined in the configuration exists in the value.</remarks> internal void ApplyConfiguration(ImageCropperConfiguration configuration) { // merge the crop values - the alias + width + height comes from // configuration, but each crop can store its own coordinates if (Crops == null) { return; } var configuredCrops = configuration?.Crops; if (configuredCrops == null) { return; } var crops = Crops.ToList(); foreach (var configuredCrop in configuredCrops) { var crop = crops.FirstOrDefault(x => x.Alias == configuredCrop.Alias); if (crop != null) { // found, apply the height & width crop.Width = configuredCrop.Width; crop.Height = configuredCrop.Height; } else { // not found, add crops.Add(new ImageCropperCrop { Alias = configuredCrop.Alias, Width = configuredCrop.Width, Height = configuredCrop.Height }); } } // assume we don't have to remove the crops in value, that // are not part of configuration anymore? Crops = crops; }