Completed
Push — feature/EVO-10275-file-hash-le... ( 4522d8 )
by
unknown
13:02
created

testPutNewFileViaFormHashToLong()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 57
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 57
rs 9.6818
cc 1
eloc 27
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * functional test for /file
4
 */
5
6
namespace Graviton\FileBundle\Tests\Controller;
7
8
use Graviton\TestBundle\Test\RestTestCase;
9
use Symfony\Component\HttpFoundation\File\UploadedFile;
10
use Symfony\Component\HttpFoundation\Response;
11
12
/**
13
 * Basic functional test for /file
14
 *
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @link     http://swisscom.ch
18
 */
19
class FileControllerTest extends RestTestCase
20
{
21
    /**
22
     * @const complete content type string expected on a resource
23
     */
24
    const CONTENT_TYPE = 'application/json; charset=UTF-8; profile=http://localhost/schema/file/item';
25
26
    /**
27
     * @const corresponding vendorized schema mime type
28
     */
29
    const COLLECTION_TYPE = 'application/json; charset=UTF-8; profile=http://localhost/schema/file/collection';
30
31
    /**
32
     * setup client and load fixtures
33
     *
34
     * @return void
35
     */
36
    public function setUp()
37
    {
38
        $this->loadFixtures(
39
            array(
40
                'GravitonDyn\FileBundle\DataFixtures\MongoDB\LoadFileData'
41
            ),
42
            null,
43
            'doctrine_mongodb'
44
        );
45
    }
46
47
    /**
48
     * check for empty collections when no fixtures are loaded
49
     *
50
     * @return void
51
     */
52 View Code Duplication
    public function testFindAllEmptyCollection()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        // reset fixtures since we already have some from setUp
55
        $this->loadFixtures(array(), null, 'doctrine_mongodb');
56
        $client = static::createRestClient();
57
        $client->request('GET', '/file/');
58
59
        $response = $client->getResponse();
60
        $results = $client->getResults();
61
62
        $this->assertResponseContentType(self::COLLECTION_TYPE, $response);
63
64
        $this->assertEquals(array(), $results);
65
    }
66
67
    /**
68
     * validate that we can post a new file
69
     *
70
     * @return void
71
     */
72
    public function testPostAndUpdateFile()
73
    {
74
        $fixtureData = file_get_contents(__DIR__.'/fixtures/test.txt');
75
        $fileHash = hash('sha256', $fixtureData);
76
        $fileHashCustom = 'some-custom-hash-for-testing';
77
78
        $client = static::createRestClient();
79
        $client->post(
80
            '/file/',
81
            $fixtureData,
82
            [],
83
            [],
84
            ['CONTENT_TYPE' => 'text/plain'],
85
            false
86
        );
87
        $this->assertEmpty($client->getResults());
88
        $response = $client->getResponse();
89
        $this->assertEquals(201, $response->getStatusCode());
90
91
        $fileLocation = $response->headers->get('Location');
92
93
        // update file contents to update mod date
94
        $client = static::createRestClient();
95
        $client->put(
96
            $fileLocation,
97
            $fixtureData,
98
            [],
99
            [],
100
            ['CONTENT_TYPE' => 'text/plain'],
101
            false
102
        );
103
        $this->assertEmpty($client->getResults(), $client->getResponse()->getContent());
104
        $response = $client->getResponse();
105
        $this->assertEquals(204, $response->getStatusCode());
106
107
        $client = static::createRestClient();
108
        $client->request('GET', $fileLocation);
109
        $data = $client->getResults();
110
111
        // check for valid format
112
        $this->assertNotFalse(\DateTime::createFromFormat(\DateTime::RFC3339, $data->metadata->createDate));
113
        $this->assertNotFalse(\DateTime::createFromFormat(\DateTime::RFC3339, $data->metadata->modificationDate));
114
        // Check valid hash encoding if no hash sent
115
        $this->assertEquals($fileHash, $data->metadata->hash);
116
117
        $data->links = [];
118
        $link = new \stdClass;
119
        $link->{'$ref'} = 'http://localhost/core/app/tablet';
120
        $data->links[] = $link;
121
122
        $filename = "test.txt";
123
        $data->metadata->filename = $filename;
124
        $data->metadata->hash = $fileHashCustom;
125
126
        $client = static::createRestClient();
127
        $client->put(sprintf('/file/%s', $data->id), $data);
128
129
        // re-fetch
130
        $client = static::createRestClient();
131
        $client->request('GET', sprintf('/file/%s', $data->id));
132
        $results = $client->getResults();
133
134
        $this->assertEquals($link->{'$ref'}, $results->links[0]->{'$ref'});
135
        $this->assertEquals($filename, $results->metadata->filename);
136
        $this->assertEquals($fileHashCustom, $data->metadata->hash);
137
138
        $client = static::createClient();
139
        $client->request('GET', sprintf('/file/%s', $data->id), [], [], ['HTTP_ACCEPT' => 'text/plain']);
140
141
        $results = $client->getResponse()->getContent();
142
143
        $this->assertEquals($fixtureData, $results);
144
145
        // change link and add second link
146
        $data->links[0]->{'$ref'} = 'http://localhost/core/app/admin';
147
        $link = new \stdClass;
148
        $link->{'$ref'} = 'http://localhost/core/app/web';
149
        $data->links[] = $link;
150
151
        // also add action command
152
        $command = new \stdClass();
153
        $command->command = 'print';
154
        $data->metadata->action = [$command];
155
156
        // also add additionalInformation
157
        $data->metadata->additionalInformation = 'someInfo';
158
159
        $client = static::createRestClient();
160
        $client->put(sprintf('/file/%s', $data->id), $data);
161
162
        // re-fetch
163
        $client = static::createRestClient();
164
        $client->request('GET', sprintf('/file/%s', $data->id));
165
        $results = $client->getResults();
166
167
        $this->assertEquals($data->links[0]->{'$ref'}, $results->links[0]->{'$ref'});
168
        $this->assertEquals($data->links[1]->{'$ref'}, $results->links[1]->{'$ref'});
169
170
        // check metadata
171
        $this->assertEquals(18, $data->metadata->size);
172
        $this->assertEquals('text/plain', $data->metadata->mime);
173
        $this->assertEquals('test.txt', $data->metadata->filename);
174
        $this->assertEquals('print', $data->metadata->action[0]->command);
175
        $this->assertEquals('someInfo', $data->metadata->additionalInformation);
176
        $this->assertNotNull($data->metadata->createDate);
177
        $this->assertNotNull($data->metadata->modificationDate);
178
179
        // remove a link
180
        unset($data->links[1]);
181
182
        $client = static::createRestClient();
183
        $client->put(sprintf('/file/%s', $data->id), $data);
184
        // re-fetch
185
        $client = static::createRestClient();
186
        $client->request('GET', sprintf('/file/%s', $data->id));
187
        $results = $client->getResults();
188
189
        $this->assertEquals($data->links[0]->{'$ref'}, $results->links[0]->{'$ref'});
190
        $this->assertCount(1, $results->links);
191
192
        // remove last link
193
        $data->links = [];
194
        $client = static::createRestClient();
195
        $client->put(sprintf('/file/%s', $data->id), $data);
196
197
        // re-fetch
198
        $client = static::createRestClient();
199
        $client->request('GET', sprintf('/file/%s', $data->id));
200
201
        $results = $client->getResults();
202
203
        $this->assertEmpty($results->links);
204
205
        // Let's update links but without sending file and still have file info
206
        $id = $data->id;
207
        $data = new \stdClass;
208
        $data->id = $id;
209
        $link = new \stdClass;
210
        $link->{'$ref'} = 'http://localhost/core/app/web';
211
        $data->links = [];
212
        $data->links[] = $link;
213
        $client = static::createRestClient();
214
        $client->put(sprintf('/file/%s', $id), $data);
215
        // re-fetch
216
        $client = static::createRestClient();
217
        $client->request('GET', sprintf('/file/%s', $data->id));
218
        $data = $client->getResults();
219
        // check metadata for kept file info
220
        $this->assertEquals(18, $data->metadata->size);
221
        $this->assertEquals('text/plain', $data->metadata->mime);
222
        $this->assertEquals('test.txt', $data->metadata->filename);
223
        $this->assertNotNull($data->metadata->createDate);
224
        $this->assertNotNull($data->metadata->modificationDate);
225
    }
226
227
    /**
228
     * validate that we can post a new file
229
     *
230
     * @return void
231
     */
232 View Code Duplication
    public function testPostNewFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
233
    {
234
        $client = static::createRestClient();
235
236
        $client->post(
237
            '/file',
238
            file_get_contents(__DIR__.'/fixtures/test.txt'),
239
            [],
240
            [],
241
            ['CONTENT_TYPE' => 'text/plain'],
242
            false
243
        );
244
245
        $response = $client->getResponse();
246
        $linkHeader = $response->headers->get('Link');
247
248
        $this->assertEquals(201, $response->getStatusCode());
249
        $this->assertRegExp('@/file/[a-z0-9]{32}>; rel="self"@', $linkHeader);
250
    }
251
252
    /**
253
     * validate that we can put a new file with a custom id
254
     *
255
     * @return void
256
     */
257 View Code Duplication
    public function testPutNewFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
258
    {
259
        $client = static::createRestClient();
260
261
        $client->put(
262
            '/file/testPutNewFile',
263
            file_get_contents(__DIR__ . '/fixtures/test.txt'),
264
            [],
265
            [],
266
            ['CONTENT_TYPE' => 'text/plain'],
267
            false
268
        );
269
270
        $response = $client->getResponse();
271
        $linkHeader = $response->headers->get('Link');
272
273
        $this->assertEquals(204, $response->getStatusCode());
274
        $this->assertContains('file/testPutNewFile>; rel="self"', $linkHeader);
275
    }
276
277
    /**
278
     * validate that we can delete a file
279
     *
280
     * @return void
281
     */
282
    public function testDeleteFile()
283
    {
284
        $fixtureData = file_get_contents(__DIR__.'/fixtures/test.txt');
285
        $client = static::createRestClient();
286
        $client->post(
287
            '/file/',
288
            $fixtureData,
289
            [],
290
            [],
291
            ['CONTENT_TYPE' => 'text/plain'],
292
            false
293
        );
294
        $response = $client->getResponse();
295
        $this->assertEquals(201, $response->getStatusCode());
296
297
        // re-fetch
298
        $client = static::createRestClient();
299
        $client->request('GET', $response->headers->get('Location'));
300
        $data = $client->getResults();
301
302
        $client = static::createRestClient();
303
        $client->request('DELETE', sprintf('/file/%s', $data->id));
304
305
        $client = static::createRestClient();
306
        $client->request('GET', sprintf('/file/%s', $data->id));
307
308
        $response = $client->getResponse();
309
        $this->assertEquals(404, $response->getStatusCode());
310
    }
311
312
    /**
313
     * validate that we can update the content from a file
314
     *
315
     * @return void
316
     */
317
    public function testUpdateFileContent()
318
    {
319
        $fixtureData = file_get_contents(__DIR__.'/fixtures/test.txt');
320
        $contentType = 'text/plain';
321
        $newData = "This is a new text!!!";
322
        $client = static::createRestClient();
323
        $client->post(
324
            '/file/',
325
            $fixtureData,
326
            [],
327
            [],
328
            ['CONTENT_TYPE' => $contentType],
329
            false
330
        );
331
        $response = $client->getResponse();
332
        $this->assertEquals(201, $response->getStatusCode());
333
334
        $linkHeader = $response->headers->get('Link');
335
        $this->assertRegExp('@/file/[a-z0-9]{32}>; rel="self"@', $linkHeader);
336
337
        // re-fetch
338
        $client = static::createRestClient();
339
        $client->request('GET', $response->headers->get('Location'));
340
        $retData = $client->getResults();
341
342
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
343
        $this->assertEquals(strlen($fixtureData), $retData->metadata->size);
344
        $this->assertEquals($contentType, $retData->metadata->mime);
345
346
        $this->updateFileContent($retData->id, $newData, $contentType);
347
    }
348
349
    /**
350
     * post a file without any mime type.. check if that mime type is correctly determined.
351
     * fetch content back and compare the contents of the file
352
     *
353
     * @return void
354
     */
355
    public function testPostFileContentMimeDetectionAndContent()
356
    {
357
        $testData = file_get_contents(__DIR__.'/resources/testpicture.jpg');
358
        $contentType = 'image/jpeg';
359
        $client = static::createRestClient();
360
361
        $client->post(
362
            '/file/',
363
            $testData,
364
            [],
365
            [],
366
            [],
367
            false
368
        );
369
        $response = $client->getResponse();
370
        $this->assertEquals(201, $response->getStatusCode());
371
372
        $linkHeader = $response->headers->get('Link');
373
        $this->assertRegExp('@/file/[a-z0-9]{32}>; rel="self"@', $linkHeader);
374
375
        // re-fetch
376
        $client = static::createRestClient();
377
        $client->request('GET', $response->headers->get('Location'));
378
        $retData = $client->getResults();
379
380
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
381
        $this->assertEquals($contentType, $retData->metadata->mime);
382
383
        /** we use the standard client as we don't want to have json forced */
384
        $client = static::createClient();
385
        $client->request(
386
            'GET',
387
            $response->headers->get('Location'),
388
            [],
389
            [],
390
            ['ACCEPT' => $contentType]
391
        );
392
393
        $response = $client->getInternalResponse();
394
395
        $this->assertTrue(($response->getContent() === $testData));
396
    }
397
398
    /**
399
     * here we PUT a file, then try to update the mimetype in the metadata
400
     * to 'something/other' and see if we can still GET it and receive the correct mime type
401
     * (meaning we were not able to modify the mime type)
402
     *
403
     * @return void
404
     */
405
    public function testIllegalMimeTypeModificationHandling()
406
    {
407
        $fileData = "This is a new text!!!";
408
        $contentType = 'text/plain';
409
410
        $client = static::createRestClient();
411
        $client->put(
412
            '/file/mimefile',
413
            $fileData,
414
            [],
415
            [],
416
            [],
417
            [],
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
418
            false
0 ignored issues
show
Unused Code introduced by
The call to Client::put() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
419
        );
420
        $response = $client->getResponse();
421
        $this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode());
422
423
        // GET the metadata
424
        $client = static::createRestClient();
425
        $client->request('GET', '/file/mimefile');
426
        $retData = $client->getResults();
427
428
        $this->assertEquals($retData->metadata->mime, $contentType);
429
430
        // change metadata and save
431
        $retData->metadata->mime = 'something/other';
432
433
        $client = static::createRestClient();
434
        $client->put('/file/mimefile', $retData);
435
436
        $client = static::createClient();
437
        $client->request(
438
            'GET',
439
            '/file/mimefile',
440
            [],
441
            [],
442
            ['ACCEPT' => '*/*']
443
        );
444
445
        $response = $client->getInternalResponse();
446
447
        // still the good one?
448
        $this->assertContains($contentType, $response->getHeader('content-type'));
449
450
        $client = static::createRestClient();
451
        $client->request('DELETE', '/file/mimefile');
452
    }
453
454
    /**
455
     * test getting collection schema
456
     *
457
     * @return void
458
     */
459
    public function testGetFileCollectionSchemaInformation()
460
    {
461
        $client = static::createRestClient();
462
463
        $client->request('GET', '/schema/file/collection');
464
465
        $response = $client->getResponse();
466
        $results = $client->getResults();
467
468
        $this->assertResponseContentType('application/schema+json', $response);
469
        $this->assertEquals(200, $response->getStatusCode());
470
471
        $this->assertEquals('Array of file objects', $results->title);
472
        $this->assertEquals('array', $results->type);
473
        $this->assertIsFileSchema($results->items);
474
475
        $this->assertCorsHeaders('GET, POST, PUT, PATCH, DELETE, OPTIONS', $response);
476
        $this->assertContains(
477
            'Link',
478
            explode(',', $response->headers->get('Access-Control-Expose-Headers'))
479
        );
480
481
        $this->assertContains(
482
            '<http://localhost/schema/file/collection>; rel="self"',
483
            explode(',', $response->headers->get('Link'))
484
        );
485
    }
486
487
    /**
488
     * test behavior when data sent was multipart/form-data
489
     *
490
     * @return void
491
     */
492
    public function testPutNewFileViaForm()
493
    {
494
        copy(__DIR__ . '/fixtures/test.txt', sys_get_temp_dir() . '/test.txt');
495
        $file = sys_get_temp_dir() . '/test.txt';
496
        $uploadedFile = new UploadedFile($file, 'test.txt', 'text/plain', 15);
497
498
        $jsonData = '{
499
          "id": "myPersonalFile",
500
          "links": [
501
            {
502
              "$ref": "http://localhost/testcase/readonly/101",
503
              "type": "owner"
504
            },
505
            {
506
              "$ref": "http://localhost/testcase/readonly/102",
507
              "type": "module"
508
            }
509
          ],
510
          "metadata": {
511
            "hash": "demo-test-hash",
512
            "action":[{"command":"print"},{"command":"archive"}],
513
            "additionalInformation": "someInfo",
514
            "additionalProperties": [
515
                {"name": "testName", "value": "testValue"},
516
                {"name": "testName2", "value": "testValue2"}
517
            ],
518
            "filename": "customFileName"
519
          }
520
        }';
521
522
        $client = static::createRestClient();
523
        $client->put(
524
            '/file/myPersonalFile',
525
            [],
526
            [
527
                'metadata' => $jsonData,
528
            ],
529
            [
530
                'upload' => $uploadedFile,
531
            ],
532
            [],
533
            false
534
        );
535
536
        $response = $client->getResponse();
537
538
        $this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode());
539
        $this->assertNotContains('location', $response->headers->all());
540
541
        $response = $this->updateFileContent('myPersonalFile', "This is a new text!!!");
542
543
        $metaData = json_decode($jsonData, true);
544
        $returnData = json_decode($response->getContent(), true);
545
546
        $this->assertEquals($metaData['links'], $returnData['links']);
547
        $this->assertEquals($metaData['metadata']['action'], $returnData['metadata']['action']);
548
        $this->assertEquals(
549
            $metaData['metadata']['additionalInformation'],
550
            $returnData['metadata']['additionalInformation']
551
        );
552
        $this->assertEquals(
553
            $metaData['metadata']['additionalProperties'],
554
            $returnData['metadata']['additionalProperties']
555
        );
556
        $this->assertCount(2, $returnData['metadata']['additionalProperties']);
557
        $this->assertEquals($metaData['metadata']['filename'], $returnData['metadata']['filename']);
558
        $this->assertEquals($metaData['metadata']['hash'], $returnData['metadata']['hash']);
559
560
        // clean up
561
        $client = $this->createClient();
562
        $client->request(
563
            'DELETE',
564
            '/file/myPersonalFile'
565
        );
566
    }
567
568
    /**
569
     * test behavior when data sent was multipart/form-data
570
     *
571
     * @return void
572
     */
573
    public function testPutNewFileViaFormHashToLong()
574
    {
575
        copy(__DIR__ . '/fixtures/test.txt', sys_get_temp_dir() . '/test.txt');
576
        $file = sys_get_temp_dir() . '/test.txt';
577
        $uploadedFile = new UploadedFile($file, 'test.txt', 'text/plain', 15);
578
579
        // Max 64 length, should not contain the extra bits
580
        $correctHash = hash('sha256', 'somestring');
581
        $toLongHashExtra = $correctHash . '-some-extra-bits ';
582
583
        $jsonData = '{
584
          "id": "myPersonalFile2",
585
          "metadata": {
586
            "hash": "'.$toLongHashExtra.'",
587
            "action":[{"command":"archive"}],
588
            "filename": "customFileName"
589
          }
590
        }';
591
592
        $client = static::createRestClient();
593
        $client->put(
594
            '/file/myPersonalFile2',
595
            [],
596
            [
597
                'metadata' => $jsonData,
598
            ],
599
            [
600
                'upload' => $uploadedFile,
601
            ],
602
            [],
603
            false
604
        );
605
606
        $response = $client->getResponse();
607
608
        $this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode());
609
        $this->assertNotContains('location', $response->headers->all());
610
611
        $response = $this->updateFileContent('myPersonalFile2', "This is a new text!!!");
612
613
        $metaData = json_decode($jsonData, true);
614
        $returnData = json_decode($response->getContent(), true);
615
616
        $this->assertEquals($metaData['metadata']['filename'], $returnData['metadata']['filename']);
617
618
        // Should NOT be equal as hash was to long and was chopped.
619
        $this->assertNotEquals($metaData['metadata']['hash'], $returnData['metadata']['hash']);
620
        // But it should be equal to to first part of the hash
621
        $this->assertEquals($correctHash, $returnData['metadata']['hash']);
622
623
        // clean up
624
        $client = $this->createClient();
625
        $client->request(
626
            'DELETE',
627
            '/file/myPersonalFile2'
628
        );
629
    }
630
631
632
633
    /**
634
    /**
635
     * test behavior when data sent was multipart/form-data
636
     *
637
     * @return void
638
     */
639
    public function testPutNewJsonFileViaForm()
640
    {
641
        $fileId = 'simple-json-content';
642
        $newContent = '{
643
          "id": "myPersonalFile",
644
          "someArrayData": [
645
            {
646
              "field1": "fieldDataValue"
647
            }
648
          ],
649
          "UnknownData": {
650
            "testField": "filed"
651
          }
652
        }';
653
654
        $this->updateFileContent($fileId, $newContent);
655
656
        $client = $this->createClient([], ['CONTENT_TYPE' => 'text/plain']);
657
        $client->request('GET', sprintf('/file/%s', $fileId));
658
659
        $retData = $client->getResponse()->getContent();
660
661
        $this->assertEquals($retData, $newContent);
662
663
        // clean up
664
        $client = $this->createClient();
665
        $client->request(
666
            'DELETE',
667
            '/file/'.$fileId
668
        );
669
    }
670
671
    /**
672
     * check if a schema is of the file type
673
     *
674
     * @param \stdClass $schema schema from service to validate
675
     *
676
     * @return void
677
     */
678
    private function assertIsFileSchema(\stdClass $schema)
679
    {
680
        $this->assertEquals('File', $schema->title);
681
        $this->assertEquals('File storage service', $schema->description);
682
        $this->assertEquals('object', $schema->type);
683
684
        $this->assertEquals('string', $schema->properties->id->type);
685
        $this->assertEquals('ID', $schema->properties->id->title);
686
        $this->assertEquals('Unique identifier', $schema->properties->id->description);
687
        $this->assertObjectNotHasAttribute('readOnly', $schema->properties->id);
688
689
        // Metadata
690
        $this->assertEquals('object', $schema->properties->metadata->type);
691
        $this->assertEquals('Metadata', $schema->properties->metadata->title);
692
        $this->assertObjectNotHasAttribute('readOnly', $schema->properties->metadata);
693
694
        // Metadata size
695
        $this->assertEquals('integer', $schema->properties->metadata->properties->size->type);
696
        $this->assertEquals('File size', $schema->properties->metadata->properties->size->title);
697
        $this->assertEquals('Size of file.', $schema->properties->metadata->properties->size->description);
698
699
        // Metadata mime
700
        $this->assertContains('string', $schema->properties->metadata->properties->mime->type);
701
        $this->assertEquals('MIME Type', $schema->properties->metadata->properties->mime->title);
702
        $this->assertEquals('MIME-Type of file.', $schema->properties->metadata->properties->mime->description);
703
704
        // Metadata createDate
705
        $this->assertEquals(['string', 'null'], $schema->properties->metadata->properties->createDate->type);
706
        $this->assertEquals('date-time', $schema->properties->metadata->properties->createDate->format);
707
        $this->assertEquals('Creation date', $schema->properties->metadata->properties->createDate->title);
708
        $this->assertEquals(
709
            'Timestamp of file upload.',
710
            $schema->properties->metadata->properties->createDate->description
711
        );
712
713
        // Metadata modificationDate
714
        $this->assertEquals(['string', 'null'], $schema->properties->metadata->properties->modificationDate->type);
715
        $this->assertEquals('date-time', $schema->properties->metadata->properties->modificationDate->format);
716
        $this->assertEquals('Modification date', $schema->properties->metadata->properties->modificationDate->title);
717
        $this->assertEquals(
718
            'Timestamp of the last file change.',
719
            $schema->properties->metadata->properties->modificationDate->description
720
        );
721
722
        // Metadata filename
723
        $this->assertContains('string', $schema->properties->metadata->properties->filename->type);
724
        $this->assertEquals('File name', $schema->properties->metadata->properties->filename->title);
725
        $this->assertEquals(
726
            'Name of the file as it should get displayed to the user.',
727
            $schema->properties->metadata->properties->filename->description
728
        );
729
        $this->assertObjectNotHasAttribute('readOnly', $schema->properties->metadata->properties->filename);
730
731
        // metadata action.command array
732
        $this->assertContains(
733
            'string',
734
            $schema->properties->metadata->properties->action->items->properties->command->type
735
        );
736
        $this->assertEquals(
737
            'Action command array',
738
            $schema->properties->metadata->properties->action->items->properties->command->title
739
        );
740
        $this->assertObjectNotHasAttribute(
741
            'readOnly',
742
            $schema->properties->metadata->properties->action->items->properties->command
743
        );
744
745
        // metadata additionalInformation
746
        $this->assertContains(
747
            'string',
748
            $schema->properties->metadata->properties->additionalInformation->type
749
        );
750
        $this->assertEquals(
751
            'Additional Information',
752
            $schema->properties->metadata->properties->additionalInformation->title
753
        );
754
        $this->assertObjectNotHasAttribute(
755
            'readOnly',
756
            $schema->properties->metadata->properties->additionalInformation
757
        );
758
759
        // metadata additionalProperties
760
        $additionalPropertiesSchema = $schema->properties->metadata->properties->additionalProperties;
761
        $this->assertEquals('array', $additionalPropertiesSchema->type);
762
        $this->assertEquals('object', $additionalPropertiesSchema->items->type);
763
        $this->assertEquals('string', $additionalPropertiesSchema->items->properties->name->type);
764
        $this->assertEquals('property name', $additionalPropertiesSchema->items->properties->name->title);
765
        $this->assertEquals('string', $additionalPropertiesSchema->items->properties->value->type);
766
        $this->assertEquals('property value', $additionalPropertiesSchema->items->properties->value->title);
767
768
        // Links
769
        $this->assertEquals('array', $schema->properties->links->type);
770
        $this->assertEquals('many', $schema->properties->links->format);
771
        $this->assertEquals('Links', $schema->properties->links->title);
772
        $this->assertEquals('@todo replace me', $schema->properties->links->description);
773
        $this->assertObjectNotHasAttribute('readOnly', $schema->properties->links);
774
775
776
        // Links items
777
        $this->assertEquals('object', $schema->properties->links->items->type);
778
        $this->assertEquals('Links', $schema->properties->links->items->title);
779
        $this->assertObjectNotHasAttribute('readOnly', $schema->properties->links->items);
780
781
782
        // Links item type
783
        $this->assertContains('string', $schema->properties->links->items->properties->type->type);
784
        $this->assertEquals('Type', $schema->properties->links->items->properties->type->title);
785
        $this->assertEquals('Type of the link.', $schema->properties->links->items->properties->type->description);
786
        $this->assertObjectNotHasAttribute('readOnly', $schema->properties->links->items->properties->type);
787
788
        // Links item $ref
789
        $this->assertEquals(['string', 'null'], $schema->properties->links->items->properties->{'$ref'}->type);
790
        $this->assertEquals('extref', $schema->properties->links->items->properties->{'$ref'}->format);
791
        $this->assertEquals('Link', $schema->properties->links->items->properties->{'$ref'}->title);
792
        $this->assertEquals(
793
            'Link to any document.',
794
            $schema->properties->links->items->properties->{'$ref'}->description
795
        );
796
        $this->assertEquals(
797
            ['*'],
798
            $schema->properties->links->items->properties->{'$ref'}->{'x-collection'}
799
        );
800
801
        $this->assertEquals(
802
            [
803
                'document.file.file.update',
804
                'document.file.file.create',
805
                'document.file.file.delete'
806
            ],
807
            $schema->{'x-events'}
808
        );
809
810
        $this->assertObjectNotHasAttribute('readOnly', $schema->properties->links->items->properties->{'$ref'});
811
    }
812
813
    /**
814
     * Verifies the update of a file content.
815
     *
816
     * @param string $fileId      identifier of the file to be updated
817
     * @param string $newContent  new content to be stored in the file
818
     * @param string $contentType Content-Type of the file
819
     *
820
     * @return null|Response
821
     */
822
    private function updateFileContent($fileId, $newContent, $contentType = 'text/plain')
823
    {
824
        $client = static::createRestClient();
825
        $client->put(
826
            sprintf('/file/%s', $fileId),
827
            $newContent,
828
            [],
829
            [],
830
            ['CONTENT_TYPE' => $contentType],
831
            false
832
        );
833
834
        $client = static::createRestClient();
835
        $client->request('GET', sprintf('/file/%s', $fileId));
836
837
        $retData = $client->getResults();
838
        $response = $client->getResponse();
839
        $this->assertEquals(200, $response->getStatusCode());
840
        $this->assertEquals($contentType, $retData->metadata->mime);
841
842
        return $response;
843
    }
844
}
845