56e6d2ee0fb6e9c3a4795f0a1333af31239b5618
[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
150     // Try to hijack the email field with a valid email.
151     $form['disabled_container']['disabled_container_email'] = [
152       '#type' => 'email',
153       '#title' => 'email',
154       '#default_value' => 'foo@example.com',
155       '#test_hijack_value' => 'bar@example.com',
156     ];
157
158     // Try to hijack the URL field with a valid URL.
159     $form['disabled_container']['disabled_container_url'] = [
160       '#type' => 'url',
161       '#title' => 'url',
162       '#default_value' => 'http://example.com',
163       '#test_hijack_value' => 'http://example.com/foo',
164     ];
165
166     // Text format.
167     $form['text_format'] = [
168       '#type' => 'text_format',
169       '#title' => 'Text format',
170       '#disabled' => TRUE,
171       '#default_value' => 'Text value',
172       '#format' => 'plain_text',
173       '#expected_value' => [
174         'value' => 'Text value',
175         'format' => 'plain_text',
176       ],
177       '#test_hijack_value' => [
178         'value' => 'HIJACK',
179         'format' => 'filtered_html',
180       ],
181     ];
182
183     // Password fields.
184     $form['password'] = [
185       '#type' => 'password',
186       '#title' => 'Password',
187       '#disabled' => TRUE,
188     ];
189     $form['password_confirm'] = [
190       '#type' => 'password_confirm',
191       '#title' => 'Password confirm',
192       '#disabled' => TRUE,
193     ];
194
195     // Files.
196     $form['file'] = [
197       '#type' => 'file',
198       '#title' => 'File',
199       '#disabled' => TRUE,
200     ];
201     $form['managed_file'] = [
202       '#type' => 'managed_file',
203       '#title' => 'Managed file',
204       '#disabled' => TRUE,
205     ];
206
207     // Buttons.
208     $form['image_button'] = [
209       '#type' => 'image_button',
210       '#value' => 'Image button',
211       '#disabled' => TRUE,
212     ];
213     $form['button'] = [
214       '#type' => 'button',
215       '#value' => 'Button',
216       '#disabled' => TRUE,
217     ];
218     $form['submit_disabled'] = [
219       '#type' => 'submit',
220       '#value' => 'Submit',
221       '#disabled' => TRUE,
222     ];
223
224     $form['submit'] = [
225       '#type' => 'submit',
226       '#value' => t('Submit'),
227     ];
228
229     return $form;
230   }
231
232   /**
233    * {@inheritdoc}
234    */
235   public function submitForm(array &$form, FormStateInterface $form_state) {
236     $form_state->setResponse(new JsonResponse($form_state->getValues()));
237   }
238
239 }