function EditorValidationTest::testImageUploadSettingsAreDynamicallyRequired

Same name in other branches
  1. 10 core/modules/editor/tests/src/Kernel/EditorValidationTest.php \Drupal\Tests\editor\Kernel\EditorValidationTest::testImageUploadSettingsAreDynamicallyRequired()

`image_upload.status = TRUE` must cause additional keys to be required.

File

core/modules/editor/tests/src/Kernel/EditorValidationTest.php, line 136

Class

EditorValidationTest
Tests validation of editor entities.

Namespace

Drupal\Tests\editor\Kernel

Code

public function testImageUploadSettingsAreDynamicallyRequired() : void {
    // When image uploads are disabled, no other key-value pairs are needed.
    $this->entity
        ->setImageUploadSettings([
        'status' => FALSE,
    ]);
    $this->assertValidationErrors([]);
    // But when they are enabled, many others are needed.
    $this->entity
        ->setImageUploadSettings([
        'status' => TRUE,
    ]);
    $this->assertValidationErrors([
        'image_upload' => [
            "'scheme' is a required key because image_upload.status is 1 (see config schema type editor.image_upload_settings.1).",
            "'directory' is a required key because image_upload.status is 1 (see config schema type editor.image_upload_settings.1).",
            "'max_size' is a required key because image_upload.status is 1 (see config schema type editor.image_upload_settings.1).",
            "'max_dimensions' is a required key because image_upload.status is 1 (see config schema type editor.image_upload_settings.1).",
        ],
    ]);
    // Specify all required keys, but forget one.
    $this->entity
        ->setImageUploadSettings([
        'status' => TRUE,
        'scheme' => 'public',
        'directory' => 'uploaded-images',
        'max_size' => '5 MB',
    ]);
    $this->assertValidationErrors([
        'image_upload' => "'max_dimensions' is a required key because image_upload.status is 1 (see config schema type editor.image_upload_settings.1).",
    ]);
    // Specify all required keys.
    $this->entity
        ->setImageUploadSettings([
        'status' => TRUE,
        'scheme' => 'public',
        'directory' => 'uploaded-images',
        'max_size' => '5 MB',
        'max_dimensions' => [
            'width' => 10000,
            'height' => 10000,
        ],
    ]);
    $this->assertValidationErrors([]);
    // Specify all required keys … but now disable image uploads again. This
    // should trigger a validation error from the ValidKeys constraint.
    $this->entity
        ->setImageUploadSettings([
        'status' => FALSE,
        'scheme' => 'public',
        'directory' => 'uploaded-images',
        'max_size' => '5 MB',
        'max_dimensions' => [
            'width' => 10000,
            'height' => 10000,
        ],
    ]);
    $this->assertValidationErrors([
        'image_upload' => [
            "'scheme' is an unknown key because image_upload.status is 0 (see config schema type editor.image_upload_settings.*).",
            "'directory' is an unknown key because image_upload.status is 0 (see config schema type editor.image_upload_settings.*).",
            "'max_size' is an unknown key because image_upload.status is 0 (see config schema type editor.image_upload_settings.*).",
            "'max_dimensions' is an unknown key because image_upload.status is 0 (see config schema type editor.image_upload_settings.*).",
        ],
    ]);
    // Remove the values that the messages said are unknown.
    $this->entity
        ->setImageUploadSettings([
        'status' => FALSE,
    ]);
    $this->assertValidationErrors([]);
    // Note how this is the same as the initial value. This proves that `status`
    // being FALSE prevents any meaningless key-value pairs to be present, and
    // `status` being TRUE requires those then meaningful pairs to be present.
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.