Completed
Pull Request — develop (#592)
by
unknown
05:47
created

ModuleControllerTest   C

Complexity

Total Complexity 23

Size/Duplication

Total Lines 853
Duplicated Lines 9.5 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 5
dl 81
loc 853
rs 5.2093
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 16 1
A testSearchIndex() 0 17 1
A testSearchWeightedIndex() 0 21 1
B testGetModuleWithPaging() 0 25 1
A testSearchWithIntegerOrFloat() 0 19 1
A testRqlSelectOnCollection() 0 19 1
A testRqlSelectOnItem() 0 16 1
A testFindAllEmptyCollection() 14 14 1
B testGetModuleWithKeyAndUseId() 0 31 1
B findByAppRefProvider() 25 25 1
A testExtrefOperators() 17 17 1
B dataExtrefOperators() 0 190 1
B testPostModule() 0 31 1
A testPostInvalidInteger() 0 22 1
B testPutModule() 0 46 1
A testDeleteModule() 0 22 1
B testExtRefTransformation() 0 26 1
B testExtReferenceValidation() 0 31 2
A testGetModuleCollectionSchemaInformationFormat() 0 20 1
A encodeRqlString() 12 12 1
A testSearchForDottedKeyInModule() 0 65 1
A testFindByAppRef() 13 13 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/module
4
 */
5
6
namespace Graviton\CoreBundle\Tests\Controller;
7
8
use Graviton\Rql\Node\SearchNode;
9
use Graviton\TestBundle\Test\RestTestCase;
10
use Symfony\Component\HttpFoundation\Response;
11
12
/**
13
 * Basic functional test for /core/module.
14
 *
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @link     http://swisscom.ch
18
 */
19
class ModuleControllerTest extends RestTestCase
20
{
21
    /**
22
     * @const complete content type string expected on a resouce
23
     */
24
    const CONTENT_TYPE = 'application/json; charset=UTF-8; profile=http://localhost/schema/core/module/item';
25
26
    /**
27
     * @const corresponding vendorized schema mime type
28
     */
29
    const COLLECTION_TYPE = 'application/json; charset=UTF-8; profile=http://localhost/schema/core/module/collection';
30
31
    /**
32
     * setup client and load fixtures, generate search indexes separately
33
     *
34
     * @return void
35
     */
36
    public function setUp()
37
    {
38
        $this->loadFixtures(
39
            array(
40
                'Graviton\I18nBundle\DataFixtures\MongoDB\LoadLanguageData',
41
                'GravitonDyn\ModuleBundle\DataFixtures\MongoDB\LoadModuleData'
42
            ),
43
            null,
44
            'doctrine_mongodb'
45
        );
46
47
        SearchNode::getInstance()->resetSearchTerms();
48
        $this->setVerbosityLevel(1);
49
        $this->isDecorated(true);
50
        $this->runCommand('graviton:generate:build-indexes', [], true);
51
    }
52
53
    /**
54
     * testing the search in search index, combined with a select (RQL)
55
     *
56
     * @return void
57
     */
58
    public function testSearchIndex()
59
    {
60
        $client = static::createRestClient();
61
        $client->request('GET', '/core/module/?search(module)&select(key)');
62
63
        // should not find 'AdminRef'
64
        $this->assertEquals(5, count($client->getResults()));
65
66
        // New query, let's reset the test node
67
        SearchNode::getInstance()->resetSearchTerms();
68
69
        // the sixth
70
        $client = static::createRestClient();
71
        $client->request('GET', '/core/module/?search(AdminRef)&select(key)');
72
        $this->assertEquals('AdminRef', $client->getResults()[0]->key);
73
        $this->assertEquals(1, count($client->getResults()));
74
    }
75
76
    /**
77
     * testing the search index using second param, non sensitive, combined with a select (RQL)
78
     *
79
     * @return void
80
     */
81
    public function testSearchWeightedIndex()
82
    {
83
        $client = static::createRestClient();
84
        $client->request('GET', '/core/module/?search(module%20payandsave)&gt(order,0)&select(key,path)');
85
        $results = $client->getResults();
86
87
        // First is a very specific request
88
        $this->assertEquals('payAndSave', $results[0]->key);
89
        $this->assertEquals(1, count($results));
90
91
        // New query, let's reset the test node
92
        SearchNode::getInstance()->resetSearchTerms();
93
94
        // Now we search for a common name, module
95
        $client = static::createRestClient();
96
        $client->request('GET', '/core/module/?search(module)&gt(order,0)&select(key,path,order)');
97
        $results = $client->getResults();
98
99
        // Here we have all 5 testing modules
100
        $this->assertEquals(5, count($results));
101
    }
102
103
    /**
104
     * test if we can get list of modules paged
105
     *
106
     * @return void
107
     */
108
    public function testGetModuleWithPaging()
109
    {
110
        $client = static::createRestClient();
111
        $client->request('GET', '/core/module/?limit(1)');
112
        $response = $client->getResponse();
113
114
        $this->assertEquals(1, count($client->getResults()));
115
116
        $this->assertContains(
117
            '<http://localhost/core/module/?limit(1)>; rel="self"',
118
            explode(',', $response->headers->get('Link'))
119
        );
120
121
        $this->assertContains(
122
            '<http://localhost/core/module/?limit(1%2C1)>; rel="next"',
123
            $response->headers->get('Link')
124
        );
125
126
        $this->assertContains(
127
            '<http://localhost/core/module/?limit(1%2C5)>; rel="last"',
128
            $response->headers->get('Link')
129
        );
130
131
        $this->assertEquals('http://localhost/core/app/admin', $client->getResults()[0]->app->{'$ref'});
132
    }
133
134
    /**
135
     * Simple check that should return no error and no result for
136
     * integers or float search. Text Indexes are string. But should find data having ints or floats.
137
     *
138
     * @return void
139
     */
140
    public function testSearchWithIntegerOrFloat()
141
    {
142
        $client = static::createRestClient();
143
        $client->request('GET', '/core/module/?search(0)');
144
        $response = $client->getResponse();
145
        $this->assertEquals(200, (integer) $response->getStatusCode());
146
147
148
        $client = static::createRestClient();
149
        $client->request('GET', '/core/module/?search(123)');
150
        $response = $client->getResponse();
151
        $this->assertEquals(200, (integer) $response->getStatusCode());
152
153
154
        $client = static::createRestClient();
155
        $client->request('GET', '/core/module/?search(123.456)');
156
        $response = $client->getResponse();
157
        $this->assertEquals(200, (integer) $response->getStatusCode());
158
    }
159
160
    /**
161
     * check if RQL select() works on collections as expected..
162
     *
163
     * @return void
164
     */
165
    public function testRqlSelectOnCollection()
166
    {
167
        $client = static::createRestClient();
168
        $client->request('GET', '/core/module/?select(app.$ref,name,path)&sort(+key)');
169
170
        $results = $client->getResults();
171
172
        // count
173
        $this->assertEquals(6, count($client->getResults()));
174
175
        // is extref rendered as expected?
176
        $this->assertEquals('http://localhost/core/app/admin', $results[0]->app->{'$ref'});
177
178
        // what about translatable?
179
        $this->assertEquals('Admin Ref Module', $results[0]->name->en);
180
181
        // we didn't select 'key', make sure it's not there..
182
        $this->assertFalse(isset($results[0]->key));
183
    }
184
185
    /**
186
     * check if RQL select() works on items as expected..
187
     *
188
     * @return void
189
     */
190
    public function testRqlSelectOnItem()
191
    {
192
        $client = static::createRestClient();
193
        $client->request('GET', '/core/module/admin-AdminRef?select(app%2E$ref,name,path)');
194
195
        $results = $client->getResults();
196
197
        // is extref rendered as expected?
198
        $this->assertEquals('http://localhost/core/app/admin', $results->app->{'$ref'});
199
200
        // what about translatable?
201
        $this->assertEquals('Admin Ref Module', $results->name->en);
202
203
        // we didn't select 'key', make sure it's not there..
204
        $this->assertFalse(isset($results->key));
205
    }
206
207
    /**
208
     * check for empty collections when no fixtures are loaded
209
     *
210
     * @return void
211
     */
212 View Code Duplication
    public function testFindAllEmptyCollection()
213
    {
214
        // reset fixtures since we already have some from setUp
215
        $this->loadFixtures(array(), null, 'doctrine_mongodb');
216
        $client = static::createRestClient();
217
        $client->request('GET', '/core/module/');
218
219
        $response = $client->getResponse();
220
        $results = $client->getResults();
221
222
        $this->assertResponseContentType(self::COLLECTION_TYPE, $response);
223
224
        $this->assertEquals(array(), $results);
225
    }
226
227
    /**
228
     * test if we can get an module first by key and then its id (id is dynamic)
229
     *
230
     * @return void
231
     */
232
    public function testGetModuleWithKeyAndUseId()
233
    {
234
        $client = static::createRestClient();
235
        $client->request('GET', '/core/module/?eq(key,investment)');
236
        $response = $client->getResponse();
237
        $results = $client->getResults();
238
239
        $this->assertResponseContentType(self::COLLECTION_TYPE, $response);
240
        $this->assertEquals('investment', $results[0]->key);
241
        $this->assertEquals(1, count($results));
242
243
        // get entry by id
244
        $moduleId = $results[0]->id;
245
246
        $client = static::createRestClient();
247
        $client->request('GET', '/core/module/'.$moduleId);
248
        $response = $client->getResponse();
249
        $results = $client->getResults();
250
251
        $this->assertResponseContentType(self::CONTENT_TYPE, $response);
252
        $this->assertEquals($moduleId, $results->id);
253
        $this->assertEquals('investment', $results->key);
254
        $this->assertEquals('/module/investment', $results->path);
255
        $this->assertEquals(2, $results->order);
256
257
        $this->assertContains(
258
            '<http://localhost/core/module/'.$moduleId.'>; rel="self"',
259
            explode(',', $response->headers->get('Link'))
260
        );
261
        $this->assertEquals('*', $response->headers->get('Access-Control-Allow-Origin'));
262
    }
263
264
    /**
265
     * test finding of modules by ref
266
     *
267
     * @dataProvider findByAppRefProvider
268
     *
269
     * @param string  $ref   which reference to search in
270
     * @param mixed   $url   ref to search for
271
     * @param integer $count number of results to expect
272
     *
273
     * @return void
274
     */
275 View Code Duplication
    public function testFindByAppRef($ref, $url, $count)
276
    {
277
        $url = sprintf(
278
            '/core/module/?%s=%s',
279
            $this->encodeRqlString($ref),
280
            $this->encodeRqlString($url)
281
        );
282
283
        $client = static::createRestClient();
284
        $client->request('GET', $url);
285
        $results = $client->getResults();
286
        $this->assertCount($count, $results);
287
    }
288
289
    /**
290
     * @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...
291
     */
292 View Code Duplication
    public function findByAppRefProvider()
293
    {
294
        return [
295
            'find all tablet records' => [
296
                'app.$ref',
297
                'http://localhost/core/app/tablet',
298
                5
299
            ],
300
            'find a linked record when searching for ref' => [
301
                'app.$ref',
302
                'http://localhost/core/app/admin',
303
                1
304
            ],
305
            'find nothing when searching for inextistant (and unlinked) ref' => [
306
                'app.$ref',
307
                'http://localhost/core/app/inexistant',
308
                0
309
            ],
310
            'return nothing when searching with incomplete ref' => [
311
                'app.$ref',
312
                'http://localhost/core/app',
313
                0
314
            ],
315
        ];
316
    }
317
318
    /**
319
     * Apply RQL operators to extref fields
320
     *
321
     * @dataProvider dataExtrefOperators
322
     *
323
     * @param string $rqlQuery    RQL query
324
     * @param array  $expectedIds Expected found IDs
325
     *
326
     * @return void
327
     */
328 View Code Duplication
    public function testExtrefOperators($rqlQuery, array $expectedIds)
329
    {
330
        $client = static::createRestClient();
331
        $client->request('GET', '/core/module/?'.$rqlQuery);
332
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
333
334
        $foundIds = array_map(
335
            function ($item) {
336
                return $item->id;
337
            },
338
            $client->getResults()
339
        );
340
341
        sort($foundIds);
342
        sort($expectedIds);
343
        $this->assertEquals($expectedIds, $foundIds);
344
    }
345
346
    /**
347
     * @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...
348
     */
349
    public function dataExtrefOperators()
350
    {
351
        $tabletIds = [
352
            'tablet-realEstate',
353
            'tablet-investment',
354
            'tablet-retirement',
355
            'tablet-requisition',
356
            'tablet-payAndSave',
357
        ];
358
        $adminIds = [
359
            'admin-AdminRef',
360
        ];
361
362
        return [
363
            '== tablet' => [
364
                sprintf(
365
                    '%s=%s',
366
                    $this->encodeRqlString('app.$ref'),
367
                    $this->encodeRqlString('http://localhost/core/app/tablet')
368
                ),
369
                $tabletIds,
370
            ],
371
            '!= tablet' => [
372
                sprintf(
373
                    '%s!=%s',
374
                    $this->encodeRqlString('app.$ref'),
375
                    $this->encodeRqlString('http://localhost/core/app/tablet')
376
                ),
377
                $adminIds,
378
            ],
379
            '> tablet' => [
380
                sprintf(
381
                    '%s>%s',
382
                    $this->encodeRqlString('app.$ref'),
383
                    $this->encodeRqlString('http://localhost/core/app/tablet')
384
                ),
385
                [],
386
            ],
387
            '< tablet' => [
388
                sprintf(
389
                    '%s<%s',
390
                    $this->encodeRqlString('app.$ref'),
391
                    $this->encodeRqlString('http://localhost/core/app/tablet')
392
                ),
393
                $adminIds,
394
            ],
395
            '>= tablet' => [
396
                sprintf(
397
                    '%s>=%s',
398
                    $this->encodeRqlString('app.$ref'),
399
                    $this->encodeRqlString('http://localhost/core/app/tablet')
400
                ),
401
                $tabletIds,
402
            ],
403
            '<= tablet' => [
404
                sprintf(
405
                    '%s<=%s',
406
                    $this->encodeRqlString('app.$ref'),
407
                    $this->encodeRqlString('http://localhost/core/app/tablet')
408
                ),
409
                array_merge($tabletIds, $adminIds),
410
            ],
411
            '=in= tablet' => [
412
                sprintf(
413
                    '%s=in=(%s)',
414
                    $this->encodeRqlString('app.$ref'),
415
                    $this->encodeRqlString('http://localhost/core/app/tablet')
416
                ),
417
                $tabletIds,
418
            ],
419
            '=out= tablet' => [
420
                sprintf(
421
                    '%s=out=(%s)',
422
                    $this->encodeRqlString('app.$ref'),
423
                    $this->encodeRqlString('http://localhost/core/app/tablet')
424
                ),
425
                $adminIds,
426
            ],
427
428
            '> admin' => [
429
                sprintf(
430
                    '%s>%s',
431
                    $this->encodeRqlString('app.$ref'),
432
                    $this->encodeRqlString('http://localhost/core/app/admin')
433
                ),
434
                $tabletIds,
435
            ],
436
            '< admin' => [
437
                sprintf(
438
                    '%s<%s',
439
                    $this->encodeRqlString('app.$ref'),
440
                    $this->encodeRqlString('http://localhost/core/app/admin')
441
                ),
442
                [],
443
            ],
444
            '>= admin' => [
445
                sprintf(
446
                    '%s>=%s',
447
                    $this->encodeRqlString('app.$ref'),
448
                    $this->encodeRqlString('http://localhost/core/app/admin')
449
                ),
450
                array_merge($tabletIds, $adminIds),
451
            ],
452
            '<= admin' => [
453
                sprintf(
454
                    '%s<=%s',
455
                    $this->encodeRqlString('app.$ref'),
456
                    $this->encodeRqlString('http://localhost/core/app/admin')
457
                ),
458
                $adminIds,
459
            ],
460
            '=in= admin' => [
461
                sprintf(
462
                    '%s=in=(%s)',
463
                    $this->encodeRqlString('app.$ref'),
464
                    $this->encodeRqlString('http://localhost/core/app/admin')
465
                ),
466
                $adminIds,
467
            ],
468
            '=out= admin' => [
469
                sprintf(
470
                    '%s=out=(%s)',
471
                    $this->encodeRqlString('app.$ref'),
472
                    $this->encodeRqlString('http://localhost/core/app/admin')
473
                ),
474
                $tabletIds,
475
            ],
476
477
            '=in= admin, tablet' => [
478
                sprintf(
479
                    '%s=in=(%s,%s)',
480
                    $this->encodeRqlString('app.$ref'),
481
                    $this->encodeRqlString('http://localhost/core/app/admin'),
482
                    $this->encodeRqlString('http://localhost/core/app/tablet')
483
                ),
484
                array_merge($adminIds, $tabletIds),
485
            ],
486
            '=out= admin, tablet' => [
487
                sprintf(
488
                    '%s=out=(%s,%s)',
489
                    $this->encodeRqlString('app.$ref'),
490
                    $this->encodeRqlString('http://localhost/core/app/admin'),
491
                    $this->encodeRqlString('http://localhost/core/app/tablet')
492
                ),
493
                [],
494
            ],
495
496
            '== admin || == tablet' => [
497
                sprintf(
498
                    '(%s==%s|%s==%s)',
499
                    $this->encodeRqlString('app.$ref'),
500
                    $this->encodeRqlString('http://localhost/core/app/admin'),
501
                    $this->encodeRqlString('app.$ref'),
502
                    $this->encodeRqlString('http://localhost/core/app/tablet')
503
                ),
504
                array_merge($adminIds, $tabletIds),
505
            ],
506
            '== admin && == tablet' => [
507
                sprintf(
508
                    '(%s==%s&%s==%s)',
509
                    $this->encodeRqlString('app.$ref'),
510
                    $this->encodeRqlString('http://localhost/core/app/admin'),
511
                    $this->encodeRqlString('app.$ref'),
512
                    $this->encodeRqlString('http://localhost/core/app/tablet')
513
                ),
514
                [],
515
            ],
516
517
            '== admin || some logic' => [
518
                sprintf(
519
                    'or(eq(%s,%s),and(eq(id,%s),eq(id,%s)))',
520
                    $this->encodeRqlString('app.$ref'),
521
                    $this->encodeRqlString('http://localhost/core/app/admin'),
522
                    $this->encodeRqlString('not-existing-id-1'),
523
                    $this->encodeRqlString('not-existing-id-2')
524
                ),
525
                $adminIds,
526
            ],
527
            '== tablet && some logic' => [
528
                sprintf(
529
                    'and(eq(%s,%s),or(eq(id,%s),eq(id,%s)))',
530
                    $this->encodeRqlString('app.$ref'),
531
                    $this->encodeRqlString('http://localhost/core/app/tablet'),
532
                    $this->encodeRqlString($tabletIds[0]),
533
                    $this->encodeRqlString($tabletIds[1])
534
                ),
535
                [$tabletIds[0], $tabletIds[1]]
536
            ],
537
        ];
538
    }
539
540
    /**
541
     * test if we can create a module through POST
542
     *
543
     * @return void
544
     */
545
    public function testPostModule()
546
    {
547
        $testModule = new \stdClass;
548
        $testModule->key = 'test';
549
        $testModule->app = new \stdClass;
550
        $testModule->app->{'$ref'} = 'http://localhost/core/app/testapp';
551
        $testModule->name = new \stdClass;
552
        $testModule->name->en = 'Name';
553
        $testModule->path = '/test/test';
554
        $testModule->order = 50;
555
556
        $client = static::createRestClient();
557
        $client->post('/core/module/', $testModule);
558
        $response = $client->getResponse();
559
        $this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode());
560
561
        $client = static::createRestClient();
562
        $client->request('GET', $response->headers->get('Location'));
563
        $response = $client->getResponse();
564
        $results = $client->getResults();
565
566
        $this->assertResponseContentType(self::CONTENT_TYPE, $response);
567
568
        $this->assertEquals('http://localhost/core/app/testapp', $results->app->{'$ref'});
569
        $this->assertEquals(50, $results->order);
570
571
        $this->assertContains(
572
            '<http://localhost/core/module/'.$results->id.'>; rel="self"',
573
            explode(',', $response->headers->get('Link'))
574
        );
575
    }
576
577
    /**
578
     * test validation error for int types
579
     *
580
     * @return void
581
     */
582
    public function testPostInvalidInteger()
583
    {
584
        $testModule = new \stdClass;
585
        $testModule->key = 'test';
586
        $testModule->app = new \stdClass;
587
        $testModule->app->{'$ref'} = 'http://localhost/core/app/testapp';
588
        $testModule->name = new \stdClass;
589
        $testModule->name->en = 'Name';
590
        $testModule->path = '/test/test';
591
        $testModule->order = 'clearly a string';
592
593
        $client = static::createRestClient();
594
        $client->post('/core/module', $testModule);
595
596
        $response = $client->getResponse();
597
        $results = $client->getResults();
598
599
        $this->assertEquals(400, $response->getStatusCode());
600
601
        $this->assertContains('order', $results[0]->propertyPath);
602
        $this->assertEquals('String value found, but an integer is required', $results[0]->message);
603
    }
604
605
    /**
606
     * test updating module
607
     *
608
     * @return void
609
     */
610
    public function testPutModule()
611
    {
612
        // get id first..
613
        $client = static::createRestClient();
614
        $client->request('GET', '/core/module/?eq(key,investment)');
615
        $response = $client->getResponse();
616
        $results = $client->getResults();
617
618
        $this->assertResponseContentType(self::COLLECTION_TYPE, $response);
619
        $this->assertEquals('investment', $results[0]->key);
620
        $this->assertEquals(1, count($results));
621
622
        // get entry by id
623
        $moduleId = $results[0]->id;
624
625
        $putModule = new \stdClass();
626
        $putModule->id = $moduleId;
627
        $putModule->key = 'test';
628
        $putModule->app = new \stdClass;
629
        $putModule->app->{'$ref'} = 'http://localhost/core/app/test';
630
        $putModule->name = new \stdClass;
631
        $putModule->name->en = 'testerle';
632
        $putModule->path = '/test/test';
633
        $putModule->order = 500;
634
635
        $client = static::createRestClient();
636
        $client->put('/core/module/'.$moduleId, $putModule);
637
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
638
639
        $client = static::createRestClient();
640
        $client->request('GET', '/core/module/'.$moduleId);
641
        $response = $client->getResponse();
642
        $results = $client->getResults();
643
644
        $this->assertResponseContentType(self::CONTENT_TYPE, $response);
645
646
        $this->assertEquals($moduleId, $results->id);
647
        $this->assertEquals('http://localhost/core/app/test', $results->app->{'$ref'});
648
        $this->assertEquals(500, $results->order);
649
650
        $this->assertContains(
651
            '<http://localhost/core/module/'.$moduleId.'>; rel="self"',
652
            explode(',', $response->headers->get('Link'))
653
        );
654
        $this->assertEquals('*', $response->headers->get('Access-Control-Allow-Origin'));
655
    }
656
657
    /**
658
     * test deleting a module
659
     *
660
     * @return void
661
     */
662
    public function testDeleteModule()
663
    {
664
        // get id first..
665
        $client = static::createRestClient();
666
        $client->request('GET', '/core/module/?eq(key,investment)');
667
        $results = $client->getResults();
668
669
        // get entry by id
670
        $moduleId = $results[0]->id;
671
672
        $client = static::createRestClient();
673
        $client->request('DELETE', '/core/module/'.$moduleId);
674
675
        $response = $client->getResponse();
676
677
        $this->assertEquals(204, $response->getStatusCode());
678
        $this->assertEquals('*', $response->headers->get('Access-Control-Allow-Origin'));
679
        $this->assertEmpty($response->getContent());
680
681
        $client->request('GET', '/core/module/'.$moduleId);
682
        $this->assertEquals(404, $client->getResponse()->getStatusCode());
683
    }
684
685
    /**
686
     * Test extref transformation
687
     *
688
     * @return void
689
     */
690
    public function testExtRefTransformation()
691
    {
692
        $client = static::createRestClient();
693
694
        $client->request('GET', '/core/module/?eq(key,investment)');
695
        $results = $client->getResults();
696
        $this->assertCount(1, $results);
697
698
        $module = $results[0];
699
        $this->assertEquals('investment', $module->key);
700
        $this->assertEquals('http://localhost/core/app/tablet', $module->app->{'$ref'});
701
702
        $module->app->{'$ref'} = 'http://localhost/core/app/admin';
703
704
        $client = static::createRestClient();
705
        $client->put('/core/module/'.$module->id, $module);
706
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
707
708
709
        $client = static::createRestClient();
710
        $client->request('GET', '/core/module/'.$module->id);
711
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
712
713
        $module = $client->getResults();
714
        $this->assertEquals('http://localhost/core/app/admin', $module->app->{'$ref'});
715
    }
716
717
    /**
718
     * Test extref validation
719
     *
720
     * @return void
721
     */
722
    public function testExtReferenceValidation()
723
    {
724
        $client = static::createRestClient();
725
        $client->request('GET', '/core/module/?eq(key,investment)');
726
        $this->assertCount(1, $client->getResults());
727
728
        $module = $client->getResults()[0];
729
730
        $urls = [
731
            'http://localhost',
732
            'http://localhost/core',
733
            'http://localhost/core/app',
734
            'http://localhost/core/noapp/admin',
735
        ];
736
        foreach ($urls as $url) {
737
            $module->app->{'$ref'} = $url;
738
739
            $client = static::createRestClient();
740
            $client->put('/core/module/'.$module->id, $module);
741
            $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
742
            $this->assertEquals(
743
                [
744
                    (object) [
745
                        'propertyPath' => 'app.$ref',
746
                        'message' => sprintf('Value "%s" is not a valid extref.', $url)
747
                    ],
748
                ],
749
                $client->getResults()
750
            );
751
        }
752
    }
753
754
    /**
755
     * test getting collection schema.
756
     * i avoid retesting everything (covered in /core/app), this test only
757
     * asserts translatable & extref representation
758
     *
759
     * @return void
760
     */
761
    public function testGetModuleCollectionSchemaInformationFormat()
762
    {
763
        $client = static::createRestClient();
764
765
        $client->request('GET', '/schema/core/module/collection');
766
        $results = $client->getResults();
767
768
        $this->assertEquals('object', $results->items->properties->app->type);
769
        $this->assertEquals('string', $results->items->properties->app->properties->{'$ref'}->type);
770
        $this->assertEquals('extref', $results->items->properties->app->properties->{'$ref'}->format);
771
772
        $service = $results->items->properties->service;
773
        $this->assertEquals('array', $service->type);
774
        $this->assertEquals('object', $service->items->properties->name->type);
775
        $this->assertEquals('string', $service->items->properties->name->properties->en->type);
776
        $this->assertEquals(['object', 'null'], $service->items->properties->description->type);
777
        $this->assertEquals('string', $service->items->properties->description->properties->en->type);
778
        $this->assertEquals('object', $service->items->properties->service->type);
779
        $this->assertEquals('string', $service->items->properties->service->properties->{'$ref'}->type);
780
    }
781
782
    /**
783
     * Encode RQL string
784
     *
785
     * @param string $value Value
786
     * @return string
787
     */
788 View Code Duplication
    private function encodeRqlString($value)
789
    {
790
        return strtr(
791
            rawurlencode($value),
792
            [
793
                '-' => '%2D',
794
                '_' => '%5F',
795
                '.' => '%2E',
796
                '~' => '%7E',
797
            ]
798
        );
799
    }
800
801
    /**
802
     * verify that finding stuff with dot in it works
803
     *
804
     * @return void
805
     */
806
    public function testSearchForDottedKeyInModule()
807
    {
808
        // Create element 1
809
        $testModule = new \stdClass;
810
        $testModule->key = 'i.can.haz.dot';
811
        $testModule->app = new \stdClass;
812
        $testModule->app->{'$ref'} = 'http://localhost/core/app/canhazdot';
813
        $testModule->name = new \stdClass;
814
        $testModule->name->en = 'My name iz different and haz not dot';
815
        $testModule->path = '/test/test';
816
        $testModule->order = 50;
817
818
        $client = static::createRestClient();
819
        $client->post('/core/module/', $testModule);
820
        $response = $client->getResponse();
821
        $this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode());
822
823
        // Create element 2
824
        $testModule = new \stdClass;
825
        $testModule->key = 'i.ban.haz.dot';
826
        $testModule->app = new \stdClass;
827
        $testModule->app->{'$ref'} = 'http://localhost/core/app/banhazdot';
828
        $testModule->name = new \stdClass;
829
        $testModule->name->en = 'My name iz different and ban not dot';
830
        $testModule->path = '/test/test';
831
        $testModule->order = 40;
832
833
        $client = static::createRestClient();
834
        $client->post('/core/module/', $testModule);
835
        $response = $client->getResponse();
836
        $this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode());
837
838
        // simple search, on second element
839
        $client = static::createRestClient();
840
841
        $client->request('GET', '/core/module/?search(i.ban)');
842
        $results = $client->getResults();
843
        $this->assertEquals(1, count($results));
844
845
        // New query, let's reset the test node
846
        SearchNode::getInstance()->resetSearchTerms();
847
848
        $module = $results[0];
849
        $this->assertEquals('i.ban.haz.dot', $module->key);
850
851
        // advanced search, on first element
852
        $client = static::createRestClient();
853
854
        $client->request('GET', '/core/module/?limit(2)&search(i.can)&gt(order,10)');
855
        $results = $client->getResults();
856
        $this->assertEquals(1, count($results));
857
858
        // New query, let's reset the test node
859
        SearchNode::getInstance()->resetSearchTerms();
860
861
        $module = $results[0];
862
        $this->assertEquals('i.can.haz.dot', $module->key);
863
864
        // advanced search, on first element
865
        $client = static::createRestClient();
866
867
        $client->request('GET', '/core/module/?limit(4)&search(haz.dot)&gt(order,10)');
868
        $results = $client->getResults();
869
        $this->assertEquals(2, count($results));
870
    }
871
}
872