View source
<?php
namespace Drupal\Tests\views_ui\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\ViewExecutable;
class HandlerTest extends UITestBase {
public static $modules = [
'node_test_views',
];
protected $defaultTheme = 'classy';
public static $testViews = [
'test_view_empty',
'test_view_broken',
'node',
'test_node_view',
];
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this
->placeBlock('page_title_block');
ViewTestData::createTestViews(get_class($this), [
'node_test_views',
]);
}
protected function schemaDefinition() {
$schema = parent::schemaDefinition();
$schema['views_test_data']['fields']['uid'] = [
'description' => "The {users}.uid of the author of the beatle entry.",
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
];
return $schema;
}
protected function viewsData() {
$data = parent::viewsData();
$data['views_test_data']['uid'] = [
'title' => t('UID'),
'help' => t('The test data UID'),
'relationship' => [
'id' => 'standard',
'base' => 'users_field_data',
'base field' => 'uid',
],
];
$data['views_test_data']['no_help'] = $data['views_test_data']['name'];
$data['views_test_data']['no_help']['field']['title'] = t('No help');
$data['views_test_data']['no_help']['field']['real field'] = 'name';
unset($data['views_test_data']['no_help']['help']);
return $data;
}
public function testUICRUD() {
$handler_types = ViewExecutable::getHandlerTypes();
foreach ($handler_types as $type => $type_info) {
$add_handler_url = "admin/structure/views/nojs/add-handler/test_view_empty/default/{$type}";
if (in_array($type, [
'header',
'footer',
'empty',
])) {
$this
->drupalPostForm($add_handler_url, [
'name[views.area]' => TRUE,
], t('Add and configure @handler', [
'@handler' => $type_info['ltitle'],
]));
$id = 'area';
$edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/{$type}/{$id}";
}
elseif ($type == 'relationship') {
$this
->drupalPostForm($add_handler_url, [
'name[views_test_data.uid]' => TRUE,
], t('Add and configure @handler', [
'@handler' => $type_info['ltitle'],
]));
$id = 'uid';
$edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/{$type}/{$id}";
}
else {
$this
->drupalPostForm($add_handler_url, [
'name[views_test_data.job]' => TRUE,
], t('Add and configure @handler', [
'@handler' => $type_info['ltitle'],
]));
$id = 'job';
$edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/{$type}/{$id}";
}
$this
->assertUrl($edit_handler_url, [], 'The user got redirected to the handler edit form.');
$random_label = $this
->randomMachineName();
$this
->drupalPostForm(NULL, [
'options[admin_label]' => $random_label,
], t('Apply'));
$this
->assertUrl('admin/structure/views/view/test_view_empty/edit/default', [], 'The user got redirected to the views edit form.');
$this
->assertLinkByHref($edit_handler_url, 0, 'The handler edit link appears in the UI.');
$links = $this
->xpath('//a[starts-with(normalize-space(text()), :label)]', [
':label' => $random_label,
]);
$this
->assertTrue(isset($links[0]), 'The handler edit link has the right label');
$this
->drupalPostForm(NULL, [], t('Save'));
$view = $this->container
->get('entity_type.manager')
->getStorage('view')
->load('test_view_empty');
$display = $view
->getDisplay('default');
$this
->assertTrue(isset($display['display_options'][$type_info['plural']][$id]), 'Ensure the field was added to the view itself.');
$this
->drupalPostForm($edit_handler_url, [], t('Remove'));
$this
->assertNoLinkByHref($edit_handler_url, 0, 'The handler edit link does not appears in the UI after removing.');
$this
->drupalPostForm(NULL, [], t('Save'));
$view = $this->container
->get('entity_type.manager')
->getStorage('view')
->load('test_view_empty');
$display = $view
->getDisplay('default');
$this
->assertFalse(isset($display['display_options'][$type_info['plural']][$id]), 'Ensure the field was removed from the view itself.');
}
$type_info = $handler_types['relationship'];
$add_handler_url = "admin/structure/views/nojs/add-handler/test_view_empty/default/relationship";
$this
->drupalPostForm($add_handler_url, [
'name[views_test_data.uid]' => TRUE,
], t('Add and configure @handler', [
'@handler' => $type_info['ltitle'],
]));
$add_handler_url = "admin/structure/views/nojs/add-handler/test_view_empty/default/field";
$type_info = $handler_types['field'];
$this
->drupalPostForm($add_handler_url, [
'name[users_field_data.name]' => TRUE,
], t('Add and configure @handler', [
'@handler' => $type_info['ltitle'],
]));
$id = 'name';
$edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/field/{$id}";
$this
->assertUrl($edit_handler_url, [], 'The user got redirected to the handler edit form.');
$this
->assertFieldByName('options[relationship]', 'uid', 'Ensure the relationship select is filled with the UID relationship.');
$this
->drupalPostForm(NULL, [], t('Apply'));
$this
->drupalPostForm(NULL, [], t('Save'));
$view = $this->container
->get('entity_type.manager')
->getStorage('view')
->load('test_view_empty');
$display = $view
->getDisplay('default');
$this
->assertTrue(isset($display['display_options'][$type_info['plural']][$id]), 'Ensure the field was added to the view itself.');
}
public function testHandlerHelpEscaping() {
$this
->drupalCreateContentType([
'type' => 'article',
]);
$this
->drupalCreateContentType([
'type' => 'page',
]);
FieldStorageConfig::create([
'field_name' => 'field_test',
'entity_type' => 'node',
'type' => 'string',
])
->save();
FieldConfig::create([
'field_name' => 'field_test',
'entity_type' => 'node',
'bundle' => 'page',
'label' => 'The giraffe" label',
])
->save();
FieldConfig::create([
'field_name' => 'field_test',
'entity_type' => 'node',
'bundle' => 'article',
'label' => 'The <em>giraffe"</em> label <script>alert("the return of the xss")</script>',
])
->save();
$this
->drupalGet('admin/structure/views/nojs/add-handler/content/default/field');
$this
->assertEscaped('The <em>giraffe"</em> label <script>alert("the return of the xss")</script>');
$this
->assertEscaped('Appears in: page, article. Also known as: Content: The giraffe" label');
}
public function testBrokenHandlers() {
$handler_types = ViewExecutable::getHandlerTypes();
foreach ($handler_types as $type => $type_info) {
$this
->drupalGet('admin/structure/views/view/test_view_broken/edit');
$href = "admin/structure/views/nojs/handler/test_view_broken/default/{$type}/id_broken";
$result = $this
->xpath('//a[contains(@href, :href)]', [
':href' => $href,
]);
$this
->assertCount(1, $result, new FormattableMarkup('Handler (%type) edit link found.', [
'%type' => $type,
]));
$text = 'Broken/missing handler';
$this
->assertIdentical($result[0]
->getText(), $text, 'Ensure the broken handler text was found.');
$this
->drupalGet($href);
$result = $this
->xpath('//h1[@class="page-title"]');
$this
->assertStringContainsString($text, $result[0]
->getText(), 'Ensure the broken handler text was found.');
$original_configuration = [
'field' => 'id_broken',
'id' => 'id_broken',
'relationship' => 'none',
'table' => 'views_test_data',
'plugin_id' => 'numeric',
];
foreach ($original_configuration as $key => $value) {
$this
->assertText(new FormattableMarkup('@key: @value', [
'@key' => $key,
'@value' => $value,
]));
}
}
}
public function testNoDuplicateFields() {
$handler_types = [
'field',
'filter',
'sort',
'argument',
];
foreach ($handler_types as $handler_type) {
$add_handler_url = 'admin/structure/views/nojs/add-handler/test_node_view/default/' . $handler_type;
$this
->drupalGet($add_handler_url);
$this
->assertNoDuplicateField('ID', 'Content');
$this
->assertNoDuplicateField('ID', 'Content revision');
$this
->assertNoDuplicateField('Content type', 'Content');
$this
->assertNoDuplicateField('UUID', 'Content');
$this
->assertNoDuplicateField('Revision ID', 'Content');
$this
->assertNoDuplicateField('Revision ID', 'Content revision');
}
}
public function testErrorMissingHelp() {
$this
->drupalGet('admin/structure/views/nojs/add-handler/test_node_view/default/field');
$this
->assertNoText('Error: missing help');
$this
->assertRaw('<td class="description"></td>', 'Empty description found');
$this
->drupalGet('admin/structure/views/nojs/add-handler/test_view_empty/default/field');
$this
->assertNoText('Error: missing help');
}
public function assertNoDuplicateField($field_name, $entity_type) {
$elements = $this
->xpath('//td[.=:entity_type]/preceding-sibling::td[@class="title" and .=:title]', [
':title' => $field_name,
':entity_type' => $entity_type,
]);
$this
->assertCount(1, $elements, $field_name . ' appears just once in ' . $entity_type . '.');
}
}