28d056cecd991f1a8b466c9a59d152fdcc3637db
[yaffs-website] / web / core / modules / system / tests / src / Kernel / Form / ProgrammaticTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Kernel\Form;
4
5 use Drupal\Core\Form\FormState;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9  * Tests the programmatic form submission behavior.
10  *
11  * @group Form
12  */
13 class ProgrammaticTest extends KernelTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['form_test'];
21
22   /**
23    * Test the programmatic form submission workflow.
24    */
25   public function testSubmissionWorkflow() {
26     // Backup the current batch status and reset it to avoid conflicts while
27     // processing the dummy form submit handler.
28     $current_batch = $batch =& batch_get();
29     $batch = [];
30
31     // Test that a programmatic form submission is rejected when a required
32     // textfield is omitted and correctly processed when it is provided.
33     $this->doSubmitForm([], FALSE);
34     $this->doSubmitForm(['textfield' => 'test 1'], TRUE);
35     $this->doSubmitForm([], FALSE);
36     $this->doSubmitForm(['textfield' => 'test 2'], TRUE);
37
38     // Test that a programmatic form submission can turn on and off checkboxes
39     // which are, by default, checked.
40     $this->doSubmitForm(['textfield' => 'dummy value', 'checkboxes' => [1 => 1, 2 => 2]], TRUE);
41     $this->doSubmitForm(['textfield' => 'dummy value', 'checkboxes' => [1 => 1, 2 => NULL]], TRUE);
42     $this->doSubmitForm(['textfield' => 'dummy value', 'checkboxes' => [1 => NULL, 2 => 2]], TRUE);
43     $this->doSubmitForm(['textfield' => 'dummy value', 'checkboxes' => [1 => NULL, 2 => NULL]], TRUE);
44
45     // Test that a programmatic form submission can correctly click a button
46     // that limits validation errors based on user input. Since we do not
47     // submit any values for "textfield" here and the textfield is required, we
48     // only expect form validation to pass when validation is limited to a
49     // different field.
50     $this->doSubmitForm(['op' => 'Submit with limited validation', 'field_to_validate' => 'all'], FALSE);
51     $this->doSubmitForm(['op' => 'Submit with limited validation', 'field_to_validate' => 'textfield'], FALSE);
52     $this->doSubmitForm(['op' => 'Submit with limited validation', 'field_to_validate' => 'field_to_validate'], TRUE);
53
54     // Restore the current batch status.
55     $batch = $current_batch;
56   }
57
58   /**
59    * Helper function used to programmatically submit the form defined in
60    * form_test.module with the given values.
61    *
62    * @param $values
63    *   An array of field values to be submitted.
64    * @param $valid_input
65    *   A boolean indicating whether or not the form submission is expected to
66    *   be valid.
67    */
68   protected function doSubmitForm($values, $valid_input) {
69     // Programmatically submit the given values.
70     $form_state = (new FormState())->setValues($values);
71     \Drupal::formBuilder()->submitForm('\Drupal\form_test\Form\FormTestProgrammaticForm', $form_state);
72
73     // Check that the form returns an error when expected, and vice versa.
74     $errors = $form_state->getErrors();
75     $valid_form = empty($errors);
76     $args = [
77       '%values' => print_r($values, TRUE),
78       '%errors' => $valid_form ? t('None') : implode(' ', $errors),
79     ];
80     $this->assertTrue($valid_input == $valid_form, format_string('Input values: %values<br />Validation handler errors: %errors', $args));
81
82     // We check submitted values only if we have a valid input.
83     if ($valid_input) {
84       // Fetching the values that were set in the submission handler.
85       $stored_values = $form_state->get('programmatic_form_submit');
86       foreach ($values as $key => $value) {
87         $this->assertEqual($stored_values[$key], $value, format_string('Submission handler correctly executed: %stored_key is %stored_value', ['%stored_key' => $key, '%stored_value' => print_r($value, TRUE)]));
88       }
89     }
90   }
91
92   /**
93    * Test the programmed_bypass_access_check flag.
94    */
95   public function testProgrammaticAccessBypass() {
96     $form_state = (new FormState())->setValues([
97       'textfield' => 'dummy value',
98       'field_restricted' => 'dummy value',
99     ]);
100
101     // Programmatically submit the form with a value for the restricted field.
102     // Since programmed_bypass_access_check is set to TRUE by default, the
103     // field is accessible and can be set.
104     \Drupal::formBuilder()->submitForm('\Drupal\form_test\Form\FormTestProgrammaticForm', $form_state);
105     $values = $form_state->get('programmatic_form_submit');
106     $this->assertEqual($values['field_restricted'], 'dummy value', 'The value for the restricted field is stored correctly.');
107
108     // Programmatically submit the form with a value for the restricted field
109     // with programmed_bypass_access_check set to FALSE. Since access
110     // restrictions apply, the restricted field is inaccessible, and the value
111     // should not be stored.
112     $form_state->setProgrammedBypassAccessCheck(FALSE);
113     \Drupal::formBuilder()->submitForm('\Drupal\form_test\Form\FormTestProgrammaticForm', $form_state);
114     $values = $form_state->get('programmatic_form_submit');
115     $this->assertNotEqual($values['field_restricted'], 'dummy value', 'The value for the restricted field is not stored.');
116
117   }
118
119 }