You are here

public function JsonApiFunctionalMultilingualTest::testReadMultilingual in JSON:API 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/JsonApiFunctionalMultilingualTest.php \Drupal\Tests\jsonapi\Functional\JsonApiFunctionalMultilingualTest::testReadMultilingual()

Tests reading multilingual content.

File

tests/src/Functional/JsonApiFunctionalMultilingualTest.php, line 49

Class

JsonApiFunctionalMultilingualTest
Tests JSON API multilingual support.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testReadMultilingual() {
  $this
    ->createDefaultContent(5, 5, TRUE, TRUE, static::IS_MULTILINGUAL, FALSE);

  // Test reading an individual entity.
  $output = Json::decode($this
    ->drupalGet('/ca/jsonapi/node/article/' . $this->nodes[0]
    ->uuid(), [
    'query' => [
      'include' => 'field_tags,field_image',
    ],
  ]));
  $this
    ->assertEquals($this->nodes[0]
    ->getTranslation('ca')
    ->getTitle(), $output['data']['attributes']['title']);
  $included_tags = array_filter($output['included'], function ($entry) {
    return $entry['type'] === 'taxonomy_term--tags';
  });
  $tag_name = $this->nodes[0]
    ->get('field_tags')->entity
    ->getTranslation('ca')
    ->getName();

  // TODO figure out how to fetcht the alt text of an image.
  $this
    ->assertEquals($tag_name, reset($included_tags)['attributes']['name']);
  $output = Json::decode($this
    ->drupalGet('/ca/jsonapi/node/article/' . $this->nodes[0]
    ->uuid()));
  $this
    ->assertEquals($this->nodes[0]
    ->getTranslation('ca')
    ->getTitle(), $output['data']['attributes']['title']);

  // Test reading a collection of entities.
  $output = Json::decode($this
    ->drupalGet('/ca/jsonapi/node/article'));
  $this
    ->assertEquals($this->nodes[0]
    ->getTranslation('ca')
    ->getTitle(), $output['data'][0]['attributes']['title']);
}