function DevelToolbarTest::testToolbarIntegration

Same name in other branches
  1. 4.x tests/src/Functional/DevelToolbarTest.php \Drupal\Tests\devel\Functional\DevelToolbarTest::testToolbarIntegration()

Tests toolbar integration.

File

tests/src/Functional/DevelToolbarTest.php, line 143

Class

DevelToolbarTest
Tests devel toolbar module functionality.

Namespace

Drupal\Tests\devel\Functional

Code

public function testToolbarIntegration() : void {
    $library_css_url = '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';
    // Ensures that devel toolbar item is accessible only for user with the
    // adequate permissions.
    $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');
    // Ensures that the configure link in the toolbar is present and point to
    // the correct page.
    $this->clickLink('Configure');
    $this->assertSession()
        ->addressEquals('admin/config/development/devel/toolbar');
    // Ensures that the toolbar tray contains the all the menu links. To the
    // links not marked as always visible will be assigned a css class that
    // allow to hide they when the toolbar has horizontal orientation.
    $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);
        // Only test the url up to the ? as the destination and token parameters
        // will vary and are not checkable.
        $this->assertEquals(strtok($link['url'], '?'), strtok($item->getAttribute('href'), '?'));
        $not_visible = !in_array($link['id'], $this->defaultToolbarItems);
        $this->assertTrue($not_visible === $item->hasClass('toolbar-horizontal-item-hidden'));
    }
    // Ensures that changing the toolbar settings configuration the changes are
    // immediately visible.
    $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'));
    // Ensures that disabling a menu link it will not more shown in the toolbar
    // and that the changes are immediately visible.
    $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);
}