function FieldCounterTest::testPager

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

Tests the counter field when using a pager.

File

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

Class

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

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testPager() {
    $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->displayHandlers
        ->get('default')
        ->setOption('pager', [
        'type' => 'mini',
        'options' => [
            'items_per_page' => 1,
        ],
    ]);
    $view->preview();
    $counter = $view->style_plugin
        ->getField(0, 'counter');
    $this->assertEquals('1', $counter);
    $view->destroy();
    // Go to the second page.
    $view->setCurrentPage(1);
    $view->preview();
    $counter = $view->style_plugin
        ->getField(0, 'counter');
    $this->assertEquals('2', $counter);
    $view->destroy();
    // Go to the third page.
    $view->setCurrentPage(2);
    $view->preview();
    $counter = $view->style_plugin
        ->getField(0, 'counter');
    $this->assertEquals('3', $counter);
    $view->destroy();
    // Test using the counter start option.
    $counter_start = 1000000;
    $view->setDisplay();
    $view->displayHandlers
        ->get('default')
        ->overrideOption('fields', [
        'counter' => [
            'id' => 'counter',
            'table' => 'views',
            'field' => 'counter',
            'relationship' => 'none',
            'counter_start' => $counter_start,
        ],
        'name' => [
            'id' => 'name',
            'table' => 'views_test_data',
            'field' => 'name',
            'relationship' => 'none',
        ],
    ]);
    $view->preview();
    $counter = $view->style_plugin
        ->getField(0, 'counter');
    $this->assertEquals($counter_start, $counter);
    $view->destroy();
    // Go to the second page.
    $view->setCurrentPage(1);
    $view->preview();
    $counter = $view->style_plugin
        ->getField(0, 'counter');
    $this->assertEquals($counter_start + 1, $counter);
    $view->destroy();
    // Go to the third page.
    $view->setCurrentPage(2);
    $view->preview();
    $counter = $view->style_plugin
        ->getField(0, 'counter');
    $this->assertEquals($counter_start + 2, $counter);
}

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