function FieldRenderedEntityTranslationTest::testTranslationRows

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Functional/Entity/FieldRenderedEntityTranslationTest.php \Drupal\Tests\views\Functional\Entity\FieldRenderedEntityTranslationTest::testTranslationRows()
  2. 8.9.x core/modules/views/tests/src/Functional/Entity/FieldRenderedEntityTranslationTest.php \Drupal\Tests\views\Functional\Entity\FieldRenderedEntityTranslationTest::testTranslationRows()
  3. 11.x core/modules/views/tests/src/Functional/Entity/FieldRenderedEntityTranslationTest.php \Drupal\Tests\views\Functional\Entity\FieldRenderedEntityTranslationTest::testTranslationRows()

Tests that different translation mechanisms can be used for base fields.

File

core/modules/views/tests/src/Functional/Entity/FieldRenderedEntityTranslationTest.php, line 76

Class

FieldRenderedEntityTranslationTest
Tests the rendering of the 'rendered_entity' field and translations.

Namespace

Drupal\Tests\views\Functional\Entity

Code

public function testTranslationRows() : void {
  // First, an EN node with an ES translation.
  /** @var \Drupal\node\NodeInterface $node */
  $node = $this->entityTypeManager
    ->getStorage('node')
    ->create([
    'type' => 'article',
    'title' => 'example EN default',
  ]);
  $node->save();
  $translation = $node->addTranslation('es');
  $translation->title->value = 'example ES translation';
  $translation->sticky->value = TRUE;
  $translation->save();
  // Next, an ES node with an EN translation.
  $node = $this->entityTypeManager
    ->getStorage('node')
    ->create([
    'type' => 'article',
    'title' => 'example ES default',
    'langcode' => 'es',
  ]);
  $node->save();
  $translation = $node->addTranslation('en');
  $translation->title->value = 'example EN translation';
  $translation->sticky->value = TRUE;
  $translation->save();
  // Next an EN node with no translation.
  $node = $this->entityTypeManager
    ->getStorage('node')
    ->create([
    'type' => 'article',
    'title' => 'example EN no translation',
    'sticky' => FALSE,
  ]);
  $node->save();
  // Next an ES node with no translation.
  $node = $this->entityTypeManager
    ->getStorage('node')
    ->create([
    'type' => 'article',
    'title' => 'example ES no translation',
    'sticky' => FALSE,
    'langcode' => 'es',
  ]);
  $node->save();
  // Views are sorted first by node id ascending, and then title ascending.
  // Confirm each node and node translation renders in its own language.
  $this->drupalGet('test_entity_field_renderered_entity/entity_translation');
  $this->assertRows([
    [
      'title' => 'example EN default',
    ],
    [
      'title' => 'example ES translation',
    ],
    [
      'title' => 'example EN translation',
    ],
    [
      'title' => 'example ES default',
    ],
    [
      'title' => 'example EN no translation',
    ],
    [
      'title' => 'example ES no translation',
    ],
  ]);
  // Confirm each node and node translation renders in the default language of
  // the node.
  $this->drupalGet('test_entity_field_renderered_entity/entity_default');
  $this->assertRows([
    [
      'title' => 'example EN default',
    ],
    [
      'title' => 'example EN default',
    ],
    [
      'title' => 'example ES default',
    ],
    [
      'title' => 'example ES default',
    ],
    [
      'title' => 'example EN no translation',
    ],
    [
      'title' => 'example ES no translation',
    ],
  ]);
  // Confirm each node and node translation renders in the site's default
  // language (en), with fallback if node does not have en content.
  $this->drupalGet('test_entity_field_renderered_entity/site_default');
  $this->assertRows([
    [
      'title' => 'example EN default',
    ],
    [
      'title' => 'example EN default',
    ],
    [
      'title' => 'example EN translation',
    ],
    [
      'title' => 'example EN translation',
    ],
    [
      'title' => 'example EN no translation',
    ],
    [
      'title' => 'example ES no translation',
    ],
  ]);
  // Confirm each node and node translation renders in the site interface
  // language (en), with fallback if node does not have en content.
  $this->drupalGet('test_entity_field_renderered_entity/language_interface');
  $this->assertRows([
    [
      'title' => 'example EN default',
    ],
    [
      'title' => 'example EN default',
    ],
    [
      'title' => 'example EN translation',
    ],
    [
      'title' => 'example EN translation',
    ],
    [
      'title' => 'example EN no translation',
    ],
    [
      'title' => 'example ES no translation',
    ],
  ]);
  // Confirm each node and node translation renders in the site interface
  // language (es), with fallback if node does not have es content.
  $this->drupalGet('test_entity_field_renderered_entity/language_interface', [
    'language' => new Language([
      'id' => 'es',
    ]),
  ]);
  $this->assertRows([
    [
      'title' => 'example ES translation',
    ],
    [
      'title' => 'example ES translation',
    ],
    [
      'title' => 'example ES default',
    ],
    [
      'title' => 'example ES default',
    ],
    [
      'title' => 'example EN no translation',
    ],
    [
      'title' => 'example ES no translation',
    ],
  ]);
  // Confirm each node and node translation renders in specified language en.
  $this->drupalGet('test_entity_field_renderered_entity/en');
  $this->assertRows([
    [
      'title' => 'example EN default',
    ],
    [
      'title' => 'example EN default',
    ],
    [
      'title' => 'example EN translation',
    ],
    [
      'title' => 'example EN translation',
    ],
    [
      'title' => 'example EN no translation',
    ],
    [
      'title' => 'example ES no translation',
    ],
  ]);
  // Confirm each node and node translation renders in specified language es.
  $this->drupalGet('test_entity_field_renderered_entity/es');
  $this->assertRows([
    [
      'title' => 'example ES translation',
    ],
    [
      'title' => 'example ES translation',
    ],
    [
      'title' => 'example ES default',
    ],
    [
      'title' => 'example ES default',
    ],
    [
      'title' => 'example EN no translation',
    ],
    [
      'title' => 'example ES no translation',
    ],
  ]);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.