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
|
|
|
public function testSearchIndex() |
57
|
|
|
{ |
58
|
|
|
$client = static::createRestClient(); |
59
|
|
|
$client->request('GET', '/core/module/?search(module)&select(key)'); |
60
|
|
|
$this->assertEquals('retirement', $client->getResults()[0]->key); |
61
|
|
|
$this->assertEquals('realEstate', $client->getResults()[1]->key); |
62
|
|
|
$this->assertEquals('investment', $client->getResults()[2]->key); |
63
|
|
|
$this->assertEquals('requisition', $client->getResults()[3]->key); |
64
|
|
|
$this->assertEquals('payAndSave', $client->getResults()[4]->key); |
65
|
|
|
|
66
|
|
|
$client = static::createRestClient(); |
67
|
|
|
$client->request('GET', '/core/module/?search(AdminRef)&select(key)'); |
68
|
|
|
$this->assertEquals('AdminRef', $client->getResults()[0]->key); |
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
|
|
|
|
80
|
|
|
$client->request('GET', '/core/module/?search(module%20adminref)>(order,0)&select(key,path)'); |
81
|
|
|
$results = $client->getResults(); |
82
|
|
|
|
83
|
|
|
$this->assertEquals('AdminRef', $results[0]->key); |
84
|
|
|
$this->assertEquals('retirement', $results[1]->key); |
85
|
|
|
$this->assertEquals('realEstate', $results[2]->key); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* test if we can get list of modules paged |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
public function testGetModuleWithPaging() |
94
|
|
|
{ |
95
|
|
|
$client = static::createRestClient(); |
96
|
|
|
$client->request('GET', '/core/module/?limit(1)'); |
97
|
|
|
$response = $client->getResponse(); |
98
|
|
|
|
99
|
|
|
$this->assertEquals(1, count($client->getResults())); |
100
|
|
|
|
101
|
|
|
$this->assertContains( |
102
|
|
|
'<http://localhost/core/module/?limit(1)>; rel="self"', |
103
|
|
|
explode(',', $response->headers->get('Link')) |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$this->assertContains( |
107
|
|
|
'<http://localhost/core/module/?limit(1%2C1)>; rel="next"', |
108
|
|
|
$response->headers->get('Link') |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$this->assertContains( |
112
|
|
|
'<http://localhost/core/module/?limit(1%2C5)>; rel="last"', |
113
|
|
|
$response->headers->get('Link') |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
$this->assertEquals('http://localhost/core/app/admin', $client->getResults()[0]->app->{'$ref'}); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* check for empty collections when no fixtures are loaded |
121
|
|
|
* |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
public function testFindAllEmptyCollection() |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
// reset fixtures since we already have some from setUp |
127
|
|
|
$this->loadFixtures(array(), null, 'doctrine_mongodb'); |
128
|
|
|
$client = static::createRestClient(); |
129
|
|
|
$client->request('GET', '/core/module/'); |
130
|
|
|
|
131
|
|
|
$response = $client->getResponse(); |
132
|
|
|
$results = $client->getResults(); |
133
|
|
|
|
134
|
|
|
$this->assertResponseContentType(self::COLLECTION_TYPE, $response); |
135
|
|
|
|
136
|
|
|
$this->assertEquals(array(), $results); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* test if we can get an module first by key and then its id (id is dynamic) |
141
|
|
|
* |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
public function testGetModuleWithKeyAndUseId() |
145
|
|
|
{ |
146
|
|
|
$client = static::createRestClient(); |
147
|
|
|
$client->request('GET', '/core/module/?eq(key,investment)'); |
148
|
|
|
$response = $client->getResponse(); |
149
|
|
|
$results = $client->getResults(); |
150
|
|
|
|
151
|
|
|
$this->assertResponseContentType(self::COLLECTION_TYPE, $response); |
152
|
|
|
$this->assertEquals('investment', $results[0]->key); |
153
|
|
|
$this->assertEquals(1, count($results)); |
154
|
|
|
|
155
|
|
|
// get entry by id |
156
|
|
|
$moduleId = $results[0]->id; |
157
|
|
|
|
158
|
|
|
$client = static::createRestClient(); |
159
|
|
|
$client->request('GET', '/core/module/'.$moduleId); |
160
|
|
|
$response = $client->getResponse(); |
161
|
|
|
$results = $client->getResults(); |
162
|
|
|
|
163
|
|
|
$this->assertResponseContentType(self::CONTENT_TYPE, $response); |
164
|
|
|
$this->assertEquals($moduleId, $results->id); |
165
|
|
|
$this->assertEquals('investment', $results->key); |
166
|
|
|
$this->assertEquals('/module/investment', $results->path); |
167
|
|
|
$this->assertEquals(2, $results->order); |
168
|
|
|
|
169
|
|
|
$this->assertContains( |
170
|
|
|
'<http://localhost/core/module/'.$moduleId.'>; rel="self"', |
171
|
|
|
explode(',', $response->headers->get('Link')) |
172
|
|
|
); |
173
|
|
|
$this->assertEquals('*', $response->headers->get('Access-Control-Allow-Origin')); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* test finding of modules by ref |
178
|
|
|
* |
179
|
|
|
* @dataProvider findByAppRefProvider |
180
|
|
|
* |
181
|
|
|
* @param string $ref which reference to search in |
182
|
|
|
* @param mixed $url ref to search for |
183
|
|
|
* @param integer $count number of results to expect |
184
|
|
|
* |
185
|
|
|
* @return void |
186
|
|
|
*/ |
187
|
|
|
public function testFindByAppRef($ref, $url, $count) |
188
|
|
|
{ |
189
|
|
|
$this->loadFixtures( |
190
|
|
|
[ |
191
|
|
|
'Graviton\I18nBundle\DataFixtures\MongoDB\LoadLanguageData', |
192
|
|
|
'GravitonDyn\ModuleBundle\DataFixtures\MongoDB\LoadModuleData', |
193
|
|
|
'Graviton\CoreBundle\DataFixtures\MongoDB\LoadAppData', |
194
|
|
|
'Graviton\CoreBundle\DataFixtures\MongoDB\LoadProductData', |
195
|
|
|
], |
196
|
|
|
null, |
197
|
|
|
'doctrine_mongodb' |
198
|
|
|
); |
199
|
|
|
|
200
|
|
|
$url = sprintf( |
201
|
|
|
'/core/module/?%s=%s', |
202
|
|
|
$this->encodeRqlString($ref), |
203
|
|
|
$this->encodeRqlString($url) |
204
|
|
|
); |
205
|
|
|
|
206
|
|
|
$client = static::createRestClient(); |
207
|
|
|
$client->request('GET', $url); |
208
|
|
|
$results = $client->getResults(); |
209
|
|
|
$this->assertCount($count, $results); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return array |
|
|
|
|
214
|
|
|
*/ |
215
|
|
View Code Duplication |
public function findByAppRefProvider() |
|
|
|
|
216
|
|
|
{ |
217
|
|
|
return [ |
218
|
|
|
'find all tablet records' => [ |
219
|
|
|
'app.$ref', |
220
|
|
|
'http://localhost/core/app/tablet', |
221
|
|
|
5 |
222
|
|
|
], |
223
|
|
|
'find a linked record when searching for ref' => [ |
224
|
|
|
'app.$ref', |
225
|
|
|
'http://localhost/core/app/admin', |
226
|
|
|
1 |
227
|
|
|
], |
228
|
|
|
'find nothing when searching for inextistant (and unlinked) ref' => [ |
229
|
|
|
'app.$ref', |
230
|
|
|
'http://localhost/core/app/inexistant', |
231
|
|
|
0 |
232
|
|
|
], |
233
|
|
|
'return nothing when searching with incomplete ref' => [ |
234
|
|
|
'app.$ref', |
235
|
|
|
'http://localhost/core/app', |
236
|
|
|
0 |
237
|
|
|
], |
238
|
|
|
]; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Apply RQL operators to extref fields |
243
|
|
|
* |
244
|
|
|
* @dataProvider dataExtrefOperators |
245
|
|
|
* |
246
|
|
|
* @param string $rqlQuery RQL query |
247
|
|
|
* @param array $expectedIds Expected found IDs |
248
|
|
|
* |
249
|
|
|
* @return void |
250
|
|
|
*/ |
251
|
|
|
public function testExtrefOperators($rqlQuery, array $expectedIds) |
252
|
|
|
{ |
253
|
|
|
$this->loadFixtures( |
254
|
|
|
[ |
255
|
|
|
'Graviton\I18nBundle\DataFixtures\MongoDB\LoadLanguageData', |
256
|
|
|
'GravitonDyn\ModuleBundle\DataFixtures\MongoDB\LoadModuleData', |
257
|
|
|
'Graviton\CoreBundle\DataFixtures\MongoDB\LoadAppData', |
258
|
|
|
'Graviton\CoreBundle\DataFixtures\MongoDB\LoadProductData', |
259
|
|
|
], |
260
|
|
|
null, |
261
|
|
|
'doctrine_mongodb' |
262
|
|
|
); |
263
|
|
|
|
264
|
|
|
$client = static::createRestClient(); |
265
|
|
|
$client->request('GET', '/core/module/?'.$rqlQuery); |
266
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
267
|
|
|
|
268
|
|
|
$foundIds = array_map( |
269
|
|
|
function ($item) { |
270
|
|
|
return $item->id; |
271
|
|
|
}, |
272
|
|
|
$client->getResults() |
273
|
|
|
); |
274
|
|
|
|
275
|
|
|
sort($foundIds); |
276
|
|
|
sort($expectedIds); |
277
|
|
|
$this->assertEquals($expectedIds, $foundIds); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @return array |
|
|
|
|
282
|
|
|
*/ |
283
|
|
|
public function dataExtrefOperators() |
284
|
|
|
{ |
285
|
|
|
$tabletIds = [ |
286
|
|
|
'tablet-realEstate', |
287
|
|
|
'tablet-investment', |
288
|
|
|
'tablet-retirement', |
289
|
|
|
'tablet-requisition', |
290
|
|
|
'tablet-payAndSave', |
291
|
|
|
]; |
292
|
|
|
$adminIds = [ |
293
|
|
|
'admin-AdminRef', |
294
|
|
|
]; |
295
|
|
|
|
296
|
|
|
return [ |
297
|
|
|
'== tablet' => [ |
298
|
|
|
sprintf( |
299
|
|
|
'%s=%s', |
300
|
|
|
$this->encodeRqlString('app.$ref'), |
301
|
|
|
$this->encodeRqlString('http://localhost/core/app/tablet') |
302
|
|
|
), |
303
|
|
|
$tabletIds, |
304
|
|
|
], |
305
|
|
|
'!= tablet' => [ |
306
|
|
|
sprintf( |
307
|
|
|
'%s!=%s', |
308
|
|
|
$this->encodeRqlString('app.$ref'), |
309
|
|
|
$this->encodeRqlString('http://localhost/core/app/tablet') |
310
|
|
|
), |
311
|
|
|
$adminIds, |
312
|
|
|
], |
313
|
|
|
'> tablet' => [ |
314
|
|
|
sprintf( |
315
|
|
|
'%s>%s', |
316
|
|
|
$this->encodeRqlString('app.$ref'), |
317
|
|
|
$this->encodeRqlString('http://localhost/core/app/tablet') |
318
|
|
|
), |
319
|
|
|
[], |
320
|
|
|
], |
321
|
|
|
'< tablet' => [ |
322
|
|
|
sprintf( |
323
|
|
|
'%s<%s', |
324
|
|
|
$this->encodeRqlString('app.$ref'), |
325
|
|
|
$this->encodeRqlString('http://localhost/core/app/tablet') |
326
|
|
|
), |
327
|
|
|
$adminIds, |
328
|
|
|
], |
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
|
|
|
array_merge($tabletIds, $adminIds), |
344
|
|
|
], |
345
|
|
|
'=in= tablet' => [ |
346
|
|
|
sprintf( |
347
|
|
|
'%s=in=(%s)', |
348
|
|
|
$this->encodeRqlString('app.$ref'), |
349
|
|
|
$this->encodeRqlString('http://localhost/core/app/tablet') |
350
|
|
|
), |
351
|
|
|
$tabletIds, |
352
|
|
|
], |
353
|
|
|
'=out= tablet' => [ |
354
|
|
|
sprintf( |
355
|
|
|
'%s=out=(%s)', |
356
|
|
|
$this->encodeRqlString('app.$ref'), |
357
|
|
|
$this->encodeRqlString('http://localhost/core/app/tablet') |
358
|
|
|
), |
359
|
|
|
$adminIds, |
360
|
|
|
], |
361
|
|
|
|
362
|
|
|
'> admin' => [ |
363
|
|
|
sprintf( |
364
|
|
|
'%s>%s', |
365
|
|
|
$this->encodeRqlString('app.$ref'), |
366
|
|
|
$this->encodeRqlString('http://localhost/core/app/admin') |
367
|
|
|
), |
368
|
|
|
$tabletIds, |
369
|
|
|
], |
370
|
|
|
'< admin' => [ |
371
|
|
|
sprintf( |
372
|
|
|
'%s<%s', |
373
|
|
|
$this->encodeRqlString('app.$ref'), |
374
|
|
|
$this->encodeRqlString('http://localhost/core/app/admin') |
375
|
|
|
), |
376
|
|
|
[], |
377
|
|
|
], |
378
|
|
|
'>= admin' => [ |
379
|
|
|
sprintf( |
380
|
|
|
'%s>=%s', |
381
|
|
|
$this->encodeRqlString('app.$ref'), |
382
|
|
|
$this->encodeRqlString('http://localhost/core/app/admin') |
383
|
|
|
), |
384
|
|
|
array_merge($tabletIds, $adminIds), |
385
|
|
|
], |
386
|
|
|
'<= admin' => [ |
387
|
|
|
sprintf( |
388
|
|
|
'%s<=%s', |
389
|
|
|
$this->encodeRqlString('app.$ref'), |
390
|
|
|
$this->encodeRqlString('http://localhost/core/app/admin') |
391
|
|
|
), |
392
|
|
|
$adminIds, |
393
|
|
|
], |
394
|
|
|
'=in= admin' => [ |
395
|
|
|
sprintf( |
396
|
|
|
'%s=in=(%s)', |
397
|
|
|
$this->encodeRqlString('app.$ref'), |
398
|
|
|
$this->encodeRqlString('http://localhost/core/app/admin') |
399
|
|
|
), |
400
|
|
|
$adminIds, |
401
|
|
|
], |
402
|
|
|
'=out= admin' => [ |
403
|
|
|
sprintf( |
404
|
|
|
'%s=out=(%s)', |
405
|
|
|
$this->encodeRqlString('app.$ref'), |
406
|
|
|
$this->encodeRqlString('http://localhost/core/app/admin') |
407
|
|
|
), |
408
|
|
|
$tabletIds, |
409
|
|
|
], |
410
|
|
|
|
411
|
|
|
'=in= admin, tablet' => [ |
412
|
|
|
sprintf( |
413
|
|
|
'%s=in=(%s,%s)', |
414
|
|
|
$this->encodeRqlString('app.$ref'), |
415
|
|
|
$this->encodeRqlString('http://localhost/core/app/admin'), |
416
|
|
|
$this->encodeRqlString('http://localhost/core/app/tablet') |
417
|
|
|
), |
418
|
|
|
array_merge($adminIds, $tabletIds), |
419
|
|
|
], |
420
|
|
|
'=out= admin, tablet' => [ |
421
|
|
|
sprintf( |
422
|
|
|
'%s=out=(%s,%s)', |
423
|
|
|
$this->encodeRqlString('app.$ref'), |
424
|
|
|
$this->encodeRqlString('http://localhost/core/app/admin'), |
425
|
|
|
$this->encodeRqlString('http://localhost/core/app/tablet') |
426
|
|
|
), |
427
|
|
|
[], |
428
|
|
|
], |
429
|
|
|
|
430
|
|
|
'== admin || == tablet' => [ |
431
|
|
|
sprintf( |
432
|
|
|
'(%s==%s|%s==%s)', |
433
|
|
|
$this->encodeRqlString('app.$ref'), |
434
|
|
|
$this->encodeRqlString('http://localhost/core/app/admin'), |
435
|
|
|
$this->encodeRqlString('app.$ref'), |
436
|
|
|
$this->encodeRqlString('http://localhost/core/app/tablet') |
437
|
|
|
), |
438
|
|
|
array_merge($adminIds, $tabletIds), |
439
|
|
|
], |
440
|
|
|
'== admin && == tablet' => [ |
441
|
|
|
sprintf( |
442
|
|
|
'(%s==%s&%s==%s)', |
443
|
|
|
$this->encodeRqlString('app.$ref'), |
444
|
|
|
$this->encodeRqlString('http://localhost/core/app/admin'), |
445
|
|
|
$this->encodeRqlString('app.$ref'), |
446
|
|
|
$this->encodeRqlString('http://localhost/core/app/tablet') |
447
|
|
|
), |
448
|
|
|
[], |
449
|
|
|
], |
450
|
|
|
|
451
|
|
|
'== admin || some logic' => [ |
452
|
|
|
sprintf( |
453
|
|
|
'or(eq(%s,%s),and(eq(id,%s),eq(id,%s)))', |
454
|
|
|
$this->encodeRqlString('app.$ref'), |
455
|
|
|
$this->encodeRqlString('http://localhost/core/app/admin'), |
456
|
|
|
$this->encodeRqlString('not-existing-id-1'), |
457
|
|
|
$this->encodeRqlString('not-existing-id-2') |
458
|
|
|
), |
459
|
|
|
$adminIds, |
460
|
|
|
], |
461
|
|
|
'== tablet && some logic' => [ |
462
|
|
|
sprintf( |
463
|
|
|
'and(eq(%s,%s),or(eq(id,%s),eq(id,%s)))', |
464
|
|
|
$this->encodeRqlString('app.$ref'), |
465
|
|
|
$this->encodeRqlString('http://localhost/core/app/tablet'), |
466
|
|
|
$this->encodeRqlString($tabletIds[0]), |
467
|
|
|
$this->encodeRqlString($tabletIds[1]) |
468
|
|
|
), |
469
|
|
|
[$tabletIds[0], $tabletIds[1]] |
470
|
|
|
], |
471
|
|
|
]; |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* test if we can create a module through POST |
476
|
|
|
* |
477
|
|
|
* @return void |
478
|
|
|
*/ |
479
|
|
|
public function testPostModule() |
480
|
|
|
{ |
481
|
|
|
$testModule = new \stdClass; |
482
|
|
|
$testModule->key = 'test'; |
483
|
|
|
$testModule->app = new \stdClass; |
484
|
|
|
$testModule->app->{'$ref'} = 'http://localhost/core/app/testapp'; |
485
|
|
|
$testModule->name = new \stdClass; |
486
|
|
|
$testModule->name->en = 'Name'; |
487
|
|
|
$testModule->path = '/test/test'; |
488
|
|
|
$testModule->order = 50; |
489
|
|
|
|
490
|
|
|
$client = static::createRestClient(); |
491
|
|
|
$client->post('/core/module/', $testModule); |
492
|
|
|
$response = $client->getResponse(); |
493
|
|
|
$this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode()); |
494
|
|
|
|
495
|
|
|
$client = static::createRestClient(); |
496
|
|
|
$client->request('GET', $response->headers->get('Location')); |
497
|
|
|
$response = $client->getResponse(); |
498
|
|
|
$results = $client->getResults(); |
499
|
|
|
|
500
|
|
|
$this->assertResponseContentType(self::CONTENT_TYPE, $response); |
501
|
|
|
|
502
|
|
|
$this->assertEquals('http://localhost/core/app/testapp', $results->app->{'$ref'}); |
503
|
|
|
$this->assertEquals(50, $results->order); |
504
|
|
|
|
505
|
|
|
$this->assertContains( |
506
|
|
|
'<http://localhost/core/module/'.$results->id.'>; rel="self"', |
507
|
|
|
explode(',', $response->headers->get('Link')) |
508
|
|
|
); |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* test validation error for int types |
513
|
|
|
* |
514
|
|
|
* @return void |
515
|
|
|
*/ |
516
|
|
|
public function testPostInvalidInteger() |
517
|
|
|
{ |
518
|
|
|
$testModule = new \stdClass; |
519
|
|
|
$testModule->key = 'test'; |
520
|
|
|
$testModule->app = new \stdClass; |
521
|
|
|
$testModule->app->{'$ref'} = 'http://localhost/core/app/testapp'; |
522
|
|
|
$testModule->name = new \stdClass; |
523
|
|
|
$testModule->name->en = 'Name'; |
524
|
|
|
$testModule->path = '/test/test'; |
525
|
|
|
$testModule->order = 'clearly a string'; |
526
|
|
|
|
527
|
|
|
$client = static::createRestClient(); |
528
|
|
|
$client->post('/core/module', $testModule); |
529
|
|
|
|
530
|
|
|
$response = $client->getResponse(); |
531
|
|
|
$results = $client->getResults(); |
532
|
|
|
|
533
|
|
|
$this->assertEquals(400, $response->getStatusCode()); |
534
|
|
|
|
535
|
|
|
$this->assertContains('order', $results[0]->propertyPath); |
536
|
|
|
$this->assertEquals('String value found, but an integer is required', $results[0]->message); |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
/** |
540
|
|
|
* test updating module |
541
|
|
|
* |
542
|
|
|
* @return void |
543
|
|
|
*/ |
544
|
|
|
public function testPutModule() |
545
|
|
|
{ |
546
|
|
|
// get id first.. |
547
|
|
|
$client = static::createRestClient(); |
548
|
|
|
$client->request('GET', '/core/module/?eq(key,investment)'); |
549
|
|
|
$response = $client->getResponse(); |
550
|
|
|
$results = $client->getResults(); |
551
|
|
|
|
552
|
|
|
$this->assertResponseContentType(self::COLLECTION_TYPE, $response); |
553
|
|
|
$this->assertEquals('investment', $results[0]->key); |
554
|
|
|
$this->assertEquals(1, count($results)); |
555
|
|
|
|
556
|
|
|
// get entry by id |
557
|
|
|
$moduleId = $results[0]->id; |
558
|
|
|
|
559
|
|
|
$putModule = new \stdClass(); |
560
|
|
|
$putModule->id = $moduleId; |
561
|
|
|
$putModule->key = 'test'; |
562
|
|
|
$putModule->app = new \stdClass; |
563
|
|
|
$putModule->app->{'$ref'} = 'http://localhost/core/app/test'; |
564
|
|
|
$putModule->name = new \stdClass; |
565
|
|
|
$putModule->name->en = 'testerle'; |
566
|
|
|
$putModule->path = '/test/test'; |
567
|
|
|
$putModule->order = 500; |
568
|
|
|
|
569
|
|
|
$client = static::createRestClient(); |
570
|
|
|
$client->put('/core/module/'.$moduleId, $putModule); |
571
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
572
|
|
|
|
573
|
|
|
$client = static::createRestClient(); |
574
|
|
|
$client->request('GET', '/core/module/'.$moduleId); |
575
|
|
|
$response = $client->getResponse(); |
576
|
|
|
$results = $client->getResults(); |
577
|
|
|
|
578
|
|
|
$this->assertResponseContentType(self::CONTENT_TYPE, $response); |
579
|
|
|
|
580
|
|
|
$this->assertEquals($moduleId, $results->id); |
581
|
|
|
$this->assertEquals('http://localhost/core/app/test', $results->app->{'$ref'}); |
582
|
|
|
$this->assertEquals(500, $results->order); |
583
|
|
|
|
584
|
|
|
$this->assertContains( |
585
|
|
|
'<http://localhost/core/module/'.$moduleId.'>; rel="self"', |
586
|
|
|
explode(',', $response->headers->get('Link')) |
587
|
|
|
); |
588
|
|
|
$this->assertEquals('*', $response->headers->get('Access-Control-Allow-Origin')); |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
/** |
592
|
|
|
* test deleting a module |
593
|
|
|
* |
594
|
|
|
* @return void |
595
|
|
|
*/ |
596
|
|
|
public function testDeleteModule() |
597
|
|
|
{ |
598
|
|
|
// get id first.. |
599
|
|
|
$client = static::createRestClient(); |
600
|
|
|
$client->request('GET', '/core/module/?eq(key,investment)'); |
601
|
|
|
$results = $client->getResults(); |
602
|
|
|
|
603
|
|
|
// get entry by id |
604
|
|
|
$moduleId = $results[0]->id; |
605
|
|
|
|
606
|
|
|
$client = static::createRestClient(); |
607
|
|
|
$client->request('DELETE', '/core/module/'.$moduleId); |
608
|
|
|
|
609
|
|
|
$response = $client->getResponse(); |
610
|
|
|
|
611
|
|
|
$this->assertEquals(204, $response->getStatusCode()); |
612
|
|
|
$this->assertEquals('*', $response->headers->get('Access-Control-Allow-Origin')); |
613
|
|
|
$this->assertEmpty($response->getContent()); |
614
|
|
|
|
615
|
|
|
$client->request('GET', '/core/module/'.$moduleId); |
616
|
|
|
$this->assertEquals(404, $client->getResponse()->getStatusCode()); |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
/** |
620
|
|
|
* Test extref transformation |
621
|
|
|
* |
622
|
|
|
* @return void |
623
|
|
|
*/ |
624
|
|
|
public function testExtRefTransformation() |
625
|
|
|
{ |
626
|
|
|
$client = static::createRestClient(); |
627
|
|
|
|
628
|
|
|
$client->request('GET', '/core/module/?eq(key,investment)'); |
629
|
|
|
$results = $client->getResults(); |
630
|
|
|
$this->assertCount(1, $results); |
631
|
|
|
|
632
|
|
|
$module = $results[0]; |
633
|
|
|
$this->assertEquals('investment', $module->key); |
634
|
|
|
$this->assertEquals('http://localhost/core/app/tablet', $module->app->{'$ref'}); |
635
|
|
|
|
636
|
|
|
$module->app->{'$ref'} = 'http://localhost/core/app/admin'; |
637
|
|
|
|
638
|
|
|
$client = static::createRestClient(); |
639
|
|
|
$client->put('/core/module/'.$module->id, $module); |
640
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
641
|
|
|
|
642
|
|
|
|
643
|
|
|
$client = static::createRestClient(); |
644
|
|
|
$client->request('GET', '/core/module/'.$module->id); |
645
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
646
|
|
|
|
647
|
|
|
$module = $client->getResults(); |
648
|
|
|
$this->assertEquals('http://localhost/core/app/admin', $module->app->{'$ref'}); |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
/** |
652
|
|
|
* Test extref validation |
653
|
|
|
* |
654
|
|
|
* @return void |
655
|
|
|
*/ |
656
|
|
|
public function testExtReferenceValidation() |
657
|
|
|
{ |
658
|
|
|
$client = static::createRestClient(); |
659
|
|
|
$client->request('GET', '/core/module/?eq(key,investment)'); |
660
|
|
|
$this->assertCount(1, $client->getResults()); |
661
|
|
|
|
662
|
|
|
$module = $client->getResults()[0]; |
663
|
|
|
|
664
|
|
|
$urls = [ |
665
|
|
|
'http://localhost', |
666
|
|
|
'http://localhost/core', |
667
|
|
|
'http://localhost/core/app', |
668
|
|
|
'http://localhost/core/noapp/admin', |
669
|
|
|
]; |
670
|
|
|
foreach ($urls as $url) { |
671
|
|
|
$module->app->{'$ref'} = $url; |
672
|
|
|
|
673
|
|
|
$client = static::createRestClient(); |
674
|
|
|
$client->put('/core/module/'.$module->id, $module); |
675
|
|
|
$this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode()); |
676
|
|
|
$this->assertEquals( |
677
|
|
|
[ |
678
|
|
|
(object) [ |
679
|
|
|
'propertyPath' => 'app.$ref', |
680
|
|
|
'message' => sprintf('Value "%s" is not a valid extref.', $url) |
681
|
|
|
], |
682
|
|
|
], |
683
|
|
|
$client->getResults() |
684
|
|
|
); |
685
|
|
|
} |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
/** |
689
|
|
|
* test getting collection schema. |
690
|
|
|
* i avoid retesting everything (covered in /core/app), this test only |
691
|
|
|
* asserts translatable & extref representation |
692
|
|
|
* |
693
|
|
|
* @return void |
694
|
|
|
*/ |
695
|
|
|
public function testGetModuleCollectionSchemaInformationFormat() |
696
|
|
|
{ |
697
|
|
|
$client = static::createRestClient(); |
698
|
|
|
|
699
|
|
|
$client->request('GET', '/schema/core/module/collection'); |
700
|
|
|
$results = $client->getResults(); |
701
|
|
|
|
702
|
|
|
$this->assertEquals('object', $results->items->properties->app->type); |
703
|
|
|
$this->assertEquals('string', $results->items->properties->app->properties->{'$ref'}->type); |
704
|
|
|
$this->assertEquals('extref', $results->items->properties->app->properties->{'$ref'}->format); |
705
|
|
|
|
706
|
|
|
$service = $results->items->properties->service; |
707
|
|
|
$this->assertEquals('array', $service->type); |
708
|
|
|
$this->assertEquals('object', $service->items->properties->name->type); |
709
|
|
|
$this->assertEquals('string', $service->items->properties->name->properties->en->type); |
710
|
|
|
$this->assertEquals(['object', 'null'], $service->items->properties->description->type); |
711
|
|
|
$this->assertEquals('string', $service->items->properties->description->properties->en->type); |
712
|
|
|
$this->assertEquals('object', $service->items->properties->service->type); |
713
|
|
|
$this->assertEquals('string', $service->items->properties->service->properties->{'$ref'}->type); |
714
|
|
|
} |
715
|
|
|
|
716
|
|
|
/** |
717
|
|
|
* Encode RQL string |
718
|
|
|
* |
719
|
|
|
* @param string $value Value |
720
|
|
|
* @return string |
721
|
|
|
*/ |
722
|
|
View Code Duplication |
private function encodeRqlString($value) |
|
|
|
|
723
|
|
|
{ |
724
|
|
|
return strtr( |
725
|
|
|
rawurlencode($value), |
726
|
|
|
[ |
727
|
|
|
'-' => '%2D', |
728
|
|
|
'_' => '%5F', |
729
|
|
|
'.' => '%2E', |
730
|
|
|
'~' => '%7E', |
731
|
|
|
] |
732
|
|
|
); |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
/** |
736
|
|
|
* verify that finding stuff with dot in it works |
737
|
|
|
* |
738
|
|
|
* @return void |
739
|
|
|
*/ |
740
|
|
|
public function testSearchForDottedKeyInModule() |
741
|
|
|
{ |
742
|
|
|
$testModule = new \stdClass; |
743
|
|
|
$testModule->key = 'i.can.haz.dot'; |
744
|
|
|
$testModule->app = new \stdClass; |
745
|
|
|
$testModule->app->{'$ref'} = 'http://localhost/core/app/canhazdot'; |
746
|
|
|
$testModule->name = new \stdClass; |
747
|
|
|
$testModule->name->en = 'My name iz different and haz not dot'; |
748
|
|
|
$testModule->path = '/test/test'; |
749
|
|
|
$testModule->order = 50; |
750
|
|
|
|
751
|
|
|
$client = static::createRestClient(); |
752
|
|
|
$client->post('/core/module/', $testModule); |
753
|
|
|
$response = $client->getResponse(); |
754
|
|
|
$this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode()); |
755
|
|
|
|
756
|
|
|
// simple search |
757
|
|
|
$client = static::createRestClient(); |
758
|
|
|
|
759
|
|
|
$client->request('GET', '/core/module/?search(can.haz)'); |
760
|
|
|
$results = $client->getResults(); |
761
|
|
|
$this->assertCount(1, $results); |
762
|
|
|
|
763
|
|
|
$module = $results[0]; |
764
|
|
|
$this->assertEquals('i.can.haz.dot', $module->key); |
765
|
|
|
|
766
|
|
|
// advanced search |
767
|
|
|
$client = static::createRestClient(); |
768
|
|
|
|
769
|
|
|
$client->request('GET', '/core/module/?limit(2)&search(can.haz)'); |
770
|
|
|
$results = $client->getResults(); |
771
|
|
|
$this->assertCount(1, $results); |
772
|
|
|
|
773
|
|
|
$module = $results[0]; |
774
|
|
|
$this->assertEquals('i.can.haz.dot', $module->key); |
775
|
|
|
} |
776
|
|
|
} |
777
|
|
|
|
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.