function TaxonomyGlossaryTest::testTaxonomyGlossaryView

Same name in other branches
  1. 8.9.x core/modules/views/tests/src/Functional/TaxonomyGlossaryTest.php \Drupal\Tests\views\Functional\TaxonomyGlossaryTest::testTaxonomyGlossaryView()
  2. 10 core/modules/views/tests/src/Functional/TaxonomyGlossaryTest.php \Drupal\Tests\views\Functional\TaxonomyGlossaryTest::testTaxonomyGlossaryView()
  3. 11.x core/modules/views/tests/src/Functional/TaxonomyGlossaryTest.php \Drupal\Tests\views\Functional\TaxonomyGlossaryTest::testTaxonomyGlossaryView()

Tests a taxonomy glossary view.

File

core/modules/views/tests/src/Functional/TaxonomyGlossaryTest.php, line 62

Class

TaxonomyGlossaryTest
Tests glossary functionality of taxonomy views.

Namespace

Drupal\Tests\views\Functional

Code

public function testTaxonomyGlossaryView() {
    $initials = [];
    foreach ($this->taxonomyTerms as $term) {
        $char = mb_strtolower(substr($term->label(), 0, 1));
        $initials += [
            $char => 0,
        ];
        $initials[$char]++;
    }
    $this->drupalGet('test_taxonomy_glossary');
    $assert_session = $this->assertSession();
    foreach ($initials as $char => $count) {
        $href = Url::fromUserInput('/test_taxonomy_glossary/' . $char)->toString();
        $xpath = $assert_session->buildXPathQuery('//a[@href=:href and normalize-space(text())=:label]', [
            ':href' => $href,
            ':label' => $char,
        ]);
        $link = $assert_session->elementExists('xpath', $xpath);
        // Assert that the expected number of results is indicated in the link.
        preg_match("/{$char} \\(([0-9]+)\\)/", $link->getParent()
            ->getText(), $matches);
        $this->assertEquals($count, $matches[1]);
    }
    // Check that no other glossary links but the expected ones have been
    // rendered.
    $assert_session->elementsCount('xpath', '/ancestor::ul//a', count($initials), $link);
    // Go the taxonomy glossary page for the first term.
    $this->drupalGet('test_taxonomy_glossary/' . substr($this->taxonomyTerms[0]
        ->getName(), 0, 1));
    $assert_session->pageTextContains($this->taxonomyTerms[0]
        ->getName());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.