3 namespace Drupal\Tests\views\Kernel\Handler;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\node\Entity\Node;
9 use Drupal\node\Entity\NodeType;
10 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
11 use Drupal\views\Views;
14 * Tests sorting on translatable and not translatable fields.
18 class SortTranslationTest extends ViewsKernelTestBase {
22 public static $modules = [
31 public static $testViews = [
32 'test_view_sort_translation',
38 protected function setUp($import_test_views = TRUE) {
39 parent::setUp($import_test_views);
40 ConfigurableLanguage::createFromLangcode('de')->save();
41 $this->installSchema('node', 'node_access');
42 $this->installEntitySchema('node');
43 $this->installEntitySchema('user');
45 // $this->installConfig('node');
46 $this->container->get('kernel')->rebuildContainer();
48 $node_type = NodeType::create(['type' => 'article']);
51 FieldStorageConfig::create([
52 'field_name' => 'text',
53 'entity_type' => 'node',
58 'field_name' => 'text',
59 'entity_type' => 'node',
60 'bundle' => 'article',
61 'label' => 'Translated text',
62 'translatable' => TRUE,
65 FieldStorageConfig::create([
66 'field_name' => 'weight',
67 'entity_type' => 'node',
72 'field_name' => 'weight',
73 'entity_type' => 'node',
74 'bundle' => 'article',
75 'translatable' => FALSE,
78 for ($i = 0; $i < 3; $i++) {
79 $node = Node::create([
81 'title' => 'Title en ' . $i,
82 'weight' => ['value' => 3 - $i],
83 'text' => ['value' => 'moo en ' . $i],
88 $translation = $node->addTranslation('de');
89 $translation->title->value = 'Title DE ' . $i;
90 $translation->text->value = 'moo DE ' . $i;
97 * Test sorting on an untranslated field.
99 public function testSortbyUntranslatedIntegerField() {
102 'node_field_data_langcode' => 'langcode',
105 $view = Views::getView('test_view_sort_translation');
106 $view->setDisplay('default');
107 $this->executeView($view);
109 // With ascending sort, the nodes should come out in reverse order.
124 $this->assertIdenticalResultset($view, $expected, $map);
127 $view = Views::getView('test_view_sort_translation');
128 $view->setDisplay('display_de');
129 $this->executeView($view);
146 // The weight field is not translated, we sort by it so the nodes
147 // should come out in the same order in both languages.
148 $this->assertIdenticalResultset($view, $expected, $map);