You are here

public function DevelEventInfoTest::testEventList in Devel 8.2

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/DevelEventInfoTest.php \Drupal\Tests\devel\Functional\DevelEventInfoTest::testEventList()
  2. 8 tests/src/Functional/DevelEventInfoTest.php \Drupal\Tests\devel\Functional\DevelEventInfoTest::testEventList()
  3. 4.x tests/src/Functional/DevelEventInfoTest.php \Drupal\Tests\devel\Functional\DevelEventInfoTest::testEventList()

Tests event info page.

File

tests/src/Functional/DevelEventInfoTest.php, line 55

Class

DevelEventInfoTest
Tests event info pages and links.

Namespace

Drupal\Tests\devel\Functional

Code

public function testEventList() {
  $event_dispatcher = \Drupal::service('event_dispatcher');
  $this
    ->drupalGet('/devel/events');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextContains('Events');
  $page = $this
    ->getSession()
    ->getPage();

  // Ensures that the event table is found.
  $table = $page
    ->find('css', 'table.devel-event-list');
  $this
    ->assertNotNull($table);

  // Ensures that the expected table headers are found.

  /* @var $headers \Behat\Mink\Element\NodeElement[] */
  $headers = $table
    ->findAll('css', 'thead th');
  $this
    ->assertEquals(3, count($headers));
  $expected_headers = [
    'Event Name',
    'Callable',
    'Priority',
  ];
  $actual_headers = array_map(function ($element) {
    return $element
      ->getText();
  }, $headers);
  $this
    ->assertSame($expected_headers, $actual_headers);

  // Ensures that all the events are listed in the table.
  $events = $event_dispatcher
    ->getListeners();
  $event_header_row = $table
    ->findAll('css', 'tbody tr th.devel-event-name-header');
  $this
    ->assertEquals(count($events), count($event_header_row));

  // Tests the presence of some (arbitrarily chosen) events and related
  // listeners in the table. The event items are tested dynamically so no
  // test failures are expected if listeners change.
  $expected_events = [
    'config.delete',
    'kernel.request',
    'routing.route_alter',
  ];
  foreach ($expected_events as $event_name) {
    $listeners = $event_dispatcher
      ->getListeners($event_name);

    // Ensures that the event header is present in the table.
    $event_header_row = $table
      ->findAll('css', sprintf('tbody tr th:contains("%s")', $event_name));
    $this
      ->assertNotNull($event_header_row);
    $this
      ->assertEquals(1, count($event_header_row));

    // Ensures that all the event listener are listed in the table.

    /* @var $event_rows \Behat\Mink\Element\NodeElement[] */
    $event_rows = $table
      ->findAll('css', sprintf('tbody tr:contains("%s")', $event_name));

    // Remove the header row.
    array_shift($event_rows);
    $this
      ->assertEquals(count($listeners), count($event_rows));
    foreach ($listeners as $index => $listener) {

      /* @var $cells \Behat\Mink\Element\NodeElement[] */
      $cells = $event_rows[$index]
        ->findAll('css', 'td');
      $this
        ->assertEquals(3, count($cells));
      $cell_event_name = $cells[0];
      $this
        ->assertEquals($event_name, $cell_event_name
        ->getText());
      $this
        ->assertTrue($cell_event_name
        ->hasClass('table-filter-text-source'));
      $this
        ->assertTrue($cell_event_name
        ->hasClass('visually-hidden'));
      $cell_callable = $cells[1];
      is_callable($listener, TRUE, $callable_name);
      $this
        ->assertEquals($callable_name, $cell_callable
        ->getText());
      $cell_methods = $cells[2];
      $this
        ->assertEquals($index, $cell_methods
        ->getText());
    }
  }

  // Ensures that the page is accessible only to the users with the adequate
  // permissions.
  $this
    ->drupalLogout();
  $this
    ->drupalGet('devel/events');
  $this
    ->assertSession()
    ->statusCodeEquals(403);
}