View source
<?php
namespace Drupal\Tests\file\Kernel;
use Drupal\file\Entity\File;
class ValidatorTest extends FileManagedUnitTestBase {
protected $image;
protected $nonImage;
protected function setUp() {
parent::setUp();
$this->image = File::create();
$this->image
->setFileUri('core/misc/druplicon.png');
$file_system = \Drupal::service('file_system');
$this->image
->setFilename($file_system
->basename($this->image
->getFileUri()));
$this->nonImage = File::create();
$this->nonImage
->setFileUri('core/assets/vendor/jquery/jquery.min.js');
$this->nonImage
->setFilename($file_system
->basename($this->nonImage
->getFileUri()));
}
public function testFileValidateExtensions() {
$file = File::create([
'filename' => 'asdf.txt',
]);
$errors = file_validate_extensions($file, 'asdf txt pork');
$this
->assertCount(0, $errors, 'Valid extension accepted.');
$file
->setFilename('asdf.txt');
$errors = file_validate_extensions($file, 'exe png');
$this
->assertCount(1, $errors, 'Invalid extension blocked.');
}
public function testFileValidateIsImage() {
$this
->assertFileExists($this->image
->getFileUri());
$errors = file_validate_is_image($this->image);
$this
->assertCount(0, $errors, 'No error reported for our image file.');
$this
->assertFileExists($this->nonImage
->getFileUri());
$errors = file_validate_is_image($this->nonImage);
$this
->assertCount(1, $errors, 'An error reported for our non-image file.');
}
public function testFileValidateImageResolution() {
$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.');
$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.');
if ($this->container
->get('image.factory')
->getToolkitId()) {
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());
$this
->assertTrue($image
->getWidth() <= 10, 'Image scaled to correct width.', 'File');
$this
->assertTrue($image
->getHeight() <= 5, 'Image scaled to correct height.', 'File');
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 {
$errors = file_validate_image_resolution($this->image, '5x10');
$this
->assertCount(1, $errors, 'Oversize images that cannot be scaled get an error.');
}
}
public function testFileValidateNameLength() {
$file = File::create();
$file
->setFilename(str_repeat('x', 240));
$this
->assertEqual(strlen($file
->getFilename()), 240);
$errors = file_validate_name_length($file);
$this
->assertCount(0, $errors, 'No errors reported for 240 length filename.');
$file
->setFilename(str_repeat('x', 241));
$errors = file_validate_name_length($file);
$this
->assertCount(1, $errors, 'An error reported for 241 length filename.');
$file
->setFilename('');
$errors = file_validate_name_length($file);
$this
->assertCount(1, $errors, 'An error reported for 0 length filename.');
}
public function testFileValidateSize() {
$file = File::create([
'filesize' => 1000,
]);
$errors = file_validate_size($file, 0, 0);
$this
->assertCount(0, $errors, 'No limits means no errors.');
$errors = file_validate_size($file, 1, 0);
$this
->assertCount(1, $errors, 'Error for the file being over the limit.');
$errors = file_validate_size($file, 0, 1);
$this
->assertCount(1, $errors, 'Error for the user being over their limit.');
$errors = file_validate_size($file, 1, 1);
$this
->assertCount(2, $errors, 'Errors for both the file and their limit.');
}
}