Issue #3425870 by phenaproxima, alexpott, Wim Leers: Add validation constraints to image.settings

(cherry picked from commit ad80289ed4)
merge-requests/7353/head
Alex Pott 2024-04-03 16:13:13 +01:00
parent 1d03e28a9a
commit ffd6065a7c
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
8 changed files with 53 additions and 5 deletions

View File

@ -12,6 +12,7 @@ use Drupal\Core\Validation\Plugin\Validation\Constraint\EmailConstraint;
use Symfony\Component\Validator\Constraints\Blank; use Symfony\Component\Validator\Constraints\Blank;
use Symfony\Component\Validator\Constraints\Callback; use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Choice; use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\Image;
use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\NotBlank;
/** /**
@ -116,6 +117,11 @@ class ConstraintManager extends DefaultPluginManager {
'class' => Choice::class, 'class' => Choice::class,
'type' => FALSE, 'type' => FALSE,
]); ]);
$this->getDiscovery()->setDefinition('Image', [
'label' => new TranslatableMarkup('Image'),
'class' => Image::class,
'type' => ['string'],
]);
} }
/** /**

View File

@ -30040,7 +30040,7 @@ $connection->insert('variable')
)) ))
->values(array( ->values(array(
'name' => 'image_style_preview_image', 'name' => 'image_style_preview_image',
'value' => 's:33:"core/modules/image/testsample.png";', 'value' => 's:23:"core/misc/druplicon.png";',
)) ))
->values(array( ->values(array(
'name' => 'image_toolkit', 'name' => 'image_toolkit',

View File

@ -87,10 +87,18 @@ image.effect.image_scale_and_crop:
image.settings: image.settings:
type: config_object type: config_object
constraints:
FullyValidatable: ~
mapping: mapping:
preview_image: preview_image:
type: string type: string
label: 'Preview image' 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: allow_insecure_derivatives:
type: boolean type: boolean
label: 'Allow insecure image derivatives' label: 'Allow insecure image derivatives'

View File

@ -29,7 +29,7 @@ class MigrateImageSettingsTest extends MigrateDrupal7TestBase {
// These settings are not recommended... // These settings are not recommended...
$this->assertTrue($config->get('allow_insecure_derivatives')); $this->assertTrue($config->get('allow_insecure_derivatives'));
$this->assertTrue($config->get('suppress_itok_output')); $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'));
} }
} }

View File

@ -0,0 +1,34 @@
<?php
namespace Drupal\Tests\image\Kernel;
use Drupal\Core\Config\Schema\SchemaIncompleteException;
use Drupal\KernelTests\KernelTestBase;
/**
* @group image
*/
class SettingsConfigValidationTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['image'];
/**
* Tests that the preview_image setting must be an existing image file.
*/
public function testPreviewImagePathIsValidated(): void {
$this->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();
}
}

View File

@ -65678,7 +65678,7 @@ $connection->insert('variable')
)) ))
->values(array( ->values(array(
'name' => 'image_style_preview_image', 'name' => 'image_style_preview_image',
'value' => 's:33:"core/modules/image/testsample.png";', 'value' => 's:23:"core/misc/druplicon.png";',
)) ))
->values(array( ->values(array(
'name' => 'image_toolkit', 'name' => 'image_toolkit',

View File

@ -32755,7 +32755,7 @@ $connection->insert('variable')
)) ))
->values(array( ->values(array(
'name' => 'image_style_preview_image', 'name' => 'image_style_preview_image',
'value' => 's:33:"core/modules/image/testsample.png";', 'value' => 's:23:"core/misc/druplicon.png";',
)) ))
->values(array( ->values(array(
'name' => 'image_toolkit', 'name' => 'image_toolkit',

View File

@ -28210,7 +28210,7 @@ $connection->insert('variable')
)) ))
->values(array( ->values(array(
'name' => 'image_style_preview_image', 'name' => 'image_style_preview_image',
'value' => 's:33:"core/modules/image/testsample.png";', 'value' => 's:23:"core/misc/druplicon.png";',
)) ))
->values(array( ->values(array(
'name' => 'image_toolkit', 'name' => 'image_toolkit',