You are here

protected function CommentTest::getExpectedDocument in JSON:API 8

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

Class

CommentTest
JSON API integration test for the "Comment" content entity type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getExpectedDocument() {
  $self_url = Url::fromUri('base:/jsonapi/comment/comment/' . $this->entity
    ->uuid())
    ->setAbsolute()
    ->toString(TRUE)
    ->getGeneratedUrl();
  $author = User::load($this->entity
    ->getOwnerId());
  $document = [
    'jsonapi' => [
      'meta' => [
        'links' => [
          'self' => 'http://jsonapi.org/format/1.0/',
        ],
      ],
      'version' => '1.0',
    ],
    'links' => [
      'self' => $self_url,
    ],
    'data' => [
      'id' => $this->entity
        ->uuid(),
      'type' => 'comment--comment',
      'links' => [
        'self' => $self_url,
      ],
      'attributes' => [
        'cid' => 1,
        '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()), */
        'comment_body' => [
          'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
          'format' => 'plain_text',
          'processed' => "<p>The name &quot;llama&quot; was adopted by European settlers from native Peruvians.</p>\n",
        ],
        'default_langcode' => TRUE,
        'entity_type' => 'entity_test',
        'field_name' => 'comment',
        'homepage' => NULL,
        'langcode' => 'en',
        'name' => NULL,
        'status' => TRUE,
        'subject' => 'Llama',
        'thread' => '01/',
        'uuid' => $this->entity
          ->uuid(),
      ],
      'relationships' => [
        'uid' => [
          'data' => [
            'id' => $author
              ->uuid(),
            'type' => 'user--user',
          ],
          'links' => [
            'related' => $self_url . '/uid',
            'self' => $self_url . '/relationships/uid',
          ],
        ],
        'comment_type' => [
          'data' => [
            'id' => CommentType::load('comment')
              ->uuid(),
            'type' => 'comment_type--comment_type',
          ],
          'links' => [
            'related' => $self_url . '/comment_type',
            'self' => $self_url . '/relationships/comment_type',
          ],
        ],
        'entity_id' => [
          'data' => [
            'id' => EntityTest::load(1)
              ->uuid(),
            'type' => 'entity_test--bar',
          ],
          'links' => [
            'related' => $self_url . '/entity_id',
            'self' => $self_url . '/relationships/entity_id',
          ],
        ],
        'pid' => [
          'data' => NULL,
          'links' => [
            'related' => $self_url . '/pid',
            'self' => $self_url . '/relationships/pid',
          ],
        ],
      ],
    ],
  ];

  // @todo Remove this when JSON API requires Drupal 8.5 or newer.
  if (floatval(\Drupal::VERSION) < 8.5) {
    unset($document['data']['attributes']['comment_body']['processed']);
  }
  return $document;
}