| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Tests\ParamValidator\Util; |
| 4 |
|
| 5 |
require_once __DIR__ . '/UploadedFileTestBase.php'; |
| 6 |
|
| 7 |
use PHPUnit\Framework\AssertionFailedError; |
| 8 |
use Psr\Http\Message\StreamInterface; |
| 9 |
use RuntimeException; |
| 10 |
use Wikimedia\ParamValidator\Util\UploadedFile; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class UploadedFileTest extends UploadedFileTestBase { |
| 16 |
|
| 17 |
public function testGetStream() { |
| 18 |
$filename = $this->makeTemp( __FUNCTION__ ); |
| 19 |
|
| 20 |
$file = new UploadedFile( [ 'error' => UPLOAD_ERR_OK, 'tmp_name' => $filename ], false ); |
| 21 |
|
| 22 |
|
| 23 |
foreach ( [ |
| 24 |
UPLOAD_ERR_INI_SIZE, |
| 25 |
UPLOAD_ERR_FORM_SIZE, |
| 26 |
UPLOAD_ERR_PARTIAL, |
| 27 |
UPLOAD_ERR_NO_FILE, |
| 28 |
UPLOAD_ERR_NO_TMP_DIR, |
| 29 |
UPLOAD_ERR_CANT_WRITE, |
| 30 |
UPLOAD_ERR_EXTENSION, |
| 31 |
-42 |
| 32 |
] as $code ) { |
| 33 |
$file2 = new UploadedFile( [ 'error' => $code, 'tmp_name' => $filename ], false ); |
| 34 |
try { |
| 35 |
$file2->getStream(); |
| 36 |
$this->fail( 'Expected exception not thrown' ); |
| 37 |
} catch ( AssertionFailedError $ex ) { |
| 38 |
throw $ex; |
| 39 |
} catch ( RuntimeException $ex ) { |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
$stream = $file->getStream(); |
| 45 |
$this->assertInstanceOf( StreamInterface::class, $stream ); |
| 46 |
$stream->seek( 0 ); |
| 47 |
$this->assertSame( 'foobar', $stream->getContents() ); |
| 48 |
|
| 49 |
|
| 50 |
$this->assertInstanceOf( StreamInterface::class, $file->getStream() ); |
| 51 |
|
| 52 |
|
| 53 |
$file->moveTo( $filename . '.xxx' ); |
| 54 |
try { |
| 55 |
try { |
| 56 |
$file->getStream(); |
| 57 |
$this->fail( 'Expected exception not thrown' ); |
| 58 |
} catch ( AssertionFailedError $ex ) { |
| 59 |
throw $ex; |
| 60 |
} catch ( RuntimeException $ex ) { |
| 61 |
$this->assertSame( 'File has already been moved', $ex->getMessage() ); |
| 62 |
} |
| 63 |
try { |
| 64 |
$stream->seek( 0 ); |
| 65 |
$stream->getContents(); |
| 66 |
$this->fail( 'Expected exception not thrown' ); |
| 67 |
} catch ( AssertionFailedError $ex ) { |
| 68 |
throw $ex; |
| 69 |
} catch ( RuntimeException $ex ) { |
| 70 |
} |
| 71 |
} finally { |
| 72 |
unlink( $filename . '.xxx' ); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
$file = new UploadedFile( [ 'error' => UPLOAD_ERR_OK, 'tmp_name' => $filename ], true ); |
| 77 |
try { |
| 78 |
$file->getStream(); |
| 79 |
$this->fail( 'Expected exception not thrown' ); |
| 80 |
} catch ( AssertionFailedError $ex ) { |
| 81 |
throw $ex; |
| 82 |
} catch ( RuntimeException $ex ) { |
| 83 |
$this->assertSame( 'Uploaded file is missing', $ex->getMessage() ); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public function testMoveTo() { |
| 88 |
|
| 89 |
$filename = $this->makeTemp( __FUNCTION__ ); |
| 90 |
$this->assertFileExists( $filename, 'existence check' ); |
| 91 |
$this->assertFileDoesNotExist( "$filename.xxx", 'Non existence check' ); |
| 92 |
$file = new UploadedFile( [ 'error' => UPLOAD_ERR_OK, 'tmp_name' => $filename ], false ); |
| 93 |
$file->moveTo( $filename . '.xxx' ); |
| 94 |
$this->assertFileDoesNotExist( $filename ); |
| 95 |
$this->assertFileExists( "$filename.xxx" ); |
| 96 |
|
| 97 |
|
| 98 |
$this->assertFileDoesNotExist( "$filename.yyy", 'Non existence check' ); |
| 99 |
try { |
| 100 |
$file->moveTo( $filename . '.yyy' ); |
| 101 |
$this->fail( 'Expected exception not thrown' ); |
| 102 |
} catch ( AssertionFailedError $ex ) { |
| 103 |
throw $ex; |
| 104 |
} catch ( RuntimeException $ex ) { |
| 105 |
$this->assertSame( 'File has already been moved', $ex->getMessage() ); |
| 106 |
} |
| 107 |
$this->assertFileDoesNotExist( $filename ); |
| 108 |
$this->assertFileExists( "$filename.xxx" ); |
| 109 |
$this->assertFileDoesNotExist( "$filename.yyy" ); |
| 110 |
|
| 111 |
|
| 112 |
$file = new UploadedFile( [ 'error' => UPLOAD_ERR_OK, 'tmp_name' => "$filename.aaa" ], false ); |
| 113 |
$this->assertFileDoesNotExist( "$filename.aaa", 'Non existence check' ); |
| 114 |
$this->assertFileDoesNotExist( "$filename.bbb", 'Non existence check' ); |
| 115 |
try { |
| 116 |
$file->moveTo( $filename . '.bbb' ); |
| 117 |
$this->fail( 'Expected exception not thrown' ); |
| 118 |
} catch ( AssertionFailedError $ex ) { |
| 119 |
throw $ex; |
| 120 |
} catch ( RuntimeException $ex ) { |
| 121 |
$this->assertSame( 'Uploaded file is missing', $ex->getMessage() ); |
| 122 |
} |
| 123 |
$this->assertFileDoesNotExist( "$filename.aaa" ); |
| 124 |
$this->assertFileDoesNotExist( "$filename.bbb" ); |
| 125 |
|
| 126 |
|
| 127 |
$filename = $this->makeTemp( __FUNCTION__ ); |
| 128 |
$this->assertFileExists( $filename, 'existence check' ); |
| 129 |
$this->assertFileDoesNotExist( "$filename.xxx", 'Non existence check' ); |
| 130 |
$file = new UploadedFile( [ 'error' => UPLOAD_ERR_OK, 'tmp_name' => $filename ] ); |
| 131 |
try { |
| 132 |
$file->moveTo( $filename . '.xxx' ); |
| 133 |
$this->fail( 'Expected exception not thrown' ); |
| 134 |
} catch ( AssertionFailedError $ex ) { |
| 135 |
throw $ex; |
| 136 |
} catch ( RuntimeException $ex ) { |
| 137 |
$this->assertSame( 'Specified file is not an uploaded file', $ex->getMessage() ); |
| 138 |
} |
| 139 |
$this->assertFileExists( $filename ); |
| 140 |
$this->assertFileDoesNotExist( "$filename.xxx" ); |
| 141 |
|
| 142 |
|
| 143 |
$filename = $this->makeTemp( __FUNCTION__ ); |
| 144 |
$this->assertFileExists( $filename, 'existence check' ); |
| 145 |
$this->assertFileDoesNotExist( "$filename.xxx", 'Non existence check' ); |
| 146 |
foreach ( [ |
| 147 |
UPLOAD_ERR_INI_SIZE, |
| 148 |
UPLOAD_ERR_FORM_SIZE, |
| 149 |
UPLOAD_ERR_PARTIAL, |
| 150 |
UPLOAD_ERR_NO_FILE, |
| 151 |
UPLOAD_ERR_NO_TMP_DIR, |
| 152 |
UPLOAD_ERR_CANT_WRITE, |
| 153 |
UPLOAD_ERR_EXTENSION, |
| 154 |
-42 |
| 155 |
] as $code ) { |
| 156 |
$file = new UploadedFile( [ 'error' => $code, 'tmp_name' => $filename ], false ); |
| 157 |
try { |
| 158 |
$file->moveTo( $filename . '.xxx' ); |
| 159 |
$this->fail( 'Expected exception not thrown' ); |
| 160 |
} catch ( AssertionFailedError $ex ) { |
| 161 |
throw $ex; |
| 162 |
} catch ( RuntimeException $ex ) { |
| 163 |
} |
| 164 |
$this->assertFileExists( $filename ); |
| 165 |
$this->assertFileDoesNotExist( "$filename.xxx" ); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
$filename = $this->makeTemp( __FUNCTION__, 'file1' ); |
| 170 |
$filename2 = $this->makeTemp( __FUNCTION__, 'file2' ); |
| 171 |
$this->assertFileExists( $filename, 'existence check' ); |
| 172 |
$file = new UploadedFile( [ 'error' => UPLOAD_ERR_OK, 'tmp_name' => $filename ], false ); |
| 173 |
try { |
| 174 |
$file->moveTo( $filename2 . DIRECTORY_SEPARATOR . 'foobar' ); |
| 175 |
$this->fail( 'Expected exception not thrown' ); |
| 176 |
} catch ( AssertionFailedError $ex ) { |
| 177 |
throw $ex; |
| 178 |
} catch ( RuntimeException $ex ) { |
| 179 |
} |
| 180 |
$this->assertFileExists( $filename ); |
| 181 |
} |
| 182 |
|
| 183 |
public function testInfoMethods() { |
| 184 |
$filename = $this->makeTemp( __FUNCTION__ ); |
| 185 |
$file = new UploadedFile( [ |
| 186 |
'name' => 'C:\\example.txt', |
| 187 |
'type' => 'text/plain', |
| 188 |
'size' => 1025, |
| 189 |
'error' => UPLOAD_ERR_OK, |
| 190 |
'tmp_name' => $filename, |
| 191 |
], false ); |
| 192 |
$this->assertSame( 1025, $file->getSize() ); |
| 193 |
$this->assertSame( UPLOAD_ERR_OK, $file->getError() ); |
| 194 |
$this->assertSame( 'C:\\example.txt', $file->getClientFilename() ); |
| 195 |
$this->assertSame( 'text/plain', $file->getClientMediaType() ); |
| 196 |
|
| 197 |
|
| 198 |
$file = new UploadedFile( [], false ); |
| 199 |
$this->assertSame( null, $file->getSize() ); |
| 200 |
$this->assertSame( UPLOAD_ERR_NO_FILE, $file->getError() ); |
| 201 |
$this->assertSame( null, $file->getClientFilename() ); |
| 202 |
$this->assertSame( null, $file->getClientMediaType() ); |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
$file = new UploadedFile( [ |
| 207 |
'name' => '', |
| 208 |
'type' => '', |
| 209 |
'size' => 100, |
| 210 |
'error' => UPLOAD_ERR_NO_FILE, |
| 211 |
'tmp_name' => $filename, |
| 212 |
], false ); |
| 213 |
$this->assertSame( null, $file->getClientFilename() ); |
| 214 |
$this->assertSame( null, $file->getClientMediaType() ); |
| 215 |
} |
| 216 |
|
| 217 |
} |