You are here

protected function TermTest::getExpectedDocument in Drupal 10

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

File

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

Class

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

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getExpectedDocument() {
  $base_url = Url::fromUri('base:/jsonapi/taxonomy_term/camelids/' . $this->entity
    ->uuid())
    ->setAbsolute();
  $self_url = clone $base_url;
  $version_identifier = 'id:' . $this->entity
    ->getRevisionId();
  $self_url = $self_url
    ->setOption('query', [
    'resourceVersion' => $version_identifier,
  ]);
  $version_query_string = '?resourceVersion=' . urlencode($version_identifier);

  // 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' => $base_url
              ->toString() . '/parent' . $version_query_string,
          ],
          'self' => [
            'href' => $base_url
              ->toString() . '/relationships/parent' . $version_query_string,
          ],
        ],
      ];
      break;
    case [
      2,
    ]:
      $expected_parent_normalization = [
        'data' => [
          [
            'id' => Term::load(2)
              ->uuid(),
            'meta' => [
              'drupal_internal__target_id' => 2,
            ],
            'type' => 'taxonomy_term--camelids',
          ],
        ],
        'links' => [
          'related' => [
            'href' => $base_url
              ->toString() . '/parent' . $version_query_string,
          ],
          'self' => [
            'href' => $base_url
              ->toString() . '/relationships/parent' . $version_query_string,
          ],
        ],
      ];
      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(),
            'meta' => [
              'drupal_internal__target_id' => 2,
            ],
            'type' => 'taxonomy_term--camelids',
          ],
        ],
        'links' => [
          'related' => [
            'href' => $base_url
              ->toString() . '/parent' . $version_query_string,
          ],
          'self' => [
            'href' => $base_url
              ->toString() . '/relationships/parent' . $version_query_string,
          ],
        ],
      ];
      break;
    case [
      3,
      2,
    ]:
      $expected_parent_normalization = [
        'data' => [
          [
            'id' => Term::load(3)
              ->uuid(),
            'meta' => [
              'drupal_internal__target_id' => 3,
            ],
            'type' => 'taxonomy_term--camelids',
          ],
          [
            'id' => Term::load(2)
              ->uuid(),
            'meta' => [
              'drupal_internal__target_id' => 2,
            ],
            'type' => 'taxonomy_term--camelids',
          ],
        ],
        'links' => [
          'related' => [
            'href' => $base_url
              ->toString() . '/parent' . $version_query_string,
          ],
          'self' => [
            'href' => $base_url
              ->toString() . '/relationships/parent' . $version_query_string,
          ],
        ],
      ];
      break;
  }
  return [
    'jsonapi' => [
      'meta' => [
        'links' => [
          'self' => [
            'href' => 'http://jsonapi.org/format/1.0/',
          ],
        ],
      ],
      'version' => '1.0',
    ],
    'links' => [
      'self' => [
        'href' => $base_url
          ->toString(),
      ],
    ],
    'data' => [
      'id' => $this->entity
        ->uuid(),
      'type' => 'taxonomy_term--camelids',
      'links' => [
        'self' => [
          'href' => $self_url
            ->toString(),
        ],
      ],
      '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(),
            'meta' => [
              'drupal_internal__target_id' => 'camelids',
            ],
            'type' => 'taxonomy_vocabulary--taxonomy_vocabulary',
          ],
          'links' => [
            'related' => [
              'href' => $base_url
                ->toString() . '/vid' . $version_query_string,
            ],
            'self' => [
              'href' => $base_url
                ->toString() . '/relationships/vid' . $version_query_string,
            ],
          ],
        ],
        'revision_user' => [
          'data' => NULL,
          'links' => [
            'related' => [
              'href' => $base_url
                ->toString() . '/revision_user' . $version_query_string,
            ],
            'self' => [
              'href' => $base_url
                ->toString() . '/relationships/revision_user' . $version_query_string,
            ],
          ],
        ],
      ],
    ],
  ];
}