3 namespace Drupal\Tests\views\Unit\Plugin\area;
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;
16 * @coversDefaultClass \Drupal\views\Plugin\views\area\Result
19 class ResultTest extends UnitTestCase {
22 * The view executable object.
24 * @var \Drupal\views\ViewExecutable
31 * @var \Drupal\views\Plugin\views\area\Result
33 protected $resultHandler;
35 protected function setUp() {
38 $storage = $this->prophesize(View::class);
39 $storage->label()->willReturn('ResultTest');
40 $storage->set(Argument::cetera())->willReturn(NULL);
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);
47 $this->resultHandler = new Result([], 'result', []);
48 $this->resultHandler->view = $this->view;
52 * Tests the query method.
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);
68 * Tests the rendered output of the Result area handler.
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.
77 * @dataProvider providerTestResultArea
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());
86 * Data provider for testResultArea.
90 public function providerTestResultArea() {
92 ['@label', 'ResultTest'],
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],
113 * Sets up a mock pager on the view executable object.
115 * @param int $items_per_page
116 * The value to return from getItemsPerPage().
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()
125 ->shouldBeCalledTimes(1);
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];