protected function TermTest::getExpectedDocument in JSON:API 8
Same name and namespace in other branches
- 8.2 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 tests/
src/ Functional/ TermTest.php - Tests GETting a term with a parent term other than the default <root> (0).
File
- tests/
src/ Functional/ TermTest.php, line 115
Class
- TermTest
- JSON API integration test for the "Term" content entity type.
Namespace
Drupal\Tests\jsonapi\FunctionalCode
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' => $self_url . '/parent',
'self' => $self_url . '/relationships/parent',
],
];
break;
case [
2,
]:
$expected_parent_normalization = [
'data' => [
[
'id' => Term::load(2)
->uuid(),
'type' => 'taxonomy_term--camelids',
],
],
'links' => [
'related' => $self_url . '/parent',
'self' => $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' => $self_url . '/parent',
'self' => $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' => $self_url . '/parent',
'self' => $self_url . '/relationships/parent',
],
];
break;
}
// @todo Remove this when JSON API requires Drupal 8.6 or newer.
if (floatval(\Drupal::VERSION) < 8.6) {
$expected_parent_normalization = [
'data' => [],
'links' => [
'related' => $self_url . '/parent',
'self' => $self_url . '/relationships/parent',
],
];
}
$expected_document = [
'jsonapi' => [
'meta' => [
'links' => [
'self' => 'http://jsonapi.org/format/1.0/',
],
],
'version' => '1.0',
],
'links' => [
'self' => $self_url,
],
'data' => [
'id' => $this->entity
->uuid(),
'type' => 'taxonomy_term--camelids',
'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()), */
'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',
],
'tid' => 1,
'uuid' => $this->entity
->uuid(),
'weight' => 0,
],
'relationships' => [
'parent' => $expected_parent_normalization,
'vid' => [
'data' => [
'id' => Vocabulary::load('camelids')
->uuid(),
'type' => 'taxonomy_vocabulary--taxonomy_vocabulary',
],
'links' => [
'related' => $self_url . '/vid',
'self' => $self_url . '/relationships/vid',
],
],
],
],
];
if (floatval(\Drupal::VERSION) >= 8.6) {
$expected_document['data']['attributes']['status'] = TRUE;
}
return $expected_document;
}