With the introduction of a REST API into MediaWiki core, we're going to want to share parameter validation logic rather than having similar code in both the Action API and the REST API. This abstracts out parameter validation logic as a library. There will be at least two follow-up patches: * One to add calls in the REST API, plus the interface for the REST API to do body validation. Should be reasonably straightforward. * One to adjust the Action API to use this. That'll be much less straightforward, as the Action API needs some MediaWiki-specific types (which the REST API might use too in the future) and needs to override the defaults on some of the library's checks (to maintain back-compat). Bug: T142080 Bug: T223239 Change-Id: I5c0cc3a8d686ace97596df5832c450a6a50f902c Depends-On: Iea05dc439688871c574c639e617765ae88a75ff7
88 lines
3.1 KiB
PHP
88 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Wikimedia\ParamValidator;
|
|
|
|
use Psr\Http\Message\UploadedFileInterface;
|
|
|
|
/**
|
|
* @covers Wikimedia\ParamValidator\SimpleCallbacks
|
|
*/
|
|
class SimpleCallbacksTest extends \PHPUnit\Framework\TestCase {
|
|
|
|
public function testDataAccess() {
|
|
$callbacks = new SimpleCallbacks(
|
|
[ 'foo' => 'Foo!', 'bar' => null ],
|
|
[
|
|
'file1' => [
|
|
'name' => 'example.txt',
|
|
'type' => 'text/plain',
|
|
'tmp_name' => '...',
|
|
'error' => UPLOAD_ERR_OK,
|
|
'size' => 123,
|
|
],
|
|
'file2' => [
|
|
'name' => '',
|
|
'type' => '',
|
|
'tmp_name' => '',
|
|
'error' => UPLOAD_ERR_NO_FILE,
|
|
'size' => 0,
|
|
],
|
|
]
|
|
);
|
|
|
|
$this->assertTrue( $callbacks->hasParam( 'foo', [] ) );
|
|
$this->assertFalse( $callbacks->hasParam( 'bar', [] ) );
|
|
$this->assertFalse( $callbacks->hasParam( 'baz', [] ) );
|
|
$this->assertFalse( $callbacks->hasParam( 'file1', [] ) );
|
|
|
|
$this->assertSame( 'Foo!', $callbacks->getValue( 'foo', null, [] ) );
|
|
$this->assertSame( null, $callbacks->getValue( 'bar', null, [] ) );
|
|
$this->assertSame( 123, $callbacks->getValue( 'bar', 123, [] ) );
|
|
$this->assertSame( null, $callbacks->getValue( 'baz', null, [] ) );
|
|
$this->assertSame( null, $callbacks->getValue( 'file1', null, [] ) );
|
|
|
|
$this->assertFalse( $callbacks->hasUpload( 'foo', [] ) );
|
|
$this->assertFalse( $callbacks->hasUpload( 'bar', [] ) );
|
|
$this->assertTrue( $callbacks->hasUpload( 'file1', [] ) );
|
|
$this->assertTrue( $callbacks->hasUpload( 'file2', [] ) );
|
|
$this->assertFalse( $callbacks->hasUpload( 'baz', [] ) );
|
|
|
|
$this->assertNull( $callbacks->getUploadedFile( 'foo', [] ) );
|
|
$this->assertNull( $callbacks->getUploadedFile( 'bar', [] ) );
|
|
$this->assertInstanceOf(
|
|
UploadedFileInterface::class, $callbacks->getUploadedFile( 'file1', [] )
|
|
);
|
|
$this->assertInstanceOf(
|
|
UploadedFileInterface::class, $callbacks->getUploadedFile( 'file2', [] )
|
|
);
|
|
$this->assertNull( $callbacks->getUploadedFile( 'baz', [] ) );
|
|
|
|
$file = $callbacks->getUploadedFile( 'file1', [] );
|
|
$this->assertSame( 'example.txt', $file->getClientFilename() );
|
|
$file = $callbacks->getUploadedFile( 'file2', [] );
|
|
$this->assertSame( UPLOAD_ERR_NO_FILE, $file->getError() );
|
|
|
|
$this->assertFalse( $callbacks->useHighLimits( [] ) );
|
|
$this->assertFalse( $callbacks->useHighLimits( [ 'useHighLimits' => false ] ) );
|
|
$this->assertTrue( $callbacks->useHighLimits( [ 'useHighLimits' => true ] ) );
|
|
}
|
|
|
|
public function testRecording() {
|
|
$callbacks = new SimpleCallbacks( [] );
|
|
|
|
$this->assertSame( [], $callbacks->getRecordedConditions() );
|
|
|
|
$ex1 = new ValidationException( 'foo', 'Foo!', [], 'foo', [] );
|
|
$callbacks->recordCondition( $ex1, [] );
|
|
$ex2 = new ValidationException( 'bar', null, [], 'barbar', [ 'bAr' => 'BaR' ] );
|
|
$callbacks->recordCondition( $ex2, [] );
|
|
$callbacks->recordCondition( $ex2, [] );
|
|
$this->assertSame( [ $ex1, $ex2, $ex2 ], $callbacks->getRecordedConditions() );
|
|
|
|
$callbacks->clearRecordedConditions();
|
|
$this->assertSame( [], $callbacks->getRecordedConditions() );
|
|
$callbacks->recordCondition( $ex1, [] );
|
|
$this->assertSame( [ $ex1 ], $callbacks->getRecordedConditions() );
|
|
}
|
|
|
|
}
|