function DevelElementInfoTest::testElementList

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

Tests element list page.

File

tests/src/Functional/DevelElementInfoTest.php, line 45

Class

DevelElementInfoTest
Tests element info pages and links.

Namespace

Drupal\Tests\devel\Functional

Code

public function testElementList() : void {
    $this->drupalGet('/devel/elements');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains('Element Info');
    $page = $this->getSession()
        ->getPage();
    // Ensures that the element list table is found.
    $table = $page->find('css', 'table.devel-element-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 = [
        'Name',
        'Provider',
        'Class',
        'Operations',
    ];
    $actual_headers = array_map(static fn(NodeElement $element) => $element->getText(), $headers);
    $this->assertSame($expected_headers, $actual_headers);
    // Tests the presence of some (arbitrarily chosen) elements in the table.
    $expected_elements = [
        'button' => [
            'class' => Button::class,
            'provider' => 'core',
        ],
        'form' => [
            'class' => Form::class,
            'provider' => 'core',
        ],
        'html' => [
            'class' => Html::class,
            'provider' => 'core',
        ],
    ];
    foreach ($expected_elements as $element_name => $element) {
        $row = $table->find('css', sprintf('tbody tr:contains("%s")', $element_name));
        $this->assertNotNull($row);
        $cells = $row->findAll('css', 'td');
        $this->assertEquals(4, count($cells));
        $cell = $cells[0];
        $this->assertEquals($element_name, $cell->getText());
        $this->assertTrue($cell->hasClass('table-filter-text-source'));
        $cell = $cells[1];
        $this->assertEquals($element['provider'], $cell->getText());
        $this->assertTrue($cell->hasClass('table-filter-text-source'));
        $cell = $cells[2];
        $this->assertEquals($element['class'], $cell->getText());
        $this->assertTrue($cell->hasClass('table-filter-text-source'));
        $cell = $cells[3];
        $actual_href = $cell->findLink('Devel')
            ->getAttribute('href');
        $expected_href = Url::fromRoute('devel.elements_page.detail', [
            'element_name' => $element_name,
        ])->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/elements');
    $this->assertSession()
        ->statusCodeEquals(403);
}