You are here

protected function MediaResourceTestBase::uploadFile in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php \Drupal\Tests\media\Functional\Rest\MediaResourceTestBase::uploadFile()
  2. 9 core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php \Drupal\Tests\media\Functional\Rest\MediaResourceTestBase::uploadFile()

This duplicates some of the 'file_upload' REST resource plugin test coverage, to be able to test it on a concrete use case.

1 call to MediaResourceTestBase::uploadFile()
MediaResourceTestBase::testPost in core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php
Tests a POST request for an entity, plus edge cases to ensure good DX.

File

core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php, line 334

Class

MediaResourceTestBase

Namespace

Drupal\Tests\media\Functional\Rest

Code

protected function uploadFile() {

  // Enable the 'file_upload' REST resource for the current format + auth.
  $this->resourceConfigStorage
    ->create([
    'id' => 'file.upload',
    'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
    'configuration' => [
      'methods' => [
        'POST',
      ],
      'formats' => [
        static::$format,
      ],
      'authentication' => isset(static::$auth) ? [
        static::$auth,
      ] : [],
    ],
    'status' => TRUE,
  ])
    ->save();
  $this
    ->refreshTestStateAfterRestConfigChange();
  $this
    ->initAuthentication();

  // POST to create a File entity.
  $url = Url::fromUri('base:file/upload/media/camelids/field_media_file');
  $url
    ->setOption('query', [
    '_format' => static::$format,
  ]);
  $request_options = [];
  $request_options[RequestOptions::HEADERS] = [
    // Set the required (and only accepted) content type for the request.
    'Content-Type' => 'application/octet-stream',
    // Set the required Content-Disposition header for the file name.
    'Content-Disposition' => 'file; filename="drupal rocks 🤘.txt"',
  ];
  $request_options[RequestOptions::BODY] = 'Drupal is the best!';
  $request_options = NestedArray::mergeDeep($request_options, $this
    ->getAuthenticationRequestOptions('POST'));
  $response = $this
    ->request('POST', $url, $request_options);
  $this
    ->assertResourceErrorResponse(403, $this
    ->getExpectedUnauthorizedAccessMessage('POST'), $response);

  // Grant necessary permission, retry.
  $this
    ->grantPermissionsToTestedRole([
    'create camelids media',
  ]);
  $response = $this
    ->request('POST', $url, $request_options);
  $this
    ->assertSame(201, $response
    ->getStatusCode());
  $expected = $this
    ->getExpectedNormalizedFileEntity();
  static::recursiveKSort($expected);
  $actual = $this->serializer
    ->decode((string) $response
    ->getBody(), static::$format);
  static::recursiveKSort($actual);
  $this
    ->assertSame($expected, $actual);

  // To still run the complete test coverage for POSTing a Media entity, we
  // must revoke the additional permissions that we granted.
  $role = Role::load(static::$auth ? RoleInterface::AUTHENTICATED_ID : RoleInterface::ANONYMOUS_ID);
  $role
    ->revokePermission('create camelids media');
  $role
    ->trustData()
    ->save();
}