function DevelContainerInfoTest::testServiceList

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

Tests service list page.

File

tests/src/Functional/DevelContainerInfoTest.php, line 44

Class

DevelContainerInfoTest
Tests container info pages and links.

Namespace

Drupal\Tests\devel\Functional

Code

public function testServiceList() : void {
    $this->drupalGet('/devel/container/service');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains('Container services');
    $this->assertContainerInfoLocalTasks();
    $page = $this->getSession()
        ->getPage();
    // Ensures that the services table is found.
    $table = $page->find('css', 'table.devel-service-list');
    $this->assertNotNull($table);
    // Ensures that the expected table headers are found.
    $headers = $table->findAll('css', 'thead th');
    $this->assertEquals(4, count($headers));
    $expected_headers = [
        'ID',
        'Class',
        'Alias',
        'Operations',
    ];
    $actual_headers = array_map(static fn($element) => $element->getText(), $headers);
    $this->assertSame($expected_headers, $actual_headers);
    // Ensures that all the services are listed in the table.
    $cached_definition = \Drupal::service('kernel')->getCachedContainerDefinition();
    $this->assertNotNull($cached_definition);
    $rows = $table->findAll('css', 'tbody tr');
    $this->assertEquals(count($cached_definition['services']), count($rows));
    // Tests the presence of some (arbitrarily chosen) services in the table.
    $expected_services = [
        // Alias changed in Drupal 10 so commented out the test for now.
        //      'config.factory' => [
        //        'class' => 'Drupal\Core\Config\ConfigFactory',
        //        'alias' => '',
        //      ],
'devel.route_subscriber' => [
            'class' => RouteSubscriber::class,
            'alias' => '',
        ],
    ];
    foreach ($expected_services as $service_id => $expected) {
        $row = $table->find('css', sprintf('tbody tr:contains("%s")', $service_id));
        $this->assertNotNull($row);
        $cells = $row->findAll('css', 'td');
        $this->assertEquals(4, count($cells));
        $cell_service_id = $cells[0];
        $this->assertEquals($service_id, $cell_service_id->getText());
        $this->assertTrue($cell_service_id->hasClass('table-filter-text-source'));
        $cell_class = $cells[1];
        $this->assertEquals($expected['class'], $cell_class->getText());
        $this->assertTrue($cell_class->hasClass('table-filter-text-source'));
        $cell_alias = $cells[2];
        $this->assertEquals($expected['alias'], $cell_alias->getText());
        $this->assertTrue($cell_class->hasClass('table-filter-text-source'));
        $cell_operations = $cells[3];
        $actual_href = $cell_operations->findLink('Devel')
            ->getAttribute('href');
        $expected_href = Url::fromRoute('devel.container_info.service.detail', [
            'service_id' => $service_id,
        ])->toString();
        $this->assertEquals($expected_href, $actual_href);
    }
    // Ensures that the page is accessible ony to users with the adequate
    // permissions.
    $this->drupalLogout();
    $this->drupalGet('devel/container/service');
    $this->assertSession()
        ->statusCodeEquals(403);
}