You are here

public function FileUploadTest::testFileUploadInvalidFileType in JSON:API 8.2

Tests using the file upload route with an invalid file type.

File

tests/src/Functional/FileUploadTest.php, line 536

Class

FileUploadTest
Tests binary data file upload route.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testFileUploadInvalidFileType() {
  $this
    ->setUpAuthorization('POST');
  $this
    ->config('jsonapi.settings')
    ->set('read_only', FALSE)
    ->save(TRUE);
  $uri = Url::fromUri('base:' . static::$postUri);

  // Test with a JSON file.
  $response = $this
    ->fileRequest($uri, '{"test":123}', [
    'Content-Disposition' => 'filename="example.json"',
  ]);
  $this
    ->assertResourceErrorResponse(422, PlainTextOutput::renderFromHtml("Unprocessable Entity: file validation failed.\nOnly files with the following extensions are allowed: <em class=\"placeholder\">txt</em>."), $uri, $response);

  // Make sure that no file was saved.
  $this
    ->assertEmpty(File::load(1));
  $this
    ->assertFalse(file_exists('public://foobar/example.txt'));
}