SimpletestResultsForm.php 9.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\simpletest\Form\SimpletestResultsForm.
 */

namespace Drupal\simpletest\Form;

use Drupal\Core\Database\Connection;
11
use Drupal\Core\Form\FormBase;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
use Symfony\Component\HttpFoundation\RedirectResponse;
14 15 16 17

/**
 * Test results form for $test_id.
 */
18
class SimpletestResultsForm extends FormBase {
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

  /**
   * Associative array of themed result images keyed by status.
   *
   * @var array
   */
  protected $statusImageMap;

  /**
   * The database connection service.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
38
    return new static(
39
      $container->get('database')
40
    );
41 42 43 44 45 46 47 48
  }

  /**
   * Constructs a \Drupal\simpletest\Form\SimpletestResultsForm object.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection service.
   */
49
  public function __construct(Connection $database) {
50 51
    $this->database = $database;
    // Initialize image mapping property.
52 53 54 55 56
    $image_pass = array(
      '#theme' => 'image',
      '#uri' => 'core/misc/watchdog-ok.png',
      '#width' => 18,
      '#height' => 18,
57
      '#alt' => $this->t('Pass'),
58 59 60 61 62 63
    );
    $image_fail = array(
      '#theme' => 'image',
      '#uri' => 'core/misc/watchdog-error.png',
      '#width' => 18,
      '#height' => 18,
64
      '#alt' => $this->t('Fail'),
65 66 67 68 69 70
    );
    $image_exception = array(
      '#theme' => 'image',
      '#uri' => 'core/misc/watchdog-warning.png',
      '#width' => 18,
      '#height' => 18,
71
      '#alt' => $this->t('Exception'),
72 73 74 75 76 77
    );
    $image_debug = array(
      '#theme' => 'image',
      '#uri' => 'core/misc/watchdog-warning.png',
      '#width' => 18,
      '#height' => 18,
78
      '#alt' => $this->t('Debug'),
79
    );
80
    $this->statusImageMap = array(
81 82 83 84
      'pass' => drupal_render($image_pass),
      'fail' => drupal_render($image_fail),
      'exception' => drupal_render($image_exception),
      'debug' => drupal_render($image_debug),
85 86 87 88 89 90
    );
  }

  /**
   * {@inheritdoc}
   */
91
  public function getFormId() {
92 93 94 95 96 97 98
    return 'simpletest_results_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, array &$form_state, $test_id = NULL) {
99 100
    // Make sure there are test results to display and a re-run is not being
    // performed.
101 102 103
    $results = array();

    if (is_numeric($test_id) && !$results = $this->getResults($test_id)) {
104
      drupal_set_message($this->t('No test results to display.'), 'error');
105
      return new RedirectResponse(url('admin/config/development/testing', array('absolute' => TRUE)));
106 107 108
    }

    // Load all classes and include CSS.
109
    $form['#attached']['css'][] = drupal_get_path('module', 'simpletest') . '/css/simpletest.module.css';
110 111 112 113 114 115 116 117 118 119

    // Keep track of which test cases passed or failed.
    $filter = array(
      'pass' => array(),
      'fail' => array(),
    );

    // Summary result widget.
    $form['result'] = array(
      '#type' => 'fieldset',
120
      '#title' => $this->t('Results'),
121 122 123 124 125 126 127 128 129 130 131 132 133
    );
    $form['result']['summary'] = $summary = array(
      '#theme' => 'simpletest_result_summary',
      '#pass' => 0,
      '#fail' => 0,
      '#exception' => 0,
      '#debug' => 0,
    );

    simpletest_classloader_register();

    // Cycle through each test group.
    $header = array(
134 135 136 137 138 139
      $this->t('Message'),
      $this->t('Group'),
      $this->t('Filename'),
      $this->t('Line'),
      $this->t('Function'),
      array('colspan' => 2, 'data' => $this->t('Status'))
140 141 142 143 144 145 146 147
    );
    $form['result']['results'] = array();
    foreach ($results as $group => $assertions) {
      // Create group details with summary information.
      $info = call_user_func(array($group, 'getInfo'));
      $form['result']['results'][$group] = array(
        '#type' => 'details',
        '#title' => $info['name'],
148
        '#open' => TRUE,
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
        '#description' => $info['description'],
      );
      $form['result']['results'][$group]['summary'] = $summary;
      $group_summary =& $form['result']['results'][$group]['summary'];

      // Create table of assertions for the group.
      $rows = array();
      foreach ($assertions as $assertion) {
        $row = array();
        $row[] = $assertion->message;
        $row[] = $assertion->message_group;
        $row[] = drupal_basename($assertion->file);
        $row[] = $assertion->line;
        $row[] = $assertion->function;
        $row[] = $this->statusImageMap[$assertion->status];

        $class = 'simpletest-' . $assertion->status;
        if ($assertion->message_group == 'Debug') {
          $class = 'simpletest-debug';
        }
        $rows[] = array('data' => $row, 'class' => array($class));

        $group_summary['#' . $assertion->status]++;
        $form['result']['summary']['#' . $assertion->status]++;
      }
      $form['result']['results'][$group]['table'] = array(
        '#theme' => 'table',
        '#header' => $header,
        '#rows' => $rows,
      );

      // Set summary information.
      $group_summary['#ok'] = $group_summary['#fail'] + $group_summary['#exception'] == 0;
182
      $form['result']['results'][$group]['#open'] = !$group_summary['#ok'];
183 184 185 186 187

      // Store test group (class) as for use in filter.
      $filter[$group_summary['#ok'] ? 'pass' : 'fail'][] = $group;
    }

188
    // Overall summary status.
189 190 191 192 193 194
    $form['result']['summary']['#ok'] = $form['result']['summary']['#fail'] + $form['result']['summary']['#exception'] == 0;

    // Actions.
    $form['#action'] = url('admin/config/development/testing/results/re-run');
    $form['action'] = array(
      '#type' => 'fieldset',
195
      '#title' => $this->t('Actions'),
196 197 198 199 200 201 202 203
      '#attributes' => array('class' => array('container-inline')),
      '#weight' => -11,
    );

    $form['action']['filter'] = array(
      '#type' => 'select',
      '#title' => 'Filter',
      '#options' => array(
204 205 206
        'all' => $this->t('All (@count)', array('@count' => count($filter['pass']) + count($filter['fail']))),
        'pass' => $this->t('Pass (@count)', array('@count' => count($filter['pass']))),
        'fail' => $this->t('Fail (@count)', array('@count' => count($filter['fail']))),
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
      ),
    );
    $form['action']['filter']['#default_value'] = ($filter['fail'] ? 'fail' : 'all');

    // Categorized test classes for to be used with selected filter value.
    $form['action']['filter_pass'] = array(
      '#type' => 'hidden',
      '#default_value' => implode(',', $filter['pass']),
    );
    $form['action']['filter_fail'] = array(
      '#type' => 'hidden',
      '#default_value' => implode(',', $filter['fail']),
    );

    $form['action']['op'] = array(
      '#type' => 'submit',
223
      '#value' => $this->t('Run tests'),
224 225 226 227
    );

    $form['action']['return'] = array(
      '#type' => 'link',
228
      '#title' => $this->t('Return to list'),
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
      '#href' => 'admin/config/development/testing',
    );

    if (is_numeric($test_id)) {
      simpletest_clean_results_table($test_id);
    }

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, array &$form_state) {
    $pass = $form_state['values']['filter_pass'] ? explode(',', $form_state['values']['filter_pass']) : array();
    $fail = $form_state['values']['filter_fail'] ? explode(',', $form_state['values']['filter_fail']) : array();

    if ($form_state['values']['filter'] == 'all') {
      $classes = array_merge($pass, $fail);
    }
    elseif ($form_state['values']['filter'] == 'pass') {
      $classes = $pass;
    }
    else {
      $classes = $fail;
    }

    if (!$classes) {
257
      $form_state['redirect_route']['route_name'] = 'simpletest.test_form';
258 259 260
      return;
    }

261
    $form_execute = array();
262 263
    $form_state_execute = array('values' => array());
    foreach ($classes as $class) {
264
      $form_state_execute['values']['tests'][$class] = $class;
265 266
    }

267
    // Submit the simpletest test form to rerun the tests.
268 269 270 271 272 273
    // Under normal circumstances, a form object's submitForm() should never be
    // called directly, FormBuilder::submitForm() should be called instead.
    // However, it sets $form_state['programmed'], which disables the Batch API.
    $simpletest_test_form = new SimpletestTestForm();
    $simpletest_test_form->buildForm($form_execute, $form_state_execute);
    $simpletest_test_form->submitForm($form_execute, $form_state_execute);
274
    $form_state['redirect_route'] = $form_state_execute['redirect_route'];
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
  }

  /**
   * Get test results for $test_id.
   *
   * @param int $test_id
   *   The test_id to retrieve results of.
   *
   * @return array
   *  Array of results grouped by test_class.
   */
  protected function getResults($test_id) {
    $results = $this->database->select('simpletest')
      ->fields('simpletest')
      ->condition('test_id', $test_id)
      ->orderBy('test_class')
      ->orderBy('message_id')
      ->execute();

    $test_results = array();
    foreach ($results as $result) {
      if (!isset($test_results[$result->test_class])) {
        $test_results[$result->test_class] = array();
      }
      $test_results[$result->test_class][] = $result;
    }

    return $test_results;
  }

}