You are here

protected function MediaTest::getExpectedDocument in JSON:API 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/MediaTest.php \Drupal\Tests\jsonapi\Functional\MediaTest::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/MediaTest.php, line 151

Class

MediaTest
JSON API integration test for the "Media" content entity type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getExpectedDocument() {
  $file = File::load(1);
  $thumbnail = File::load(3);
  $author = User::load($this->entity
    ->getOwnerId());
  $self_url = Url::fromUri('base:/jsonapi/media/camelids/' . $this->entity
    ->uuid())
    ->setAbsolute()
    ->toString(TRUE)
    ->getGeneratedUrl();
  $data = [
    'jsonapi' => [
      'meta' => [
        'links' => [
          'self' => 'http://jsonapi.org/format/1.0/',
        ],
      ],
      'version' => '1.0',
    ],
    'links' => [
      'self' => $self_url,
    ],
    'data' => [
      'id' => $this->entity
        ->uuid(),
      'type' => 'media--camelids',
      'links' => [
        'self' => $self_url,
      ],
      'attributes' => [
        'mid' => 1,
        'vid' => 1,
        'langcode' => 'en',
        'name' => 'Llama',
        'status' => TRUE,
        'created' => 123456789,
        // @todo uncomment this in https://www.drupal.org/project/jsonapi/issues/2929932

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

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

        /* 'revision_created' => $this->formatExpectedTimestampItemValues((int) $this->entity->getRevisionCreationTime()), */
        'default_langcode' => TRUE,
        'revision_log_message' => NULL,
        // @todo Attempt to remove this in https://www.drupal.org/project/drupal/issues/2933518.
        'revision_translation_affected' => TRUE,
        'uuid' => $this->entity
          ->uuid(),
      ],
      'relationships' => [
        'field_media_file' => [
          'data' => [
            'id' => $file
              ->uuid(),
            'meta' => [
              'description' => NULL,
              'display' => NULL,
            ],
            'type' => 'file--file',
          ],
          'links' => [
            'related' => $self_url . '/field_media_file',
            'self' => $self_url . '/relationships/field_media_file',
          ],
        ],
        'thumbnail' => [
          'data' => [
            'id' => $thumbnail
              ->uuid(),
            'meta' => [
              'alt' => '',
              'width' => 180,
              'height' => 180,
              'title' => NULL,
            ],
            'type' => 'file--file',
          ],
          'links' => [
            'related' => $self_url . '/thumbnail',
            'self' => $self_url . '/relationships/thumbnail',
          ],
        ],
        'bundle' => [
          'data' => [
            'id' => MediaType::load('camelids')
              ->uuid(),
            'type' => 'media_type--media_type',
          ],
          'links' => [
            'related' => $self_url . '/bundle',
            'self' => $self_url . '/relationships/bundle',
          ],
        ],
        'uid' => [
          'data' => [
            'id' => $author
              ->uuid(),
            'type' => 'user--user',
          ],
          'links' => [
            'related' => $self_url . '/uid',
            'self' => $self_url . '/relationships/uid',
          ],
        ],
        'revision_user' => [
          'data' => [
            'id' => $author
              ->uuid(),
            'type' => 'user--user',
          ],
          'links' => [
            'related' => $self_url . '/revision_user',
            'self' => $self_url . '/relationships/revision_user',
          ],
        ],
      ],
    ],
  ];

  // @todo Make this unconditional when JSON:API requires Drupal 8.6 or newer.
  if (floatval(\Drupal::VERSION) < 8.6) {
    $data['data']['relationships']['thumbnail']['data']['meta']['alt'] = 'Thumbnail';
    $data['data']['relationships']['thumbnail']['data']['meta']['title'] = 'Llama';
  }
  return $data;
}