TYPO3 CMS  TYPO3_7-6
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 
27 {
28  protected $tmpFile;
29 
30  protected function setUp()
31  {
32  $this->tmpfile = null;
33  }
34 
35  protected function tearDown()
36  {
37  if (is_scalar($this->tmpFile) && file_exists($this->tmpFile)) {
38  unlink($this->tmpFile);
39  }
40  }
41 
45  public function invalidStreamsDataProvider()
46  {
47  return [
48  'null' => [null],
49  'true' => [true],
50  'false' => [false],
51  'int' => [1],
52  'float' => [1.1],
53  /* Have not figured out a valid way to test an invalid path yet; null byte injection
54  * appears to get caught by fopen()
55  'invalid-path' => [ ('WIN' === strtoupper(substr(PHP_OS, 0, 3))) ? '[:]' : 'foo' . chr(0) ],
56  */
57  'array' => [['filename']],
58  'object' => [(object) ['filename']],
59  ];
60  }
61 
66  public function constructorRaisesExceptionOnInvalidStreamOrFile($streamOrFile)
67  {
68  $this->setExpectedException('InvalidArgumentException');
69  new UploadedFile($streamOrFile, 0, UPLOAD_ERR_OK);
70  }
71 
75  public function invalidSizesDataProvider()
76  {
77  return [
78  'null' => [null],
79  'true' => [true],
80  'false' => [false],
81  'float' => [1.1],
82  'string' => ['1'],
83  'array' => [[1]],
84  'object' => [(object) [1]],
85  ];
86  }
87 
93  {
94  $this->setExpectedException('InvalidArgumentException', 'size');
95  new UploadedFile(fopen('php://temp', 'wb+'), $size, UPLOAD_ERR_OK);
96  }
97 
102  {
103  return [
104  'null' => [null],
105  'true' => [true],
106  'false' => [false],
107  'float' => [1.1],
108  'string' => ['1'],
109  'array' => [[1]],
110  'object' => [(object) [1]],
111  'negative' => [-1],
112  'too-big' => [9],
113  ];
114  }
115 
121  {
122  $this->setExpectedException('InvalidArgumentException', 'status');
123  new UploadedFile(fopen('php://temp', 'wb+'), 0, $status);
124  }
125 
130  {
131  return [
132  'true' => [true],
133  'false' => [false],
134  'int' => [1],
135  'float' => [1.1],
136  'array' => [['string']],
137  'object' => [(object) ['string']],
138  ];
139  }
140 
146  {
147  $this->setExpectedException('InvalidArgumentException', 'filename');
148  new UploadedFile(fopen('php://temp', 'wb+'), 0, UPLOAD_ERR_OK, $filename);
149  }
150 
156  {
157  $this->setExpectedException('InvalidArgumentException', 'media type');
158  new UploadedFile(fopen('php://temp', 'wb+'), 0, UPLOAD_ERR_OK, 'foobar.baz', $mediaType);
159  }
160 
165  {
166  $stream = new Stream('php://temp');
167  $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
168  $this->assertSame($stream, $upload->getStream());
169  }
170 
175  {
176  $stream = fopen('php://temp', 'wb+');
177  $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
178  $uploadStream = $upload->getStream()->detach();
179  $this->assertSame($stream, $uploadStream);
180  }
181 
186  {
187  $this->tmpFile = $stream = tempnam(sys_get_temp_dir(), 'phly');
188  $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
189  $uploadStream = $upload->getStream();
190  $r = new \ReflectionProperty($uploadStream, 'stream');
191  $r->setAccessible(true);
192  $this->assertSame($stream, $r->getValue($uploadStream));
193  }
194 
199  {
200  $stream = new Stream('php://temp', 'wb+');
201  $stream->write('Foo bar!');
202  $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
203 
204  $this->tmpFile = $to = GeneralUtility::tempnam('psr7');
205  $upload->moveTo($to);
206  $this->assertTrue(file_exists($to));
207  $contents = file_get_contents($to);
208  $this->assertEquals($stream->__toString(), $contents);
209  }
210 
215  {
216  return [
217  'null' => [null],
218  'true' => [true],
219  'false' => [false],
220  'int' => [1],
221  'float' => [1.1],
222  'empty' => [''],
223  'array' => [['filename']],
224  'object' => [(object) ['filename']],
225  ];
226  }
227 
232  public function moveToRaisesExceptionForInvalidPath($path)
233  {
234  $stream = new Stream('php://temp', 'wb+');
235  $stream->write('Foo bar!');
236  $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
237 
238  $this->tmpFile = $path;
239  $this->setExpectedException('InvalidArgumentException', 'path');
240  $upload->moveTo($path);
241  }
242 
247  {
248  $stream = new Stream('php://temp', 'wb+');
249  $stream->write('Foo bar!');
250  $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
251 
252  $this->tmpFile = $to = GeneralUtility::tempnam('psr7');
253  $upload->moveTo($to);
254  $this->assertTrue(file_exists($to));
255 
256  $this->setExpectedException('RuntimeException', 'moved');
257  $upload->moveTo($to);
258  }
259 
264  {
265  $stream = new Stream('php://temp', 'wb+');
266  $stream->write('Foo bar!');
267  $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
268 
269  $this->tmpFile = $to = GeneralUtility::tempnam('psr7');
270  $upload->moveTo($to);
271  $this->assertTrue(file_exists($to));
272 
273  $this->setExpectedException('RuntimeException', 'moved');
274  $upload->getStream();
275  }
276 }
static tempnam($filePrefix, $fileSuffix='')