Security update for Core, with self-updated composer
[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 class FormTestInputForgeryForm extends FormBase {
10
11   /**
12    * {@inheritdoc}
13    */
14   public function getFormId() {
15     return '_form_test_input_forgery';
16   }
17
18   /**
19    * {@inheritdoc}
20    */
21   public function buildForm(array $form, FormStateInterface $form_state) {
22     // For testing that a user can't submit a value not matching one of the
23     // allowed options.
24     $form['checkboxes'] = [
25       '#title' => t('Checkboxes'),
26       '#type' => 'checkboxes',
27       '#options' => [
28         'one' => 'One',
29         'two' => 'Two',
30       ],
31     ];
32     $form['submit'] = [
33       '#type' => 'submit',
34       '#value' => t('Submit'),
35     ];
36     $form['#post_render'][] = [static::class, 'postRender'];
37
38     return $form;
39   }
40
41   /**
42    * Alters the rendered form to simulate input forgery.
43    *
44    * It's necessary to alter the rendered form here because Mink does not
45    * support manipulating the DOM tree.
46    *
47    * @param string $rendered_form
48    *   The rendered form.
49    *
50    * @return string
51    *   The modified rendered form.
52    *
53    * @see \Drupal\Tests\system\Functional\Form\FormTest::testInputForgery()
54    */
55   public static function postRender($rendered_form) {
56     return str_replace('value="two"', 'value="FORGERY"', $rendered_form);
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function submitForm(array &$form, FormStateInterface $form_state) {
63     return new JsonResponse($form_state->getValues());
64   }
65
66 }