View source
<?php
namespace Drupal\views\Tests\Entity;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\NodeType;
use Drupal\views\Tests\ViewKernelTestBase;
use Drupal\views\Views;
class RowEntityRenderersTest extends ViewKernelTestBase {
public static $modules = [
'field',
'filter',
'text',
'node',
'user',
'language',
'views_test_language',
];
public static $testViews = array(
'test_entity_row_renderers',
);
protected $langcodes;
protected $expected;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installSchema('node', array(
'node_access',
));
$this
->installConfig(array(
'node',
'language',
));
$this->container
->get('router.builder')
->rebuild();
$this->langcodes = array(
\Drupal::languageManager()
->getDefaultLanguage()
->getId(),
);
for ($i = 0; $i < 2; $i++) {
$langcode = 'l' . $i;
$this->langcodes[] = $langcode;
ConfigurableLanguage::createFromLangcode($langcode)
->save();
}
$node_type = NodeType::create(array(
'type' => 'test',
));
$node_type
->setDisplaySubmitted(FALSE);
$node_type
->save();
$this->values = array();
$controller = \Drupal::entityManager()
->getStorage('node');
$langcode_index = 0;
for ($i = 0; $i < count($this->langcodes); $i++) {
$default_langcode = $this->langcodes[$langcode_index++];
$node = $controller
->create(array(
'type' => 'test',
'uid' => 0,
'langcode' => $default_langcode,
));
$langcodes = array_merge(array(
$default_langcode,
), array_diff($this->langcodes, array(
$default_langcode,
)));
foreach ($langcodes as $langcode) {
$this->values[$i][$langcode] = $i . '-' . $langcode . '-' . $this
->randomMachineName();
if ($langcode != $default_langcode) {
$node
->addTranslation($langcode, array(
'title' => $this->values[$i][$langcode],
));
}
else {
$node
->setTitle($this->values[$i][$langcode]);
}
$node
->save();
}
}
}
public function testEntityRenderers() {
$this
->checkLanguageRenderers('page_1', $this->values);
}
public function testFieldRenderers() {
$this
->checkLanguageRenderers('page_2', $this->values);
}
protected function checkLanguageRenderers($display, $values) {
$expected = array(
$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 = array(
$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 = array(
$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 = array(
$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 = array(
$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;
}
}
return $this
->assertTrue($result, $message, $group);
}
}