View source
<?php
namespace Drupal\Tests\views\Unit\Plugin\area;
use Drupal\Tests\UnitTestCase;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\area\Result;
class ResultTest extends UnitTestCase {
protected $view;
protected $resultHandler;
protected function setUp() {
parent::setUp();
$storage = $this
->getMockBuilder('Drupal\\views\\Entity\\View')
->disableOriginalConstructor()
->setMethods(array(
'label',
))
->getMock();
$storage
->expects($this
->any())
->method('label')
->will($this
->returnValue('ResultTest'));
$user = $this
->getMock('Drupal\\Core\\Session\\AccountInterface');
$views_data = $this
->getMockBuilder('Drupal\\views\\ViewsData')
->disableOriginalConstructor()
->getMock();
$route_provider = $this
->getMock('Drupal\\Core\\Routing\\RouteProviderInterface');
$this->view = new ViewExecutable($storage, $user, $views_data, $route_provider);
$this->resultHandler = new Result(array(), 'result', array());
$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(array(
'#markup' => $expected,
), $this->resultHandler
->render());
}
public function providerTestResultArea() {
return array(
array(
'@label',
'ResultTest',
),
array(
'@start',
'1',
),
array(
'@start',
'1',
1,
),
array(
'@end',
'100',
),
array(
'@end',
'1',
1,
),
array(
'@total',
'100',
),
array(
'@total',
'100',
1,
),
array(
'@per_page',
'0',
),
array(
'@per_page',
'1',
1,
),
array(
'@current_page',
'1',
),
array(
'@current_page',
'1',
1,
),
array(
'@current_record_count',
'100',
),
array(
'@current_record_count',
'1',
1,
),
array(
'@page_count',
'1',
),
array(
'@page_count',
'100',
1,
),
array(
'@start | @end | @total',
'1 | 100 | 100',
),
array(
'@start | @end | @total',
'1 | 1 | 100',
1,
),
);
}
protected function setupViewPager($items_per_page = 0) {
$pager = $this
->getMockBuilder('Drupal\\views\\Plugin\\views\\pager\\PagerPluginBase')
->disableOriginalConstructor()
->setMethods(array(
'getItemsPerPage',
'getCurrentPage',
))
->getMock();
$pager
->expects($this
->once())
->method('getItemsPerPage')
->will($this
->returnValue($items_per_page));
$pager
->expects($this
->once())
->method('getCurrentPage')
->will($this
->returnValue(0));
$this->view->pager = $pager;
$this->view->style_plugin = new \stdClass();
$this->view->total_rows = 100;
$this->view->result = array(
1,
2,
3,
4,
5,
);
}
}