1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\Tests\graphql_core\Kernel\Entity; |
4
|
|
|
|
5
|
|
|
use Drupal\file\Entity\File; |
6
|
|
|
use Drupal\graphql\Utility\StringHelper; |
7
|
|
|
use Drupal\node\Entity\Node; |
8
|
|
|
use Drupal\Tests\graphql_core\Kernel\GraphQLContentTestBase; |
9
|
|
|
use GraphQL\Server\OperationParams; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Test basic entity fields. |
13
|
|
|
* |
14
|
|
|
* @group graphql_core |
15
|
|
|
*/ |
16
|
|
|
class EntityFieldValueTest extends GraphQLContentTestBase { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var File |
20
|
|
|
*/ |
21
|
|
|
protected $testFile; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var File |
25
|
|
|
*/ |
26
|
|
|
protected $testImage; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public static $modules = [ |
32
|
|
|
'link', |
33
|
|
|
'datetime', |
34
|
|
|
'image', |
35
|
|
|
'file', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
protected function setUp() { |
42
|
|
|
parent::setUp(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Test boolean fields. |
47
|
|
|
*/ |
48
|
|
|
public function testBoolean() { |
49
|
|
|
$this->addField('boolean', "field_boolean", FALSE); |
50
|
|
|
|
51
|
|
|
$this->mockNode([ |
52
|
|
|
'field_boolean' => TRUE, |
53
|
|
|
]); |
54
|
|
|
|
55
|
|
|
$this->assertGraphQLFields([ |
56
|
|
|
['NodeTest', 'fieldBoolean', 'Boolean'], |
57
|
|
|
]); |
58
|
|
|
|
59
|
|
|
$query = <<<GQL |
60
|
|
|
query { |
61
|
|
|
node { |
62
|
|
|
fieldBoolean |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
GQL; |
66
|
|
|
|
67
|
|
|
$metadata = $this->defaultCacheMetaData(); |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
$metadata->addCacheTags([ |
71
|
|
|
'config:field.storage.node.field_boolean', |
72
|
|
|
'entity_field_info', |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
$this->assertResults($query, [], [ |
76
|
|
|
'node' => [ |
77
|
|
|
'fieldBoolean' => TRUE, |
78
|
|
|
], |
79
|
|
|
], $metadata); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Test a simple text field. |
84
|
|
|
*/ |
85
|
|
|
public function testText() { |
86
|
|
|
$this->addField('text', "field_text", FALSE); |
87
|
|
|
$this->mockNode([ |
88
|
|
|
'field_text' => [ |
89
|
|
|
'value' => 'Foo', |
90
|
|
|
], |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
$this->assertGraphQLFields([ |
94
|
|
|
['NodeTest', 'fieldText', 'FieldNodeFieldText'], |
95
|
|
|
['FieldNodeFieldText', 'value', 'String'], |
96
|
|
|
['FieldNodeFieldText', 'processed', 'String'], |
97
|
|
|
['FieldNodeFieldText', 'format', 'String'], |
98
|
|
|
]); |
99
|
|
|
|
100
|
|
|
$query = <<<GQL |
101
|
|
|
query { |
102
|
|
|
node { |
103
|
|
|
fieldText { |
104
|
|
|
value |
105
|
|
|
processed |
106
|
|
|
format |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
GQL; |
111
|
|
|
|
112
|
|
|
$metadata = $this->defaultCacheMetaData(); |
113
|
|
|
$metadata->addCacheTags([ |
114
|
|
|
'config:field.storage.node.field_text', |
115
|
|
|
'entity_field_info', |
116
|
|
|
]); |
117
|
|
|
|
118
|
|
|
$this->assertResults($query, [], [ |
119
|
|
|
'node' => [ |
120
|
|
|
'fieldText' => [ |
121
|
|
|
'value' => 'Foo', |
122
|
|
|
'processed' => "<p>Foo</p>\n", |
123
|
|
|
'format' => null, |
124
|
|
|
], |
125
|
|
|
], |
126
|
|
|
], $metadata); |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Test filtered text fields. |
132
|
|
|
*/ |
133
|
|
|
public function testFilteredText() { |
134
|
|
|
$this->mockNode([ |
135
|
|
|
'body' => [ |
136
|
|
|
'value' => 'http://www.drupal.org', |
137
|
|
|
'format' => 'plain_text', |
138
|
|
|
], |
139
|
|
|
]); |
140
|
|
|
|
141
|
|
|
$this->assertGraphQLFields([ |
142
|
|
|
['NodeTest', 'body', 'FieldNodeBody'], |
143
|
|
|
['FieldNodeBody', 'format', 'String'], |
144
|
|
|
['FieldNodeBody', 'value', 'String'], |
145
|
|
|
['FieldNodeBody', 'processed', 'String'], |
146
|
|
|
['FieldNodeBody', 'summary', 'String'], |
147
|
|
|
['FieldNodeBody', 'summaryProcessed', 'String'], |
148
|
|
|
]); |
149
|
|
|
|
150
|
|
|
$query = <<<GQL |
151
|
|
|
query { |
152
|
|
|
node { |
153
|
|
|
body { |
154
|
|
|
value |
155
|
|
|
processed |
156
|
|
|
summary |
157
|
|
|
summaryProcessed |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
GQL; |
162
|
|
|
|
163
|
|
|
$metadata = $this->defaultCacheMetaData(); |
164
|
|
|
$metadata->addCacheTags([ |
165
|
|
|
'config:field.storage.node.body', |
166
|
|
|
'entity_field_info', |
167
|
|
|
]); |
168
|
|
|
|
169
|
|
|
$this->assertResults($query, [], [ |
170
|
|
|
'node' => [ |
171
|
|
|
'body' => [ |
172
|
|
|
'value' => 'http://www.drupal.org', |
173
|
|
|
'processed' => "<p><a href=\"http://www.drupal.org\">http://www.drupal.org</a></p>\n", |
174
|
|
|
'summary' => null, |
175
|
|
|
'summaryProcessed' => '', |
176
|
|
|
], |
177
|
|
|
], |
178
|
|
|
], $metadata); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Verify that fields are assigned correctly among bundles. |
183
|
|
|
*/ |
184
|
|
|
public function testFieldAssignment() { |
185
|
|
|
$this->createContentType(['type' => 'a']); |
186
|
|
|
$this->createContentType(['type' => 'b']); |
187
|
|
|
$this->addField('boolean', 'field_a', FALSE, 'A', 'a'); |
188
|
|
|
$this->addField('boolean', 'field_b', FALSE, 'B', 'b'); |
189
|
|
|
|
190
|
|
|
// Verify that the fields for a given bundle are there. |
191
|
|
|
$this->assertGraphQLFields([ |
192
|
|
|
['NodeA', 'fieldA', 'Boolean'], |
193
|
|
|
['NodeB', 'fieldB', 'Boolean'], |
194
|
|
|
]); |
195
|
|
|
|
196
|
|
|
// Verify that the fields of another bundle are *not* there. |
197
|
|
|
$this->assertGraphQLFields([ |
198
|
|
|
['NodeA', 'fieldB', 'Boolean'], |
199
|
|
|
['NodeB', 'fieldA', 'Boolean'], |
200
|
|
|
], TRUE); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Test if the basic fields are available on the interface. |
205
|
|
|
* |
206
|
|
|
* @dataProvider nodeValuesProvider |
207
|
|
|
* |
208
|
|
|
* @param array $actualFieldValues |
209
|
|
|
* @param array $expectedFieldValues |
210
|
|
|
*/ |
211
|
|
|
public function testRawValues($actualFieldValues, $expectedFieldValues) { |
212
|
|
|
$this->installEntitySchema('file'); |
213
|
|
|
$this->installSchema('file', ['file_usage']); |
214
|
|
|
$this->addField('text', "field_text"); |
215
|
|
|
$this->addField('boolean', "field_boolean"); |
216
|
|
|
$this->addField('link', "field_link"); |
217
|
|
|
$this->addField('integer', "field_integer"); |
218
|
|
|
$this->addField('float', "field_float"); |
219
|
|
|
$this->addField('decimal', "field_decimal"); |
220
|
|
|
$this->addField('datetime', "field_datetime"); |
221
|
|
|
$this->addField('timestamp', "field_timestamp"); |
222
|
|
|
$this->addField('email', "field_email"); |
223
|
|
|
$this->addField('string', "field_string"); |
224
|
|
|
$this->addField('entity_reference', 'field_reference'); |
225
|
|
|
$this->addField('file', 'field_file'); |
226
|
|
|
$this->addField('image', 'field_image'); |
227
|
|
|
|
228
|
|
|
// File 1 |
229
|
|
|
file_put_contents('public://example.txt', $this->randomMachineName()); |
230
|
|
|
$this->testFile = File::create([ |
231
|
|
|
'uri' => 'public://example.txt', |
232
|
|
|
]); |
233
|
|
|
$this->testFile->save(); |
234
|
|
|
|
235
|
|
|
// File 2 |
236
|
|
|
file_put_contents('public://example.png', $this->randomMachineName()); |
237
|
|
|
$this->testImage = File::create([ |
238
|
|
|
'uri' => 'public://example.png', |
239
|
|
|
]); |
240
|
|
|
$this->testImage->save(); |
241
|
|
|
$values = [ |
242
|
|
|
'title' => 'Test', |
243
|
|
|
'type' => 'test', |
244
|
|
|
]; |
245
|
|
|
|
246
|
|
|
$node = $this->createNode($values + $actualFieldValues); |
247
|
|
|
|
248
|
|
|
// Workaround for public file urls. |
249
|
|
|
$expectedFieldValues['fieldFile'][0]['entity']['url'] = file_create_url($this->testFile->getFileUri()); |
250
|
|
|
$expectedFieldValues['fieldFile'][1]['entity']['url'] = file_create_url($this->testImage->getFileUri()); |
251
|
|
|
$expectedFieldValues['fieldImage'][0]['entity']['url'] = file_create_url($this->testFile->getFileUri()); |
252
|
|
|
$expectedFieldValues['fieldImage'][1]['entity']['url'] = file_create_url($this->testImage->getFileUri()); |
253
|
|
|
|
254
|
|
|
$metadata = $this->defaultCacheMetaData(); |
255
|
|
|
$metadata->addCacheTags([ |
256
|
|
|
'config:field.storage.node.body', |
257
|
|
|
'config:field.storage.node.field_text', |
258
|
|
|
'config:field.storage.node.field_boolean', |
259
|
|
|
'config:field.storage.node.field_datetime', |
260
|
|
|
'config:field.storage.node.field_decimal', |
261
|
|
|
'config:field.storage.node.field_email', |
262
|
|
|
'config:field.storage.node.field_float', |
263
|
|
|
'config:field.storage.node.field_file', |
264
|
|
|
'config:field.storage.node.field_image', |
265
|
|
|
'config:field.storage.node.field_integer', |
266
|
|
|
'config:field.storage.node.field_link', |
267
|
|
|
'config:field.storage.node.field_string', |
268
|
|
|
'config:field.storage.node.field_timestamp', |
269
|
|
|
'config:field.storage.node.field_reference', |
270
|
|
|
'entity_field_info', |
271
|
|
|
'node:1', |
272
|
|
|
'file:1', |
273
|
|
|
'file:2', |
274
|
|
|
]); |
275
|
|
|
|
276
|
|
|
$this->assertResults($this->getQueryFromFile('raw_field_values.gql'), [ |
277
|
|
|
'path' => '/node/' . $node->id(), |
278
|
|
|
], [ |
279
|
|
|
'route' => ['entity' => $expectedFieldValues], |
280
|
|
|
], $metadata); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Data provider for testRawValues. |
285
|
|
|
* |
286
|
|
|
* @return array |
287
|
|
|
*/ |
288
|
|
|
public function nodeValuesProvider() { |
289
|
|
|
$fieldValues = [ |
290
|
|
|
'body' => [ |
291
|
|
|
'value' => 'test', |
292
|
|
|
'summary' => 'test summary', |
293
|
|
|
], |
294
|
|
|
'field_text' => ['a', 'b', 'c'], |
295
|
|
|
'field_boolean' => [TRUE, FALSE], |
296
|
|
|
'field_link' => [ |
297
|
|
|
[ |
298
|
|
|
'title' => 'Internal link', |
299
|
|
|
'uri' => 'internal:/node/1', |
300
|
|
|
'options' => ['attributes' => ['_target' => 'blank']], |
301
|
|
|
], [ |
302
|
|
|
'title' => 'External link', |
303
|
|
|
'uri' => 'http://drupal.org', |
304
|
|
|
'options' => ['attributes' => ['_target' => 'blank']], |
305
|
|
|
], |
306
|
|
|
], |
307
|
|
|
'field_integer' => [10, -5], |
308
|
|
|
'field_float' => [3.14145, -8.8], |
309
|
|
|
'field_decimal' => [10.5, -17.22], |
310
|
|
|
'field_datetime' => ['2017-01-01', '1900-01-01'], |
311
|
|
|
'field_timestamp' => [0, 300], |
312
|
|
|
'field_email' => ['[email protected]'], |
313
|
|
|
'field_string' => ['test', '123'], |
314
|
|
|
'field_reference' => [ |
315
|
|
|
['target_id' => 1], |
316
|
|
|
], |
317
|
|
|
'field_file' => [ |
318
|
|
|
[ |
319
|
|
|
'target_id' => 1, |
320
|
|
|
'display' => 0, |
321
|
|
|
'description' => 'description test 1', |
322
|
|
|
], |
323
|
|
|
[ |
324
|
|
|
'target_id' => 2, |
325
|
|
|
'display' => 1, |
326
|
|
|
'description' => 'description test 2', |
327
|
|
|
], |
328
|
|
|
], |
329
|
|
|
'field_image' => [ |
330
|
|
|
[ |
331
|
|
|
'target_id' => 1, |
332
|
|
|
'alt' => 'alt test 1', |
333
|
|
|
'title' => 'title test 1', |
334
|
|
|
'width' => 100, |
335
|
|
|
'height' => 50, |
336
|
|
|
], |
337
|
|
|
[ |
338
|
|
|
'target_id' => 2, |
339
|
|
|
'alt' => 'alt test 2', |
340
|
|
|
'title' => 'title test 2', |
341
|
|
|
'width' => 200, |
342
|
|
|
'height' => 100, |
343
|
|
|
], |
344
|
|
|
], |
345
|
|
|
]; |
346
|
|
|
|
347
|
|
|
$expected = [ |
348
|
|
|
'nid' => 1, |
349
|
|
|
'vid' => 1, |
350
|
|
|
'langcode' => [ |
351
|
|
|
'value' => 'en', |
352
|
|
|
], |
353
|
|
|
'type' => [ |
354
|
|
|
'targetId' => 'test', |
355
|
|
|
], |
356
|
|
|
'uid' => [ |
357
|
|
|
'targetId' => 0, |
358
|
|
|
'entity' => NULL, |
359
|
|
|
], |
360
|
|
|
'title' => 'Test', |
361
|
|
|
'status' => TRUE, |
362
|
|
|
'promote' => TRUE, |
363
|
|
|
'sticky' => FALSE, |
364
|
|
|
'revisionTranslationAffected' => TRUE, |
365
|
|
|
'body' => [ |
366
|
|
|
'value' => 'test', |
367
|
|
|
'summary' => 'test summary', |
368
|
|
|
'summaryProcessed' => "<p>test summary</p>\n", |
369
|
|
|
'processed' => "<p>test</p>\n", |
370
|
|
|
'format' => null, |
371
|
|
|
], |
372
|
|
|
'fieldText' => [ |
373
|
|
|
['value' => 'a'], |
374
|
|
|
['value' => 'b'], |
375
|
|
|
['value' => 'c'], |
376
|
|
|
], |
377
|
|
|
'fieldBoolean' => [ |
378
|
|
|
TRUE, |
379
|
|
|
FALSE, |
380
|
|
|
], |
381
|
|
|
'fieldLink' => [ |
382
|
|
|
['title' => 'Internal link', 'uri' => 'internal:/node/1', 'target' => 'blank', 'url' => ['internal' => '/node/1']], |
383
|
|
|
['title' => 'External link', 'uri' => 'http://drupal.org', 'target' => 'blank', 'url' => ['external' => 'http://drupal.org']], |
384
|
|
|
], |
385
|
|
|
'fieldInteger' => [ |
386
|
|
|
10, |
387
|
|
|
-5, |
388
|
|
|
], |
389
|
|
|
'fieldFloat' => [ |
390
|
|
|
3.14145, |
391
|
|
|
-8.8, |
392
|
|
|
], |
393
|
|
|
'fieldDecimal' => [ |
394
|
|
|
10.5, |
395
|
|
|
-17.22, |
396
|
|
|
], |
397
|
|
|
'fieldDatetime' => [ |
398
|
|
|
['value' => '2017-01-01'], |
399
|
|
|
['value' => '1900-01-01'], |
400
|
|
|
], |
401
|
|
|
'fieldTimestamp' => [ |
402
|
|
|
0, |
403
|
|
|
300, |
404
|
|
|
], |
405
|
|
|
'fieldEmail' => ['[email protected]'], |
406
|
|
|
'fieldString' => [ |
407
|
|
|
'test', |
408
|
|
|
'123', |
409
|
|
|
], |
410
|
|
|
'fieldReference' => [ |
411
|
|
|
[ |
412
|
|
|
'targetId' => 1, |
413
|
|
|
'entity' => [ |
414
|
|
|
'title' => 'Test', |
415
|
|
|
'fieldReference' => [ |
416
|
|
|
[ |
417
|
|
|
'targetId' => 1, |
418
|
|
|
'entity' => [ |
419
|
|
|
'title' => 'Test', |
420
|
|
|
], |
421
|
|
|
], |
422
|
|
|
], |
423
|
|
|
], |
424
|
|
|
], |
425
|
|
|
], |
426
|
|
|
'fieldFile' => [ |
427
|
|
|
[ |
428
|
|
|
'targetId' => 1, |
429
|
|
|
'display' => FALSE, |
430
|
|
|
'description' => 'description test 1', |
431
|
|
|
'entity' => [ |
432
|
|
|
// 'uri' => [ |
|
|
|
|
433
|
|
|
// 'value' => 'public://example.txt', |
434
|
|
|
// ], |
435
|
|
|
], |
436
|
|
|
], |
437
|
|
|
[ |
438
|
|
|
'targetId' => 2, |
439
|
|
|
'display' => TRUE, |
440
|
|
|
'description' => 'description test 2', |
441
|
|
|
'entity' => [ |
442
|
|
|
// 'uri' => [ |
|
|
|
|
443
|
|
|
// 'value' => 'public://example.png', |
444
|
|
|
// ], |
445
|
|
|
], |
446
|
|
|
], |
447
|
|
|
], |
448
|
|
|
'fieldImage' => [ |
449
|
|
|
[ |
450
|
|
|
'targetId' => 1, |
451
|
|
|
'alt' => 'alt test 1', |
452
|
|
|
'title' => 'title test 1', |
453
|
|
|
'width' => 100, |
454
|
|
|
'height' => 50, |
455
|
|
|
'entity' => [ |
456
|
|
|
// 'uri' => [ |
|
|
|
|
457
|
|
|
// 'value' => 'public://example.txt', |
458
|
|
|
// ], |
459
|
|
|
], |
460
|
|
|
], |
461
|
|
|
[ |
462
|
|
|
'targetId' => 2, |
463
|
|
|
'alt' => 'alt test 2', |
464
|
|
|
'title' => 'title test 2', |
465
|
|
|
'width' => 200, |
466
|
|
|
'height' => 100, |
467
|
|
|
'entity' => [ |
468
|
|
|
// 'uri' => [ |
|
|
|
|
469
|
|
|
// 'value' => 'public://example.png', |
470
|
|
|
// ], |
471
|
|
|
], |
472
|
|
|
], |
473
|
|
|
], |
474
|
|
|
]; |
475
|
|
|
|
476
|
|
|
return [ |
477
|
|
|
[$fieldValues, $expected], |
478
|
|
|
]; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
} |
482
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.