Verified Commit ffd6065a authored by Alex Pott's avatar Alex Pott
Browse files

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

(cherry picked from commit ad80289e)
parent 1d03e28a
Loading
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@
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 @@ public function registerDefinitions() {
      'class' => Choice::class,
      'type' => FALSE,
    ]);
    $this->getDiscovery()->setDefinition('Image', [
      'label' => new TranslatableMarkup('Image'),
      'class' => Image::class,
      'type' => ['string'],
    ]);
  }

  /**
+1 −1
Original line number Diff line number Diff line
@@ -30040,7 +30040,7 @@
))
->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',
+8 −0
Original line number Diff line number Diff line
@@ -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'
+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ public function testMigration() {
    // 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'));
  }

}
+34 −0
Original line number Diff line number Diff line
<?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();
  }

}
Loading