function DevelRouteInfoTest::testRouteList

Same name in other branches
  1. 5.x tests/src/Functional/DevelRouteInfoTest.php \Drupal\Tests\devel\Functional\DevelRouteInfoTest::testRouteList()

Tests routes info.

File

tests/src/Functional/DevelRouteInfoTest.php, line 27

Class

DevelRouteInfoTest
Tests routes info pages and links.

Namespace

Drupal\Tests\devel\Functional

Code

public function testRouteList() {
    // Ensures that the routes info link is present on the devel menu and that
    // it points to the correct page.
    $this->drupalGet('');
    $this->clickLink('Routes Info');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->addressEquals('/devel/routes');
    $this->assertSession()
        ->pageTextContains('Routes');
    $page = $this->getSession()
        ->getPage();
    // Ensures that the expected table headers are found.
    
    /** @var \Behat\Mink\Element\NodeElement[] $headers */
    $headers = $page->findAll('css', 'table.devel-route-list thead th');
    $this->assertEquals(4, count($headers));
    $expected_items = [
        'Route Name',
        'Path',
        'Allowed Methods',
        'Operations',
    ];
    foreach ($headers as $key => $element) {
        $this->assertSame($element->getText(), $expected_items[$key]);
    }
    // Ensures that all the routes are listed in the table.
    $routes = \Drupal::service('router.route_provider')->getAllRoutes();
    $rows = $page->findAll('css', 'table.devel-route-list tbody tr');
    $this->assertEquals(count($routes), count($rows));
    // Tests the presence of some (arbitrarily chosen) routes in the table.
    $expected_routes = [
        '<current>' => [
            'path' => '/<current>',
            'methods' => [
                'GET',
                'POST',
            ],
            'dynamic' => FALSE,
        ],
        'user.login' => [
            'path' => '/user/login',
            'methods' => [
                'GET',
                'POST',
            ],
            'dynamic' => FALSE,
        ],
        'entity.user.canonical' => [
            'path' => '/user/{user}',
            'methods' => [
                'GET',
                'POST',
            ],
            'dynamic' => TRUE,
        ],
        'entity.user.devel_load' => [
            'path' => '/devel/user/{user}',
            'methods' => [
                'ANY',
            ],
            'dynamic' => TRUE,
        ],
    ];
    foreach ($expected_routes as $route_name => $expected) {
        $row = $page->find('css', sprintf('table.devel-route-list tbody tr:contains("%s")', $route_name));
        $this->assertNotNull($row);
        
        /** @var \Behat\Mink\Element\NodeElement[] $cells */
        $cells = $row->findAll('css', 'td');
        $this->assertEquals(4, count($cells));
        $cell_route_name = $cells[0];
        $this->assertEquals($route_name, $cell_route_name->getText());
        $this->assertTrue($cell_route_name->hasClass('table-filter-text-source'));
        $cell_path = $cells[1];
        $this->assertEquals($expected['path'], $cell_path->getText());
        $this->assertTrue($cell_path->hasClass('table-filter-text-source'));
        $cell_methods = $cells[2];
        $this->assertEquals(implode('', $expected['methods']), $cell_methods->getText());
        $cell_operations = $cells[3];
        $actual_href = $cell_operations->findLink('Devel')
            ->getAttribute('href');
        if ($expected['dynamic']) {
            $parameters = [
                'query' => [
                    'route_name' => $route_name,
                ],
            ];
        }
        else {
            $parameters = [
                'query' => [
                    'path' => $expected['path'],
                ],
            ];
        }
        $expected_href = Url::fromRoute('devel.route_info.item', [], $parameters)->toString();
        $this->assertEquals($expected_href, $actual_href);
    }
    // Ensures that the page is accessible only to the users with the adequate
    // permissions.
    $this->drupalLogout();
    $this->drupalGet('devel/routes');
    $this->assertSession()
        ->statusCodeEquals(403);
}