aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/htmlform/HTMLFormFieldTest.php
diff options
context:
space:
mode:
authormainframe98 <k.s.werf@hotmail.com>2023-06-10 13:00:27 +0200
committermainframe98 <k.s.werf@hotmail.com>2023-07-09 19:53:01 +0200
commitb6ded3b2c51885f2a6731af6f79d7dbedc6505fd (patch)
tree8f482923994e4c11fbd6bc2354bb2c4de14d77a0 /tests/phpunit/includes/htmlform/HTMLFormFieldTest.php
parent73fbd7bfaeec90080f317de1f931214e98b9474b (diff)
downloadmediawikicore-b6ded3b2c51885f2a6731af6f79d7dbedc6505fd.tar.gz
mediawikicore-b6ded3b2c51885f2a6731af6f79d7dbedc6505fd.zip
htmlform: Allow validation-callback to return Status instances
Convert this in HTMLFormField::validate to prevent callers from receiving unexpected Status instances that will be identified as an error result, even if the Status is good. Bug: T338677 Change-Id: If2208cca92c5c496eef73f25363221bc490d9c88
Diffstat (limited to 'tests/phpunit/includes/htmlform/HTMLFormFieldTest.php')
-rw-r--r--tests/phpunit/includes/htmlform/HTMLFormFieldTest.php117
1 files changed, 116 insertions, 1 deletions
diff --git a/tests/phpunit/includes/htmlform/HTMLFormFieldTest.php b/tests/phpunit/includes/htmlform/HTMLFormFieldTest.php
index bea3371d4cb1..fafb53287d10 100644
--- a/tests/phpunit/includes/htmlform/HTMLFormFieldTest.php
+++ b/tests/phpunit/includes/htmlform/HTMLFormFieldTest.php
@@ -11,7 +11,7 @@ class HTMLFormFieldTest extends PHPUnit\Framework\TestCase {
use MediaWikiCoversValidator;
- public function getNewForm( $descriptor, $requestData ) {
+ public function getNewForm( $descriptor, $requestData = [] ) {
$requestData += [ 'wpEditToken' => 'ABC123' ];
$request = new FauxRequest( $requestData, true );
$context = new DerivativeContext( RequestContext::getMain() );
@@ -445,4 +445,119 @@ class HTMLFormFieldTest extends PHPUnit\Framework\TestCase {
$this->assertArrayNotHasKey( 'notices', $configWithoutNotice );
}
+ /**
+ * @dataProvider provideCallables
+ */
+ public function testValidationCallbacks( callable $callable ) {
+ $field = new class( [
+ 'parent' => $this->getNewForm( [] ),
+ 'fieldname' => __FUNCTION__,
+ 'validation-callback' => $callable
+ ] ) extends HTMLFormField {
+ public function getInputHTML( $value ) {
+ return '';
+ }
+ };
+
+ $this->assertTrue( $field->validate( '', [] ) );
+ }
+
+ public static function provideCallables() {
+ $callable = new class() {
+ public function validate( $value, array $fields, HTMLForm $form ): bool {
+ return $value || $fields || $form->wasSubmitted();
+ }
+
+ public static function validateStatic( $value, array $fields, HTMLForm $form ): bool {
+ return $value || $fields || $form->wasSubmitted();
+ }
+
+ public function __invoke( ...$values ): bool {
+ return self::validateStatic( ...$values );
+ }
+ };
+
+ return [
+ 'Closure (short)' => [
+ static fn ( $value, array $fields, HTMLForm $form ) => $value || $fields || $form->wasSubmitted()
+ ],
+ 'Closure (traditional)' => [
+ static function ( $value, array $fields, HTMLForm $form ) {
+ return $value || $fields || $form->wasSubmitted();
+ }
+ ],
+ 'Array' => [ [ $callable, 'validate' ] ],
+ 'Array (static)' => [ [ get_class( $callable ), 'validateStatic' ] ],
+ 'String' => [ get_class( $callable ) . '::validateStatic' ],
+ 'Invokable' => [ $callable ]
+ ];
+ }
+
+ /**
+ * @dataProvider provideValidationResults
+ */
+ public function testValidationCallbackResults( $callbackResult, $expected ) {
+ $field = new class( [
+ 'parent' => $this->getNewForm( [] ),
+ 'fieldname' => __FUNCTION__,
+ 'validation-callback' => static fn () => $callbackResult
+ ] ) extends HTMLFormField {
+ public function getInputHTML( $value ) {
+ return '';
+ }
+ };
+
+ $this->assertEquals( $expected, $field->validate( '', [] ) );
+ }
+
+ public static function provideValidationResults() {
+ $ok = ( new Status() )
+ ->warning( 'test-warning' )
+ ->setOK( true );
+
+ return [
+ 'Ok Status' => [ $ok, "<p>⧼test-warning⧽\n</p>" ],
+ 'Good Status' => [ Status::newGood(), true ],
+ 'Fatal Status' => [ Status::newFatal( 'test-fatal' ), "<p>⧼test-fatal⧽\n</p>" ],
+ 'Good StatusValue' => [ StatusValue::newGood(), true ],
+ 'Fatal StatusValue' => [ Status::newFatal( 'test-fatal' ), "<p>⧼test-fatal⧽\n</p>" ],
+ 'String' => [ '<strong>Invalid input</strong>', '<strong>Invalid input</strong>' ],
+ 'True' => [ true, true ],
+ 'False' => [ false, false ]
+ ];
+ }
+
+ public function testValidationCallbackResultMessage() {
+ $message = $this->createMock( Message::class );
+
+ $this->testValidationCallbackResults( $message, $message );
+ }
+
+ /**
+ * @dataProvider provideValues
+ */
+ public function testValidateWithRequiredNotGiven( $value ) {
+ $field = new class( [
+ 'parent' => $this->getNewForm( [] ),
+ 'fieldname' => __FUNCTION__,
+ 'required' => true
+ ] ) extends HTMLFormField {
+ public function getInputHTML( $value ) {
+ return '';
+ }
+ };
+
+ $returnValue = $field->validate( $value, [ 'text' => $value ] );
+
+ $this->assertInstanceOf( Message::class, $returnValue );
+ $this->assertEquals( 'htmlform-required', $returnValue->getKey() );
+ }
+
+ public static function provideValues() {
+ return [
+ 'Empty string' => [ '' ],
+ 'False' => [ false ],
+ 'Null' => [ null ]
+ ];
+ }
}