Commit 5c7ce575 authored by catch's avatar catch

Issue #1901290 by dawehner: Replace the mini pager with a lite pager (which...

Issue #1901290 by dawehner: Replace the mini pager with a lite pager (which does not run a count query).
parent 612c7e2c
<?php
/**
* @file
* Contains \Drupal\node\Tests\Views\RowPluginTest.
*/
namespace Drupal\node\Tests\Views;
/**
* Tests the node row plugin.
*
* @see \Drupal\node\Plugin\views\row\NodeRow
*/
class RowPluginTest extends NodeTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('node', 'comment');
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('test_node_row_plugin');
/**
* Contains all comments keyed by node used by the test.
*
* @var array
*/
protected $comments;
/**
* Contains all nodes used by this test.
*
* @var array
*/
protected $nodes;
public static function getInfo() {
return array(
'name' => 'Node: Row plugin',
'description' => 'Tests the node row plugin.',
'group' => 'Views Modules',
);
}
protected function setUp() {
parent::setUp();
$this->drupalCreateContentType(array('type' => 'article'));
// Create two nodes, with 5 comments on all of them.
for ($i = 0; $i < 2; $i++) {
$this->nodes[] = $this->drupalCreateNode(
array(
'type' => 'article',
'body' => array(
LANGUAGE_NOT_SPECIFIED => array(
array(
'value' => $this->randomName(42),
'format' => filter_default_format(),
'summary' => $this->randomName(),
),
),
),
)
);
}
foreach ($this->nodes as $node) {
for ($i = 0; $i < 5; $i++) {
$this->comments[$node->id()][] = $this->drupalCreateComment(array('nid' => $node->id()));
}
}
}
/**
* Helper function to create a random comment.
*
* @param array $settings
* (optional) An associative array of settings for the comment, as used in
* entity_create().
*
* @return \Drupal\comment\Plugin\Core\Entity\Comment
* Returns the created and saved comment.
*/
public function drupalCreateComment(array $settings = array()) {
$node = node_load($settings['nid']);
$settings += array(
'subject' => $this->randomName(),
'node_type' => "comment_node_{$node->bundle()}",
'comment_body' => $this->randomName(40),
);
$comment = entity_create('comment', $settings);
$comment->save();
return $comment;
}
/**
* Tests the node row plugin.
*/
public function testRowPlugin() {
$view = views_get_view('test_node_row_plugin');
$view->initDisplay();
$view->setDisplay('page');
$view->initStyle();
$view->style_plugin->row_plugin->options['view_mode'] = 'full';
// Test with view_mode full.
$output = $view->preview();
foreach ($this->nodes as $node) {
$body = $node->body;
$teaser = $body[LANGUAGE_NOT_SPECIFIED][0]['summary'];
$full = $body[LANGUAGE_NOT_SPECIFIED][0]['value'];
$this->assertFalse(strpos($output, $teaser) !== FALSE, 'Make sure the teaser appears in the output of the view.');
$this->assertTrue(strpos($output, $full) !== FALSE, 'Make sure the full text appears in the output of the view.');
}
// Test with teasers.
$view->style_plugin->row_plugin->options['view_mode'] = 'teaser';
$output = $view->preview();
foreach ($this->nodes as $node) {
$body = $node->body;
$teaser = $body[LANGUAGE_NOT_SPECIFIED][0]['summary'];
$full = $body[LANGUAGE_NOT_SPECIFIED][0]['value'];
$this->assertTrue(strpos($output, $teaser) !== FALSE, 'Make sure the teaser appears in the output of the view.');
$this->assertFalse(strpos($output, $full) !== FALSE, 'Make sure the full text does not appears in the output of the view if teaser is set as viewmode.');
}
// Test with links disabled.
$view->style_plugin->row_plugin->options['links'] = FALSE;
$output = $view->preview();
$this->drupalSetContent($output);
foreach ($this->nodes as $node) {
$this->assertFalse($this->xpath('//li[contains(@class, :class)]/a[contains(@href, :href)]', array(':class' => 'node-readmore', ':href' => "node/{$node->id()}")), 'Make sure no readmore link appears.');
}
// Test with links enabled.
$view->style_plugin->row_plugin->options['links'] = TRUE;
$output = $view->preview();
$this->drupalSetContent($output);
foreach ($this->nodes as $node) {
$this->assertTrue($this->xpath('//li[contains(@class, :class)]/a[contains(@href, :href)]', array(':class' => 'node-readmore', ':href' => "node/{$node->id()}")), 'Make sure no readmore link appears.');
}
// Test with comments enabled.
$view->style_plugin->row_plugin->options['comments'] = TRUE;
$output = $view->preview();
foreach ($this->nodes as $node) {
foreach ($this->comments[$node->id()] as $comment) {
$this->assertTrue(strpos($output, $comment->comment_body->value) !== FALSE, 'Make sure the comment appears in the output.');
}
}
// Test with comments disabled.
$view->style_plugin->row_plugin->options['comments'] = FALSE;
$output = $view->preview();
foreach ($this->nodes as $node) {
foreach ($this->comments[$node->id()] as $comment) {
$this->assertFalse(strpos($output, $comment->comment_body->value) !== FALSE, 'Make sure the comment does not appears in the output when the comments option disabled.');
}
}
}
}
base_field: nid
base_table: node
core: 8
description: ''
status: '1'
display:
default:
display_options:
access:
type: perm
cache:
type: none
exposed_form:
type: basic
filters:
status:
expose:
operator: '0'
field: status
group: '1'
id: status
table: node
value: '1'
plugin_id: boolean
pager:
options:
items_per_page: '10'
type: full
query:
type: views_query
row:
options:
build_mode: teaser
comments: '0'
links: '1'
type: node
sorts: { }
style:
type: default
title: test_node_row_plugin
display_plugin: default
display_title: Master
id: default
position: { }
page:
display_options:
path: test-node-row-plugin
display_plugin: page
display_title: Page
id: page
position: { }
human_name: test_node_row_plugin
langcode: und
module: views
id: test_node_row_plugin
tag: default
......@@ -19,7 +19,8 @@
* id = "mini",
* title = @Translation("Paged output, mini pager"),
* short_title = @Translation("Mini"),
* help = @Translation("Use the mini pager output.")
* help = @Translation("A simple pager containing previous and next links."),
* theme = "views_mini_pager"
* )
*/
class Mini extends SqlBase {
......@@ -48,17 +49,54 @@ public function summaryTitle() {
return format_plural($this->options['items_per_page'], 'Mini pager, @count item', 'Mini pager, @count items', array('@count' => $this->options['items_per_page']));
}
/**
* Overrides \Drupal\views\Plugin\views\pager\SqlBase::query().
*/
public function query() {
parent::query();
// Don't query for the next page if we have a pager that has a limited
// amount of pages.
if (empty($this->options['total_pages']) || ($this->get_current_page() < $this->options['total_pages'])) {
// Increase the items in the query in order to be able to find out whether
// there is another page.
$limit = $this->view->query->getLimit();
$limit += 1;
$this->view->query->set_limit($limit);
}
}
/**
* Overrides \Drupal\views\Plugin\views\pager\PagerPluginBase::use_count_query().
*/
public function use_count_query() {
return FALSE;
}
/**
* Overrides \Drupal\views\Plugin\views\pager\PagerPluginBase::postExecute().
*/
public function postExecute(&$result) {
// In query() one more item might have been retrieved than necessary. If so,
// the next link needs to be displayed and the item removed.
if (count($result) > $this->get_items_per_page()) {
array_pop($result);
// Make sure the pager shows the next link by setting the total items to
// the biggest possible number.
pager_default_initialize(PHP_INT_MAX, $this->get_items_per_page(), $this->options['id']);
}
}
/**
* Overrides \Drupal\views\Plugin\views\pager\PagerPluginBase::render().
*/
function render($input) {
$pager_theme = views_theme_functions('views_mini_pager', $this->view, $this->view->display_handler->display);
// The 1, 3 index are correct, see theme_pager().
$tags = array(
1 => $this->options['tags']['previous'],
3 => $this->options['tags']['next'],
);
$output = theme($pager_theme, array(
$output = theme($this->themeFunctions(), array(
'parameters' => $input,
'element' => $this->options['id'],
'tags' => $tags,
......
......@@ -23,6 +23,13 @@ abstract class QueryPluginBase extends PluginBase implements QueryInterface {
*/
var $pager = NULL;
/**
* Stores the limit of items that should be requested in the query.
*
* @var int
*/
protected $limit;
/**
* Generate a query and a countquery from all of the information supplied
* to the object.
......@@ -103,6 +110,13 @@ function set_offset($offset) {
$this->offset = $offset;
}
/**
* Returns the limit of the query.
*/
public function getLimit() {
return $this->limit;
}
/**
* Create a new grouping for the WHERE or HAVING clause.
*
......
......@@ -51,16 +51,38 @@ protected function setUp() {
public function testMiniPagerRender() {
menu_router_rebuild();
$this->drupalGet('test_mini_pager');
$this->assertText('›› test', 'Make sure the next link appears on the first page.');
$this->assertNoText('‹‹ test', 'Make sure the previous link does not appear on the first page.');
$this->assertText('›› test', 'The next link appears on the first page.');
$this->assertText('Page 1', 'The current page info shows the first page.');
$this->assertNoText('‹‹ test', 'The previous link does not appear on the first page.');
$this->assertText($this->nodes[0]->label());
$this->assertText($this->nodes[1]->label());
$this->assertText($this->nodes[2]->label());
$this->drupalGet('test_mini_pager', array('query' => array('page' => 1)));
$this->assertText('‹‹ test', 'Make sure the previous link appears.');
$this->assertText('›› test', 'Make sure the next link appears.');
$this->assertText('‹‹ test', 'The previous link appears.');
$this->assertText('Page 2', 'The current page info shows the second page.');
$this->assertText('›› test', 'The next link appears.');
$this->assertText($this->nodes[3]->label());
$this->assertText($this->nodes[4]->label());
$this->assertText($this->nodes[5]->label());
$this->drupalGet('test_mini_pager', array('query' => array('page' => 6)));
$this->assertNoText('›› test', 'Make sure the next link appears on the last page.');
$this->assertText('‹‹ test', 'Make sure the previous link does not appear on the last page.');
$this->assertNoText('›› test', 'The next link appears on the last page.');
$this->assertText('Page 7', 'The current page info shows the last page.');
$this->assertText('‹‹ test', 'The previous link does not appear on the last page.');
$this->assertText($this->nodes[18]->label());
$this->assertText($this->nodes[19]->label());
// Remove all items beside 1, so there should be no links shown.
for ($i = 0; $i < 19; $i++) {
$this->nodes[$i]->delete();
}
$this->drupalGet('test_mini_pager');
$this->assertNoText('‹‹ test', 'The previous link does not appear on the page.');
$this->assertText('Page 1', 'The current page info shows the only page.');
$this->assertNoText('‹‹ test', 'The previous link does not appear on the page.');
$this->assertText($this->nodes[19]->label());
}
}
......@@ -43,6 +43,7 @@ protected function viewsData() {
public function testQuery() {
$this->_testInitQuery();
$this->_testQueryExecute();
$this->queryMethodsTests();
}
/**
......@@ -67,4 +68,20 @@ public function _testQueryExecute() {
$this->assertTrue($view->result, 'Make sure the view result got filled');
}
/**
* Test methods provided by the QueryPluginBase.
*
* @see \Drupal\views\Plugin\views\query\QueryPluginBase
*/
protected function queryMethodsTests() {
$view = views_get_view('test_view');
$view->setDisplay();
$view->initQuery();
$this->assertFalse($view->query->getLimit(), 'Default to an empty limit.');
$rand_number = rand(5, 10);
$view->query->set_limit($rand_number);
$this->assertEqual($view->query->getLimit(), $rand_number, 'set_limit adapts the amount of items.');
}
}
......@@ -7,6 +7,7 @@
namespace Drupal\views\Tests;
use DOMDocument;
use Drupal\simpletest\WebTestBase;
/**
......
......@@ -66,7 +66,13 @@ display:
link_to_node: '1'
plugin_id: node
filters: { }
sorts: { }
sorts:
nid:
id: nid
table: node
field: nid
plugin_id: standard
order: ASC
title: test_mini_pager
filter_groups:
operator: AND
......
......@@ -234,6 +234,7 @@ function views_plugin_list() {
* node portion of the theme registry.
*/
function views_preprocess_node(&$vars) {
module_load_include('inc', 'node', 'node.views');
// The 'view' attribute of the node is added in views_preprocess_node()
if (!empty($vars['node']->view) && $vars['node']->view->storage->id()) {
$vars['view'] = $vars['node']->view;
......@@ -250,7 +251,7 @@ function views_preprocess_node(&$vars) {
}
// Allow to alter comments and links based on the settings in the row plugin.
if (!empty($vars['view']->style_plugin->row_plugin) && get_class($vars['view']->style_plugin->row_plugin) == 'views_plugin_row_node_view') {
if (!empty($vars['view']->style_plugin->row_plugin) && $vars['view']->style_plugin->row_plugin->getPluginId() == 'node') {
node_row_node_view_preprocess_node($vars);
}
}
......
......@@ -1042,12 +1042,9 @@ function theme_views_mini_pager($vars) {
$tags = $vars['tags'];
$element = $vars['element'];
$parameters = $vars['parameters'];
$quantity = $vars['quantity'];
// current is the page we are currently paged to
// Current is the page we are currently paged to.
$pager_current = $pager_page_array[$element] + 1;
// max is the maximum page number
$pager_max = $pager_total[$element];
// End of marker calculations.
$li_previous = array();
......@@ -1080,7 +1077,7 @@ function theme_views_mini_pager($vars) {
$items[] = array(
'#wrapper_attributes' => array('class' => array('pager-current')),
'#markup' => t('@current of @max', array('@current' => $pager_current, '@max' => $pager_max)),
'#markup' => t('Page @current', array('@current' => $pager_current)),
);
$items[] = array(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment