You are here

protected function FileUploadTest::getExpectedDocument in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Functional/FileUploadTest.php \Drupal\Tests\jsonapi\Functional\FileUploadTest::getExpectedDocument()
  2. 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.

File

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

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