function FieldCounterTest::testSimple

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

File

core/modules/views/tests/src/Kernel/Handler/FieldCounterTest.php, line 29

Class

FieldCounterTest
Tests the Drupal\views\Plugin\views\field\Counter handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testSimple() : void {
    $view = Views::getView('test_view');
    $view->setDisplay();
    $view->displayHandlers
        ->get('default')
        ->overrideOption('fields', [
        'counter' => [
            'id' => 'counter',
            'table' => 'views',
            'field' => 'counter',
            'relationship' => 'none',
        ],
        'name' => [
            'id' => 'name',
            'table' => 'views_test_data',
            'field' => 'name',
            'relationship' => 'none',
        ],
    ]);
    $view->preview();
    $counter = $view->style_plugin
        ->getField(0, 'counter');
    $this->assertEquals('1', $counter, "Make sure the expected number (1) patches with the rendered number ({$counter})");
    $counter = $view->style_plugin
        ->getField(1, 'counter');
    $this->assertEquals('2', $counter, "Make sure the expected number (2) patches with the rendered number ({$counter})");
    $counter = $view->style_plugin
        ->getField(2, 'counter');
    $this->assertEquals('3', $counter, "Make sure the expected number (3) patches with the rendered number ({$counter})");
    $view->destroy();
    $view->storage
        ->invalidateCaches();
    $view->setDisplay();
    $rand_start = rand(5, 10);
    $view->displayHandlers
        ->get('default')
        ->overrideOption('fields', [
        'counter' => [
            'id' => 'counter',
            'table' => 'views',
            'field' => 'counter',
            'relationship' => 'none',
            'counter_start' => $rand_start,
        ],
        'name' => [
            'id' => 'name',
            'table' => 'views_test_data',
            'field' => 'name',
            'relationship' => 'none',
        ],
    ]);
    $view->preview();
    $counter = $view->style_plugin
        ->getField(0, 'counter');
    $expected_number = 0 + $rand_start;
    $this->assertEquals((string) $expected_number, $counter, "Make sure the expected number ({$expected_number}) patches with the rendered number ({$counter})");
    $counter = $view->style_plugin
        ->getField(1, 'counter');
    $expected_number = 1 + $rand_start;
    $this->assertEquals((string) $expected_number, $counter, "Make sure the expected number ({$expected_number}) patches with the rendered number ({$counter})");
    $counter = $view->style_plugin
        ->getField(2, 'counter');
    $expected_number = 2 + $rand_start;
    $this->assertEquals((string) $expected_number, $counter, "Make sure the expected number ({$expected_number}) patches with the rendered number ({$counter})");
}

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