function ImageFieldValidateTest::testResolution

Same name in other branches
  1. 9 core/modules/image/tests/src/Functional/ImageFieldValidateTest.php \Drupal\Tests\image\Functional\ImageFieldValidateTest::testResolution()
  2. 8.9.x core/modules/image/tests/src/Functional/ImageFieldValidateTest.php \Drupal\Tests\image\Functional\ImageFieldValidateTest::testResolution()
  3. 10 core/modules/image/tests/src/Functional/ImageFieldValidateTest.php \Drupal\Tests\image\Functional\ImageFieldValidateTest::testResolution()

Tests min/max dimensions settings.

File

core/modules/image/tests/src/Functional/ImageFieldValidateTest.php, line 92

Class

ImageFieldValidateTest
Tests validation functions such as min/max dimensions.

Namespace

Drupal\Tests\image\Functional

Code

public function testResolution() : void {
    $field_names = [
        0 => $this->randomMachineName(),
        1 => $this->randomMachineName(),
        2 => $this->randomMachineName(),
    ];
    $min_resolution = [
        'width' => 50,
        'height' => 50,
    ];
    $max_resolution = [
        'width' => 100,
        'height' => 100,
    ];
    $no_height_min_resolution = [
        'width' => 50,
        'height' => NULL,
    ];
    $no_height_max_resolution = [
        'width' => 100,
        'height' => NULL,
    ];
    $no_width_min_resolution = [
        'width' => NULL,
        'height' => 50,
    ];
    $no_width_max_resolution = [
        'width' => NULL,
        'height' => 100,
    ];
    $field_settings = [
        0 => $this->getFieldSettings($min_resolution, $max_resolution),
        1 => $this->getFieldSettings($no_height_min_resolution, $no_height_max_resolution),
        2 => $this->getFieldSettings($no_width_min_resolution, $no_width_max_resolution),
    ];
    $this->createImageField($field_names[0], 'node', 'article', [], $field_settings[0]);
    $this->createImageField($field_names[1], 'node', 'article', [], $field_settings[1]);
    $this->createImageField($field_names[2], 'node', 'article', [], $field_settings[2]);
    // We want a test image that is too small, and a test image that is too
    // big, so cycle through test image files until we have what we need.
    $image_that_is_too_big = FALSE;
    $image_that_is_too_small = FALSE;
    $image_factory = $this->container
        ->get('image.factory');
    foreach ($this->drupalGetTestFiles('image') as $image) {
        $image_file = $image_factory->get($image->uri);
        if ($image_file->getWidth() > $max_resolution['width']) {
            $image_that_is_too_big = $image;
        }
        if ($image_file->getWidth() < $min_resolution['width']) {
            $image_that_is_too_small = $image;
            $image_that_is_too_small_file = $image_file;
        }
        if ($image_that_is_too_small && $image_that_is_too_big) {
            break;
        }
    }
    $this->uploadNodeImage($image_that_is_too_small, $field_names[0], 'article');
    $this->assertSession()
        ->statusMessageContains("The specified file {$image_that_is_too_small->filename} could not be uploaded.", 'error');
    $this->assertSession()
        ->statusMessageContains("The image is too small. The minimum dimensions are 50x50 pixels and the image size is {$image_that_is_too_small_file->getWidth()}x{$image_that_is_too_small_file->getHeight()} pixels.", 'error');
    $this->uploadNodeImage($image_that_is_too_big, $field_names[0], 'article');
    $this->assertSession()
        ->statusMessageContains('The image was resized to fit within the maximum allowed dimensions of 100x100 pixels.', 'status');
    $this->uploadNodeImage($image_that_is_too_small, $field_names[1], 'article');
    $this->assertSession()
        ->statusMessageContains("The specified file {$image_that_is_too_small->filename} could not be uploaded.", 'error');
    $this->uploadNodeImage($image_that_is_too_big, $field_names[1], 'article');
    $this->assertSession()
        ->statusMessageContains('The image was resized to fit within the maximum allowed width of 100 pixels.', 'status');
    $this->uploadNodeImage($image_that_is_too_small, $field_names[2], 'article');
    $this->assertSession()
        ->statusMessageContains("The specified file {$image_that_is_too_small->filename} could not be uploaded.", 'error');
    $this->uploadNodeImage($image_that_is_too_big, $field_names[2], 'article');
    $this->assertSession()
        ->statusMessageContains('The image was resized to fit within the maximum allowed height of 100 pixels.', 'status');
}

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