View source
<?php
namespace Drupal\views\Tests\Entity;
use Drupal\Core\Language\Language;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\views\Tests\ViewTestBase;
use Symfony\Component\CssSelector\CssSelector;
class FieldEntityTranslationTest extends ViewTestBase {
public static $modules = [
'language',
'locale',
'content_translation',
'node',
];
public static $testViews = [
'test_entity_field_renderers',
];
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$node_type = NodeType::create([
'type' => 'article',
'label' => 'Article',
]);
$node_type
->save();
$content_translation_manager = \Drupal::service('content_translation.manager');
$content_translation_manager
->setEnabled('node', 'article', 'title');
$content_translation_manager
->setEnabled('node', 'article', 'sticky');
$content_translation_manager
->setEnabled('node', 'article', 'published');
$language = ConfigurableLanguage::create([
'id' => 'es',
'label' => 'Spanish',
]);
$language
->save();
$this
->rebuildContainer();
}
public function testTranslationRows() {
$node = Node::create([
'type' => 'article',
'title' => 'example EN',
'sticky' => false,
]);
$node
->save();
$translation = $node
->addTranslation('es');
$translation->title->value = 'example ES';
$translation->sticky->value = true;
$translation
->save();
$this
->drupalGet('test_entity_field_renderers/entity_translation');
$this
->assertRows([
[
'title' => 'example EN',
'sticky' => 'Off',
],
[
'title' => 'example ES',
'sticky' => 'On',
],
]);
$this
->drupalGet('test_entity_field_renderers/entity_default');
$this
->assertRows([
[
'title' => 'example EN',
'sticky' => 'Off',
],
[
'title' => 'example EN',
'sticky' => 'Off',
],
]);
$this
->drupalGet('test_entity_field_renderers/site_default');
$this
->assertRows([
[
'title' => 'example EN',
'sticky' => 'Off',
],
[
'title' => 'example EN',
'sticky' => 'Off',
],
]);
$this
->drupalGet('test_entity_field_renderers/language_interface');
$this
->assertRows([
[
'title' => 'example EN',
'sticky' => 'Off',
],
[
'title' => 'example EN',
'sticky' => 'Off',
],
]);
$this
->drupalGet('test_entity_field_renderers/language_interface', [
'language' => new Language([
'id' => 'es',
]),
]);
$this
->assertRows([
[
'title' => 'example ES',
'sticky' => 'On',
],
[
'title' => 'example ES',
'sticky' => 'On',
],
]);
$this
->drupalGet('test_entity_field_renderers/en');
$this
->assertRows([
[
'title' => 'example EN',
'sticky' => 'Off',
],
[
'title' => 'example EN',
'sticky' => 'Off',
],
]);
$this
->drupalGet('test_entity_field_renderers/es');
$this
->assertRows([
[
'title' => 'example ES',
'sticky' => 'On',
],
[
'title' => 'example ES',
'sticky' => 'On',
],
]);
}
protected function assertRows($expected = []) {
$actual = [];
$rows = $this
->cssSelect('div.views-row');
foreach ($rows as $row) {
$actual[] = [
'title' => (string) $row
->xpath(CssSelector::toXPath('.views-field-title span.field-content a'))[0],
'sticky' => (string) $row
->xpath(CssSelector::toXPath('.views-field-sticky span.field-content'))[0],
];
}
$this
->assertEqual($actual, $expected);
}
}