You are here

protected function FileTest::getExpectedDocument in JSON:API 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/FileTest.php \Drupal\Tests\jsonapi\Functional\FileTest::getExpectedDocument()

Returns the expected JSON API document for the entity.

Return value

array A JSON API response document.

Overrides ResourceTestBase::getExpectedDocument

See also

::createEntity()

File

tests/src/Functional/FileTest.php, line 127

Class

FileTest
JSON API integration test for the "File" content entity type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getExpectedDocument() {
  $self_url = Url::fromUri('base:/jsonapi/file/file/' . $this->entity
    ->uuid())
    ->setAbsolute()
    ->toString(TRUE)
    ->getGeneratedUrl();
  $normalization = [
    'jsonapi' => [
      'meta' => [
        'links' => [
          'self' => 'http://jsonapi.org/format/1.0/',
        ],
      ],
      'version' => '1.0',
    ],
    'links' => [
      'self' => $self_url,
    ],
    'data' => [
      'id' => $this->entity
        ->uuid(),
      'type' => 'file--file',
      'links' => [
        'self' => $self_url,
      ],
      'attributes' => [
        'changed' => $this->entity
          ->getChangedTime(),
        // @todo uncomment this in https://www.drupal.org/project/jsonapi/issues/2929932

        /* 'changed' => $this->formatExpectedTimestampItemValues($this->entity->getChangedTime()), */
        'created' => (int) $this->entity
          ->getCreatedTime(),
        // @todo uncomment this in https://www.drupal.org/project/jsonapi/issues/2929932

        /* 'created' => $this->formatExpectedTimestampItemValues((int) $this->entity->getCreatedTime()), */
        'fid' => 1,
        'filemime' => 'text/plain',
        'filename' => 'drupal.txt',
        'filesize' => (int) $this->entity
          ->getSize(),
        'langcode' => 'en',
        'status' => TRUE,
        // @todo Decide what to do with this in https://www.drupal.org/project/jsonapi/issues/2926463
        'url' => base_path() . $this->siteDirectory . '/files/drupal.txt',
        'uri' => [
          'url' => base_path() . $this->siteDirectory . '/files/drupal.txt',
          'value' => 'public://drupal.txt',
        ],
        'uuid' => $this->entity
          ->uuid(),
      ],
      'relationships' => [
        'uid' => [
          'data' => [
            'id' => $this->author
              ->uuid(),
            'type' => 'user--user',
          ],
          'links' => [
            'related' => $self_url . '/uid',
            'self' => $self_url . '/relationships/uid',
          ],
        ],
      ],
    ],
  ];

  // @todo Remove this modification when JSON API requires Drupal 8.5 or newer, and do an early return above instead.
  if (floatval(\Drupal::VERSION) < 8.5) {
    $normalization['data']['attributes']['uri'] = $normalization['data']['attributes']['uri']['url'];
  }
  return $normalization;
}