View source
<?php
namespace Drupal\Tests\system\Functional\Database;
use Drupal\Component\Render\FormattableMarkup;
class SelectTableSortDefaultTest extends DatabaseTestBase {
protected $defaultTheme = 'stark';
public function testTableSortQuery() {
$sorts = [
[
'field' => t('Task ID'),
'sort' => 'desc',
'first' => 'perform at superbowl',
'last' => 'eat',
],
[
'field' => t('Task ID'),
'sort' => 'asc',
'first' => 'eat',
'last' => 'perform at superbowl',
],
[
'field' => t('Task'),
'sort' => 'asc',
'first' => 'code',
'last' => 'sleep',
],
[
'field' => t('Task'),
'sort' => 'desc',
'first' => 'sleep',
'last' => 'code',
],
];
foreach ($sorts as $sort) {
$this
->drupalGet('database_test/tablesort/', [
'query' => [
'order' => $sort['field'],
'sort' => $sort['sort'],
],
]);
$data = json_decode($this
->getSession()
->getPage()
->getContent());
$first = array_shift($data->tasks);
$last = array_pop($data->tasks);
$this
->assertEquals($sort['first'], $first->task, 'Items appear in the correct order.');
$this
->assertEquals($sort['last'], $last->task, 'Items appear in the correct order.');
}
}
public function testTableSortQueryFirst() {
$sorts = [
[
'field' => t('Task ID'),
'sort' => 'desc',
'first' => 'perform at superbowl',
'last' => 'eat',
],
[
'field' => t('Task ID'),
'sort' => 'asc',
'first' => 'eat',
'last' => 'perform at superbowl',
],
[
'field' => t('Task'),
'sort' => 'asc',
'first' => 'code',
'last' => 'sleep',
],
[
'field' => t('Task'),
'sort' => 'desc',
'first' => 'sleep',
'last' => 'code',
],
];
foreach ($sorts as $sort) {
$this
->drupalGet('database_test/tablesort_first/', [
'query' => [
'order' => $sort['field'],
'sort' => $sort['sort'],
],
]);
$data = json_decode($this
->getSession()
->getPage()
->getContent());
$first = array_shift($data->tasks);
$last = array_pop($data->tasks);
$this
->assertEquals($sort['first'], $first->task, new FormattableMarkup('Items appear in the correct order sorting by @field @sort.', [
'@field' => $sort['field'],
'@sort' => $sort['sort'],
]));
$this
->assertEquals($sort['last'], $last->task, new FormattableMarkup('Items appear in the correct order sorting by @field @sort.', [
'@field' => $sort['field'],
'@sort' => $sort['sort'],
]));
}
}
public function testTableSortDefaultSort() {
$assert = $this
->assertSession();
$this
->drupalGet('database_test/tablesort_default_sort');
$assert
->pageTextContains('Username');
$assert
->linkByHrefExists('database_test/tablesort_default_sort');
$assert
->responseMatches('/\\<a.*title\\=\\"sort by Username\\".*\\>/');
}
}