Debian Codegraph @

tests/phpunit/integration/includes/HTMLForm/HTMLFormFieldTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Integration\HTMLForm;
4
5 use DomainException;
6 use InvalidArgumentException;
7 use MediaWiki\Context\DerivativeContext;
8 use MediaWiki\Context\RequestContext;
9 use MediaWiki\HTMLForm\Field\HTMLFormFieldCloner;
10 use MediaWiki\HTMLForm\HTMLForm;
11 use MediaWiki\HTMLForm\HTMLFormField;
12 use MediaWiki\Message\Message;
13 use MediaWiki\Request\FauxRequest;
14 use MediaWiki\Status\Status;
15 use MediaWiki\Title\Title;
16 use MediaWikiCoversValidator;
17 use MediaWikiIntegrationTestCase;
18 use PHPUnit\Framework\Assert;
19 use StatusValue;
20 use Wikimedia\TestingAccessWrapper;
21
22 /**
23 * @covers \MediaWiki\HTMLForm\HTMLFormField
24 * @covers \MediaWiki\HTMLForm\Field\HTMLFormFieldCloner
25 */
26 class HTMLFormFieldTest extends MediaWikiIntegrationTestCase {
27 use MediaWikiCoversValidator;
28
29 public function getNewForm( $descriptor, $requestData = [] ) {
30 $requestData += [ 'wpEditToken' => 'ABC123' ];
31 $request = new FauxRequest( $requestData, true );
32 $context = new DerivativeContext( RequestContext::getMain() );
33 $context->setRequest( $request );
34 $form = HTMLForm::factory( 'ooui', $descriptor, $context );
35 $form->setTitle( Title::makeTitle( NS_MAIN, 'Main Page' ) )->setSubmitCallback( static function () {
36 return true;
37 } )->prepareForm();
38 $status = $form->trySubmit();
39 $this->assertTrue( $status );
40 // HTMLFormFieldCloner would create special template fields for JS users,
41 // make sure no warnings come from there.
42 $form->getHTML( $status );
43 return $form;
44 }
45
46 /**
47 * @dataProvider provideCondState
48 */
49 public function testCondState( $fieldInfo, $requestData, $callback, $exception = null ) {
50 if ( $exception ) {
51 $this->expectException( $exception[0] );
52 $this->expectExceptionMessageMatches( $exception[1] );
53 }
54 $form = $this->getNewForm( array_merge_recursive( $fieldInfo, [
55 'check1' => [ 'type' => 'check' ],
56 'check2' => [ 'type' => 'check', 'invert' => true ],
57 'check3' => [ 'type' => 'check', 'name' => 'foo' ],
58 'select1' => [ 'type' => 'select', 'options' => [ 'a' => 'a', 'b' => 'b', 'c' => 'c' ], 'default' => 'b' ],
59 'text1' => [ 'type' => 'text' ],
60 'cloner' => [
61 'class' => HTMLFormFieldCloner::class,
62 'fields' => [
63 'check1' => [ 'type' => 'check' ],
64 'check2' => [ 'type' => 'check', 'invert' => true ],
65 'check3' => [ 'type' => 'check', 'name' => 'foo' ],
66 ]
67 ]
68 ] ), $requestData );
69 $callback( $form, $form->mFieldData );
70 }
71
72 public function provideCondState() {
73 yield 'Field hidden if "check" field is checked' => [
74 'fieldInfo' => [
75 'text1' => [ 'hide-if' => [ '===', 'check1', '1' ] ],
76 ],
77 'requestData' => [
78 'wpcheck1' => '1',
79 ],
80 'callback' => function ( $form, $fieldData ) {
81 $this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
82 }
83 ];
84 yield 'Field hidden if "check" field is not checked' => [
85 'fieldInfo' => [
86 'text1' => [ 'hide-if' => [ '===', 'check1', '' ] ],
87 ],
88 'requestData' => [],
89 'callback' => function ( $form, $fieldData ) {
90 $this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
91 }
92 ];
93 yield 'Field not hidden if "check" field is not checked' => [
94 'fieldInfo' => [
95 'text1' => [ 'hide-if' => [ '===', 'check1', '1' ] ],
96 ],
97 'requestData' => [],
98 'callback' => function ( $form, $fieldData ) {
99 $this->assertFalse( $form->getField( 'text1' )->isHidden( $fieldData ) );
100 }
101 ];
102 yield 'Field hidden if "check" field (invert) is checked' => [
103 'fieldInfo' => [
104 'text1' => [ 'hide-if' => [ '===', 'check2', '1' ] ],
105 ],
106 'requestData' => [
107 'wpcheck2' => '1',
108 ],
109 'callback' => function ( $form, $fieldData ) {
110 $this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
111 }
112 ];
113 yield 'Field hidden if "check" field (invert) is not checked' => [
114 'fieldInfo' => [
115 'text1' => [ 'hide-if' => [ '!==', 'check2', '1' ] ],
116 ],
117 'requestData' => [],
118 'callback' => function ( $form, $fieldData ) {
119 $this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
120 }
121 ];
122 yield 'Field not hidden if "check" field (invert) is checked' => [
123 'fieldInfo' => [
124 'text1' => [ 'hide-if' => [ '!==', 'check2', '1' ] ],
125 ],
126 'requestData' => [
127 'wpcheck2' => '1',
128 ],
129 'callback' => function ( $form, $fieldData ) {
130 $this->assertFalse( $form->getField( 'text1' )->isHidden( $fieldData ) );
131 }
132 ];
133 yield 'Field hidden if "select" field has value' => [
134 'fieldInfo' => [
135 'text1' => [ 'hide-if' => [ '===', 'select1', 'a' ] ],
136 ],
137 'requestData' => [
138 'wpselect1' => 'a',
139 ],
140 'callback' => function ( $form, $fieldData ) {
141 $this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
142 }
143 ];
144 yield 'Field hidden if "text" field has value' => [
145 'fieldInfo' => [
146 'select1' => [ 'hide-if' => [ '===', 'text1', 'hello' ] ],
147 ],
148 'requestData' => [
149 'wptext1' => 'hello',
150 ],
151 'callback' => function ( $form, $fieldData ) {
152 $this->assertTrue( $form->getField( 'select1' )->isHidden( $fieldData ) );
153 }
154 ];
155
156 yield 'Field hidden using AND conditions' => [
157 'fieldInfo' => [
158 'text1' => [ 'hide-if' => [ 'AND',
159 [ '===', 'check1', '1' ],
160 [ '===', 'select1', 'a' ]
161 ] ],
162 ],
163 'requestData' => [
164 'wpcheck1' => '1',
165 'wpselect1' => 'a',
166 ],
167 'callback' => function ( $form, $fieldData ) {
168 $this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
169 }
170 ];
171 yield 'Field hidden using OR conditions' => [
172 'fieldInfo' => [
173 'text1' => [ 'hide-if' => [ 'OR',
174 [ '===', 'check1', '1' ],
175 [ '===', 'select1', 'a' ]
176 ] ],
177 ],
178 'requestData' => [
179 'wpcheck1' => '1',
180 ],
181 'callback' => function ( $form, $fieldData ) {
182 $this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
183 }
184 ];
185 yield 'Field hidden using NAND conditions' => [
186 'fieldInfo' => [
187 'text1' => [ 'hide-if' => [ 'NAND',
188 [ '===', 'check1', '1' ],
189 [ '===', 'select1', 'a' ]
190 ] ],
191 ],
192 'requestData' => [
193 'wpcheck1' => '1',
194 ],
195 'callback' => function ( $form, $fieldData ) {
196 $this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
197 }
198 ];
199 yield 'Field hidden using NOR conditions' => [
200 'fieldInfo' => [
201 'text1' => [ 'hide-if' => [ 'NOR',
202 [ '===', 'check1', '1' ],
203 [ '===', 'select1', 'a' ]
204 ] ],
205 ],
206 'requestData' => [],
207 'callback' => function ( $form, $fieldData ) {
208 $this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
209 }
210 ];
211 yield 'Field hidden using complex conditions' => [
212 'fieldInfo' => [
213 'text1' => [ 'hide-if' => [ 'OR',
214 [ 'NOT', [ 'AND',
215 [ '===', 'check1', '1' ],
216 [ '===', 'check2', '1' ]
217 ] ],
218 [ '===', 'select1', 'a' ]
219 ] ],
220 ],
221 'requestData' => [],
222 'callback' => function ( $form, $fieldData ) {
223 $this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
224 }
225 ];
226
227 yield 'Invalid conditional specification (unsupported)' => [
228 'fieldInfo' => [
229 'text1' => [ 'hide-if' => [ '>', 'test1', '10' ] ],
230 ],
231 'requestData' => [],
232 'callback' => null,
233 'exception' => [ InvalidArgumentException::class, '/Unknown operation/' ],
234 ];
235 yield 'Invalid conditional specification (NOT)' => [
236 'fieldInfo' => [
237 'text1' => [ 'hide-if' => [ 'NOT', '===', 'check1', '1' ] ],
238 ],
239 'requestData' => [],
240 'callback' => null,
241 'exception' => [ InvalidArgumentException::class, '/NOT takes exactly one parameter/' ],
242 ];
243 yield 'Invalid conditional specification (AND/OR/NAND/NOR)' => [
244 'fieldInfo' => [
245 'text1' => [ 'hide-if' => [ 'AND', '===', 'check1', '1' ] ],
246 ],
247 'requestData' => [],
248 'callback' => null,
249 'exception' => [ InvalidArgumentException::class, '/Expected array, found string/' ],
250 ];
251 yield 'Invalid conditional specification (===/!==) 1' => [
252 'fieldInfo' => [
253 'text1' => [ 'hide-if' => [ '===', 'check1' ] ],
254 ],
255 'requestData' => [],
256 'callback' => null,
257 'exception' => [ InvalidArgumentException::class, '/=== takes exactly two parameters/' ],
258 ];
259 yield 'Invalid conditional specification (===/!==) 2' => [
260 'fieldInfo' => [
261 'text1' => [ 'hide-if' => [ '===', [ '===', 'check1', '1' ], '1' ] ],
262 ],
263 'requestData' => [],
264 'callback' => null,
265 'exception' => [ InvalidArgumentException::class, '/Parameters for === must be strings/' ],
266 ];
267
268 yield 'Field disabled if "check" field is checked' => [
269 'fieldInfo' => [
270 'text1' => [ 'disable-if' => [ '===', 'check1', '1' ] ],
271 ],
272 'requestData' => [
273 'wpcheck1' => '1',
274 ],
275 'callback' => function ( $form, $fieldData ) {
276 $this->assertTrue( $form->getField( 'text1' )->isDisabled( $fieldData ) );
277 }
278 ];
279 yield 'Field disabled if hidden' => [
280 'fieldInfo' => [
281 'text1' => [ 'hide-if' => [ '===', 'check1', '1' ] ],
282 ],
283 'requestData' => [
284 'wpcheck1' => '1',
285 ],
286 'callback' => function ( $form, $fieldData ) {
287 $this->assertTrue( $form->getField( 'text1' )->isDisabled( $fieldData ) );
288 }
289 ];
290
291 yield 'Field disabled even the field it relied on is named' => [
292 'fieldInfo' => [
293 'text1' => [ 'disable-if' => [ '===', 'check3', '1' ] ],
294 ],
295 'requestData' => [
296 'foo' => '1',
297 ],
298 'callback' => function ( $form, $fieldData ) {
299 $this->assertTrue( $form->getField( 'text1' )->isDisabled( $fieldData ) );
300 }
301 ];
302 yield 'Field disabled even the \'wp\' prefix is used (back-compat)' => [
303 'fieldInfo' => [
304 'text1' => [ 'disable-if' => [ '===', 'wpcheck1', '1' ] ],
305 ],
306 'requestData' => [
307 'wpcheck1' => '1',
308 ],
309 'callback' => function ( $form, $fieldData ) {
310 $this->assertTrue( $form->getField( 'text1' )->isDisabled( $fieldData ) );
311 }
312 ];
313 yield 'Field name does not exist' => [
314 'fieldInfo' => [
315 'text1' => [ 'disable-if' => [ '===', 'foo', '1' ] ],
316 ],
317 'requestData' => [],
318 'callback' => null,
319 'exception' => [ DomainException::class, '/no field named foo/' ],
320 ];
321
322 yield 'Field disabled in cloner if "check" field is checked' => [
323 'fieldInfo' => [
324 'cloner' => [ 'fields' => [
325 'check2' => [ 'disable-if' => [ '===', 'check1', '1' ] ],
326 ] ]
327 ],
328 'requestData' => [
329 'wpcloner' => [ 0 => [ 'check1' => '1' ] ],
330 ],
331 'callback' => function ( $form, $fieldData ) {
332 $this->assertTrue( $this->getFieldInCloner( $form, 'cloner', 0, 'check2' )
333 ->isDisabled( $fieldData ) );
334 }
335 ];
336 yield 'Field disabled in cloner if "check" (invert) field is checked' => [
337 'fieldInfo' => [
338 'cloner' => [ 'fields' => [
339 'check1' => [ 'disable-if' => [ '===', 'check2', '1' ] ],
340 ] ]
341 ],
342 'requestData' => [
343 'wpcloner' => [ 0 => [ 'check2' => '1' ] ],
344 ],
345 'callback' => function ( $form, $fieldData ) {
346 $this->assertTrue( $this->getFieldInCloner( $form, 'cloner', 0, 'check1' )
347 ->isDisabled( $fieldData ) );
348 }
349 ];
350 yield 'Field disabled in cloner if "check" (named) field is checked' => [
351 'fieldInfo' => [
352 'cloner' => [ 'fields' => [
353 'check1' => [ 'disable-if' => [ '===', 'check3', '1' ] ],
354 ] ]
355 ],
356 'requestData' => [
357 'wpcloner' => [ 0 => [ 'foo' => '1' ] ],
358 ],
359 'callback' => function ( $form, $fieldData ) {
360 $this->assertTrue( $this->getFieldInCloner( $form, 'cloner', 0, 'check1' )
361 ->isDisabled( $fieldData ) );
362 }
363 ];
364 yield 'Field disabled in cloner if "select" (outside) field has value' => [
365 'fieldInfo' => [
366 'cloner' => [ 'fields' => [
367 'check1' => [ 'disable-if' => [ '===', 'select1', 'a' ] ],
368 ] ]
369 ],
370 'requestData' => [
371 'wpselect1' => 'a',
372 ],
373 'callback' => function ( $form, $fieldData ) {
374 $this->assertTrue( $this->getFieldInCloner( $form, 'cloner', 0, 'check1' )
375 ->isDisabled( $fieldData ) );
376 }
377 ];
378 yield 'Reset "check1" to default, chained dependency works' => [
379 'fieldInfo' => [
380 'check1' => [ 'hide-if' => [ '===', 'select1', 'a' ] ],
381 'text1' => [ 'hide-if' => [ '===', 'check1', '' ] ],
382 ],
383 'requestData' => [
384 'wpselect1' => 'a',
385 'wpcheck1' => '1',
386 ],
387 'callback' => static function ( $form, $fieldData ) {
388 Assert::assertTrue( $form->getField( 'check1' )->isDisabled( $fieldData ),
389 'Assert check1 in is disabled' );
390 Assert::assertTrue( $form->getField( 'text1' )->isDisabled( $fieldData ),
391 'Assert text1 is disabled' );
392 }
393 ];
394 yield 'Reset "check1" in cloner to default, chained dependency works' => [
395 'fieldInfo' => [
396 'cloner' => [ 'fields' => [
397 'check1' => [ 'hide-if' => [ '===', 'select1', 'a' ] ],
398 'check2' => [ 'hide-if' => [ '===', 'check1', '' ] ],
399 ] ]
400 ],
401 'requestData' => [
402 'wpselect1' => 'a',
403 'wpcloner' => [ 0 => [ 'check1' => '1' ] ],
404 ],
405 'callback' => function ( $form, $fieldData ) {
406 Assert::assertTrue( $this->getFieldInCloner( $form, 'cloner', 0, 'check1' )
407 ->isHidden( $fieldData ), 'Assert check1 in cloner is hidden' );
408 Assert::assertTrue( $this->getFieldInCloner( $form, 'cloner', 0, 'check2' )
409 ->isHidden( $fieldData ), 'Assert check2 in cloner is hidden' );
410 }
411 ];
412 }
413
414 private function getFieldInCloner( $form, $clonerName, $index, $fieldName ) {
415 $cloner = TestingAccessWrapper::newFromObject( $form->getField( $clonerName ) );
416 return $cloner->getFieldsForKey( $index )[$fieldName];
417 }
418
419 /**
420 * @dataProvider provideParseCondState
421 */
422 public function testParseCondState( $fieldName, $condState, $excepted ) {
423 $form = $this->getNewForm( [
424 'normal' => [ 'type' => 'check' ],
425 'named' => [ 'type' => 'check', 'name' => 'foo' ],
426 'test' => [ 'type' => 'text' ],
427 'cloner' => [
428 'class' => HTMLFormFieldCloner::class,
429 'fields' => [
430 'normal' => [ 'type' => 'check' ],
431 'named' => [ 'type' => 'check', 'name' => 'foo' ],
432 'test' => [ 'type' => 'text' ],
433 ]
434 ]
435 ], [] );
436 $field = $form->getField( $fieldName ?? 'test' );
437 $wrapped = TestingAccessWrapper::newFromObject( $field );
438 if ( $field instanceof HTMLFormFieldCloner ) {
439 $field = $wrapped->getFieldsForKey( 0 )['test'];
440 $wrapped = TestingAccessWrapper::newFromObject( $field );
441 }
442 $parsed = $wrapped->parseCondState( $condState );
443 $this->assertSame( $excepted, $parsed );
444 }
445
446 public static function provideParseCondState() {
447 yield 'Normal' => [
448 null,
449 [ '===', 'normal', '1' ],
450 [ '===', 'wpnormal', '1' ],
451 ];
452 yield 'With the \'wp\' prefix' => [
453 null,
454 [ '===', 'wpnormal', '1' ],
455 [ '===', 'wpnormal', '1' ],
456 ];
457 yield 'Named' => [
458 null,
459 [ '===', 'named', '1' ],
460 [ '===', 'foo', '1' ],
461 ];
462
463 yield 'Normal in cloner' => [
464 'cloner',
465 [ '===', 'normal', '1' ],
466 [ '===', 'wpcloner[0][normal]', '1' ],
467 ];
468 yield 'Named in cloner' => [
469 'cloner',
470 [ '===', 'named', '1' ],
471 [ '===', 'wpcloner[0][foo]', '1' ],
472 ];
473 }
474
475 public function testNoticeInfo() {
476 $form = $this->getNewForm( [
477 'withNotice' => [ 'type' => 'check', 'notices' => [ 'a notice' ] ],
478 'withoutNotice' => [ 'type' => 'check' ],
479 ], [] );
480
481 $configWithNotice = $configWithoutNotice = [];
482 $form->getField( 'withNotice' )->getOOUI( '' )->getConfig( $configWithNotice );
483 $form->getField( 'withoutNotice' )->getOOUI( '' )->getConfig( $configWithoutNotice );
484
485 $this->assertArrayHasKey( 'notices', $configWithNotice );
486 $this->assertSame(
487 [ 'a notice' ],
488 $configWithNotice['notices']
489 );
490 $this->assertArrayNotHasKey( 'notices', $configWithoutNotice );
491 }
492
493 /**
494 * @dataProvider provideCallables
495 */
496 public function testValidationCallbacks( callable $callable ) {
497 $field = new class( [
498 'parent' => $this->getNewForm( [] ),
499 'fieldname' => __FUNCTION__,
500 'validation-callback' => $callable
501 ] ) extends HTMLFormField {
502 public function getInputHTML( $value ) {
503 return '';
504 }
505 };
506
507 $this->assertTrue( $field->validate( '', [] ) );
508 }
509
510 public static function provideCallables() {
511 $callable = new class() {
512 public function validate( $value, array $fields, HTMLForm $form ): bool {
513 return $value || $fields || $form->wasSubmitted();
514 }
515
516 public static function validateStatic( $value, array $fields, HTMLForm $form ): bool {
517 return $value || $fields || $form->wasSubmitted();
518 }
519
520 public function __invoke( ...$values ): bool {
521 return self::validateStatic( ...$values );
522 }
523 };
524
525 return [
526 'Closure (short)' => [
527 static fn ( $value, array $fields, HTMLForm $form ) => $value || $fields || $form->wasSubmitted()
528 ],
529 'Closure (traditional)' => [
530 static function ( $value, array $fields, HTMLForm $form ) {
531 return $value || $fields || $form->wasSubmitted();
532 }
533 ],
534 'Array' => [ [ $callable, 'validate' ] ],
535 'Array (static)' => [ [ get_class( $callable ), 'validateStatic' ] ],
536 'String' => [ get_class( $callable ) . '::validateStatic' ],
537 'Invokable' => [ $callable ]
538 ];
539 }
540
541 /**
542 * @dataProvider provideValidationResults
543 */
544 public function testValidationCallbackResults( $callbackResult, $expected ) {
545 $field = new class( [
546 'parent' => $this->getNewForm( [] ),
547 'fieldname' => __FUNCTION__,
548 'validation-callback' => static fn () => $callbackResult
549 ] ) extends HTMLFormField {
550 public function getInputHTML( $value ) {
551 return '';
552 }
553 };
554
555 $this->assertEquals( $expected, $field->validate( '', [] ) );
556 }
557
558 public static function provideValidationResults() {
559 $ok = ( new Status() )
560 ->warning( 'test-warning' )
561 ->setOK( true );
562
563 return [
564 'Ok Status' => [ $ok, "<p>⧼test-warning⧽\n</p>" ],
565 'Good Status' => [ Status::newGood(), true ],
566 'Fatal Status' => [ Status::newFatal( 'test-fatal' ), "<p>⧼test-fatal⧽\n</p>" ],
567 'Good StatusValue' => [ StatusValue::newGood(), true ],
568 'Fatal StatusValue' => [ Status::newFatal( 'test-fatal' ), "<p>⧼test-fatal⧽\n</p>" ],
569 'String' => [ '<strong>Invalid input</strong>', '<strong>Invalid input</strong>' ],
570 'True' => [ true, true ],
571 'False' => [ false, false ]
572 ];
573 }
574
575 public function testValidationCallbackResultMessage() {
576 $message = $this->createMock( Message::class );
577
578 $this->testValidationCallbackResults( $message, $message );
579 }
580
581 /**
582 * @dataProvider provideValues
583 */
584 public function testValidateWithRequiredNotGiven( $value ) {
585 $field = new class( [
586 'parent' => $this->getNewForm( [] ),
587 'fieldname' => __FUNCTION__,
588 'required' => true
589 ] ) extends HTMLFormField {
590 public function getInputHTML( $value ) {
591 return '';
592 }
593 };
594
595 $returnValue = $field->validate( $value, [ 'text' => $value ] );
596
597 $this->assertInstanceOf( Message::class, $returnValue );
598 $this->assertEquals( 'htmlform-required', $returnValue->getKey() );
599 }
600
601 public static function provideValues() {
602 return [
603 'Empty string' => [ '' ],
604 'False' => [ false ],
605 'Null' => [ null ]
606 ];
607 }
608 }