3 namespace Drupal\Tests\views\Functional;
5 use Drupal\Component\Render\MarkupInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\language\Entity\ContentLanguageSettings;
9 use Drupal\node\Entity\Node;
10 use Drupal\node\Entity\NodeType;
11 use Drupal\Tests\field\Functional\Views\FieldTestBase;
12 use Drupal\views\Views;
15 * Tests the Field Views data.
19 class FieldApiDataTest extends FieldTestBase {
24 public static $modules = ['language'];
29 public static $testViews = ['test_field_config_translation_filter'];
32 * The nodes used by the translation filter tests.
34 * @var \Drupal\node\NodeInterface[]
36 protected $translationNodes;
38 protected function setUp($import_test_views = TRUE) {
39 parent::setUp($import_test_views);
41 $field_names = $this->setUpFieldStorages(4);
43 // Attach the field to nodes only.
45 'field_name' => $field_names[0],
46 'entity_type' => 'node',
48 'label' => 'GiraffeA" label',
50 FieldConfig::create($field)->save();
52 // Attach the same field to a different bundle with a different label.
53 $this->drupalCreateContentType(['type' => 'article']);
55 'field_name' => $field_names[0],
56 'entity_type' => 'node',
57 'bundle' => 'article',
58 'label' => 'GiraffeB" label',
61 // Now create some example nodes/users for the view result.
62 for ($i = 0; $i < 5; $i++) {
64 $field_names[0] => [(['value' => $this->randomMachineName()])],
66 $nodes[] = $this->drupalCreateNode($edit);
70 $bundles[] = $bundle = NodeType::create(['type' => 'bundle1']);
72 $bundles[] = $bundle = NodeType::create(['type' => 'bundle2']);
75 // Make the first field translatable on all bundles.
76 $field = FieldConfig::create([
77 'field_name' => $field_names[1],
78 'entity_type' => 'node',
79 'bundle' => $bundles[0]->id(),
80 'translatable' => TRUE,
83 $field = FieldConfig::create([
84 'field_name' => $field_names[1],
85 'entity_type' => 'node',
86 'bundle' => $bundles[1]->id(),
87 'translatable' => TRUE,
91 // Make the second field not translatable on any bundle.
92 $field = FieldConfig::create([
93 'field_name' => $field_names[2],
94 'entity_type' => 'node',
95 'bundle' => $bundles[0]->id(),
96 'translatable' => FALSE,
99 $field = FieldConfig::create([
100 'field_name' => $field_names[2],
101 'entity_type' => 'node',
102 'bundle' => $bundles[1]->id(),
103 'translatable' => FALSE,
107 // Make the last field translatable on some bundles.
108 $field = FieldConfig::create([
109 'field_name' => $field_names[3],
110 'entity_type' => 'node',
111 'bundle' => $bundles[0]->id(),
112 'translatable' => TRUE,
115 $field = FieldConfig::create([
116 'field_name' => $field_names[3],
117 'entity_type' => 'node',
118 'bundle' => $bundles[1]->id(),
119 'translatable' => FALSE,
123 // Create some example content.
124 ConfigurableLanguage::create([
127 ConfigurableLanguage::create([
131 $config = ContentLanguageSettings::loadByEntityTypeBundle('node', $bundles[0]->id());
132 $config->setDefaultLangcode('es')
133 ->setLanguageAlterable(TRUE)
135 $config = ContentLanguageSettings::loadByEntityTypeBundle('node', $bundles[1]->id());
136 $config->setDefaultLangcode('es')
137 ->setLanguageAlterable(TRUE)
140 $node = Node::create([
141 'title' => 'Test title ' . $bundles[0]->id(),
142 'type' => $bundles[0]->id(),
144 $field_names[1] => 'field name 1: es',
145 $field_names[2] => 'field name 2: es',
146 $field_names[3] => 'field name 3: es',
149 $this->translationNodes[] = $node;
150 $translation = $node->addTranslation('fr');
151 $translation->{$field_names[1]}->value = 'field name 1: fr';
152 $translation->{$field_names[3]}->value = 'field name 3: fr';
153 $translation->title->value = $node->title->value;
154 $translation->save();
156 $node = Node::create([
157 'title' => 'Test title ' . $bundles[1]->id(),
158 'type' => $bundles[1]->id(),
160 $field_names[1] => 'field name 1: es',
161 $field_names[2] => 'field name 2: es',
162 $field_names[3] => 'field name 3: es',
165 $this->translationNodes[] = $node;
166 $translation = $node->addTranslation('fr');
167 $translation->{$field_names[1]}->value = 'field name 1: fr';
168 $translation->title->value = $node->title->value;
169 $translation->save();
174 * Unit testing the views data structure.
176 * We check data structure for both node and node revision tables.
178 public function testViewsData() {
179 $table_mapping = \Drupal::entityManager()->getStorage('node')->getTableMapping();
180 $field_storage = $this->fieldStorages[0];
181 $current_table = $table_mapping->getDedicatedDataTableName($field_storage);
182 $revision_table = $table_mapping->getDedicatedRevisionTableName($field_storage);
183 $data = $this->getViewsData();
185 $this->assertTrue(isset($data[$current_table]));
186 $this->assertTrue(isset($data[$revision_table]));
187 // The node field should join against node_field_data.
188 $this->assertTrue(isset($data[$current_table]['table']['join']['node_field_data']));
189 $this->assertTrue(isset($data[$revision_table]['table']['join']['node_field_revision']));
192 'table' => $current_table,
193 'left_field' => 'nid',
194 'field' => 'entity_id',
196 ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
197 ['left_field' => 'langcode', 'field' => 'langcode'],
200 $this->assertEqual($expected_join, $data[$current_table]['table']['join']['node_field_data']);
202 'table' => $revision_table,
203 'left_field' => 'vid',
204 'field' => 'revision_id',
206 ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
207 ['left_field' => 'langcode', 'field' => 'langcode'],
210 $this->assertEqual($expected_join, $data[$revision_table]['table']['join']['node_field_revision']);
212 // Test click sortable.
213 $this->assertTrue($data[$current_table][$field_storage->getName()]['field']['click sortable'], 'String field is click sortable.');
214 // Click sort should only be on the primary field.
215 $this->assertTrue(empty($data[$revision_table][$field_storage->getName()]['field']['click sortable']), 'Non-primary fields are not click sortable');
217 $this->assertTrue($data[$current_table][$field_storage->getName()]['help'] instanceof MarkupInterface);
218 $this->assertEqual($data[$current_table][$field_storage->getName()]['help'], 'Appears in: page, article. Also known as: Content: GiraffeB" label');
220 $this->assertTrue($data[$current_table][$field_storage->getName() . '_value']['help'] instanceof MarkupInterface);
221 $this->assertEqual($data[$current_table][$field_storage->getName() . '_value']['help'], 'Appears in: page, article. Also known as: Content: GiraffeA" label (field_name_0)');
223 // Since each label is only used once, views_entity_field_label() will
224 // return a label using alphabetical sorting.
225 $this->assertEqual('GiraffeA" label (field_name_0)', $data[$current_table][$field_storage->getName() . '_value']['title']);
227 // Attach the same field to a different bundle with a different label.
228 $this->drupalCreateContentType(['type' => 'news']);
229 FieldConfig::create([
230 'field_name' => $this->fieldStorages[0]->getName(),
231 'entity_type' => 'node',
233 'label' => 'GiraffeB" label',
235 $this->container->get('views.views_data')->clear();
236 $data = $this->getViewsData();
238 // Now the 'GiraffeB" label' is used twice and therefore will be
239 // selected by views_entity_field_label().
240 $this->assertEqual('GiraffeB" label (field_name_0)', $data[$current_table][$field_storage->getName() . '_value']['title']);
241 $this->assertTrue($data[$current_table][$field_storage->getName()]['help'] instanceof MarkupInterface);
242 $this->assertEqual($data[$current_table][$field_storage->getName()]['help'], 'Appears in: page, article, news. Also known as: Content: GiraffeA" label');
246 * Gets the views data for the field created in setUp().
250 protected function getViewsData() {
251 $views_data = $this->container->get('views.views_data');
254 // Check the table and the joins of the first field.
255 // Attached to node only.
256 /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
257 $table_mapping = \Drupal::entityManager()->getStorage('node')->getTableMapping();
258 $current_table = $table_mapping->getDedicatedDataTableName($this->fieldStorages[0]);
259 $revision_table = $table_mapping->getDedicatedRevisionTableName($this->fieldStorages[0]);
260 $data[$current_table] = $views_data->get($current_table);
261 $data[$revision_table] = $views_data->get($revision_table);
266 * Tests filtering entries with different translatability.
268 public function testEntityFieldFilter() {
271 'langcode' => 'langcode',
274 $view = Views::getView('test_field_config_translation_filter');
276 // Filter by 'field name 1: es'.
277 $view->setDisplay('embed_1');
278 $this->executeView($view);
281 'nid' => $this->translationNodes[0]->id(),
285 'nid' => $this->translationNodes[1]->id(),
290 $this->assertIdenticalResultset($view, $expected, $map);
293 // Filter by 'field name 1: fr'.
294 $view->setDisplay('embed_2');
295 $this->executeView($view);
298 'nid' => $this->translationNodes[0]->id(),
302 'nid' => $this->translationNodes[1]->id(),
307 $this->assertIdenticalResultset($view, $expected, $map);
310 // Filter by 'field name 2: es'.
311 $view->setDisplay('embed_3');
312 $this->executeView($view);
315 'nid' => $this->translationNodes[0]->id(),
319 'nid' => $this->translationNodes[0]->id(),
323 'nid' => $this->translationNodes[1]->id(),
327 'nid' => $this->translationNodes[1]->id(),
332 $this->assertIdenticalResultset($view, $expected, $map);
335 // Filter by 'field name 2: fr', which doesn't exist.
336 $view->setDisplay('embed_4');
337 $this->executeView($view);
340 $this->assertIdenticalResultset($view, $expected, $map);
343 // Filter by 'field name 3: es'.
344 $view->setDisplay('embed_5');
345 $this->executeView($view);
348 'nid' => $this->translationNodes[0]->id(),
352 'nid' => $this->translationNodes[1]->id(),
355 // Why is this one returned?
357 'nid' => $this->translationNodes[1]->id(),
362 $this->assertIdenticalResultset($view, $expected, $map);
365 // Filter by 'field name 3: fr'.
366 $view->setDisplay('embed_6');
367 $this->executeView($view);
370 'nid' => $this->translationNodes[0]->id(),
375 $this->assertIdenticalResultset($view, $expected, $map);