function FileUploadResourceTestBase::testFileUploadInvalidFileType

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

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

File

core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php, line 456

Class

FileUploadResourceTestBase
Tests binary data file upload route.

Namespace

Drupal\Tests\rest\Functional

Code

public function testFileUploadInvalidFileType() {
    $this->initAuthentication();
    $this->provisionResource([
        static::$format,
    ], static::$auth ? [
        static::$auth,
    ] : [], [
        'POST',
    ]);
    $this->setUpAuthorization('POST');
    $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>."), $response);
    // Make sure that no file was saved.
    $this->assertEmpty(File::load(1));
    $this->assertFileNotExists('public://foobar/example.txt');
}

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