You are here

public function FileUploadResourceTestBase::testFileUploadLargerFileSize in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php \Drupal\Tests\rest\Functional\FileUploadResourceTestBase::testFileUploadLargerFileSize()
  2. 9 core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php \Drupal\Tests\rest\Functional\FileUploadResourceTestBase::testFileUploadLargerFileSize()

Tests using the file upload route with a file size larger than allowed.

File

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

Class

FileUploadResourceTestBase
Tests binary data file upload route.

Namespace

Drupal\Tests\rest\Functional

Code

public function testFileUploadLargerFileSize() {

  // Set a limit of 50 bytes.
  $this->field
    ->setSetting('max_filesize', 50)
    ->save();
  $this
    ->refreshTestStateAfterRestConfigChange();
  $this
    ->initAuthentication();
  $this
    ->provisionResource([
    static::$format,
  ], static::$auth ? [
    static::$auth,
  ] : [], [
    'POST',
  ]);
  $this
    ->setUpAuthorization('POST');
  $uri = Url::fromUri('base:' . static::$postUri);

  // Generate a string larger than the 50 byte limit set.
  $response = $this
    ->fileRequest($uri, $this
    ->randomString(100));
  $this
    ->assertResourceErrorResponse(422, PlainTextOutput::renderFromHtml("Unprocessable Entity: file validation failed.\nThe file is <em class=\"placeholder\">100 bytes</em> exceeding the maximum file size of <em class=\"placeholder\">50 bytes</em>."), $response);

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