You are here

protected function FileUploadTest::getExpectedDocument in Drupal 8

Same name and namespace in other branches
  1. 9 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::testFileUploadNoExtensionSetting in core/modules/jsonapi/tests/src/Functional/FileUploadTest.php
Tests using the file upload POST route no extension configured.
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 771

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);
  $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(),
            'type' => 'user--user',
          ],
          'links' => [
            'related' => [
              'href' => $self_url . '/uid',
            ],
            'self' => [
              'href' => $self_url . '/relationships/uid',
            ],
          ],
        ],
      ],
    ],
  ];
}