You are here

public function JsonApiFunctionalMultilingualTest::testReadMultilingual in Drupal 8

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

Tests reading multilingual content.

File

core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalMultilingualTest.php, line 67

Class

JsonApiFunctionalMultilingualTest
Tests JSON:API multilingual support.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testReadMultilingual() {

  // Different databases have different sort orders, so a sort is required so
  // test expectations do not need to vary per database.
  $default_sort = [
    'sort' => 'drupal_internal__nid',
  ];

  // Test reading an individual entity translation.
  $output = Json::decode($this
    ->drupalGet('/ca/jsonapi/node/article/' . $this->nodes[0]
    ->uuid(), [
    'query' => [
      'include' => 'field_tags,field_image',
    ] + $default_sort,
  ]));
  $this
    ->assertEquals($this->nodes[0]
    ->getTranslation('ca')
    ->getTitle(), $output['data']['attributes']['title']);
  $this
    ->assertSame('ca', $output['data']['attributes']['langcode']);
  $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();
  $this
    ->assertEquals($tag_name, reset($included_tags)['attributes']['name']);
  $alt = $this->nodes[0]
    ->getTranslation('ca')
    ->get('field_image')->alt;
  $this
    ->assertSame($alt, $output['data']['relationships']['field_image']['data']['meta']['alt']);

  // Test reading an individual entity fallback.
  $output = Json::decode($this
    ->drupalGet('/ca-fr/jsonapi/node/article/' . $this->nodes[0]
    ->uuid()));
  $this
    ->assertEquals($this->nodes[0]
    ->getTranslation('ca')
    ->getTitle(), $output['data']['attributes']['title']);
  $output = Json::decode($this
    ->drupalGet('/ca/jsonapi/node/article/' . $this->nodes[0]
    ->uuid(), [
    'query' => $default_sort,
  ]));
  $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', [
    'query' => $default_sort,
  ]));
  $this
    ->assertEquals($this->nodes[0]
    ->getTranslation('ca')
    ->getTitle(), $output['data'][0]['attributes']['title']);
}