3 namespace Drupal\form_test\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Symfony\Component\HttpFoundation\JsonResponse;
10 * Form to test input forgery.
14 class FormTestInputForgeryForm extends FormBase {
19 public function getFormId() {
20 return '_form_test_input_forgery';
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
29 $form['checkboxes'] = [
30 '#title' => t('Checkboxes'),
31 '#type' => 'checkboxes',
39 '#value' => t('Submit'),
41 $form['#post_render'][] = [static::class, 'postRender'];
47 * Alters the rendered form to simulate input forgery.
49 * It's necessary to alter the rendered form here because Mink does not
50 * support manipulating the DOM tree.
52 * @param string $rendered_form
56 * The modified rendered form.
58 * @see \Drupal\Tests\system\Functional\Form\FormTest::testInputForgery()
60 public static function postRender($rendered_form) {
61 return str_replace('value="two"', 'value="FORGERY"', $rendered_form);
67 public function submitForm(array &$form, FormStateInterface $form_state) {
68 return new JsonResponse($form_state->getValues());