function FileUploadTest::getExpectedDocument

Same name in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/FileUploadTest.php \Drupal\Tests\jsonapi\Functional\FileUploadTest::getExpectedDocument()
  2. 8.9.x core/modules/jsonapi/tests/src/Functional/FileUploadTest.php \Drupal\Tests\jsonapi\Functional\FileUploadTest::getExpectedDocument()
  3. 11.x core/modules/jsonapi/tests/src/Functional/FileUploadTest.php \Drupal\Tests\jsonapi\Functional\FileUploadTest::getExpectedDocument()

Returns the expected JSON:API document for the expected file entity.

Parameters

int $fid: The file ID to load and create a JSON:API document for.

string $expected_filename: The expected filename for the stored file.

bool $expected_as_filename: Whether the expected filename should be the filename property too.

bool $expected_status: The expected file status. Defaults to FALSE.

Return value

array A JSON:API response document.

Overrides ResourceTestBase::getExpectedDocument

7 calls to FileUploadTest::getExpectedDocument()
FileUploadTest::testFileUploadMaliciousExtension in core/modules/jsonapi/tests/src/Functional/FileUploadTest.php
Tests using the file upload POST route with malicious extensions.
FileUploadTest::testFileUploadNoConfiguration in core/modules/jsonapi/tests/src/Functional/FileUploadTest.php
Tests using the file upload POST route no configuration.
FileUploadTest::testFileUploadStrippedFilePath in core/modules/jsonapi/tests/src/Functional/FileUploadTest.php
Tests using the file upload route with any path prefixes being stripped.
FileUploadTest::testFileUploadUnicodeFilename in core/modules/jsonapi/tests/src/Functional/FileUploadTest.php
Tests using the file upload route with a unicode file name.
FileUploadTest::testFileUploadZeroByteFile in core/modules/jsonapi/tests/src/Functional/FileUploadTest.php
Tests using the file upload route with a zero byte file.

... See full list

File

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

Class

FileUploadTest
Tests binary data file upload route.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getExpectedDocument($fid = 1, $expected_filename = 'example.txt', $expected_as_filename = FALSE, $expected_status = FALSE) {
    $author = User::load($this->account
        ->id());
    $file = File::load($fid);
    $this->assertInstanceOf(File::class, $file);
    $self_url = Url::fromUri('base:/jsonapi/file/file/' . $file->uuid())
        ->setAbsolute()
        ->toString(TRUE)
        ->getGeneratedUrl();
    return [
        'jsonapi' => [
            'meta' => [
                'links' => [
                    'self' => [
                        'href' => 'http://jsonapi.org/format/1.0/',
                    ],
                ],
            ],
            'version' => '1.0',
        ],
        'links' => [
            'self' => [
                'href' => $self_url,
            ],
        ],
        'data' => [
            'id' => $file->uuid(),
            'type' => 'file--file',
            'links' => [
                'self' => [
                    'href' => $self_url,
                ],
            ],
            'attributes' => [
                'created' => (new \DateTime())->setTimestamp($file->getCreatedTime())
                    ->setTimezone(new \DateTimeZone('UTC'))
                    ->format(\DateTime::RFC3339),
                'changed' => (new \DateTime())->setTimestamp($file->getChangedTime())
                    ->setTimezone(new \DateTimeZone('UTC'))
                    ->format(\DateTime::RFC3339),
                'filemime' => 'text/plain',
                'filename' => $expected_as_filename ? $expected_filename : 'example.txt',
                'filesize' => strlen($this->testFileData),
                'langcode' => 'en',
                'status' => $expected_status,
                'uri' => [
                    'value' => 'public://foobar/' . $expected_filename,
                    'url' => base_path() . $this->siteDirectory . '/files/foobar/' . rawurlencode($expected_filename),
                ],
                'drupal_internal__fid' => (int) $file->id(),
            ],
            'relationships' => [
                'uid' => [
                    'data' => [
                        'id' => $author->uuid(),
                        'meta' => [
                            'drupal_internal__target_id' => (int) $author->id(),
                        ],
                        'type' => 'user--user',
                    ],
                    'links' => [
                        'related' => [
                            'href' => $self_url . '/uid',
                        ],
                        'self' => [
                            'href' => $self_url . '/relationships/uid',
                        ],
                    ],
                ],
            ],
        ],
    ];
}

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