Commit a7955198 authored by alexpott's avatar alexpott

Issue #2572533 by mondrake: Add tests for multiple pagers on a given page

parent 30555f01
......@@ -90,6 +90,89 @@ protected function testPagerQueryParametersAndCacheContext() {
$this->assertCacheContext('url.query_args');
}
/**
* Test proper functioning of multiple pagers.
*/
protected function testMultiplePagers() {
// First page.
$this->drupalGet('pager-test/multiple-pagers');
// Test data.
// Expected URL query string param is 0-indexed.
// Expected page per pager is 1-indexed.
$test_data = [
// With no query, all pagers set to first page.
[
'input_query' => NULL,
'expected_page' => [0 => '1', 1 => '1', 4 => '1'],
'expected_query' => '?page=0,0,,,0',
],
// Blanks around page numbers should not be relevant.
[
'input_query' => '?page=2 , 10,,, 5 ,,',
'expected_page' => [0 => '3', 1 => '11', 4 => '6'],
'expected_query' => '?page=2,10,,,5',
],
// Blanks within page numbers should lead to only the first integer
// to be considered.
[
'input_query' => '?page=2 , 3 0,,, 4 13 ,,',
'expected_page' => [0 => '3', 1 => '4', 4 => '5'],
'expected_query' => '?page=2,3,,,4',
],
// If floats are passed as page numbers, only the integer part is
// returned.
[
'input_query' => '?page=2.1,6.999,,,5.',
'expected_page' => [0 => '3', 1 => '7', 4 => '6'],
'expected_query' => '?page=2,6,,,5',
],
// Partial page fragment, undefined pagers set to first page.
[
'input_query' => '?page=5,2',
'expected_page' => [0 => '6', 1 => '3', 4 => '1'],
'expected_query' => '?page=5,2,,,0',
],
// Partial page fragment, undefined pagers set to first page.
[
'input_query' => '?page=,2',
'expected_page' => [0 => '1', 1 => '3', 4 => '1'],
'expected_query' => '?page=0,2,,,0',
],
// Partial page fragment, undefined pagers set to first page.
[
'input_query' => '?page=,',
'expected_page' => [0 => '1', 1 => '1', 4 => '1'],
'expected_query' => '?page=0,0,,,0',
],
// With overflow pages, all pagers set to max page.
[
'input_query' => '?page=99,99,,,99',
'expected_page' => [0 => '16', 1 => '16', 4 => '16'],
'expected_query' => '?page=15,15,,,15',
],
// Wrong value for the page resets pager to first page.
[
'input_query' => '?page=bar,5,foo,qux,bet',
'expected_page' => [0 => '1', 1 => '6', 4 => '1'],
'expected_query' => '?page=0,5,,,0',
],
];
// We loop through the page with the test data query parameters, and check
// that the active page for each pager element has the expected page
// (1-indexed) and resulting query parameter
foreach ($test_data as $data) {
$input_query = str_replace(' ', '%20', $data['input_query']);
$this->drupalGet($GLOBALS['base_root'] . parse_url($this->getUrl())['path'] . $input_query, ['external' => TRUE]);
foreach ([0, 1, 4] as $pager_element) {
$active_page = $this->cssSelect("div.test-pager-{$pager_element} ul.pager__items li.is-active:contains('{$data['expected_page'][$pager_element]}')");
$destination = str_replace('%2C', ',', $active_page[0]->a['href'][0]->__toString());
$this->assertEqual($destination, $data['expected_query']);
}
}
}
/**
* Test proper functioning of the ellipsis.
*/
......
pager_test.multiple_pagers:
path: '/pager-test/multiple-pagers'
defaults:
_title: 'Page using multiple pagers for testing'
_controller: '\Drupal\pager_test\Controller\PagerTestController::multiplePagers'
requirements:
_access: 'TRUE'
pager_test.query_parameters:
path: '/pager-test/query-parameters'
defaults:
......
......@@ -9,6 +9,41 @@
*/
class PagerTestController extends ControllerBase {
/**
* Builds a render array for a pageable test table.
*
* @param int $element
* The pager element to be used for paging.
* @param int $limit
* The limit of rows per page for the specified element.
*
* @return array
* A render array.
*/
protected function buildTestTable($element, $limit) {
$header = [
['data' => 'wid'],
['data' => 'type'],
['data' => 'timestamp'],
];
$query = db_select('watchdog', 'd')->extend('Drupal\Core\Database\Query\PagerSelectExtender')->element($element);
$result = $query
->fields('d', array('wid', 'type', 'timestamp'))
->limit($limit)
->orderBy('d.wid')
->execute();
$rows = [];
foreach ($result as $row) {
$rows[] = ['data' => (array) $row];
}
return [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => $this->t("There are no watchdog records found in the db"),
];
}
/**
* Returns a pager with 'parameters' variable.
*
......@@ -18,27 +53,7 @@ class PagerTestController extends ControllerBase {
public function queryParameters() {
// Example query.
$header_0 = array(
array('data' => 'wid'),
array('data' => 'type'),
array('data' => 'timestamp'),
);
$query_0 = db_select('watchdog', 'd')->extend('Drupal\Core\Database\Query\PagerSelectExtender')->element(0);
$query_0->fields('d', array('wid', 'type', 'timestamp'));
$result_0 = $query_0
->limit(5)
->orderBy('d.wid')
->execute();
$rows_0 = array();
foreach ($result_0 as $row) {
$rows_0[] = array('data' => (array) $row);
}
$build['pager_table_0'] = array(
'#theme' => 'table',
'#header' => $header_0,
'#rows' => $rows_0,
'#empty' => $this->t("There are no watchdog records found in the db"),
);
$build['pager_table_0'] = $this->buildTestTable(0, 5);
// Counter of calls to the current pager.
$query_params = pager_get_query_parameters();
......@@ -60,6 +75,45 @@ public function queryParameters() {
return $build;
}
/**
* Returns a page with multiple pagers.
*/
public function multiplePagers() {
// Build three tables with same query and different pagers.
$build['pager_table_0'] = $this->buildTestTable(0, 20);
$build['pager_pager_0'] = array(
'#type' => 'container',
'#attributes' => ['class' => ['test-pager-0']],
'pager' => [
'#type' => 'pager',
'#element' => 0,
],
);
$build['pager_table_1'] = $this->buildTestTable(1, 20);
$build['pager_pager_1'] = array(
'#type' => 'container',
'#attributes' => ['class' => ['test-pager-1']],
'pager' => [
'#type' => 'pager',
'#element' => 1,
],
);
$build['pager_table_4'] = $this->buildTestTable(4, 20);
$build['pager_pager_4'] = array(
'#type' => 'container',
'#attributes' => ['class' => ['test-pager-4']],
'pager' => [
'#type' => 'pager',
'#element' => 4,
],
);
return $build;
}
/**
* #pre_render callback for #type => pager that shows the pager cache context.
*/
......
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