3 namespace Drupal\Tests\Core\Form;
5 use Drupal\Core\Form\FormState;
6 use Drupal\Tests\UnitTestCase;
9 * @coversDefaultClass \Drupal\Core\Form\FormErrorHandler
12 class FormErrorHandlerTest extends UnitTestCase {
15 * @covers ::handleFormErrors
16 * @covers ::displayErrorMessages
18 public function testDisplayErrorMessages() {
19 $form_error_handler = $this->getMockBuilder('Drupal\Core\Form\FormErrorHandler')
20 ->setMethods(['drupalSetMessage'])
23 $form_error_handler->expects($this->at(0))
24 ->method('drupalSetMessage')
25 ->with('invalid', 'error');
26 $form_error_handler->expects($this->at(1))
27 ->method('drupalSetMessage')
28 ->with('invalid', 'error');
29 $form_error_handler->expects($this->at(2))
30 ->method('drupalSetMessage')
31 ->with('invalid', 'error');
32 $form_error_handler->expects($this->at(3))
33 ->method('drupalSetMessage')
34 ->with('no title given', 'error');
35 $form_error_handler->expects($this->at(4))
36 ->method('drupalSetMessage')
37 ->with('element is invisible', 'error');
38 $form_error_handler->expects($this->at(5))
39 ->method('drupalSetMessage')
40 ->with('this missing element is invalid', 'error');
44 '#array_parents' => [],
47 '#type' => 'textfield',
49 '#parents' => ['test1'],
50 '#array_parents' => ['test1'],
51 '#id' => 'edit-test1',
54 '#type' => 'textfield',
55 '#title' => 'Test 2 & a half',
56 '#parents' => ['test2'],
57 '#array_parents' => ['test2'],
58 '#id' => 'edit-test2',
61 '#parents' => ['fieldset'],
62 '#array_parents' => ['fieldset'],
64 '#type' => 'textfield',
66 '#parents' => ['fieldset', 'test3'],
67 '#array_parents' => ['fieldset', 'test3'],
68 '#id' => 'edit-test3',
72 '#type' => 'textfield',
73 '#parents' => ['test5'],
74 '#array_parents' => ['test5'],
75 '#id' => 'edit-test5',
80 '#parents' => ['test6'],
81 '#array_parents' => ['test6'],
82 '#id' => 'edit-test6',
84 $form_state = new FormState();
85 $form_state->setErrorByName('test1', 'invalid');
86 $form_state->setErrorByName('test2', 'invalid');
87 $form_state->setErrorByName('fieldset][test3', 'invalid');
88 $form_state->setErrorByName('test5', 'no title given');
89 $form_state->setErrorByName('test6', 'element is invisible');
90 $form_state->setErrorByName('missing_element', 'this missing element is invalid');
91 $form_error_handler->handleFormErrors($form, $form_state);
92 $this->assertSame('invalid', $form['test1']['#errors']);
96 * @covers ::handleFormErrors
97 * @covers ::setElementErrorsFromFormState
99 public function testSetElementErrorsFromFormState() {
100 $form_error_handler = $this->getMockBuilder('Drupal\Core\Form\FormErrorHandler')
101 ->setMethods(['drupalSetMessage'])
106 '#array_parents' => [],
109 '#type' => 'textfield',
111 '#parents' => ['test'],
112 '#array_parents' => ['test'],
113 '#id' => 'edit-test',
116 '#type' => 'details',
117 '#title' => 'Details grouping test',
118 '#parents' => ['details'],
119 '#array_parents' => ['details'],
120 '#id' => 'edit-details',
122 $form['grouping_test'] = [
123 '#type' => 'textfield',
124 '#title' => 'Grouping test',
125 '#parents' => ['grouping_test'],
126 '#array_parents' => ['grouping_test'],
127 '#id' => 'edit-grouping-test',
128 '#group' => 'details',
130 $form['grouping_test2'] = [
131 '#type' => 'textfield',
132 '#title' => 'Grouping test 2',
133 '#parents' => ['grouping_test2'],
134 '#array_parents' => ['grouping_test2'],
135 '#id' => 'edit-grouping-test2',
136 '#group' => 'details',
138 $form['details2'] = [
139 '#type' => 'details',
140 '#title' => 'Details grouping test 2',
141 '#parents' => ['details2'],
142 '#array_parents' => ['details2'],
143 '#id' => 'edit-details2',
145 $form['grouping_test3'] = [
146 '#type' => 'textfield',
147 '#title' => 'Grouping test 3',
148 '#parents' => ['grouping_test3'],
149 '#array_parents' => ['grouping_test3'],
150 '#id' => 'edit-grouping-test3',
151 '#group' => 'details2',
153 $form['fieldset'] = [
154 '#type' => 'fieldset',
155 '#parents' => ['fieldset'],
156 '#array_parents' => ['fieldset'],
157 '#id' => 'edit-fieldset',
159 '#type' => 'textfield',
160 '#title' => 'Nested test',
161 '#parents' => ['fieldset', 'nested_test'],
162 '#array_parents' => ['fieldset', 'nested_test'],
163 '#id' => 'edit-nested_test',
166 '#type' => 'textfield',
167 '#title' => 'Nested test2',
168 '#parents' => ['fieldset', 'nested_test2'],
169 '#array_parents' => ['fieldset', 'nested_test2'],
170 '#id' => 'edit-nested_test2',
173 $form_state = new FormState();
174 $form_state->setErrorByName('test', 'invalid');
175 $form_state->setErrorByName('grouping_test', 'invalid');
176 $form_state->setErrorByName('grouping_test2', 'invalid');
177 $form_state->setErrorByName('fieldset][nested_test', 'invalid');
178 $form_state->setErrorByName('fieldset][nested_test2', 'invalid2');
179 $form_error_handler->handleFormErrors($form, $form_state);
180 $this->assertSame('invalid', $form['test']['#errors']);
182 'grouping_test' => 'invalid',
183 'grouping_test2' => 'invalid',
184 ], $form['details']['#children_errors']);
186 'fieldset][nested_test' => 'invalid',
187 'fieldset][nested_test2' => 'invalid2',
188 ], $form['fieldset']['#children_errors']);
189 $this->assertEmpty($form['details2']['#children_errors'], 'Children errors are empty for grouping element.');
190 $this->assertCount(5, $form['#children_errors']);