DisabledDisplayTest.php in Drupal 10
File
core/modules/views/tests/src/Functional/Plugin/DisabledDisplayTest.php
View source
<?php
namespace Drupal\Tests\views\Functional\Plugin;
use Drupal\Tests\views\Functional\ViewTestBase;
class DisabledDisplayTest extends ViewTestBase {
public static $testViews = [
'test_disabled_display',
];
protected static $modules = [
'block',
'node',
'views',
];
protected $defaultTheme = 'classy';
protected function setUp($import_test_views = TRUE, $modules = [
'views_test_config',
]) : void {
parent::setUp($import_test_views, $modules);
$this
->enableViewsTestModule();
$this
->drupalPlaceBlock('page_title_block');
$admin_user = $this
->drupalCreateUser([
'administer site configuration',
]);
$this
->drupalLogin($admin_user);
}
public function testDisabledDisplays() {
$display_ids = [
'attachment_1',
'block_1',
'embed_1',
'feed_1',
'page_2',
];
$this
->drupalCreateContentType([
'type' => 'page',
]);
$this
->drupalCreateNode();
$view = $this->container
->get('entity_type.manager')
->getStorage('view')
->load('test_disabled_display');
$view
->getExecutable()
->setDisplay();
$this
->drupalGet('test-disabled-display');
$result = $this
->xpath('//h1[@class="page-title"]');
$this
->assertEquals('test_disabled_display', $result[0]
->getText(), 'The enabled page_1 display is accessible.');
$this
->drupalGet('test-disabled-display-2');
$this
->assertSession()
->statusCodeEquals(404);
foreach ($display_ids as $display_id) {
$view
->getExecutable()->displayHandlers
->get($display_id)
->setOption('enabled', TRUE);
$view
->save();
$enabled = $view
->getExecutable()->displayHandlers
->get($display_id)
->isEnabled();
$this
->assertTrue($enabled, 'Display ' . $display_id . ' is now enabled');
}
\Drupal::service('router.builder')
->rebuildIfNeeded();
$this
->drupalGet('test-disabled-display-2');
$result = $this
->xpath('//h1[@class="page-title"]');
$this
->assertEquals('test_disabled_display', $result[0]
->getText(), 'The enabled page_2 display is accessible.');
foreach ($display_ids as $display_id) {
$view
->getExecutable()->displayHandlers
->get($display_id)
->setOption('enabled', FALSE);
$view
->save();
$enabled = $view
->getExecutable()->displayHandlers
->get($display_id)
->isEnabled();
$this
->assertFalse($enabled, 'Display ' . $display_id . ' is now disabled');
}
\Drupal::service('router.builder')
->rebuild();
$this
->drupalGet('test-disabled-display');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('test-disabled-display-2');
$this
->assertSession()
->statusCodeEquals(404);
}
}