Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Form / TriggeringElementProgrammedTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Form;
4
5 use Drupal\Core\Form\FormInterface;
6 use Drupal\Core\Form\FormState;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\KernelTests\KernelTestBase;
9
10 /**
11  * Tests detection of triggering_element for programmed form submissions.
12  *
13  * @group Form
14  */
15 class TriggeringElementProgrammedTest extends KernelTestBase implements FormInterface {
16
17   /**
18    * {@inheritdoc}
19    */
20   public function getFormId() {
21     return 'triggering_element_programmed_form';
22   }
23
24   /**
25    * {@inheritdoc}
26    */
27   public function buildForm(array $form, FormStateInterface $form_state) {
28     $form['one'] = [
29       '#type' => 'textfield',
30       '#title' => 'One',
31       '#required' => TRUE,
32     ];
33     $form['two'] = [
34       '#type' => 'textfield',
35       '#title' => 'Two',
36       '#required' => TRUE,
37     ];
38     $form['actions'] = ['#type' => 'actions'];
39     $user_input = $form_state->getUserInput();
40     $form['actions']['submit'] = [
41       '#type' => 'submit',
42       '#value' => 'Save',
43       '#limit_validation_errors' => [
44         [$user_input['section']],
45       ],
46       // Required for #limit_validation_errors.
47       '#submit' => [[$this, 'submitForm']],
48     ];
49     return $form;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function validateForm(array &$form, FormStateInterface $form_state) {
56     // Verify that the only submit button was recognized as triggering_element.
57     $this->assertEqual($form['actions']['submit']['#array_parents'], $form_state->getTriggeringElement()['#array_parents']);
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function submitForm(array &$form, FormStateInterface $form_state) {
64   }
65
66   /**
67    * Tests that #limit_validation_errors of the only submit button takes effect.
68    */
69   public function testLimitValidationErrors() {
70     // Programmatically submit the form.
71     $form_state = new FormState();
72     $form_state->setValue('section', 'one');
73     $form_builder = $this->container->get('form_builder');
74     $form_builder->submitForm($this, $form_state);
75
76     // Verify that only the specified section was validated.
77     $errors = $form_state->getErrors();
78     $this->assertTrue(isset($errors['one']), "Section 'one' was validated.");
79     $this->assertFalse(isset($errors['two']), "Section 'two' was not validated.");
80
81     // Verify that there are only values for the specified section.
82     $this->assertTrue($form_state->hasValue('one'), "Values for section 'one' found.");
83     $this->assertFalse($form_state->hasValue('two'), "Values for section 'two' not found.");
84   }
85
86 }