simpletest.pages.inc

Page callbacks for simpletest module.

Functions

Namesort descending Description
simpletest_result_form Test results form for $test_id.
simpletest_result_form_submit Re-run the tests that match the filter.
simpletest_result_get Get test results for $test_id.
simpletest_result_status_image Get the appropriate image for the status.
simpletest_settings_form Provides settings form for SimpleTest variables.
simpletest_settings_form_validate Validation handler for simpletest_settings_form().
simpletest_test_form List tests arranged in groups that can be selected and run.
simpletest_test_form_submit Run selected tests.
theme_simpletest_result_summary Returns HTML for the summary status of a simpletest result.
theme_simpletest_test_table Returns HTML for a test list generated by simpletest_test_form() into a table.

File

drupal/modules/simpletest/simpletest.pages.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Page callbacks for simpletest module.
  5. */
  6. /**
  7. * List tests arranged in groups that can be selected and run.
  8. */
  9. function simpletest_test_form($form) {
  10. $form['tests'] = array(
  11. '#type' => 'fieldset',
  12. '#title' => t('Tests'),
  13. '#description' => t('Select the test(s) or test group(s) you would like to run, and click <em>Run tests</em>.'),
  14. );
  15. $form['tests']['table'] = array(
  16. '#theme' => 'simpletest_test_table',
  17. );
  18. // Generate the list of tests arranged by group.
  19. $groups = simpletest_test_get_all();
  20. foreach ($groups as $group => $tests) {
  21. $form['tests']['table'][$group] = array(
  22. '#collapsed' => TRUE,
  23. );
  24. foreach ($tests as $class => $info) {
  25. $form['tests']['table'][$group][$class] = array(
  26. '#type' => 'checkbox',
  27. '#title' => $info['name'],
  28. '#description' => $info['description'],
  29. );
  30. }
  31. }
  32. // Operation buttons.
  33. $form['tests']['op'] = array(
  34. '#type' => 'submit',
  35. '#value' => t('Run tests'),
  36. );
  37. $form['clean'] = array(
  38. '#type' => 'fieldset',
  39. '#collapsible' => FALSE,
  40. '#collapsed' => FALSE,
  41. '#title' => t('Clean test environment'),
  42. '#description' => t('Remove tables with the prefix "simpletest" and temporary directories that are left over from tests that crashed. This is intended for developers when creating tests.'),
  43. );
  44. $form['clean']['op'] = array(
  45. '#type' => 'submit',
  46. '#value' => t('Clean environment'),
  47. '#submit' => array('simpletest_clean_environment'),
  48. );
  49. return $form;
  50. }
  51. /**
  52. * Returns HTML for a test list generated by simpletest_test_form() into a table.
  53. *
  54. * @param $variables
  55. * An associative array containing:
  56. * - table: A render element representing the table.
  57. *
  58. * @ingroup themeable
  59. */
  60. function theme_simpletest_test_table($variables) {
  61. $table = $variables['table'];
  62. drupal_add_css(drupal_get_path('module', 'simpletest') . '/simpletest.css');
  63. drupal_add_js(drupal_get_path('module', 'simpletest') . '/simpletest.js');
  64. drupal_add_js('misc/tableselect.js');
  65. // Create header for test selection table.
  66. $header = array(
  67. array('class' => array('select-all')),
  68. array('data' => t('Test'), 'class' => array('simpletest_test')),
  69. array('data' => t('Description'), 'class' => array('simpletest_description')),
  70. );
  71. // Define the images used to expand/collapse the test groups.
  72. $js = array(
  73. 'images' => array(
  74. theme('image', array('path' => 'misc/menu-collapsed.png', 'width' => 7, 'height' => 7, 'alt' => t('Expand'), 'title' => t('Expand'))) . ' <a href="#" class="simpletest-collapse">(' . t('Expand') . ')</a>',
  75. theme('image', array('path' => 'misc/menu-expanded.png', 'width' => 7, 'height' => 7, 'alt' => t('Collapse'), 'title' => t('Collapse'))) . ' <a href="#" class="simpletest-collapse">(' . t('Collapse') . ')</a>',
  76. ),
  77. );
  78. // Cycle through each test group and create a row.
  79. $rows = array();
  80. foreach (element_children($table) as $key) {
  81. $element = &$table[$key];
  82. $row = array();
  83. // Make the class name safe for output on the page by replacing all
  84. // non-word/decimal characters with a dash (-).
  85. $test_class = strtolower(trim(preg_replace("/[^\w\d]/", "-", $key)));
  86. // Select the right "expand"/"collapse" image, depending on whether the
  87. // category is expanded (at least one test selected) or not.
  88. $collapsed = !empty($element['#collapsed']);
  89. $image_index = $collapsed ? 0 : 1;
  90. // Place-holder for checkboxes to select group of tests.
  91. $row[] = array('id' => $test_class, 'class' => array('simpletest-select-all'));
  92. // Expand/collapse image and group title.
  93. $row[] = array(
  94. 'data' => '<div class="simpletest-image" id="simpletest-test-group-' . $test_class . '"></div>' .
  95. '<label for="' . $test_class . '-select-all" class="simpletest-group-label">' . $key . '</label>',
  96. 'class' => array('simpletest-group-label'),
  97. );
  98. $row[] = array(
  99. 'data' => '&nbsp;',
  100. 'class' => array('simpletest-group-description'),
  101. );
  102. $rows[] = array('data' => $row, 'class' => array('simpletest-group'));
  103. // Add individual tests to group.
  104. $current_js = array(
  105. 'testClass' => $test_class . '-test',
  106. 'testNames' => array(),
  107. 'imageDirection' => $image_index,
  108. 'clickActive' => FALSE,
  109. );
  110. // Sorting $element by children's #title attribute instead of by class name.
  111. uasort($element, 'element_sort_by_title');
  112. // Cycle through each test within the current group.
  113. foreach (element_children($element) as $test_name) {
  114. $test = $element[$test_name];
  115. $row = array();
  116. $current_js['testNames'][] = $test['#id'];
  117. // Store test title and description so that checkbox won't render them.
  118. $title = $test['#title'];
  119. $description = $test['#description'];
  120. $test['#title_display'] = 'invisible';
  121. unset($test['#description']);
  122. // Test name is used to determine what tests to run.
  123. $test['#name'] = $test_name;
  124. $row[] = array(
  125. 'data' => drupal_render($test),
  126. 'class' => array('simpletest-test-select'),
  127. );
  128. $row[] = array(
  129. 'data' => '<label for="' . $test['#id'] . '">' . $title . '</label>',
  130. 'class' => array('simpletest-test-label'),
  131. );
  132. $row[] = array(
  133. 'data' => '<div class="description">' . $description . '</div>',
  134. 'class' => array('simpletest-test-description'),
  135. );
  136. $rows[] = array('data' => $row, 'class' => array($test_class . '-test', ($collapsed ? 'js-hide' : '')));
  137. }
  138. $js['simpletest-test-group-' . $test_class] = $current_js;
  139. unset($table[$key]);
  140. }
  141. // Add js array of settings.
  142. drupal_add_js(array('simpleTest' => $js), 'setting');
  143. if (empty($rows)) {
  144. return '<strong>' . t('No tests to display.') . '</strong>';
  145. }
  146. else {
  147. return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'simpletest-form-table')));
  148. }
  149. }
  150. /**
  151. * Run selected tests.
  152. */
  153. function simpletest_test_form_submit($form, &$form_state) {
  154. simpletest_classloader_register();
  155. // Get list of tests.
  156. $tests_list = array();
  157. foreach ($form_state['values'] as $class_name => $value) {
  158. // Since class_exists() will likely trigger an autoload lookup,
  159. // we do the fast check first.
  160. if ($value === 1 && class_exists($class_name)) {
  161. $tests_list[] = $class_name;
  162. }
  163. }
  164. if (count($tests_list) > 0 ) {
  165. $test_id = simpletest_run_tests($tests_list, 'drupal');
  166. $form_state['redirect'] = 'admin/config/development/testing/results/' . $test_id;
  167. }
  168. else {
  169. drupal_set_message(t('No test(s) selected.'), 'error');
  170. }
  171. }
  172. /**
  173. * Test results form for $test_id.
  174. */
  175. function simpletest_result_form($form, &$form_state, $test_id) {
  176. // Make sure there are test results to display and a re-run is not being performed.
  177. $results = array();
  178. if (is_numeric($test_id) && !$results = simpletest_result_get($test_id)) {
  179. drupal_set_message(t('No test results to display.'), 'error');
  180. drupal_goto('admin/config/development/testing');
  181. return $form;
  182. }
  183. // Load all classes and include CSS.
  184. drupal_add_css(drupal_get_path('module', 'simpletest') . '/simpletest.css');
  185. // Keep track of which test cases passed or failed.
  186. $filter = array(
  187. 'pass' => array(),
  188. 'fail' => array(),
  189. );
  190. // Summary result fieldset.
  191. $form['result'] = array(
  192. '#type' => 'fieldset',
  193. '#title' => t('Results'),
  194. );
  195. $form['result']['summary'] = $summary = array(
  196. '#theme' => 'simpletest_result_summary',
  197. '#pass' => 0,
  198. '#fail' => 0,
  199. '#exception' => 0,
  200. '#debug' => 0,
  201. );
  202. simpletest_classloader_register();
  203. // Cycle through each test group.
  204. $header = array(t('Message'), t('Group'), t('Filename'), t('Line'), t('Function'), array('colspan' => 2, 'data' => t('Status')));
  205. $form['result']['results'] = array();
  206. foreach ($results as $group => $assertions) {
  207. // Create group fieldset with summary information.
  208. $info = call_user_func(array($group, 'getInfo'));
  209. $form['result']['results'][$group] = array(
  210. '#type' => 'fieldset',
  211. '#title' => $info['name'],
  212. '#description' => $info['description'],
  213. '#collapsible' => TRUE,
  214. );
  215. $form['result']['results'][$group]['summary'] = $summary;
  216. $group_summary = &$form['result']['results'][$group]['summary'];
  217. // Create table of assertions for the group.
  218. $rows = array();
  219. foreach ($assertions as $assertion) {
  220. $row = array();
  221. $row[] = $assertion->message;
  222. $row[] = $assertion->message_group;
  223. $row[] = drupal_basename($assertion->file);
  224. $row[] = $assertion->line;
  225. $row[] = $assertion->function;
  226. $row[] = simpletest_result_status_image($assertion->status);
  227. $class = 'simpletest-' . $assertion->status;
  228. if ($assertion->message_group == 'Debug') {
  229. $class = 'simpletest-debug';
  230. }
  231. $rows[] = array('data' => $row, 'class' => array($class));
  232. $group_summary['#' . $assertion->status]++;
  233. $form['result']['summary']['#' . $assertion->status]++;
  234. }
  235. $form['result']['results'][$group]['table'] = array(
  236. '#theme' => 'table',
  237. '#header' => $header,
  238. '#rows' => $rows,
  239. );
  240. // Set summary information.
  241. $group_summary['#ok'] = $group_summary['#fail'] + $group_summary['#exception'] == 0;
  242. $form['result']['results'][$group]['#collapsed'] = $group_summary['#ok'];
  243. // Store test group (class) as for use in filter.
  244. $filter[$group_summary['#ok'] ? 'pass' : 'fail'][] = $group;
  245. }
  246. // Overal summary status.
  247. $form['result']['summary']['#ok'] = $form['result']['summary']['#fail'] + $form['result']['summary']['#exception'] == 0;
  248. // Actions.
  249. $form['#action'] = url('admin/config/development/testing/results/re-run');
  250. $form['action'] = array(
  251. '#type' => 'fieldset',
  252. '#title' => t('Actions'),
  253. '#attributes' => array('class' => array('container-inline')),
  254. '#weight' => -11,
  255. );
  256. $form['action']['filter'] = array(
  257. '#type' => 'select',
  258. '#title' => 'Filter',
  259. '#options' => array(
  260. 'all' => t('All (@count)', array('@count' => count($filter['pass']) + count($filter['fail']))),
  261. 'pass' => t('Pass (@count)', array('@count' => count($filter['pass']))),
  262. 'fail' => t('Fail (@count)', array('@count' => count($filter['fail']))),
  263. ),
  264. );
  265. $form['action']['filter']['#default_value'] = ($filter['fail'] ? 'fail' : 'all');
  266. // Categorized test classes for to be used with selected filter value.
  267. $form['action']['filter_pass'] = array(
  268. '#type' => 'hidden',
  269. '#default_value' => implode(',', $filter['pass']),
  270. );
  271. $form['action']['filter_fail'] = array(
  272. '#type' => 'hidden',
  273. '#default_value' => implode(',', $filter['fail']),
  274. );
  275. $form['action']['op'] = array(
  276. '#type' => 'submit',
  277. '#value' => t('Run tests'),
  278. );
  279. $form['action']['return'] = array(
  280. '#type' => 'link',
  281. '#title' => t('Return to list'),
  282. '#href' => 'admin/config/development/testing',
  283. );
  284. if (is_numeric($test_id)) {
  285. simpletest_clean_results_table($test_id);
  286. }
  287. return $form;
  288. }
  289. /**
  290. * Re-run the tests that match the filter.
  291. */
  292. function simpletest_result_form_submit($form, &$form_state) {
  293. $pass = $form_state['values']['filter_pass'] ? explode(',', $form_state['values']['filter_pass']) : array();
  294. $fail = $form_state['values']['filter_fail'] ? explode(',', $form_state['values']['filter_fail']) : array();
  295. if ($form_state['values']['filter'] == 'all') {
  296. $classes = array_merge($pass, $fail);
  297. }
  298. elseif ($form_state['values']['filter'] == 'pass') {
  299. $classes = $pass;
  300. }
  301. else {
  302. $classes = $fail;
  303. }
  304. if (!$classes) {
  305. $form_state['redirect'] = 'admin/config/development/testing';
  306. return;
  307. }
  308. $form_state_execute = array('values' => array());
  309. foreach ($classes as $class) {
  310. $form_state_execute['values'][$class] = 1;
  311. }
  312. simpletest_test_form_submit(array(), $form_state_execute);
  313. $form_state['redirect'] = $form_state_execute['redirect'];
  314. }
  315. /**
  316. * Returns HTML for the summary status of a simpletest result.
  317. *
  318. * @param $variables
  319. * An associative array containing:
  320. * - form: A render element representing the form.
  321. *
  322. * @ingroup themeable
  323. */
  324. function theme_simpletest_result_summary($variables) {
  325. $form = $variables['form'];
  326. return '<div class="simpletest-' . ($form['#ok'] ? 'pass' : 'fail') . '">' . _simpletest_format_summary_line($form) . '</div>';
  327. }
  328. /**
  329. * Get test results for $test_id.
  330. *
  331. * @param $test_id The test_id to retrieve results of.
  332. * @return Array of results grouped by test_class.
  333. */
  334. function simpletest_result_get($test_id) {
  335. $results = db_select('simpletest')
  336. ->fields('simpletest')
  337. ->condition('test_id', $test_id)
  338. ->orderBy('test_class')
  339. ->orderBy('message_id')
  340. ->execute();
  341. $test_results = array();
  342. foreach ($results as $result) {
  343. if (!isset($test_results[$result->test_class])) {
  344. $test_results[$result->test_class] = array();
  345. }
  346. $test_results[$result->test_class][] = $result;
  347. }
  348. return $test_results;
  349. }
  350. /**
  351. * Get the appropriate image for the status.
  352. *
  353. * @param $status Status string, either: pass, fail, exception.
  354. * @return HTML image or false.
  355. */
  356. function simpletest_result_status_image($status) {
  357. // $map does not use drupal_static() as its value never changes.
  358. static $map;
  359. if (!isset($map)) {
  360. $map = array(
  361. 'pass' => theme('image', array('path' => 'misc/watchdog-ok.png', 'width' => 18, 'height' => 18, 'alt' => t('Pass'))),
  362. 'fail' => theme('image', array('path' => 'misc/watchdog-error.png', 'width' => 18, 'height' => 18, 'alt' => t('Fail'))),
  363. 'exception' => theme('image', array('path' => 'misc/watchdog-warning.png', 'width' => 18, 'height' => 18, 'alt' => t('Exception'))),
  364. 'debug' => theme('image', array('path' => 'misc/watchdog-warning.png', 'width' => 18, 'height' => 18, 'alt' => t('Debug'))),
  365. );
  366. }
  367. if (isset($map[$status])) {
  368. return $map[$status];
  369. }
  370. return FALSE;
  371. }
  372. /**
  373. * Provides settings form for SimpleTest variables.
  374. *
  375. * @ingroup forms
  376. * @see simpletest_settings_form_validate()
  377. */
  378. function simpletest_settings_form($form, &$form_state) {
  379. $form['general'] = array(
  380. '#type' => 'fieldset',
  381. '#title' => t('General'),
  382. );
  383. $form['general']['simpletest_clear_results'] = array(
  384. '#type' => 'checkbox',
  385. '#title' => t('Clear results after each complete test suite run'),
  386. '#description' => t('By default SimpleTest will clear the results after they have been viewed on the results page, but in some cases it may be useful to leave the results in the database. The results can then be viewed at <em>admin/config/development/testing/[test_id]</em>. The test ID can be found in the database, simpletest table, or kept track of when viewing the results the first time. Additionally, some modules may provide more analysis or features that require this setting to be disabled.'),
  387. '#default_value' => variable_get('simpletest_clear_results', TRUE),
  388. );
  389. $form['general']['simpletest_verbose'] = array(
  390. '#type' => 'checkbox',
  391. '#title' => t('Provide verbose information when running tests'),
  392. '#description' => t('The verbose data will be printed along with the standard assertions and is useful for debugging. The verbose data will be erased between each test suite run. The verbose data output is very detailed and should only be used when debugging.'),
  393. '#default_value' => variable_get('simpletest_verbose', TRUE),
  394. );
  395. $form['httpauth'] = array(
  396. '#type' => 'fieldset',
  397. '#title' => t('HTTP authentication'),
  398. '#description' => t('HTTP auth settings to be used by the SimpleTest browser during testing. Useful when the site requires basic HTTP authentication.'),
  399. '#collapsible' => TRUE,
  400. '#collapsed' => TRUE,
  401. );
  402. $form['httpauth']['simpletest_httpauth_method'] = array(
  403. '#type' => 'select',
  404. '#title' => t('Method'),
  405. '#options' => array(
  406. CURLAUTH_BASIC => t('Basic'),
  407. CURLAUTH_DIGEST => t('Digest'),
  408. CURLAUTH_GSSNEGOTIATE => t('GSS negotiate'),
  409. CURLAUTH_NTLM => t('NTLM'),
  410. CURLAUTH_ANY => t('Any'),
  411. CURLAUTH_ANYSAFE => t('Any safe'),
  412. ),
  413. '#default_value' => variable_get('simpletest_httpauth_method', CURLAUTH_BASIC),
  414. );
  415. $username = variable_get('simpletest_httpauth_username');
  416. $password = variable_get('simpletest_httpauth_password');
  417. $form['httpauth']['simpletest_httpauth_username'] = array(
  418. '#type' => 'textfield',
  419. '#title' => t('Username'),
  420. '#default_value' => $username,
  421. );
  422. if ($username && $password) {
  423. $form['httpauth']['simpletest_httpauth_username']['#description'] = t('Leave this blank to delete both the existing username and password.');
  424. }
  425. $form['httpauth']['simpletest_httpauth_password'] = array(
  426. '#type' => 'password',
  427. '#title' => t('Password'),
  428. );
  429. if ($password) {
  430. $form['httpauth']['simpletest_httpauth_password']['#description'] = t('To change the password, enter the new password here.');
  431. }
  432. return system_settings_form($form);
  433. }
  434. /**
  435. * Validation handler for simpletest_settings_form().
  436. */
  437. function simpletest_settings_form_validate($form, &$form_state) {
  438. // If a username was provided but a password wasn't, preserve the existing
  439. // password.
  440. if (!empty($form_state['values']['simpletest_httpauth_username']) && empty($form_state['values']['simpletest_httpauth_password'])) {
  441. $form_state['values']['simpletest_httpauth_password'] = variable_get('simpletest_httpauth_password', '');
  442. }
  443. // If a password was provided but a username wasn't, the credentials are
  444. // incorrect, so throw an error.
  445. if (empty($form_state['values']['simpletest_httpauth_username']) && !empty($form_state['values']['simpletest_httpauth_password'])) {
  446. form_set_error('simpletest_httpauth_username', t('HTTP authentication credentials must include a username in addition to a password.'));
  447. }
  448. }