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. 8.9.x core/modules/views/tests/src/Unit/Plugin/field/CounterTest.php \Drupal\Tests\views\Unit\Plugin\field\CounterTest
  3. 10 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 18

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() : void {
        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');
        $display_plugin_manager = $this->getMockBuilder('\\Drupal\\views\\Plugin\\ViewsPluginManager')
            ->disableOriginalConstructor()
            ->getMock();
        $this->view = new ViewExecutable($storage, $user, $views_data, $route_provider, $display_plugin_manager);
        $this->display = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\DisplayPluginBase')
            ->disableOriginalConstructor()
            ->getMock();
        $this->pager = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\pager\\Full')
            ->disableOriginalConstructor()
            ->onlyMethods([])
            ->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 static function providerRowIndexes() {
        return [
            [
                0,
            ],
            [
                1,
            ],
            [
                2,
            ],
        ];
    }
    
    /**
     * Tests a simple counter field.
     *
     * @dataProvider providerRowIndexes
     */
    public function testSimpleCounter($i) : void {
        $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) : void {
        // 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) : void {
        // 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) : void {
        $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 Modifiers Object type Summary Overriden Title
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 static 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.
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

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