Completed
Push — master ( 11b317...37df4d )
by Lucas
09:27
created

AppControllerTest   D

Complexity

Total Complexity 34

Size/Duplication

Total Lines 873
Duplicated Lines 7.45 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 34
c 1
b 1
f 0
lcom 1
cbo 2
dl 65
loc 873
rs 4.6

32 Methods

Rating   Name   Duplication   Size   Complexity  
A testRqlIsParsedOnlyOnGetRequest() 0 51 1
A setUp() 0 14 1
B testFindAll() 0 27 1
B testGetAppWithFilteringAndPaging() 0 26 1
A testGetAppPagingWithRql() 0 51 1
B testOnlyRqlSelectIsAllowedOnGetOne() 0 28 2
A testFindAllEmptyCollection() 14 14 1
A testGetApp() 0 19 1
B testPostApp() 0 29 1
A testPostEmptyApp() 0 16 1
A testPostNonObjectApp() 0 9 1
B testPostMalformedApp() 0 29 2
A testPostWithId() 0 14 1
B testPutApp() 0 28 1
B testPatchAppRequestApplyChanges() 0 37 1
B testMalformedPatchAppRequest() 0 29 1
A testNonMatchingIdPutApp() 0 20 1
A testPutAppNoIdInPayload() 0 21 1
A testUpsertApp() 0 13 1
A testDeleteApp() 0 20 1
A testFailingBooleanValidationOnAppUpdate() 0 18 1
A testGetAppSchemaInformation() 9 9 1
A testNoRecordsAreGeneratedOnPreRequests() 12 12 1
A testGetAppSchemaInformationCanonical() 8 8 1
B testGetAppCollectionSchemaInformation() 0 28 1
A testSearchableTranslations() 0 14 1
A searchableTranslationDataProvider() 0 11 1
A testRqlSyntaxError() 14 14 1
A assertIsSchemaResponse() 0 5 1
B assertIsAppSchema() 0 25 1
A testInvalidPagingPageSize() 8 8 1
A invalidPagingPageSizeProvider() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * functional test for /core/app
4
 */
5
6
namespace Graviton\CoreBundle\Tests\Controller;
7
8
use Graviton\TestBundle\Test\RestTestCase;
9
use Symfony\Component\HttpFoundation\Response;
10
11
/**
12
 * Basic functional test for /core/app.
13
 *
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
class AppControllerTest extends RestTestCase
19
{
20
    /**
21
     * @const complete content type string expected on a resouce
22
     */
23
    const CONTENT_TYPE = 'application/json; charset=UTF-8; profile=http://localhost/schema/core/app/item';
24
25
    /**
26
     * @const corresponding vendorized schema mime type
27
     */
28
    const COLLECTION_TYPE = 'application/json; charset=UTF-8; profile=http://localhost/schema/core/app/collection';
29
30
    /**
31
     * setup client and load fixtures
32
     *
33
     * @return void
34
     */
35
    public function setUp()
36
    {
37
        $this->loadFixtures(
38
            array(
39
                'Graviton\CoreBundle\DataFixtures\MongoDB\LoadAppData',
40
                'Graviton\I18nBundle\DataFixtures\MongoDB\LoadLanguageData',
41
                'Graviton\I18nBundle\DataFixtures\MongoDB\LoadMultiLanguageData',
42
                'Graviton\I18nBundle\DataFixtures\MongoDB\LoadTranslatableData',
43
                'Graviton\I18nBundle\DataFixtures\MongoDB\LoadTranslatablesApp'
44
            ),
45
            null,
46
            'doctrine_mongodb'
47
        );
48
    }
49
    /**
50
     * check if all fixtures are returned on GET
51
     *
52
     * @return void
53
     */
54
    public function testFindAll()
55
    {
56
        $client = static::createRestClient();
57
        $client->request('GET', '/core/app/');
58
59
        $response = $client->getResponse();
60
        $results = $client->getResults();
61
62
        $this->assertResponseContentType(self::COLLECTION_TYPE, $response);
63
        $this->assertEquals(2, count($results));
64
65
        $this->assertEquals('admin', $results[0]->id);
66
        $this->assertEquals('Administration', $results[0]->name->en);
67
        $this->assertEquals(true, $results[0]->showInMenu);
68
        $this->assertEquals(2, $results[0]->order);
69
70
        $this->assertEquals('tablet', $results[1]->id);
71
        $this->assertEquals('Tablet', $results[1]->name->en);
72
        $this->assertEquals(true, $results[1]->showInMenu);
73
        $this->assertEquals(1, $results[1]->order);
74
75
        $this->assertContains(
76
            '<http://localhost/core/app/>; rel="self"',
77
            $response->headers->get('Link')
78
        );
79
        $this->assertEquals('*', $response->headers->get('Access-Control-Allow-Origin'));
80
    }
81
82
    /**
83
     * test if we can get list of apps, paged and with filters..
84
     *
85
     * @return void
86
     */
87
    public function testGetAppWithFilteringAndPaging()
88
    {
89
        $client = static::createRestClient();
90
        $_SERVER['QUERY_STRING'] = 'eq(showInMenu,true)&limit(1)';
91
        $client->request('GET', '/core/app/?eq(showInMenu,true)&limit(1)');
92
        unset($_SERVER['QUERY_STRING']);
93
        $response = $client->getResponse();
94
95
        $this->assertEquals(1, count($client->getResults()));
96
97
        $this->assertContains(
98
            '<http://localhost/core/app/?eq(showInMenu%2Ctrue)&limit(1)>; rel="self"',
99
            $response->headers->get('Link')
100
        );
101
102
        $this->assertContains(
103
            '<http://localhost/core/app/?eq(showInMenu%2Ctrue)&limit(1%2C1)>; rel="next"',
104
            $response->headers->get('Link')
105
        );
106
107
        $this->assertContains(
108
            '<http://localhost/core/app/?eq(showInMenu%2Ctrue)&limit(1%2C1)>; rel="last"',
109
            $response->headers->get('Link')
110
        );
111
112
    }
113
114
    /**
115
     * rql limit() should *never* be overwritten by default value
116
     *
117
     * @return void
118
     */
119
    public function testGetAppPagingWithRql()
120
    {
121
        // does limit work?
122
        $client = static::createRestClient();
123
        $client->request('GET', '/core/app/?limit(1)');
124
        $this->assertEquals(1, count($client->getResults()));
125
126
        $response = $client->getResponse();
127
128
        $this->assertContains(
129
            '<http://localhost/core/app/?limit(1)>; rel="self"',
130
            $response->headers->get('Link')
131
        );
132
133
        $this->assertContains(
134
            '<http://localhost/core/app/?limit(1%2C1)>; rel="next"',
135
            $response->headers->get('Link')
136
        );
137
138
        $this->assertContains(
139
            '<http://localhost/core/app/?limit(1%2C1)>; rel="last"',
140
            $response->headers->get('Link')
141
        );
142
143
        $this->assertSame('2', $response->headers->get('X-Total-Count'));
144
145
        /*** pagination tests **/
146
        $client = static::createRestClient();
147
        $client->request('GET', '/core/app/?limit(1,1)');
148
        $this->assertEquals(1, count($client->getResults()));
149
150
        $response = $client->getResponse();
151
152
        $this->assertContains(
153
            '<http://localhost/core/app/?limit(1%2C1)>; rel="self"',
154
            $response->headers->get('Link')
155
        );
156
157
        $this->assertContains(
158
            '<http://localhost/core/app/?limit(1%2C0)>; rel="prev"',
159
            $response->headers->get('Link')
160
        );
161
162
        // we're on the 'last' page - so 'last' should not be in in Link header
163
        $this->assertNotContains(
164
            'rel="last"',
165
            $response->headers->get('Link')
166
        );
167
168
        $this->assertSame('2', $response->headers->get('X-Total-Count'));
169
    }
170
171
    /**
172
     * check for a client error if invalid limit value is provided
173
     *
174
     * @dataProvider invalidPagingPageSizeProvider
175
     *
176
     * @param integer $limit limit value that should fail
177
     * @return void
178
     */
179 View Code Duplication
    public function testInvalidPagingPageSize($limit)
180
    {
181
        $client = static::createRestClient();
182
        $client->request('GET', sprintf('/core/app/?limit(%s)', $limit));
183
184
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
185
        $this->assertContains('negative or null limit in rql', $client->getResults()->message);
186
    }
187
188
    /**
189
     * page size test provides
190
     *
191
     * @return array[]
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use integer[][].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
192
     */
193
    public function invalidPagingPageSizeProvider()
194
    {
195
        return [
196
            [0],
197
            [-1],
198
        ];
199
    }
200
201
    /**
202
     * RQL is parsed only when we get apps
203
     *
204
     * @return void
205
     */
206
    public function testRqlIsParsedOnlyOnGetRequest()
207
    {
208
        $appData = [
209
            'showInMenu' => false,
210
            'order'      => 100,
211
            'name'      => ['en' => 'Administration'],
212
        ];
213
214
        $client = static::createRestClient();
215
        $client->request('GET', '/core/app/?invalidrqlquery');
216
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
217
        $this->assertContains('syntax error in rql', $client->getResults()->message);
218
219
        $client = static::createRestClient();
220
        $client->request('GET', '/core/app/admin?invalidrqlquery');
221
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
222
        $this->assertContains('syntax error in rql', $client->getResults()->message);
223
224
        $client = static::createRestClient();
225
        $client->request('OPTIONS', '/core/app/?invalidrqlquery');
226
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
227
228
        $client = static::createRestClient();
229
        $client->request('OPTIONS', '/schema/core/app/collection?invalidrqlquery');
230
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
231
232
        $client = static::createRestClient();
233
        $client->request('OPTIONS', '/schema/core/app/item?invalidrqlquery');
234
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
235
236
        $client = static::createRestClient();
237
        $client->request('GET', '/schema/core/app/collection?invalidrqlquery');
238
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
239
240
        $client = static::createRestClient();
241
        $client->request('GET', '/schema/core/app/item?invalidrqlquery');
242
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
243
244
245
        $client = static::createRestClient();
246
        $client->post('/core/app/?invalidrqlquery', $appData);
247
        $this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode());
248
249
        $client = static::createRestClient();
250
        $client->put('/core/app/admin?invalidrqlquery', $appData);
251
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
252
253
        $client = static::createRestClient();
254
        $client->request('DELETE', '/core/app/admin?invalidrqlquery');
255
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
256
    }
257
258
    /**
259
     * Test only RQL select() operator is allowed for GET one
260
     *
261
     * @return void
262
     * @group tmp
263
     */
264
    public function testOnlyRqlSelectIsAllowedOnGetOne()
265
    {
266
        $client = static::createRestClient();
267
        $client->request('GET', '/core/app/?select(id)');
268
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
269
270
        $client = static::createRestClient();
271
        $client->request('GET', '/core/app/admin?select(id)');
272
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
273
274
        foreach ([
275
                     'limit' => 'limit(1)',
276
                     'sort'  => 'sort(+id)',
277
                     'eq'    => 'eq(id,a)',
278
                 ] as $extraRqlOperator => $extraRqlOperatorQuery) {
279
            $client = static::createRestClient();
280
            $client->request('GET', '/core/app/?select(id)&'.$extraRqlOperatorQuery);
281
            $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
282
283
            $client = static::createRestClient();
284
            $client->request('GET', '/core/app/admin?select(id)&'.$extraRqlOperatorQuery);
285
            $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
286
            $this->assertEquals(
287
                sprintf('RQL operator "%s" is not allowed for this request', $extraRqlOperator),
288
                $client->getResults()->message
289
            );
290
        }
291
    }
292
293
    /**
294
     * check for empty collections when no fixtures are loaded
295
     *
296
     * @return void
297
     */
298 View Code Duplication
    public function testFindAllEmptyCollection()
299
    {
300
        // reset fixtures since we already have some from setUp
301
        $this->loadFixtures(array(), null, 'doctrine_mongodb');
302
        $client = static::createRestClient();
303
        $client->request('GET', '/core/app/');
304
305
        $response = $client->getResponse();
306
        $results = $client->getResults();
307
308
        $this->assertResponseContentType(self::COLLECTION_TYPE, $response);
309
310
        $this->assertEquals(array(), $results);
311
    }
312
313
    /**
314
     * test if we can get an app by id
315
     *
316
     * @return void
317
     */
318
    public function testGetApp()
319
    {
320
        $client = static::createRestClient();
321
        $client->request('GET', '/core/app/admin');
322
        $response = $client->getResponse();
323
        $results = $client->getResults();
324
325
        $this->assertResponseContentType(self::CONTENT_TYPE, $response);
326
327
        $this->assertEquals('admin', $results->id);
328
        $this->assertEquals('Administration', $results->name->en);
329
        $this->assertEquals(true, $results->showInMenu);
330
331
        $this->assertContains(
332
            '<http://localhost/core/app/admin>; rel="self"',
333
            $response->headers->get('Link')
334
        );
335
        $this->assertEquals('*', $response->headers->get('Access-Control-Allow-Origin'));
336
    }
337
338
    /**
339
     * test if we can create an app through POST
340
     *
341
     * @return void
342
     */
343
    public function testPostApp()
344
    {
345
        $testApp = new \stdClass;
346
        $testApp->name = new \stdClass;
347
        $testApp->name->en = 'new Test App';
348
        $testApp->showInMenu = true;
349
350
        $client = static::createRestClient();
351
        $client->post('/core/app/', $testApp);
352
        $response = $client->getResponse();
353
        $results = $client->getResults();
354
355
        // we sent a location header so we don't want a body
356
        $this->assertNull($results);
357
        $this->assertContains('/core/app/', $response->headers->get('Location'));
358
359
        $client = static::createRestClient();
360
        $client->request('GET', $response->headers->get('Location'));
361
        $response = $client->getResponse();
362
        $results = $client->getResults();
363
364
        $this->assertResponseContentType(self::CONTENT_TYPE, $response);
365
        $this->assertEquals('new Test App', $results->name->en);
366
        $this->assertTrue($results->showInMenu);
367
        $this->assertContains(
368
            '<http://localhost/core/app/'.$results->id.'>; rel="self"',
369
            explode(',', $response->headers->get('Link'))
370
        );
371
    }
372
373
    /**
374
     * test if we get a correct return if we post empty.
375
     *
376
     * @return void
377
     */
378
    public function testPostEmptyApp()
379
    {
380
        $client = static::createRestClient();
381
382
        // send nothing really..
383
        $client->post('/core/app/', "", array(), array(), array(), false);
384
385
        $response = $client->getResponse();
386
387
        $this->assertContains(
388
            'No input data',
389
            $response->getContent()
390
        );
391
392
        $this->assertEquals(400, $response->getStatusCode());
393
    }
394
395
    /**
396
     * test if we get a correct return if we post empty.
397
     *
398
     * @return void
399
     */
400
    public function testPostNonObjectApp()
401
    {
402
        $client = static::createRestClient();
403
        $client->post('/core/app/', "non-object value");
404
405
        $response = $client->getResponse();
406
        $this->assertContains('JSON request body must be an object', $response->getContent());
407
        $this->assertEquals(400, $response->getStatusCode());
408
    }
409
410
    /**
411
     * test if 500 error is reported when posting an malformed input
412
     *
413
     * @return void
414
     */
415
    public function testPostMalformedApp()
416
    {
417
        $testApp = new \stdClass;
418
        $testApp->name = new \stdClass;
419
        $testApp->name->en = 'new Test App';
420
        $testApp->showInMenu = true;
421
422
        // malform it ;-)
423
        $input = str_replace(":", ";", json_encode($testApp));
424
425
        $client = static::createRestClient();
426
427
        // make sure this is sent as 'raw' input (not json_encoded again)
428
        $client->post('/core/app/', $input, array(), array(), array(), false);
429
430
        $response = $client->getResponse();
431
432
        // Check that error message contains detailed reason
433
        json_decode($input);
434
        $lastJsonError = function_exists('json_last_error_msg')
435
            ? json_last_error_msg()
436
            : 'Unable to decode JSON string';
437
        $this->assertContains(
438
            $lastJsonError,
439
            $client->getResults()->message
440
        );
441
442
        $this->assertEquals(400, $response->getStatusCode());
443
    }
444
445
    /**
446
     * Tests if an error is returned when an id is send in a post
447
     *
448
     * @return void
449
     */
450
    public function testPostWithId()
451
    {
452
        $helloApp = new \stdClass();
453
        $helloApp->id = 101;
454
        $helloApp->name = "tubel";
455
456
        $client = static::createRestClient();
457
        $client->post('/person/customer', $helloApp);
458
459
        $this->assertEquals(
460
            'Can not be given on a POST request. Do a PUT request instead to update an existing record.',
461
            $client->getResults()[0]->message
462
        );
463
    }
464
    /**
465
     * test updating apps
466
     *
467
     * @return void
468
     */
469
    public function testPutApp()
470
    {
471
        $helloApp = new \stdClass();
472
        $helloApp->id = "tablet";
473
        $helloApp->name = new \stdClass();
474
        $helloApp->name->en = "Tablet";
475
        $helloApp->showInMenu = false;
476
477
        $client = static::createRestClient();
478
        $client->put('/core/app/tablet', $helloApp);
479
480
        $this->assertNull($client->getResults());
481
        $this->assertNull($client->getResponse()->headers->get('Location'));
482
483
        $client = static::createRestClient();
484
        $client->request('GET', '/core/app/tablet');
485
        $response = $client->getResponse();
486
        $results = $client->getResults();
487
488
        $this->assertResponseContentType(self::CONTENT_TYPE, $response);
489
        $this->assertEquals('Tablet', $results->name->en);
490
        $this->assertFalse($results->showInMenu);
491
        $this->assertContains(
492
            '<http://localhost/core/app/tablet>; rel="self"',
493
            explode(',', $response->headers->get('Link'))
494
        );
495
496
    }
497
498
    /**
499
     * Test for PATCH Request
500
     *
501
     * @return void
502
     */
503
    public function testPatchAppRequestApplyChanges()
504
    {
505
        $helloApp = new \stdClass();
506
        $helloApp->id = "testapp";
507
        $helloApp->name = new \stdClass();
508
        $helloApp->name->en = "Test App";
509
        $helloApp->showInMenu = false;
510
511
        // 1. Create some App
512
        $client = static::createRestClient();
513
        $client->put('/core/app/' . $helloApp->id, $helloApp);
514
515
        // 2. PATCH request
516
        $client = static::createRestClient();
517
        $patchJson = json_encode(
518
            [
519
                [
520
                    'op' => 'replace',
521
                    'path' => '/name/en',
522
                    'value' => 'Test App Patched'
523
                ]
524
            ]
525
        );
526
        $client->request('PATCH', '/core/app/' . $helloApp->id, array(), array(), array(), $patchJson);
527
        $response = $client->getResponse();
528
529
        $this->assertEquals(200, $response->getStatusCode());
530
531
        // 3. Get changed App and check changed title
532
        $client = static::createRestClient();
533
        $client->request('GET', '/core/app/' . $helloApp->id);
534
        $response = $client->getResponse();
535
        $results = $client->getResults();
536
537
        $this->assertResponseContentType(self::CONTENT_TYPE, $response);
538
        $this->assertEquals('Test App Patched', $results->name->en);
539
    }
540
541
    /**
542
     * Test for Malformed PATCH Request
543
     *
544
     * @return void
545
     */
546
    public function testMalformedPatchAppRequest()
547
    {
548
        $helloApp = new \stdClass();
549
        $helloApp->id = "testapp";
550
        $helloApp->title = new \stdClass();
551
        $helloApp->title->en = "Test App";
552
        $helloApp->showInMenu = false;
553
554
        // 1. Create some App
555
        $client = static::createRestClient();
556
        $client->put('/core/app/' . $helloApp->id, $helloApp);
557
558
        // 2. PATCH request
559
        $client = static::createRestClient();
560
        $patchJson = json_encode(
561
            array(
562
                'op' => 'unknown',
563
                'path' => '/title/en'
564
            )
565
        );
566
        $client->request('PATCH', '/core/app/' . $helloApp->id, array(), array(), array(), $patchJson);
567
        $response = $client->getResponse();
568
569
        $this->assertEquals(400, $response->getStatusCode());
570
        $this->assertContains(
571
            'Invalid JSON patch request',
572
            $response->getContent()
573
        );
574
    }
575
576
    /**
577
     * Try to update an app with a non matching ID in GET and req body
578
     *
579
     * @return void
580
     */
581
    public function testNonMatchingIdPutApp()
582
    {
583
        $helloApp = new \stdClass();
584
        $helloApp->id = "tablet";
585
        $helloApp->name = new \stdClass();
586
        $helloApp->name->en = "Tablet";
587
        $helloApp->showInMenu = false;
588
589
        $client = static::createRestClient();
590
        $client->put('/core/app/someotherapp', $helloApp);
591
592
        $response = $client->getResponse();
593
594
        $this->assertContains(
595
            'Record ID in your payload must be the same',
596
            $response->getContent()
597
        );
598
599
        $this->assertEquals(400, $response->getStatusCode());
600
    }
601
602
    /**
603
     * We had an issue when PUTing without ID would create a new record.
604
     * This test ensures that we don't do that, instead we should apply the ID from the GET req.
605
     *
606
     * @return void
607
     */
608
    public function testPutAppNoIdInPayload()
609
    {
610
        $helloApp = new \stdClass();
611
        $helloApp->name = new \stdClass();
612
        $helloApp->name->en = 'New tablet';
613
        $helloApp->showInMenu = false;
614
615
        $client = static::createRestClient();
616
        $client->put('/core/app/tablet', $helloApp);
617
618
        // we sent a location header so we don't want a body
619
        $this->assertNull($client->getResults());
620
621
        $client = static::createRestClient();
622
        $client->request('GET', '/core/app/tablet');
623
        $results = $client->getResults();
624
625
        $this->assertEquals('tablet', $results->id);
626
        $this->assertEquals('New tablet', $results->name->en);
627
        $this->assertFalse($results->showInMenu);
628
    }
629
630
    /**
631
     * test updating an inexistant document (upsert)
632
     *
633
     * @return void
634
     */
635
    public function testUpsertApp()
636
    {
637
        $isnogudApp = new \stdClass;
638
        $isnogudApp->id = 'isnogud';
639
        $isnogudApp->name = new \stdClass;
640
        $isnogudApp->name->en = 'I don\'t exist';
641
        $isnogudApp->showInMenu = true;
642
643
        $client = static::createRestClient();
644
        $client->put('/core/app/isnogud', $isnogudApp);
645
646
        $this->assertEquals(204, $client->getResponse()->getStatusCode());
647
    }
648
649
    /**
650
     * test deleting an app
651
     *
652
     * @return void
653
     */
654
    public function testDeleteApp()
655
    {
656
        $testApp = new \stdClass;
657
        $testApp->id = 'tablet';
658
        $testApp->name = 'Tablet';
659
        $testApp->showInMenu = true;
660
        $testApp->order = 1;
661
662
        $client = static::createRestClient();
663
        $client->request('DELETE', '/core/app/tablet');
664
665
        $response = $client->getResponse();
666
667
        $this->assertEquals(204, $response->getStatusCode());
668
        $this->assertEquals('*', $response->headers->get('Access-Control-Allow-Origin'));
669
        $this->assertEmpty($response->getContent());
670
671
        $client->request('GET', '/core/app/tablet');
672
        $this->assertEquals(404, $client->getResponse()->getStatusCode());
673
    }
674
675
    /**
676
     * test failing validation on boolean field
677
     *
678
     * @return void
679
     */
680
    public function testFailingBooleanValidationOnAppUpdate()
681
    {
682
        $helloApp = new \stdClass;
683
        $helloApp->id = 'tablet';
684
        $helloApp->name = new \stdClass;
685
        $helloApp->name->en = 'Tablet';
686
        $helloApp->showInMenu = 'false';
687
688
        $client = static::createRestClient();
689
        $client->put('/core/app/tablet', $helloApp);
690
691
        $results = $client->getResults();
692
693
        $this->assertEquals(400, $client->getResponse()->getStatusCode());
694
695
        $this->assertContains('showInMenu', $results[0]->propertyPath);
696
        $this->assertEquals('The value "false" is not a valid boolean.', $results[0]->message);
697
    }
698
699
    /**
700
     * test getting schema information
701
     *
702
     * @return void
703
     */
704 View Code Duplication
    public function testGetAppSchemaInformation()
705
    {
706
        $client = static::createRestClient();
707
        $client->request('OPTIONS', '/core/app/hello');
708
709
        $response = $client->getResponse();
710
711
        $this->assertCorsHeaders('GET, POST, PUT, PATCH, DELETE, OPTIONS', $response);
712
    }
713
714
    /**
715
     * requests on OPTIONS and HEAD shall not lead graviton to get any data from mongodb.
716
     * if we page limit(1) this will lead to presence of the x-total-count header if
717
     * data is generated (asserted by testGetAppPagingWithRql()). thus, if we don't
718
     * have this header, we can safely assume that no data has been processed in RestController.
719
     *
720
     * @return void
721
     */
722 View Code Duplication
    public function testNoRecordsAreGeneratedOnPreRequests()
723
    {
724
        $client = static::createRestClient();
725
        $client->request('OPTIONS', '/core/app/?limit(1)');
726
        $response = $client->getResponse();
727
        $this->assertArrayNotHasKey('x-total-count', $response->headers->all());
728
729
        $client = static::createRestClient();
730
        $client->request('HEAD', '/core/app/?limit(1)');
731
        $response = $client->getResponse();
732
        $this->assertArrayNotHasKey('x-total-count', $response->headers->all());
733
    }
734
735
    /**
736
     * test getting schema information from canonical url
737
     *
738
     * @return void
739
     */
740 View Code Duplication
    public function testGetAppSchemaInformationCanonical()
741
    {
742
        $client = static::createRestClient();
743
        $client->request('GET', '/schema/core/app/item');
744
745
        $this->assertIsSchemaResponse($client->getResponse());
746
        $this->assertIsAppSchema($client->getResults());
747
    }
748
749
    /**
750
     * test getting collection schema
751
     *
752
     * @return void
753
     */
754
    public function testGetAppCollectionSchemaInformation()
755
    {
756
        $client = static::createRestClient();
757
758
        $client->request('GET', '/schema/core/app/collection');
759
760
        $response = $client->getResponse();
761
        $results = $client->getResults();
762
763
        $this->assertResponseContentType('application/schema+json', $response);
764
        $this->assertEquals(200, $response->getStatusCode());
765
766
        $this->assertEquals('Array of app objects', $results->title);
767
        $this->assertEquals('array', $results->type);
768
        $this->assertIsAppSchema($results->items);
769
        $this->assertEquals('en', $results->items->properties->name->required[0]);
770
771
        $this->assertCorsHeaders('GET, POST, PUT, PATCH, DELETE, OPTIONS', $response);
772
        $this->assertContains(
773
            'Link',
774
            explode(',', $response->headers->get('Access-Control-Expose-Headers'))
775
        );
776
777
        $this->assertContains(
778
            '<http://localhost/schema/core/app/collection>; rel="self"',
779
            explode(',', $response->headers->get('Link'))
780
        );
781
    }
782
783
    /**
784
     * Test for searchable translations
785
     *
786
     * @dataProvider searchableTranslationDataProvider
787
     *
788
     * @param string $expr     expression
789
     * @param int    $expCount count
790
     *
791
     * @return void
792
     */
793
    public function testSearchableTranslations($expr, $expCount)
794
    {
795
        $client = static::createRestClient();
796
        $client->request(
797
            'GET',
798
            '/core/app/?'.$expr,
799
            array(),
800
            array(),
801
            array('HTTP_ACCEPT_LANGUAGE' => 'en, de')
802
        );
803
804
        $result = $client->getResults();
805
        $this->assertCount($expCount, $result);
806
    }
807
808
    /**
809
     * data provider for searchable translations
810
     *
811
     * @return array data
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array<string|integer>>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
812
     */
813
    public function searchableTranslationDataProvider()
814
    {
815
        return [
816
            'simple-de' => array('eq(name.de,Die%20Administration)', 1),
817
            'non-existent' => array('eq(name.de,Administration)', 0),
818
            'english' => array('eq(name.en,Administration)', 1),
819
            'no-lang' => array('eq(name,Administration)', 1),
820
            'glob' => array('like(name.de,*Administr*)', 1),
821
            'all-glob' => array('like(name.de,*a*)', 2)
822
        ];
823
    }
824
825
    /**
826
     * ensure we have nice parse error output in rql parse failure
827
     *
828
     * @return void
829
     */
830 View Code Duplication
    public function testRqlSyntaxError()
831
    {
832
        $client = static::createRestClient();
833
834
        $client->request('GET', '/core/app/?eq(name)');
835
836
        $response = $client->getResponse();
837
        $results = $client->getResults();
838
839
        $this->assertEquals(400, $response->getStatusCode());
840
841
        $this->assertContains('syntax error in rql: ', $results->message);
842
        $this->assertContains('Unexpected token', $results->message);
843
    }
844
845
    /**
846
     * check if response looks like schema
847
     *
848
     * @param object $response response
849
     *
850
     * @return void
851
     */
852
    private function assertIsSchemaResponse($response)
853
    {
854
        $this->assertResponseContentType('application/schema+json', $response);
855
        $this->assertEquals(200, $response->getStatusCode());
856
    }
857
858
    /**
859
     * check if a schema is of the app type
860
     *
861
     * @param \stdClass $schema schema from service to validate
862
     *
863
     * @return void
864
     */
865
    private function assertIsAppSchema(\stdClass $schema)
866
    {
867
        $this->assertEquals('App', $schema->title);
868
        $this->assertEquals('A graviton based app.', $schema->description);
869
        $this->assertEquals('object', $schema->type);
870
871
        $this->assertEquals('string', $schema->properties->id->type);
872
        $this->assertEquals('ID', $schema->properties->id->title);
873
        $this->assertEquals('Unique identifier for an app.', $schema->properties->id->description);
874
        $this->assertContains('id', $schema->required);
875
876
        $this->assertEquals('object', $schema->properties->name->type);
877
        $this->assertEquals('translatable', $schema->properties->name->format);
878
        $this->assertEquals('Name', $schema->properties->name->title);
879
        $this->assertEquals('Display name for an app.', $schema->properties->name->description);
880
        $this->assertEquals('string', $schema->properties->name->properties->en->type);
881
        $this->assertContains('name', $schema->required);
882
883
        $this->assertEquals('boolean', $schema->properties->showInMenu->type);
884
        $this->assertEquals('Show in Menu', $schema->properties->showInMenu->title);
885
        $this->assertEquals(
886
            'Define if an app should be exposed on the top level menu.',
887
            $schema->properties->showInMenu->description
888
        );
889
    }
890
}
891