SimpletestTestForm.php 8.1 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

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

namespace Drupal\simpletest\Form;

10
use Drupal\Component\Utility\SortArray;
11
use Drupal\Core\Form\FormBase;
12
use Drupal\Core\Form\FormStateInterface;
13 14
use Drupal\Core\Render\RendererInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
15 16 17 18

/**
 * List tests arranged in groups that can be selected and run.
 */
19
class SimpletestTestForm extends FormBase {
20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('renderer')
    );
  }

  /**
   * Constructs a new SimpletestTestForm.
   *
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
   */
  public function __construct(RendererInterface $renderer) {
    $this->renderer = $renderer;
  }

47 48 49
  /**
   * {@inheritdoc}
   */
50
  public function getFormId() {
51 52 53 54 55 56
    return 'simpletest_test_form';
  }

  /**
   * {@inheritdoc}
   */
57
  public function buildForm(array $form, FormStateInterface $form_state) {
58 59 60 61 62 63 64 65 66 67
    $form['actions'] = array('#type' => 'actions');
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this->t('Run tests'),
      '#tableselect' => TRUE,
      '#button_type' => 'primary',
    );

    // Do not needlessly re-execute a full test discovery if the user input
    // already contains an explicit list of test classes to run.
68 69
    $user_input = $form_state->getUserInput();
    if (!empty($user_input['tests'])) {
70 71 72
      return $form;
    }

73 74 75 76 77 78 79 80 81
    // JavaScript-only table filters.
    $form['filters'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array('table-filter', 'js-show'),
      ),
    );
    $form['filters']['text'] = array(
      '#type' => 'search',
82
      '#title' => $this->t('Search'),
83
      '#size' => 30,
84
      '#placeholder' => $this->t('Enter test name…'),
85 86 87 88
      '#attributes' => array(
        'class' => array('table-filter-text'),
        'data-table' => '#simpletest-test-form',
        'autocomplete' => 'off',
89
        'title' => $this->t('Enter at least 3 characters of the test name or description to filter by.'),
90 91 92
      ),
    );

93
    $form['tests'] = array(
94 95 96 97 98 99 100 101 102 103
      '#type' => 'table',
      '#id' => 'simpletest-form-table',
      '#tableselect' => TRUE,
      '#header' => array(
        array('data' => $this->t('Test'), 'class' => array('simpletest-test-label')),
        array('data' => $this->t('Description'), 'class' => array('simpletest-test-description')),
      ),
      '#empty' => $this->t('No tests to display.'),
      '#attached' => array(
        'library' => array(
104
          'simpletest/drupal.simpletest',
105 106
        ),
      ),
107 108
    );

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    // Define the images used to expand/collapse the test groups.
    $image_collapsed = array(
      '#theme' => 'image',
      '#uri' => 'core/misc/menu-collapsed.png',
      '#width' => '7',
      '#height' => '7',
      '#alt' => $this->t('Expand'),
      '#title' => $this->t('Expand'),
      '#suffix' => '<a href="#" class="simpletest-collapse">(' . $this->t('Expand') . ')</a>',
    );
    $image_extended = array(
      '#theme' => 'image',
      '#uri' => 'core/misc/menu-expanded.png',
      '#width' => '7',
      '#height' => '7',
      '#alt' => $this->t('Collapse'),
      '#title' => $this->t('Collapse'),
      '#suffix' => '<a href="#" class="simpletest-collapse">(' . $this->t('Collapse') . ')</a>',
    );
128
    $form['tests']['#attached']['drupalSettings']['simpleTest']['images'] = [
129 130
      (string) $this->renderer->renderPlain($image_collapsed),
      (string) $this->renderer->renderPlain($image_extended),
131
    ];
132 133 134 135

    // Generate the list of tests arranged by group.
    $groups = simpletest_test_get_all();
    foreach ($groups as $group => $tests) {
136 137 138 139 140 141 142 143 144 145 146 147 148
      $form['tests'][$group] = array(
        '#attributes' => array('class' => array('simpletest-group')),
      );

      // Make the class name safe for output on the page by replacing all
      // non-word/decimal characters with a dash (-).
      $group_class = 'module-' . strtolower(trim(preg_replace("/[^\w\d]/", "-", $group)));

      // Override tableselect column with custom selector for this group.
      // This group-select-all checkbox is injected via JavaScript.
      $form['tests'][$group]['select'] = array(
        '#wrapper_attributes' => array(
          'id' => $group_class,
149
          'class' => array('simpletest-group-select-all'),
150 151 152 153 154
        ),
      );
      $form['tests'][$group]['title'] = array(
        // Expand/collapse image.
        '#prefix' => '<div class="simpletest-image" id="simpletest-test-group-' . $group_class . '"></div>',
155
        '#markup' => '<label for="' . $group_class . '-group-select-all">' . $group . '</label>',
156 157 158 159 160 161 162 163 164 165 166 167
        '#wrapper_attributes' => array(
          'class' => array('simpletest-group-label'),
        ),
      );
      $form['tests'][$group]['description'] = array(
        '#markup' => '&nbsp;',
        '#wrapper_attributes' => array(
          'class' => array('simpletest-group-description'),
        ),
      );

      // Cycle through each test within the current group.
168
      foreach ($tests as $class => $info) {
169 170 171 172 173
        $form['tests'][$class] = array(
          '#attributes' => array('class' => array($group_class . '-test', 'js-hide')),
        );
        $form['tests'][$class]['title'] = array(
          '#type' => 'label',
174
          '#title' => '\\' . $info['name'],
175 176 177 178 179 180
          '#wrapper_attributes' => array(
            'class' => array('simpletest-test-label', 'table-filter-text-source'),
          ),
        );
        $form['tests'][$class]['description'] = array(
          '#prefix' => '<div class="description">',
181
          '#plain_text' => $info['description'],
182 183 184 185
          '#suffix' => '</div>',
          '#wrapper_attributes' => array(
            'class' => array('simpletest-test-description', 'table-filter-text-source'),
          ),
186 187 188 189 190 191
        );
      }
    }

    $form['clean'] = array(
      '#type' => 'fieldset',
192 193
      '#title' => $this->t('Clean test environment'),
      '#description' => $this->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.'),
194
      '#weight' => 200,
195 196 197
    );
    $form['clean']['op'] = array(
      '#type' => 'submit',
198
      '#value' => $this->t('Clean environment'),
199 200 201 202 203 204 205 206 207
      '#submit' => array('simpletest_clean_environment'),
    );

    return $form;
  }

  /**
   * {@inheritdoc}
   */
208
  public function submitForm(array &$form, FormStateInterface $form_state) {
209
    global $base_url;
210
    // Test discovery does not run upon form submission.
211 212
    simpletest_classloader_register();

213 214 215 216 217 218 219 220 221
    // This form accepts arbitrary user input for 'tests'.
    // An invalid value will cause the $class_name lookup below to die with a
    // fatal error. Regular user access mechanisms to this form are intact.
    // The only validation effectively being skipped is the validation of
    // available checkboxes vs. submitted checkboxes.
    // @todo Refactor Form API to allow to POST values without constructing the
    //   entire form more easily, BUT retaining routing access security and
    //   retaining Form API CSRF #token security validation, and without having
    //   to rely on form caching.
222 223 224
    $user_input = $form_state->getUserInput();
    if ($form_state->isValueEmpty('tests') && !empty($user_input['tests'])) {
      $form_state->setValue('tests', $user_input['tests']);
225
    }
226

227
    $tests_list = array();
228
    foreach ($form_state->getValue('tests') as $class_name => $value) {
229 230 231 232 233 234 235
      if ($value === $class_name) {
        if (is_subclass_of($class_name, 'PHPUnit_Framework_TestCase')) {
          $test_type = 'phpunit';
        }
        else {
          $test_type = 'simpletest';
        }
236 237 238
        $tests_list[$test_type][] = $class_name;
      }
    }
239
    if (!empty($tests_list)) {
240
      putenv('SIMPLETEST_BASE_URL=' . $base_url);
241
      $test_id = simpletest_run_tests($tests_list, 'drupal');
242 243 244
      $form_state->setRedirect(
        'simpletest.result_form',
        array('test_id' => $test_id)
245
      );
246 247 248 249
    }
  }

}