3 namespace Drupal\views\Tests;
5 use Drupal\Component\Render\MarkupInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Tests\Views\FieldTestBase;
10 * Tests the Field Views data.
14 class FieldApiDataTest extends FieldTestBase {
16 protected function setUp() {
19 $field_names = $this->setUpFieldStorages(1);
21 // Attach the field to nodes only.
23 'field_name' => $field_names[0],
24 'entity_type' => 'node',
26 'label' => 'GiraffeA" label'
28 FieldConfig::create($field)->save();
30 // Attach the same field to a different bundle with a different label.
31 $this->drupalCreateContentType(['type' => 'article']);
33 'field_name' => $field_names[0],
34 'entity_type' => 'node',
35 'bundle' => 'article',
36 'label' => 'GiraffeB" label'
39 // Now create some example nodes/users for the view result.
40 for ($i = 0; $i < 5; $i++) {
42 $field_names[0] => [(['value' => $this->randomMachineName()])],
44 $nodes[] = $this->drupalCreateNode($edit);
49 * Unit testing the views data structure.
51 * We check data structure for both node and node revision tables.
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();
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']));
67 'table' => $current_table,
68 'left_field' => 'nid',
69 'field' => 'entity_id',
71 ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
72 ['left_field' => 'langcode', 'field' => 'langcode'],
75 $this->assertEqual($expected_join, $data[$current_table]['table']['join']['node_field_data']);
77 'table' => $revision_table,
78 'left_field' => 'vid',
79 'field' => 'revision_id',
81 ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
82 ['left_field' => 'langcode', 'field' => 'langcode'],
85 $this->assertEqual($expected_join, $data[$revision_table]['table']['join']['node_field_revision']);
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');
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" label');
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" label (field_name_0)');
98 // Since each label is only used once, views_entity_field_label() will
99 // return a label using alphabetical sorting.
100 $this->assertEqual('GiraffeA" label (field_name_0)', $data[$current_table][$field_storage->getName() . '_value']['title']);
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',
108 'label' => 'GiraffeB" label'
110 $this->container->get('views.views_data')->clear();
111 $data = $this->getViewsData();
113 // Now the 'GiraffeB" label' is used twice and therefore will be
114 // selected by views_entity_field_label().
115 $this->assertEqual('GiraffeB" 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" label');
121 * Gets the views data for the field created in setUp().
125 protected function getViewsData() {
126 $views_data = $this->container->get('views.views_data');
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);