View source
<?php
namespace Drupal\Tests\views\Functional\Plugin;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Views;
use Drupal\views_test_data\Plugin\views\display\DisplayTest as DisplayTestPlugin;
class DisplayTest extends ViewTestBase {
public static $testViews = [
'test_filter_groups',
'test_get_attach_displays',
'test_view',
'test_display_more',
'test_display_invalid',
'test_display_empty',
'test_exposed_relationship_admin_ui',
'test_simple_argument',
];
protected static $modules = [
'views_ui',
'node',
'block',
];
protected $defaultTheme = 'stark';
protected function setUp($import_test_views = TRUE) : void {
parent::setUp();
$this
->enableViewsTestModule();
$this->adminUser = $this
->drupalCreateUser([
'administer views',
]);
$this
->drupalLogin($this->adminUser);
for ($i = 0; $i <= 10; $i++) {
$this
->drupalCreateNode([
'promote' => TRUE,
]);
}
}
public function testDisplayPlugin() {
$renderer = $this->container
->get('renderer');
$view = Views::getView('test_view');
$view->storage
->addDisplay('display_test');
$displays = $view->storage
->get('display');
$this
->assertTrue(isset($displays['display_test_1']), 'Added display has been assigned to "display_test_1"');
$options = [
'display_options' => [],
'display_plugin' => 'display_test',
'id' => 'display_test_1',
'display_title' => 'Display test',
'position' => 1,
];
$this
->assertEquals($options, $displays['display_test_1']);
$view->storage
->addDisplay('display_test');
$displays = $view->storage
->get('display');
$options = [
'display_options' => [],
'display_plugin' => 'display_test',
'id' => 'display_test_2',
'display_title' => 'Display test 2',
'position' => 2,
];
$this
->assertEquals($options, $displays['display_test_2']);
$displays['display_test_1']['position'] = 2;
$displays['display_test_2']['position'] = 1;
$view->storage
->set('display', $displays);
$view
->save();
$view
->setDisplay('display_test_1');
$this
->assertInstanceOf(DisplayTestPlugin::class, $view->display_handler);
$this
->assertSame('', $view->display_handler
->getOption('test_option'));
$style = $view->display_handler
->getOption('style');
$style['type'] = 'test_style';
$view->display_handler
->setOption('style', $style);
$view
->initDisplay();
$view
->initStyle();
$view->style_plugin
->setUsesRowPlugin(FALSE);
$output = $view
->preview();
$output = $renderer
->renderRoot($output);
$this
->assertStringContainsString('<h1></h1>', $output, 'An empty value for test_option found in output.');
$view->display_handler
->overrideOption('test_option', 'Test option title');
$view
->save();
$output = $view
->preview();
$output = $renderer
->renderRoot($output);
$this
->assertStringContainsString('<h1>Test option title</h1>', $output, 'The test_option value found in display output title.');
$this
->drupalGet('admin/structure/views/view/test_view/edit/display_test_1');
$this
->assertSession()
->pageTextContains('Display test settings');
$result = $this
->xpath('//ul[@id="views-display-menu-tabs"]/li/a/child::text()');
$this
->assertEquals('Display test 2', $result[0]
->getText());
$this
->assertEquals('Display test', $result[1]
->getText());
$this
->clickLink('Test option title');
$test_option = $this
->randomString();
$this
->submitForm([
'test_option' => $test_option,
], 'Apply');
$this
->drupalGet('admin/structure/views/view/test_view/edit/display_test_1');
$this
->assertSession()
->linkExists($test_option);
$view->display_handler
->setOption('enabled', FALSE);
$this
->assertFalse($view->display_handler
->isEnabled(), 'Make sure that isEnabled returns FALSE on a disabled display.');
$view->display_handler
->setOption('enabled', TRUE);
$this
->assertTrue($view->display_handler
->isEnabled(), 'Make sure that isEnabled returns TRUE on a disabled display.');
}
public function testFilterGroupsOverriding() {
$view = Views::getView('test_filter_groups');
$view
->initDisplay();
$view->displayHandlers
->get('page')
->setOverride('filter_groups', FALSE);
$this
->assertFalse($view->displayHandlers
->get('page')
->isDefaulted('filter_groups'), "Make sure that 'filter_groups' is marked as overridden.");
$this
->assertFalse($view->displayHandlers
->get('page')
->isDefaulted('filters'), "Make sure that 'filters'' is marked as overridden.");
}
public function testGetAttachedDisplays() {
$view = Views::getView('test_get_attach_displays');
$view
->setDisplay('page_1');
$this
->assertEquals([
'feed_1',
'feed_2',
], $view->display_handler
->getAttachedDisplays());
$view
->setDisplay('feed_1');
$this
->assertEquals([], $view->display_handler
->getAttachedDisplays());
}
public function testReadMoreNoDisplay() {
$view = Views::getView('test_display_more');
$errors = $view
->validate();
$this
->assertTrue(empty($errors), 'More link validation has no errors.');
$view
->setDisplay('page_1');
$view->display_handler
->setOption('enabled', FALSE);
$view
->setDisplay('default');
$errors = $view
->validate();
$this
->assertTrue(!empty($errors), 'More link validation has some errors.');
$this
->assertEquals('Display "Default" uses a "more" link but there are no displays it can link to. You need to specify a custom URL.', $errors['default'][0], 'More link validation has the right error.');
$view = Views::getView('test_view');
$view
->setDisplay('default');
$view->display_handler
->setOption('use_more', 1);
$errors = $view
->validate();
$this
->assertTrue(!empty($errors), 'More link validation has some errors.');
$this
->assertEquals('Display "Default" uses a "more" link but there are no displays it can link to. You need to specify a custom URL.', $errors['default'][0], 'More link validation has the right error.');
}
public function testReadMoreCustomURL() {
$renderer = $this->container
->get('renderer');
$view = Views::getView('test_display_more');
$view
->setDisplay('default');
$view->display_handler
->setOption('use_more', 1);
$view->display_handler
->setOption('use_more_always', 1);
$view->display_handler
->setOption('link_display', 'custom_url');
$view->display_handler
->setOption('link_url', 'node');
$this
->executeView($view);
$output = $view
->preview();
$output = $renderer
->renderRoot($output);
$this
->assertStringContainsString('/node', $output, 'The read more link with href "/node" was found.');
$view->display_handler
->setOption('link_display', 'custom_url');
$view->display_handler
->setOption('link_url', '/node');
$this
->executeView($view);
$output = $view
->preview();
$output = $renderer
->renderRoot($output);
$this
->assertStringContainsString('/node', $output, 'The read more link with href "/node" was found.');
$view->display_handler
->setOption('link_display', 'custom_url');
$view->display_handler
->setOption('link_url', 'http://drupal.org');
$this
->executeView($view);
$output = $view
->preview();
$output = $renderer
->renderRoot($output);
$this
->assertStringContainsString('http://drupal.org', $output, 'The read more link with href "http://drupal.org" was found.');
$view->display_handler
->setOption('link_display', 'custom_url');
$view->display_handler
->setOption('link_url', 'node?page=1&foo=bar');
$this
->executeView($view);
$output = $view
->preview();
$output = $renderer
->renderRoot($output);
$this
->assertStringContainsString('/node?page=1&foo=bar', $output, 'The read more link with href "/node?page=1&foo=bar" was found.');
$view->display_handler
->setOption('link_display', 'custom_url');
$view->display_handler
->setOption('link_url', 'node#target');
$this
->executeView($view);
$output = $view
->preview();
$output = $renderer
->renderRoot($output);
$this
->assertStringContainsString('/node#target', $output, 'The read more link with href "/node#target" was found.');
$view = Views::getView('test_simple_argument');
$view
->setDisplay('default');
$view->display_handler
->setOption('use_more', 1);
$view->display_handler
->setOption('use_more_always', 1);
$view->display_handler
->setOption('link_display', 'custom_url');
$view->display_handler
->setOption('link_url', 'node?date={{ raw_arguments.age }}&foo=bar');
$view
->setArguments([
22,
]);
$this
->executeView($view);
$output = $view
->preview();
$output = $renderer
->renderRoot($output);
$this
->assertStringContainsString('/node?date=22&foo=bar', $output, 'The read more link with href "/node?date=22&foo=bar" was found.');
$view = Views::getView('test_simple_argument');
$view
->setDisplay('default');
$view->display_handler
->setOption('use_more', 1);
$view->display_handler
->setOption('use_more_always', 1);
$view->display_handler
->setOption('link_display', 'custom_url');
$view->display_handler
->setOption('link_url', '/node?f[0]=foo:bar&f[1]=foo:{{ raw_arguments.age }}');
$view
->setArguments([
22,
]);
$this
->executeView($view);
$output = $view
->preview();
$output = $renderer
->renderRoot($output);
$this
->assertStringContainsString('/node?f%5B0%5D=foo%3Abar&f%5B1%5D=foo%3A22', $output, 'The read more link with href "/node?f[0]=foo:bar&f[1]=foo:22" was found.');
$view->display_handler
->setOption('link_url', 'node/{{ raw_arguments.age }}?date={{ raw_arguments.age }}&foo=bar');
$view
->setArguments([
22,
]);
$this
->executeView($view);
$output = $view
->preview();
$output = $renderer
->renderRoot($output);
$this
->assertStringContainsString('/node/22?date=22&foo=bar', $output, 'The read more link with href "/node/22?date=22&foo=bar" was found.');
$view->display_handler
->setOption('link_url', 'node?date={{ raw_arguments.age }}&foo=bar#{{ raw_arguments.age }}');
$view
->setArguments([
22,
]);
$this
->executeView($view);
$output = $view
->preview();
$output = $renderer
->renderRoot($output);
$this
->assertStringContainsString('/node?date=22&foo=bar#22', $output, 'The read more link with href "/node?date=22&foo=bar#22" was found.');
}
public function testInvalidDisplayPlugins() {
$this
->drupalGet('test_display_invalid');
$this
->assertSession()
->statusCodeEquals(200);
$config = $this
->config('views.view.test_display_invalid');
$config
->set('display.page_1.display_plugin', 'invalid');
$config
->save();
$this
->drupalGet('test_display_invalid');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('The "invalid" plugin does not exist.');
views_invalidate_cache();
\Drupal::service('router.builder')
->rebuildIfNeeded();
$this
->drupalGet('test_display_invalid');
$this
->assertSession()
->statusCodeEquals(404);
$config = $this
->config('views.view.test_display_invalid');
$config
->set('display.page_1.display_plugin', 'page');
$config
->save();
$block = $this
->drupalPlaceBlock('views_block:test_display_invalid-block_1', [
'label' => 'Invalid display',
]);
$this
->drupalGet('<front>');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->elementsCount('xpath', "//div[@id = 'block-{$block->id()}']", 1);
$config = $this
->config('views.view.test_display_invalid');
$config
->set('display.block_1.display_plugin', 'invalid');
$config
->save();
$this
->drupalGet('<front>');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('The "invalid" plugin does not exist.');
$this
->assertSession()
->elementNotExists('xpath', "//div[@id = 'block-{$block->id()}']");
}
public function testMissingRelationship() {
$view = Views::getView('test_exposed_relationship_admin_ui');
$view
->removeHandler('default', 'relationship', 'uid_1');
$errors = $view
->validate();
$this
->assertTrue(empty($errors['default']), 'No errors found when removing unused relationship.');
unset($view->display_handler->handlers['relationship']);
$view
->removeHandler('default', 'relationship', 'uid');
$errors = $view
->validate();
$this
->assertCount(2, $errors['default'], 'Error messages found for required relationship');
$this
->assertEquals(t('The %handler_type %handler uses a relationship that has been removed.', [
'%handler_type' => 'field',
'%handler' => 'User: Last login',
]), $errors['default'][0]);
$this
->assertEquals(t('The %handler_type %handler uses a relationship that has been removed.', [
'%handler_type' => 'field',
'%handler' => 'User: Created',
]), $errors['default'][1]);
}
public function testOutputIsEmpty() {
$view = Views::getView('test_display_empty');
$this
->executeView($view);
$this
->assertNotEmpty($view->result);
$this
->assertFalse($view->display_handler
->outputIsEmpty(), 'Ensure the view output is marked as not empty.');
$view
->destroy();
$view
->setDisplay('default');
$item = [
'table' => 'views_test_data',
'field' => 'id',
'id' => 'id',
'value' => [
'value' => 7297,
],
];
$view
->setHandler('default', 'filter', 'id', $item);
$this
->executeView($view);
$this
->assertEmpty($view->result, 'Ensure the result of the view is empty.');
$this
->assertFalse($view->display_handler
->outputIsEmpty(), 'Ensure the view output is marked as not empty, because the empty text still appears.');
$view
->destroy();
$view
->removeHandler('default', 'empty', 'area');
$item = $view
->getHandler('default', 'header', 'area');
$item['empty'] = TRUE;
$view
->setHandler('default', 'header', 'area', $item);
$this
->executeView($view);
$this
->assertEmpty($view->result, 'Ensure the result of the view is empty.');
$this
->assertFalse($view->display_handler
->outputIsEmpty(), 'Ensure the view output is marked as not empty, because the header text still appears.');
$view
->destroy();
$item = $view
->getHandler('default', 'header', 'area');
$item['empty'] = FALSE;
$view
->setHandler('default', 'header', 'area', $item);
$this
->executeView($view);
$this
->assertEmpty($view->result, 'Ensure the result of the view is empty.');
$this
->assertTrue($view->display_handler
->outputIsEmpty(), 'Ensure the view output is marked as empty.');
}
public function testTranslationSetting() {
\Drupal::service('module_installer')
->install([
'file',
]);
$this
->checkTranslationSetting();
\Drupal::service('module_installer')
->install([
'language',
]);
$this
->checkTranslationSetting();
ConfigurableLanguage::createFromLangcode('it')
->save();
$this
->checkTranslationSetting(TRUE);
}
protected function checkTranslationSetting($expected_node_translatability = FALSE) {
$not_supported_text = 'The view is not based on a translatable entity type or the site is not multilingual.';
$supported_text = 'All content that supports translations will be displayed in the selected language.';
$this
->drupalGet('admin/structure/views/nojs/display/content/page_1/rendering_language');
if ($expected_node_translatability) {
$this
->assertSession()
->pageTextNotContains($not_supported_text);
$this
->assertSession()
->pageTextContains($supported_text);
}
else {
$this
->assertSession()
->pageTextContains($not_supported_text);
$this
->assertSession()
->pageTextNotContains($supported_text);
}
$this
->drupalGet('admin/structure/views/nojs/display/files/page_1/rendering_language');
$this
->assertSession()
->pageTextContains($not_supported_text);
$this
->assertSession()
->pageTextNotContains($supported_text);
}
}