7d482a428f8dc3489c5f1dbe58b30cf3c854ef9d
[yaffs-website] / web / core / modules / system / tests / modules / form_test / src / Form / FormTestDisabledElementsForm.php
1 <?php
2
3 namespace Drupal\form_test\Form;
4
5 use Drupal\Core\Datetime\DrupalDateTime;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Symfony\Component\HttpFoundation\JsonResponse;
9
10 /**
11  * Builds a form to test disabled elements.
12  */
13 class FormTestDisabledElementsForm extends FormBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function getFormId() {
19     return '_form_test_disabled_elements';
20   }
21
22   /**
23    * {@inheritdoc}
24    */
25   public function buildForm(array $form, FormStateInterface $form_state) {
26     // Elements that take a simple default value.
27     foreach (['textfield', 'textarea', 'search', 'tel', 'hidden'] as $type) {
28       $form[$type] = [
29         '#type' => $type,
30         '#title' => $type,
31         '#default_value' => $type,
32         '#test_hijack_value' => 'HIJACK',
33         '#disabled' => TRUE,
34       ];
35     }
36
37     // Multiple values option elements.
38     foreach (['checkboxes', 'select'] as $type) {
39       $form[$type . '_multiple'] = [
40         '#type' => $type,
41         '#title' => $type . ' (multiple)',
42         '#options' => [
43           'test_1' => 'Test 1',
44           'test_2' => 'Test 2',
45         ],
46         '#multiple' => TRUE,
47         '#default_value' => ['test_2' => 'test_2'],
48         // The keys of #test_hijack_value need to match the #name of the control.
49         // @see FormsTestCase::testDisabledElements()
50         '#test_hijack_value' => $type == 'select' ? ['' => 'test_1'] : ['test_1' => 'test_1'],
51         '#disabled' => TRUE,
52       ];
53     }
54
55     // Single values option elements.
56     foreach (['radios', 'select'] as $type) {
57       $form[$type . '_single'] = [
58         '#type' => $type,
59         '#title' => $type . ' (single)',
60         '#options' => [
61           'test_1' => 'Test 1',
62           'test_2' => 'Test 2',
63         ],
64         '#multiple' => FALSE,
65         '#default_value' => 'test_2',
66         '#test_hijack_value' => 'test_1',
67         '#disabled' => TRUE,
68       ];
69     }
70
71     // Checkbox and radio.
72     foreach (['checkbox', 'radio'] as $type) {
73       $form[$type . '_unchecked'] = [
74         '#type' => $type,
75         '#title' => $type . ' (unchecked)',
76         '#return_value' => 1,
77         '#default_value' => 0,
78         '#test_hijack_value' => 1,
79         '#disabled' => TRUE,
80       ];
81       $form[$type . '_checked'] = [
82         '#type' => $type,
83         '#title' => $type . ' (checked)',
84         '#return_value' => 1,
85         '#default_value' => 1,
86         '#test_hijack_value' => NULL,
87         '#disabled' => TRUE,
88       ];
89     }
90
91     // Weight, number, range.
92     foreach (['weight', 'number', 'range'] as $type) {
93       $form[$type] = [
94         '#type' => $type,
95         '#title' => $type,
96         '#default_value' => 10,
97         '#test_hijack_value' => 5,
98         '#disabled' => TRUE,
99       ];
100     }
101
102     // Color.
103     $form['color'] = [
104       '#type' => 'color',
105       '#title' => 'color',
106       '#default_value' => '#0000ff',
107       '#test_hijack_value' => '#ff0000',
108       '#disabled' => TRUE,
109     ];
110
111     // The #disabled state should propagate to children.
112     $form['disabled_container'] = [
113       '#disabled' => TRUE,
114     ];
115     foreach (['textfield', 'textarea', 'hidden', 'tel', 'url'] as $type) {
116       $form['disabled_container']['disabled_container_' . $type] = [
117         '#type' => $type,
118         '#title' => $type,
119         '#default_value' => $type,
120         '#test_hijack_value' => 'HIJACK',
121       ];
122     }
123
124     // Date.
125     $date = new DrupalDateTime('1978-11-01 10:30:00', 'Europe/Berlin');
126     // Starting with PHP 5.4.30, 5.5.15, JSON encoded DateTime objects include
127     // microseconds. Make sure that the expected value is correct for all
128     // versions by encoding and decoding it again instead of hardcoding it.
129     // See https://github.com/php/php-src/commit/fdb2709dd27c5987c2d2c8aaf0cdbebf9f17f643
130     $expected = json_decode(json_encode($date), TRUE);
131     $form['disabled_container']['disabled_container_datetime'] = [
132       '#type' => 'datetime',
133       '#title' => 'datetime',
134       '#default_value' => $date,
135       '#expected_value' => $expected,
136       '#test_hijack_value' => new DrupalDateTime('1978-12-02 11:30:00', 'Europe/Berlin'),
137       '#date_timezone' => 'Europe/Berlin',
138     ];
139
140     $form['disabled_container']['disabled_container_date'] = [
141       '#type' => 'date',
142       '#title' => 'date',
143       '#default_value' => '2001-01-13',
144       '#expected_value' => '2001-01-13',
145       '#test_hijack_value' => '2013-01-01',
146       '#date_timezone' => 'Europe/Berlin',
147     ];
148
149     // Try to hijack the email field with a valid email.
150     $form['disabled_container']['disabled_container_email'] = [
151       '#type' => 'email',
152       '#title' => 'email',
153       '#default_value' => 'foo@example.com',
154       '#test_hijack_value' => 'bar@example.com',
155     ];
156
157     // Try to hijack the URL field with a valid URL.
158     $form['disabled_container']['disabled_container_url'] = [
159       '#type' => 'url',
160       '#title' => 'url',
161       '#default_value' => 'http://example.com',
162       '#test_hijack_value' => 'http://example.com/foo',
163     ];
164
165     // Text format.
166     $form['text_format'] = [
167       '#type' => 'text_format',
168       '#title' => 'Text format',
169       '#disabled' => TRUE,
170       '#default_value' => 'Text value',
171       '#format' => 'plain_text',
172       '#expected_value' => [
173         'value' => 'Text value',
174         'format' => 'plain_text',
175       ],
176       '#test_hijack_value' => [
177         'value' => 'HIJACK',
178         'format' => 'filtered_html',
179       ],
180     ];
181
182     // Password fields.
183     $form['password'] = [
184       '#type' => 'password',
185       '#title' => 'Password',
186       '#disabled' => TRUE,
187     ];
188     $form['password_confirm'] = [
189       '#type' => 'password_confirm',
190       '#title' => 'Password confirm',
191       '#disabled' => TRUE,
192     ];
193
194     // Files.
195     $form['file'] = [
196       '#type' => 'file',
197       '#title' => 'File',
198       '#disabled' => TRUE,
199     ];
200     $form['managed_file'] = [
201       '#type' => 'managed_file',
202       '#title' => 'Managed file',
203       '#disabled' => TRUE,
204     ];
205
206     // Buttons.
207     $form['image_button'] = [
208       '#type' => 'image_button',
209       '#value' => 'Image button',
210       '#disabled' => TRUE,
211     ];
212     $form['button'] = [
213       '#type' => 'button',
214       '#value' => 'Button',
215       '#disabled' => TRUE,
216     ];
217     $form['submit_disabled'] = [
218       '#type' => 'submit',
219       '#value' => 'Submit',
220       '#disabled' => TRUE,
221     ];
222
223     $form['submit'] = [
224       '#type' => 'submit',
225       '#value' => t('Submit'),
226     ];
227
228     return $form;
229   }
230
231   /**
232    * {@inheritdoc}
233    */
234   public function submitForm(array &$form, FormStateInterface $form_state) {
235     $form_state->setResponse(new JsonResponse($form_state->getValues()));
236   }
237
238 }