From ffd6065a7cc76ff9f37a62ac596f017ff82a3ded Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Wed, 3 Apr 2024 16:13:13 +0100 Subject: [PATCH] Issue #3425870 by phenaproxima, alexpott, Wim Leers: Add validation constraints to image.settings (cherry picked from commit ad80289ed42cebb75ee7b365d466f3148383736e) --- .../Core/Validation/ConstraintManager.php | 6 ++++ core/modules/forum/tests/fixtures/drupal7.php | 2 +- .../image/config/schema/image.schema.yml | 8 +++++ .../Migrate/d7/MigrateImageSettingsTest.php | 2 +- .../Kernel/SettingsConfigValidationTest.php | 34 +++++++++++++++++++ .../migrate_drupal/tests/fixtures/drupal7.php | 2 +- .../statistics/tests/fixtures/drupal7.php | 2 +- .../tracker/tests/fixtures/drupal7.php | 2 +- 8 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 core/modules/image/tests/src/Kernel/SettingsConfigValidationTest.php diff --git a/core/lib/Drupal/Core/Validation/ConstraintManager.php b/core/lib/Drupal/Core/Validation/ConstraintManager.php index 5dc07e61824b..228c8b397e09 100644 --- a/core/lib/Drupal/Core/Validation/ConstraintManager.php +++ b/core/lib/Drupal/Core/Validation/ConstraintManager.php @@ -12,6 +12,7 @@ use Drupal\Core\Validation\Plugin\Validation\Constraint\EmailConstraint; use Symfony\Component\Validator\Constraints\Blank; use Symfony\Component\Validator\Constraints\Callback; use Symfony\Component\Validator\Constraints\Choice; +use Symfony\Component\Validator\Constraints\Image; use Symfony\Component\Validator\Constraints\NotBlank; /** @@ -116,6 +117,11 @@ class ConstraintManager extends DefaultPluginManager { 'class' => Choice::class, 'type' => FALSE, ]); + $this->getDiscovery()->setDefinition('Image', [ + 'label' => new TranslatableMarkup('Image'), + 'class' => Image::class, + 'type' => ['string'], + ]); } /** diff --git a/core/modules/forum/tests/fixtures/drupal7.php b/core/modules/forum/tests/fixtures/drupal7.php index 95f92005c0b1..015d9c406876 100644 --- a/core/modules/forum/tests/fixtures/drupal7.php +++ b/core/modules/forum/tests/fixtures/drupal7.php @@ -30040,7 +30040,7 @@ $connection->insert('variable') )) ->values(array( 'name' => 'image_style_preview_image', - 'value' => 's:33:"core/modules/image/testsample.png";', + 'value' => 's:23:"core/misc/druplicon.png";', )) ->values(array( 'name' => 'image_toolkit', diff --git a/core/modules/image/config/schema/image.schema.yml b/core/modules/image/config/schema/image.schema.yml index bd438deb8229..428ba96648c4 100644 --- a/core/modules/image/config/schema/image.schema.yml +++ b/core/modules/image/config/schema/image.schema.yml @@ -87,10 +87,18 @@ image.effect.image_scale_and_crop: image.settings: type: config_object + constraints: + FullyValidatable: ~ mapping: preview_image: type: string label: 'Preview image' + constraints: + NotBlank: [] + # We need to use Symfony's Image constraint because it will accept a file + # path as a string, whereas the File module's FileIsImage constraint expects + # a full file entity. + Image: [] allow_insecure_derivatives: type: boolean label: 'Allow insecure image derivatives' diff --git a/core/modules/image/tests/src/Kernel/Migrate/d7/MigrateImageSettingsTest.php b/core/modules/image/tests/src/Kernel/Migrate/d7/MigrateImageSettingsTest.php index da458ed2d684..ae7eb12c4e0d 100644 --- a/core/modules/image/tests/src/Kernel/Migrate/d7/MigrateImageSettingsTest.php +++ b/core/modules/image/tests/src/Kernel/Migrate/d7/MigrateImageSettingsTest.php @@ -29,7 +29,7 @@ class MigrateImageSettingsTest extends MigrateDrupal7TestBase { // These settings are not recommended... $this->assertTrue($config->get('allow_insecure_derivatives')); $this->assertTrue($config->get('suppress_itok_output')); - $this->assertSame("core/modules/image/testsample.png", $config->get('preview_image')); + $this->assertSame("core/misc/druplicon.png", $config->get('preview_image')); } } diff --git a/core/modules/image/tests/src/Kernel/SettingsConfigValidationTest.php b/core/modules/image/tests/src/Kernel/SettingsConfigValidationTest.php new file mode 100644 index 000000000000..7c78e68f247b --- /dev/null +++ b/core/modules/image/tests/src/Kernel/SettingsConfigValidationTest.php @@ -0,0 +1,34 @@ +installConfig('image'); + + $file = sys_get_temp_dir() . '/fake_image.png'; + file_put_contents($file, 'Not an image!'); + + $this->expectException(SchemaIncompleteException::class); + $this->expectExceptionMessage('[preview_image] This file is not a valid image.'); + $this->config('image.settings') + ->set('preview_image', $file) + ->save(); + } + +} diff --git a/core/modules/migrate_drupal/tests/fixtures/drupal7.php b/core/modules/migrate_drupal/tests/fixtures/drupal7.php index 6e85c3661908..b5d57779ab80 100644 --- a/core/modules/migrate_drupal/tests/fixtures/drupal7.php +++ b/core/modules/migrate_drupal/tests/fixtures/drupal7.php @@ -65678,7 +65678,7 @@ $connection->insert('variable') )) ->values(array( 'name' => 'image_style_preview_image', - 'value' => 's:33:"core/modules/image/testsample.png";', + 'value' => 's:23:"core/misc/druplicon.png";', )) ->values(array( 'name' => 'image_toolkit', diff --git a/core/modules/statistics/tests/fixtures/drupal7.php b/core/modules/statistics/tests/fixtures/drupal7.php index a4c3130aafac..db3c26ccf688 100644 --- a/core/modules/statistics/tests/fixtures/drupal7.php +++ b/core/modules/statistics/tests/fixtures/drupal7.php @@ -32755,7 +32755,7 @@ $connection->insert('variable') )) ->values(array( 'name' => 'image_style_preview_image', - 'value' => 's:33:"core/modules/image/testsample.png";', + 'value' => 's:23:"core/misc/druplicon.png";', )) ->values(array( 'name' => 'image_toolkit', diff --git a/core/modules/tracker/tests/fixtures/drupal7.php b/core/modules/tracker/tests/fixtures/drupal7.php index 6cf600dc4aca..ea7e7dde333c 100644 --- a/core/modules/tracker/tests/fixtures/drupal7.php +++ b/core/modules/tracker/tests/fixtures/drupal7.php @@ -28210,7 +28210,7 @@ $connection->insert('variable') )) ->values(array( 'name' => 'image_style_preview_image', - 'value' => 's:33:"core/modules/image/testsample.png";', + 'value' => 's:23:"core/misc/druplicon.png";', )) ->values(array( 'name' => 'image_toolkit',