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