diff --git a/core/modules/image/image.post_update.php b/core/modules/image/image.post_update.php index 04d8c4b7b9..c5db868b75 100644 --- a/core/modules/image/image.post_update.php +++ b/core/modules/image/image.post_update.php @@ -7,6 +7,7 @@ use Drupal\Core\Entity\Entity\EntityViewDisplay; use Drupal\Core\Entity\Entity\EntityFormDisplay; +use Drupal\image\Entity\ImageStyle; /** * Saves the image style dependencies into form and view display entities. @@ -20,3 +21,26 @@ function image_post_update_image_style_dependencies() { $display->save(); } } + +/** + * @addtogroup updates-8.6.0 + * @{ + */ + +/** + * Add 'anchor' setting to 'Scale and crop' effects. + */ +function image_post_update_scale_and_crop_effect_add_anchor() { + foreach (ImageStyle::loadMultiple() as $image_style) { + foreach ($image_style->getEffects() as $effect) { + if ($effect->getPluginId() === 'image_scale_and_crop') { + $image_style->save(); + break; + } + } + } +} + +/** + * @} End of "addtogroup updates-8.6.0". + */ diff --git a/core/modules/image/src/Tests/Update/ScaleAndCropAddAnchorUpdateTest.php b/core/modules/image/src/Tests/Update/ScaleAndCropAddAnchorUpdateTest.php new file mode 100644 index 0000000000..cbef409266 --- /dev/null +++ b/core/modules/image/src/Tests/Update/ScaleAndCropAddAnchorUpdateTest.php @@ -0,0 +1,59 @@ +databaseDumpFiles = [ + __DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.4.0.bare.standard.php.gz', + __DIR__ . '/../../../tests/fixtures/update/test_scale_and_crop_add_anchor.php', + ]; + } + + /** + * Tests that 'anchor' setting is properly added. + */ + public function testImagePostUpdateScaleAndCropEffectAddAnchor() { + // Test that the first effect does not have an 'anchor' setting. + $effect_data = $this->config('image.style.test_scale_and_crop_add_anchor')->get('effects.8c7170c9-5bcc-40f9-8698-f88a8be6d434.data'); + $this->assertFalse(array_key_exists('anchor', $effect_data)); + + // Test that the second effect has an 'anchor' setting. + $effect_data = $this->config('image.style.test_scale_and_crop_add_anchor')->get('effects.a8d83b12-abc6-40c8-9c2f-78a4e421cf97.data'); + $this->assertTrue(array_key_exists('anchor', $effect_data)); + + // Test that the third effect does not have an 'anchor' setting. + $effect_data = $this->config('image.style.test_scale_and_crop_add_anchor')->get('effects.1bffd475-19d0-439a-b6a1-7e5850ce40f9.data'); + $this->assertFalse(array_key_exists('anchor', $effect_data)); + + $this->runUpdates(); + + // Test that the first effect now has an 'anchor' setting. + $effect_data = $this->config('image.style.test_scale_and_crop_add_anchor')->get('effects.8c7170c9-5bcc-40f9-8698-f88a8be6d434.data'); + $this->assertTrue(array_key_exists('anchor', $effect_data)); + $this->assertEquals('center-center', $effect_data['anchor']); + + // Test that the second effect's 'anchor' setting is unchanged. + $effect_data = $this->config('image.style.test_scale_and_crop_add_anchor')->get('effects.a8d83b12-abc6-40c8-9c2f-78a4e421cf97.data'); + $this->assertTrue(array_key_exists('anchor', $effect_data)); + $this->assertEquals('left-top', $effect_data['anchor']); + + // Test that the third effect still does not have an 'anchor' setting. + $effect_data = $this->config('image.style.test_scale_and_crop_add_anchor')->get('effects.1bffd475-19d0-439a-b6a1-7e5850ce40f9.data'); + $this->assertFalse(array_key_exists('anchor', $effect_data)); + } + +} diff --git a/core/modules/image/tests/fixtures/update/image.image_style.test_scale_and_crop_add_anchor.yml b/core/modules/image/tests/fixtures/update/image.image_style.test_scale_and_crop_add_anchor.yml new file mode 100644 index 0000000000..b8dd8db666 --- /dev/null +++ b/core/modules/image/tests/fixtures/update/image.image_style.test_scale_and_crop_add_anchor.yml @@ -0,0 +1,28 @@ +langcode: en +status: true +name: test_scale_and_crop_add_anchor +label: test_scale_and_crop_add_anchor +effects: + 8c7170c9-5bcc-40f9-8698-f88a8be6d434: + uuid: 8c7170c9-5bcc-40f9-8698-f88a8be6d434 + id: image_scale_and_crop + weight: 1 + data: + width: 100 + height: 100 + a8d83b12-abc6-40c8-9c2f-78a4e421cf97: + uuid: a8d83b12-abc6-40c8-9c2f-78a4e421cf97 + id: image_scale_and_crop + weight: 2 + data: + width: 100 + height: 100 + anchor: left-top + 1bffd475-19d0-439a-b6a1-7e5850ce40f9: + uuid: 1bffd475-19d0-439a-b6a1-7e5850ce40f9 + id: image_rotate + weight: 3 + data: + degrees: 180 + bgcolor: '' + random: false diff --git a/core/modules/image/tests/fixtures/update/test_scale_and_crop_add_anchor.php b/core/modules/image/tests/fixtures/update/test_scale_and_crop_add_anchor.php new file mode 100644 index 0000000000..a215f55ea8 --- /dev/null +++ b/core/modules/image/tests/fixtures/update/test_scale_and_crop_add_anchor.php @@ -0,0 +1,19 @@ +insert('config') + ->fields(array( + 'collection' => '', + 'name' => 'image.style.test_scale_and_crop_add_anchor', + 'data' => serialize(Yaml::decode(file_get_contents('core/modules/image/tests/fixtures/update/image.image_style.test_scale_and_crop_add_anchor.yml'))), + )) + ->execute();