From 41007bc77168db192d31ce72ce1b211e13dfb464 Mon Sep 17 00:00:00 2001 From: Aurelian zaha Date: Tue, 2 May 2023 15:53:26 +0200 Subject: [PATCH 01/12] feat(utility/fileupload): file_validate_image_resolution --- src/GraphQL/Utility/FileUpload.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/GraphQL/Utility/FileUpload.php b/src/GraphQL/Utility/FileUpload.php index 1fbb3fadc..2ab33a4d8 100644 --- a/src/GraphQL/Utility/FileUpload.php +++ b/src/GraphQL/Utility/FileUpload.php @@ -490,6 +490,15 @@ protected function getUploadValidators(array $settings): array { $validators['file_validate_extensions'] = [$settings['file_extensions']]; } + // Add the resolution check if necessary. + if (!empty($settings['max_resolution']) + || !empty($settings['min_resolution']) + ) { + $maxResolution = !empty($settings['max_resolution']) ? $settings['max_resolution'] : 0; + $minResolution = !empty($settings['min_resolution']) ? $settings['min_resolution'] : 0; + $validators['file_validate_image_resolution'] = [$maxResolution, $minResolution]; + } + return $validators; } From 23cbb71ce1883529ce85cef87a36febf6499ead5 Mon Sep 17 00:00:00 2001 From: Aurelian zaha Date: Tue, 2 May 2023 15:59:34 +0200 Subject: [PATCH 02/12] feat(utility/fileupload): fix CS --- src/GraphQL/Utility/FileUpload.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/GraphQL/Utility/FileUpload.php b/src/GraphQL/Utility/FileUpload.php index 2ab33a4d8..b76986431 100644 --- a/src/GraphQL/Utility/FileUpload.php +++ b/src/GraphQL/Utility/FileUpload.php @@ -496,7 +496,10 @@ protected function getUploadValidators(array $settings): array { ) { $maxResolution = !empty($settings['max_resolution']) ? $settings['max_resolution'] : 0; $minResolution = !empty($settings['min_resolution']) ? $settings['min_resolution'] : 0; - $validators['file_validate_image_resolution'] = [$maxResolution, $minResolution]; + $validators['file_validate_image_resolution'] = [ + $maxResolution, + $minResolution, + ]; } return $validators; From b8d0e45b44454ccfc8781ef7c6393e0a2d130b97 Mon Sep 17 00:00:00 2001 From: Aurelian zaha Date: Tue, 2 May 2023 16:05:26 +0200 Subject: [PATCH 03/12] feat(utility/fileupload): simplify operators --- src/GraphQL/Utility/FileUpload.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/GraphQL/Utility/FileUpload.php b/src/GraphQL/Utility/FileUpload.php index b76986431..a98c899e4 100644 --- a/src/GraphQL/Utility/FileUpload.php +++ b/src/GraphQL/Utility/FileUpload.php @@ -494,8 +494,8 @@ protected function getUploadValidators(array $settings): array { if (!empty($settings['max_resolution']) || !empty($settings['min_resolution']) ) { - $maxResolution = !empty($settings['max_resolution']) ? $settings['max_resolution'] : 0; - $minResolution = !empty($settings['min_resolution']) ? $settings['min_resolution'] : 0; + $maxResolution = $settings['max_resolution'] ?? 0; + $minResolution = $settings['min_resolution'] ?? 0; $validators['file_validate_image_resolution'] = [ $maxResolution, $minResolution, From aea4cda421ef661bc9f7df124054eda6b7c1bb38 Mon Sep 17 00:00:00 2001 From: Aurelian zaha Date: Thu, 4 May 2023 13:57:17 +0200 Subject: [PATCH 04/12] feat(FileUpload): graphql_file_validate_image_resolution --- graphql.module | 81 ++++++++++++++++++++++++++++++ src/GraphQL/Utility/FileUpload.php | 2 +- 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/graphql.module b/graphql.module index 07a3b48a5..88b76edeb 100644 --- a/graphql.module +++ b/graphql.module @@ -5,6 +5,8 @@ * Primary module hooks for GraphQL module. */ +use Drupal\file\FileInterface; + /** * Implements hook_help(). */ @@ -44,3 +46,82 @@ function graphql_theme(): array { ], ]; } + +/** + * Copy of file_validate_image_resolution() without creating messages. + * + * Verifies that image dimensions are within the specified maximum and minimum. + * + * Non-image files will be ignored. If an image toolkit is available the image + * will be scaled to fit within the desired maximum dimensions. + * + * @param \Drupal\file\FileInterface $file + * A file entity. This function may resize the file affecting its size. + * @param string|int $maximum_dimensions + * (optional) A string in the form WIDTHxHEIGHT; for example, '640x480' or + * '85x85'. If an image toolkit is installed, the image will be resized down + * to these dimensions. A value of zero (the default) indicates no restriction + * on size, so no resizing will be attempted. + * @param string|int $minimum_dimensions + * (optional) A string in the form WIDTHxHEIGHT. This will check that the + * image meets a minimum size. A value of zero (the default) indicates that + * there is no restriction on size. + * + * @return array + * An empty array if the file meets the specified dimensions, was resized + * successfully to meet those requirements or is not an image. If the image + * does not meet the requirements or an attempt to resize it fails, an array + * containing the error message will be returned. + * + * @see hook_file_validate() + */ +function graphql_file_validate_image_resolution(FileInterface $file, $maximum_dimensions = 0, $minimum_dimensions = 0) { + $errors = []; + + // Check first that the file is an image. + $image_factory = \Drupal::service('image.factory'); + $image = $image_factory->get($file->getFileUri()); + + if ($image->isValid()) { + $scaling = FALSE; + if ($maximum_dimensions) { + // Check that it is smaller than the given dimensions. + [$width, $height] = explode('x', $maximum_dimensions); + if ($image->getWidth() > $width || $image->getHeight() > $height) { + // Try to resize the image to fit the dimensions. + if ($image->scale($width, $height)) { + $scaling = TRUE; + $image->save(); + } + else { + $errors[] = t('The image exceeds the maximum allowed dimensions and an attempt to resize it failed.'); + } + } + } + + if ($minimum_dimensions) { + // Check that it is larger than the given dimensions. + [$width, $height] = explode('x', $minimum_dimensions); + if ($image->getWidth() < $width || $image->getHeight() < $height) { + if ($scaling) { + $errors[] = t('The resized image is too small. The minimum dimensions are %dimensions pixels and after resizing, the image size will be %widthx%height pixels.', + [ + '%dimensions' => $minimum_dimensions, + '%width' => $image->getWidth(), + '%height' => $image->getHeight(), + ]); + } + else { + $errors[] = t('The image is too small. The minimum dimensions are %dimensions pixels and the image size is %widthx%height pixels.', + [ + '%dimensions' => $minimum_dimensions, + '%width' => $image->getWidth(), + '%height' => $image->getHeight(), + ]); + } + } + } + } + + return $errors; +} diff --git a/src/GraphQL/Utility/FileUpload.php b/src/GraphQL/Utility/FileUpload.php index a98c899e4..5364c9fb5 100644 --- a/src/GraphQL/Utility/FileUpload.php +++ b/src/GraphQL/Utility/FileUpload.php @@ -496,7 +496,7 @@ protected function getUploadValidators(array $settings): array { ) { $maxResolution = $settings['max_resolution'] ?? 0; $minResolution = $settings['min_resolution'] ?? 0; - $validators['file_validate_image_resolution'] = [ + $validators['graphql_file_validate_image_resolution'] = [ $maxResolution, $minResolution, ]; From 427fa7855d5eb6997572531f1642219143a595ed Mon Sep 17 00:00:00 2001 From: Aurelian zaha Date: Thu, 4 May 2023 14:25:23 +0200 Subject: [PATCH 05/12] feat(FileUpload): fix CS --- graphql.module | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/graphql.module b/graphql.module index 88b76edeb..fa798b8cb 100644 --- a/graphql.module +++ b/graphql.module @@ -79,7 +79,9 @@ function graphql_file_validate_image_resolution(FileInterface $file, $maximum_di $errors = []; // Check first that the file is an image. + /** @var \Drupal\Core\Image\ImageFactory $image_factory */ $image_factory = \Drupal::service('image.factory'); + /** @var \Drupal\Core\Image\ImageInterface $image */ $image = $image_factory->get($file->getFileUri()); if ($image->isValid()) { @@ -89,7 +91,7 @@ function graphql_file_validate_image_resolution(FileInterface $file, $maximum_di [$width, $height] = explode('x', $maximum_dimensions); if ($image->getWidth() > $width || $image->getHeight() > $height) { // Try to resize the image to fit the dimensions. - if ($image->scale($width, $height)) { + if ($image->scale((int) $width, (int) $height)) { $scaling = TRUE; $image->save(); } From 15f76e56fade604f893557cf34216ac2abf4be29 Mon Sep 17 00:00:00 2001 From: Aurelian zaha Date: Mon, 8 May 2023 13:20:01 +0200 Subject: [PATCH 06/12] feat(FileUpload): make validateFileImageResolution internal function --- graphql.module | 81 ---------------------- graphql.services.yml | 1 + src/GraphQL/Utility/FileUpload.php | 104 +++++++++++++++++++++++++---- 3 files changed, 92 insertions(+), 94 deletions(-) diff --git a/graphql.module b/graphql.module index fa798b8cb..a2a78da93 100644 --- a/graphql.module +++ b/graphql.module @@ -46,84 +46,3 @@ function graphql_theme(): array { ], ]; } - -/** - * Copy of file_validate_image_resolution() without creating messages. - * - * Verifies that image dimensions are within the specified maximum and minimum. - * - * Non-image files will be ignored. If an image toolkit is available the image - * will be scaled to fit within the desired maximum dimensions. - * - * @param \Drupal\file\FileInterface $file - * A file entity. This function may resize the file affecting its size. - * @param string|int $maximum_dimensions - * (optional) A string in the form WIDTHxHEIGHT; for example, '640x480' or - * '85x85'. If an image toolkit is installed, the image will be resized down - * to these dimensions. A value of zero (the default) indicates no restriction - * on size, so no resizing will be attempted. - * @param string|int $minimum_dimensions - * (optional) A string in the form WIDTHxHEIGHT. This will check that the - * image meets a minimum size. A value of zero (the default) indicates that - * there is no restriction on size. - * - * @return array - * An empty array if the file meets the specified dimensions, was resized - * successfully to meet those requirements or is not an image. If the image - * does not meet the requirements or an attempt to resize it fails, an array - * containing the error message will be returned. - * - * @see hook_file_validate() - */ -function graphql_file_validate_image_resolution(FileInterface $file, $maximum_dimensions = 0, $minimum_dimensions = 0) { - $errors = []; - - // Check first that the file is an image. - /** @var \Drupal\Core\Image\ImageFactory $image_factory */ - $image_factory = \Drupal::service('image.factory'); - /** @var \Drupal\Core\Image\ImageInterface $image */ - $image = $image_factory->get($file->getFileUri()); - - if ($image->isValid()) { - $scaling = FALSE; - if ($maximum_dimensions) { - // Check that it is smaller than the given dimensions. - [$width, $height] = explode('x', $maximum_dimensions); - if ($image->getWidth() > $width || $image->getHeight() > $height) { - // Try to resize the image to fit the dimensions. - if ($image->scale((int) $width, (int) $height)) { - $scaling = TRUE; - $image->save(); - } - else { - $errors[] = t('The image exceeds the maximum allowed dimensions and an attempt to resize it failed.'); - } - } - } - - if ($minimum_dimensions) { - // Check that it is larger than the given dimensions. - [$width, $height] = explode('x', $minimum_dimensions); - if ($image->getWidth() < $width || $image->getHeight() < $height) { - if ($scaling) { - $errors[] = t('The resized image is too small. The minimum dimensions are %dimensions pixels and after resizing, the image size will be %widthx%height pixels.', - [ - '%dimensions' => $minimum_dimensions, - '%width' => $image->getWidth(), - '%height' => $image->getHeight(), - ]); - } - else { - $errors[] = t('The image is too small. The minimum dimensions are %dimensions pixels and the image size is %widthx%height pixels.', - [ - '%dimensions' => $minimum_dimensions, - '%width' => $image->getWidth(), - '%height' => $image->getHeight(), - ]); - } - } - } - } - - return $errors; -} diff --git a/graphql.services.yml b/graphql.services.yml index b86695715..39a5eed99 100644 --- a/graphql.services.yml +++ b/graphql.services.yml @@ -183,6 +183,7 @@ services: - '@config.factory' - '@renderer' - '@event_dispatcher' + - '@image.factory' plugin.manager.graphql.persisted_query: class: Drupal\graphql\Plugin\PersistedQueryPluginManager diff --git a/src/GraphQL/Utility/FileUpload.php b/src/GraphQL/Utility/FileUpload.php index 5364c9fb5..b7252d7ae 100644 --- a/src/GraphQL/Utility/FileUpload.php +++ b/src/GraphQL/Utility/FileUpload.php @@ -10,6 +10,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\File\Exception\FileException; use Drupal\Core\File\FileSystemInterface; +use Drupal\Core\Image\ImageFactory; use Drupal\Core\Lock\LockBackendInterface; use Drupal\Core\Logger\LoggerChannelInterface; use Drupal\Core\Render\RenderContext; @@ -103,6 +104,13 @@ class FileUpload { */ protected $eventDispatcher; + /** + * The image factory service. + * + * @var \Drupal\Core\Image\ImageFactory + */ + protected $imageFactory; + /** * Constructor. */ @@ -116,7 +124,8 @@ public function __construct( LockBackendInterface $lock, ConfigFactoryInterface $config_factory, RendererInterface $renderer, - EventDispatcherInterface $eventDispatcher + EventDispatcherInterface $eventDispatcher, + ImageFactory $image_factory, ) { /** @var \Drupal\file\FileStorageInterface $file_storage */ $file_storage = $entityTypeManager->getStorage('file'); @@ -130,6 +139,7 @@ public function __construct( $this->systemFileConfig = $config_factory->get('system.file'); $this->renderer = $renderer; $this->eventDispatcher = $eventDispatcher; + $this->imageFactory = $image_factory; } /** @@ -259,6 +269,9 @@ public function saveFileUpload(UploadedFile $uploaded_file, array $settings): Fi // Validate against file_validate() first with the temporary path. $errors = file_validate($file, $validators); + $maxResolution = $settings['max_resolution'] ?? 0; + $minResolution = $settings['min_resolution'] ?? 0; + $errors += $this->validateFileImageResolution($file, $maxResolution, $minResolution); if (!empty($errors)) { $response->addViolations($errors); @@ -370,6 +383,83 @@ protected function validate(FileInterface $file, array $validators, FileUploadRe return TRUE; } + /** + * Copy of file_validate_image_resolution() without creating messages. + * + * Verifies that image dimensions are within the specified maximum and minimum. + * + * Non-image files will be ignored. If an image toolkit is available the image + * will be scaled to fit within the desired maximum dimensions. + * + * @param \Drupal\file\FileInterface $file + * A file entity. This function may resize the file affecting its size. + * @param string|int $maximum_dimensions + * (optional) A string in the form WIDTHxHEIGHT; for example, '640x480' or + * '85x85'. If an image toolkit is installed, the image will be resized down + * to these dimensions. A value of zero (the default) indicates no restriction + * on size, so no resizing will be attempted. + * @param string|int $minimum_dimensions + * (optional) A string in the form WIDTHxHEIGHT. This will check that the + * image meets a minimum size. A value of zero (the default) indicates that + * there is no restriction on size. + * + * @return array + * An empty array if the file meets the specified dimensions, was resized + * successfully to meet those requirements or is not an image. If the image + * does not meet the requirements or an attempt to resize it fails, an array + * containing the error message will be returned. + */ + protected function validateFileImageResolution(FileInterface $file, $maximum_dimensions = 0, $minimum_dimensions = 0) { + $errors = []; + + // Check first that the file is an image. + /** @var \Drupal\Core\Image\ImageInterface $image */ + $image = $this->imageFactory->get($file->getFileUri()); + + if ($image->isValid()) { + $scaling = FALSE; + if ($maximum_dimensions) { + // Check that it is smaller than the given dimensions. + [$width, $height] = explode('x', $maximum_dimensions); + if ($image->getWidth() > $width || $image->getHeight() > $height) { + // Try to resize the image to fit the dimensions. + if ($image->scale((int) $width, (int) $height)) { + $scaling = TRUE; + $image->save(); + } + else { + $errors[] = t('The image exceeds the maximum allowed dimensions and an attempt to resize it failed.'); + } + } + } + + if ($minimum_dimensions) { + // Check that it is larger than the given dimensions. + [$width, $height] = explode('x', $minimum_dimensions); + if ($image->getWidth() < $width || $image->getHeight() < $height) { + if ($scaling) { + $errors[] = t('The resized image is too small. The minimum dimensions are %dimensions pixels and after resizing, the image size will be %widthx%height pixels.', + [ + '%dimensions' => $minimum_dimensions, + '%width' => $image->getWidth(), + '%height' => $image->getHeight(), + ]); + } + else { + $errors[] = t('The image is too small. The minimum dimensions are %dimensions pixels and the image size is %widthx%height pixels.', + [ + '%dimensions' => $minimum_dimensions, + '%width' => $image->getWidth(), + '%height' => $image->getHeight(), + ]); + } + } + } + } + + return $errors; + } + /** * Prepares the filename to strip out any malicious extensions. * @@ -490,18 +580,6 @@ protected function getUploadValidators(array $settings): array { $validators['file_validate_extensions'] = [$settings['file_extensions']]; } - // Add the resolution check if necessary. - if (!empty($settings['max_resolution']) - || !empty($settings['min_resolution']) - ) { - $maxResolution = $settings['max_resolution'] ?? 0; - $minResolution = $settings['min_resolution'] ?? 0; - $validators['graphql_file_validate_image_resolution'] = [ - $maxResolution, - $minResolution, - ]; - } - return $validators; } From 68b40c06bc7c9aea14d6e484700a3caaae5b7073 Mon Sep 17 00:00:00 2001 From: Aurelian zaha Date: Mon, 8 May 2023 13:20:58 +0200 Subject: [PATCH 07/12] feat(FileUpload): revert --- graphql.module | 2 -- 1 file changed, 2 deletions(-) diff --git a/graphql.module b/graphql.module index a2a78da93..07a3b48a5 100644 --- a/graphql.module +++ b/graphql.module @@ -5,8 +5,6 @@ * Primary module hooks for GraphQL module. */ -use Drupal\file\FileInterface; - /** * Implements hook_help(). */ From b8aaa04dbb6323a4beac17d761bcf379a39d4ac5 Mon Sep 17 00:00:00 2001 From: Aurelian zaha Date: Mon, 8 May 2023 13:26:43 +0200 Subject: [PATCH 08/12] feat(UploadFileServiceTest): update test --- tests/src/Kernel/Framework/UploadFileServiceTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/src/Kernel/Framework/UploadFileServiceTest.php b/tests/src/Kernel/Framework/UploadFileServiceTest.php index c3f6d8d9c..3a8f9a841 100644 --- a/tests/src/Kernel/Framework/UploadFileServiceTest.php +++ b/tests/src/Kernel/Framework/UploadFileServiceTest.php @@ -205,6 +205,7 @@ public function testLockReleased(): void { \Drupal::service('config.factory'), \Drupal::service('renderer'), \Drupal::service('event_dispatcher'), + \Drupal::service('image.factory'), ); // Create a Symfony dummy uploaded file in test mode. From bb42b2e1cbc4cafb40cba535ac8043069e1d6553 Mon Sep 17 00:00:00 2001 From: Aurelian zaha Date: Mon, 8 May 2023 13:31:32 +0200 Subject: [PATCH 09/12] feat(FileUpload): fix CS --- src/GraphQL/Utility/FileUpload.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/GraphQL/Utility/FileUpload.php b/src/GraphQL/Utility/FileUpload.php index b7252d7ae..bc91df1e4 100644 --- a/src/GraphQL/Utility/FileUpload.php +++ b/src/GraphQL/Utility/FileUpload.php @@ -386,7 +386,8 @@ protected function validate(FileInterface $file, array $validators, FileUploadRe /** * Copy of file_validate_image_resolution() without creating messages. * - * Verifies that image dimensions are within the specified maximum and minimum. + * Verifies that image dimensions are within the specified maximum and + * minimum. * * Non-image files will be ignored. If an image toolkit is available the image * will be scaled to fit within the desired maximum dimensions. @@ -396,8 +397,8 @@ protected function validate(FileInterface $file, array $validators, FileUploadRe * @param string|int $maximum_dimensions * (optional) A string in the form WIDTHxHEIGHT; for example, '640x480' or * '85x85'. If an image toolkit is installed, the image will be resized down - * to these dimensions. A value of zero (the default) indicates no restriction - * on size, so no resizing will be attempted. + * to these dimensions. A value of zero (the default) indicates no + * restriction on size, so no resizing will be attempted. * @param string|int $minimum_dimensions * (optional) A string in the form WIDTHxHEIGHT. This will check that the * image meets a minimum size. A value of zero (the default) indicates that @@ -428,7 +429,7 @@ protected function validateFileImageResolution(FileInterface $file, $maximum_dim $image->save(); } else { - $errors[] = t('The image exceeds the maximum allowed dimensions and an attempt to resize it failed.'); + $errors[] = $this->t('The image exceeds the maximum allowed dimensions and an attempt to resize it failed.'); } } } @@ -438,7 +439,7 @@ protected function validateFileImageResolution(FileInterface $file, $maximum_dim [$width, $height] = explode('x', $minimum_dimensions); if ($image->getWidth() < $width || $image->getHeight() < $height) { if ($scaling) { - $errors[] = t('The resized image is too small. The minimum dimensions are %dimensions pixels and after resizing, the image size will be %widthx%height pixels.', + $errors[] = $this->t('The resized image is too small. The minimum dimensions are %dimensions pixels and after resizing, the image size will be %widthx%height pixels.', [ '%dimensions' => $minimum_dimensions, '%width' => $image->getWidth(), @@ -446,7 +447,7 @@ protected function validateFileImageResolution(FileInterface $file, $maximum_dim ]); } else { - $errors[] = t('The image is too small. The minimum dimensions are %dimensions pixels and the image size is %widthx%height pixels.', + $errors[] = $this->t('The image is too small. The minimum dimensions are %dimensions pixels and the image size is %widthx%height pixels.', [ '%dimensions' => $minimum_dimensions, '%width' => $image->getWidth(), From f825f03e29935706f37d30f1d9b73accdde839a6 Mon Sep 17 00:00:00 2001 From: Aurelian zaha Date: Mon, 8 May 2023 13:47:21 +0200 Subject: [PATCH 10/12] feat(FileUpload): fix CS --- src/GraphQL/Utility/FileUpload.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GraphQL/Utility/FileUpload.php b/src/GraphQL/Utility/FileUpload.php index bc91df1e4..6e6319873 100644 --- a/src/GraphQL/Utility/FileUpload.php +++ b/src/GraphQL/Utility/FileUpload.php @@ -125,7 +125,7 @@ public function __construct( ConfigFactoryInterface $config_factory, RendererInterface $renderer, EventDispatcherInterface $eventDispatcher, - ImageFactory $image_factory, + ImageFactory $image_factory ) { /** @var \Drupal\file\FileStorageInterface $file_storage */ $file_storage = $entityTypeManager->getStorage('file'); From 803d5e9583e2b757201054aa23e8f2039b59c3a2 Mon Sep 17 00:00:00 2001 From: Aurelian zaha Date: Mon, 8 May 2023 14:42:24 +0200 Subject: [PATCH 11/12] feat(UploadFileServiceTest): testDimensionValidation --- tests/files/image/10x10.png | Bin 0 -> 1065 bytes .../Framework/UploadFileServiceTest.php | 26 ++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 tests/files/image/10x10.png diff --git a/tests/files/image/10x10.png b/tests/files/image/10x10.png new file mode 100644 index 0000000000000000000000000000000000000000..f59f2d5e15c207cfc570cb9af184fb65dfdfd39f GIT binary patch literal 1065 zcmeAS@N?(olHy`uVBq!ia0vp^AT}2V3y@T|W;X*;3dtTpz6=aiY77hwEes65fIC+X!N8z1{{B`YZkFc3V0m<7;)`> z*i+-<{>{%Geyy5xxA5u>w~n@D6EDy2zyEUj|E!ap=5k@WuNSQ`*HxP`b<2v(%X6(S z+U0G(t&7!yotj&}Cbr%GSoW&#a&9Y%W@PDNw4i=RJ3 zAK#_N*}}`bao$yxZ#THrBdntSsy=796QAoTz0JCqaZOWq31@$Gv3 z#>Hb-*FU=*+7|<4YTDY3`>H%F9lHeYSMJn$^WW9?MoH^$j>_lhqVJ<-J2x}TuHe}A zXwz3SPhrl~^S&KAhQ8XfPRA_QxVd1{88M}hh`V8XH$F;A5MLHO*);}z6 zR_+hJ7^gJs+QmIf+0NoI}**azkbWSjaT0by*vz%5P-@w8QsMB)-=v^vf-o~hWk{fW^7Z_7#d zOnxw5n=E4YluPXq&_2}?*NBpo#FA92mdKI;Vst09NO?ZU6uP literal 0 HcmV?d00001 diff --git a/tests/src/Kernel/Framework/UploadFileServiceTest.php b/tests/src/Kernel/Framework/UploadFileServiceTest.php index 3a8f9a841..fa160931f 100644 --- a/tests/src/Kernel/Framework/UploadFileServiceTest.php +++ b/tests/src/Kernel/Framework/UploadFileServiceTest.php @@ -146,6 +146,32 @@ public function testSizeValidation(): void { ); } + /** + * Tests that the file dimension is not be larger than the limit. + * + * Image should de resized. + */ + public function testDimensionValidation(): void { + // Create a Symfony dummy uploaded file in test mode. + $uploadFile = $this->getUploadedFile(UPLOAD_ERR_OK, 4); + + $image = file_get_contents(\Drupal::service('extension.list.module')->getPath('graphql') . '/tests/files/image/10x10.png'); + + // Create a file with 4 bytes. + file_put_contents($uploadFile->getRealPath(), $image); + + $file_upload_response = $this->uploadService->saveFileUpload($uploadFile, [ + 'uri_scheme' => 'public', + 'file_directory' => 'test', + // Only allow 5x5 dimension. + 'max_resolution' => '5x5', + ]); + $file_entity = $file_upload_response->getFileEntity(); + $image = \Drupal::service('image.factory')->get($file_entity->getFileUri()); + $this->assertEquals(5, $image->getWidth()); + $this->assertEquals(5, $image->getHeight()); + } + /** * Tests that the uploaded file extension is renamed to txt. */ From dfa212a20741f28721c41ad053747645e01ab240 Mon Sep 17 00:00:00 2001 From: Aurelian zaha Date: Wed, 17 May 2023 10:35:08 +0200 Subject: [PATCH 12/12] feat(tests): add test testDimensionTooSmallValidation --- src/GraphQL/Utility/FileUpload.php | 6 ++-- .../Framework/UploadFileServiceTest.php | 34 ++++++++++++++++--- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/GraphQL/Utility/FileUpload.php b/src/GraphQL/Utility/FileUpload.php index 6e6319873..bd22f7e0a 100644 --- a/src/GraphQL/Utility/FileUpload.php +++ b/src/GraphQL/Utility/FileUpload.php @@ -271,7 +271,9 @@ public function saveFileUpload(UploadedFile $uploaded_file, array $settings): Fi $errors = file_validate($file, $validators); $maxResolution = $settings['max_resolution'] ?? 0; $minResolution = $settings['min_resolution'] ?? 0; - $errors += $this->validateFileImageResolution($file, $maxResolution, $minResolution); + if (!empty($maxResolution) || !empty($minResolution)) { + $errors += $this->validateFileImageResolution($file, $maxResolution, $minResolution); + } if (!empty($errors)) { $response->addViolations($errors); @@ -410,7 +412,7 @@ protected function validate(FileInterface $file, array $validators, FileUploadRe * does not meet the requirements or an attempt to resize it fails, an array * containing the error message will be returned. */ - protected function validateFileImageResolution(FileInterface $file, $maximum_dimensions = 0, $minimum_dimensions = 0) { + protected function validateFileImageResolution(FileInterface $file, $maximum_dimensions = 0, $minimum_dimensions = 0): array { $errors = []; // Check first that the file is an image. diff --git a/tests/src/Kernel/Framework/UploadFileServiceTest.php b/tests/src/Kernel/Framework/UploadFileServiceTest.php index fa160931f..ce668c835 100644 --- a/tests/src/Kernel/Framework/UploadFileServiceTest.php +++ b/tests/src/Kernel/Framework/UploadFileServiceTest.php @@ -147,11 +147,9 @@ public function testSizeValidation(): void { } /** - * Tests that the file dimension is not be larger than the limit. - * - * Image should de resized. + * Tests that a larger image is resized to maximum dimensions. */ - public function testDimensionValidation(): void { + public function testDimensionTooLargeValidation(): void { // Create a Symfony dummy uploaded file in test mode. $uploadFile = $this->getUploadedFile(UPLOAD_ERR_OK, 4); @@ -163,7 +161,7 @@ public function testDimensionValidation(): void { $file_upload_response = $this->uploadService->saveFileUpload($uploadFile, [ 'uri_scheme' => 'public', 'file_directory' => 'test', - // Only allow 5x5 dimension. + // Only allow maximum 5x5 dimension. 'max_resolution' => '5x5', ]); $file_entity = $file_upload_response->getFileEntity(); @@ -172,6 +170,32 @@ public function testDimensionValidation(): void { $this->assertEquals(5, $image->getHeight()); } + /** + * Tests that a image that is too small returns a violation. + */ + public function testDimensionTooSmallValidation(): void { + // Create a Symfony dummy uploaded file in test mode. + $uploadFile = $this->getUploadedFile(UPLOAD_ERR_OK, 4); + + $image = file_get_contents(\Drupal::service('extension.list.module')->getPath('graphql') . '/tests/files/image/10x10.png'); + + // Create a file with 4 bytes. + file_put_contents($uploadFile->getRealPath(), $image); + + $file_upload_response = $this->uploadService->saveFileUpload($uploadFile, [ + 'uri_scheme' => 'public', + 'file_directory' => 'test', + // Only allow minimum dimension 15x15. + 'min_resolution' => '15x15', + ]); + $violations = $file_upload_response->getViolations(); + + $this->assertStringMatchesFormat( + 'The image is too small. The minimum dimensions are 15x15 pixels and the image size is 10x10 pixels.', + $violations[0]['message'] + ); + } + /** * Tests that the uploaded file extension is renamed to txt. */