3 namespace Drupal\KernelTests\Core\Form;
5 use Drupal\Core\Form\FormInterface;
6 use Drupal\Core\Form\FormState;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\KernelTests\KernelTestBase;
11 * Tests automatically added form handlers.
15 class FormDefaultHandlersTest extends KernelTestBase implements FormInterface {
22 public static $modules = ['system'];
27 protected function setUp() {
29 $this->installSchema('system', ['key_value_expire']);
35 public function getFormId() {
36 return 'test_form_handlers';
42 public function buildForm(array $form, FormStateInterface $form_state) {
43 $form['#validate'][] = '::customValidateForm';
44 $form['#submit'][] = '::customSubmitForm';
45 $form['submit'] = ['#type' => 'submit', '#value' => 'Save'];
52 public function customValidateForm(array &$form, FormStateInterface $form_state) {
53 $test_handlers = $form_state->get('test_handlers');
54 $test_handlers['validate'][] = __FUNCTION__;
55 $form_state->set('test_handlers', $test_handlers);
61 public function validateForm(array &$form, FormStateInterface $form_state) {
62 $test_handlers = $form_state->get('test_handlers');
63 $test_handlers['validate'][] = __FUNCTION__;
64 $form_state->set('test_handlers', $test_handlers);
70 public function customSubmitForm(array &$form, FormStateInterface $form_state) {
71 $test_handlers = $form_state->get('test_handlers');
72 $test_handlers['submit'][] = __FUNCTION__;
73 $form_state->set('test_handlers', $test_handlers);
79 public function submitForm(array &$form, FormStateInterface $form_state) {
80 $test_handlers = $form_state->get('test_handlers');
81 $test_handlers['submit'][] = __FUNCTION__;
82 $form_state->set('test_handlers', $test_handlers);
86 * Tests that default handlers are added even if custom are specified.
88 public function testDefaultAndCustomHandlers() {
89 $form_state = new FormState();
90 $form_builder = $this->container->get('form_builder');
91 $form_builder->submitForm($this, $form_state);
93 $handlers = $form_state->get('test_handlers');
95 $this->assertIdentical(count($handlers['validate']), 2);
96 $this->assertIdentical($handlers['validate'][0], 'customValidateForm');
97 $this->assertIdentical($handlers['validate'][1], 'validateForm');
99 $this->assertIdentical(count($handlers['submit']), 2);
100 $this->assertIdentical($handlers['submit'][0], 'customSubmitForm');
101 $this->assertIdentical($handlers['submit'][1], 'submitForm');