Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / ParamValidator / TypeDef / UploadDefTest.php
1 <?php
2
3 namespace Wikimedia\ParamValidator\TypeDef;
4
5 use Wikimedia\ParamValidator\SimpleCallbacks;
6 use Wikimedia\ParamValidator\Util\UploadedFile;
7 use Wikimedia\ParamValidator\ValidationException;
8
9 /**
10 * @covers Wikimedia\ParamValidator\TypeDef\UploadDef
11 */
12 class UploadDefTest extends TypeDefTestCase {
13
14 protected static $testClass = UploadDef::class;
15
16 protected function getCallbacks( $value, array $options ) {
17 if ( $value instanceof UploadedFile ) {
18 return new SimpleCallbacks( [], [ 'test' => $value ] );
19 } else {
20 return new SimpleCallbacks( [ 'test' => $value ] );
21 }
22 }
23
24 protected function getInstance( SimpleCallbacks $callbacks, array $options ) {
25 $ret = $this->getMockBuilder( UploadDef::class )
26 ->setConstructorArgs( [ $callbacks ] )
27 ->setMethods( [ 'getIniSize' ] )
28 ->getMock();
29 $ret->method( 'getIniSize' )->willReturn( $options['inisize'] ?? 2 * 1024 * 1024 );
30 return $ret;
31 }
32
33 private function makeUpload( $err = UPLOAD_ERR_OK ) {
34 return new UploadedFile( [
35 'name' => 'example.txt',
36 'type' => 'text/plain',
37 'size' => 0,
38 'tmp_name' => '...',
39 'error' => $err,
40 ] );
41 }
42
43 public function testGetNoFile() {
44 $typeDef = $this->getInstance(
45 $this->getCallbacks( $this->makeUpload( UPLOAD_ERR_NO_FILE ), [] ),
46 []
47 );
48
49 $this->assertNull( $typeDef->getValue( 'test', [], [] ) );
50 $this->assertNull( $typeDef->getValue( 'nothing', [], [] ) );
51 }
52
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 ]
58 );
59
60 return [
61 'Valid upload' => [ $okFile, $okFile ],
62 'Not an upload' => [
63 'bar',
64 new ValidationException( 'test', 'bar', [], 'badupload-notupload', [] ),
65 ],
66
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' ] ],
74
75 'Form size' => [
76 $this->makeUpload( UPLOAD_ERR_FORM_SIZE ),
77 new ValidationException( 'test', '', [], 'badupload-formsize', [] ),
78 ],
79 'Partial' => [
80 $this->makeUpload( UPLOAD_ERR_PARTIAL ),
81 new ValidationException( 'test', '', [], 'badupload-partial', [] ),
82 ],
83 'No tmp' => [
84 $this->makeUpload( UPLOAD_ERR_NO_TMP_DIR ),
85 new ValidationException( 'test', '', [], 'badupload-notmpdir', [] ),
86 ],
87 'Can\'t write' => [
88 $this->makeUpload( UPLOAD_ERR_CANT_WRITE ),
89 new ValidationException( 'test', '', [], 'badupload-cantwrite', [] ),
90 ],
91 'Ext abort' => [
92 $this->makeUpload( UPLOAD_ERR_EXTENSION ),
93 new ValidationException( 'test', '', [], 'badupload-phpext', [] ),
94 ],
95 'Unknown' => [
96 $this->makeUpload( -43 ), // Should be safe from ever being an UPLOAD_ERR_* constant
97 new ValidationException( 'test', '', [], 'badupload-unknown', [ 'code' => -43 ] ),
98 ],
99
100 'Validating null' => [
101 null,
102 new ValidationException( 'test', '', [], 'badupload', [] ),
103 ],
104 ];
105 }
106
107 public function provideStringifyValue() {
108 return [
109 'Yeah, right' => [ $this->makeUpload(), null ],
110 ];
111 }
112
113 }