Completed
Push — master ( 52939d...6c1864 )
by Narcotic
146:21 queued 140:20
created

ModuleControllerTest::testRqlSelectOnItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
/**
3
 * functional test for /core/module
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/module.
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 ModuleControllerTest 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/module/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/module/collection';
29
30
    /**
31
     * setup client and load fixtures, generate search indexes separately
32
     *
33
     * @return void
34
     */
35
    public function setUp()
36
    {
37
        $this->loadFixtures(
38
            array(
39
                'Graviton\I18nBundle\DataFixtures\MongoDB\LoadLanguageData',
40
                'GravitonDyn\ModuleBundle\DataFixtures\MongoDB\LoadModuleData'
41
            ),
42
            null,
43
            'doctrine_mongodb'
44
        );
45
46
        $this->setVerbosityLevel(1);
47
        $this->isDecorated(true);
48
        $this->runCommand('graviton:generate:build-indexes', [], true);
49
    }
50
51
    /**
52
     * testing the search in search index, combined with a select (RQL)
53
     *
54
     * @return void
55
     */
56 View Code Duplication
    public function testSearchIndex()
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...
57
    {
58
        $client = static::createRestClient();
59
        $client->request('GET', '/core/module/?search(module)&select(key)');
60
61
        // should not find 'AdminRef'
62
        $this->assertEquals(5, count($client->getResults()));
63
64
        // the sixth
65
        $client = static::createRestClient();
66
        $client->request('GET', '/core/module/?search(AdminRef)&select(key)');
67
        $this->assertEquals('AdminRef', $client->getResults()[0]->key);
68
        $this->assertEquals(1, count($client->getResults()));
69
    }
70
71
    /**
72
     * testing the search index using second param, non sensitive, combined with a select (RQL)
73
     *
74
     * @return void
75
     */
76
    public function testSearchWeightedIndex()
77
    {
78
        $client = static::createRestClient();
79
        $client->request('GET', '/core/module/?search(module%20payandsave%20realestate)&gt(order,0)&select(key,path)');
80
        $results = $client->getResults();
81
82
        // This are the weighted important result, payAndSave and realEstate, order is not important.
83
        $importantResults = [
84
            $results[0]->key => $results[0]->path,
85
            $results[1]->key => $results[1]->path
86
        ];
87
88
        $this->assertArrayHasKey('payAndSave', $importantResults);
89
        $this->assertArrayHasKey('realEstate', $importantResults);
90
91
        // Now, there shall be 5 results as we have 5 path containing module
92
        $this->assertEquals(5, count($results));
93
    }
94
95
    /**
96
     * test if we can get list of modules paged
97
     *
98
     * @return void
99
     */
100
    public function testGetModuleWithPaging()
101
    {
102
        $client = static::createRestClient();
103
        $client->request('GET', '/core/module/?limit(1)');
104
        $response = $client->getResponse();
105
106
        $this->assertEquals(1, count($client->getResults()));
107
108
        $this->assertContains(
109
            '<http://localhost/core/module/?limit(1)>; rel="self"',
110
            explode(',', $response->headers->get('Link'))
111
        );
112
113
        $this->assertContains(
114
            '<http://localhost/core/module/?limit(1%2C1)>; rel="next"',
115
            $response->headers->get('Link')
116
        );
117
118
        $this->assertContains(
119
            '<http://localhost/core/module/?limit(1%2C5)>; rel="last"',
120
            $response->headers->get('Link')
121
        );
122
123
        $this->assertEquals('http://localhost/core/app/admin', $client->getResults()[0]->app->{'$ref'});
124
    }
125
126
    /**
127
     * check if RQL select() works on collections as expected..
128
     *
129
     * @return void
130
     */
131
    public function testRqlSelectOnCollection()
132
    {
133
        $client = static::createRestClient();
134
        $client->request('GET', '/core/module/?select(app.$ref,name,path)&sort(+key)');
135
136
        $results = $client->getResults();
137
138
        // count
139
        $this->assertEquals(6, count($client->getResults()));
140
141
        // is extref rendered as expected?
142
        $this->assertEquals('http://localhost/core/app/admin', $results[0]->app->{'$ref'});
143
144
        // what about translatable?
145
        $this->assertEquals('Admin Ref Module', $results[0]->name->en);
146
147
        // we didn't select 'key', make sure it's not there..
148
        $this->assertFalse(isset($results[0]->key));
149
    }
150
151
    /**
152
     * check if RQL select() works on items as expected..
153
     *
154
     * @return void
155
     */
156
    public function testRqlSelectOnItem()
157
    {
158
        $client = static::createRestClient();
159
        $client->request('GET', '/core/module/admin-AdminRef?select(app%2E$ref,name,path)');
160
161
        $results = $client->getResults();
162
163
        // is extref rendered as expected?
164
        $this->assertEquals('http://localhost/core/app/admin', $results->app->{'$ref'});
165
166
        // what about translatable?
167
        $this->assertEquals('Admin Ref Module', $results->name->en);
168
169
        // we didn't select 'key', make sure it's not there..
170
        $this->assertFalse(isset($results->key));
171
    }
172
173
    /**
174
     * check for empty collections when no fixtures are loaded
175
     *
176
     * @return void
177
     */
178 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...
179
    {
180
        // reset fixtures since we already have some from setUp
181
        $this->loadFixtures(array(), null, 'doctrine_mongodb');
182
        $client = static::createRestClient();
183
        $client->request('GET', '/core/module/');
184
185
        $response = $client->getResponse();
186
        $results = $client->getResults();
187
188
        $this->assertResponseContentType(self::COLLECTION_TYPE, $response);
189
190
        $this->assertEquals(array(), $results);
191
    }
192
193
    /**
194
     * test if we can get an module first by key and then its id (id is dynamic)
195
     *
196
     * @return void
197
     */
198
    public function testGetModuleWithKeyAndUseId()
199
    {
200
        $client = static::createRestClient();
201
        $client->request('GET', '/core/module/?eq(key,investment)');
202
        $response = $client->getResponse();
203
        $results = $client->getResults();
204
205
        $this->assertResponseContentType(self::COLLECTION_TYPE, $response);
206
        $this->assertEquals('investment', $results[0]->key);
207
        $this->assertEquals(1, count($results));
208
209
        // get entry by id
210
        $moduleId = $results[0]->id;
211
212
        $client = static::createRestClient();
213
        $client->request('GET', '/core/module/'.$moduleId);
214
        $response = $client->getResponse();
215
        $results = $client->getResults();
216
217
        $this->assertResponseContentType(self::CONTENT_TYPE, $response);
218
        $this->assertEquals($moduleId, $results->id);
219
        $this->assertEquals('investment', $results->key);
220
        $this->assertEquals('/module/investment', $results->path);
221
        $this->assertEquals(2, $results->order);
222
223
        $this->assertContains(
224
            '<http://localhost/core/module/'.$moduleId.'>; rel="self"',
225
            explode(',', $response->headers->get('Link'))
226
        );
227
        $this->assertEquals('*', $response->headers->get('Access-Control-Allow-Origin'));
228
    }
229
230
    /**
231
     * test finding of modules by ref
232
     *
233
     * @dataProvider findByAppRefProvider
234
     *
235
     * @param string  $ref   which reference to search in
236
     * @param mixed   $url   ref to search for
237
     * @param integer $count number of results to expect
238
     *
239
     * @return void
240
     */
241
    public function testFindByAppRef($ref, $url, $count)
242
    {
243
        $this->loadFixtures(
244
            [
245
                'Graviton\I18nBundle\DataFixtures\MongoDB\LoadLanguageData',
246
                'GravitonDyn\ModuleBundle\DataFixtures\MongoDB\LoadModuleData',
247
                'Graviton\CoreBundle\DataFixtures\MongoDB\LoadAppData',
248
                'Graviton\CoreBundle\DataFixtures\MongoDB\LoadProductData',
249
            ],
250
            null,
251
            'doctrine_mongodb'
252
        );
253
254
        $url = sprintf(
255
            '/core/module/?%s=%s',
256
            $this->encodeRqlString($ref),
257
            $this->encodeRqlString($url)
258
        );
259
260
        $client = static::createRestClient();
261
        $client->request('GET', $url);
262
        $results = $client->getResults();
263
        $this->assertCount($count, $results);
264
    }
265
266
    /**
267
     * @return array
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...
268
     */
269 View Code Duplication
    public function findByAppRefProvider()
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...
270
    {
271
        return [
272
            'find all tablet records' => [
273
                'app.$ref',
274
                'http://localhost/core/app/tablet',
275
                5
276
            ],
277
            'find a linked record when searching for ref' => [
278
                'app.$ref',
279
                'http://localhost/core/app/admin',
280
                1
281
            ],
282
            'find nothing when searching for inextistant (and unlinked) ref' => [
283
                'app.$ref',
284
                'http://localhost/core/app/inexistant',
285
                0
286
            ],
287
            'return nothing when searching with incomplete ref' => [
288
                'app.$ref',
289
                'http://localhost/core/app',
290
                0
291
            ],
292
        ];
293
    }
294
295
    /**
296
     * Apply RQL operators to extref fields
297
     *
298
     * @dataProvider dataExtrefOperators
299
     *
300
     * @param string $rqlQuery    RQL query
301
     * @param array  $expectedIds Expected found IDs
302
     *
303
     * @return void
304
     */
305
    public function testExtrefOperators($rqlQuery, array $expectedIds)
306
    {
307
        $this->loadFixtures(
308
            [
309
                'Graviton\I18nBundle\DataFixtures\MongoDB\LoadLanguageData',
310
                'GravitonDyn\ModuleBundle\DataFixtures\MongoDB\LoadModuleData',
311
                'Graviton\CoreBundle\DataFixtures\MongoDB\LoadAppData',
312
                'Graviton\CoreBundle\DataFixtures\MongoDB\LoadProductData',
313
            ],
314
            null,
315
            'doctrine_mongodb'
316
        );
317
318
        $client = static::createRestClient();
319
        $client->request('GET', '/core/module/?'.$rqlQuery);
320
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
321
322
        $foundIds = array_map(
323
            function ($item) {
324
                return $item->id;
325
            },
326
            $client->getResults()
327
        );
328
329
        sort($foundIds);
330
        sort($expectedIds);
331
        $this->assertEquals($expectedIds, $foundIds);
332
    }
333
334
    /**
335
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array<string|array>>.

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...
336
     */
337
    public function dataExtrefOperators()
338
    {
339
        $tabletIds = [
340
            'tablet-realEstate',
341
            'tablet-investment',
342
            'tablet-retirement',
343
            'tablet-requisition',
344
            'tablet-payAndSave',
345
        ];
346
        $adminIds = [
347
            'admin-AdminRef',
348
        ];
349
350
        return [
351
            '== tablet' => [
352
                sprintf(
353
                    '%s=%s',
354
                    $this->encodeRqlString('app.$ref'),
355
                    $this->encodeRqlString('http://localhost/core/app/tablet')
356
                ),
357
                $tabletIds,
358
            ],
359
            '!= tablet' => [
360
                sprintf(
361
                    '%s!=%s',
362
                    $this->encodeRqlString('app.$ref'),
363
                    $this->encodeRqlString('http://localhost/core/app/tablet')
364
                ),
365
                $adminIds,
366
            ],
367
            '> tablet' => [
368
                sprintf(
369
                    '%s>%s',
370
                    $this->encodeRqlString('app.$ref'),
371
                    $this->encodeRqlString('http://localhost/core/app/tablet')
372
                ),
373
                [],
374
            ],
375
            '< tablet' => [
376
                sprintf(
377
                    '%s<%s',
378
                    $this->encodeRqlString('app.$ref'),
379
                    $this->encodeRqlString('http://localhost/core/app/tablet')
380
                ),
381
                $adminIds,
382
            ],
383
            '>= tablet' => [
384
                sprintf(
385
                    '%s>=%s',
386
                    $this->encodeRqlString('app.$ref'),
387
                    $this->encodeRqlString('http://localhost/core/app/tablet')
388
                ),
389
                $tabletIds,
390
            ],
391
            '<= tablet' => [
392
                sprintf(
393
                    '%s<=%s',
394
                    $this->encodeRqlString('app.$ref'),
395
                    $this->encodeRqlString('http://localhost/core/app/tablet')
396
                ),
397
                array_merge($tabletIds, $adminIds),
398
            ],
399
            '=in= tablet' => [
400
                sprintf(
401
                    '%s=in=(%s)',
402
                    $this->encodeRqlString('app.$ref'),
403
                    $this->encodeRqlString('http://localhost/core/app/tablet')
404
                ),
405
                $tabletIds,
406
            ],
407
            '=out= tablet' => [
408
                sprintf(
409
                    '%s=out=(%s)',
410
                    $this->encodeRqlString('app.$ref'),
411
                    $this->encodeRqlString('http://localhost/core/app/tablet')
412
                ),
413
                $adminIds,
414
            ],
415
416
            '> admin' => [
417
                sprintf(
418
                    '%s>%s',
419
                    $this->encodeRqlString('app.$ref'),
420
                    $this->encodeRqlString('http://localhost/core/app/admin')
421
                ),
422
                $tabletIds,
423
            ],
424
            '< admin' => [
425
                sprintf(
426
                    '%s<%s',
427
                    $this->encodeRqlString('app.$ref'),
428
                    $this->encodeRqlString('http://localhost/core/app/admin')
429
                ),
430
                [],
431
            ],
432
            '>= admin' => [
433
                sprintf(
434
                    '%s>=%s',
435
                    $this->encodeRqlString('app.$ref'),
436
                    $this->encodeRqlString('http://localhost/core/app/admin')
437
                ),
438
                array_merge($tabletIds, $adminIds),
439
            ],
440
            '<= admin' => [
441
                sprintf(
442
                    '%s<=%s',
443
                    $this->encodeRqlString('app.$ref'),
444
                    $this->encodeRqlString('http://localhost/core/app/admin')
445
                ),
446
                $adminIds,
447
            ],
448
            '=in= admin' => [
449
                sprintf(
450
                    '%s=in=(%s)',
451
                    $this->encodeRqlString('app.$ref'),
452
                    $this->encodeRqlString('http://localhost/core/app/admin')
453
                ),
454
                $adminIds,
455
            ],
456
            '=out= admin' => [
457
                sprintf(
458
                    '%s=out=(%s)',
459
                    $this->encodeRqlString('app.$ref'),
460
                    $this->encodeRqlString('http://localhost/core/app/admin')
461
                ),
462
                $tabletIds,
463
            ],
464
465
            '=in= admin, tablet' => [
466
                sprintf(
467
                    '%s=in=(%s,%s)',
468
                    $this->encodeRqlString('app.$ref'),
469
                    $this->encodeRqlString('http://localhost/core/app/admin'),
470
                    $this->encodeRqlString('http://localhost/core/app/tablet')
471
                ),
472
                array_merge($adminIds, $tabletIds),
473
            ],
474
            '=out= admin, tablet' => [
475
                sprintf(
476
                    '%s=out=(%s,%s)',
477
                    $this->encodeRqlString('app.$ref'),
478
                    $this->encodeRqlString('http://localhost/core/app/admin'),
479
                    $this->encodeRqlString('http://localhost/core/app/tablet')
480
                ),
481
                [],
482
            ],
483
484
            '== admin || == tablet' => [
485
                sprintf(
486
                    '(%s==%s|%s==%s)',
487
                    $this->encodeRqlString('app.$ref'),
488
                    $this->encodeRqlString('http://localhost/core/app/admin'),
489
                    $this->encodeRqlString('app.$ref'),
490
                    $this->encodeRqlString('http://localhost/core/app/tablet')
491
                ),
492
                array_merge($adminIds, $tabletIds),
493
            ],
494
            '== admin && == tablet' => [
495
                sprintf(
496
                    '(%s==%s&%s==%s)',
497
                    $this->encodeRqlString('app.$ref'),
498
                    $this->encodeRqlString('http://localhost/core/app/admin'),
499
                    $this->encodeRqlString('app.$ref'),
500
                    $this->encodeRqlString('http://localhost/core/app/tablet')
501
                ),
502
                [],
503
            ],
504
505
            '== admin || some logic' => [
506
                sprintf(
507
                    'or(eq(%s,%s),and(eq(id,%s),eq(id,%s)))',
508
                    $this->encodeRqlString('app.$ref'),
509
                    $this->encodeRqlString('http://localhost/core/app/admin'),
510
                    $this->encodeRqlString('not-existing-id-1'),
511
                    $this->encodeRqlString('not-existing-id-2')
512
                ),
513
                $adminIds,
514
            ],
515
            '== tablet && some logic' => [
516
                sprintf(
517
                    'and(eq(%s,%s),or(eq(id,%s),eq(id,%s)))',
518
                    $this->encodeRqlString('app.$ref'),
519
                    $this->encodeRqlString('http://localhost/core/app/tablet'),
520
                    $this->encodeRqlString($tabletIds[0]),
521
                    $this->encodeRqlString($tabletIds[1])
522
                ),
523
                [$tabletIds[0], $tabletIds[1]]
524
            ],
525
        ];
526
    }
527
528
    /**
529
     * test if we can create a module through POST
530
     *
531
     * @return void
532
     */
533
    public function testPostModule()
534
    {
535
        $testModule = new \stdClass;
536
        $testModule->key = 'test';
537
        $testModule->app = new \stdClass;
538
        $testModule->app->{'$ref'} = 'http://localhost/core/app/testapp';
539
        $testModule->name = new \stdClass;
540
        $testModule->name->en = 'Name';
541
        $testModule->path = '/test/test';
542
        $testModule->order = 50;
543
544
        $client = static::createRestClient();
545
        $client->post('/core/module/', $testModule);
546
        $response = $client->getResponse();
547
        $this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode());
548
549
        $client = static::createRestClient();
550
        $client->request('GET', $response->headers->get('Location'));
551
        $response = $client->getResponse();
552
        $results = $client->getResults();
553
554
        $this->assertResponseContentType(self::CONTENT_TYPE, $response);
555
556
        $this->assertEquals('http://localhost/core/app/testapp', $results->app->{'$ref'});
557
        $this->assertEquals(50, $results->order);
558
559
        $this->assertContains(
560
            '<http://localhost/core/module/'.$results->id.'>; rel="self"',
561
            explode(',', $response->headers->get('Link'))
562
        );
563
    }
564
565
    /**
566
     * test validation error for int types
567
     *
568
     * @return void
569
     */
570
    public function testPostInvalidInteger()
571
    {
572
        $testModule = new \stdClass;
573
        $testModule->key = 'test';
574
        $testModule->app = new \stdClass;
575
        $testModule->app->{'$ref'} = 'http://localhost/core/app/testapp';
576
        $testModule->name = new \stdClass;
577
        $testModule->name->en = 'Name';
578
        $testModule->path = '/test/test';
579
        $testModule->order = 'clearly a string';
580
581
        $client = static::createRestClient();
582
        $client->post('/core/module', $testModule);
583
584
        $response = $client->getResponse();
585
        $results = $client->getResults();
586
587
        $this->assertEquals(400, $response->getStatusCode());
588
589
        $this->assertContains('order', $results[0]->propertyPath);
590
        $this->assertEquals('String value found, but an integer is required', $results[0]->message);
591
    }
592
593
    /**
594
     * test updating module
595
     *
596
     * @return void
597
     */
598
    public function testPutModule()
599
    {
600
        // get id first..
601
        $client = static::createRestClient();
602
        $client->request('GET', '/core/module/?eq(key,investment)');
603
        $response = $client->getResponse();
604
        $results = $client->getResults();
605
606
        $this->assertResponseContentType(self::COLLECTION_TYPE, $response);
607
        $this->assertEquals('investment', $results[0]->key);
608
        $this->assertEquals(1, count($results));
609
610
        // get entry by id
611
        $moduleId = $results[0]->id;
612
613
        $putModule = new \stdClass();
614
        $putModule->id = $moduleId;
615
        $putModule->key = 'test';
616
        $putModule->app = new \stdClass;
617
        $putModule->app->{'$ref'} = 'http://localhost/core/app/test';
618
        $putModule->name = new \stdClass;
619
        $putModule->name->en = 'testerle';
620
        $putModule->path = '/test/test';
621
        $putModule->order = 500;
622
623
        $client = static::createRestClient();
624
        $client->put('/core/module/'.$moduleId, $putModule);
625
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
626
627
        $client = static::createRestClient();
628
        $client->request('GET', '/core/module/'.$moduleId);
629
        $response = $client->getResponse();
630
        $results = $client->getResults();
631
632
        $this->assertResponseContentType(self::CONTENT_TYPE, $response);
633
634
        $this->assertEquals($moduleId, $results->id);
635
        $this->assertEquals('http://localhost/core/app/test', $results->app->{'$ref'});
636
        $this->assertEquals(500, $results->order);
637
638
        $this->assertContains(
639
            '<http://localhost/core/module/'.$moduleId.'>; rel="self"',
640
            explode(',', $response->headers->get('Link'))
641
        );
642
        $this->assertEquals('*', $response->headers->get('Access-Control-Allow-Origin'));
643
    }
644
645
    /**
646
     * test deleting a module
647
     *
648
     * @return void
649
     */
650
    public function testDeleteModule()
651
    {
652
        // get id first..
653
        $client = static::createRestClient();
654
        $client->request('GET', '/core/module/?eq(key,investment)');
655
        $results = $client->getResults();
656
657
        // get entry by id
658
        $moduleId = $results[0]->id;
659
660
        $client = static::createRestClient();
661
        $client->request('DELETE', '/core/module/'.$moduleId);
662
663
        $response = $client->getResponse();
664
665
        $this->assertEquals(204, $response->getStatusCode());
666
        $this->assertEquals('*', $response->headers->get('Access-Control-Allow-Origin'));
667
        $this->assertEmpty($response->getContent());
668
669
        $client->request('GET', '/core/module/'.$moduleId);
670
        $this->assertEquals(404, $client->getResponse()->getStatusCode());
671
    }
672
673
    /**
674
     * Test extref transformation
675
     *
676
     * @return void
677
     */
678
    public function testExtRefTransformation()
679
    {
680
        $client = static::createRestClient();
681
682
        $client->request('GET', '/core/module/?eq(key,investment)');
683
        $results = $client->getResults();
684
        $this->assertCount(1, $results);
685
686
        $module = $results[0];
687
        $this->assertEquals('investment', $module->key);
688
        $this->assertEquals('http://localhost/core/app/tablet', $module->app->{'$ref'});
689
690
        $module->app->{'$ref'} = 'http://localhost/core/app/admin';
691
692
        $client = static::createRestClient();
693
        $client->put('/core/module/'.$module->id, $module);
694
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
695
696
697
        $client = static::createRestClient();
698
        $client->request('GET', '/core/module/'.$module->id);
699
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
700
701
        $module = $client->getResults();
702
        $this->assertEquals('http://localhost/core/app/admin', $module->app->{'$ref'});
703
    }
704
705
    /**
706
     * Test extref validation
707
     *
708
     * @return void
709
     */
710
    public function testExtReferenceValidation()
711
    {
712
        $client = static::createRestClient();
713
        $client->request('GET', '/core/module/?eq(key,investment)');
714
        $this->assertCount(1, $client->getResults());
715
716
        $module = $client->getResults()[0];
717
718
        $urls = [
719
            'http://localhost',
720
            'http://localhost/core',
721
            'http://localhost/core/app',
722
            'http://localhost/core/noapp/admin',
723
        ];
724
        foreach ($urls as $url) {
725
            $module->app->{'$ref'} = $url;
726
727
            $client = static::createRestClient();
728
            $client->put('/core/module/'.$module->id, $module);
729
            $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
730
            $this->assertEquals(
731
                [
732
                    (object) [
733
                        'propertyPath' => 'app.$ref',
734
                        'message' => sprintf('Value "%s" is not a valid extref.', $url)
735
                    ],
736
                ],
737
                $client->getResults()
738
            );
739
        }
740
    }
741
742
    /**
743
     * test getting collection schema.
744
     * i avoid retesting everything (covered in /core/app), this test only
745
     * asserts translatable & extref representation
746
     *
747
     * @return void
748
     */
749
    public function testGetModuleCollectionSchemaInformationFormat()
750
    {
751
        $client = static::createRestClient();
752
753
        $client->request('GET', '/schema/core/module/collection');
754
        $results = $client->getResults();
755
756
        $this->assertEquals('object', $results->items->properties->app->type);
757
        $this->assertEquals('string', $results->items->properties->app->properties->{'$ref'}->type);
758
        $this->assertEquals('extref', $results->items->properties->app->properties->{'$ref'}->format);
759
760
        $service = $results->items->properties->service;
761
        $this->assertEquals('array', $service->type);
762
        $this->assertEquals('object', $service->items->properties->name->type);
763
        $this->assertEquals('string', $service->items->properties->name->properties->en->type);
764
        $this->assertEquals(['object', 'null'], $service->items->properties->description->type);
765
        $this->assertEquals('string', $service->items->properties->description->properties->en->type);
766
        $this->assertEquals('object', $service->items->properties->service->type);
767
        $this->assertEquals('string', $service->items->properties->service->properties->{'$ref'}->type);
768
    }
769
770
    /**
771
     * Encode RQL string
772
     *
773
     * @param string $value Value
774
     * @return string
775
     */
776 View Code Duplication
    private function encodeRqlString($value)
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...
777
    {
778
        return strtr(
779
            rawurlencode($value),
780
            [
781
                '-' => '%2D',
782
                '_' => '%5F',
783
                '.' => '%2E',
784
                '~' => '%7E',
785
            ]
786
        );
787
    }
788
789
    /**
790
     * verify that finding stuff with dot in it works
791
     *
792
     * @return void
793
     */
794
    public function testSearchForDottedKeyInModule()
795
    {
796
        // Create element 1
797
        $testModule = new \stdClass;
798
        $testModule->key = 'i.can.haz.dot';
799
        $testModule->app = new \stdClass;
800
        $testModule->app->{'$ref'} = 'http://localhost/core/app/canhazdot';
801
        $testModule->name = new \stdClass;
802
        $testModule->name->en = 'My name iz different and haz not dot';
803
        $testModule->path = '/test/test';
804
        $testModule->order = 50;
805
806
        $client = static::createRestClient();
807
        $client->post('/core/module/', $testModule);
808
        $response = $client->getResponse();
809
        $this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode());
810
811
        // Create element 2
812
        $testModule = new \stdClass;
813
        $testModule->key = 'i.ban.haz.dot';
814
        $testModule->app = new \stdClass;
815
        $testModule->app->{'$ref'} = 'http://localhost/core/app/banhazdot';
816
        $testModule->name = new \stdClass;
817
        $testModule->name->en = 'My name iz different and ban not dot';
818
        $testModule->path = '/test/test';
819
        $testModule->order = 40;
820
821
        $client = static::createRestClient();
822
        $client->post('/core/module/', $testModule);
823
        $response = $client->getResponse();
824
        $this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode());
825
826
        // simple search, on second element
827
        $client = static::createRestClient();
828
829
        $client->request('GET', '/core/module/?search(i.ban)');
830
        $results = $client->getResults();
831
        $this->assertEquals(1, count($results));
832
833
        $module = $results[0];
834
        $this->assertEquals('i.ban.haz.dot', $module->key);
835
836
        // advanced search, on first element
837
        $client = static::createRestClient();
838
839
        $client->request('GET', '/core/module/?limit(2)&search(i.can)&gt(order,10)');
840
        $results = $client->getResults();
841
        $this->assertEquals(1, count($results));
842
843
        $module = $results[0];
844
        $this->assertEquals('i.can.haz.dot', $module->key);
845
846
        // advanced search, on first element
847
        $client = static::createRestClient();
848
849
        $client->request('GET', '/core/module/?limit(4)&search(haz.dot)&gt(order,10)');
850
        $results = $client->getResults();
851
        $this->assertEquals(2, count($results));
852
    }
853
}
854