Version 1
[yaffs-website] / web / core / modules / views / src / Tests / FieldApiDataTest.php
1 <?php
2
3 namespace Drupal\views\Tests;
4
5 use Drupal\Component\Render\MarkupInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Tests\Views\FieldTestBase;
8
9 /**
10  * Tests the Field Views data.
11  *
12  * @group views
13  */
14 class FieldApiDataTest extends FieldTestBase {
15
16   protected function setUp() {
17     parent::setUp();
18
19     $field_names = $this->setUpFieldStorages(1);
20
21     // Attach the field to nodes only.
22     $field = [
23       'field_name' => $field_names[0],
24       'entity_type' => 'node',
25       'bundle' => 'page',
26       'label' => 'GiraffeA" label'
27     ];
28     FieldConfig::create($field)->save();
29
30     // Attach the same field to a different bundle with a different label.
31     $this->drupalCreateContentType(['type' => 'article']);
32     FieldConfig::create([
33       'field_name' => $field_names[0],
34       'entity_type' => 'node',
35       'bundle' => 'article',
36       'label' => 'GiraffeB" label'
37     ])->save();
38
39     // Now create some example nodes/users for the view result.
40     for ($i = 0; $i < 5; $i++) {
41       $edit = [
42         $field_names[0] => [(['value' => $this->randomMachineName()])],
43       ];
44       $nodes[] = $this->drupalCreateNode($edit);
45     }
46   }
47
48   /**
49    * Unit testing the views data structure.
50    *
51    * We check data structure for both node and node revision tables.
52    */
53   public function testViewsData() {
54     $table_mapping = \Drupal::entityManager()->getStorage('node')->getTableMapping();
55     $field_storage = $this->fieldStorages[0];
56     $current_table = $table_mapping->getDedicatedDataTableName($field_storage);
57     $revision_table = $table_mapping->getDedicatedRevisionTableName($field_storage);
58     $data = $this->getViewsData();
59
60     $this->assertTrue(isset($data[$current_table]));
61     $this->assertTrue(isset($data[$revision_table]));
62     // The node field should join against node_field_data.
63     $this->assertTrue(isset($data[$current_table]['table']['join']['node_field_data']));
64     $this->assertTrue(isset($data[$revision_table]['table']['join']['node_field_revision']));
65
66     $expected_join = [
67       'table' => $current_table,
68       'left_field' => 'nid',
69       'field' => 'entity_id',
70       'extra' => [
71         ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
72         ['left_field' => 'langcode', 'field' => 'langcode'],
73       ],
74     ];
75     $this->assertEqual($expected_join, $data[$current_table]['table']['join']['node_field_data']);
76     $expected_join = [
77       'table' => $revision_table,
78       'left_field' => 'vid',
79       'field' => 'revision_id',
80       'extra' => [
81         ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
82         ['left_field' => 'langcode', 'field' => 'langcode'],
83       ],
84     ];
85     $this->assertEqual($expected_join, $data[$revision_table]['table']['join']['node_field_revision']);
86
87     // Test click sortable.
88     $this->assertTrue($data[$current_table][$field_storage->getName()]['field']['click sortable'], 'String field is click sortable.');
89     // Click sort should only be on the primary field.
90     $this->assertTrue(empty($data[$revision_table][$field_storage->getName()]['field']['click sortable']), 'Non-primary fields are not click sortable');
91
92     $this->assertTrue($data[$current_table][$field_storage->getName()]['help'] instanceof MarkupInterface);
93     $this->assertEqual($data[$current_table][$field_storage->getName()]['help'], 'Appears in: page, article. Also known as: Content: GiraffeB&quot; label');
94
95     $this->assertTrue($data[$current_table][$field_storage->getName() . '_value']['help'] instanceof MarkupInterface);
96     $this->assertEqual($data[$current_table][$field_storage->getName() . '_value']['help'], 'Appears in: page, article. Also known as: Content: GiraffeA&quot; label (field_name_0)');
97
98     // Since each label is only used once, views_entity_field_label() will
99     // return a label using alphabetical sorting.
100     $this->assertEqual('GiraffeA&quot; label (field_name_0)', $data[$current_table][$field_storage->getName() . '_value']['title']);
101
102     // Attach the same field to a different bundle with a different label.
103     $this->drupalCreateContentType(['type' => 'news']);
104     FieldConfig::create([
105       'field_name' => $this->fieldStorages[0]->getName(),
106       'entity_type' => 'node',
107       'bundle' => 'news',
108       'label' => 'GiraffeB" label'
109     ])->save();
110     $this->container->get('views.views_data')->clear();
111     $data = $this->getViewsData();
112
113     // Now the 'GiraffeB&quot; label' is used twice and therefore will be
114     // selected by views_entity_field_label().
115     $this->assertEqual('GiraffeB&quot; label (field_name_0)', $data[$current_table][$field_storage->getName() . '_value']['title']);
116     $this->assertTrue($data[$current_table][$field_storage->getName()]['help'] instanceof MarkupInterface);
117     $this->assertEqual($data[$current_table][$field_storage->getName()]['help'], 'Appears in: page, article, news. Also known as: Content: GiraffeA&quot; label');
118   }
119
120   /**
121    * Gets the views data for the field created in setUp().
122    *
123    * @return array
124    */
125   protected function getViewsData() {
126     $views_data = $this->container->get('views.views_data');
127     $data = [];
128
129     // Check the table and the joins of the first field.
130     // Attached to node only.
131     /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
132     $table_mapping = \Drupal::entityManager()->getStorage('node')->getTableMapping();
133     $current_table = $table_mapping->getDedicatedDataTableName($this->fieldStorages[0]);
134     $revision_table = $table_mapping->getDedicatedRevisionTableName($this->fieldStorages[0]);
135     $data[$current_table] = $views_data->get($current_table);
136     $data[$revision_table] = $views_data->get($revision_table);
137     return $data;
138   }
139
140 }