You are here

public function DevelContainerInfoTest::testServiceList in Devel 8

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

Tests service list page.

File

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

Class

DevelContainerInfoTest
Tests container info pages and links.

Namespace

Drupal\Tests\devel\Functional

Code

public function testServiceList() {
  $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.

  /** @var $headers \Behat\Mink\Element\NodeElement[] */
  $headers = $table
    ->findAll('css', 'thead th');
  $this
    ->assertEquals(4, count($headers));
  $expected_headers = [
    'ID',
    'Class',
    'Alias',
    'Operations',
  ];
  $actual_headers = array_map(function ($element) {
    return $element
      ->getText();
  }, $headers);
  $this
    ->assertSame($expected_headers, $actual_headers);

  // Ensures that all the serivices 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 = [
    'config.factory' => [
      'class' => 'Drupal\\Core\\Config\\ConfigFactory',
      'alias' => '',
    ],
    'devel.route_subscriber' => [
      'class' => 'Drupal\\devel\\Routing\\RouteSubscriber',
      'alias' => '',
    ],
    'plugin.manager.element_info' => [
      'class' => 'Drupal\\Core\\Render\\ElementInfoManager',
      'alias' => 'element_info',
    ],
  ];
  foreach ($expected_services as $service_id => $expected) {
    $row = $table
      ->find('css', sprintf('tbody tr:contains("%s")', $service_id));
    $this
      ->assertNotNull($row);

    /** @var $cells \Behat\Mink\Element\NodeElement[] */
    $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);
}