function FileUploadTest::testFileUploadInvalidFileType

Same name in other branches
  1. 8.9.x core/modules/jsonapi/tests/src/Functional/FileUploadTest.php \Drupal\Tests\jsonapi\Functional\FileUploadTest::testFileUploadInvalidFileType()
  2. 10 core/modules/jsonapi/tests/src/Functional/FileUploadTest.php \Drupal\Tests\jsonapi\Functional\FileUploadTest::testFileUploadInvalidFileType()
  3. 11.x core/modules/jsonapi/tests/src/Functional/FileUploadTest.php \Drupal\Tests\jsonapi\Functional\FileUploadTest::testFileUploadInvalidFileType()

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

File

core/modules/jsonapi/tests/src/Functional/FileUploadTest.php, line 567

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->assertFileDoesNotExist('public://foobar/example.txt');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.