67d71b361e1f081ba036a9084ed6a4b925cbdd54
[yaffs-website] / web / core / modules / system / tests / modules / form_test / src / Form / FormTestInputForgeryForm.php
1 <?php
2
3 namespace Drupal\form_test\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Symfony\Component\HttpFoundation\JsonResponse;
8
9 /**
10  * Form to test input forgery.
11  *
12  * @internal
13  */
14 class FormTestInputForgeryForm extends FormBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public function getFormId() {
20     return '_form_test_input_forgery';
21   }
22
23   /**
24    * {@inheritdoc}
25    */
26   public function buildForm(array $form, FormStateInterface $form_state) {
27     // For testing that a user can't submit a value not matching one of the
28     // allowed options.
29     $form['checkboxes'] = [
30       '#title' => t('Checkboxes'),
31       '#type' => 'checkboxes',
32       '#options' => [
33         'one' => 'One',
34         'two' => 'Two',
35       ],
36     ];
37     $form['submit'] = [
38       '#type' => 'submit',
39       '#value' => t('Submit'),
40     ];
41     $form['#post_render'][] = [static::class, 'postRender'];
42
43     return $form;
44   }
45
46   /**
47    * Alters the rendered form to simulate input forgery.
48    *
49    * It's necessary to alter the rendered form here because Mink does not
50    * support manipulating the DOM tree.
51    *
52    * @param string $rendered_form
53    *   The rendered form.
54    *
55    * @return string
56    *   The modified rendered form.
57    *
58    * @see \Drupal\Tests\system\Functional\Form\FormTest::testInputForgery()
59    */
60   public static function postRender($rendered_form) {
61     return str_replace('value="two"', 'value="FORGERY"', $rendered_form);
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function submitForm(array &$form, FormStateInterface $form_state) {
68     return new JsonResponse($form_state->getValues());
69   }
70
71 }