3 namespace Drupal\views_ui\Tests;
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;
12 * Tests handler UI for views.
15 * @see \Drupal\views\Plugin\views\HandlerBase
17 class HandlerTest extends UITestBase {
22 public static $modules = ['node_test_views'];
25 * Views used by this test.
29 public static $testViews = ['test_view_empty', 'test_view_broken', 'node', 'test_node_view'];
34 protected function setUp() {
37 $this->drupalPlaceBlock('page_title_block');
38 ViewTestData::createTestViews(get_class($this), ['node_test_views']);
42 * Overrides \Drupal\views\Tests\ViewTestBase::schemaDefinition().
44 * Adds a uid column to test the relationships.
46 protected function schemaDefinition() {
47 $schema = parent::schemaDefinition();
49 $schema['views_test_data']['fields']['uid'] = [
50 'description' => "The {users}.uid of the author of the beatle entry.",
61 * Overrides \Drupal\views\Tests\ViewTestBase::viewsData().
64 * - a relationship for the uid column.
65 * - a dummy field with no help text.
67 protected function viewsData() {
68 $data = parent::viewsData();
69 $data['views_test_data']['uid'] = [
71 'help' => t('The test data UID'),
74 'base' => 'users_field_data',
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']);
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";
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']]));
101 $edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/$type/$id";
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']]));
106 $edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/$type/$id";
109 $this->drupalPostForm($add_handler_url, ['name[views_test_data.job]' => TRUE], t('Add and configure @handler', ['@handler' => $type_info['ltitle']]));
111 $edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/$type/$id";
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'));
118 $this->assertUrl('admin/structure/views/view/test_view_empty/edit/default', [], 'The user got redirected to the views edit form.');
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');
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.');
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.');
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.');
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']]));
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']]));
149 $edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/field/$id";
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'));
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.');
162 * Tests escaping of field labels in help text.
164 public function testHandlerHelpEscaping() {
165 // Setup a field with two instances using a different label.
166 // Ensure that the label is escaped properly.
168 $this->drupalCreateContentType(['type' => 'article']);
169 $this->drupalCreateContentType(['type' => 'page']);
171 FieldStorageConfig::create([
172 'field_name' => 'field_test',
173 'entity_type' => 'node',
177 FieldConfig::create([
178 'field_name' => 'field_test',
179 'entity_type' => 'node',
181 'label' => 'The giraffe" label'
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>'
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');
197 * Tests broken handlers.
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');
204 $href = "admin/structure/views/nojs/handler/test_view_broken/default/$type/id_broken";
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]));
209 $text = 'Broken/missing handler';
211 $this->assertIdentical((string) $result[0], $text, 'Ensure the broken handler text was found.');
213 $this->drupalGet($href);
214 $result = $this->xpath('//h1[@class="page-title"]');
215 $this->assertTrue(strpos((string) $result[0], $text) !== FALSE, 'Ensure the broken handler text was found.');
217 $original_configuration = [
218 'field' => 'id_broken',
220 'relationship' => 'none',
221 'table' => 'views_test_data',
222 'plugin_id' => 'numeric',
225 foreach ($original_configuration as $key => $value) {
226 $this->assertText(SafeMarkup::format('@key: @value', ['@key' => $key, '@value' => $value]));
232 * Ensures that neither node type or node ID appears multiple times.
234 * @see \Drupal\views\EntityViewsData
236 public function testNoDuplicateFields() {
237 $handler_types = ['field', 'filter', 'sort', 'argument'];
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);
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');
253 * Ensures that no missing help text is shown.
255 * @see \Drupal\views\EntityViewsData
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');
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');
270 * Asserts that fields only appear once.
272 * @param string $field_name
274 * @param string $entity_type
275 * The entity type to which the field belongs.
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 . '.');