3 namespace Drupal\Tests\file\Kernel;
5 use Drupal\file\Entity\File;
8 * Tests the functions used to validate uploaded files.
12 class ValidatorTest extends FileManagedUnitTestBase {
17 * @var \Drupal\file\FileInterface
22 * A file which is not an image.
24 * @var \Drupal\file\Entity\File
28 protected function setUp() {
31 $this->image = File::create();
32 $this->image->setFileUri('core/misc/druplicon.png');
33 $this->image->setFilename(drupal_basename($this->image->getFileUri()));
35 $this->nonImage = File::create();
36 $this->nonImage->setFileUri('core/assets/vendor/jquery/jquery.min.js');
37 $this->nonImage->setFilename(drupal_basename($this->nonImage->getFileUri()));
41 * Test the file_validate_extensions() function.
43 public function testFileValidateExtensions() {
44 $file = File::create(['filename' => 'asdf.txt']);
45 $errors = file_validate_extensions($file, 'asdf txt pork');
46 $this->assertEqual(count($errors), 0, 'Valid extension accepted.', 'File');
48 $file->setFilename('asdf.txt');
49 $errors = file_validate_extensions($file, 'exe png');
50 $this->assertEqual(count($errors), 1, 'Invalid extension blocked.', 'File');
54 * This ensures a specific file is actually an image.
56 public function testFileValidateIsImage() {
57 $this->assertTrue(file_exists($this->image->getFileUri()), 'The image being tested exists.', 'File');
58 $errors = file_validate_is_image($this->image);
59 $this->assertEqual(count($errors), 0, 'No error reported for our image file.', 'File');
61 $this->assertTrue(file_exists($this->nonImage->getFileUri()), 'The non-image being tested exists.', 'File');
62 $errors = file_validate_is_image($this->nonImage);
63 $this->assertEqual(count($errors), 1, 'An error reported for our non-image file.', 'File');
67 * This ensures the resolution of a specific file is within bounds.
69 * The image will be resized if it's too large.
71 public function testFileValidateImageResolution() {
73 $errors = file_validate_image_resolution($this->nonImage);
74 $this->assertEqual(count($errors), 0, 'Should not get any errors for a non-image file.', 'File');
75 $errors = file_validate_image_resolution($this->nonImage, '50x50', '100x100');
76 $this->assertEqual(count($errors), 0, 'Do not check the resolution on non files.', 'File');
79 $errors = file_validate_image_resolution($this->image);
80 $this->assertEqual(count($errors), 0, 'No errors for an image when there is no minimum or maximum resolution.', 'File');
81 $errors = file_validate_image_resolution($this->image, 0, '200x1');
82 $this->assertEqual(count($errors), 1, 'Got an error for an image that was not wide enough.', 'File');
83 $errors = file_validate_image_resolution($this->image, 0, '1x200');
84 $this->assertEqual(count($errors), 1, 'Got an error for an image that was not tall enough.', 'File');
85 $errors = file_validate_image_resolution($this->image, 0, '200x200');
86 $this->assertEqual(count($errors), 1, 'Small images report an error.', 'File');
89 if ($this->container->get('image.factory')->getToolkitId()) {
90 // Copy the image so that the original doesn't get resized.
91 copy('core/misc/druplicon.png', 'temporary://druplicon.png');
92 $this->image->setFileUri('temporary://druplicon.png');
94 $errors = file_validate_image_resolution($this->image, '10x5');
95 $this->assertEqual(count($errors), 0, 'No errors should be reported when an oversized image can be scaled down.', 'File');
97 $image = $this->container->get('image.factory')->get($this->image->getFileUri());
98 $this->assertTrue($image->getWidth() <= 10, 'Image scaled to correct width.', 'File');
99 $this->assertTrue($image->getHeight() <= 5, 'Image scaled to correct height.', 'File');
101 // Once again, now with negative width and height to force an error.
102 copy('core/misc/druplicon.png', 'temporary://druplicon.png');
103 $this->image->setFileUri('temporary://druplicon.png');
104 $errors = file_validate_image_resolution($this->image, '-10x-5');
105 $this->assertEqual(count($errors), 1, 'An error reported for an oversized image that can not be scaled down.', 'File');
107 drupal_unlink('temporary://druplicon.png');
110 // TODO: should check that the error is returned if no toolkit is available.
111 $errors = file_validate_image_resolution($this->image, '5x10');
112 $this->assertEqual(count($errors), 1, 'Oversize images that cannot be scaled get an error.', 'File');
117 * This will ensure the filename length is valid.
119 public function testFileValidateNameLength() {
120 // Create a new file entity.
121 $file = File::create();
123 // Add a filename with an allowed length and test it.
124 $file->setFilename(str_repeat('x', 240));
125 $this->assertEqual(strlen($file->getFilename()), 240);
126 $errors = file_validate_name_length($file);
127 $this->assertEqual(count($errors), 0, 'No errors reported for 240 length filename.', 'File');
129 // Add a filename with a length too long and test it.
130 $file->setFilename(str_repeat('x', 241));
131 $errors = file_validate_name_length($file);
132 $this->assertEqual(count($errors), 1, 'An error reported for 241 length filename.', 'File');
134 // Add a filename with an empty string and test it.
135 $file->setFilename('');
136 $errors = file_validate_name_length($file);
137 $this->assertEqual(count($errors), 1, 'An error reported for 0 length filename.', 'File');
142 * Test file_validate_size().
144 public function testFileValidateSize() {
145 // Create a file with a size of 1000 bytes, and quotas of only 1 byte.
146 $file = File::create(['filesize' => 1000]);
147 $errors = file_validate_size($file, 0, 0);
148 $this->assertEqual(count($errors), 0, 'No limits means no errors.', 'File');
149 $errors = file_validate_size($file, 1, 0);
150 $this->assertEqual(count($errors), 1, 'Error for the file being over the limit.', 'File');
151 $errors = file_validate_size($file, 0, 1);
152 $this->assertEqual(count($errors), 1, 'Error for the user being over their limit.', 'File');
153 $errors = file_validate_size($file, 1, 1);
154 $this->assertEqual(count($errors), 2, 'Errors for both the file and their limit.', 'File');