3 namespace Drupal\Tests\system\Functional\Database;
6 * Tests the tablesort query extender.
10 class SelectTableSortDefaultTest extends DatabaseTestBase {
13 * Confirms that a tablesort query returns the correct results.
15 * Note that we have to make an HTTP request to a test page handler
16 * because the pager depends on GET parameters.
18 public function testTableSortQuery() {
20 ['field' => t('Task ID'), 'sort' => 'desc', 'first' => 'perform at superbowl', 'last' => 'eat'],
21 ['field' => t('Task ID'), 'sort' => 'asc', 'first' => 'eat', 'last' => 'perform at superbowl'],
22 ['field' => t('Task'), 'sort' => 'asc', 'first' => 'code', 'last' => 'sleep'],
23 ['field' => t('Task'), 'sort' => 'desc', 'first' => 'sleep', 'last' => 'code'],
28 foreach ($sorts as $sort) {
29 $this->drupalGet('database_test/tablesort/', ['query' => ['order' => $sort['field'], 'sort' => $sort['sort']]]);
30 $data = json_decode($this->getSession()->getPage()->getContent());
32 $first = array_shift($data->tasks);
33 $last = array_pop($data->tasks);
35 $this->assertEqual($first->task, $sort['first'], 'Items appear in the correct order.');
36 $this->assertEqual($last->task, $sort['last'], 'Items appear in the correct order.');
41 * Confirms precedence of tablesorts headers.
43 * If a tablesort's orderByHeader is called before another orderBy, then its
44 * header happens first.
46 public function testTableSortQueryFirst() {
48 ['field' => t('Task ID'), 'sort' => 'desc', 'first' => 'perform at superbowl', 'last' => 'eat'],
49 ['field' => t('Task ID'), 'sort' => 'asc', 'first' => 'eat', 'last' => 'perform at superbowl'],
50 ['field' => t('Task'), 'sort' => 'asc', 'first' => 'code', 'last' => 'sleep'],
51 ['field' => t('Task'), 'sort' => 'desc', 'first' => 'sleep', 'last' => 'code'],
56 foreach ($sorts as $sort) {
57 $this->drupalGet('database_test/tablesort_first/', ['query' => ['order' => $sort['field'], 'sort' => $sort['sort']]]);
58 $data = json_decode($this->getSession()->getPage()->getContent());
60 $first = array_shift($data->tasks);
61 $last = array_pop($data->tasks);
63 $this->assertEqual($first->task, $sort['first'], format_string('Items appear in the correct order sorting by @field @sort.', ['@field' => $sort['field'], '@sort' => $sort['sort']]));
64 $this->assertEqual($last->task, $sort['last'], format_string('Items appear in the correct order sorting by @field @sort.', ['@field' => $sort['field'], '@sort' => $sort['sort']]));
69 * Confirms that tableselect is rendered without error.
71 * Specifically that no sort is set in a tableselect, and that header links
74 public function testTableSortDefaultSort() {
75 $assert = $this->assertSession();
77 $this->drupalGet('database_test/tablesort_default_sort');
79 // Verify that the table was displayed. Just the header is checked for
80 // because if there were any fatal errors or exceptions in displaying the
81 // sorted table, it would not print the table.
82 $assert->pageTextContains(t('Username'));
84 // Verify that the header links are built properly.
85 $assert->linkByHrefExists('database_test/tablesort_default_sort');
86 $assert->responseMatches('/\<a.*title\=\"' . t('sort by Username') . '\".*\>/');