You are here

public function TermTest::testGetIndividualTermWithParent 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::testGetIndividualTermWithParent()

Tests GETting a term with a parent term other than the default <root> (0).

@dataProvider providerTestGetIndividualTermWithParent

See also

::getExpectedNormalizedEntity()

File

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

Class

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

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testGetIndividualTermWithParent(array $parent_term_ids) {

  // Create all possible parent terms.
  Term::create([
    'vid' => Vocabulary::load('camelids')
      ->id(),
  ])
    ->setName('Lamoids')
    ->save();
  Term::create([
    'vid' => Vocabulary::load('camelids')
      ->id(),
  ])
    ->setName('Wimoids')
    ->save();

  // Modify the entity under test to use the provided parent terms.
  $this->entity
    ->set('parent', $parent_term_ids)
    ->save();

  // @todo Remove line below in favor of commented line in https://www.drupal.org/project/drupal/issues/2878463.
  $url = Url::fromRoute(sprintf('jsonapi.%s.individual', static::$resourceTypeName), [
    'entity' => $this->entity
      ->uuid(),
  ]);

  // $url = $this->entity->toUrl('jsonapi');
  $request_options = [];
  $request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
  $request_options = NestedArray::mergeDeep($request_options, $this
    ->getAuthenticationRequestOptions());
  $this
    ->setUpAuthorization('GET');
  $response = $this
    ->request('GET', $url, $request_options);
  $this
    ->assertSameDocument($this
    ->getExpectedDocument(), Json::decode($response
    ->getBody()));
}