Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views / tests / src / Unit / Plugin / field / CounterTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Unit\Plugin\field;
4
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;
10
11 /**
12  * @coversDefaultClass \Drupal\views\Plugin\views\field\Counter
13  * @group views
14  */
15 class CounterTest extends UnitTestCase {
16
17   /**
18    * The pager plugin instance.
19    *
20    * @var \Drupal\views\Plugin\views\pager\PagerPluginBase
21    */
22   protected $pager;
23
24   /**
25    * The view executable.
26    *
27    * @var \Drupal\views\ViewExecutable
28    */
29   protected  $view;
30
31   /**
32    * The display plugin instance.
33    *
34    * @var \Drupal\views\Plugin\views\display\DisplayPluginBase
35    */
36   protected $display;
37
38
39   /**
40    * Stores the test data.
41    *
42    * @var array
43    */
44   protected $testData = [];
45
46   /**
47    * The handler definition of the counter field.
48    *
49    * @return array
50    */
51   protected $definition;
52
53   /**
54    * {@inheritdoc}
55    */
56   protected function setUp() {
57     parent::setUp();
58
59     // Setup basic stuff like the view and the display.
60     $config = [];
61     $config['display']['default'] = [
62       'id' => 'default',
63       'display_plugin' => 'default',
64       'display_title' => 'Default',
65     ];
66
67     $storage = new View($config, 'view');
68     $user = $this->getMock('Drupal\Core\Session\AccountInterface');
69     $views_data = $this->getMockBuilder('Drupal\views\ViewsData')
70       ->disableOriginalConstructor()
71       ->getMock();
72     $route_provider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');
73     $this->view = $this->getMock('Drupal\views\ViewExecutable', NULL, [$storage, $user, $views_data, $route_provider]);
74
75     $this->display = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase')
76       ->disableOriginalConstructor()
77       ->getMock();
78
79     $this->pager = $this->getMockBuilder('Drupal\views\Plugin\views\pager\Full')
80       ->disableOriginalConstructor()
81       ->setMethods(NULL)
82       ->getMock();
83
84     $this->view->display_handler = $this->display;
85     $this->view->pager = $this->pager;
86
87     foreach (ViewTestData::dataSet() as $index => $set) {
88       $this->testData[] = new ResultRow($set + ['index' => $index]);
89     }
90
91     $this->definition = ['title' => 'counter field', 'plugin_type' => 'field'];
92   }
93
94   /**
95    * Provides some row index to test.
96    *
97    * @return array
98    *   Returns an array of row index to test.
99    */
100   public function providerRowIndexes() {
101     return [
102       [0],
103       [1],
104       [2],
105     ];
106   }
107
108   /**
109    * Tests a simple counter field.
110    *
111    * @dataProvider providerRowIndexes
112    */
113   public function testSimpleCounter($i) {
114     $counter_handler = new Counter([], 'counter', $this->definition);
115     $options = [];
116     $counter_handler->init($this->view, $this->display, $options);
117
118     $this->view->row_index = $i;
119     $expected = $i + 1;
120
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');
125   }
126
127   /**
128    * Tests a counter with a random start.
129    *
130    * @param int $i
131    *   The row index to test.
132    *
133    * @dataProvider providerRowIndexes
134    */
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);
139     $options = [
140       'counter_start' => $rand_start,
141     ];
142     $counter_handler->init($this->view, $this->display, $options);
143
144     $this->view->row_index = $i;
145     $expected = $rand_start + $i;
146
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');
151   }
152
153   /**
154    * Tests a counter field with a random pager offset.
155    *
156    * @param int $i
157    *   The row index to test.
158    *
159    * @dataProvider providerRowIndexes
160    */
161   public function testCounterRandomPagerOffset($i) {
162     // Setup a counter field with a pager with a random offset.
163     $offset = 3;
164     $this->pager->setOffset($offset);
165
166     $rand_start = rand(5, 10);
167     $counter_handler = new Counter([], 'counter', $this->definition);
168     $options = [
169       'counter_start' => $rand_start,
170     ];
171     $counter_handler->init($this->view, $this->display, $options);
172
173     $this->view->row_index = $i;
174     $expected = $offset + $rand_start + $i;
175
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');
180   }
181
182   /**
183    * Tests a counter field on the second page.
184    *
185    * @param int $i
186    *   The row index to test.
187    *
188    * @dataProvider providerRowIndexes
189    */
190   public function testCounterSecondPage($i) {
191     $offset = 3;
192     // Setup a pager on the second page.
193     $this->pager->setOffset($offset);
194     $items_per_page = 5;
195     $this->pager->setItemsPerPage($items_per_page);
196     $current_page = 1;
197     $this->pager->setCurrentPage($current_page);
198
199     $rand_start = rand(5, 10);
200     $counter_handler = new Counter([], 'counter', $this->definition);
201     $options = [
202       'counter_start' => $rand_start,
203     ];
204     $counter_handler->init($this->view, $this->display, $options);
205
206     $this->view->row_index = $i;
207     $expected = $items_per_page + $offset + $rand_start + $i;
208
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');
213   }
214
215   /**
216    * Renders the counter field handler.
217    *
218    * @param \Drupal\views\Plugin\views\field\Counter $handler
219    *   The counter handler.
220    * @param \Drupal\views\ResultRow $row
221    *   A result row.
222    *
223    * @return string
224    *   The counter rendered markup.
225    */
226   protected function renderCounter(Counter $handler, ResultRow $row) {
227     $markup = $handler->render($row);
228     $handler->postRender($row, $markup);
229     return $handler->last_render;
230   }
231
232 }