Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views / tests / src / Unit / Plugin / area / ResultTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Unit\Plugin\area;
4
5 use Drupal\Core\Routing\RouteProviderInterface;
6 use Drupal\Core\Session\AccountInterface;
7 use Drupal\Tests\UnitTestCase;
8 use Drupal\views\Entity\View;
9 use Drupal\views\Plugin\views\pager\PagerPluginBase;
10 use Drupal\views\ViewExecutable;
11 use Drupal\views\Plugin\views\area\Result;
12 use Drupal\views\ViewsData;
13 use Prophecy\Argument;
14
15 /**
16  * @coversDefaultClass \Drupal\views\Plugin\views\area\Result
17  * @group views
18  */
19 class ResultTest extends UnitTestCase {
20
21   /**
22    * The view executable object.
23    *
24    * @var \Drupal\views\ViewExecutable
25    */
26   protected $view;
27
28   /**
29    * The Result handler.
30    *
31    * @var \Drupal\views\Plugin\views\area\Result
32    */
33   protected $resultHandler;
34
35   protected function setUp() {
36     parent::setUp();
37
38     $storage = $this->prophesize(View::class);
39     $storage->label()->willReturn('ResultTest');
40     $storage->set(Argument::cetera())->willReturn(NULL);
41
42     $user = $this->prophesize(AccountInterface::class)->reveal();
43     $views_data = $this->prophesize(ViewsData::class)->reveal();
44     $route_provider = $this->prophesize(RouteProviderInterface::class)->reveal();
45     $this->view = new ViewExecutable($storage->reveal(), $user, $views_data, $route_provider);
46
47     $this->resultHandler = new Result([], 'result', []);
48     $this->resultHandler->view = $this->view;
49   }
50
51   /**
52    * Tests the query method.
53    */
54   public function testQuery() {
55     $this->assertNull($this->view->get_total_rows);
56     // @total should set get_total_rows.
57     $this->resultHandler->options['content'] = '@total';
58     $this->resultHandler->query();
59     $this->assertTrue($this->view->get_total_rows);
60     // A different token should not.
61     $this->view->get_total_rows = NULL;
62     $this->resultHandler->options['content'] = '@current_page';
63     $this->resultHandler->query();
64     $this->assertNull($this->view->get_total_rows);
65   }
66
67   /**
68    * Tests the rendered output of the Result area handler.
69    *
70    * @param string $content
71    *   The content to use when rendering the handler.
72    * @param string $expected
73    *   The expected content string.
74    * @param int $items_per_page
75    *   The items per page of the configuration.
76    *
77    * @dataProvider providerTestResultArea
78    */
79   public function testResultArea($content, $expected, $items_per_page = 0) {
80     $this->setupViewPager($items_per_page);
81     $this->resultHandler->options['content'] = $content;
82     $this->assertEquals(['#markup' => $expected], $this->resultHandler->render());
83   }
84
85   /**
86    * Data provider for testResultArea.
87    *
88    * @return array
89    */
90   public function providerTestResultArea() {
91     return [
92       ['@label', 'ResultTest'],
93       ['@start', '1'],
94       ['@start', '1', 1],
95       ['@end', '100'],
96       ['@end', '1', 1],
97       ['@total', '100'],
98       ['@total', '100', 1],
99       ['@per_page', '0'],
100       ['@per_page', '1', 1],
101       ['@current_page', '1'],
102       ['@current_page', '1', 1],
103       ['@current_record_count', '100'],
104       ['@current_record_count', '1', 1],
105       ['@page_count', '1'],
106       ['@page_count', '100', 1],
107       ['@start | @end | @total', '1 | 100 | 100'],
108       ['@start | @end | @total', '1 | 1 | 100', 1],
109     ];
110   }
111
112   /**
113    * Sets up a mock pager on the view executable object.
114    *
115    * @param int $items_per_page
116    *   The value to return from getItemsPerPage().
117    */
118   protected function setupViewPager($items_per_page = 0) {
119     $pager = $this->prophesize(PagerPluginBase::class);
120     $pager->getItemsPerPage()
121       ->willReturn($items_per_page)
122       ->shouldBeCalledTimes(1);
123     $pager->getCurrentPage()
124       ->willReturn(0)
125       ->shouldBeCalledTimes(1);
126
127     $this->view->pager = $pager->reveal();
128     $this->view->style_plugin = new \stdClass();
129     $this->view->total_rows = 100;
130     $this->view->result = [1, 2, 3, 4, 5];
131   }
132
133 }