database_test.module 6.54 KB
Newer Older
1
2
<?php

Crell's avatar
Crell committed
3
4
use Drupal\Database\Query\AlterableInterface;

5
/**
6
 * Implements hook_query_alter().
7
 */
Crell's avatar
Crell committed
8
function database_test_query_alter(AlterableInterface $query) {
9
10
11
12
13
14

  if ($query->hasTag('database_test_alter_add_range')) {
    $query->range(0, 2);
  }

  if ($query->hasTag('database_test_alter_add_join')) {
15
16
    $people_alias = $query->join('test', 'people', "test_task.pid = %alias.id");
    $name_field = $query->addField($people_alias, 'name', 'name');
17
18
    $query->condition($people_alias . '.id', 2);
  }
19

20
21
22
23
  if ($query->hasTag('database_test_alter_change_conditional')) {
    $conditions =& $query->conditions();
    $conditions[0]['value'] = 2;
  }
24

25
26
27
28
  if ($query->hasTag('database_test_alter_change_fields')) {
    $fields =& $query->getFields();
    unset($fields['age']);
  }
29

30
31
32
  if ($query->hasTag('database_test_alter_change_expressions')) {
    $expressions =& $query->getExpressions();
    $expressions['double_age']['expression'] = 'age*3';
33
  }
34
}
35

36
37

/**
38
39
40
 * Implements hook_query_TAG_alter().
 *
 * Called by DatabaseTestCase::testAlterRemoveRange.
41
 */
Crell's avatar
Crell committed
42
function database_test_query_database_test_alter_remove_range_alter(AlterableInterface $query) {
43
44
45
  $query->range();
}

46
/**
47
 * Implements hook_menu().
48
49
 */
function database_test_menu() {
50
  $items['database_test/db_query_temporary'] = array(
51
52
53
    'access callback' => TRUE,
    'page callback' => 'database_test_db_query_temporary',
  );
54
55
56
57
58
59
60
61
62
63
64
65
  $items['database_test/pager_query_even'] = array(
    'access callback' => TRUE,
    'page callback' => 'database_test_even_pager_query',
  );
  $items['database_test/pager_query_odd'] = array(
    'access callback' => TRUE,
    'page callback' => 'database_test_odd_pager_query',
  );
  $items['database_test/tablesort'] = array(
    'access callback' => TRUE,
    'page callback' => 'database_test_tablesort',
  );
66
67
68
69
  $items['database_test/tablesort_first'] = array(
    'access callback' => TRUE,
    'page callback' => 'database_test_tablesort_first',
  );
70
71
72
73
74
  $items['database_test/tablesort_default_sort'] = array(
    'access callback' => TRUE,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('database_test_theme_tablesort'),
  );
75
76
77
78
  return $items;
}

/**
79
 * Run a db_query_temporary and output the table name and its number of rows.
80
81
82
83
84
85
 *
 * We need to test that the table created is temporary, so we run it here, in a
 * separate menu callback request; After this request is done, the temporary
 * table should automatically dropped.
 */
function database_test_db_query_temporary() {
86
  $table_name = db_query_temporary('SELECT status FROM {system}', array());
87
  drupal_json_output(array(
88
89
90
    'table_name' => $table_name,
    'row_count' => db_select($table_name)->countQuery()->execute()->fetchField(),
  ));
91
92
  exit;
}
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

/**
 * Run a pager query and return the results.
 *
 * This function does care about the page GET parameter, as set by the
 * simpletest HTTP call.
 */
function database_test_even_pager_query($limit) {

  $query = db_select('test', 't');
  $query
    ->fields('t', array('name'))
    ->orderBy('age');

  // This should result in 2 pages of results.
  $query = $query->extend('PagerDefault')->limit($limit);

  $names = $query->execute()->fetchCol();

112
  drupal_json_output(array(
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    'names' => $names,
  ));
  exit;
}

/**
 * Run a pager query and return the results.
 *
 * This function does care about the page GET parameter, as set by the
 * simpletest HTTP call.
 */
function database_test_odd_pager_query($limit) {

  $query = db_select('test_task', 't');
  $query
    ->fields('t', array('task'))
    ->orderBy('pid');

  // This should result in 4 pages of results.
  $query = $query->extend('PagerDefault')->limit($limit);

  $names = $query->execute()->fetchCol();

136
  drupal_json_output(array(
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    'names' => $names,
  ));
  exit;
}

/**
 * Run a tablesort query and return the results.
 *
 * This function does care about the page GET parameter, as set by the
 * simpletest HTTP call.
 */
function database_test_tablesort() {
  $header = array(
    'tid' => array('data' => t('Task ID'), 'field' => 'tid', 'sort' => 'desc'),
    'pid' => array('data' => t('Person ID'), 'field' => 'pid'),
    'task' => array('data' => t('Task'), 'field' => 'task'),
    'priority' => array('data' => t('Priority'), 'field' => 'priority', ),
  );

  $query = db_select('test_task', 't');
  $query
    ->fields('t', array('tid', 'pid', 'task', 'priority'));
159

160
161
162
163
164
  $query = $query->extend('TableSort')->orderByHeader($header);

  // We need all the results at once to check the sort.
  $tasks = $query->execute()->fetchAll();

165
  drupal_json_output(array(
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    'tasks' => $tasks,
  ));
  exit;
}

/**
 * Run a tablesort query with a second order_by after and return the results.
 *
 * This function does care about the page GET parameter, as set by the
 * simpletest HTTP call.
 */
function database_test_tablesort_first() {
  $header = array(
    'tid' => array('data' => t('Task ID'), 'field' => 'tid', 'sort' => 'desc'),
    'pid' => array('data' => t('Person ID'), 'field' => 'pid'),
    'task' => array('data' => t('Task'), 'field' => 'task'),
    'priority' => array('data' => t('Priority'), 'field' => 'priority', ),
  );

  $query = db_select('test_task', 't');
  $query
    ->fields('t', array('tid', 'pid', 'task', 'priority'));
188

189
  $query = $query->extend('TableSort')->orderByHeader($header)->orderBy('priority');
190
191
192
193

  // We need all the results at once to check the sort.
  $tasks = $query->execute()->fetchAll();

194
  drupal_json_output(array(
195
196
197
    'tasks' => $tasks,
  ));
  exit;
198
}
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243

/**
 * Output a form without setting a header sort.
 */
function database_test_theme_tablesort($form, &$form_state) {
  $header = array(
    'username' => array('data' => t('Username'), 'field' => 'u.name'),
    'status' => array('data' => t('Status'), 'field' => 'u.status'),
  );

  $query = db_select('users', 'u');
  $query->condition('u.uid', 0, '<>');
  user_build_filter_query($query);

  $count_query = clone $query;
  $count_query->addExpression('COUNT(u.uid)');

  $query = $query->extend('PagerDefault')->extend('TableSort');
  $query
    ->fields('u', array('uid', 'name', 'status', 'created', 'access'))
    ->limit(50)
    ->orderByHeader($header)
    ->setCountQuery($count_query);
  $result = $query->execute();

  $options = array();

  $status = array(t('blocked'), t('active'));
  $accounts = array();
  foreach ($result as $account) {
    $options[$account->uid] = array(
      'username' => check_plain($account->name),
      'status' =>  $status[$account->status],
    );
  }

  $form['accounts'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('No people available.'),
  );

  return $form;
}