Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views_ui / tests / src / Functional / HandlerTest.php
1 <?php
2
3 namespace Drupal\Tests\views_ui\Functional;
4
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\views\Tests\ViewTestData;
9 use Drupal\views\ViewExecutable;
10
11 /**
12  * Tests handler UI for views.
13  *
14  * @group views_ui
15  * @see \Drupal\views\Plugin\views\HandlerBase
16  */
17 class HandlerTest extends UITestBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = ['node_test_views'];
23
24   /**
25    * Views used by this test.
26    *
27    * @var array
28    */
29   public static $testViews = ['test_view_empty', 'test_view_broken', 'node', 'test_node_view'];
30
31   /**
32    * {@inheritdoc}
33    */
34   protected function setUp($import_test_views = TRUE) {
35     parent::setUp($import_test_views);
36
37     $this->placeBlock('page_title_block');
38     ViewTestData::createTestViews(get_class($this), ['node_test_views']);
39   }
40
41   /**
42    * Overrides \Drupal\views\Tests\ViewTestBase::schemaDefinition().
43    *
44    * Adds a uid column to test the relationships.
45    */
46   protected function schemaDefinition() {
47     $schema = parent::schemaDefinition();
48
49     $schema['views_test_data']['fields']['uid'] = [
50       'description' => "The {users}.uid of the author of the beatle entry.",
51       'type' => 'int',
52       'unsigned' => TRUE,
53       'not null' => TRUE,
54       'default' => 0
55     ];
56
57     return $schema;
58   }
59
60   /**
61    * Overrides \Drupal\views\Tests\ViewTestBase::viewsData().
62    *
63    * Adds:
64    * - a relationship for the uid column.
65    * - a dummy field with no help text.
66    */
67   protected function viewsData() {
68     $data = parent::viewsData();
69     $data['views_test_data']['uid'] = [
70       'title' => t('UID'),
71       'help' => t('The test data UID'),
72       'relationship' => [
73         'id' => 'standard',
74         'base' => 'users_field_data',
75         'base field' => 'uid'
76       ]
77     ];
78
79     // Create a dummy field with no help text.
80     $data['views_test_data']['no_help'] = $data['views_test_data']['name'];
81     $data['views_test_data']['no_help']['field']['title'] = t('No help');
82     $data['views_test_data']['no_help']['field']['real field'] = 'name';
83     unset($data['views_test_data']['no_help']['help']);
84
85     return $data;
86   }
87
88   /**
89    * Tests UI CRUD.
90    */
91   public function testUICRUD() {
92     $handler_types = ViewExecutable::getHandlerTypes();
93     foreach ($handler_types as $type => $type_info) {
94       // Test adding handlers.
95       $add_handler_url = "admin/structure/views/nojs/add-handler/test_view_empty/default/$type";
96
97       // Area handler types need to use a different handler.
98       if (in_array($type, ['header', 'footer', 'empty'])) {
99         $this->drupalPostForm($add_handler_url, ['name[views.area]' => TRUE], t('Add and configure @handler', ['@handler' => $type_info['ltitle']]));
100         $id = 'area';
101         $edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/$type/$id";
102       }
103       elseif ($type == 'relationship') {
104         $this->drupalPostForm($add_handler_url, ['name[views_test_data.uid]' => TRUE], t('Add and configure @handler', ['@handler' => $type_info['ltitle']]));
105         $id = 'uid';
106         $edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/$type/$id";
107       }
108       else {
109         $this->drupalPostForm($add_handler_url, ['name[views_test_data.job]' => TRUE], t('Add and configure @handler', ['@handler' => $type_info['ltitle']]));
110         $id = 'job';
111         $edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/$type/$id";
112       }
113
114       $this->assertUrl($edit_handler_url, [], 'The user got redirected to the handler edit form.');
115       $random_label = $this->randomMachineName();
116       $this->drupalPostForm(NULL, ['options[admin_label]' => $random_label], t('Apply'));
117
118       $this->assertUrl('admin/structure/views/view/test_view_empty/edit/default', [], 'The user got redirected to the views edit form.');
119
120       $this->assertLinkByHref($edit_handler_url, 0, 'The handler edit link appears in the UI.');
121       $links = $this->xpath('//a[starts-with(normalize-space(text()), :label)]', [':label' => $random_label]);
122       $this->assertTrue(isset($links[0]), 'The handler edit link has the right label');
123
124       // Save the view and have a look whether the handler was added as expected.
125       $this->drupalPostForm(NULL, [], t('Save'));
126       $view = $this->container->get('entity.manager')->getStorage('view')->load('test_view_empty');
127       $display = $view->getDisplay('default');
128       $this->assertTrue(isset($display['display_options'][$type_info['plural']][$id]), 'Ensure the field was added to the view itself.');
129
130       // Remove the item and check that it's removed
131       $this->drupalPostForm($edit_handler_url, [], t('Remove'));
132       $this->assertNoLinkByHref($edit_handler_url, 0, 'The handler edit link does not appears in the UI after removing.');
133
134       $this->drupalPostForm(NULL, [], t('Save'));
135       $view = $this->container->get('entity.manager')->getStorage('view')->load('test_view_empty');
136       $display = $view->getDisplay('default');
137       $this->assertFalse(isset($display['display_options'][$type_info['plural']][$id]), 'Ensure the field was removed from the view itself.');
138     }
139
140     // Test adding a field of the user table using the uid relationship.
141     $type_info = $handler_types['relationship'];
142     $add_handler_url = "admin/structure/views/nojs/add-handler/test_view_empty/default/relationship";
143     $this->drupalPostForm($add_handler_url, ['name[views_test_data.uid]' => TRUE], t('Add and configure @handler', ['@handler' => $type_info['ltitle']]));
144
145     $add_handler_url = "admin/structure/views/nojs/add-handler/test_view_empty/default/field";
146     $type_info = $handler_types['field'];
147     $this->drupalPostForm($add_handler_url, ['name[users_field_data.name]' => TRUE], t('Add and configure @handler', ['@handler' => $type_info['ltitle']]));
148     $id = 'name';
149     $edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/field/$id";
150
151     $this->assertUrl($edit_handler_url, [], 'The user got redirected to the handler edit form.');
152     $this->assertFieldByName('options[relationship]', 'uid', 'Ensure the relationship select is filled with the UID relationship.');
153     $this->drupalPostForm(NULL, [], t('Apply'));
154
155     $this->drupalPostForm(NULL, [], t('Save'));
156     $view = $this->container->get('entity.manager')->getStorage('view')->load('test_view_empty');
157     $display = $view->getDisplay('default');
158     $this->assertTrue(isset($display['display_options'][$type_info['plural']][$id]), 'Ensure the field was added to the view itself.');
159   }
160
161   /**
162    * Tests escaping of field labels in help text.
163    */
164   public function testHandlerHelpEscaping() {
165     // Setup a field with two instances using a different label.
166     // Ensure that the label is escaped properly.
167
168     $this->drupalCreateContentType(['type' => 'article']);
169     $this->drupalCreateContentType(['type' => 'page']);
170
171     FieldStorageConfig::create([
172       'field_name' => 'field_test',
173       'entity_type' => 'node',
174       'type' => 'string',
175     ])->save();
176
177     FieldConfig::create([
178       'field_name' => 'field_test',
179       'entity_type' => 'node',
180       'bundle' => 'page',
181       'label' => 'The giraffe" label'
182     ])->save();
183
184     FieldConfig::create([
185       'field_name' => 'field_test',
186       'entity_type' => 'node',
187       'bundle' => 'article',
188       'label' => 'The <em>giraffe"</em> label <script>alert("the return of the xss")</script>'
189     ])->save();
190
191     $this->drupalGet('admin/structure/views/nojs/add-handler/content/default/field');
192     $this->assertEscaped('The <em>giraffe"</em> label <script>alert("the return of the xss")</script>');
193     $this->assertEscaped('Appears in: page, article. Also known as: Content: The giraffe" label');
194   }
195
196   /**
197    * Tests broken handlers.
198    */
199   public function testBrokenHandlers() {
200     $handler_types = ViewExecutable::getHandlerTypes();
201     foreach ($handler_types as $type => $type_info) {
202       $this->drupalGet('admin/structure/views/view/test_view_broken/edit');
203
204       $href = "admin/structure/views/nojs/handler/test_view_broken/default/$type/id_broken";
205
206       $result = $this->xpath('//a[contains(@href, :href)]', [':href' => $href]);
207       $this->assertEqual(count($result), 1, SafeMarkup::format('Handler (%type) edit link found.', ['%type' => $type]));
208
209       $text = 'Broken/missing handler';
210
211       $this->assertIdentical($result[0]->getText(), $text, 'Ensure the broken handler text was found.');
212
213       $this->drupalGet($href);
214       $result = $this->xpath('//h1[@class="page-title"]');
215       $this->assertContains($text, $result[0]->getText(), 'Ensure the broken handler text was found.');
216
217       $original_configuration = [
218         'field' => 'id_broken',
219         'id' => 'id_broken',
220         'relationship' => 'none',
221         'table' => 'views_test_data',
222         'plugin_id' => 'numeric',
223       ];
224
225       foreach ($original_configuration as $key => $value) {
226         $this->assertText(SafeMarkup::format('@key: @value', ['@key' => $key, '@value' => $value]));
227       }
228     }
229   }
230
231   /**
232    * Ensures that neither node type or node ID appears multiple times.
233    *
234    * @see \Drupal\views\EntityViewsData
235    */
236   public function testNoDuplicateFields() {
237     $handler_types = ['field', 'filter', 'sort', 'argument'];
238
239     foreach ($handler_types as $handler_type) {
240       $add_handler_url = 'admin/structure/views/nojs/add-handler/test_node_view/default/' . $handler_type;
241       $this->drupalGet($add_handler_url);
242
243       $this->assertNoDuplicateField('ID', 'Content');
244       $this->assertNoDuplicateField('ID', 'Content revision');
245       $this->assertNoDuplicateField('Content type', 'Content');
246       $this->assertNoDuplicateField('UUID', 'Content');
247       $this->assertNoDuplicateField('Revision ID', 'Content');
248       $this->assertNoDuplicateField('Revision ID', 'Content revision');
249     }
250   }
251
252   /**
253    * Ensures that no missing help text is shown.
254    *
255    * @see \Drupal\views\EntityViewsData
256    */
257   public function testErrorMissingHelp() {
258     // Test that the error message is not shown for entity fields but an empty
259     // description field is shown instead.
260     $this->drupalGet('admin/structure/views/nojs/add-handler/test_node_view/default/field');
261     $this->assertNoText('Error: missing help');
262     $this->assertRaw('<td class="description"></td>', 'Empty description found');
263
264     // Test that no error message is shown for other fields.
265     $this->drupalGet('admin/structure/views/nojs/add-handler/test_view_empty/default/field');
266     $this->assertNoText('Error: missing help');
267   }
268
269   /**
270    * Asserts that fields only appear once.
271    *
272    * @param string $field_name
273    *   The field name.
274    * @param string $entity_type
275    *   The entity type to which the field belongs.
276    */
277   public function assertNoDuplicateField($field_name, $entity_type) {
278     $elements = $this->xpath('//td[.=:entity_type]/preceding-sibling::td[@class="title" and .=:title]', [':title' => $field_name, ':entity_type' => $entity_type]);
279     $this->assertEqual(1, count($elements), $field_name . ' appears just once in ' . $entity_type . '.');
280   }
281
282 }