3 namespace Drupal\simpletest\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Render\RendererInterface;
8 use Drupal\simpletest\TestDiscovery;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
12 * List tests arranged in groups that can be selected and run.
16 class SimpletestTestForm extends FormBase {
21 * @var \Drupal\Core\Render\RendererInterface
26 * The test discovery service.
28 * @var \Drupal\simpletest\TestDiscovery
30 protected $testDiscovery;
35 public static function create(ContainerInterface $container) {
37 $container->get('renderer'),
38 $container->get('test_discovery')
43 * Constructs a new SimpletestTestForm.
45 * @param \Drupal\Core\Render\RendererInterface $renderer
47 * @param \Drupal\simpletest\TestDiscovery $test_discovery
48 * The test discovery service.
50 public function __construct(RendererInterface $renderer, TestDiscovery $test_discovery) {
51 $this->renderer = $renderer;
52 $this->testDiscovery = $test_discovery;
58 public function getFormId() {
59 return 'simpletest_test_form';
65 public function buildForm(array $form, FormStateInterface $form_state) {
66 $form['actions'] = ['#type' => 'actions'];
67 $form['actions']['submit'] = [
69 '#value' => $this->t('Run tests'),
70 '#tableselect' => TRUE,
71 '#button_type' => 'primary',
74 '#type' => 'fieldset',
75 '#title' => $this->t('Clean test environment'),
76 '#description' => $this->t('Remove tables with the prefix "test" followed by digits and temporary directories that are left over from tests that crashed. This is intended for developers when creating tests.'),
79 $form['clean']['op'] = [
81 '#value' => $this->t('Clean environment'),
82 '#submit' => ['simpletest_clean_environment'],
85 // Do not needlessly re-execute a full test discovery if the user input
86 // already contains an explicit list of test classes to run.
87 $user_input = $form_state->getUserInput();
88 if (!empty($user_input['tests'])) {
92 // JavaScript-only table filters.
94 '#type' => 'container',
96 'class' => ['table-filter', 'js-show'],
99 $form['filters']['text'] = [
101 '#title' => $this->t('Search'),
103 '#placeholder' => $this->t('Enter test nameā¦'),
105 'class' => ['table-filter-text'],
106 'data-table' => '#simpletest-test-form',
107 'autocomplete' => 'off',
108 'title' => $this->t('Enter at least 3 characters of the test name or description to filter by.'),
114 'keys' => ['simpletest_ui_table'],
115 'contexts' => ['test_discovery'],
118 '#id' => 'simpletest-form-table',
119 '#tableselect' => TRUE,
121 ['data' => $this->t('Test'), 'class' => ['simpletest-test-label']],
122 ['data' => $this->t('Description'), 'class' => ['simpletest-test-description']],
124 '#empty' => $this->t('No tests to display.'),
127 'simpletest/drupal.simpletest',
132 // Define the images used to expand/collapse the test groups.
135 '#uri' => 'core/misc/menu-collapsed.png',
138 '#alt' => $this->t('Expand'),
139 '#title' => $this->t('Expand'),
140 '#suffix' => '<a href="#" class="simpletest-collapse">(' . $this->t('Expand') . ')</a>',
144 '#uri' => 'core/misc/menu-expanded.png',
147 '#alt' => $this->t('Collapse'),
148 '#title' => $this->t('Collapse'),
149 '#suffix' => '<a href="#" class="simpletest-collapse">(' . $this->t('Collapse') . ')</a>',
151 $form['tests']['#attached']['drupalSettings']['simpleTest']['images'] = [
152 (string) $this->renderer->renderPlain($image_collapsed),
153 (string) $this->renderer->renderPlain($image_extended),
156 // Generate the list of tests arranged by group.
157 $groups = $this->testDiscovery->getTestClasses();
158 foreach ($groups as $group => $tests) {
159 $form['tests'][$group] = [
160 '#attributes' => ['class' => ['simpletest-group']],
163 // Make the class name safe for output on the page by replacing all
164 // non-word/decimal characters with a dash (-).
165 $group_class = 'module-' . strtolower(trim(preg_replace("/[^\w\d]/", "-", $group)));
167 // Override tableselect column with custom selector for this group.
168 // This group-select-all checkbox is injected via JavaScript.
169 $form['tests'][$group]['select'] = [
170 '#wrapper_attributes' => [
171 'id' => $group_class,
172 'class' => ['simpletest-group-select-all'],
175 $form['tests'][$group]['title'] = [
176 // Expand/collapse image.
177 '#prefix' => '<div class="simpletest-image" id="simpletest-test-group-' . $group_class . '"></div>',
178 '#markup' => '<label for="' . $group_class . '-group-select-all">' . $group . '</label>',
179 '#wrapper_attributes' => [
180 'class' => ['simpletest-group-label'],
183 $form['tests'][$group]['description'] = [
184 '#markup' => ' ',
185 '#wrapper_attributes' => [
186 'class' => ['simpletest-group-description'],
190 // Cycle through each test within the current group.
191 foreach ($tests as $class => $info) {
192 $form['tests'][$class] = [
193 '#attributes' => ['class' => [$group_class . '-test', 'js-hide']],
195 $form['tests'][$class]['title'] = [
197 '#title' => '\\' . $info['name'],
198 '#wrapper_attributes' => [
199 'class' => ['simpletest-test-label', 'table-filter-text-source'],
202 $form['tests'][$class]['description'] = [
203 '#prefix' => '<div class="description">',
204 '#plain_text' => $info['description'],
205 '#suffix' => '</div>',
206 '#wrapper_attributes' => [
207 'class' => ['simpletest-test-description', 'table-filter-text-source'],
219 public function submitForm(array &$form, FormStateInterface $form_state) {
220 // Test discovery does not run upon form submission.
221 $this->testDiscovery->registerTestNamespaces();
223 // This form accepts arbitrary user input for 'tests'.
224 // An invalid value will cause the $class_name lookup below to die with a
225 // fatal error. Regular user access mechanisms to this form are intact.
226 // The only validation effectively being skipped is the validation of
227 // available checkboxes vs. submitted checkboxes.
228 // @todo Refactor Form API to allow to POST values without constructing the
229 // entire form more easily, BUT retaining routing access security and
230 // retaining Form API CSRF #token security validation, and without having
231 // to rely on form caching.
232 $user_input = $form_state->getUserInput();
233 if ($form_state->isValueEmpty('tests') && !empty($user_input['tests'])) {
234 $form_state->setValue('tests', $user_input['tests']);
237 $tests_list = array_filter($form_state->getValue('tests'));
238 if (!empty($tests_list)) {
239 $test_id = simpletest_run_tests($tests_list, 'drupal');
240 $form_state->setRedirect(
241 'simpletest.result_form',
242 ['test_id' => $test_id]