2022-01-28 13:25:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
2022-10-28 10:04:25 +00:00
|
|
|
use MediaWiki\Request\FauxRequest;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-01-16 15:37:10 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
2022-01-28 13:25:57 +00:00
|
|
|
/**
|
|
|
|
|
* @covers HTMLFormField
|
|
|
|
|
*/
|
|
|
|
|
class HTMLFormFieldTest extends PHPUnit\Framework\TestCase {
|
|
|
|
|
|
|
|
|
|
use MediaWikiCoversValidator;
|
|
|
|
|
|
2022-01-16 15:37:10 +00:00
|
|
|
public function getNewForm( $descriptor, $requestData ) {
|
2022-02-09 12:08:26 +00:00
|
|
|
$requestData += [ 'wpEditToken' => 'ABC123' ];
|
2022-01-16 15:37:10 +00:00
|
|
|
$request = new FauxRequest( $requestData, true );
|
|
|
|
|
$context = new DerivativeContext( RequestContext::getMain() );
|
|
|
|
|
$context->setRequest( $request );
|
|
|
|
|
$form = HTMLForm::factory( 'ooui', $descriptor, $context );
|
2022-09-23 19:53:11 +00:00
|
|
|
$form->setTitle( Title::makeTitle( NS_MAIN, 'Main Page' ) )->setSubmitCallback( static function () {
|
2022-01-16 15:37:10 +00:00
|
|
|
return true;
|
|
|
|
|
} )->prepareForm();
|
|
|
|
|
$status = $form->trySubmit();
|
|
|
|
|
$this->assertTrue( $status );
|
|
|
|
|
return $form;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-28 13:25:57 +00:00
|
|
|
/**
|
|
|
|
|
* @covers HTMLFormField::isHidden
|
|
|
|
|
* @covers HTMLFormField::isDisabled
|
|
|
|
|
* @covers HTMLFormField::checkStateRecurse
|
2022-01-20 03:18:41 +00:00
|
|
|
* @covers HTMLFormField::validateCondState
|
2022-01-16 15:37:10 +00:00
|
|
|
* @covers HTMLFormField::getNearestField
|
|
|
|
|
* @covers HTMLFormField::getNearestFieldValue
|
2022-01-28 13:25:57 +00:00
|
|
|
* @dataProvider provideCondState
|
|
|
|
|
*/
|
2022-02-01 02:12:45 +00:00
|
|
|
public function testCondState( $fieldInfo, $requestData, $callback, $exception = null ) {
|
2022-01-28 13:25:57 +00:00
|
|
|
if ( $exception ) {
|
2022-01-16 15:37:10 +00:00
|
|
|
$this->expectException( $exception[0] );
|
|
|
|
|
$this->expectExceptionMessageMatches( $exception[1] );
|
2022-01-28 13:25:57 +00:00
|
|
|
}
|
2022-02-07 08:18:40 +00:00
|
|
|
$form = $this->getNewForm( array_merge_recursive( $fieldInfo, [
|
2022-01-28 13:25:57 +00:00
|
|
|
'check1' => [ 'type' => 'check' ],
|
|
|
|
|
'check2' => [ 'type' => 'check', 'invert' => true ],
|
2022-01-16 15:37:10 +00:00
|
|
|
'check3' => [ 'type' => 'check', 'name' => 'foo' ],
|
2022-02-01 02:12:45 +00:00
|
|
|
'select1' => [ 'type' => 'select', 'options' => [ 'a' => 'a', 'b' => 'b', 'c' => 'c' ], 'default' => 'b' ],
|
2022-01-28 13:25:57 +00:00
|
|
|
'text1' => [ 'type' => 'text' ],
|
2022-02-07 08:18:40 +00:00
|
|
|
'cloner' => [
|
|
|
|
|
'class' => HTMLFormFieldCloner::class,
|
|
|
|
|
'fields' => [
|
|
|
|
|
'check1' => [ 'type' => 'check' ],
|
|
|
|
|
'check2' => [ 'type' => 'check', 'invert' => true ],
|
|
|
|
|
'check3' => [ 'type' => 'check', 'name' => 'foo' ],
|
|
|
|
|
]
|
|
|
|
|
]
|
2022-01-16 15:37:10 +00:00
|
|
|
] ), $requestData );
|
2022-02-01 02:12:45 +00:00
|
|
|
$callback( $form, $form->mFieldData );
|
2022-01-28 13:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideCondState() {
|
|
|
|
|
yield 'Field hidden if "check" field is checked' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'text1' => [ 'hide-if' => [ '===', 'check1', '1' ] ],
|
|
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [
|
|
|
|
|
'wpcheck1' => '1',
|
2022-01-28 13:25:57 +00:00
|
|
|
],
|
|
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
yield 'Field hidden if "check" field is not checked' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'text1' => [ 'hide-if' => [ '===', 'check1', '' ] ],
|
|
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [],
|
2022-01-28 13:25:57 +00:00
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
yield 'Field not hidden if "check" field is not checked' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'text1' => [ 'hide-if' => [ '===', 'check1', '1' ] ],
|
|
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [],
|
2022-01-28 13:25:57 +00:00
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertFalse( $form->getField( 'text1' )->isHidden( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
yield 'Field hidden if "check" field (invert) is checked' => [
|
|
|
|
|
'fieldInfo' => [
|
2022-02-01 02:12:45 +00:00
|
|
|
'text1' => [ 'hide-if' => [ '===', 'check2', '1' ] ],
|
2022-01-28 13:25:57 +00:00
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [
|
|
|
|
|
'wpcheck2' => '1',
|
2022-01-28 13:25:57 +00:00
|
|
|
],
|
|
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
yield 'Field hidden if "check" field (invert) is not checked' => [
|
|
|
|
|
'fieldInfo' => [
|
2022-02-01 07:13:09 +00:00
|
|
|
'text1' => [ 'hide-if' => [ '!==', 'check2', '1' ] ],
|
2022-01-28 13:25:57 +00:00
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [],
|
2022-01-28 13:25:57 +00:00
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
yield 'Field not hidden if "check" field (invert) is checked' => [
|
|
|
|
|
'fieldInfo' => [
|
2022-02-01 02:12:45 +00:00
|
|
|
'text1' => [ 'hide-if' => [ '!==', 'check2', '1' ] ],
|
2022-01-28 13:25:57 +00:00
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [
|
|
|
|
|
'wpcheck2' => '1',
|
2022-01-28 13:25:57 +00:00
|
|
|
],
|
|
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertFalse( $form->getField( 'text1' )->isHidden( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
yield 'Field hidden if "select" field has value' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'text1' => [ 'hide-if' => [ '===', 'select1', 'a' ] ],
|
|
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [
|
|
|
|
|
'wpselect1' => 'a',
|
2022-01-28 13:25:57 +00:00
|
|
|
],
|
|
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
yield 'Field hidden if "text" field has value' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'select1' => [ 'hide-if' => [ '===', 'text1', 'hello' ] ],
|
|
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [
|
|
|
|
|
'wptext1' => 'hello',
|
2022-01-28 13:25:57 +00:00
|
|
|
],
|
|
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertTrue( $form->getField( 'select1' )->isHidden( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield 'Field hidden using AND conditions' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'text1' => [ 'hide-if' => [ 'AND',
|
|
|
|
|
[ '===', 'check1', '1' ],
|
|
|
|
|
[ '===', 'select1', 'a' ]
|
|
|
|
|
] ],
|
|
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [
|
|
|
|
|
'wpcheck1' => '1',
|
|
|
|
|
'wpselect1' => 'a',
|
2022-01-28 13:25:57 +00:00
|
|
|
],
|
|
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
yield 'Field hidden using OR conditions' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'text1' => [ 'hide-if' => [ 'OR',
|
|
|
|
|
[ '===', 'check1', '1' ],
|
|
|
|
|
[ '===', 'select1', 'a' ]
|
|
|
|
|
] ],
|
|
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [
|
|
|
|
|
'wpcheck1' => '1',
|
2022-01-28 13:25:57 +00:00
|
|
|
],
|
|
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
yield 'Field hidden using NAND conditions' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'text1' => [ 'hide-if' => [ 'NAND',
|
|
|
|
|
[ '===', 'check1', '1' ],
|
|
|
|
|
[ '===', 'select1', 'a' ]
|
|
|
|
|
] ],
|
|
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [
|
|
|
|
|
'wpcheck1' => '1',
|
2022-01-28 13:25:57 +00:00
|
|
|
],
|
|
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
yield 'Field hidden using NOR conditions' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'text1' => [ 'hide-if' => [ 'NOR',
|
|
|
|
|
[ '===', 'check1', '1' ],
|
|
|
|
|
[ '===', 'select1', 'a' ]
|
|
|
|
|
] ],
|
|
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [],
|
2022-01-28 13:25:57 +00:00
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
yield 'Field hidden using complex conditions' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'text1' => [ 'hide-if' => [ 'OR',
|
|
|
|
|
[ 'NOT', [ 'AND',
|
|
|
|
|
[ '===', 'check1', '1' ],
|
|
|
|
|
[ '===', 'check2', '1' ]
|
|
|
|
|
] ],
|
|
|
|
|
[ '===', 'select1', 'a' ]
|
|
|
|
|
] ],
|
|
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [],
|
2022-01-28 13:25:57 +00:00
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertTrue( $form->getField( 'text1' )->isHidden( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield 'Invalid conditional specification (unsupported)' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'text1' => [ 'hide-if' => [ '>', 'test1', '10' ] ],
|
|
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [],
|
2022-01-28 13:25:57 +00:00
|
|
|
'callback' => null,
|
2022-01-16 15:37:10 +00:00
|
|
|
'exception' => [ MWException::class, '/Unknown operation/' ],
|
2022-01-28 13:25:57 +00:00
|
|
|
];
|
|
|
|
|
yield 'Invalid conditional specification (NOT)' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'text1' => [ 'hide-if' => [ 'NOT', '===', 'check1', '1' ] ],
|
|
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [],
|
2022-01-28 13:25:57 +00:00
|
|
|
'callback' => null,
|
2022-01-16 15:37:10 +00:00
|
|
|
'exception' => [ MWException::class, '/NOT takes exactly one parameter/' ],
|
2022-01-28 13:25:57 +00:00
|
|
|
];
|
|
|
|
|
yield 'Invalid conditional specification (AND/OR/NAND/NOR)' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'text1' => [ 'hide-if' => [ 'AND', '===', 'check1', '1' ] ],
|
|
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [],
|
2022-01-28 13:25:57 +00:00
|
|
|
'callback' => null,
|
2022-01-16 15:37:10 +00:00
|
|
|
'exception' => [ MWException::class, '/Expected array, found string/' ],
|
2022-01-28 13:25:57 +00:00
|
|
|
];
|
2022-02-01 02:12:45 +00:00
|
|
|
yield 'Invalid conditional specification (===/!==) 1' => [
|
2022-01-28 13:25:57 +00:00
|
|
|
'fieldInfo' => [
|
|
|
|
|
'text1' => [ 'hide-if' => [ '===', 'check1' ] ],
|
|
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [],
|
2022-01-28 13:25:57 +00:00
|
|
|
'callback' => null,
|
2022-01-16 15:37:10 +00:00
|
|
|
'exception' => [ MWException::class, '/=== takes exactly two parameters/' ],
|
2022-01-28 13:25:57 +00:00
|
|
|
];
|
2022-02-01 02:12:45 +00:00
|
|
|
yield 'Invalid conditional specification (===/!==) 2' => [
|
2022-01-28 13:25:57 +00:00
|
|
|
'fieldInfo' => [
|
|
|
|
|
'text1' => [ 'hide-if' => [ '===', [ '===', 'check1', '1' ], '1' ] ],
|
|
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [],
|
2022-01-28 13:25:57 +00:00
|
|
|
'callback' => null,
|
2022-01-16 15:37:10 +00:00
|
|
|
'exception' => [ MWException::class, '/Parameters for === must be strings/' ],
|
2022-01-28 13:25:57 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield 'Field disabled if "check" field is checked' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'text1' => [ 'disable-if' => [ '===', 'check1', '1' ] ],
|
|
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [
|
|
|
|
|
'wpcheck1' => '1',
|
2022-01-28 13:25:57 +00:00
|
|
|
],
|
|
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertTrue( $form->getField( 'text1' )->isDisabled( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
yield 'Field disabled if hidden' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'text1' => [ 'hide-if' => [ '===', 'check1', '1' ] ],
|
|
|
|
|
],
|
2022-02-01 02:12:45 +00:00
|
|
|
'requestData' => [
|
|
|
|
|
'wpcheck1' => '1',
|
2022-01-28 13:25:57 +00:00
|
|
|
],
|
|
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertTrue( $form->getField( 'text1' )->isDisabled( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
2022-01-16 15:37:10 +00:00
|
|
|
|
|
|
|
|
yield 'Field disabled even the field it relied on is named' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'text1' => [ 'disable-if' => [ '===', 'check3', '1' ] ],
|
|
|
|
|
],
|
|
|
|
|
'requestData' => [
|
|
|
|
|
'foo' => '1',
|
|
|
|
|
],
|
|
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertTrue( $form->getField( 'text1' )->isDisabled( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
yield 'Field disabled even the \'wp\' prefix is used (back-compat)' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'text1' => [ 'disable-if' => [ '===', 'wpcheck1', '1' ] ],
|
|
|
|
|
],
|
|
|
|
|
'requestData' => [
|
|
|
|
|
'wpcheck1' => '1',
|
|
|
|
|
],
|
|
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertTrue( $form->getField( 'text1' )->isDisabled( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
yield 'Field name does not exist' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'text1' => [ 'disable-if' => [ '===', 'foo', '1' ] ],
|
|
|
|
|
],
|
|
|
|
|
'requestData' => [],
|
|
|
|
|
'callback' => null,
|
|
|
|
|
'exception' => [ DomainException::class, '/no field named foo/' ],
|
|
|
|
|
];
|
2022-02-07 08:18:40 +00:00
|
|
|
|
|
|
|
|
yield 'Field disabled in cloner if "check" field is checked' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'cloner' => [ 'fields' => [
|
|
|
|
|
'check2' => [ 'disable-if' => [ '===', 'check1', '1' ] ],
|
|
|
|
|
] ]
|
|
|
|
|
],
|
|
|
|
|
'requestData' => [
|
|
|
|
|
'wpcloner' => [ 0 => [ 'check1' => '1' ] ],
|
|
|
|
|
],
|
|
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertTrue( $this->getFieldInCloner( $form, 'cloner', 0, 'check2' )
|
|
|
|
|
->isDisabled( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
yield 'Field disabled in cloner if "check" (invert) field is checked' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'cloner' => [ 'fields' => [
|
|
|
|
|
'check1' => [ 'disable-if' => [ '===', 'check2', '1' ] ],
|
|
|
|
|
] ]
|
|
|
|
|
],
|
|
|
|
|
'requestData' => [
|
|
|
|
|
'wpcloner' => [ 0 => [ 'check2' => '1' ] ],
|
|
|
|
|
],
|
|
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertTrue( $this->getFieldInCloner( $form, 'cloner', 0, 'check1' )
|
|
|
|
|
->isDisabled( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
yield 'Field disabled in cloner if "check" (named) field is checked' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'cloner' => [ 'fields' => [
|
|
|
|
|
'check1' => [ 'disable-if' => [ '===', 'check3', '1' ] ],
|
|
|
|
|
] ]
|
|
|
|
|
],
|
|
|
|
|
'requestData' => [
|
|
|
|
|
'wpcloner' => [ 0 => [ 'foo' => '1' ] ],
|
|
|
|
|
],
|
|
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertTrue( $this->getFieldInCloner( $form, 'cloner', 0, 'check1' )
|
|
|
|
|
->isDisabled( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
yield 'Field disabled in cloner if "select" (outside) field has value' => [
|
|
|
|
|
'fieldInfo' => [
|
|
|
|
|
'cloner' => [ 'fields' => [
|
|
|
|
|
'check1' => [ 'disable-if' => [ '===', 'select1', 'a' ] ],
|
|
|
|
|
] ]
|
|
|
|
|
],
|
|
|
|
|
'requestData' => [
|
|
|
|
|
'wpselect1' => 'a',
|
|
|
|
|
],
|
|
|
|
|
'callback' => function ( $form, $fieldData ) {
|
|
|
|
|
$this->assertTrue( $this->getFieldInCloner( $form, 'cloner', 0, 'check1' )
|
|
|
|
|
->isDisabled( $fieldData ) );
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getFieldInCloner( $form, $clonerName, $index, $fieldName ) {
|
|
|
|
|
$cloner = TestingAccessWrapper::newFromObject( $form->getField( $clonerName ) );
|
|
|
|
|
return $cloner->getFieldsForKey( $index )[$fieldName];
|
2022-01-16 15:37:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers HTMLFormField::parseCondState
|
|
|
|
|
* @dataProvider provideParseCondState
|
|
|
|
|
*/
|
|
|
|
|
public function testParseCondState( $fieldName, $condState, $excepted ) {
|
|
|
|
|
$form = $this->getNewForm( [
|
|
|
|
|
'normal' => [ 'type' => 'check' ],
|
|
|
|
|
'named' => [ 'type' => 'check', 'name' => 'foo' ],
|
|
|
|
|
'test' => [ 'type' => 'text' ],
|
|
|
|
|
'cloner' => [
|
|
|
|
|
'class' => HTMLFormFieldCloner::class,
|
|
|
|
|
'fields' => [
|
|
|
|
|
'normal' => [ 'type' => 'check' ],
|
|
|
|
|
'named' => [ 'type' => 'check', 'name' => 'foo' ],
|
|
|
|
|
'test' => [ 'type' => 'text' ],
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
], [] );
|
|
|
|
|
$field = $form->getField( $fieldName ?? 'test' );
|
|
|
|
|
$wrapped = TestingAccessWrapper::newFromObject( $field );
|
|
|
|
|
if ( $field instanceof HTMLFormFieldCloner ) {
|
|
|
|
|
$field = $wrapped->getFieldsForKey( 0 )['test'];
|
|
|
|
|
$wrapped = TestingAccessWrapper::newFromObject( $field );
|
|
|
|
|
}
|
|
|
|
|
$parsed = $wrapped->parseCondState( $condState );
|
|
|
|
|
$this->assertSame( $excepted, $parsed );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideParseCondState() {
|
|
|
|
|
yield 'Normal' => [
|
|
|
|
|
null,
|
|
|
|
|
[ '===', 'normal', '1' ],
|
|
|
|
|
[ '===', 'wpnormal', '1' ],
|
|
|
|
|
];
|
|
|
|
|
yield 'With the \'wp\' prefix' => [
|
|
|
|
|
null,
|
|
|
|
|
[ '===', 'wpnormal', '1' ],
|
|
|
|
|
[ '===', 'wpnormal', '1' ],
|
|
|
|
|
];
|
|
|
|
|
yield 'Named' => [
|
|
|
|
|
null,
|
|
|
|
|
[ '===', 'named', '1' ],
|
|
|
|
|
[ '===', 'foo', '1' ],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield 'Normal in cloner' => [
|
|
|
|
|
'cloner',
|
|
|
|
|
[ '===', 'normal', '1' ],
|
|
|
|
|
[ '===', 'wpcloner[0][normal]', '1' ],
|
|
|
|
|
];
|
|
|
|
|
yield 'Named in cloner' => [
|
|
|
|
|
'cloner',
|
|
|
|
|
[ '===', 'named', '1' ],
|
|
|
|
|
[ '===', 'wpcloner[0][foo]', '1' ],
|
|
|
|
|
];
|
2022-01-28 13:25:57 +00:00
|
|
|
}
|
|
|
|
|
}
|