3 namespace Drupal\Tests\views\Unit\Plugin\field;
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\views\Entity\View;
7 use Drupal\views\Plugin\views\field\Counter;
8 use Drupal\views\ResultRow;
9 use Drupal\views\Tests\ViewTestData;
12 * @coversDefaultClass \Drupal\views\Plugin\views\field\Counter
15 class CounterTest extends UnitTestCase {
18 * The pager plugin instance.
20 * @var \Drupal\views\Plugin\views\pager\PagerPluginBase
25 * The view executable.
27 * @var \Drupal\views\ViewExecutable
32 * The display plugin instance.
34 * @var \Drupal\views\Plugin\views\display\DisplayPluginBase
40 * Stores the test data.
44 protected $testData = [];
47 * The handler definition of the counter field.
51 protected $definition;
56 protected function setUp() {
59 // Setup basic stuff like the view and the display.
61 $config['display']['default'] = [
63 'display_plugin' => 'default',
64 'display_title' => 'Default',
67 $storage = new View($config, 'view');
68 $user = $this->getMock('Drupal\Core\Session\AccountInterface');
69 $views_data = $this->getMockBuilder('Drupal\views\ViewsData')
70 ->disableOriginalConstructor()
72 $route_provider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');
73 $this->view = $this->getMock('Drupal\views\ViewExecutable', NULL, [$storage, $user, $views_data, $route_provider]);
75 $this->display = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase')
76 ->disableOriginalConstructor()
79 $this->pager = $this->getMockBuilder('Drupal\views\Plugin\views\pager\Full')
80 ->disableOriginalConstructor()
84 $this->view->display_handler = $this->display;
85 $this->view->pager = $this->pager;
87 foreach (ViewTestData::dataSet() as $index => $set) {
88 $this->testData[] = new ResultRow($set + ['index' => $index]);
91 $this->definition = ['title' => 'counter field', 'plugin_type' => 'field'];
95 * Provides some row index to test.
98 * Returns an array of row index to test.
100 public function providerRowIndexes() {
109 * Tests a simple counter field.
111 * @dataProvider providerRowIndexes
113 public function testSimpleCounter($i) {
114 $counter_handler = new Counter([], 'counter', $this->definition);
116 $counter_handler->init($this->view, $this->display, $options);
118 $this->view->row_index = $i;
121 $counter = $counter_handler->getValue($this->testData[$i]);
122 $this->assertEquals($expected, $counter, 'The expected number matches with the counter number');
123 $counter = $this->renderCounter($counter_handler, $this->testData[$i]);
124 $this->assertEquals($expected, $counter, 'The expected number matches with the rendered number');
128 * Tests a counter with a random start.
131 * The row index to test.
133 * @dataProvider providerRowIndexes
135 public function testCounterRandomStart($i) {
136 // Setup a counter field with a random start.
137 $rand_start = rand(5, 10);
138 $counter_handler = new Counter([], 'counter', $this->definition);
140 'counter_start' => $rand_start,
142 $counter_handler->init($this->view, $this->display, $options);
144 $this->view->row_index = $i;
145 $expected = $rand_start + $i;
147 $counter = $counter_handler->getValue($this->testData[$i]);
148 $this->assertEquals($expected, $counter, 'The expected number matches with the counter number');
149 $counter = $this->renderCounter($counter_handler, $this->testData[$i]);
150 $this->assertEquals($expected, $counter, 'The expected number matches with the rendered number');
154 * Tests a counter field with a random pager offset.
157 * The row index to test.
159 * @dataProvider providerRowIndexes
161 public function testCounterRandomPagerOffset($i) {
162 // Setup a counter field with a pager with a random offset.
164 $this->pager->setOffset($offset);
166 $rand_start = rand(5, 10);
167 $counter_handler = new Counter([], 'counter', $this->definition);
169 'counter_start' => $rand_start,
171 $counter_handler->init($this->view, $this->display, $options);
173 $this->view->row_index = $i;
174 $expected = $offset + $rand_start + $i;
176 $counter = $counter_handler->getValue($this->testData[$i]);
177 $this->assertEquals($expected, $counter, 'The expected number matches with the counter number');
178 $counter = $this->renderCounter($counter_handler, $this->testData[$i]);
179 $this->assertEquals($expected, $counter, 'The expected number matches with the rendered number');
183 * Tests a counter field on the second page.
186 * The row index to test.
188 * @dataProvider providerRowIndexes
190 public function testCounterSecondPage($i) {
192 // Setup a pager on the second page.
193 $this->pager->setOffset($offset);
195 $this->pager->setItemsPerPage($items_per_page);
197 $this->pager->setCurrentPage($current_page);
199 $rand_start = rand(5, 10);
200 $counter_handler = new Counter([], 'counter', $this->definition);
202 'counter_start' => $rand_start,
204 $counter_handler->init($this->view, $this->display, $options);
206 $this->view->row_index = $i;
207 $expected = $items_per_page + $offset + $rand_start + $i;
209 $counter = $counter_handler->getValue($this->testData[$i]);
210 $this->assertEquals($expected, $counter, 'The expected number matches with the counter number');
211 $counter = $this->renderCounter($counter_handler, $this->testData[$i]);
212 $this->assertEquals($expected, $counter, 'The expected number matches with the rendered number');
216 * Renders the counter field handler.
218 * @param \Drupal\views\Plugin\views\field\Counter $handler
219 * The counter handler.
220 * @param \Drupal\views\ResultRow $row
224 * The counter rendered markup.
226 protected function renderCounter(Counter $handler, ResultRow $row) {
227 $markup = $handler->render($row);
228 $handler->postRender($row, $markup);
229 return $handler->last_render;