‪TYPO3CMS  10.4
UploadedFileTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
21 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
22 
28 class ‪UploadedFileTest extends UnitTestCase
29 {
30  protected ‪$tmpFile;
31 
32  protected function ‪setUp(): void
33  {
34  parent::setUp();
35  $this->tmpFile = null;
36  }
37 
38  protected function ‪tearDown(): void
39  {
40  if (is_scalar($this->tmpFile) && file_exists($this->tmpFile)) {
41  unlink($this->tmpFile);
42  }
43  parent::tearDown();
44  }
45 
50  {
51  return [
52  'null' => [null],
53  'true' => [true],
54  'false' => [false],
55  'int' => [1],
56  'float' => [1.1],
57  /* Have not figured out a valid way to test an invalid path yet; null byte injection
58  * appears to get caught by fopen()
59  'invalid-path' => [ ('WIN' === strtoupper(substr(PHP_OS, 0, 3))) ? '[:]' : 'foo' . "\0" ],
60  */
61  'array' => [['filename']],
62  'object' => [(object)['filename']],
63  ];
64  }
65 
71  {
72  $this->expectException(\InvalidArgumentException::class);
73  new ‪UploadedFile($streamOrFile, 0, UPLOAD_ERR_OK);
74  }
75 
79  public function ‪invalidSizesDataProvider()
80  {
81  return [
82  'null' => [null],
83  'true' => [true],
84  'false' => [false],
85  'float' => [1.1],
86  'string' => ['1'],
87  'array' => [[1]],
88  'object' => [(object)[1]],
89  ];
90  }
91 
97  {
98  $this->expectException(\InvalidArgumentException::class);
99  $this->expectExceptionCode(1436717302);
100  new ‪UploadedFile(fopen('php://temp', 'wb+'), $size, UPLOAD_ERR_OK);
101  }
102 
107  {
108  return [
109  'null' => [null],
110  'true' => [true],
111  'false' => [false],
112  'float' => [1.1],
113  'string' => ['1'],
114  'array' => [[1]],
115  'object' => [(object)[1]],
116  'negative' => [-1],
117  'too-big' => [9],
118  ];
119  }
120 
126  {
127  $this->expectException(\InvalidArgumentException::class);
128  $this->expectExceptionCode(1436717303);
129  new ‪UploadedFile(fopen('php://temp', 'wb+'), 0, $status);
130  }
131 
136  {
137  return [
138  'true' => [true],
139  'false' => [false],
140  'int' => [1],
141  'float' => [1.1],
142  'array' => [['string']],
143  'object' => [(object)['string']],
144  ];
145  }
146 
152  {
153  $this->expectException(\InvalidArgumentException::class);
154  $this->expectExceptionCode(1436717304);
155  new ‪UploadedFile(fopen('php://temp', 'wb+'), 0, UPLOAD_ERR_OK, $filename);
156  }
157 
163  {
164  $this->expectException(\InvalidArgumentException::class);
165  $this->expectExceptionCode(1436717305);
166  new ‪UploadedFile(fopen('php://temp', 'wb+'), 0, UPLOAD_ERR_OK, 'foobar.baz', $mediaType);
167  }
168 
173  {
174  $stream = new ‪Stream('php://temp');
175  $upload = new ‪UploadedFile($stream, 0, UPLOAD_ERR_OK);
176  self::assertSame($stream, $upload->getStream());
177  }
178 
183  {
184  $stream = fopen('php://temp', 'wb+');
185  $upload = new ‪UploadedFile($stream, 0, UPLOAD_ERR_OK);
186  $uploadStream = $upload->getStream()->detach();
187  self::assertSame($stream, $uploadStream);
188  }
189 
194  {
195  $this->tmpFile = $stream = tempnam(sys_get_temp_dir(), 'phly');
196  $upload = new ‪UploadedFile($stream, 0, UPLOAD_ERR_OK);
197  $uploadStream = $upload->getStream();
198  $r = new \ReflectionProperty($uploadStream, 'stream');
199  $r->setAccessible(true);
200  self::assertSame($stream, $r->getValue($uploadStream));
201  }
202 
207  {
208  $stream = new ‪Stream('php://temp', 'wb+');
209  $stream->write('Foo bar!');
210  $upload = new ‪UploadedFile($stream, 0, UPLOAD_ERR_OK);
211 
212  $this->tmpFile = $to = GeneralUtility::tempnam('psr7');
213  $upload->moveTo($to);
214  self::assertTrue(file_exists($to));
215  $contents = file_get_contents($to);
216  self::assertEquals($stream->__toString(), $contents);
217  }
218 
223  {
224  return [
225  'null' => [null],
226  'true' => [true],
227  'false' => [false],
228  'int' => [1],
229  'float' => [1.1],
230  'empty' => [''],
231  'array' => [['filename']],
232  'object' => [(object)['filename']],
233  ];
234  }
235 
241  {
242  $stream = new ‪Stream('php://temp', 'wb+');
243  $stream->write('Foo bar!');
244  $upload = new ‪UploadedFile($stream, 0, UPLOAD_ERR_OK);
245 
246  $this->tmpFile = $path;
247  $this->expectException(\InvalidArgumentException::class);
248  $this->expectExceptionCode(1436717307);
249  $upload->moveTo($path);
250  }
251 
256  {
257  $stream = new ‪Stream('php://temp', 'wb+');
258  $stream->write('Foo bar!');
259  $upload = new ‪UploadedFile($stream, 0, UPLOAD_ERR_OK);
260 
261  $this->tmpFile = $to = GeneralUtility::tempnam('psr7');
262  $upload->moveTo($to);
263  self::assertTrue(file_exists($to));
264 
265  $this->expectException(\RuntimeException::class);
266  $this->expectExceptionCode(1436717308);
267  $upload->moveTo($to);
268  }
269 
274  {
275  $stream = new ‪Stream('php://temp', 'wb+');
276  $stream->write('Foo bar!');
277  $upload = new ‪UploadedFile($stream, 0, UPLOAD_ERR_OK);
278 
279  $this->tmpFile = $to = GeneralUtility::tempnam('psr7');
280  $upload->moveTo($to);
281  self::assertTrue(file_exists($to));
282 
283  $this->expectException(\RuntimeException::class);
284  $this->expectExceptionCode(1436717306);
285  $upload->getStream();
286  }
287 }
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\moveToCannotBeCalledMoreThanOnce
‪moveToCannotBeCalledMoreThanOnce()
Definition: UploadedFileTest.php:255
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\constructorRaisesExceptionOnInvalidClientFilename
‪constructorRaisesExceptionOnInvalidClientFilename($filename)
Definition: UploadedFileTest.php:151
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\moveToMovesFileToDesignatedPath
‪moveToMovesFileToDesignatedPath()
Definition: UploadedFileTest.php:206
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest
Definition: UploadedFileTest.php:29
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\$tmpFile
‪$tmpFile
Definition: UploadedFileTest.php:30
‪TYPO3\CMS\Core\Tests\Unit\Http
Definition: ApplicationTypeTest.php:16
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\constructorRaisesExceptionOnInvalidClientMediaType
‪constructorRaisesExceptionOnInvalidClientMediaType($mediaType)
Definition: UploadedFileTest.php:162
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\getStreamReturnsOriginalStreamObject
‪getStreamReturnsOriginalStreamObject()
Definition: UploadedFileTest.php:172
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\invalidMovePathsDataProvider
‪array invalidMovePathsDataProvider()
Definition: UploadedFileTest.php:222
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\setUp
‪setUp()
Definition: UploadedFileTest.php:32
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\moveToRaisesExceptionForInvalidPath
‪moveToRaisesExceptionForInvalidPath($path)
Definition: UploadedFileTest.php:240
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\invalidFilenamesAndMediaTypesDataProvider
‪array invalidFilenamesAndMediaTypesDataProvider()
Definition: UploadedFileTest.php:135
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\constructorRaisesExceptionOnInvalidStreamOrFile
‪constructorRaisesExceptionOnInvalidStreamOrFile($streamOrFile)
Definition: UploadedFileTest.php:70
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\getGetStreamRaisesExceptionAfterMove
‪getGetStreamRaisesExceptionAfterMove()
Definition: UploadedFileTest.php:273
‪TYPO3\CMS\Core\Http\Stream
Definition: Stream.php:29
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\constructorRaisesExceptionOnInvalidErrorStatus
‪constructorRaisesExceptionOnInvalidErrorStatus($status)
Definition: UploadedFileTest.php:125
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\invalidStreamsDataProvider
‪array invalidStreamsDataProvider()
Definition: UploadedFileTest.php:49
‪TYPO3\CMS\Core\Http\UploadedFile
Definition: UploadedFile.php:31
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\tearDown
‪tearDown()
Definition: UploadedFileTest.php:38
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\constructorRaisesExceptionOnInvalidSize
‪constructorRaisesExceptionOnInvalidSize($size)
Definition: UploadedFileTest.php:96
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\getStreamReturnsWrappedPhpStream
‪getStreamReturnsWrappedPhpStream()
Definition: UploadedFileTest.php:182
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\invalidSizesDataProvider
‪array invalidSizesDataProvider()
Definition: UploadedFileTest.php:79
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\invalidErrorStatusesDataProvider
‪array invalidErrorStatusesDataProvider()
Definition: UploadedFileTest.php:106
‪TYPO3\CMS\Core\Tests\Unit\Http\UploadedFileTest\getStreamReturnsStreamForFile
‪getStreamReturnsStreamForFile()
Definition: UploadedFileTest.php:193