View source
<?php
namespace Drupal\Tests\views\Functional\Entity;
use Drupal\Core\Language\Language;
use Drupal\Tests\views\Functional\ViewTestBase;
use Symfony\Component\CssSelector\CssSelectorConverter;
class FieldRenderedEntityTranslationTest extends ViewTestBase {
protected static $modules = [
'language',
'locale',
'content_translation',
'node',
];
protected $defaultTheme = 'classy';
public static $testViews = [
'test_entity_field_renderered_entity',
];
protected $entityTypeManager;
protected function setUp($import_test_views = TRUE, $modules = [
'views_test_config',
]) : void {
parent::setUp($import_test_views, $modules);
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$node_type = $this->entityTypeManager
->getStorage('node_type')
->create([
'type' => 'article',
'label' => 'Article',
]);
$node_type
->save();
$content_translation_manager = $this->container
->get('content_translation.manager');
$content_translation_manager
->setEnabled('node', 'article', TRUE);
$language = $this->entityTypeManager
->getStorage('configurable_language')
->create([
'id' => 'es',
'label' => 'Spanish',
]);
$language
->save();
$this
->rebuildContainer();
}
public function testTranslationRows() {
$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();
$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();
$node = $this->entityTypeManager
->getStorage('node')
->create([
'type' => 'article',
'title' => 'example EN no translation',
'sticky' => FALSE,
]);
$node
->save();
$node = $this->entityTypeManager
->getStorage('node')
->create([
'type' => 'article',
'title' => 'example ES no translation',
'sticky' => FALSE,
'langcode' => 'es',
]);
$node
->save();
$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',
],
]);
$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',
],
]);
$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',
],
]);
$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',
],
]);
$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',
],
]);
$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',
],
]);
$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',
],
]);
}
protected function assertRows(array $expected = []) : void {
$actual = [];
$rows = $this
->cssSelect('div.views-row');
foreach ($rows as $row) {
$actual[] = [
'title' => $row
->find('xpath', (new CssSelectorConverter())
->toXPath('h2 a .field--name-title'))
->getText(),
];
}
$this
->assertEquals($actual, $expected);
}
}