FormDefaultHandlersTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Drupal\KernelTests\Core\Form;
  3. use Drupal\Core\Form\FormInterface;
  4. use Drupal\Core\Form\FormState;
  5. use Drupal\Core\Form\FormStateInterface;
  6. use Drupal\KernelTests\KernelTestBase;
  7. /**
  8. * Tests automatically added form handlers.
  9. *
  10. * @group Form
  11. */
  12. class FormDefaultHandlersTest extends KernelTestBase implements FormInterface {
  13. /**
  14. * Modules to enable.
  15. *
  16. * @var array
  17. */
  18. public static $modules = ['system'];
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected function setUp() {
  23. parent::setUp();
  24. $this->installSchema('system', ['key_value_expire']);
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function getFormId() {
  30. return 'test_form_handlers';
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function buildForm(array $form, FormStateInterface $form_state) {
  36. $form['#validate'][] = '::customValidateForm';
  37. $form['#submit'][] = '::customSubmitForm';
  38. $form['submit'] = ['#type' => 'submit', '#value' => 'Save'];
  39. return $form;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function customValidateForm(array &$form, FormStateInterface $form_state) {
  45. $test_handlers = $form_state->get('test_handlers');
  46. $test_handlers['validate'][] = __FUNCTION__;
  47. $form_state->set('test_handlers', $test_handlers);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function validateForm(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);
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function customSubmitForm(array &$form, FormStateInterface $form_state) {
  61. $test_handlers = $form_state->get('test_handlers');
  62. $test_handlers['submit'][] = __FUNCTION__;
  63. $form_state->set('test_handlers', $test_handlers);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function submitForm(array &$form, FormStateInterface $form_state) {
  69. $test_handlers = $form_state->get('test_handlers');
  70. $test_handlers['submit'][] = __FUNCTION__;
  71. $form_state->set('test_handlers', $test_handlers);
  72. }
  73. /**
  74. * Tests that default handlers are added even if custom are specified.
  75. */
  76. public function testDefaultAndCustomHandlers() {
  77. $form_state = new FormState();
  78. $form_builder = $this->container->get('form_builder');
  79. $form_builder->submitForm($this, $form_state);
  80. $handlers = $form_state->get('test_handlers');
  81. $this->assertIdentical(count($handlers['validate']), 2);
  82. $this->assertIdentical($handlers['validate'][0], 'customValidateForm');
  83. $this->assertIdentical($handlers['validate'][1], 'validateForm');
  84. $this->assertIdentical(count($handlers['submit']), 2);
  85. $this->assertIdentical($handlers['submit'][0], 'customSubmitForm');
  86. $this->assertIdentical($handlers['submit'][1], 'submitForm');
  87. }
  88. }