View source
<?php
namespace Drupal\Tests\views\Kernel\Entity;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\user\Entity\User;
use Drupal\views\Views;
class RowEntityRenderersTest extends ViewsKernelTestBase {
protected static $modules = [
'field',
'filter',
'text',
'node',
'user',
'language',
'views_test_language',
];
public static $testViews = [
'test_entity_row_renderers',
'test_entity_row_renderers_revisions_base',
];
protected $langcodes;
protected $expected;
protected $testAuthor;
protected $testIds;
protected function setUp($import_test_views = TRUE) : void {
parent::setUp();
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installSchema('node', [
'node_access',
]);
$this
->installConfig([
'node',
'language',
]);
$this->langcodes = [
\Drupal::languageManager()
->getDefaultLanguage()
->getId(),
];
for ($i = 0; $i < 2; $i++) {
$langcode = 'l' . $i;
$this->langcodes[] = $langcode;
ConfigurableLanguage::createFromLangcode($langcode)
->save();
}
$this->testAuthor = User::create([
'name' => 'foo',
]);
$this->testAuthor
->save();
$node_type = NodeType::create([
'type' => 'test',
]);
$node_type
->setDisplaySubmitted(FALSE);
$node_type
->save();
$this->values = [];
$this->ids = [];
$controller = \Drupal::entityTypeManager()
->getStorage('node');
$langcode_index = 0;
for ($i = 0; $i < count($this->langcodes); $i++) {
$default_langcode = $this->langcodes[$langcode_index++];
$node = $controller
->create([
'type' => 'test',
'uid' => $this->testAuthor
->id(),
'langcode' => $default_langcode,
]);
$langcodes = array_merge([
$default_langcode,
], array_diff($this->langcodes, [
$default_langcode,
]));
foreach ($langcodes as $langcode) {
$this->values[$i][$langcode] = $i . '-' . $langcode . '-' . $this
->randomMachineName();
if ($langcode != $default_langcode) {
$node
->addTranslation($langcode, [
'title' => $this->values[$i][$langcode],
]);
}
else {
$node
->setTitle($this->values[$i][$langcode]);
}
$node
->save();
$this->ids[] = [
'nid' => $node
->id(),
'uid' => $this->testAuthor
->id(),
];
}
}
}
public function testEntityRenderers() {
$this
->checkLanguageRenderers('page_1', $this->values);
}
public function testFieldRenderers() {
$this
->checkLanguageRenderers('page_2', $this->values);
}
public function testRevisionBaseTable() {
$view = Views::getView('test_entity_row_renderers_revisions_base');
$view
->execute();
$this
->assertIdenticalResultset($view, $this->ids, [
'nid' => 'nid',
'uid' => 'uid',
]);
}
protected function checkLanguageRenderers($display, $values) {
$expected = [
$values[0]['en'],
$values[0]['en'],
$values[0]['en'],
$values[1]['en'],
$values[1]['en'],
$values[1]['en'],
$values[2]['en'],
$values[2]['en'],
$values[2]['en'],
];
$this
->assertTranslations($display, '***LANGUAGE_language_content***', $expected, 'The current language renderer behaves as expected.');
$expected = [
$values[0]['en'],
$values[0]['en'],
$values[0]['en'],
$values[1]['l0'],
$values[1]['l0'],
$values[1]['l0'],
$values[2]['l1'],
$values[2]['l1'],
$values[2]['l1'],
];
$this
->assertTranslations($display, '***LANGUAGE_entity_default***', $expected, 'The default language renderer behaves as expected.');
$expected = [
$values[0]['en'],
$values[0]['l0'],
$values[0]['l1'],
$values[1]['en'],
$values[1]['l0'],
$values[1]['l1'],
$values[2]['en'],
$values[2]['l0'],
$values[2]['l1'],
];
$this
->assertTranslations($display, '***LANGUAGE_entity_translation***', $expected, 'The translation language renderer behaves as expected.');
$expected = [
$values[0][$this->langcodes[0]],
$values[0][$this->langcodes[0]],
$values[0][$this->langcodes[0]],
$values[1][$this->langcodes[0]],
$values[1][$this->langcodes[0]],
$values[1][$this->langcodes[0]],
$values[2][$this->langcodes[0]],
$values[2][$this->langcodes[0]],
$values[2][$this->langcodes[0]],
];
$this
->assertTranslations($display, '***LANGUAGE_site_default***', $expected, 'The site default language renderer behaves as expected.');
$expected = [
$values[0]['l0'],
$values[0]['l0'],
$values[0]['l0'],
$values[1]['l0'],
$values[1]['l0'],
$values[1]['l0'],
$values[2]['l0'],
$values[2]['l0'],
$values[2]['l0'],
];
$this
->assertTranslations($display, 'l0', $expected, 'The language specific renderer behaves as expected.');
}
protected function assertTranslations($display, $renderer_id, array $expected, $message = '', $group = 'Other') {
$view = Views::getView('test_entity_row_renderers');
$view->storage
->invalidateCaches();
$view
->setDisplay($display);
$view
->getDisplay()
->setOption('rendering_language', $renderer_id);
$view
->preview();
$result = FALSE;
foreach ($expected as $index => $expected_output) {
if (!empty($view->result[$index])) {
$build = $view->rowPlugin
->render($view->result[$index]);
$output = \Drupal::service('renderer')
->renderRoot($build);
$result = strpos($output, $expected_output) !== FALSE;
if (!$result) {
break;
}
}
else {
$result = FALSE;
break;
}
}
$this
->assertTrue($result, $message, $group);
}
}