database_test.module

File

drupal-7.x/modules/simpletest/tests/database_test.module
View source
  1. <?php
  2. /**
  3. * Implements hook_query_alter().
  4. */
  5. function database_test_query_alter(QueryAlterableInterface $query) {
  6. if ($query->hasTag('database_test_alter_add_range')) {
  7. $query->range(0, 2);
  8. }
  9. if ($query->hasTag('database_test_alter_add_join')) {
  10. $people_alias = $query->join('test', 'people', "test_task.pid = %alias.id");
  11. $name_field = $query->addField($people_alias, 'name', 'name');
  12. $query->condition($people_alias . '.id', 2);
  13. }
  14. if ($query->hasTag('database_test_alter_change_conditional')) {
  15. $conditions =& $query->conditions();
  16. $conditions[0]['value'] = 2;
  17. }
  18. if ($query->hasTag('database_test_alter_change_fields')) {
  19. $fields =& $query->getFields();
  20. unset($fields['age']);
  21. }
  22. if ($query->hasTag('database_test_alter_change_expressions')) {
  23. $expressions =& $query->getExpressions();
  24. $expressions['double_age']['expression'] = 'age*3';
  25. }
  26. }
  27. /**
  28. * Implements hook_query_TAG_alter().
  29. *
  30. * Called by DatabaseTestCase::testAlterRemoveRange.
  31. */
  32. function database_test_query_database_test_alter_remove_range_alter(QueryAlterableInterface $query) {
  33. $query->range();
  34. }
  35. /**
  36. * Implements hook_menu().
  37. */
  38. function database_test_menu() {
  39. $items['database_test/db_query_temporary'] = array(
  40. 'access callback' => TRUE,
  41. 'page callback' => 'database_test_db_query_temporary',
  42. );
  43. $items['database_test/pager_query_even'] = array(
  44. 'access callback' => TRUE,
  45. 'page callback' => 'database_test_even_pager_query',
  46. );
  47. $items['database_test/pager_query_odd'] = array(
  48. 'access callback' => TRUE,
  49. 'page callback' => 'database_test_odd_pager_query',
  50. );
  51. $items['database_test/tablesort'] = array(
  52. 'access callback' => TRUE,
  53. 'page callback' => 'database_test_tablesort',
  54. );
  55. $items['database_test/tablesort_first'] = array(
  56. 'access callback' => TRUE,
  57. 'page callback' => 'database_test_tablesort_first',
  58. );
  59. $items['database_test/tablesort_default_sort'] = array(
  60. 'access callback' => TRUE,
  61. 'page callback' => 'drupal_get_form',
  62. 'page arguments' => array('database_test_theme_tablesort'),
  63. );
  64. return $items;
  65. }
  66. /**
  67. * Run a db_query_temporary and output the table name and its number of rows.
  68. *
  69. * We need to test that the table created is temporary, so we run it here, in a
  70. * separate menu callback request; After this request is done, the temporary
  71. * table should automatically dropped.
  72. */
  73. function database_test_db_query_temporary() {
  74. $table_name = db_query_temporary('SELECT status FROM {system}', array());
  75. drupal_json_output(array(
  76. 'table_name' => $table_name,
  77. 'row_count' => db_select($table_name)->countQuery()->execute()->fetchField(),
  78. ));
  79. exit;
  80. }
  81. /**
  82. * Run a pager query and return the results.
  83. *
  84. * This function does care about the page GET parameter, as set by the
  85. * simpletest HTTP call.
  86. */
  87. function database_test_even_pager_query($limit) {
  88. $query = db_select('test', 't');
  89. $query
  90. ->fields('t', array('name'))
  91. ->orderBy('age');
  92. // This should result in 2 pages of results.
  93. $query = $query->extend('PagerDefault')->limit($limit);
  94. $names = $query->execute()->fetchCol();
  95. drupal_json_output(array(
  96. 'names' => $names,
  97. ));
  98. exit;
  99. }
  100. /**
  101. * Run a pager query and return the results.
  102. *
  103. * This function does care about the page GET parameter, as set by the
  104. * simpletest HTTP call.
  105. */
  106. function database_test_odd_pager_query($limit) {
  107. $query = db_select('test_task', 't');
  108. $query
  109. ->fields('t', array('task'))
  110. ->orderBy('pid');
  111. // This should result in 4 pages of results.
  112. $query = $query->extend('PagerDefault')->limit($limit);
  113. $names = $query->execute()->fetchCol();
  114. drupal_json_output(array(
  115. 'names' => $names,
  116. ));
  117. exit;
  118. }
  119. /**
  120. * Run a tablesort query and return the results.
  121. *
  122. * This function does care about the page GET parameter, as set by the
  123. * simpletest HTTP call.
  124. */
  125. function database_test_tablesort() {
  126. $header = array(
  127. 'tid' => array('data' => t('Task ID'), 'field' => 'tid', 'sort' => 'desc'),
  128. 'pid' => array('data' => t('Person ID'), 'field' => 'pid'),
  129. 'task' => array('data' => t('Task'), 'field' => 'task'),
  130. 'priority' => array('data' => t('Priority'), 'field' => 'priority', ),
  131. );
  132. $query = db_select('test_task', 't');
  133. $query
  134. ->fields('t', array('tid', 'pid', 'task', 'priority'));
  135. $query = $query->extend('TableSort')->orderByHeader($header);
  136. // We need all the results at once to check the sort.
  137. $tasks = $query->execute()->fetchAll();
  138. drupal_json_output(array(
  139. 'tasks' => $tasks,
  140. ));
  141. exit;
  142. }
  143. /**
  144. * Run a tablesort query with a second order_by after and return the results.
  145. *
  146. * This function does care about the page GET parameter, as set by the
  147. * simpletest HTTP call.
  148. */
  149. function database_test_tablesort_first() {
  150. $header = array(
  151. 'tid' => array('data' => t('Task ID'), 'field' => 'tid', 'sort' => 'desc'),
  152. 'pid' => array('data' => t('Person ID'), 'field' => 'pid'),
  153. 'task' => array('data' => t('Task'), 'field' => 'task'),
  154. 'priority' => array('data' => t('Priority'), 'field' => 'priority', ),
  155. );
  156. $query = db_select('test_task', 't');
  157. $query
  158. ->fields('t', array('tid', 'pid', 'task', 'priority'));
  159. $query = $query->extend('TableSort')->orderByHeader($header)->orderBy('priority');
  160. // We need all the results at once to check the sort.
  161. $tasks = $query->execute()->fetchAll();
  162. drupal_json_output(array(
  163. 'tasks' => $tasks,
  164. ));
  165. exit;
  166. }
  167. /**
  168. * Output a form without setting a header sort.
  169. */
  170. function database_test_theme_tablesort($form, &$form_state) {
  171. $header = array(
  172. 'username' => array('data' => t('Username'), 'field' => 'u.name'),
  173. 'status' => array('data' => t('Status'), 'field' => 'u.status'),
  174. );
  175. $query = db_select('users', 'u');
  176. $query->condition('u.uid', 0, '<>');
  177. user_build_filter_query($query);
  178. $count_query = clone $query;
  179. $count_query->addExpression('COUNT(u.uid)');
  180. $query = $query->extend('PagerDefault')->extend('TableSort');
  181. $query
  182. ->fields('u', array('uid', 'name', 'status', 'created', 'access'))
  183. ->limit(50)
  184. ->orderByHeader($header)
  185. ->setCountQuery($count_query);
  186. $result = $query->execute();
  187. $options = array();
  188. $status = array(t('blocked'), t('active'));
  189. $accounts = array();
  190. foreach ($result as $account) {
  191. $options[$account->uid] = array(
  192. 'username' => check_plain($account->name),
  193. 'status' => $status[$account->status],
  194. );
  195. }
  196. $form['accounts'] = array(
  197. '#type' => 'tableselect',
  198. '#header' => $header,
  199. '#options' => $options,
  200. '#empty' => t('No people available.'),
  201. );
  202. return $form;
  203. }