PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/web/core/modules/file/tests/src/Kernel/ValidatorTest.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 242 lines | 152 code | 27 blank | 63 comment | 2 complexity | 95c71ec9a883a7d913809b6c1bcc1da2 MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\file\Kernel;
  3. use Drupal\file\Entity\File;
  4. /**
  5. * Tests the functions used to validate uploaded files.
  6. *
  7. * @group file
  8. */
  9. class ValidatorTest extends FileManagedUnitTestBase {
  10. /**
  11. * An image file.
  12. *
  13. * @var \Drupal\file\FileInterface
  14. */
  15. protected $image;
  16. /**
  17. * A file which is not an image.
  18. *
  19. * @var \Drupal\file\Entity\File
  20. */
  21. protected $nonImage;
  22. protected function setUp(): void {
  23. parent::setUp();
  24. $this->image = File::create();
  25. $this->image->setFileUri('core/misc/druplicon.png');
  26. /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  27. $file_system = \Drupal::service('file_system');
  28. $this->image->setFilename($file_system->basename($this->image->getFileUri()));
  29. $this->nonImage = File::create();
  30. $this->nonImage->setFileUri('core/assets/vendor/jquery/jquery.min.js');
  31. $this->nonImage->setFilename($file_system->basename($this->nonImage->getFileUri()));
  32. }
  33. /**
  34. * Tests the file_validate_extensions() function.
  35. */
  36. public function testFileValidateExtensions() {
  37. $file = File::create(['filename' => 'asdf.txt']);
  38. $errors = file_validate_extensions($file, 'asdf txt pork');
  39. $this->assertCount(0, $errors, 'Valid extension accepted.');
  40. $file->setFilename('asdf.txt');
  41. $errors = file_validate_extensions($file, 'exe png');
  42. $this->assertCount(1, $errors, 'Invalid extension blocked.');
  43. }
  44. /**
  45. * Tests the file_validate_extensions() function.
  46. *
  47. * @param array $file_properties
  48. * The properties of the file being validated.
  49. * @param string[] $extensions
  50. * An array of the allowed file extensions.
  51. * @param string[] $expected_errors
  52. * The expected error messages as string.
  53. *
  54. * @dataProvider providerTestFileValidateExtensionsOnUri
  55. */
  56. public function testFileValidateExtensionsOnUri(array $file_properties, array $extensions, array $expected_errors) {
  57. $file = File::create($file_properties);
  58. $actual_errors = file_validate_extensions($file, implode(' ', $extensions));
  59. $actual_errors_as_string = array_map(function ($error_message) {
  60. return (string) $error_message;
  61. }, $actual_errors);
  62. $this->assertEquals($expected_errors, $actual_errors_as_string);
  63. }
  64. /**
  65. * Data provider for ::testFileValidateExtensionsOnUri.
  66. *
  67. * @return array[][]
  68. * The test cases.
  69. */
  70. public function providerTestFileValidateExtensionsOnUri(): array {
  71. $temporary_txt_file_properties = [
  72. 'filename' => 'asdf.txt',
  73. 'uri' => 'temporary://asdf',
  74. 'status' => 0,
  75. ];
  76. $permanent_txt_file_properties = [
  77. 'filename' => 'asdf.txt',
  78. 'uri' => 'public://asdf_0.txt',
  79. 'status' => 1,
  80. ];
  81. $permanent_png_file_properties = [
  82. 'filename' => 'The Druplicon',
  83. 'uri' => 'public://druplicon.png',
  84. 'status' => 1,
  85. ];
  86. return [
  87. 'Temporary txt validated with "asdf", "txt", "pork"' => [
  88. 'File properties' => $temporary_txt_file_properties,
  89. 'Allowed_extensions' => ['asdf', 'txt', 'pork'],
  90. 'Expected errors' => [],
  91. ],
  92. 'Temporary txt validated with "exe" and "png"' => [
  93. 'File properties' => $temporary_txt_file_properties,
  94. 'Allowed_extensions' => ['exe', 'png'],
  95. 'Expected errors' => [
  96. 'Only files with the following extensions are allowed: <em class="placeholder">exe png</em>.',
  97. ],
  98. ],
  99. 'Permanent txt validated with "asdf", "txt", "pork"' => [
  100. 'File properties' => $permanent_txt_file_properties,
  101. 'Allowed_extensions' => ['asdf', 'txt', 'pork'],
  102. 'Expected errors' => [],
  103. ],
  104. 'Permanent txt validated with "exe" and "png"' => [
  105. 'File properties' => $permanent_txt_file_properties,
  106. 'Allowed_extensions' => ['exe', 'png'],
  107. 'Expected errors' => [
  108. 'Only files with the following extensions are allowed: <em class="placeholder">exe png</em>.',
  109. ],
  110. ],
  111. 'Permanent png validated with "png", "gif", "jpg", "jpeg"' => [
  112. 'File properties' => $permanent_png_file_properties,
  113. 'Allowed_extensions' => ['png', 'gif', 'jpg', 'jpeg'],
  114. 'Expected errors' => [],
  115. ],
  116. 'Permanent png validated with "exe" and "txt"' => [
  117. 'File properties' => $permanent_png_file_properties,
  118. 'Allowed_extensions' => ['exe', 'txt'],
  119. 'Expected errors' => [
  120. 'Only files with the following extensions are allowed: <em class="placeholder">exe txt</em>.',
  121. ],
  122. ],
  123. ];
  124. }
  125. /**
  126. * This ensures a specific file is actually an image.
  127. */
  128. public function testFileValidateIsImage() {
  129. $this->assertFileExists($this->image->getFileUri());
  130. $errors = file_validate_is_image($this->image);
  131. $this->assertCount(0, $errors, 'No error reported for our image file.');
  132. $this->assertFileExists($this->nonImage->getFileUri());
  133. $errors = file_validate_is_image($this->nonImage);
  134. $this->assertCount(1, $errors, 'An error reported for our non-image file.');
  135. }
  136. /**
  137. * This ensures the resolution of a specific file is within bounds.
  138. *
  139. * The image will be resized if it's too large.
  140. */
  141. public function testFileValidateImageResolution() {
  142. // Non-images.
  143. $errors = file_validate_image_resolution($this->nonImage);
  144. $this->assertCount(0, $errors, 'Should not get any errors for a non-image file.');
  145. $errors = file_validate_image_resolution($this->nonImage, '50x50', '100x100');
  146. $this->assertCount(0, $errors, 'Do not check the resolution on non files.');
  147. // Minimum size.
  148. $errors = file_validate_image_resolution($this->image);
  149. $this->assertCount(0, $errors, 'No errors for an image when there is no minimum or maximum resolution.');
  150. $errors = file_validate_image_resolution($this->image, 0, '200x1');
  151. $this->assertCount(1, $errors, 'Got an error for an image that was not wide enough.');
  152. $errors = file_validate_image_resolution($this->image, 0, '1x200');
  153. $this->assertCount(1, $errors, 'Got an error for an image that was not tall enough.');
  154. $errors = file_validate_image_resolution($this->image, 0, '200x200');
  155. $this->assertCount(1, $errors, 'Small images report an error.');
  156. // Maximum size.
  157. if ($this->container->get('image.factory')->getToolkitId()) {
  158. // Copy the image so that the original doesn't get resized.
  159. copy('core/misc/druplicon.png', 'temporary://druplicon.png');
  160. $this->image->setFileUri('temporary://druplicon.png');
  161. $errors = file_validate_image_resolution($this->image, '10x5');
  162. $this->assertCount(0, $errors, 'No errors should be reported when an oversized image can be scaled down.');
  163. $image = $this->container->get('image.factory')->get($this->image->getFileUri());
  164. // Verify that the image was scaled to the correct width and height.
  165. $this->assertLessThanOrEqual(10, $image->getWidth());
  166. $this->assertLessThanOrEqual(5, $image->getHeight());
  167. // Once again, now with negative width and height to force an error.
  168. copy('core/misc/druplicon.png', 'temporary://druplicon.png');
  169. $this->image->setFileUri('temporary://druplicon.png');
  170. $errors = file_validate_image_resolution($this->image, '-10x-5');
  171. $this->assertCount(1, $errors, 'An error reported for an oversized image that can not be scaled down.');
  172. \Drupal::service('file_system')->unlink('temporary://druplicon.png');
  173. }
  174. else {
  175. // TODO: should check that the error is returned if no toolkit is available.
  176. $errors = file_validate_image_resolution($this->image, '5x10');
  177. $this->assertCount(1, $errors, 'Oversize images that cannot be scaled get an error.');
  178. }
  179. }
  180. /**
  181. * This will ensure the filename length is valid.
  182. */
  183. public function testFileValidateNameLength() {
  184. // Create a new file entity.
  185. $file = File::create();
  186. // Add a filename with an allowed length and test it.
  187. $file->setFilename(str_repeat('x', 240));
  188. $this->assertEquals(240, strlen($file->getFilename()));
  189. $errors = file_validate_name_length($file);
  190. $this->assertCount(0, $errors, 'No errors reported for 240 length filename.');
  191. // Add a filename with a length too long and test it.
  192. $file->setFilename(str_repeat('x', 241));
  193. $errors = file_validate_name_length($file);
  194. $this->assertCount(1, $errors, 'An error reported for 241 length filename.');
  195. // Add a filename with an empty string and test it.
  196. $file->setFilename('');
  197. $errors = file_validate_name_length($file);
  198. $this->assertCount(1, $errors, 'An error reported for 0 length filename.');
  199. }
  200. /**
  201. * Tests file_validate_size().
  202. */
  203. public function testFileValidateSize() {
  204. // Create a file with a size of 1000 bytes, and quotas of only 1 byte.
  205. $file = File::create(['filesize' => 1000]);
  206. $errors = file_validate_size($file, 0, 0);
  207. $this->assertCount(0, $errors, 'No limits means no errors.');
  208. $errors = file_validate_size($file, 1, 0);
  209. $this->assertCount(1, $errors, 'Error for the file being over the limit.');
  210. $errors = file_validate_size($file, 0, 1);
  211. $this->assertCount(1, $errors, 'Error for the user being over their limit.');
  212. $errors = file_validate_size($file, 1, 1);
  213. $this->assertCount(2, $errors, 'Errors for both the file and their limit.');
  214. }
  215. }