You are here

public function FileUploadTest::testFileUploadZeroByteFile in JSON:API 8.2

Tests using the file upload route with a zero byte file.

File

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

Class

FileUploadTest
Tests binary data file upload route.

Namespace

Drupal\Tests\jsonapi\Functional

Code

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

  // Test with a zero byte file.
  $response = $this
    ->fileRequest($uri, NULL);
  $this
    ->assertSame(201, $response
    ->getStatusCode());
  $expected = $this
    ->getExpectedDocument();

  // Modify the default expected data to account for the 0 byte file.
  $expected['data']['attributes']['filesize'] = 0;
  $this
    ->assertResponseData($expected, $response);

  // Check the actual file data.
  $this
    ->assertSame('', file_get_contents('public://foobar/example.txt'));
}