function ValidatorTest::testFileValidateImageResolution

Same name in other branches
  1. 8.9.x core/modules/file/tests/src/Kernel/ValidatorTest.php \Drupal\Tests\file\Kernel\ValidatorTest::testFileValidateImageResolution()

This ensures the resolution of a specific file is within bounds.

The image will be resized if it's too large.

File

core/modules/file/tests/src/Kernel/ValidatorTest.php, line 159

Class

ValidatorTest
Tests the functions used to validate uploaded files.

Namespace

Drupal\Tests\file\Kernel

Code

public function testFileValidateImageResolution() {
    // Non-images.
    $errors = file_validate_image_resolution($this->nonImage);
    $this->assertCount(0, $errors, 'Should not get any errors for a non-image file.');
    $errors = file_validate_image_resolution($this->nonImage, '50x50', '100x100');
    $this->assertCount(0, $errors, 'Do not check the resolution on non files.');
    // Minimum size.
    $errors = file_validate_image_resolution($this->image);
    $this->assertCount(0, $errors, 'No errors for an image when there is no minimum or maximum resolution.');
    $errors = file_validate_image_resolution($this->image, 0, '200x1');
    $this->assertCount(1, $errors, 'Got an error for an image that was not wide enough.');
    $errors = file_validate_image_resolution($this->image, 0, '1x200');
    $this->assertCount(1, $errors, 'Got an error for an image that was not tall enough.');
    $errors = file_validate_image_resolution($this->image, 0, '200x200');
    $this->assertCount(1, $errors, 'Small images report an error.');
    // Maximum size.
    if ($this->container
        ->get('image.factory')
        ->getToolkitId()) {
        // Copy the image so that the original doesn't get resized.
        copy('core/misc/druplicon.png', 'temporary://druplicon.png');
        $this->image
            ->setFileUri('temporary://druplicon.png');
        $errors = file_validate_image_resolution($this->image, '10x5');
        $this->assertCount(0, $errors, 'No errors should be reported when an oversized image can be scaled down.');
        $image = $this->container
            ->get('image.factory')
            ->get($this->image
            ->getFileUri());
        // Verify that the image was scaled to the correct width and height.
        $this->assertLessThanOrEqual(10, $image->getWidth());
        $this->assertLessThanOrEqual(5, $image->getHeight());
        // Once again, now with negative width and height to force an error.
        copy('core/misc/druplicon.png', 'temporary://druplicon.png');
        $this->image
            ->setFileUri('temporary://druplicon.png');
        $errors = file_validate_image_resolution($this->image, '-10x-5');
        $this->assertCount(1, $errors, 'An error reported for an oversized image that can not be scaled down.');
        \Drupal::service('file_system')->unlink('temporary://druplicon.png');
    }
    else {
        // TODO: should check that the error is returned if no toolkit is available.
        $errors = file_validate_image_resolution($this->image, '5x10');
        $this->assertCount(1, $errors, 'Oversize images that cannot be scaled get an error.');
    }
}

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