View source
<?php
namespace Drupal\Tests\system\Functional\Database;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Database\Database;
use Symfony\Component\HttpFoundation\Request;
class SelectPagerDefaultTest extends DatabaseTestBase {
protected $defaultTheme = 'stark';
public function testEvenPagerQuery() {
$limit = 2;
$count = Database::getConnection()
->select('test')
->countQuery()
->execute()
->fetchField();
$correct_number = $limit;
$num_pages = floor($count / $limit);
if (!($num_pages * $limit < $count)) {
$num_pages--;
}
for ($page = 0; $page <= $num_pages; ++$page) {
$this
->drupalGet('database_test/pager_query_even/' . $limit, [
'query' => [
'page' => $page,
],
]);
$data = json_decode($this
->getSession()
->getPage()
->getContent());
if ($page == $num_pages) {
$correct_number = $count - $limit * $page;
}
$this
->assertCount($correct_number, $data->names, new FormattableMarkup('Correct number of records returned by pager: @number', [
'@number' => $correct_number,
]));
}
}
public function testOddPagerQuery() {
$limit = 2;
$count = Database::getConnection()
->select('test_task')
->countQuery()
->execute()
->fetchField();
$correct_number = $limit;
$num_pages = floor($count / $limit);
if (!($num_pages * $limit < $count)) {
$num_pages--;
}
for ($page = 0; $page <= $num_pages; ++$page) {
$this
->drupalGet('database_test/pager_query_odd/' . $limit, [
'query' => [
'page' => $page,
],
]);
$data = json_decode($this
->getSession()
->getPage()
->getContent());
if ($page == $num_pages) {
$correct_number = $count - $limit * $page;
}
$this
->assertCount($correct_number, $data->names, new FormattableMarkup('Correct number of records returned by pager: @number', [
'@number' => $correct_number,
]));
}
}
public function testInnerPagerQuery() {
$connection = Database::getConnection();
$query = $connection
->select('test', 't')
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender');
$query
->fields('t', [
'age',
])
->orderBy('age')
->limit(5);
$outer_query = $connection
->select($query);
$outer_query
->addField('subquery', 'age');
$outer_query
->orderBy('age');
$ages = $outer_query
->execute()
->fetchCol();
$this
->assertEqual($ages, [
25,
26,
27,
28,
], 'Inner pager query returned the correct ages.');
}
public function testHavingPagerQuery() {
$query = Database::getConnection()
->select('test', 't')
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender');
$query
->fields('t', [
'name',
])
->orderBy('name')
->groupBy('name')
->having('MAX(age) > :count', [
':count' => 26,
])
->limit(5);
$ages = $query
->execute()
->fetchCol();
$this
->assertEqual($ages, [
'George',
'Ringo',
], 'Pager query with having expression returned the correct ages.');
}
public function testElementNumbers() {
$request = Request::createFromGlobals();
$request->query
->replace([
'page' => '3, 2, 1, 0',
]);
\Drupal::getContainer()
->get('request_stack')
->push($request);
$connection = Database::getConnection();
$name = $connection
->select('test', 't')
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->element(2)
->fields('t', [
'name',
])
->orderBy('age')
->limit(1)
->execute()
->fetchField();
$this
->assertEqual($name, 'Paul', 'Pager query #1 with a specified element ID returned the correct results.');
$name = $connection
->select('test', 't')
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->element(1)
->fields('t', [
'name',
])
->orderBy('age')
->limit(1)
->execute()
->fetchField();
$this
->assertEqual($name, 'George', 'Pager query #2 with a specified element ID returned the correct results.');
$name = $connection
->select('test', 't')
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->fields('t', [
'name',
])
->orderBy('age')
->limit(1)
->execute()
->fetchField();
$this
->assertEqual($name, 'John', 'Pager query #3 with a generated element ID returned the correct results.');
}
}