Skip to content

Commit

Permalink
Removing hook_file_validate test module.
Browse files Browse the repository at this point in the history
  • Loading branch information
apathak18 committed Jul 25, 2024
1 parent b9ade93 commit 3a8b7d8
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 45 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ jobs:
strategy:
fail-fast: false
matrix:
# # php-versions: ['8.1', '8.2']
# # drupal-core: ['10.3.x']
# phpstan: ['0']
php-versions: ['8.1', '8.2']
drupal-core: ['10.3.x']
phpstan: ['0']
include:
# Extra run to test older supported Drupal 10.2.x.
# - php-versions: '8.1'
# drupal-core: '10.2.x'
# phpstan: '0'
- php-versions: '8.1'
drupal-core: '10.2.x'
phpstan: '0'
# We only need to run PHPStan once on the latest PHP version.
# - php-versions: '8.3'
# drupal-core: '10.3.x'
# phpstan: '1'
- php-versions: '8.3'
drupal-core: '10.3.x'
phpstan: '1'
- php-versions: '8.3'
drupal-core: '11.0.x'
phpstan: '1'
Expand Down
2 changes: 1 addition & 1 deletion src/EventSubscriber/SubrequestSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function onKernelRequestFinished(FinishRequestEvent $event): void {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
public static function getSubscribedEvents(): array {
return [
KernelEvents::REQUEST => 'onKernelRequest',
KernelEvents::FINISH_REQUEST => 'onKernelRequestFinished',
Expand Down
16 changes: 9 additions & 7 deletions src/GraphQL/Utility/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class FileUpload {
use StringTranslationTrait;

/**
* The file storage where we will create new file entities from.
* The entity storage for the 'file' entity type.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
* @var \Drupal\file\FileStorageInterface
*/
protected $fileStorage;

Expand Down Expand Up @@ -137,7 +137,9 @@ public function __construct(
ImageFactory $image_factory,
FileValidatorInterface $file_validator,
) {
$this->fileStorage = $entityTypeManager->getStorage('file');
/** @var \Drupal\file\FileStorageInterface $file_storage */
$file_storage = $entityTypeManager->getStorage('file');
$this->fileStorage = $file_storage;
$this->currentUser = $currentUser;
$this->mimeTypeGuesser = $mimeTypeGuesser;
$this->fileSystem = $fileSystem;
Expand Down Expand Up @@ -268,7 +270,7 @@ public function saveFileUpload(UploadedFile $uploaded_file, array $settings): Fi

try {
// Begin building file entity.
/** @var \Drupal\Core\Entity\EntityInterface $file */
/** @var \Drupal\file\FileInterface $file */
$file = $this->fileStorage->create([]);
$file->setOwnerId($this->currentUser->id());
$file->setFilename($prepared_filename);
Expand All @@ -281,10 +283,11 @@ public function saveFileUpload(UploadedFile $uploaded_file, array $settings): Fi
// Validate against fileValidator first with the temporary path.
/** @var \Symfony\Component\Validator\ConstraintViolationListInterface $errors */
$errors = $this->fileValidator->validate($file, $validators);
if (!empty($errors)) {
if (count($errors) > 0) {
$response->addViolations($errors);
return $response;
}

// Validate Image resolution.
$maxResolution = $settings['max_resolution'] ?? 0;
$minResolution = $settings['min_resolution'] ?? 0;
Expand Down Expand Up @@ -323,7 +326,6 @@ public function saveFileUpload(UploadedFile $uploaded_file, array $settings): Fi
}

$file->save();

$response->setFileEntity($file);
return $response;
}
Expand Down Expand Up @@ -515,7 +517,7 @@ protected function prepareFilename(string $filename, array &$validators): string

$passes_validation = FALSE;
if (!empty($validators['FileExtension']['extensions'])) {
/** @var \Drupal\Core\Entity\EntityInterface $file */
/** @var \Drupal\file\FileInterface $file */
$file = $this->fileStorage->create([]);
$file->setFilename($filename);
$passes_validation = empty($this->fileValidator->validate($file, $validators['FileExtension']['extensions']));
Expand Down

This file was deleted.

17 changes: 0 additions & 17 deletions tests/modules/graphql_file_validate/graphql_file_validate.module

This file was deleted.

13 changes: 7 additions & 6 deletions tests/src/Kernel/Framework/UploadFileServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@
*/
class UploadFileServiceTest extends GraphQLTestBase {

/**
* {@inheritdoc}
*/
protected static $modules = ['file', 'graphql_file_validate'];

/**
* The FileUpload object we want to test, gets prepared in setUp().
*
* @var \Drupal\graphql\GraphQL\Utility\FileUpload
*/
protected $uploadService;

/**
* Modules to enable.
*
* @var array
*/
protected static $modules = ['file'];

/**
* Gets the file path of the source file.
*
Expand Down Expand Up @@ -67,7 +69,6 @@ public function testSuccess(): void {
'file_directory' => 'test',
]);
$file_entity = $file_upload_response->getFileEntity();

$this->assertSame('public://test/test.txt', $file_entity->getFileUri());
$this->assertFileExists($file_entity->getFileUri());
}
Expand Down

0 comments on commit 3a8b7d8

Please sign in to comment.