Commit cffa061c authored by dawehner's avatar dawehner Committed by tim.plunkett
Browse files

Issue #1091554 by acrollet, tim.plunkett, dawehner | Take sure the pager api...

Issue #1091554 by acrollet, tim.plunkett, dawehner | Take sure the pager api functions work all the time as expected if the pager is not initialized yet.
parent 816a95e6
......@@ -301,24 +301,40 @@ function set_arguments($args) {
*/
function set_current_page($page) {
$this->current_page = $page;
// If the pager is already initialized, pass it through to the pager.
if (!empty($this->query->pager)) {
return $this->query->pager->set_current_page($page);
}
}
/**
* Get the current page from the pager.
*/
function get_current_page() {
// If the pager is already initialized, pass it through to the pager.
if (!empty($this->query->pager)) {
return $this->query->pager->get_current_page();
}
if (isset($this->current_page)) {
return $this->current_page;
}
}
/**
* Get the items per page from the pager.
*/
function get_items_per_page() {
// If the pager is already initialized, pass it through to the pager.
if (!empty($this->query->pager)) {
return $this->query->pager->get_items_per_page();
}
if (isset($this->items_per_page)) {
return $this->items_per_page;
}
}
/**
......@@ -337,9 +353,14 @@ function set_items_per_page($items_per_page) {
* Get the pager offset from the pager.
*/
function get_offset() {
// If the pager is already initialized, pass it through to the pager.
if (!empty($this->query->pager)) {
return $this->query->pager->get_offset();
}
if (isset($this->offset)) {
return $this->offset;
}
}
/**
......@@ -660,7 +681,7 @@ function init_pager() {
if (empty($this->query->pager)) {
$this->query->pager = $this->display_handler->get_plugin('pager');
if ($this->query->pager->use_pager()) {
if ($this->query->pager->use_pager() && isset($this->current_page)) {
$this->query->pager->set_current_page($this->current_page);
}
......@@ -1963,7 +1984,7 @@ function destroy() {
unset($this->query);
}
$keys = array('current_display', 'display_handler', 'build_info', 'built', 'executed', 'attachment_before', 'attachment_after', 'field', 'argument', 'filter', 'sort', 'relationship', 'header', 'footer', 'empty', 'query', 'result', 'inited', 'style_plugin', 'plugin_name', 'exposed_data', 'exposed_input', 'many_to_one_tables');
$keys = array('current_display', 'display_handler', 'build_info', 'built', 'executed', 'attachment_before', 'attachment_after', 'field', 'argument', 'filter', 'sort', 'relationship', 'header', 'footer', 'empty', 'query', 'result', 'inited', 'style_plugin', 'plugin_name', 'exposed_data', 'exposed_input', 'many_to_one_tables', 'items_per_page', 'offset', 'current_page');
foreach ($keys as $key) {
if (isset($this->$key)) {
unset($this->$key);
......
......@@ -8,7 +8,7 @@
/**
* Tests the pluggable pager system.
*/
class ViewsPagerTest extends ViewsTestCase {
class ViewsPagerTest extends ViewsSqlTest {
public static function getInfo() {
return array(
'name' => 'Pager',
......@@ -422,4 +422,56 @@ class ViewsPagerTest extends ViewsTestCase {
$output = $view->render();
$this->assertEqual(preg_match('/<ul class="pager">/', $output), 0, t('The pager is not rendered.'));
}
/**
* Test the api functions on the view object.
*/
function testPagerApi() {
$view = $this->viewsPagerFull();
// On the first round don't initialize the pager.
$this->assertEqual($view->get_items_per_page(), NULL, 'If the pager is not initialized and no manual override there is no items per page.');
$rand_number = rand(1, 5);
$view->set_items_per_page($rand_number);
$this->assertEqual($view->get_items_per_page(), $rand_number, 'Make sure get_items_per_page uses the settings of set_items_per_page.');
$this->assertEqual($view->get_offset(), NULL, 'If the pager is not initialized and no manual override there is no offset.');
$rand_number = rand(1, 5);
$view->set_offset($rand_number);
$this->assertEqual($view->get_offset(), $rand_number, 'Make sure get_offset uses the settings of set_offset.');
$this->assertEqual($view->get_current_page(), NULL, 'If the pager is not initialized and no manual override there is no current page.');
$rand_number = rand(1, 5);
$view->set_current_page($rand_number);
$this->assertEqual($view->get_current_page(), $rand_number, 'Make sure get_current_page uses the settings of set_current_page.');
$view->destroy();
// On this round enable the pager.
$view->init_display();
$view->init_query();
$view->init_pager();
$this->assertEqual($view->get_items_per_page(), 5, 'Per default the view has 5 items per page.');
$rand_number = rand(1, 5);
$view->set_items_per_page($rand_number);
$rand_number = rand(6, 11);
$view->query->pager->set_items_per_page($rand_number);
$this->assertEqual($view->get_items_per_page(), $rand_number, 'Make sure get_items_per_page uses the settings of set_items_per_page.');
$this->assertEqual($view->get_offset(), 0, 'Per default a view has a 0 offset.');
$rand_number = rand(1, 5);
$view->set_offset($rand_number);
$rand_number = rand(6, 11);
$view->query->pager->set_offset($rand_number);
$this->assertEqual($view->get_offset(), $rand_number, 'Make sure get_offset uses the settings of set_offset.');
$this->assertEqual($view->get_current_page(), 0, 'Per default the current page is 0.');
$rand_number = rand(1, 5);
$view->set_current_page($rand_number);
$rand_number = rand(6, 11);
$view->query->pager->set_current_page($rand_number);
$this->assertEqual($view->get_current_page(), $rand_number, 'Make sure get_current_page uses the settings of set_current_page.');
}
}
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