You are here

public function FileUploadTest::testPostFileUploadAndUseInSingleRequest in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Functional/FileUploadTest.php \Drupal\Tests\jsonapi\Functional\FileUploadTest::testPostFileUploadAndUseInSingleRequest()

Tests using the 'file upload and "use" file in single request" POST route.

File

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

Class

FileUploadTest
Tests binary data file upload route.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testPostFileUploadAndUseInSingleRequest() {

  // Update the test entity so it already has a file. This allows verifying
  // that this route appends files, and does not replace them.
  mkdir('public://foobar');
  file_put_contents('public://foobar/existing.txt', $this->testFileData);
  $existing_file = File::create([
    'uri' => 'public://foobar/existing.txt',
  ]);
  $existing_file
    ->setOwnerId($this->account
    ->id());
  $existing_file
    ->setPermanent();
  $existing_file
    ->save();
  $this->entity
    ->set('field_rest_file_test', [
    'target_id' => $existing_file
      ->id(),
  ])
    ->save();
  $uri = Url::fromUri('base:' . '/jsonapi/entity_test/entity_test/' . $this->entity
    ->uuid() . '/field_rest_file_test');

  // DX: 405 when read-only mode is enabled.
  $response = $this
    ->fileRequest($uri, $this->testFileData);
  $this
    ->assertResourceErrorResponse(405, sprintf("JSON:API is configured to accept only read operations. Site administrators can configure this at %s.", Url::fromUri('base:/admin/config/services/jsonapi')
    ->setAbsolute()
    ->toString(TRUE)
    ->getGeneratedUrl()), $uri, $response);
  $this
    ->assertSame([
    'GET',
  ], $response
    ->getHeader('Allow'));
  $this
    ->config('jsonapi.settings')
    ->set('read_only', FALSE)
    ->save(TRUE);

  // DX: 403 when unauthorized.
  $response = $this
    ->fileRequest($uri, $this->testFileData);
  $this
    ->assertResourceErrorResponse(403, $this
    ->getExpectedUnauthorizedAccessMessage('PATCH'), $uri, $response);
  $this
    ->setUpAuthorization('PATCH');

  // 404 when the field name is invalid.
  $invalid_uri = Url::fromUri($uri
    ->getUri() . '_invalid');
  $response = $this
    ->fileRequest($invalid_uri, $this->testFileData);
  $this
    ->assertResourceErrorResponse(404, 'Field "field_rest_file_test_invalid" does not exist.', $invalid_uri, $response);

  // This request fails despite the upload succeeding, because we're not
  // allowed to view the entity we're uploading to.
  $response = $this
    ->fileRequest($uri, $this->testFileData);
  $this
    ->assertResourceErrorResponse(403, $this
    ->getExpectedUnauthorizedAccessMessage('GET'), $uri, $response, FALSE, [
    '4xx-response',
    'http_response',
  ], [
    'url.site',
    'user.permissions',
  ]);
  $this
    ->setUpAuthorization('GET');

  // Reuploading the same file will result in the file being uploaded twice
  // and referenced twice.
  $response = $this
    ->fileRequest($uri, $this->testFileData);
  $this
    ->assertSame(200, $response
    ->getStatusCode());
  $expected = [
    'jsonapi' => [
      'meta' => [
        'links' => [
          'self' => [
            'href' => 'http://jsonapi.org/format/1.0/',
          ],
        ],
      ],
      'version' => '1.0',
    ],
    'links' => [
      'self' => [
        'href' => Url::fromUri('base:/jsonapi/entity_test/entity_test/' . $this->entity
          ->uuid() . '/field_rest_file_test')
          ->setAbsolute(TRUE)
          ->toString(),
      ],
    ],
    'data' => [
      0 => $this
        ->getExpectedDocument(1, 'existing.txt', TRUE, TRUE)['data'],
      1 => $this
        ->getExpectedDocument(2, 'example.txt', TRUE, TRUE)['data'],
      2 => $this
        ->getExpectedDocument(3, 'example_0.txt', FALSE, TRUE)['data'],
    ],
  ];
  $this
    ->assertResponseData($expected, $response);

  // The response document received for the POST request is identical to the
  // response document received by GETting the same URL.
  $request_options = [];
  $request_options[RequestOptions::HEADERS]['Content-Type'] = 'application/vnd.api+json';
  $request_options = NestedArray::mergeDeep($request_options, $this
    ->getAuthenticationRequestOptions());
  $response = $this
    ->request('GET', $uri, $request_options);
  $this
    ->assertSame(200, $response
    ->getStatusCode());
  $this
    ->assertResponseData($expected, $response);

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