|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Test suite for the FileManager |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\FileBundle\Tests; |
|
7
|
|
|
|
|
8
|
|
|
use Graviton\FileBundle\Manager\FileManager; |
|
9
|
|
|
use Graviton\TestBundle\Test\RestTestCase; |
|
10
|
|
|
use GravitonDyn\FileBundle\Document\File; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\File\File as SfFile; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Basic functional test for /file |
|
17
|
|
|
* |
|
18
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
19
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
20
|
|
|
* @link http://swisscom.ch |
|
21
|
|
|
*/ |
|
22
|
|
|
class FileManagerTest extends RestTestCase |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var FileManager $fileManager */ |
|
25
|
|
|
private $fileManager; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Initiates mandatory properties |
|
29
|
|
|
* |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
|
|
public function setUp() |
|
33
|
|
|
{ |
|
34
|
|
|
if (!$this->fileManager) { |
|
35
|
|
|
$this->fileManager = $this->getContainer()->get('graviton.file.file_manager'); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Verifies the correct behavior of the FileManager |
|
42
|
|
|
* |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
|
|
public function testSaveFiles() |
|
46
|
|
|
{ |
|
47
|
|
|
$jsonData = '{ |
|
48
|
|
|
"links": [ |
|
49
|
|
|
{ |
|
50
|
|
|
"$ref": "http://localhost/testcase/readonly/101", |
|
51
|
|
|
"type": "owner" |
|
52
|
|
|
}, |
|
53
|
|
|
{ |
|
54
|
|
|
"$ref": "http://localhost/testcase/readonly/102", |
|
55
|
|
|
"type": "module" |
|
56
|
|
|
} |
|
57
|
|
|
], |
|
58
|
|
|
"metadata": { |
|
59
|
|
|
"action":[{"command":"print"},{"command":"archive"}] |
|
60
|
|
|
} |
|
61
|
|
|
}'; |
|
62
|
|
|
|
|
63
|
|
|
$uploadedFile = $this->getUploadFile('test.txt'); |
|
64
|
|
|
$client = $this->createClient(); |
|
65
|
|
|
$client->request( |
|
66
|
|
|
'POST', |
|
67
|
|
|
'/file', |
|
68
|
|
|
[ |
|
69
|
|
|
'metadata' => $jsonData, |
|
70
|
|
|
], |
|
71
|
|
|
[ |
|
72
|
|
|
'upload' => $uploadedFile, |
|
73
|
|
|
] |
|
74
|
|
|
); |
|
75
|
|
|
$response = $client->getResponse(); |
|
76
|
|
|
$location = $response->headers->get('location'); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode()); |
|
79
|
|
|
$this->assertContains('/file/', $location); |
|
80
|
|
|
|
|
81
|
|
|
// receive generated file information |
|
82
|
|
|
$client = $this->createClient(); |
|
83
|
|
|
$client->request('GET', $location, [], [], ['HTTP_ACCEPT' => 'application/json']); |
|
84
|
|
|
|
|
85
|
|
|
$response = $client->getResponse(); |
|
86
|
|
|
$contentArray = json_decode($response->getContent(), true); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertEquals([["command" => "print"], ["command" => "archive"]], $contentArray['metadata']['action']); |
|
89
|
|
|
$this->assertJsonStringEqualsJsonString( |
|
90
|
|
|
'[ |
|
91
|
|
|
{ |
|
92
|
|
|
"$ref": "http://localhost/testcase/readonly/101", |
|
93
|
|
|
"type": "owner" |
|
94
|
|
|
}, |
|
95
|
|
|
{ |
|
96
|
|
|
"$ref": "http://localhost/testcase/readonly/102", |
|
97
|
|
|
"type": "module" |
|
98
|
|
|
} |
|
99
|
|
|
]', |
|
100
|
|
|
json_encode($contentArray['links']) |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
|
|
// Check contain data |
|
104
|
|
|
$this->assertArrayHasKey('modificationDate', $contentArray['metadata']); |
|
105
|
|
|
$this->assertArrayHasKey('createDate', $contentArray['metadata']); |
|
106
|
|
|
|
|
107
|
|
|
// Test Metadata, and Remove date. |
|
108
|
|
|
unset($contentArray['metadata']['modificationDate']); |
|
109
|
|
|
unset($contentArray['metadata']['createDate']); |
|
110
|
|
|
|
|
111
|
|
|
$this->assertJsonStringEqualsJsonString( |
|
112
|
|
|
'{ |
|
113
|
|
|
"size":16, |
|
114
|
|
|
"action":[ |
|
115
|
|
|
{"command":"print"}, |
|
116
|
|
|
{"command":"archive"} |
|
117
|
|
|
], |
|
118
|
|
|
"mime":"text\/plain", |
|
119
|
|
|
"filename":"test.txt", |
|
120
|
|
|
"hash":"4f3cbec0e58903d8bdcbd03d283cf43ed49a95d8d8b341ee38c0ba085204e2d5", |
|
121
|
|
|
"additionalProperties":[] |
|
122
|
|
|
}', |
|
123
|
|
|
json_encode($contentArray['metadata']) |
|
124
|
|
|
); |
|
125
|
|
|
|
|
126
|
|
|
// clean up |
|
127
|
|
|
$client = $this->createClient(); |
|
128
|
|
|
$client->request( |
|
129
|
|
|
'DELETE', |
|
130
|
|
|
$location |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Verifies the correct behavior of the FileManager |
|
137
|
|
|
* |
|
138
|
|
|
* @return void |
|
139
|
|
|
*/ |
|
140
|
|
|
public function testUpdateFiles() |
|
141
|
|
|
{ |
|
142
|
|
|
$timeFormat = 'Y-m-d\TH:i:sP'; |
|
143
|
|
|
|
|
144
|
|
|
$jsonData = '{ |
|
145
|
|
|
"links": [ |
|
146
|
|
|
{ |
|
147
|
|
|
"$ref": "http://localhost/testcase/readonly/101", |
|
148
|
|
|
"type": "owner" |
|
149
|
|
|
}, |
|
150
|
|
|
{ |
|
151
|
|
|
"$ref": "http://localhost/testcase/readonly/102", |
|
152
|
|
|
"type": "module" |
|
153
|
|
|
} |
|
154
|
|
|
], |
|
155
|
|
|
"metadata": { |
|
156
|
|
|
"action":[{"command":"print"},{"command":"archive"}] |
|
157
|
|
|
} |
|
158
|
|
|
}'; |
|
159
|
|
|
|
|
160
|
|
|
$uploadedFile = $this->getUploadFile('test.txt'); |
|
161
|
|
|
$client = $this->createClient(); |
|
162
|
|
|
$client->request( |
|
163
|
|
|
'POST', |
|
164
|
|
|
'/file', |
|
165
|
|
|
[ |
|
166
|
|
|
'metadata' => $jsonData, |
|
167
|
|
|
], |
|
168
|
|
|
[ |
|
169
|
|
|
'upload' => $uploadedFile, |
|
170
|
|
|
] |
|
171
|
|
|
); |
|
172
|
|
|
$response = $client->getResponse(); |
|
173
|
|
|
$location = $response->headers->get('location'); |
|
174
|
|
|
|
|
175
|
|
|
$this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode()); |
|
176
|
|
|
$this->assertContains('/file/', $location); |
|
177
|
|
|
|
|
178
|
|
|
// receive generated file information |
|
179
|
|
|
$client = $this->createClient(); |
|
180
|
|
|
$client->request('GET', $location, [], [], ['HTTP_ACCEPT' => 'application/json']); |
|
181
|
|
|
|
|
182
|
|
|
$response = $client->getResponse(); |
|
183
|
|
|
$contentArray = json_decode($response->getContent(), true); |
|
184
|
|
|
|
|
185
|
|
|
// Check contain data |
|
186
|
|
|
$this->assertArrayHasKey('modificationDate', $contentArray['metadata']); |
|
187
|
|
|
$this->assertArrayHasKey('createDate', $contentArray['metadata']); |
|
188
|
|
|
// Test Metadata, and Remove date. |
|
189
|
|
|
$originalAt = \DateTime::createFromFormat($timeFormat, $contentArray['metadata']['modificationDate']); |
|
190
|
|
|
unset($contentArray['metadata']['modificationDate']); |
|
191
|
|
|
unset($contentArray['metadata']['createDate']); |
|
192
|
|
|
|
|
193
|
|
|
// PUT Lets UPDATE some additional Params |
|
194
|
|
|
$client = $this->createClient(); |
|
195
|
|
|
$value = new \stdClass(); |
|
196
|
|
|
$value->name = 'aField'; |
|
197
|
|
|
$value->value = 'aValue'; |
|
198
|
|
|
$contentArray['metadata']['additionalProperties'] = [$value]; |
|
199
|
|
|
sleep(1); |
|
200
|
|
|
$client->request( |
|
201
|
|
|
'PUT', |
|
202
|
|
|
$location, |
|
203
|
|
|
[ |
|
204
|
|
|
'metadata' => json_encode($contentArray), |
|
205
|
|
|
] |
|
206
|
|
|
); |
|
207
|
|
|
$response = $client->getResponse(); |
|
208
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode(), $response->getContent()); |
|
209
|
|
|
|
|
210
|
|
|
$client = $this->createClient(); |
|
211
|
|
|
$client->request('GET', $location, [], [], ['HTTP_ACCEPT' => 'application/json']); |
|
212
|
|
|
|
|
213
|
|
|
$response = $client->getResponse(); |
|
214
|
|
|
$contentUpdatedArray = json_decode($response->getContent(), true); |
|
215
|
|
|
$modifiedAt = \DateTime::createFromFormat($timeFormat, $contentUpdatedArray['metadata']['modificationDate']); |
|
216
|
|
|
|
|
217
|
|
|
$this->assertTrue($modifiedAt > $originalAt, 'File put should have changed modification date and did not'); |
|
|
|
|
|
|
218
|
|
|
|
|
219
|
|
|
// PATCH Lets patch, and time should be changed |
|
220
|
|
|
$value->value = 'bValue'; |
|
221
|
|
|
$patchJson = json_encode( |
|
222
|
|
|
array( |
|
223
|
|
|
'op' => 'replace', |
|
224
|
|
|
'path' => '/metadata/additionalProperties', |
|
225
|
|
|
'value' => [$value] |
|
226
|
|
|
) |
|
227
|
|
|
); |
|
228
|
|
|
sleep(1); |
|
229
|
|
|
$client = $this->createClient(); |
|
230
|
|
|
$client->request('PATCH', $location, array(), array(), array(), $patchJson); |
|
231
|
|
|
$response = $client->getResponse(); |
|
232
|
|
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); |
|
233
|
|
|
|
|
234
|
|
|
$client->request('GET', $location, [], [], ['HTTP_ACCEPT' => 'application/json']); |
|
235
|
|
|
$response = $client->getResponse(); |
|
236
|
|
|
$contentPatchedArray = json_decode($response->getContent(), true); |
|
237
|
|
|
$pacthedAt = \DateTime::createFromFormat($timeFormat, $contentPatchedArray['metadata']['modificationDate']); |
|
238
|
|
|
$this->assertTrue($pacthedAt > $modifiedAt, 'File patched should have changed modification date and did not'); |
|
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
// clean up |
|
241
|
|
|
$client = $this->createClient(); |
|
242
|
|
|
$client->request( |
|
243
|
|
|
'DELETE', |
|
244
|
|
|
$location |
|
245
|
|
|
); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* validIdRequest in File Manager, should not be valid to post or put |
|
251
|
|
|
* |
|
252
|
|
|
* @return void |
|
253
|
|
|
*/ |
|
254
|
|
|
public function testPostOrPutIdvalidIdRequestError() |
|
255
|
|
|
{ |
|
256
|
|
|
$document = new File(); |
|
257
|
|
|
|
|
258
|
|
|
$shouldFailArray = [ |
|
259
|
|
|
'fail-not-equal-0' => ['file-id', 'request-id'], |
|
260
|
|
|
'fail-not-equal-1' => ['' , 'request-id'], |
|
261
|
|
|
'fail-not-equal-2' => ['file-id', ''] |
|
262
|
|
|
]; |
|
263
|
|
|
|
|
264
|
|
|
foreach ($shouldFailArray as $name => $values) { |
|
265
|
|
|
$document->setId($values[0]); |
|
266
|
|
|
$requestId = $values[1]; |
|
267
|
|
|
|
|
268
|
|
|
$method = $this->getPrivateClassMethod(get_class($this->fileManager), 'validIdRequest'); |
|
269
|
|
|
$result = $method->invokeArgs($this->fileManager, [$document, $requestId]); |
|
270
|
|
|
|
|
271
|
|
|
$this->assertFalse($result, $name); |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* To test standard file upload, just posting a file |
|
277
|
|
|
* |
|
278
|
|
|
* @return void |
|
279
|
|
|
*/ |
|
280
|
|
|
public function testNormalDirectUpload() |
|
281
|
|
|
{ |
|
282
|
|
|
$upload = $this->getUploadFile('test-file.txt'); |
|
283
|
|
|
$client = $this->createClient(); |
|
284
|
|
|
|
|
285
|
|
|
$client->request('POST', '/file', [], [$upload]); |
|
286
|
|
|
$response = $client->getResponse(); |
|
287
|
|
|
|
|
288
|
|
|
$this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode(), $response->getContent()); |
|
289
|
|
|
$location = $response->headers->get('location'); |
|
290
|
|
|
|
|
291
|
|
|
// Lets get content as JSON |
|
292
|
|
|
$client->request('GET', $location, [], [], ['HTTP_ACCEPT' => 'application/json']); |
|
293
|
|
|
$response = $client->getResponse(); |
|
294
|
|
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode(), $response->getContent()); |
|
295
|
|
|
$contentArray = json_decode($response->getContent(), true); |
|
296
|
|
|
|
|
297
|
|
|
// Check contain data |
|
298
|
|
|
$this->assertArrayHasKey('modificationDate', $contentArray['metadata']); |
|
299
|
|
|
$this->assertArrayHasKey('createDate', $contentArray['metadata']); |
|
300
|
|
|
$this->assertArrayHasKey('mime', $contentArray['metadata']); |
|
301
|
|
|
$this->assertArrayHasKey('hash', $contentArray['metadata']); |
|
302
|
|
|
$this->assertEquals('text/plain', $contentArray['metadata']['mime']); |
|
303
|
|
|
$this->assertEquals('test-file.txt', $contentArray['metadata']['filename']); |
|
304
|
|
|
|
|
305
|
|
|
$client->request('DELETE', $location, [], [], ['HTTP_ACCEPT' => 'application/json']); |
|
306
|
|
|
$response = $client->getResponse(); |
|
307
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode(), $response->getContent()); |
|
308
|
|
|
|
|
309
|
|
|
$client->request('DELETE', $location, [], [], ['HTTP_ACCEPT' => 'application/json']); |
|
310
|
|
|
$response = $client->getResponse(); |
|
311
|
|
|
$this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode(), $response->getContent()); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* Simple function to create a upload file |
|
316
|
|
|
* |
|
317
|
|
|
* @param string $fileName desired new filename |
|
318
|
|
|
* @return UploadedFile |
|
319
|
|
|
*/ |
|
320
|
|
|
private function getUploadFile($fileName) |
|
321
|
|
|
{ |
|
322
|
|
|
$newDir = sys_get_temp_dir() . '/'. $fileName; |
|
323
|
|
|
copy(__DIR__ . '/Fixtures/test.txt', $newDir); |
|
324
|
|
|
$file = new SfFile($newDir); |
|
325
|
|
|
return new UploadedFile( |
|
326
|
|
|
$file->getRealPath(), |
|
327
|
|
|
$fileName, |
|
328
|
|
|
$file->getMimeType(), |
|
329
|
|
|
$file->getSize() |
|
330
|
|
|
); |
|
331
|
|
|
} |
|
332
|
|
|
} |
|
333
|
|
|
|