Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / simpletest / src / Form / SimpletestTestForm.php
1 <?php
2
3 namespace Drupal\simpletest\Form;
4
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;
10
11 /**
12  * List tests arranged in groups that can be selected and run.
13  *
14  * @internal
15  */
16 class SimpletestTestForm extends FormBase {
17
18   /**
19    * The renderer.
20    *
21    * @var \Drupal\Core\Render\RendererInterface
22    */
23   protected $renderer;
24
25   /**
26    * The test discovery service.
27    *
28    * @var \Drupal\simpletest\TestDiscovery
29    */
30   protected $testDiscovery;
31
32   /**
33    * {@inheritdoc}
34    */
35   public static function create(ContainerInterface $container) {
36     return new static(
37       $container->get('renderer'),
38       $container->get('test_discovery')
39     );
40   }
41
42   /**
43    * Constructs a new SimpletestTestForm.
44    *
45    * @param \Drupal\Core\Render\RendererInterface $renderer
46    *   The renderer.
47    * @param \Drupal\simpletest\TestDiscovery $test_discovery
48    *   The test discovery service.
49    */
50   public function __construct(RendererInterface $renderer, TestDiscovery $test_discovery) {
51     $this->renderer = $renderer;
52     $this->testDiscovery = $test_discovery;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function getFormId() {
59     return 'simpletest_test_form';
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function buildForm(array $form, FormStateInterface $form_state) {
66     $form['actions'] = ['#type' => 'actions'];
67     $form['actions']['submit'] = [
68       '#type' => 'submit',
69       '#value' => $this->t('Run tests'),
70       '#tableselect' => TRUE,
71       '#button_type' => 'primary',
72     ];
73     $form['clean'] = [
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.'),
77       '#weight' => 200,
78     ];
79     $form['clean']['op'] = [
80       '#type' => 'submit',
81       '#value' => $this->t('Clean environment'),
82       '#submit' => ['simpletest_clean_environment'],
83     ];
84
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'])) {
89       return $form;
90     }
91
92     // JavaScript-only table filters.
93     $form['filters'] = [
94       '#type' => 'container',
95       '#attributes' => [
96         'class' => ['table-filter', 'js-show'],
97       ],
98     ];
99     $form['filters']['text'] = [
100       '#type' => 'search',
101       '#title' => $this->t('Search'),
102       '#size' => 30,
103       '#placeholder' => $this->t('Enter test nameā€¦'),
104       '#attributes' => [
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.'),
109       ],
110     ];
111
112     $form['tests'] = [
113       '#cache' => [
114         'keys' => ['simpletest_ui_table'],
115         'contexts' => ['test_discovery'],
116       ],
117       '#type' => 'table',
118       '#id' => 'simpletest-form-table',
119       '#tableselect' => TRUE,
120       '#header' => [
121         ['data' => $this->t('Test'), 'class' => ['simpletest-test-label']],
122         ['data' => $this->t('Description'), 'class' => ['simpletest-test-description']],
123       ],
124       '#empty' => $this->t('No tests to display.'),
125       '#attached' => [
126         'library' => [
127           'simpletest/drupal.simpletest',
128         ],
129       ],
130     ];
131
132     // Define the images used to expand/collapse the test groups.
133     $image_collapsed = [
134       '#theme' => 'image',
135       '#uri' => 'core/misc/menu-collapsed.png',
136       '#width' => '7',
137       '#height' => '7',
138       '#alt' => $this->t('Expand'),
139       '#title' => $this->t('Expand'),
140       '#suffix' => '<a href="#" class="simpletest-collapse">(' . $this->t('Expand') . ')</a>',
141     ];
142     $image_extended = [
143       '#theme' => 'image',
144       '#uri' => 'core/misc/menu-expanded.png',
145       '#width' => '7',
146       '#height' => '7',
147       '#alt' => $this->t('Collapse'),
148       '#title' => $this->t('Collapse'),
149       '#suffix' => '<a href="#" class="simpletest-collapse">(' . $this->t('Collapse') . ')</a>',
150     ];
151     $form['tests']['#attached']['drupalSettings']['simpleTest']['images'] = [
152       (string) $this->renderer->renderPlain($image_collapsed),
153       (string) $this->renderer->renderPlain($image_extended),
154     ];
155
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']],
161       ];
162
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)));
166
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'],
173         ],
174       ];
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'],
181         ],
182       ];
183       $form['tests'][$group]['description'] = [
184         '#markup' => '&nbsp;',
185         '#wrapper_attributes' => [
186           'class' => ['simpletest-group-description'],
187         ],
188       ];
189
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']],
194         ];
195         $form['tests'][$class]['title'] = [
196           '#type' => 'label',
197           '#title' => '\\' . $info['name'],
198           '#wrapper_attributes' => [
199             'class' => ['simpletest-test-label', 'table-filter-text-source'],
200           ],
201         ];
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'],
208           ],
209         ];
210       }
211     }
212
213     return $form;
214   }
215
216   /**
217    * {@inheritdoc}
218    */
219   public function submitForm(array &$form, FormStateInterface $form_state) {
220     // Test discovery does not run upon form submission.
221     $this->testDiscovery->registerTestNamespaces();
222
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']);
235     }
236
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]
243       );
244     }
245   }
246
247 }