You are here

public function StyleSerializerTest::testMulEntityRows in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest::testMulEntityRows()
  2. 9 core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest::testMulEntityRows()

Tests multilingual entity rows.

File

core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php, line 851

Class

StyleSerializerTest
Tests the serializer style plugin.

Namespace

Drupal\Tests\rest\Functional\Views

Code

public function testMulEntityRows() {

  // Create some languages.
  ConfigurableLanguage::createFromLangcode('l1')
    ->save();
  ConfigurableLanguage::createFromLangcode('l2')
    ->save();

  // Create an entity with no translations.
  $storage = \Drupal::entityTypeManager()
    ->getStorage('entity_test_mul');
  $storage
    ->create([
    'langcode' => 'l1',
    'name' => 'mul-none',
  ])
    ->save();

  // Create some entities with translations.
  $entity = $storage
    ->create([
    'langcode' => 'l1',
    'name' => 'mul-l1-orig',
  ]);
  $entity
    ->save();
  $entity
    ->addTranslation('l2', [
    'name' => 'mul-l1-l2',
  ])
    ->save();
  $entity = $storage
    ->create([
    'langcode' => 'l2',
    'name' => 'mul-l2-orig',
  ]);
  $entity
    ->save();
  $entity
    ->addTranslation('l1', [
    'name' => 'mul-l2-l1',
  ])
    ->save();

  // Get the names of the output.
  $json = $this
    ->drupalGet('test/serialize/translated_entity', [
    'query' => [
      '_format' => 'json',
    ],
  ]);
  $decoded = $this->container
    ->get('serializer')
    ->decode($json, 'json');
  $names = [];
  foreach ($decoded as $item) {
    $names[] = $item['name'][0]['value'];
  }
  sort($names);

  // Check that the names are correct.
  $expected = [
    'mul-l1-l2',
    'mul-l1-orig',
    'mul-l2-l1',
    'mul-l2-orig',
    'mul-none',
  ];
  $this
    ->assertSame($expected, $names, 'The translated content was found in the JSON.');
}