You are here

protected function NodeTest::getExpectedDocument in JSON:API 8

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

Class

NodeTest
JSON API integration test for the "Node" content entity type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getExpectedDocument() {
  $author = User::load($this->entity
    ->getOwnerId());
  $self_url = Url::fromUri('base:/jsonapi/node/camelids/' . $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' => 'node--camelids',
      'links' => [
        'self' => $self_url,
      ],
      'attributes' => [
        '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()), */
        'default_langcode' => TRUE,
        'langcode' => 'en',
        'nid' => 1,
        'path' => [
          'alias' => '/llama',
          'pid' => 1,
          'langcode' => 'en',
        ],
        'promote' => TRUE,
        'revision_log' => NULL,
        'revision_timestamp' => 123456789,
        // @todo uncomment this in https://www.drupal.org/project/jsonapi/issues/2929932

        /* 'revision_timestamp' => $this->formatExpectedTimestampItemValues(123456789), */

        // @todo Attempt to remove this in https://www.drupal.org/project/drupal/issues/2933518.
        'revision_translation_affected' => TRUE,
        'status' => TRUE,
        'sticky' => FALSE,
        'title' => 'Llama',
        'uuid' => $this->entity
          ->uuid(),
        'vid' => 1,
      ],
      'relationships' => [
        'type' => [
          'data' => [
            'id' => NodeType::load('camelids')
              ->uuid(),
            'type' => 'node_type--node_type',
          ],
          'links' => [
            'related' => $self_url . '/type',
            'self' => $self_url . '/relationships/type',
          ],
        ],
        'uid' => [
          'data' => [
            'id' => $author
              ->uuid(),
            'type' => 'user--user',
          ],
          'links' => [
            'related' => $self_url . '/uid',
            'self' => $self_url . '/relationships/uid',
          ],
        ],
        'revision_uid' => [
          'data' => [
            'id' => $author
              ->uuid(),
            'type' => 'user--user',
          ],
          'links' => [
            'related' => $self_url . '/revision_uid',
            'self' => $self_url . '/relationships/revision_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) {
    unset($normalization['data']['attributes']['revision_default']);
  }
  return $normalization;
}