function ArgumentStringTest::testGlossary

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Kernel/Handler/ArgumentStringTest.php \Drupal\Tests\views\Kernel\Handler\ArgumentStringTest::testGlossary()
  2. 8.9.x core/modules/views/tests/src/Kernel/Handler/ArgumentStringTest.php \Drupal\Tests\views\Kernel\Handler\ArgumentStringTest::testGlossary()
  3. 10 core/modules/views/tests/src/Kernel/Handler/ArgumentStringTest.php \Drupal\Tests\views\Kernel\Handler\ArgumentStringTest::testGlossary()

Tests the glossary feature.

File

core/modules/views/tests/src/Kernel/Handler/ArgumentStringTest.php, line 34

Class

ArgumentStringTest
Tests the core <a href="/api/drupal/core%21modules%21views%21src%21Plugin%21views%21argument%21StringArgument.php/class/StringArgument/11.x" title="Argument handler for string." class="local">Drupal\views\Plugin\views\argument\StringArgument</a> handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testGlossary() : void {
    $this->installEntitySchema('user');
    $this->installEntitySchema('node');
    NodeType::create([
        'type' => 'page',
        'name' => 'Page',
    ])->save();
    // Setup some nodes, one with a, two with b and three with c.
    $counter = 1;
    foreach ([
        'a',
        'b',
        'c',
    ] as $char) {
        for ($i = 0; $i < $counter; $i++) {
            Node::create([
                'type' => 'page',
                'title' => $char . $this->randomMachineName(),
            ])
                ->save();
        }
    }
    $view = Views::getView('test_glossary');
    $this->executeView($view);
    $count_field = 'nid';
    foreach ($view->result as &$row) {
        if (str_starts_with($view->field['title']
            ->getValue($row), 'a')) {
            $this->assertEquals(1, $row->{$count_field});
        }
        if (str_starts_with($view->field['title']
            ->getValue($row), 'b')) {
            $this->assertEquals(2, $row->{$count_field});
        }
        if (str_starts_with($view->field['title']
            ->getValue($row), 'c')) {
            $this->assertEquals(3, $row->{$count_field});
        }
    }
}

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