You are here

public function UploadFileServiceTest::testSizeValidation in GraphQL 8.4

Tests that the file must not be larger than the file size limit.

File

tests/src/Kernel/Framework/UploadFileServiceTest.php, line 127

Class

UploadFileServiceTest
Tests file uploads that should be mapped to a field in a resolver.

Namespace

Drupal\Tests\graphql\Kernel\Framework

Code

public function testSizeValidation() : void {

  // Create a Symfony dummy uploaded file in test mode.
  $uploadFile = $this
    ->getUploadedFile(UPLOAD_ERR_OK, 4);

  // Create a file with 4 bytes.
  file_put_contents($uploadFile
    ->getRealPath(), 'test');
  $file_upload_response = $this->uploadService
    ->saveFileUpload($uploadFile, [
    'uri_scheme' => 'public',
    'file_directory' => 'test',
    // Only allow 1 byte.
    'max_filesize' => 1,
  ]);
  $violations = $file_upload_response
    ->getViolations();

  // @todo Do we want HTML tags in our violations or not?
  $this
    ->assertStringMatchesFormat('The file is <em class="placeholder">4 bytes</em> exceeding the maximum file size of <em class="placeholder">1 byte</em>.', $violations[0]['message']);
}