You are here

protected function TermTest::getExpectedDocument in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/TermTest.php \Drupal\Tests\jsonapi\Functional\TermTest::getExpectedDocument()

Returns the expected JSON:API document for the entity.

Return value

array A JSON:API response document.

Overrides ResourceTestBase::getExpectedDocument

See also

::createEntity()

1 call to TermTest::getExpectedDocument()
TermTest::testGetIndividualTermWithParent in core/modules/jsonapi/tests/src/Functional/TermTest.php
Tests GETting a term with a parent term other than the default <root> (0).

File

core/modules/jsonapi/tests/src/Functional/TermTest.php, line 113

Class

TermTest
JSON:API integration test for the "Term" content entity type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getExpectedDocument() {
  $self_url = Url::fromUri('base:/jsonapi/taxonomy_term/camelids/' . $this->entity
    ->uuid())
    ->setAbsolute()
    ->toString(TRUE)
    ->getGeneratedUrl();

  // We test with multiple parent terms, and combinations thereof.
  // @see ::createEntity()
  // @see ::testGetIndividual()
  // @see ::testGetIndividualTermWithParent()
  // @see ::providerTestGetIndividualTermWithParent()
  $parent_term_ids = [];
  for ($i = 0; $i < $this->entity
    ->get('parent')
    ->count(); $i++) {
    $parent_term_ids[$i] = (int) $this->entity
      ->get('parent')[$i]->target_id;
  }
  $expected_parent_normalization = FALSE;
  switch ($parent_term_ids) {
    case [
      0,
    ]:
      $expected_parent_normalization = [
        'data' => [
          [
            'id' => 'virtual',
            'type' => 'taxonomy_term--camelids',
            'meta' => [
              'links' => [
                'help' => [
                  'href' => 'https://www.drupal.org/docs/8/modules/json-api/core-concepts#virtual',
                  'meta' => [
                    'about' => "Usage and meaning of the 'virtual' resource identifier.",
                  ],
                ],
              ],
            ],
          ],
        ],
        'links' => [
          'related' => [
            'href' => $self_url . '/parent',
          ],
          'self' => [
            'href' => $self_url . '/relationships/parent',
          ],
        ],
      ];
      break;
    case [
      2,
    ]:
      $expected_parent_normalization = [
        'data' => [
          [
            'id' => Term::load(2)
              ->uuid(),
            'type' => 'taxonomy_term--camelids',
          ],
        ],
        'links' => [
          'related' => [
            'href' => $self_url . '/parent',
          ],
          'self' => [
            'href' => $self_url . '/relationships/parent',
          ],
        ],
      ];
      break;
    case [
      0,
      2,
    ]:
      $expected_parent_normalization = [
        'data' => [
          [
            'id' => 'virtual',
            'type' => 'taxonomy_term--camelids',
            'meta' => [
              'links' => [
                'help' => [
                  'href' => 'https://www.drupal.org/docs/8/modules/json-api/core-concepts#virtual',
                  'meta' => [
                    'about' => "Usage and meaning of the 'virtual' resource identifier.",
                  ],
                ],
              ],
            ],
          ],
          [
            'id' => Term::load(2)
              ->uuid(),
            'type' => 'taxonomy_term--camelids',
          ],
        ],
        'links' => [
          'related' => [
            'href' => $self_url . '/parent',
          ],
          'self' => [
            'href' => $self_url . '/relationships/parent',
          ],
        ],
      ];
      break;
    case [
      3,
      2,
    ]:
      $expected_parent_normalization = [
        'data' => [
          [
            'id' => Term::load(3)
              ->uuid(),
            'type' => 'taxonomy_term--camelids',
          ],
          [
            'id' => Term::load(2)
              ->uuid(),
            'type' => 'taxonomy_term--camelids',
          ],
        ],
        'links' => [
          'related' => [
            'href' => $self_url . '/parent',
          ],
          'self' => [
            'href' => $self_url . '/relationships/parent',
          ],
        ],
      ];
      break;
  }
  return [
    'jsonapi' => [
      'meta' => [
        'links' => [
          'self' => [
            'href' => 'http://jsonapi.org/format/1.0/',
          ],
        ],
      ],
      'version' => '1.0',
    ],
    'links' => [
      'self' => [
        'href' => $self_url,
      ],
    ],
    'data' => [
      'id' => $this->entity
        ->uuid(),
      'type' => 'taxonomy_term--camelids',
      'links' => [
        'self' => [
          'href' => $self_url,
        ],
      ],
      'attributes' => [
        'changed' => (new \DateTime())
          ->setTimestamp($this->entity
          ->getChangedTime())
          ->setTimezone(new \DateTimeZone('UTC'))
          ->format(\DateTime::RFC3339),
        'default_langcode' => TRUE,
        'description' => [
          'value' => 'It is a little known fact that llamas cannot count higher than seven.',
          'format' => NULL,
          'processed' => "<p>It is a little known fact that llamas cannot count higher than seven.</p>\n",
        ],
        'langcode' => 'en',
        'name' => 'Llama',
        'path' => [
          'alias' => '/llama',
          'pid' => 1,
          'langcode' => 'en',
        ],
        'weight' => 0,
        'drupal_internal__tid' => 1,
        'status' => TRUE,
        'drupal_internal__revision_id' => 1,
        'revision_created' => (new \DateTime())
          ->setTimestamp($this->entity
          ->getRevisionCreationTime())
          ->setTimezone(new \DateTimeZone('UTC'))
          ->format(\DateTime::RFC3339),
        'revision_log_message' => NULL,
        // @todo Attempt to remove this in https://www.drupal.org/project/drupal/issues/2933518.
        'revision_translation_affected' => TRUE,
      ],
      'relationships' => [
        'parent' => $expected_parent_normalization,
        'vid' => [
          'data' => [
            'id' => Vocabulary::load('camelids')
              ->uuid(),
            'type' => 'taxonomy_vocabulary--taxonomy_vocabulary',
          ],
          'links' => [
            'related' => [
              'href' => $self_url . '/vid',
            ],
            'self' => [
              'href' => $self_url . '/relationships/vid',
            ],
          ],
        ],
        'revision_user' => [
          'data' => NULL,
          'links' => [
            'related' => [
              'href' => $self_url . '/revision_user',
            ],
            'self' => [
              'href' => $self_url . '/relationships/revision_user',
            ],
          ],
        ],
      ],
    ],
  ];
}