function MediaResourceTestBase::uploadFile

Same name in other branches
  1. 9 core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php \Drupal\Tests\media\Functional\Rest\MediaResourceTestBase::uploadFile()
  2. 8.9.x core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php \Drupal\Tests\media\Functional\Rest\MediaResourceTestBase::uploadFile()
  3. 11.x core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php \Drupal\Tests\media\Functional\Rest\MediaResourceTestBase::uploadFile()

Tests the 'file_upload' REST resource plugin.

This test duplicates some of the 'file_upload' REST resource plugin test coverage.

See also

\Drupal\Tests\rest\Functional\FileUploadResourceTestBase

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 350

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);
    // Make sure the role save below properly invalidates cache tags.
    $this->refreshVariables();
    // 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();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.