Completed
Push — master ( 4703e1...97bad7 )
by
unknown
11:22
created

ModuleControllerTest::testFindByAppRef()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 13
Ratio 100 %

Importance

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