1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the eZ Publish Kernel package. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\IO\Tests; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException; |
12
|
|
|
use eZ\Publish\Core\IO\IOService; |
13
|
|
|
use eZ\Publish\Core\IO\IOBinarydataHandler; |
14
|
|
|
use eZ\Publish\Core\IO\IOMetadataHandler; |
15
|
|
|
use eZ\Publish\Core\IO\Values\BinaryFile; |
16
|
|
|
use eZ\Publish\Core\IO\Values\BinaryFileCreateStruct; |
17
|
|
|
use eZ\Publish\SPI\IO\BinaryFile as SPIBinaryFile; |
18
|
|
|
use eZ\Publish\SPI\IO\MimeTypeDetector; |
19
|
|
|
use PHPUnit\Framework\TestCase; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Test case for IO Service. |
23
|
|
|
*/ |
24
|
|
|
class IOServiceTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
const PREFIX = 'test-prefix'; |
27
|
|
|
|
28
|
|
|
/** @var IOService */ |
29
|
|
|
protected $IOService; |
30
|
|
|
|
31
|
|
|
/** @var \eZ\Publish\Core\IO\IOMetadataHandler|\PHPUnit\Framework\MockObject\MockObject */ |
32
|
|
|
protected $metadataHandlerMock; |
33
|
|
|
|
34
|
|
|
/** @var \eZ\Publish\Core\IO\IOBinarydataHandler|\PHPUnit\Framework\MockObject\MockObject */ |
35
|
|
|
protected $binarydataHandlerMock; |
36
|
|
|
|
37
|
|
|
/** @var MimeTypeDetector|\PHPUnit\Framework\MockObject\MockObject */ |
38
|
|
|
protected $mimeTypeDetectorMock; |
39
|
|
|
|
40
|
|
|
public function setUp() |
41
|
|
|
{ |
42
|
|
|
parent::setUp(); |
43
|
|
|
|
44
|
|
|
$this->binarydataHandlerMock = $this->createMock(IOBinarydataHandler::class); |
45
|
|
|
$this->metadataHandlerMock = $this->createMock(IOMetadataHandler::class); |
46
|
|
|
$this->mimeTypeDetectorMock = $this->createMock(MimeTypeDetector::class); |
47
|
|
|
|
48
|
|
|
$this->IOService = new IOService( |
49
|
|
|
$this->metadataHandlerMock, |
50
|
|
|
$this->binarydataHandlerMock, |
51
|
|
|
$this->mimeTypeDetectorMock, |
52
|
|
|
['prefix' => self::PREFIX] |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Test creating new BinaryCreateStruct from uploaded file. |
58
|
|
|
* |
59
|
|
|
* @covers \eZ\Publish\Core\IO\IOService::newBinaryCreateStructFromUploadedFile |
60
|
|
|
*/ |
61
|
|
|
public function testNewBinaryCreateStructFromUploadedFile() |
62
|
|
|
{ |
63
|
|
|
self::markTestSkipped('Test skipped as it seems to depend on php-cgi'); |
64
|
|
|
$uploadTest = $this->getFileUploadTest(); |
|
|
|
|
65
|
|
|
$result = $uploadTest->run(); // Fails because of unset cgi param and missing php-cgi exe |
66
|
|
|
// Params bellow makes the code execute but fails: |
67
|
|
|
//->run( null, array( 'cgi' => 'php' ) ); |
68
|
|
|
|
69
|
|
|
if ($result->failureCount() > 0) { |
70
|
|
|
self::fail( |
71
|
|
|
'Failed file upload test, failureCount() > 0: ' . |
72
|
|
|
$this->expandFailureMessages($result->failures()) |
|
|
|
|
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($result->errorCount() > 0) { |
77
|
|
|
self::fail( |
78
|
|
|
'Failed file upload test, errorCount() > 0: ' . |
79
|
|
|
$this->expandFailureMessages($result->errors()) |
|
|
|
|
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($result->skippedCount() > 0) { |
84
|
|
|
self::fail( |
85
|
|
|
'Failed file upload test, skippedCount() > 0: ' . |
86
|
|
|
$this->expandFailureMessages($result->skipped()) |
|
|
|
|
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @covers \eZ\Publish\Core\IO\IOService::newBinaryCreateStructFromUploadedFile |
93
|
|
|
*/ |
94
|
|
|
public function testNewBinaryCreateStructFromLocalFile() |
95
|
|
|
{ |
96
|
|
|
$file = __FILE__; |
97
|
|
|
|
98
|
|
|
$this->mimeTypeDetectorMock |
99
|
|
|
->expects($this->once()) |
100
|
|
|
->method('getFromPath') |
101
|
|
|
->with($this->equalTo($file)) |
102
|
|
|
->will($this->returnValue('text/x-php')); |
103
|
|
|
|
104
|
|
|
$binaryCreateStruct = $this->getIOService()->newBinaryCreateStructFromLocalFile( |
105
|
|
|
$file |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
self::assertInstanceOf(BinaryFileCreateStruct::class, $binaryCreateStruct); |
109
|
|
|
self::assertNull($binaryCreateStruct->id); |
110
|
|
|
self::assertTrue(is_resource($binaryCreateStruct->inputStream)); |
111
|
|
|
self::assertEquals(filesize(__FILE__), $binaryCreateStruct->size); |
112
|
|
|
self::assertEquals('text/x-php', $binaryCreateStruct->mimeType); |
113
|
|
|
|
114
|
|
|
return $binaryCreateStruct; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @covers \eZ\Publish\Core\IO\IOService::createBinaryFile |
119
|
|
|
* @covers \eZ\Publish\Core\IO\IOService::buildSPIBinaryFileCreateStructObject |
120
|
|
|
* @covers \eZ\Publish\Core\IO\IOService::buildDomainBinaryFileObject |
121
|
|
|
* @depends testNewBinaryCreateStructFromLocalFile |
122
|
|
|
*/ |
123
|
|
|
public function testCreateBinaryFile(BinaryFileCreateStruct $createStruct) |
124
|
|
|
{ |
125
|
|
|
$createStruct->id = 'my/path.php'; |
126
|
|
|
$id = $this->getPrefixedUri($createStruct->id); |
127
|
|
|
|
128
|
|
|
$spiBinaryFile = new SPIBinaryFile(); |
129
|
|
|
$spiBinaryFile->id = $id; |
130
|
|
|
$spiBinaryFile->uri = $id; |
131
|
|
|
$spiBinaryFile->size = filesize(__FILE__); |
132
|
|
|
$spiBinaryFile->mimeType = 'text/x-php'; |
133
|
|
|
|
134
|
|
|
$this->binarydataHandlerMock |
135
|
|
|
->expects($this->once()) |
136
|
|
|
->method('create') |
137
|
|
|
->with( |
138
|
|
|
$this->callback( |
139
|
|
|
function ($subject) use ($id) { |
140
|
|
|
if (!$subject instanceof \eZ\Publish\SPI\IO\BinaryFileCreateStruct) { |
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $subject->id == $id; |
145
|
|
|
} |
146
|
|
|
) |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
$this->metadataHandlerMock |
150
|
|
|
->expects($this->once()) |
151
|
|
|
->method('create') |
152
|
|
|
->with($this->callback($this->getSPIBinaryFileCreateStructCallback($id))) |
153
|
|
|
->will($this->returnValue($spiBinaryFile)); |
154
|
|
|
|
155
|
|
|
$binaryFile = $this->IOService->createBinaryFile($createStruct); |
156
|
|
|
self::assertInstanceOf(BinaryFile::class, $binaryFile); |
157
|
|
|
self::assertEquals($createStruct->id, $binaryFile->id); |
158
|
|
|
self::assertEquals($createStruct->size, $binaryFile->size); |
159
|
|
|
|
160
|
|
|
return $binaryFile; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @covers \eZ\Publish\Core\IO\IOService::loadBinaryFile |
165
|
|
|
*/ |
166
|
|
|
public function testLoadBinaryFile() |
167
|
|
|
{ |
168
|
|
|
$id = 'my/path.png'; |
169
|
|
|
$spiId = $this->getPrefixedUri($id); |
170
|
|
|
$spiBinaryFile = new SPIBinaryFile(); |
171
|
|
|
$spiBinaryFile->id = $spiId; |
172
|
|
|
$spiBinaryFile->size = 12345; |
173
|
|
|
$spiBinaryFile->mimeType = 'application/any'; |
174
|
|
|
$spiBinaryFile->uri = $spiId; |
175
|
|
|
|
176
|
|
|
$this->metadataHandlerMock |
177
|
|
|
->expects($this->once()) |
178
|
|
|
->method('load') |
179
|
|
|
->with($spiId) |
180
|
|
|
->will($this->returnValue($spiBinaryFile)); |
181
|
|
|
|
182
|
|
|
$binaryFile = $this->getIOService()->loadBinaryFile($id); |
183
|
|
|
self::assertEquals($id, $binaryFile->id); |
184
|
|
|
|
185
|
|
|
return $binaryFile; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @covers \eZ\Publish\Core\IO\IOService::loadBinaryFile |
190
|
|
|
*/ |
191
|
|
|
public function testLoadBinaryFileNoMetadataUri() |
192
|
|
|
{ |
193
|
|
|
$id = 'my/path.png'; |
194
|
|
|
$spiId = $this->getPrefixedUri($id); |
195
|
|
|
$spiBinaryFile = new SPIBinaryFile(); |
196
|
|
|
$spiBinaryFile->id = $spiId; |
197
|
|
|
$spiBinaryFile->size = 12345; |
198
|
|
|
|
199
|
|
|
$this->metadataHandlerMock |
200
|
|
|
->expects($this->once()) |
201
|
|
|
->method('load') |
202
|
|
|
->with($spiId) |
203
|
|
|
->will($this->returnValue($spiBinaryFile)); |
204
|
|
|
|
205
|
|
|
$this->binarydataHandlerMock |
206
|
|
|
->expects($this->once()) |
207
|
|
|
->method('getUri') |
208
|
|
|
->with($spiId) |
209
|
|
|
->will($this->returnValue("/$spiId")); |
210
|
|
|
|
211
|
|
|
$binaryFile = $this->getIOService()->loadBinaryFile($id); |
212
|
|
|
|
213
|
|
|
$expectedBinaryFile = new BinaryFile(['id' => $id, 'size' => 12345, 'uri' => "/$spiId"]); |
214
|
|
|
|
215
|
|
|
self::assertEquals($expectedBinaryFile, $binaryFile); |
216
|
|
|
|
217
|
|
|
return $binaryFile; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @covers \eZ\Publish\Core\IO\IOService::loadBinaryFile |
222
|
|
|
* @expectedException \eZ\Publish\Core\Base\Exceptions\NotFoundException |
223
|
|
|
* |
224
|
|
|
* @return mixed Whatever loadBinaryFile returns |
225
|
|
|
*/ |
226
|
|
View Code Duplication |
public function testLoadBinaryFileNotFound() |
227
|
|
|
{ |
228
|
|
|
$id = 'id.ext'; |
229
|
|
|
$prefixedUri = $this->getPrefixedUri($id); |
230
|
|
|
$this->metadataHandlerMock |
231
|
|
|
->expects($this->once()) |
232
|
|
|
->method('load') |
233
|
|
|
->with($prefixedUri) |
234
|
|
|
->will($this->throwException(new BinaryFileNotFoundException($prefixedUri))); |
235
|
|
|
|
236
|
|
|
return $this->getIOService()->loadBinaryFile($id); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function testLoadBinaryFileByUri() |
240
|
|
|
{ |
241
|
|
|
$id = 'my/path.png'; |
242
|
|
|
$spiId = $this->getPrefixedUri($id); |
243
|
|
|
$spiBinaryFile = new SPIBinaryFile(); |
244
|
|
|
$spiBinaryFile->id = $spiId; |
245
|
|
|
$spiBinaryFile->size = 12345; |
246
|
|
|
$spiBinaryFile->mimeType = 'application/any'; |
247
|
|
|
$spiBinaryFile->uri = $spiId; |
248
|
|
|
|
249
|
|
|
$this->binarydataHandlerMock |
250
|
|
|
->expects($this->once()) |
251
|
|
|
->method('getIdFromUri') |
252
|
|
|
->with($spiId) |
253
|
|
|
->will($this->returnValue($spiId)); |
254
|
|
|
|
255
|
|
|
$this->metadataHandlerMock |
256
|
|
|
->expects($this->once()) |
257
|
|
|
->method('load') |
258
|
|
|
->with($spiId) |
259
|
|
|
->will($this->returnValue($spiBinaryFile)); |
260
|
|
|
|
261
|
|
|
$binaryFile = $this->getIOService()->loadBinaryFileByUri($spiId); |
262
|
|
|
self::assertEquals($id, $binaryFile->id); |
263
|
|
|
|
264
|
|
|
return $binaryFile; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return mixed Whatever loadBinaryFileByUri returns |
269
|
|
|
* @expectedException \eZ\Publish\Core\Base\Exceptions\NotFoundException |
270
|
|
|
*/ |
271
|
|
|
public function testLoadBinaryFileByUriNotFound() |
272
|
|
|
{ |
273
|
|
|
$id = 'my/path.png'; |
274
|
|
|
$spiId = $this->getPrefixedUri($id); |
275
|
|
|
|
276
|
|
|
$this->binarydataHandlerMock |
277
|
|
|
->expects($this->once()) |
278
|
|
|
->method('getIdFromUri') |
279
|
|
|
->with($spiId) |
280
|
|
|
->will($this->returnValue($spiId)); |
281
|
|
|
|
282
|
|
|
$this->metadataHandlerMock |
283
|
|
|
->expects($this->once()) |
284
|
|
|
->method('load') |
285
|
|
|
->with($spiId) |
286
|
|
|
->will($this->throwException(new BinaryFileNotFoundException($spiId))); |
287
|
|
|
|
288
|
|
|
return $this->getIOService()->loadBinaryFileByUri($spiId); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @covers \eZ\Publish\Core\IO\IOService::getFileInputStream |
293
|
|
|
* @depends testCreateBinaryFile |
294
|
|
|
*/ |
295
|
|
|
public function testGetFileInputStream(BinaryFile $binaryFile) |
|
|
|
|
296
|
|
|
{ |
297
|
|
|
self::markTestSkipped('Not implemented'); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @depends testLoadBinaryFile |
302
|
|
|
* @covers \eZ\Publish\Core\IO\IOService::getFileContents |
303
|
|
|
*/ |
304
|
|
|
public function testGetFileContents(BinaryFile $binaryFile) |
305
|
|
|
{ |
306
|
|
|
$expectedContents = file_get_contents(__FILE__); |
307
|
|
|
|
308
|
|
|
$this->binarydataHandlerMock |
309
|
|
|
->expects($this->once()) |
310
|
|
|
->method('getContents') |
311
|
|
|
->with($this->equalTo($this->getPrefixedUri($binaryFile->id))) |
312
|
|
|
->will($this->returnValue($expectedContents)); |
313
|
|
|
|
314
|
|
|
self::assertEquals( |
315
|
|
|
$expectedContents, |
316
|
|
|
$this->getIOService()->getFileContents($binaryFile) |
317
|
|
|
); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @depends testCreateBinaryFile |
322
|
|
|
* @covers \eZ\Publish\Core\IO\IOService::exists() |
323
|
|
|
*/ |
324
|
|
|
public function testExists(BinaryFile $binaryFile) |
325
|
|
|
{ |
326
|
|
|
$this->metadataHandlerMock |
327
|
|
|
->expects($this->once()) |
328
|
|
|
->method('exists') |
329
|
|
|
->with($this->equalTo($this->getPrefixedUri($binaryFile->id))) |
330
|
|
|
->will($this->returnValue(true)); |
331
|
|
|
|
332
|
|
|
self::assertTrue( |
333
|
|
|
$this->getIOService()->exists( |
334
|
|
|
$binaryFile->id |
335
|
|
|
) |
336
|
|
|
); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @covers \eZ\Publish\Core\IO\IOService::exists() |
341
|
|
|
*/ |
342
|
|
|
public function testExistsNot() |
343
|
|
|
{ |
344
|
|
|
$this->metadataHandlerMock |
345
|
|
|
->expects($this->once()) |
346
|
|
|
->method('exists') |
347
|
|
|
->with($this->equalTo($this->getPrefixedUri(__METHOD__))) |
348
|
|
|
->will($this->returnValue(false)); |
349
|
|
|
|
350
|
|
|
self::assertFalse( |
351
|
|
|
$this->getIOService()->exists( |
352
|
|
|
__METHOD__ |
353
|
|
|
) |
354
|
|
|
); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* @depends testCreateBinaryFile |
359
|
|
|
* @covers \eZ\Publish\Core\IO\IOService::getMimeType() |
360
|
|
|
*/ |
361
|
|
|
public function testGetMimeType(BinaryFile $binaryFile) |
362
|
|
|
{ |
363
|
|
|
$this->metadataHandlerMock |
364
|
|
|
->expects($this->once()) |
365
|
|
|
->method('getMimeType') |
366
|
|
|
->with($this->equalTo($this->getPrefixedUri($binaryFile->id))) |
367
|
|
|
->will($this->returnValue($binaryFile->mimeType)); |
368
|
|
|
|
369
|
|
|
self::assertEquals( |
370
|
|
|
$binaryFile->mimeType, |
371
|
|
|
$this->getIOService()->getMimeType( |
372
|
|
|
$binaryFile->id |
373
|
|
|
) |
374
|
|
|
); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @covers \eZ\Publish\Core\IO\IOService::deleteBinaryFile |
379
|
|
|
* @depends testCreateBinaryFile |
380
|
|
|
*/ |
381
|
|
|
public function testDeleteBinaryFile(BinaryFile $binaryFile) |
382
|
|
|
{ |
383
|
|
|
$this->metadataHandlerMock |
384
|
|
|
->expects($this->once()) |
385
|
|
|
->method('delete') |
386
|
|
|
->with($this->equalTo($this->getPrefixedUri($binaryFile->id))); |
387
|
|
|
|
388
|
|
|
$this->binarydataHandlerMock |
389
|
|
|
->expects($this->once()) |
390
|
|
|
->method('delete') |
391
|
|
|
->with($this->equalTo($this->getPrefixedUri($binaryFile->id))); |
392
|
|
|
|
393
|
|
|
$this->getIOService()->deleteBinaryFile($binaryFile); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* @covers \eZ\Publish\Core\IO\IOService::deleteDirectory() |
398
|
|
|
*/ |
399
|
|
|
public function testDeleteDirectory() |
400
|
|
|
{ |
401
|
|
|
$id = 'some/directory'; |
402
|
|
|
$spiId = $this->getPrefixedUri($id); |
403
|
|
|
|
404
|
|
|
$this->binarydataHandlerMock |
405
|
|
|
->expects($this->once()) |
406
|
|
|
->method('deleteDirectory') |
407
|
|
|
->with($spiId); |
408
|
|
|
|
409
|
|
|
$this->metadataHandlerMock |
410
|
|
|
->expects($this->once()) |
411
|
|
|
->method('deleteDirectory') |
412
|
|
|
->with($spiId); |
413
|
|
|
|
414
|
|
|
$this->getIOService()->deleteDirectory('some/directory'); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* @covers \eZ\Publish\Core\IO\IOService::deleteBinaryFile |
419
|
|
|
* @expectedException \eZ\Publish\Core\Base\Exceptions\NotFoundException |
420
|
|
|
* |
421
|
|
|
* @return mixed Whatever deleteBinaryFile returned |
422
|
|
|
*/ |
423
|
|
View Code Duplication |
public function testDeleteBinaryFileNotFound() |
424
|
|
|
{ |
425
|
|
|
$binaryFile = new BinaryFile( |
426
|
|
|
['id' => __METHOD__] |
427
|
|
|
); |
428
|
|
|
|
429
|
|
|
$prefixedId = $this->getPrefixedUri($binaryFile->id); |
430
|
|
|
$this->metadataHandlerMock |
431
|
|
|
->expects($this->once()) |
432
|
|
|
->method('delete') |
433
|
|
|
->with($this->equalTo($prefixedId)) |
434
|
|
|
->will($this->throwException(new BinaryFileNotFoundException($prefixedId))); |
435
|
|
|
|
436
|
|
|
$this->getIOService()->deleteBinaryFile($binaryFile); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
public function getPrefixedUri($uri) |
440
|
|
|
{ |
441
|
|
|
return self::PREFIX . '/' . $uri; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* @return \eZ\Publish\Core\IO\IOService |
446
|
|
|
*/ |
447
|
|
|
protected function getIOService() |
448
|
|
|
{ |
449
|
|
|
return $this->IOService; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* Asserts that the given $ioCreateStruct is of the right type and that id matches the expected value. |
454
|
|
|
* |
455
|
|
|
* @param $ioCreateStruct |
456
|
|
|
* |
457
|
|
|
* @return bool |
458
|
|
|
*/ |
459
|
|
|
private function getSPIBinaryFileCreateStructCallback($spiId) |
460
|
|
|
{ |
461
|
|
|
return function ($subject) use ($spiId) { |
462
|
|
|
if (!$subject instanceof \eZ\Publish\SPI\IO\BinaryFileCreateStruct) { |
463
|
|
|
return false; |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
return $subject->id == $spiId; |
467
|
|
|
}; |
468
|
|
|
} |
469
|
|
|
} |
470
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.