1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
||
5 | * @copyright Aimeos (aimeos.org), 2016-2018 |
||
6 | */ |
||
7 | |||
8 | |||
9 | namespace Aimeos\Controller\Common\Media; |
||
10 | |||
11 | |||
12 | class StandardTest extends \PHPUnit\Framework\TestCase |
||
13 | { |
||
14 | private $context; |
||
15 | private $object; |
||
16 | |||
17 | |||
18 | protected function setUp() |
||
19 | { |
||
20 | $this->context = \TestHelperCntl::getContext(); |
||
21 | $this->object = new \Aimeos\Controller\Common\Media\Standard( $this->context ); |
||
22 | } |
||
23 | |||
24 | |||
25 | protected function tearDown() |
||
26 | { |
||
27 | $this->object = null; |
||
28 | } |
||
29 | |||
30 | |||
31 | public function testAdd() |
||
32 | { |
||
33 | $object = $this->getMockBuilder( \Aimeos\Controller\Common\Media\Standard::class ) |
||
34 | ->setMethods( array( 'checkFileUpload', 'storeFile' ) ) |
||
35 | ->setConstructorArgs( array( $this->context ) ) |
||
36 | ->getMock(); |
||
37 | |||
38 | $object->expects( $this->once() )->method( 'checkFileUpload' ); |
||
39 | $object->expects( $this->exactly( 2 ) )->method( 'storeFile' ); |
||
40 | |||
41 | $file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock(); |
||
42 | $file->expects( $this->once() )->method( 'getStream' ) |
||
43 | ->will( $this->returnValue( file_get_contents( __DIR__ . '/testfiles/test.gif' ) ) ); |
||
44 | |||
45 | $item = \Aimeos\MShop::create( $this->context, 'media' )->createItem(); |
||
46 | |||
47 | $object->add( $item, $file ); |
||
48 | } |
||
49 | |||
50 | |||
51 | public function testAddBinary() |
||
52 | { |
||
53 | $object = $this->getMockBuilder( \Aimeos\Controller\Common\Media\Standard::class ) |
||
54 | ->setMethods( array( 'checkFileUpload', 'storeFile' ) ) |
||
55 | ->setConstructorArgs( array( $this->context ) ) |
||
56 | ->getMock(); |
||
57 | |||
58 | $object->expects( $this->once() )->method( 'checkFileUpload' ); |
||
59 | $object->expects( $this->once() )->method( 'storeFile' ); |
||
60 | |||
61 | $file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock(); |
||
62 | $file->expects( $this->once() )->method( 'getStream' ) |
||
63 | ->will( $this->returnValue( file_get_contents( __DIR__ . '/testfiles/test.pdf' ) ) ); |
||
64 | |||
65 | $item = \Aimeos\MShop::create( $this->context, 'media' )->createItem(); |
||
66 | |||
67 | $object->add( $item, $file ); |
||
68 | } |
||
69 | |||
70 | |||
71 | public function testDelete() |
||
72 | { |
||
73 | $fsm = $this->getMockBuilder( \Aimeos\MW\Filesystem\Manager\Standard::class ) |
||
74 | ->setMethods( array( 'get' ) ) |
||
75 | ->disableOriginalConstructor() |
||
76 | ->getMock(); |
||
77 | |||
78 | $fs = $this->getMockBuilder( \Aimeos\MW\Filesystem\Standard::class ) |
||
79 | ->setMethods( array( 'has', 'rm' ) ) |
||
80 | ->disableOriginalConstructor() |
||
81 | ->getMock(); |
||
82 | |||
83 | $fsm->expects( $this->once() )->method( 'get' ) |
||
84 | ->will( $this->returnValue( $fs ) ); |
||
85 | |||
86 | $fs->expects( $this->exactly( 2 ) )->method( 'has' ) |
||
87 | ->will( $this->returnValue( true ) ); |
||
88 | |||
89 | $fs->expects( $this->exactly( 2 ) )->method( 'rm' ); |
||
90 | |||
91 | $this->context->setFilesystemManager( $fsm ); |
||
92 | |||
93 | $item = \Aimeos\MShop::create( $this->context, 'media' )->createItem(); |
||
94 | $item->setPreview( 'test' ); |
||
95 | $item->setUrl( 'test' ); |
||
96 | |||
97 | $this->object->delete( $item ); |
||
98 | } |
||
99 | |||
100 | |||
101 | public function testScale() |
||
102 | { |
||
103 | $this->context->getConfig()->set( 'controller/common/media/standard/files/scale', true ); |
||
104 | |||
105 | $object = $this->getMockBuilder( \Aimeos\Controller\Common\Media\Standard::class ) |
||
106 | ->setMethods( array( 'getFileContent', 'storeFile' ) ) |
||
107 | ->setConstructorArgs( array( $this->context ) ) |
||
108 | ->getMock(); |
||
109 | |||
110 | $object->expects( $this->once() )->method( 'getFileContent' ) |
||
111 | ->will( $this->returnValue( file_get_contents( __DIR__ . '/testfiles/test.png' ) ) ); |
||
112 | |||
113 | $object->expects( $this->exactly( 2 ) )->method( 'storeFile' ); |
||
114 | |||
115 | |||
116 | $item = \Aimeos\MShop::create( $this->context, 'media' )->createItem(); |
||
117 | $item->setPreview( 'preview.jpg' ); |
||
118 | $item->setUrl( 'test.jpg' ); |
||
119 | |||
120 | $object->scale( $item ); |
||
121 | |||
122 | $this->assertNotEquals( 'test.jpg', $item->getUrl() ); |
||
123 | $this->assertNotEquals( 'preview.jpg', $item->getPreview() ); |
||
124 | } |
||
125 | |||
126 | |||
127 | public function testCheckFileUploadOK() |
||
128 | { |
||
129 | $file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock(); |
||
130 | $file->expects( $this->once() )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_OK ) ); |
||
131 | |||
132 | $this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) ); |
||
133 | } |
||
134 | |||
135 | |||
136 | public function testCheckFileUploadSize() |
||
137 | { |
||
138 | $file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock(); |
||
139 | $file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_INI_SIZE ) ); |
||
140 | |||
141 | $this->setExpectedException( \Aimeos\Controller\Common\Exception::class ); |
||
142 | $this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) ); |
||
143 | } |
||
144 | |||
145 | |||
146 | public function testCheckFileUploadPartitial() |
||
147 | { |
||
148 | $file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock(); |
||
149 | $file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_PARTIAL ) ); |
||
150 | |||
151 | $this->setExpectedException( \Aimeos\Controller\Common\Exception::class ); |
||
152 | $this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) ); |
||
153 | } |
||
154 | |||
155 | |||
156 | public function testCheckFileUploadNoFile() |
||
157 | { |
||
158 | $file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock(); |
||
159 | $file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_NO_FILE ) ); |
||
160 | |||
161 | $this->setExpectedException( \Aimeos\Controller\Common\Exception::class ); |
||
162 | $this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) ); |
||
163 | } |
||
164 | |||
165 | |||
166 | public function testCheckFileUploadNoTmpdir() |
||
167 | { |
||
168 | $file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock(); |
||
169 | $file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_NO_TMP_DIR ) ); |
||
170 | |||
171 | $this->setExpectedException( \Aimeos\Controller\Common\Exception::class ); |
||
172 | $this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) ); |
||
173 | } |
||
174 | |||
175 | |||
176 | public function testCheckFileUploadCantWrite() |
||
177 | { |
||
178 | $file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock(); |
||
179 | $file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_CANT_WRITE ) ); |
||
180 | |||
181 | $this->setExpectedException( \Aimeos\Controller\Common\Exception::class ); |
||
182 | $this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) ); |
||
183 | } |
||
184 | |||
185 | |||
186 | public function testCheckFileUploadExtension() |
||
187 | { |
||
188 | $file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock(); |
||
189 | $file->expects( $this->exactly( 2 ) )->method( 'getError' )->will( $this->returnValue( UPLOAD_ERR_EXTENSION ) ); |
||
190 | |||
191 | $this->setExpectedException( \Aimeos\Controller\Common\Exception::class ); |
||
192 | $this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) ); |
||
193 | } |
||
194 | |||
195 | |||
196 | public function testCheckFileUploadException() |
||
197 | { |
||
198 | $file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock(); |
||
199 | |||
200 | $this->setExpectedException( \Aimeos\Controller\Common\Exception::class ); |
||
201 | $this->access( 'checkFileUpload' )->invokeArgs( $this->object, array( $file ) ); |
||
202 | } |
||
203 | |||
204 | |||
205 | public function testGetFileContent() |
||
206 | { |
||
207 | $dest = dirname( dirname( dirname( __DIR__ ) ) ) . '/tmp/'; |
||
208 | if( !is_dir( $dest ) ) { mkdir( $dest, 0755, true ); } |
||
209 | copy( __DIR__ . '/testfiles/test.gif', $dest . 'test.gif' ); |
||
210 | |||
211 | $result = $this->access( 'getFileContent' )->invokeArgs( $this->object, array( 'test.gif', 'fs-media' ) ); |
||
212 | |||
213 | $this->assertNotEquals( '', $result ); |
||
214 | } |
||
215 | |||
216 | |||
217 | public function testGetFileContentHttp() |
||
218 | { |
||
219 | $url = 'https://aimeos.org/fileadmin/logos/favicon.png'; |
||
220 | $result = $this->access( 'getFileContent' )->invokeArgs( $this->object, array( $url, 'fs-media' ) ); |
||
221 | |||
222 | $this->assertNotEquals( '', $result ); |
||
223 | } |
||
224 | |||
225 | |||
226 | public function testGetFileContentException() |
||
227 | { |
||
228 | $this->setExpectedException( \Aimeos\Controller\Common\Exception::class ); |
||
229 | $this->access( 'getFileContent' )->invokeArgs( $this->object, array( '', 'fs-media' ) ); |
||
230 | } |
||
231 | |||
232 | |||
233 | public function testGetFilePathOctet() |
||
234 | { |
||
235 | $result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'application/octet-stream' ) ); |
||
236 | $this->assertFalse( strpos( $result, '.' ) ); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
237 | } |
||
238 | |||
239 | |||
240 | public function testGetFilePathPDF() |
||
241 | { |
||
242 | $result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'application/pdf' ) ); |
||
243 | $this->assertEquals( '.pdf', substr( $result, -4 ) ); |
||
244 | } |
||
245 | |||
246 | |||
247 | public function testGetFilePathGIF() |
||
248 | { |
||
249 | $result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'image/gif' ) ); |
||
250 | $this->assertEquals( '.gif', substr( $result, -4 ) ); |
||
251 | } |
||
252 | |||
253 | |||
254 | public function testGetFilePathJPEG() |
||
255 | { |
||
256 | $result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'image/jpeg' ) ); |
||
257 | $this->assertEquals( '.jpg', substr( $result, -4 ) ); |
||
258 | } |
||
259 | |||
260 | |||
261 | public function testGetFilePathPNG() |
||
262 | { |
||
263 | $result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'image/png' ) ); |
||
264 | $this->assertEquals( '.png', substr( $result, -4 ) ); |
||
265 | } |
||
266 | |||
267 | |||
268 | public function testGetFilePathTIFF() |
||
269 | { |
||
270 | $result = $this->access( 'getFilePath' )->invokeArgs( $this->object, array( '', 'files', 'image/tiff' ) ); |
||
271 | $this->assertEquals( '.tif', substr( $result, -4 ) ); |
||
272 | } |
||
273 | |||
274 | |||
275 | public function testGetMediaFile() |
||
276 | { |
||
277 | $result = $this->access( 'getMediaFile' )->invokeArgs( $this->object, array( __FILE__ ) ); |
||
278 | $this->assertInstanceOf( \Aimeos\MW\Media\Iface::class, $result ); |
||
279 | } |
||
280 | |||
281 | |||
282 | public function testGetMimeIcon() |
||
283 | { |
||
284 | $result = $this->access( 'getMimeIcon' )->invokeArgs( $this->object, array( 'image/jpeg' ) ); |
||
285 | $this->assertContains( 'tmp/media/mimeicons/image/jpeg.png', $result ); |
||
286 | } |
||
287 | |||
288 | |||
289 | public function testGetMimeIconNoConfig() |
||
290 | { |
||
291 | $this->context->getConfig()->set( 'controller/common/media/standard/mimeicon/directory', '' ); |
||
292 | $result = $this->access( 'getMimeIcon' )->invokeArgs( $this->object, array( 'image/jpeg' ) ); |
||
293 | $this->assertEquals( '', $result ); |
||
294 | } |
||
295 | |||
296 | |||
297 | public function testGetMimeType() |
||
298 | { |
||
299 | $file = \Aimeos\MW\Media\Factory::get( __DIR__ . '/testfiles/test.png' ); |
||
300 | |||
301 | $result = $this->access( 'getMimeType' )->invokeArgs( $this->object, array( $file, 'files' ) ); |
||
302 | $this->assertEquals( 'image/png', $result ); |
||
303 | } |
||
304 | |||
305 | |||
306 | public function testGetMimeTypeNotAllowed() |
||
307 | { |
||
308 | $file = \Aimeos\MW\Media\Factory::get( __DIR__ . '/testfiles/test.gif' ); |
||
309 | $this->context->getConfig()->set( 'controller/common/media/standard/files/allowedtypes', array( 'image/jpeg' ) ); |
||
310 | |||
311 | $result = $this->access( 'getMimeType' )->invokeArgs( $this->object, array( $file, 'files' ) ); |
||
312 | $this->assertEquals( 'image/jpeg', $result ); |
||
313 | } |
||
314 | |||
315 | |||
316 | public function testGetMimeTypeNoTypes() |
||
317 | { |
||
318 | $file = \Aimeos\MW\Media\Factory::get( __DIR__ . '/testfiles/test.gif' ); |
||
319 | $this->context->getConfig()->set( 'controller/common/media/standard/files/allowedtypes', [] ); |
||
320 | |||
321 | $this->setExpectedException( \Aimeos\Controller\Common\Exception::class ); |
||
322 | $this->access( 'getMimeType' )->invokeArgs( $this->object, array( $file, 'files' ) ); |
||
323 | } |
||
324 | |||
325 | |||
326 | public function testScaleImage() |
||
327 | { |
||
328 | $file = \Aimeos\MW\Media\Factory::get( __DIR__ . '/testfiles/test.gif' ); |
||
329 | |||
330 | $this->access( 'scaleImage' )->invokeArgs( $this->object, array( $file, 'files' ) ); |
||
331 | } |
||
332 | |||
333 | |||
334 | public function testStoreFile() |
||
335 | { |
||
336 | $content = file_get_contents( __DIR__ . '/testfiles/test.gif' ); |
||
337 | |||
338 | $dest = dirname( dirname( dirname( __DIR__ ) ) ) . '/tmp/'; |
||
339 | if( !is_dir( $dest ) ) { mkdir( $dest, 0755, true ); } |
||
340 | copy( __DIR__ . '/testfiles/test.gif', $dest . 'test2.gif' ); |
||
341 | |||
342 | $this->access( 'storeFile' )->invokeArgs( $this->object, array( $content, 'fs-media', 'test.gif', 'test2.gif' ) ); |
||
343 | |||
344 | $this->assertFalse( file_exists( $dest . 'test2.gif' ) ); |
||
345 | $this->assertTrue( file_exists( $dest . 'test.gif' ) ); |
||
346 | |||
347 | unlink( $dest . 'test.gif' ); |
||
348 | } |
||
349 | |||
350 | |||
351 | protected function access( $name ) |
||
352 | { |
||
353 | $class = new \ReflectionClass( \Aimeos\Controller\Common\Media\Standard::class ); |
||
354 | $method = $class->getMethod( $name ); |
||
355 | $method->setAccessible( true ); |
||
356 | |||
357 | return $method; |
||
358 | } |
||
359 | } |
||
360 |