function LegacyValidatorTest::testFileValidateImageResolution

Same name and namespace in other branches
  1. 10 core/modules/file/tests/src/Kernel/LegacyValidatorTest.php \Drupal\Tests\file\Kernel\LegacyValidatorTest::testFileValidateImageResolution()

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

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

File

core/modules/file/tests/src/Kernel/LegacyValidatorTest.php, line 164

Class

LegacyValidatorTest
Tests the functions used to validate uploaded files.

Namespace

Drupal\Tests\file\Kernel

Code

public function testFileValidateImageResolution() {
    // Non-images.
    $this->expectDeprecation('file_validate_image_resolution() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use the \'file.validator\' service instead. See https://www.drupal.org/node/3363700');
    $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 dimensions 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 dimensions.');
    $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());
        // Verify that the file size has been updated after resizing.
        $this->assertEquals($this->image
            ->getSize(), $image->getFileSize());
        // 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.