View source
<?php
namespace Drupal\Tests\views\Unit\Plugin\area;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\views\Entity\View;
use Drupal\views\Plugin\views\pager\PagerPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\area\Result;
use Drupal\views\ViewsData;
use Prophecy\Argument;
class ResultTest extends UnitTestCase {
protected $view;
protected $resultHandler;
protected function setUp() {
parent::setUp();
$storage = $this
->prophesize(View::class);
$storage
->label()
->willReturn('ResultTest');
$storage
->set(Argument::cetera())
->willReturn(NULL);
$user = $this
->prophesize(AccountInterface::class)
->reveal();
$views_data = $this
->prophesize(ViewsData::class)
->reveal();
$route_provider = $this
->prophesize(RouteProviderInterface::class)
->reveal();
$this->view = new ViewExecutable($storage
->reveal(), $user, $views_data, $route_provider);
$this->resultHandler = new Result([], 'result', []);
$this->resultHandler->view = $this->view;
}
public function testQuery() {
$this
->assertNull($this->view->get_total_rows);
$this->resultHandler->options['content'] = '@total';
$this->resultHandler
->query();
$this
->assertTrue($this->view->get_total_rows);
$this->view->get_total_rows = NULL;
$this->resultHandler->options['content'] = '@current_page';
$this->resultHandler
->query();
$this
->assertNull($this->view->get_total_rows);
}
public function testResultArea($content, $expected, $items_per_page = 0) {
$this
->setupViewPager($items_per_page);
$this->resultHandler->options['content'] = $content;
$this
->assertEquals([
'#markup' => $expected,
], $this->resultHandler
->render());
}
public function providerTestResultArea() {
return [
[
'@label',
'ResultTest',
],
[
'@start',
'1',
],
[
'@start',
'1',
1,
],
[
'@end',
'100',
],
[
'@end',
'1',
1,
],
[
'@total',
'100',
],
[
'@total',
'100',
1,
],
[
'@per_page',
'0',
],
[
'@per_page',
'1',
1,
],
[
'@current_page',
'1',
],
[
'@current_page',
'1',
1,
],
[
'@current_record_count',
'100',
],
[
'@current_record_count',
'1',
1,
],
[
'@page_count',
'1',
],
[
'@page_count',
'100',
1,
],
[
'@start | @end | @total',
'1 | 100 | 100',
],
[
'@start | @end | @total',
'1 | 1 | 100',
1,
],
];
}
protected function setupViewPager($items_per_page = 0) {
$pager = $this
->prophesize(PagerPluginBase::class);
$pager
->getItemsPerPage()
->willReturn($items_per_page)
->shouldBeCalledTimes(1);
$pager
->getCurrentPage()
->willReturn(0)
->shouldBeCalledTimes(1);
$this->view->pager = $pager
->reveal();
$this->view->style_plugin = new \stdClass();
$this->view->total_rows = 100;
$this->view->result = [
1,
2,
3,
4,
5,
];
}
}