function DevelEventInfoTest::testEventList

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

Tests event info page.

File

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

Class

DevelEventInfoTest
Tests event info pages and links.

Namespace

Drupal\Tests\devel\Functional

Code

public function testEventList() : void {
    
    /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher */
    $event_dispatcher = $this->container
        ->get('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.
    $headers = $table->findAll('css', 'thead th');
    $this->assertEquals(3, count($headers));
    $expected_headers = [
        'Event Name',
        'Callable',
        'Priority',
    ];
    $actual_headers = array_map(static fn($element) => $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) {
        // 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->assertTrue(count($event_header_row) >= 1);
        // Ensures that all the event listener are listed in the table.
        $event_rows = $table->findAll('css', sprintf('tbody tr:contains("%s")', $event_name));
        // Remove the header row.
        array_shift($event_rows);
        $listeners = $event_dispatcher->getListeners($event_name);
        foreach ($listeners as $index => $listener) {
            $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];
            $priority = $event_dispatcher->getListenerPriority($event_name, $listener);
            $this->assertEquals($priority, $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);
}