class CounterTest

Same name in other branches
  1. 9 core/modules/views/tests/src/Unit/Plugin/field/CounterTest.php \Drupal\Tests\views\Unit\Plugin\field\CounterTest
  2. 10 core/modules/views/tests/src/Unit/Plugin/field/CounterTest.php \Drupal\Tests\views\Unit\Plugin\field\CounterTest
  3. 11.x core/modules/views/tests/src/Unit/Plugin/field/CounterTest.php \Drupal\Tests\views\Unit\Plugin\field\CounterTest

@coversDefaultClass \Drupal\views\Plugin\views\field\Counter @group views

Hierarchy

Expanded class hierarchy of CounterTest

File

core/modules/views/tests/src/Unit/Plugin/field/CounterTest.php, line 16

Namespace

Drupal\Tests\views\Unit\Plugin\field
View source
class CounterTest extends UnitTestCase {
    
    /**
     * The pager plugin instance.
     *
     * @var \Drupal\views\Plugin\views\pager\PagerPluginBase
     */
    protected $pager;
    
    /**
     * The view executable.
     *
     * @var \Drupal\views\ViewExecutable
     */
    protected $view;
    
    /**
     * The display plugin instance.
     *
     * @var \Drupal\views\Plugin\views\display\DisplayPluginBase
     */
    protected $display;
    
    /**
     * Stores the test data.
     *
     * @var array
     */
    protected $testData = [];
    
    /**
     * The handler definition of the counter field.
     *
     * @var array
     */
    protected $definition;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() {
        parent::setUp();
        // Setup basic stuff like the view and the display.
        $config = [];
        $config['display']['default'] = [
            'id' => 'default',
            'display_plugin' => 'default',
            'display_title' => 'Default',
        ];
        $storage = new View($config, 'view');
        $user = $this->createMock('Drupal\\Core\\Session\\AccountInterface');
        $views_data = $this->getMockBuilder('Drupal\\views\\ViewsData')
            ->disableOriginalConstructor()
            ->getMock();
        $route_provider = $this->createMock('Drupal\\Core\\Routing\\RouteProviderInterface');
        $this->view = new ViewExecutable($storage, $user, $views_data, $route_provider);
        $this->display = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\DisplayPluginBase')
            ->disableOriginalConstructor()
            ->getMock();
        $this->pager = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\pager\\Full')
            ->disableOriginalConstructor()
            ->setMethods(NULL)
            ->getMock();
        $this->view->display_handler = $this->display;
        $this->view->pager = $this->pager;
        foreach (ViewTestData::dataSet() as $index => $set) {
            $this->testData[] = new ResultRow($set + [
                'index' => $index,
            ]);
        }
        $this->definition = [
            'title' => 'counter field',
            'plugin_type' => 'field',
        ];
    }
    
    /**
     * Provides some row index to test.
     *
     * @return array
     *   Returns an array of row index to test.
     */
    public function providerRowIndexes() {
        return [
            [
                0,
            ],
            [
                1,
            ],
            [
                2,
            ],
        ];
    }
    
    /**
     * Tests a simple counter field.
     *
     * @dataProvider providerRowIndexes
     */
    public function testSimpleCounter($i) {
        $counter_handler = new Counter([], 'counter', $this->definition);
        $options = [];
        $counter_handler->init($this->view, $this->display, $options);
        $this->view->row_index = $i;
        $expected = $i + 1;
        $counter = $counter_handler->getValue($this->testData[$i]);
        $this->assertEquals($expected, $counter, 'The expected number matches with the counter number');
        $counter = $this->renderCounter($counter_handler, $this->testData[$i]);
        $this->assertEquals($expected, $counter, 'The expected number matches with the rendered number');
    }
    
    /**
     * Tests a counter with a random start.
     *
     * @param int $i
     *   The row index to test.
     *
     * @dataProvider providerRowIndexes
     */
    public function testCounterRandomStart($i) {
        // Setup a counter field with a random start.
        $rand_start = rand(5, 10);
        $counter_handler = new Counter([], 'counter', $this->definition);
        $options = [
            'counter_start' => $rand_start,
        ];
        $counter_handler->init($this->view, $this->display, $options);
        $this->view->row_index = $i;
        $expected = $rand_start + $i;
        $counter = $counter_handler->getValue($this->testData[$i]);
        $this->assertEquals($expected, $counter, 'The expected number matches with the counter number');
        $counter = $this->renderCounter($counter_handler, $this->testData[$i]);
        $this->assertEquals($expected, $counter, 'The expected number matches with the rendered number');
    }
    
    /**
     * Tests a counter field with a random pager offset.
     *
     * @param int $i
     *   The row index to test.
     *
     * @dataProvider providerRowIndexes
     */
    public function testCounterRandomPagerOffset($i) {
        // Setup a counter field with a pager with a random offset.
        $offset = 3;
        $this->pager
            ->setOffset($offset);
        $rand_start = rand(5, 10);
        $counter_handler = new Counter([], 'counter', $this->definition);
        $options = [
            'counter_start' => $rand_start,
        ];
        $counter_handler->init($this->view, $this->display, $options);
        $this->view->row_index = $i;
        $expected = $offset + $rand_start + $i;
        $counter = $counter_handler->getValue($this->testData[$i]);
        $this->assertEquals($expected, $counter, 'The expected number matches with the counter number');
        $counter = $this->renderCounter($counter_handler, $this->testData[$i]);
        $this->assertEquals($expected, $counter, 'The expected number matches with the rendered number');
    }
    
    /**
     * Tests a counter field on the second page.
     *
     * @param int $i
     *   The row index to test.
     *
     * @dataProvider providerRowIndexes
     */
    public function testCounterSecondPage($i) {
        $offset = 3;
        // Setup a pager on the second page.
        $this->pager
            ->setOffset($offset);
        $items_per_page = 5;
        $this->pager
            ->setItemsPerPage($items_per_page);
        $current_page = 1;
        $this->pager
            ->setCurrentPage($current_page);
        $rand_start = rand(5, 10);
        $counter_handler = new Counter([], 'counter', $this->definition);
        $options = [
            'counter_start' => $rand_start,
        ];
        $counter_handler->init($this->view, $this->display, $options);
        $this->view->row_index = $i;
        $expected = $items_per_page + $offset + $rand_start + $i;
        $counter = $counter_handler->getValue($this->testData[$i]);
        $this->assertEquals($expected, $counter, 'The expected number matches with the counter number');
        $counter = $this->renderCounter($counter_handler, $this->testData[$i]);
        $this->assertEquals($expected, $counter, 'The expected number matches with the rendered number');
    }
    
    /**
     * Renders the counter field handler.
     *
     * @param \Drupal\views\Plugin\views\field\Counter $handler
     *   The counter handler.
     * @param \Drupal\views\ResultRow $row
     *   A result row.
     *
     * @return string
     *   The counter rendered markup.
     */
    protected function renderCounter(Counter $handler, ResultRow $row) {
        $markup = $handler->render($row);
        $handler->postRender($row, $markup);
        return $handler->last_render;
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
CounterTest::$definition protected property The handler definition of the counter field.
CounterTest::$display protected property The display plugin instance.
CounterTest::$pager protected property The pager plugin instance.
CounterTest::$testData protected property Stores the test data.
CounterTest::$view protected property The view executable.
CounterTest::providerRowIndexes public function Provides some row index to test.
CounterTest::renderCounter protected function Renders the counter field handler.
CounterTest::setUp protected function Overrides UnitTestCase::setUp
CounterTest::testCounterRandomPagerOffset public function Tests a counter field with a random pager offset.
CounterTest::testCounterRandomStart public function Tests a counter with a random start.
CounterTest::testCounterSecondPage public function Tests a counter field on the second page.
CounterTest::testSimpleCounter public function Tests a simple counter field.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.

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