View source
<?php
namespace Drupal\Tests\views_ui\Functional;
use Drupal\views\Entity\View;
use Drupal\views\Views;
class DisplayTest extends UITestBase {
public static $testViews = [
'test_display',
];
protected static $modules = [
'contextual',
];
protected $defaultTheme = 'stark';
public function testAddDisplay() {
$view = $this
->randomView();
$this
->assertSession()
->elementNotExists('xpath', '//li[@data-drupal-selector="edit-displays-top-tabs-block-1"]');
$this
->assertSession()
->elementNotExists('xpath', '//li[@data-drupal-selector="edit-displays-top-tabs-block-2"]');
$this
->assertSession()
->pageTextMatchesCount(0, '/Block name:/');
$this
->submitForm([], 'Add Block');
$this
->assertSession()
->elementTextContains('xpath', '//li[@data-drupal-selector="edit-displays-top-tabs-block-1"]', 'Block*');
$this
->assertSession()
->elementNotExists('xpath', '//li[@data-drupal-selector="edit-displays-top-tabs-block-2"]');
$this
->assertSession()
->pageTextMatchesCount(1, '/Block name:/');
}
public function testReorderDisplay() {
$view = [
'block[create]' => TRUE,
];
$view = $this
->randomView($view);
$this
->clickLink('Reorder displays');
$this
->assertNotEmpty($this
->xpath('//tr[@id="display-row-default"]'), 'Make sure the default display appears on the reorder listing');
$this
->assertNotEmpty($this
->xpath('//tr[@id="display-row-page_1"]'), 'Make sure the page display appears on the reorder listing');
$this
->assertNotEmpty($this
->xpath('//tr[@id="display-row-block_1"]'), 'Make sure the block display appears on the reorder listing');
$expected_display_order = [
'default',
'block_1',
'page_1',
];
$this
->assertEquals($expected_display_order, array_keys(Views::getView($view['id'])->storage
->get('display')), 'The correct display names are present.');
$edit = [
'displays[page_1][weight]' => 2,
'displays[block_1][weight]' => 1,
];
$this
->submitForm($edit, 'Apply');
$this
->submitForm([], 'Save');
$view = Views::getView($view['id']);
$displays = $view->storage
->get('display');
$this
->assertEquals(0, $displays['default']['position'], 'Make sure the default display comes first.');
$this
->assertEquals(1, $displays['block_1']['position'], 'Make sure the block display comes before the page display.');
$this
->assertEquals(2, $displays['page_1']['position'], 'Make sure the page display comes after the block display.');
$this
->assertEquals($expected_display_order, array_keys($view->storage
->get('display')), 'The correct display names are present.');
}
public function testDisableDisplay() {
$view = $this
->randomView();
$path_prefix = 'admin/structure/views/view/' . $view['id'] . '/edit';
$this
->drupalGet($path_prefix);
$this
->assertSession()
->elementNotExists('xpath', "//div[contains(@class, 'views-display-disabled')]");
$this
->assertSession()
->buttonExists('edit-displays-settings-settings-content-tab-content-details-top-actions-disable');
$this
->assertSession()
->buttonNotExists('edit-displays-settings-settings-content-tab-content-details-top-actions-enable');
$this
->submitForm([], 'Disable Page');
$this
->assertSession()
->elementExists('xpath', "//div[contains(@class, 'views-display-disabled')]");
$this
->assertSession()
->buttonNotExists('edit-displays-settings-settings-content-tab-content-details-top-actions-disable');
$this
->assertSession()
->buttonExists('edit-displays-settings-settings-content-tab-content-details-top-actions-enable');
$this
->submitForm([], 'Enable Page');
$this
->assertSession()
->elementNotExists('xpath', "//div[contains(@class, 'views-display-disabled')]");
}
public function testDisplayPluginsAlter() {
$definitions = Views::pluginManager('display')
->getDefinitions();
$expected = [
'route_name' => 'entity.view.edit_form',
'route_parameters_names' => [
'view' => 'id',
],
];
foreach ($definitions as $definition) {
$this
->assertSame($expected, $definition['contextual links']['entity.view.edit_form'], 'Expected views_ui array found in plugin definition.');
}
}
public function testDisplayAreas() {
$this
->config('views.settings')
->set('ui.show.advanced_column', TRUE)
->save();
$view = Views::getView('test_display');
$view->storage
->addDisplay('display_no_area_test');
$view
->save();
$this
->drupalGet('admin/structure/views/view/test_display/edit/display_no_area_test_1');
$areas = [
'header',
'footer',
'empty',
];
foreach ($areas as $type) {
$this
->assertSession()
->elementTextEquals('xpath', "//div[contains(@class, '{$type}')]/div", "The selected display type does not use {$type} plugins");
}
}
public function testLinkDisplay() {
$path = 'admin/structure/views/view/test_display/edit/block_1';
$link_display_path = 'admin/structure/views/nojs/display/test_display/block_1/link_display';
$this
->drupalGet($path);
$this
->assertSession()
->elementTextEquals('xpath', "//a[contains(@href, '{$link_display_path}')]", 'None');
$this
->drupalGet($link_display_path);
$this
->assertSession()
->checkboxChecked('edit-link-display-0');
$this
->drupalGet($link_display_path);
$this
->submitForm([
'link_display' => 'page_1',
], 'Apply');
$this
->drupalGet($path);
$this
->assertSession()
->elementTextEquals('xpath', "//a[contains(@href, '{$link_display_path}')]", 'Page');
$this
->drupalGet($link_display_path);
$this
->submitForm([
'link_display' => 'custom_url',
'link_url' => 'a-custom-url',
], 'Apply');
$this
->drupalGet($path);
$this
->assertSession()
->linkExists('Custom URL', 0, 'The link option has custom URL as summary.');
$this
->submitForm([], 'Add Block');
$this
->assertSession()
->addressEquals('admin/structure/views/view/test_display/edit/block_2');
$this
->clickLink('Custom URL');
$this
->assertSession()
->fieldValueEquals('link_url', 'a-custom-url');
}
public function testViewStatus() {
$view = $this
->randomView();
$id = $view['id'];
$this
->drupalGet('admin/structure/views/view/' . $id);
$this
->assertSession()
->elementExists('xpath', "//div[contains(@class, 'views-edit-view') and contains(@class, 'enabled')]");
$view = Views::getView($id);
$view->storage
->disable()
->save();
$this
->drupalGet('admin/structure/views/view/' . $id);
$this
->assertSession()
->elementExists('xpath', "//div[contains(@class, 'views-edit-view') and contains(@class, 'disabled')]");
}
public function testDisplayTitleInButtonsXss() {
$xss_markup = '"><script>alert(123)</script>';
$view = $this
->randomView();
$view = View::load($view['id']);
\Drupal::configFactory()
->getEditable('views.settings')
->set('ui.show.default_display', TRUE)
->save();
foreach ([
$xss_markup,
'"><script>alert(123)</script>',
] as $input) {
$display =& $view
->getDisplay('page_1');
$display['display_title'] = $input;
$view
->save();
$this
->drupalGet("admin/structure/views/view/{$view->id()}");
$escaped = views_ui_truncate($input, 25);
$this
->assertSession()
->assertEscaped($escaped);
$this
->assertSession()
->responseNotContains($xss_markup);
$this
->drupalGet("admin/structure/views/view/{$view->id()}/edit/page_1");
$this
->assertSession()
->assertEscaped("View {$escaped}");
$this
->assertSession()
->responseNotContains("View {$xss_markup}");
$this
->assertSession()
->assertEscaped("Duplicate {$escaped}");
$this
->assertSession()
->responseNotContains("Duplicate {$xss_markup}");
$this
->assertSession()
->assertEscaped("Delete {$escaped}");
$this
->assertSession()
->responseNotContains("Delete {$xss_markup}");
}
}
public function testActionLinks() {
$display_title = "'<test>'";
$this
->drupalGet('admin/structure/views/view/test_display');
$display_title_path = 'admin/structure/views/nojs/display/test_display/block_1/display_title';
$this
->drupalGet($display_title_path);
$this
->submitForm([
'display_title' => $display_title,
], 'Apply');
$this
->assertSession()
->assertEscaped($display_title);
$this
->assertSession()
->responseNotContains($display_title);
$this
->assertSession()
->buttonExists('Duplicate ' . $display_title);
$this
->assertSession()
->buttonExists('Delete ' . $display_title);
$this
->assertSession()
->buttonExists('Disable ' . $display_title);
$this
->assertSession()
->buttonNotExists('Enable ' . $display_title);
$this
->submitForm([], 'Disable ' . $display_title);
$this
->assertSession()
->buttonExists('Enable ' . $display_title);
$this
->assertSession()
->buttonNotExists('Disable ' . $display_title);
$this
->assertSession()
->assertEscaped($display_title);
$this
->assertSession()
->responseNotContains($display_title);
}
public function testHideDisplayOverride() {
$this
->drupalGet('admin/structure/views/nojs/handler/test_display/page_1/field/title');
$this
->assertSession()
->pageTextContains('All displays');
$this
->drupalGet('admin/structure/views/view/test_display/edit/block_1');
$this
->submitForm([], 'Delete Block');
$this
->submitForm([], 'Save');
$this
->drupalGet('admin/structure/views/nojs/handler/test_display/page_1/field/title');
$this
->assertSession()
->pageTextNotContains('All displays');
\Drupal::configFactory()
->getEditable('views.settings')
->set('ui.show.default_display', TRUE)
->save();
$this
->drupalGet('admin/structure/views/nojs/handler/test_display/page_1/field/title');
$this
->assertSession()
->pageTextContains('All displays');
$this
->submitForm([
'override[dropdown]' => 'page_1',
], 'Apply');
\Drupal::configFactory()
->getEditable('views.settings')
->set('ui.show.default_display', FALSE)
->save();
$this
->drupalGet('admin/structure/views/nojs/handler/test_display/page_1/field/title');
$this
->assertSession()
->pageTextContains('Revert to default');
}
}