FormDefaultHandlersTest.php in Drupal 10
File
core/tests/Drupal/KernelTests/Core/Form/FormDefaultHandlersTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Form;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\KernelTests\KernelTestBase;
class FormDefaultHandlersTest extends KernelTestBase implements FormInterface {
public function getFormId() {
return 'test_form_handlers';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#validate'][] = '::customValidateForm';
$form['#submit'][] = '::customSubmitForm';
$form['submit'] = [
'#type' => 'submit',
'#value' => 'Save',
];
return $form;
}
public function customValidateForm(array &$form, FormStateInterface $form_state) {
$test_handlers = $form_state
->get('test_handlers');
$test_handlers['validate'][] = __FUNCTION__;
$form_state
->set('test_handlers', $test_handlers);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$test_handlers = $form_state
->get('test_handlers');
$test_handlers['validate'][] = __FUNCTION__;
$form_state
->set('test_handlers', $test_handlers);
}
public function customSubmitForm(array &$form, FormStateInterface $form_state) {
$test_handlers = $form_state
->get('test_handlers');
$test_handlers['submit'][] = __FUNCTION__;
$form_state
->set('test_handlers', $test_handlers);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$test_handlers = $form_state
->get('test_handlers');
$test_handlers['submit'][] = __FUNCTION__;
$form_state
->set('test_handlers', $test_handlers);
}
public function testDefaultAndCustomHandlers() {
$form_state = new FormState();
$form_builder = $this->container
->get('form_builder');
$form_builder
->submitForm($this, $form_state);
$handlers = $form_state
->get('test_handlers');
$this
->assertCount(2, $handlers['validate']);
$this
->assertSame('customValidateForm', $handlers['validate'][0]);
$this
->assertSame('validateForm', $handlers['validate'][1]);
$this
->assertCount(2, $handlers['submit']);
$this
->assertSame('customSubmitForm', $handlers['submit'][0]);
$this
->assertSame('submitForm', $handlers['submit'][1]);
}
}