You are here

public function FileUploadTest::testPostFileUploadDuplicateFile in JSON:API 8.2

Tests using the file upload POST route with a duplicate file name.

A new file should be created with a suffixed name.

File

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

Class

FileUploadTest
Tests binary data file upload route.

Namespace

Drupal\Tests\jsonapi\Functional

Code

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

  // This request will have the default 'application/octet-stream' content
  // type header.
  $response = $this
    ->fileRequest($uri, $this->testFileData);
  $this
    ->assertSame(201, $response
    ->getStatusCode());

  // Make the same request again. The file should be saved as a new file
  // entity that has the same file name but a suffixed file URI.
  $response = $this
    ->fileRequest($uri, $this->testFileData);
  $this
    ->assertSame(201, $response
    ->getStatusCode());

  // Loading expected normalized data for file 2, the duplicate file.
  $expected = $this
    ->getExpectedDocument(2, 'example_0.txt');
  $this
    ->assertResponseData($expected, $response);

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