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. 10 core/modules/image/tests/src/Functional/ImageFieldValidateTest.php \Drupal\Tests\image\Functional\ImageFieldValidateTest::testResolution()
  3. 11.x core/modules/image/tests/src/Functional/ImageFieldValidateTest.php \Drupal\Tests\image\Functional\ImageFieldValidateTest::testResolution()

Test min/max resolution settings.

File

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

Class

ImageFieldValidateTest
Tests validation functions such as min/max resolution.

Namespace

Drupal\Tests\image\Functional

Code

public function testResolution() {
    $field_names = [
        0 => strtolower($this->randomMachineName()),
        1 => strtolower($this->randomMachineName()),
        2 => strtolower($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], 'article', [], $field_settings[0]);
    $this->createImageField($field_names[1], 'article', [], $field_settings[1]);
    $this->createImageField($field_names[2], '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->assertRaw(t('The specified file %name could not be uploaded.', [
        '%name' => $image_that_is_too_small->filename,
    ]));
    $this->assertRaw(t('The image is too small. The minimum dimensions are %dimensions pixels and the image size is %widthx%height pixels.', [
        '%dimensions' => '50x50',
        '%width' => $image_that_is_too_small_file->getWidth(),
        '%height' => $image_that_is_too_small_file->getHeight(),
    ]));
    $this->uploadNodeImage($image_that_is_too_big, $field_names[0], 'article');
    $this->assertText(t('The image was resized to fit within the maximum allowed dimensions of 100x100 pixels.'));
    $this->uploadNodeImage($image_that_is_too_small, $field_names[1], 'article');
    $this->assertRaw(t('The specified file %name could not be uploaded.', [
        '%name' => $image_that_is_too_small->filename,
    ]));
    $this->uploadNodeImage($image_that_is_too_big, $field_names[1], 'article');
    $this->assertText(t('The image was resized to fit within the maximum allowed width of 100 pixels.'));
    $this->uploadNodeImage($image_that_is_too_small, $field_names[2], 'article');
    $this->assertRaw(t('The specified file %name could not be uploaded.', [
        '%name' => $image_that_is_too_small->filename,
    ]));
    $this->uploadNodeImage($image_that_is_too_big, $field_names[2], 'article');
    $this->assertText(t('The image was resized to fit within the maximum allowed height of 100 pixels.'));
}

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