diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/area/Result.php b/core/modules/views/lib/Drupal/views/Plugin/views/area/Result.php index 8b1e218..f1276ca 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/area/Result.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/area/Result.php @@ -8,6 +8,9 @@ namespace Drupal\views\Plugin\views\area; use Drupal\Component\Annotation\PluginID; +use Drupal\Component\Utility\String; +use Drupal\Component\Utility\Xss; +use Drupal\views\Plugin\views\style\DefaultSummary; /** * Views area handler to display some configurable result summary. @@ -54,13 +57,21 @@ public function buildOptionsForm(&$form, &$form_state) { ); } + /** + * {@inheritdoc} + */ + public function query() { + if (strpos($this->options['content'], '@total') !== FALSE) { + $this->view->get_total_rows = TRUE; + } + } /** * Implements \Drupal\views\Plugin\views\area\AreaPluginBase::render(). */ public function render($empty = FALSE) { // Must have options and does not work on summaries. - if (!isset($this->options['content']) || $this->view->plugin_name == 'default_summary') { + if (!isset($this->options['content']) || $this->view->style_plugin instanceof DefaultSummary) { return array(); } $output = ''; @@ -72,7 +83,7 @@ public function render($empty = FALSE) { // @TODO: Maybe use a possible is views empty functionality. // Not every view has total_rows set, use view->result instead. $total = isset($this->view->total_rows) ? $this->view->total_rows : count($this->view->result); - $label = check_plain($this->view->storage->label()); + $label = String::checkPlain($this->view->storage->label()); if ($per_page === 0) { $page_count = 1; $start = 1; @@ -96,7 +107,7 @@ public function render($empty = FALSE) { } // Send the output. if (!empty($total)) { - $output .= filter_xss_admin(str_replace(array_keys($replacements), array_values($replacements), $format)); + $output .= Xss::filterAdmin(str_replace(array_keys($replacements), array_values($replacements), $format)); } // Return as render array. return array( diff --git a/core/modules/views/tests/Drupal/views/Tests/Plugin/area/ResultTest.php b/core/modules/views/tests/Drupal/views/Tests/Plugin/area/ResultTest.php new file mode 100644 index 0000000..531993f --- /dev/null +++ b/core/modules/views/tests/Drupal/views/Tests/Plugin/area/ResultTest.php @@ -0,0 +1,145 @@ + 'Area: Result', + 'description' => 'Tests the Drupal\views\Plugin\views\area\Result handler.', + 'group' => 'Views Handlers', + ); + } + + public 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')); + + $this->view = new ViewExecutable($storage); + + $this->resultHandler = new Result(array(), 'result', array()); + $this->resultHandler->view = $this->view; + } + + /** + * Tests the query method. + */ + public function testQuery() { + $this->assertNull($this->view->get_total_rows); + // @total should set get_total_rows. + $this->resultHandler->options['content'] = '@total'; + $this->resultHandler->query(); + $this->assertTrue($this->view->get_total_rows); + // A different token should not. + $this->view->get_total_rows = NULL; + $this->resultHandler->options['content'] = '@current_page'; + $this->resultHandler->query(); + $this->assertNull($this->view->get_total_rows); + } + + /** + * Tests the rendered output of the Result area handler. + * + * @param string $content + * The content to use when rendering the handler. + * @param string $expected + * The expected content string. + * @param int $items_per_page + * The items per page of the configuration. + * + * @dataProvider providerTestResultArea + */ + 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()); + } + + /** + * Data provider for testResultArea. + * + * @return array + */ + 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), + ); + } + + /** + * Sets up a mock pager on the view executable object. + * + * @param int $items_per_page + * The value to return from getItemsPerPage(). + */ + 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); + } + +}