Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Form / FormDefaultHandlersTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Form;
4
5 use Drupal\Core\Form\FormInterface;
6 use Drupal\Core\Form\FormState;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\KernelTests\KernelTestBase;
9
10 /**
11  * Tests automatically added form handlers.
12  *
13  * @group Form
14  */
15 class FormDefaultHandlersTest extends KernelTestBase implements FormInterface {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['system'];
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     parent::setUp();
29     $this->installSchema('system', ['key_value_expire']);
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public function getFormId() {
36     return 'test_form_handlers';
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public function buildForm(array $form, FormStateInterface $form_state) {
43     $form['#validate'][] = '::customValidateForm';
44     $form['#submit'][] = '::customSubmitForm';
45     $form['submit'] = ['#type' => 'submit', '#value' => 'Save'];
46     return $form;
47   }
48
49   /**
50    * {@inheritdoc}
51    */
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);
56   }
57
58   /**
59    * {@inheritdoc}
60    */
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);
65   }
66
67   /**
68    * {@inheritdoc}
69    */
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);
74   }
75
76   /**
77    * {@inheritdoc}
78    */
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);
83   }
84
85   /**
86    * Tests that default handlers are added even if custom are specified.
87    */
88   public function testDefaultAndCustomHandlers() {
89     $form_state = new FormState();
90     $form_builder = $this->container->get('form_builder');
91     $form_builder->submitForm($this, $form_state);
92
93     $handlers = $form_state->get('test_handlers');
94
95     $this->assertIdentical(count($handlers['validate']), 2);
96     $this->assertIdentical($handlers['validate'][0], 'customValidateForm');
97     $this->assertIdentical($handlers['validate'][1], 'validateForm');
98
99     $this->assertIdentical(count($handlers['submit']), 2);
100     $this->assertIdentical($handlers['submit'][0], 'customSubmitForm');
101     $this->assertIdentical($handlers['submit'][1], 'submitForm');
102   }
103
104 }