1a88d483505b42a2f8ae60f3dc620ff5813d28c4
[yaffs-website] / vendor / symfony / http-foundation / Tests / File / UploadedFileTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpFoundation\Tests\File;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\File\UploadedFile;
16
17 class UploadedFileTest extends TestCase
18 {
19     protected function setUp()
20     {
21         if (!ini_get('file_uploads')) {
22             $this->markTestSkipped('file_uploads is disabled in php.ini');
23         }
24     }
25
26     public function testConstructWhenFileNotExists()
27     {
28         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
29
30         new UploadedFile(
31             __DIR__.'/Fixtures/not_here',
32             'original.gif',
33             null
34         );
35     }
36
37     public function testFileUploadsWithNoMimeType()
38     {
39         $file = new UploadedFile(
40             __DIR__.'/Fixtures/test.gif',
41             'original.gif',
42             null,
43             filesize(__DIR__.'/Fixtures/test.gif'),
44             UPLOAD_ERR_OK
45         );
46
47         $this->assertEquals('application/octet-stream', $file->getClientMimeType());
48
49         if (\extension_loaded('fileinfo')) {
50             $this->assertEquals('image/gif', $file->getMimeType());
51         }
52     }
53
54     public function testFileUploadsWithUnknownMimeType()
55     {
56         $file = new UploadedFile(
57             __DIR__.'/Fixtures/.unknownextension',
58             'original.gif',
59             null,
60             filesize(__DIR__.'/Fixtures/.unknownextension'),
61             UPLOAD_ERR_OK
62         );
63
64         $this->assertEquals('application/octet-stream', $file->getClientMimeType());
65     }
66
67     public function testGuessClientExtension()
68     {
69         $file = new UploadedFile(
70             __DIR__.'/Fixtures/test.gif',
71             'original.gif',
72             'image/gif',
73             filesize(__DIR__.'/Fixtures/test.gif'),
74             null
75         );
76
77         $this->assertEquals('gif', $file->guessClientExtension());
78     }
79
80     public function testGuessClientExtensionWithIncorrectMimeType()
81     {
82         $file = new UploadedFile(
83             __DIR__.'/Fixtures/test.gif',
84             'original.gif',
85             'image/jpeg',
86             filesize(__DIR__.'/Fixtures/test.gif'),
87             null
88         );
89
90         $this->assertEquals('jpeg', $file->guessClientExtension());
91     }
92
93     public function testErrorIsOkByDefault()
94     {
95         $file = new UploadedFile(
96             __DIR__.'/Fixtures/test.gif',
97             'original.gif',
98             'image/gif',
99             filesize(__DIR__.'/Fixtures/test.gif'),
100             null
101         );
102
103         $this->assertEquals(UPLOAD_ERR_OK, $file->getError());
104     }
105
106     public function testGetClientOriginalName()
107     {
108         $file = new UploadedFile(
109             __DIR__.'/Fixtures/test.gif',
110             'original.gif',
111             'image/gif',
112             filesize(__DIR__.'/Fixtures/test.gif'),
113             null
114         );
115
116         $this->assertEquals('original.gif', $file->getClientOriginalName());
117     }
118
119     public function testGetClientOriginalExtension()
120     {
121         $file = new UploadedFile(
122             __DIR__.'/Fixtures/test.gif',
123             'original.gif',
124             'image/gif',
125             filesize(__DIR__.'/Fixtures/test.gif'),
126             null
127         );
128
129         $this->assertEquals('gif', $file->getClientOriginalExtension());
130     }
131
132     /**
133      * @expectedException \Symfony\Component\HttpFoundation\File\Exception\FileException
134      */
135     public function testMoveLocalFileIsNotAllowed()
136     {
137         $file = new UploadedFile(
138             __DIR__.'/Fixtures/test.gif',
139             'original.gif',
140             'image/gif',
141             filesize(__DIR__.'/Fixtures/test.gif'),
142             UPLOAD_ERR_OK
143         );
144
145         $movedFile = $file->move(__DIR__.'/Fixtures/directory');
146     }
147
148     public function testMoveLocalFileIsAllowedInTestMode()
149     {
150         $path = __DIR__.'/Fixtures/test.copy.gif';
151         $targetDir = __DIR__.'/Fixtures/directory';
152         $targetPath = $targetDir.'/test.copy.gif';
153         @unlink($path);
154         @unlink($targetPath);
155         copy(__DIR__.'/Fixtures/test.gif', $path);
156
157         $file = new UploadedFile(
158             $path,
159             'original.gif',
160             'image/gif',
161             filesize($path),
162             UPLOAD_ERR_OK,
163             true
164         );
165
166         $movedFile = $file->move(__DIR__.'/Fixtures/directory');
167
168         $this->assertFileExists($targetPath);
169         $this->assertFileNotExists($path);
170         $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
171
172         @unlink($targetPath);
173     }
174
175     public function testGetClientOriginalNameSanitizeFilename()
176     {
177         $file = new UploadedFile(
178             __DIR__.'/Fixtures/test.gif',
179             '../../original.gif',
180             'image/gif',
181             filesize(__DIR__.'/Fixtures/test.gif'),
182             null
183         );
184
185         $this->assertEquals('original.gif', $file->getClientOriginalName());
186     }
187
188     public function testGetSize()
189     {
190         $file = new UploadedFile(
191             __DIR__.'/Fixtures/test.gif',
192             'original.gif',
193             'image/gif',
194             filesize(__DIR__.'/Fixtures/test.gif'),
195             null
196         );
197
198         $this->assertEquals(filesize(__DIR__.'/Fixtures/test.gif'), $file->getSize());
199
200         $file = new UploadedFile(
201             __DIR__.'/Fixtures/test',
202             'original.gif',
203             'image/gif'
204         );
205
206         $this->assertEquals(filesize(__DIR__.'/Fixtures/test'), $file->getSize());
207     }
208
209     public function testGetExtension()
210     {
211         $file = new UploadedFile(
212             __DIR__.'/Fixtures/test.gif',
213             'original.gif',
214             null
215         );
216
217         $this->assertEquals('gif', $file->getExtension());
218     }
219
220     public function testIsValid()
221     {
222         $file = new UploadedFile(
223             __DIR__.'/Fixtures/test.gif',
224             'original.gif',
225             null,
226             filesize(__DIR__.'/Fixtures/test.gif'),
227             UPLOAD_ERR_OK,
228             true
229         );
230
231         $this->assertTrue($file->isValid());
232     }
233
234     /**
235      * @dataProvider uploadedFileErrorProvider
236      */
237     public function testIsInvalidOnUploadError($error)
238     {
239         $file = new UploadedFile(
240             __DIR__.'/Fixtures/test.gif',
241             'original.gif',
242             null,
243             filesize(__DIR__.'/Fixtures/test.gif'),
244             $error
245         );
246
247         $this->assertFalse($file->isValid());
248     }
249
250     public function uploadedFileErrorProvider()
251     {
252         return array(
253             array(UPLOAD_ERR_INI_SIZE),
254             array(UPLOAD_ERR_FORM_SIZE),
255             array(UPLOAD_ERR_PARTIAL),
256             array(UPLOAD_ERR_NO_TMP_DIR),
257             array(UPLOAD_ERR_EXTENSION),
258         );
259     }
260
261     public function testIsInvalidIfNotHttpUpload()
262     {
263         $file = new UploadedFile(
264             __DIR__.'/Fixtures/test.gif',
265             'original.gif',
266             null,
267             filesize(__DIR__.'/Fixtures/test.gif'),
268             UPLOAD_ERR_OK
269         );
270
271         $this->assertFalse($file->isValid());
272     }
273 }