Skip to content

Commit a944bf8

Browse files
committed
Adding graphql_file_validate with subscriber.
1 parent fe4f738 commit a944bf8

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type: module
2+
name: GraphQL File Validate Test
3+
description: Tests file validate on uploads.
4+
package: Testing
5+
hidden: TRUE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Test module for file validation.
6+
*/
7+
8+
use Drupal\file\FileInterface;
9+
10+
/**
11+
* Test to validate file exists.
12+
*/
13+
function graphql_file_validate(FileInterface $file): void {
14+
if (!file_exists($file->getFileUri())) {
15+
throw new \Exception('File does not exist during validation: ' . $file->getFileUri());
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
graphql_file_validate_test_subscriber:
3+
class: Drupal\graphql_file_validate\EventSubscriber\GraphqlFileValidationTestSubscriber
4+
tags:
5+
- { name: event_subscriber }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Drupal\graphql_file_validate\EventSubscriber;
6+
7+
use Drupal\file\Validation\FileValidationEvent;
8+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9+
10+
/**
11+
* Provides a file validation listener for graphql file validate test.
12+
*/
13+
class GraphqlFileValidationTestSubscriber implements EventSubscriberInterface {
14+
15+
/**
16+
* Handles the file validation event.
17+
*
18+
* @param \Drupal\file\Validation\FileValidationEvent $event
19+
* The event.
20+
*/
21+
public function onFileValidation(FileValidationEvent $event): void {
22+
graphql_file_validate('validate', [$event->file->id()]);
23+
}
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public static function getSubscribedEvents(): array {
29+
return [FileValidationEvent::class => 'onFileValidation'];
30+
}
31+
32+
}

tests/src/Kernel/Framework/UploadFileServiceTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class UploadFileServiceTest extends GraphQLTestBase {
1818
/**
1919
* {@inheritdoc}
2020
*/
21-
protected static $modules = ['file_validator_test'];
21+
protected static $modules = ['graphql_file_validate'];
2222

2323
/**
2424
* The FileUpload object we want to test, gets prepared in setUp().

0 commit comments

Comments
 (0)