Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / system / tests / modules / database_test / src / Controller / DatabaseTestController.php
1 <?php
2
3 namespace Drupal\database_test\Controller;
4
5 use Symfony\Component\HttpFoundation\JsonResponse;
6
7 /**
8  * Controller routines for database_test routes.
9  */
10 class DatabaseTestController {
11
12   /**
13    * Runs db_query_temporary() and outputs the table name and its number of rows.
14    *
15    * We need to test that the table created is temporary, so we run it here, in a
16    * separate menu callback request; After this request is done, the temporary
17    * table should automatically dropped.
18    *
19    * @return \Symfony\Component\HttpFoundation\JsonResponse
20    */
21   public function dbQueryTemporary() {
22     $table_name = db_query_temporary('SELECT age FROM {test}', []);
23     return new JsonResponse([
24       'table_name' => $table_name,
25       'row_count' => db_select($table_name)->countQuery()->execute()->fetchField(),
26     ]);
27   }
28
29   /**
30    * Runs a pager query and returns the results.
31    *
32    * This function does care about the page GET parameter, as set by the
33    * simpletest HTTP call.
34    *
35    * @return \Symfony\Component\HttpFoundation\JsonResponse
36    */
37   public function pagerQueryEven($limit) {
38     $query = db_select('test', 't');
39     $query
40       ->fields('t', ['name'])
41       ->orderBy('age');
42
43     // This should result in 2 pages of results.
44     $query = $query
45       ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
46       ->limit($limit);
47
48     $names = $query->execute()->fetchCol();
49
50     return new JsonResponse([
51       'names' => $names,
52     ]);
53   }
54
55   /**
56    * Runs a pager query and returns the results.
57    *
58    * This function does care about the page GET parameter, as set by the
59    * simpletest HTTP call.
60    *
61    * @return \Symfony\Component\HttpFoundation\JsonResponse
62    */
63   public function pagerQueryOdd($limit) {
64     $query = db_select('test_task', 't');
65     $query
66       ->fields('t', ['task'])
67       ->orderBy('pid');
68
69     // This should result in 4 pages of results.
70     $query = $query
71       ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
72       ->limit($limit);
73
74     $names = $query->execute()->fetchCol();
75
76     return new JsonResponse([
77       'names' => $names,
78     ]);
79   }
80
81   /**
82    * Runs a tablesort query and returns the results.
83    *
84    * This function does care about the page GET parameter, as set by the
85    * simpletest HTTP call.
86    *
87    * @return \Symfony\Component\HttpFoundation\JsonResponse
88    */
89   public function testTablesort() {
90     $header = [
91       'tid' => ['data' => t('Task ID'), 'field' => 'tid', 'sort' => 'desc'],
92       'pid' => ['data' => t('Person ID'), 'field' => 'pid'],
93       'task' => ['data' => t('Task'), 'field' => 'task'],
94       'priority' => ['data' => t('Priority'), 'field' => 'priority'],
95     ];
96
97     $query = db_select('test_task', 't');
98     $query
99       ->fields('t', ['tid', 'pid', 'task', 'priority']);
100
101     $query = $query
102       ->extend('Drupal\Core\Database\Query\TableSortExtender')
103       ->orderByHeader($header);
104
105     // We need all the results at once to check the sort.
106     $tasks = $query->execute()->fetchAll();
107
108     return new JsonResponse([
109       'tasks' => $tasks,
110     ]);
111   }
112
113   /**
114    * Runs a tablesort query with a second order_by after and returns the results.
115    *
116    * This function does care about the page GET parameter, as set by the
117    * simpletest HTTP call.
118    *
119    * @return \Symfony\Component\HttpFoundation\JsonResponse
120    */
121   public function testTablesortFirst() {
122     $header = [
123       'tid' => ['data' => t('Task ID'), 'field' => 'tid', 'sort' => 'desc'],
124       'pid' => ['data' => t('Person ID'), 'field' => 'pid'],
125       'task' => ['data' => t('Task'), 'field' => 'task'],
126       'priority' => ['data' => t('Priority'), 'field' => 'priority'],
127     ];
128
129     $query = db_select('test_task', 't');
130     $query
131       ->fields('t', ['tid', 'pid', 'task', 'priority']);
132
133     $query = $query
134       ->extend('Drupal\Core\Database\Query\TableSortExtender')
135       ->orderByHeader($header)
136       ->orderBy('priority');
137
138     // We need all the results at once to check the sort.
139     $tasks = $query->execute()->fetchAll();
140
141     return new JsonResponse([
142       'tasks' => $tasks,
143     ]);
144   }
145
146 }