3 namespace Wikimedia\ParamValidator\TypeDef
;
5 use Wikimedia\ParamValidator\SimpleCallbacks
;
6 use Wikimedia\ParamValidator\Util\UploadedFile
;
7 use Wikimedia\ParamValidator\ValidationException
;
10 * @covers Wikimedia\ParamValidator\TypeDef\UploadDef
12 class UploadDefTest
extends TypeDefTestCase
{
14 protected static $testClass = UploadDef
::class;
16 protected function getCallbacks( $value, array $options ) {
17 if ( $value instanceof UploadedFile
) {
18 return new SimpleCallbacks( [], [ 'test' => $value ] );
20 return new SimpleCallbacks( [ 'test' => $value ] );
24 protected function getInstance( SimpleCallbacks
$callbacks, array $options ) {
25 $ret = $this->getMockBuilder( UploadDef
::class )
26 ->setConstructorArgs( [ $callbacks ] )
27 ->setMethods( [ 'getIniSize' ] )
29 $ret->method( 'getIniSize' )->willReturn( $options['inisize'] ??
2 * 1024 * 1024 );
33 private function makeUpload( $err = UPLOAD_ERR_OK
) {
34 return new UploadedFile( [
35 'name' => 'example.txt',
36 'type' => 'text/plain',
43 public function testGetNoFile() {
44 $typeDef = $this->getInstance(
45 $this->getCallbacks( $this->makeUpload( UPLOAD_ERR_NO_FILE
), [] ),
49 $this->assertNull( $typeDef->getValue( 'test', [], [] ) );
50 $this->assertNull( $typeDef->getValue( 'nothing', [], [] ) );
53 public function provideValidate() {
54 $okFile = $this->makeUpload();
55 $iniFile = $this->makeUpload( UPLOAD_ERR_INI_SIZE
);
56 $exIni = new ValidationException(
57 'test', '', [], 'badupload-inisize', [ 'size' => 2 * 1024 * 1024 * 1024 ]
61 'Valid upload' => [ $okFile, $okFile ],
64 new ValidationException( 'test', 'bar', [], 'badupload-notupload', [] ),
67 'Too big (bytes)' => [ $iniFile, $exIni, [], [ 'inisize' => 2 * 1024 * 1024 * 1024 ] ],
68 'Too big (k)' => [ $iniFile, $exIni, [], [ 'inisize' => ( 2 * 1024 * 1024 ) . 'k' ] ],
69 'Too big (K)' => [ $iniFile, $exIni, [], [ 'inisize' => ( 2 * 1024 * 1024 ) . 'K' ] ],
70 'Too big (m)' => [ $iniFile, $exIni, [], [ 'inisize' => ( 2 * 1024 ) . 'm' ] ],
71 'Too big (M)' => [ $iniFile, $exIni, [], [ 'inisize' => ( 2 * 1024 ) . 'M' ] ],
72 'Too big (g)' => [ $iniFile, $exIni, [], [ 'inisize' => '2g' ] ],
73 'Too big (G)' => [ $iniFile, $exIni, [], [ 'inisize' => '2G' ] ],
76 $this->makeUpload( UPLOAD_ERR_FORM_SIZE
),
77 new ValidationException( 'test', '', [], 'badupload-formsize', [] ),
80 $this->makeUpload( UPLOAD_ERR_PARTIAL
),
81 new ValidationException( 'test', '', [], 'badupload-partial', [] ),
84 $this->makeUpload( UPLOAD_ERR_NO_TMP_DIR
),
85 new ValidationException( 'test', '', [], 'badupload-notmpdir', [] ),
88 $this->makeUpload( UPLOAD_ERR_CANT_WRITE
),
89 new ValidationException( 'test', '', [], 'badupload-cantwrite', [] ),
92 $this->makeUpload( UPLOAD_ERR_EXTENSION
),
93 new ValidationException( 'test', '', [], 'badupload-phpext', [] ),
96 $this->makeUpload( -43 ), // Should be safe from ever being an UPLOAD_ERR_* constant
97 new ValidationException( 'test', '', [], 'badupload-unknown', [ 'code' => -43 ] ),
100 'Validating null' => [
102 new ValidationException( 'test', '', [], 'badupload', [] ),
107 public function provideStringifyValue() {
109 'Yeah, right' => [ $this->makeUpload(), null ],