View source
<?php
namespace Drupal\Tests\devel\Functional;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Tests\BrowserTestBase;
class DevelToolbarTest extends BrowserTestBase {
public static $modules = [
'devel',
'toolbar',
'block',
];
protected $toolbarUser;
protected $develUser;
protected $defaultToolbarItems = [
'devel.cache_clear',
'devel.container_info.service',
'devel.admin_settings_link',
'devel.execute_php',
'devel.menu_rebuild',
'devel.reinstall',
'devel.route_info',
'devel.run_cron',
];
public function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalPlaceBlock('page_title_block');
$this->develUser = $this
->drupalCreateUser([
'administer site configuration',
'access devel information',
'execute php code',
'access toolbar',
]);
$this->toolbarUser = $this
->drupalCreateUser([
'access toolbar',
]);
}
public function testConfigurationForm() {
$this
->drupalGet('admin/config/development/devel/toolbar');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this->develUser);
$this
->drupalGet('admin/config/development/devel/toolbar');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->elementExists('css', '.tabs .primary a:contains("Toolbar Settings")');
$this
->assertSession()
->pageTextContains('Devel Toolbar Settings');
foreach ($this
->getMenuLinkInfos() as $link) {
$this
->assertSession()
->fieldExists(sprintf('toolbar_items[%s]', $link['id']));
}
foreach ($this->defaultToolbarItems as $item) {
$this
->assertSession()
->checkboxChecked(sprintf('toolbar_items[%s]', $item));
}
$edit = [
'toolbar_items[devel.event_info]' => 'devel.event_info',
'toolbar_items[devel.theme_registry]' => 'devel.theme_registry',
];
$this
->drupalPostForm('admin/config/development/devel/toolbar', $edit, t('Save configuration'));
$this
->assertSession()
->pageTextContains('The configuration options have been saved.');
$expected_items = array_merge($this->defaultToolbarItems, [
'devel.event_info',
'devel.theme_registry',
]);
sort($expected_items);
$config_items = \Drupal::config('devel.toolbar.settings')
->get('toolbar_items');
sort($config_items);
$this
->assertEquals($expected_items, $config_items);
}
public function testCacheHeaders() {
\Drupal::service('module_installer')
->install([
'toolbar_disable_user_toolbar',
]);
$this
->drupalLogin($this->toolbarUser);
$this
->assertSession()
->responseHeaderNotContains('X-Drupal-Cache-Tags', 'config:devel.toolbar.settings');
$this
->assertSession()
->responseHeaderNotContains('X-Drupal-Cache-Tags', 'config:system.menu.devel');
$this
->drupalLogin($this->develUser);
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'config:devel.toolbar.settings');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'config:system.menu.devel');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Dynamic-Cache', 'MISS');
$this
->drupalGet('');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Dynamic-Cache', 'HIT');
}
public function testToolbarIntegration() {
$library_css_url = 'devel/css/devel.toolbar.css';
$toolbar_selector = '#toolbar-bar .toolbar-tab';
$toolbar_tab_selector = '#toolbar-bar .toolbar-tab a.toolbar-icon-devel';
$toolbar_tray_selector = '#toolbar-bar .toolbar-tab #toolbar-item-devel-tray';
$this
->drupalGet('');
$this
->assertSession()
->responseNotContains($library_css_url);
$this
->assertSession()
->elementNotExists('css', $toolbar_selector);
$this
->assertSession()
->elementNotExists('css', $toolbar_tab_selector);
$this
->drupalLogin($this->toolbarUser);
$this
->assertSession()
->responseNotContains($library_css_url);
$this
->assertSession()
->elementExists('css', $toolbar_selector);
$this
->assertSession()
->elementNotExists('css', $toolbar_tab_selector);
$this
->drupalLogin($this->develUser);
$this
->assertSession()
->responseContains($library_css_url);
$this
->assertSession()
->elementExists('css', $toolbar_selector);
$this
->assertSession()
->elementExists('css', $toolbar_tab_selector);
$this
->assertSession()
->elementTextContains('css', $toolbar_tab_selector, 'Devel');
$this
->clickLink('Configure');
$this
->assertSession()
->addressEquals('admin/config/development/devel/toolbar');
$this
->drupalGet('');
$toolbar_tray = $this
->assertSession()
->elementExists('css', $toolbar_tray_selector);
$devel_menu_items = $this
->getMenuLinkInfos();
$toolbar_items = $toolbar_tray
->findAll('css', 'ul.toolbar-menu a');
$this
->assertCount(count($devel_menu_items), $toolbar_items);
foreach ($devel_menu_items as $link) {
$item_selector = sprintf('ul.toolbar-menu a:contains("%s")', $link['title']);
$item = $this
->assertSession()
->elementExists('css', $item_selector, $toolbar_tray);
$this
->assertContains(strtok($link['url'], '?'), $item
->getAttribute('href'));
$not_visible = !in_array($link['id'], $this->defaultToolbarItems);
$this
->assertTrue($not_visible === $item
->hasClass('toolbar-horizontal-item-hidden'));
}
$saved_items = $this
->config('devel.toolbar.settings')
->get('toolbar_items');
$saved_items[] = 'devel.event_info';
$this
->config('devel.toolbar.settings')
->set('toolbar_items', $saved_items)
->save();
$this
->drupalGet('');
$toolbar_tray = $this
->assertSession()
->elementExists('css', $toolbar_tray_selector);
$item = $this
->assertSession()
->elementExists('css', sprintf('ul.toolbar-menu a:contains("%s")', 'Events Info'), $toolbar_tray);
$this
->assertFalse($item
->hasClass('toolbar-horizontal-item-hidden'));
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
$menu_link_manager
->updateDefinition('devel.event_info', [
'enabled' => FALSE,
]);
$this
->drupalGet('');
$toolbar_tray = $this
->assertSession()
->elementExists('css', $toolbar_tray_selector);
$this
->assertSession()
->elementNotExists('css', sprintf('ul.toolbar-menu a:contains("%s")', 'Events Info'), $toolbar_tray);
}
public function testToolbarModuleNotInstalled() {
\Drupal::service('module_installer')
->uninstall([
'toolbar',
]);
$this
->drupalLogin($this->develUser);
$this
->drupalGet('admin/config/development/devel/toolbar');
$this
->assertSession()
->statusCodeEquals(404);
$this
->drupalGet('admin/config/development/devel');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->elementNotExists('css', '.tabs .primary a:contains("Toolbar Settings")');
$this
->drupalGet('');
$this
->assertSession()
->responseHeaderNotContains('X-Drupal-Cache-Tags', 'config:devel.toolbar.settings');
$this
->assertSession()
->responseHeaderNotContains('X-Drupal-Cache-Tags', 'config:system.menu.devel');
}
protected function getMenuLinkInfos() {
$parameters = new MenuTreeParameters();
$parameters
->onlyEnabledLinks()
->setTopLevelOnly();
$tree = \Drupal::menuTree()
->load('devel', $parameters);
$links = [];
foreach ($tree as $element) {
$links[] = [
'id' => $element->link
->getPluginId(),
'title' => $element->link
->getTitle(),
'url' => $element->link
->getUrlObject()
->toString(),
];
}
return $links;
}
}