Simpletest Coverage - modules/simpletest/tests/database_test.module

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

Legend

Missed
lines code that were not excersized during program execution.
Covered
lines code were excersized during program execution.
Comment/non executable
Comment or non-executable line of code.
Dead
lines of code that according to xdebug could not be executed. This is counted as coverage code because in almost all cases it is code that runnable.