Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / file / tests / src / Kernel / ValidatorTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Kernel;
4
5 use Drupal\file\Entity\File;
6
7 /**
8  * Tests the functions used to validate uploaded files.
9  *
10  * @group file
11  */
12 class ValidatorTest extends FileManagedUnitTestBase {
13
14   /**
15    * An image file.
16    *
17    * @var \Drupal\file\FileInterface
18    */
19   protected $image;
20
21   /**
22    * A file which is not an image.
23    *
24    * @var \Drupal\file\Entity\File
25    */
26   protected $nonImage;
27
28   protected function setUp() {
29     parent::setUp();
30
31     $this->image = File::create();
32     $this->image->setFileUri('core/misc/druplicon.png');
33     $this->image->setFilename(drupal_basename($this->image->getFileUri()));
34
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()));
38   }
39
40   /**
41    * Test the file_validate_extensions() function.
42    */
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');
47
48     $file->setFilename('asdf.txt');
49     $errors = file_validate_extensions($file, 'exe png');
50     $this->assertEqual(count($errors), 1, 'Invalid extension blocked.', 'File');
51   }
52
53   /**
54    * This ensures a specific file is actually an image.
55    */
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');
60
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');
64   }
65
66   /**
67    * This ensures the resolution of a specific file is within bounds.
68    *
69    * The image will be resized if it's too large.
70    */
71   public function testFileValidateImageResolution() {
72     // Non-images.
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');
77
78     // Minimum size.
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');
87
88     // Maximum size.
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');
93
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');
96
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');
100
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');
106
107       drupal_unlink('temporary://druplicon.png');
108     }
109     else {
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');
113     }
114   }
115
116   /**
117    * This will ensure the filename length is valid.
118    */
119   public function testFileValidateNameLength() {
120     // Create a new file entity.
121     $file = File::create();
122
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');
128
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');
133
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');
138   }
139
140   /**
141    * Test file_validate_size().
142    */
143   public function testFileValidateSize() {
144     // Create a file with a size of 1000 bytes, and quotas of only 1 byte.
145     $file = File::create(['filesize' => 1000]);
146     $errors = file_validate_size($file, 0, 0);
147     $this->assertEqual(count($errors), 0, 'No limits means no errors.', 'File');
148     $errors = file_validate_size($file, 1, 0);
149     $this->assertEqual(count($errors), 1, 'Error for the file being over the limit.', 'File');
150     $errors = file_validate_size($file, 0, 1);
151     $this->assertEqual(count($errors), 1, 'Error for the user being over their limit.', 'File');
152     $errors = file_validate_size($file, 1, 1);
153     $this->assertEqual(count($errors), 2, 'Errors for both the file and their limit.', 'File');
154   }
155
156 }