Commit 66f87070 authored by David Suissa's avatar David Suissa Committed by David Suissa
Browse files

Issue #3540080 by dydave: Automated tests: Combined several Functional test classes.

parent 7d0da56d
Loading
Loading
Loading
Loading
Loading
+0 −88
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\admin_toolbar_search\Functional;

use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;

/**
 * Test the functionality of admin toolbar search.
 *
 * @group admin_toolbar
 * @group admin_toolbar_search
 */
class AdminToolbarSearchSettingTest extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'admin_toolbar_search',
    'node',
    'media',
    'field_ui',
    'menu_ui',
    'block',
  ];

  /**
   * A user with the 'Use Admin Toolbar search' permission.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $userWithAccess;

  /**
   * A test user without the 'Use Admin Toolbar search' permission..
   *
   * @var \Drupal\user\UserInterface
   */
  protected $noAccessUser;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    $permissions = [
      'access toolbar',
      'administer menu',
      'access administration pages',
      'administer site configuration',
      'administer content types',
    ];
    $this->noAccessUser = $this->drupalCreateUser($permissions);
    $permissions[] = 'use admin toolbar search';
    $this->userWithAccess = $this->drupalCreateUser($permissions);
  }

  /**
   * Tests search functionality without admin_toolbar_tools enabled.
   *
   * @return void
   *   Nothing to return.
   */
  public function testToolbarSearch() {
    $this->drupalLogin($this->userWithAccess);

    $this->drupalGet(Url::fromRoute('system.admin'));
    $this->assertSession()->responseNotContains('id="toolbar-item-administration-search');

    $this->config('admin_toolbar_search.settings')->set('display_menu_item', 1);
    $this->config('admin_toolbar_search.settings')->save();
    $this->drupalGet(Url::fromRoute('system.admin'));
    $this->assertSession()->responseContains('id="toolbar-item-administration-search');

    $this->config('admin_toolbar_search.settings')->set('display_menu_item', 0);
    $this->config('admin_toolbar_search.settings')->save();
    $this->drupalGet(Url::fromRoute('system.admin'));
    $this->assertSession()->responseNotContains('id="toolbar-item-administration-search');
  }

}
+129 −37
Original line number Diff line number Diff line
@@ -2,13 +2,13 @@

namespace Drupal\Tests\admin_toolbar_search\Functional;

use Drupal\block\Entity\Block;
use Drupal\Tests\BrowserTestBase;

/**
 * Test the Admin Toolbar Search settings form.
 * Test the Admin Toolbar Search settings form and module's permission.
 *
 * @group admin_toolbar
 * @group admin_toolbar_search
 */
class AdminToolbarSearchSettingsFormTest extends BrowserTestBase {

@@ -30,38 +30,104 @@ class AdminToolbarSearchSettingsFormTest extends BrowserTestBase {
  ];

  /**
   * A user with access to the Admin Toolbar settings form permission.
   * A user with access to the Admin Toolbar settings form and search.
   *
   * Permissions to access the site's admin backend form pages and the 'use
   * admin toolbar search' permission defined by the module.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $adminUser;

  /**
   * A test user without the 'use admin toolbar search' permission.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $noAccessUser;

  /**
   * The HTML IDs used to test the display of the search in the toolbar.
   *
   * @var array<string>
   */
  protected $testHtmlIds = [
    'search_tab' => 'admin-toolbar-search-tab',
    'search_toolbar_item' => 'toolbar-item-administration-search',
    'search_tray' => 'toolbar-item-administration-search-tray',
    // HTML ID of the mobile search tab.
    'search_tab_mobile' => 'admin-toolbar-mobile-search-tab',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    /* Create custom objects. */

    // Create a block for testing module's primary local tasks.
    $this->createPrimaryLocalTasksBlock();

    /* Setup users for testing access and permissions. */

    // Access to the toolbar and site's configuration admin pages, so the 'Admin
    // Toolbar Search settings' form page could be tested.
    $permissions = [
      'access toolbar',
      'access administration pages',
      'administer site configuration',
      // This permission is needed to test the inclusion of the JS libraries.
      'use admin toolbar search',
    ];
    // Create a user with limited permissions.
    $this->noAccessUser = $this->drupalCreateUser($permissions);
    // This permission is needed to test the inclusion of the JS libraries.
    $permissions[] = 'use admin toolbar search';
    // Create a user with access to the admin toolbar search.
    $this->adminUser = $this->drupalCreateUser($permissions);
    // Create a block for testing module's primary local tasks.
    $this->createPrimaryLocalTasksBlock();
  }

  /**
   * Test backend Admin Toolbar Tools settings form fields and submission.
   * Helper function to create a block to test module's primary local tasks.
   */
  private function createPrimaryLocalTasksBlock(): void {
    $values = [
      // A unique ID for the block instance.
      'id' => $this->defaultTheme . '_primary_local_tasks',
      // The plugin block id as defined in the class.
      'plugin' => 'local_tasks_block',
      // The machine name of the theme region.
      'region' => 'highlighted',
      'settings' => [
        'label' => 'Primary tabs',
        'label_display' => '0',
        'primary' => TRUE,
        'secondary' => FALSE,
      ],
      // The machine name of the theme.
      'theme' => $this->defaultTheme,
      'visibility' => [],
      'weight' => 100,
    ];

    \Drupal::entityTypeManager()->getStorage('block')
      ->create($values)
      ->save();
  }

  /**
   * Test backend Admin Toolbar Search settings form fields and submission.
   *
   * Login as an admin user and browse to module's settings form page.
   * Change the configuration values, save and check the results several times.
   * Logout and login with a user without access to the search to test the
   * permission 'use admin toolbar search'.
   */
  public function testAdminToolbarToolsSettingsForm(): void {
  public function testAdminToolbarSearchSettingsForm(): void {
    /** @var \Drupal\Tests\WebAssert $assert */
    $assert = $this->assertSession();

    // Log in as an admin user to test admin pages.
    // Log in as an admin user to test module's settings form.
    $this->drupalLogin($this->adminUser);

    // Test the 'Admin Toolbar Search settings' page form submission and fields.
@@ -75,6 +141,14 @@ class AdminToolbarSearchSettingsFormTest extends BrowserTestBase {
    $assert->responseContains($keyboard_shortcut_js);
    // Check the display menu item is disabled by default.
    $assert->responseContains('"displayMenuItem":false');
    // Check the HTML IDs of the search toolbar item are *not* displayed by
    // default, so the search field is directly in the toolbar
    // (display_menu_item: false).
    $assert->responseContains('id="' . $this->testHtmlIds['search_tab'] . '"');
    $assert->responseNotContains($this->testHtmlIds['search_toolbar_item']);
    $assert->responseNotContains($this->testHtmlIds['search_tray']);
    // Check the HTML ID of the mobile search tab is displayed by default.
    $assert->responseContains('id="' . $this->testHtmlIds['search_tab_mobile'] . '"');

    // Change the value of 'display_menu_item' and submit the form.
    $this->submitForm([
@@ -87,8 +161,15 @@ class AdminToolbarSearchSettingsFormTest extends BrowserTestBase {

    // Check the keyboard shortcut library is loaded by default.
    $assert->responseContains($keyboard_shortcut_js);
    // Check the display menu item is disabled by default.
    // Check the display menu item is enabled with the expected JS variable.
    $assert->responseContains('"displayMenuItem":true');
    // Check the HTML IDs are found on the page (display_menu_item: true), since
    // the search field is displayed in a toolbar tray.
    $assert->responseContains('id="' . $this->testHtmlIds['search_tab'] . '"');
    $assert->responseContains('id="' . $this->testHtmlIds['search_toolbar_item'] . '"');
    $assert->responseContains('id="' . $this->testHtmlIds['search_tray'] . '"');
    // Check the HTML ID of the mobile search tab is *not* displayed.
    $assert->responseNotContains($this->testHtmlIds['search_tab_mobile']);

    // Change the value of 'enable_keyboard_shortcut' and submit the form.
    $this->submitForm([
@@ -101,35 +182,46 @@ class AdminToolbarSearchSettingsFormTest extends BrowserTestBase {
    $assert->responseNotContains('admin_toolbar_search.keyboard_shortcut.js');
    // Check the display menu item JS is not found.
    $assert->responseNotContains('displayMenuItem');
    // Check the module's local tasks are displayed as expected.
    // Check the three modules local tasks (tabs) are displayed as expected.
    $local_tasks_regex = '/<div.*-primary-local-tasks.*>([\r\n].*)+<a.*>Toolbar settings<\/a>.*[\r\n].*<a.*>Search settings<\/a>.*[\r\n].*<a.*>Tools settings<\/a>.*[\r\n].*([\r\n].*)+<\/div>/';
    $assert->responseMatches($local_tasks_regex);
  }

  /**
   * Helper function to create a block to test module's primary local tasks.
   */
  private function createPrimaryLocalTasksBlock(): void {
    $values = [
      // A unique ID for the block instance.
      'id' => 'stark_primary_local_tasks',
      // The plugin block id as defined in the class.
      'plugin' => 'local_tasks_block',
      // The machine name of the theme region.
      'region' => 'highlighted',
      'settings' => [
        'label' => 'Primary tabs',
        'label_display' => '0',
        'primary' => TRUE,
        'secondary' => FALSE,
      ],
      // The machine name of the theme.
      'theme' => $this->defaultTheme,
      'visibility' => [],
      'weight' => 100,
    ];
    $block = Block::create($values);
    $block->save();
    /* Test the permission: 'use admin toolbar search'. */

    $admin_toolbar_search_css = 'admin_toolbar_search/css/admin.toolbar_search.css';
    $admin_toolbar_search_js = 'admin_toolbar_search/js/admin_toolbar_search.js';
    // Check the CSS and JS files of the module are loaded correctly.
    $assert->responseContains($admin_toolbar_search_css);
    $assert->responseContains($admin_toolbar_search_js);

    // Test the extra links search route does not return an access denied error,
    // but an empty array, even with the 'admin_toolbar_tools' module enabled,
    // since the modules adding the extra links are all disabled in this test
    // ('field_ui', 'node', 'media', 'menu_ui', etc...).
    $this->drupalGet('/admin/admin-toolbar-search');
    $assert->responseContains('[]');

    // Logout the current session and login with a user *without* search access.
    $this->drupalLogout();
    $this->drupalLogin($this->noAccessUser);

    // Check the 'Search' tab is *not* displayed, since the user does not have
    // the required permission.
    // Check the CSS and JS files of the module are *not* loaded.
    $assert->responseNotContains($admin_toolbar_search_css);
    $assert->responseNotContains($admin_toolbar_search_js);

    // Check none of the HTML IDs selectors are found on the page, which means
    // the admin toolbar search is not loaded.
    $assert->responseNotContains($this->testHtmlIds['search_tab']);
    $assert->responseNotContains($this->testHtmlIds['search_toolbar_item']);
    $assert->responseNotContains($this->testHtmlIds['search_tray']);
    $assert->responseNotContains($this->testHtmlIds['search_tab_mobile']);

    // Ensure the extra links search route is not accessible without the
    // required permission.
    $this->drupalGet('/admin/admin-toolbar-search');
    $assert->statusCodeEquals(403);
  }

}
+0 −60
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\admin_toolbar_tools\Functional;

use Drupal\Tests\BrowserTestBase;

/**
 * Tests for the existence of Admin Toolbar tools new links.
 *
 * @group admin_toolbar
 */
class AdminToolbarToolsAlterTest extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'toolbar',
    'admin_toolbar',
    'admin_toolbar_tools',
  ];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * A test user with permission to access the administrative toolbar.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $adminUser;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    // Create and log in an administrative user.
    $this->adminUser = $this->drupalCreateUser([
      'access toolbar',
      'access administration pages',
      'administer site configuration',
    ]);
    $this->drupalLogin($this->adminUser);
  }

  /**
   * Tests for the hover of sub menus.
   *
   * @return void
   *   Nothing to return.
   */
  public function testAdminToolbarTools() {
    // Assert that special menu items are present in the HTML.
    $this->assertSession()->responseContains('class="toolbar-icon toolbar-icon-admin-toolbar-tools-flush"');
  }

}
+3 −0
Original line number Diff line number Diff line
@@ -54,6 +54,9 @@ class AdminToolbarToolsSettingsFormTest extends BrowserTestBase {
    // Log in as an admin user to test admin pages.
    $this->drupalLogin($this->adminUser);

    // Assert that special menu items are present in the HTML.
    $assert->responseContains('class="toolbar-icon toolbar-icon-admin-toolbar-tools-flush"');

    // Test the 'Admin Toolbar Tools settings' page form submission and fields.
    $this->drupalGet('admin/config/user-interface/admin-toolbar-tools');
    // Submit the form with default values.

css/admin.toolbar_search.css

deleted100644 → 0
+0 −33
Original line number Diff line number Diff line
#toolbar-item-administration-search-tray {
  padding-left: 1em;
}

#admin-toolbar-search-tab .toolbar-item::before {
  background-image: url(../misc/icons/bebebe/loupe.svg);
}

#admin-toolbar-search-tab .toolbar-item:active::before,
#admin-toolbar-search-tab .toolbar-item.is-active::before {
  background-image: url(../misc/icons/ffffff/loupe.svg);
}

#toolbar-item-administration-search-tray label {
  display: inline-block;
  margin-right: 0.5em;
  color: #000;
  font-weight: bold;
}

#toolbar-item-administration-search-tray div.form-item {
  margin: 0.75em 0;
}

#toolbar-item-administration-search-tray input {
  display: inline-block;
  padding: 0.3em 0.4em 0.3em 0.5em;
  font-size: 1em;
}

.ui-autocomplete .ui-menu-item span.admin-toolbar-search-url {
  display: none;
}
Loading