3 namespace Drupal\Tests\system\Kernel\Form;
5 use Drupal\Core\Form\FormState;
6 use Drupal\KernelTests\KernelTestBase;
9 * Tests the programmatic form submission behavior.
13 class ProgrammaticTest extends KernelTestBase {
20 public static $modules = ['form_test'];
23 * Test the programmatic form submission workflow.
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();
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);
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);
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
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);
54 // Restore the current batch status.
55 $batch = $current_batch;
59 * Helper function used to programmatically submit the form defined in
60 * form_test.module with the given values.
63 * An array of field values to be submitted.
65 * A boolean indicating whether or not the form submission is expected to
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);
73 // Check that the form returns an error when expected, and vice versa.
74 $errors = $form_state->getErrors();
75 $valid_form = empty($errors);
77 '%values' => print_r($values, TRUE),
78 '%errors' => $valid_form ? t('None') : implode(' ', $errors),
80 $this->assertTrue($valid_input == $valid_form, format_string('Input values: %values<br />Validation handler errors: %errors', $args));
82 // We check submitted values only if we have a 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)]));
93 * Test the programmed_bypass_access_check flag.
95 public function testProgrammaticAccessBypass() {
96 $form_state = (new FormState())->setValues([
97 'textfield' => 'dummy value',
98 'field_restricted' => 'dummy value',
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.');
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.');