1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\Tests\graphql_core\Kernel\Entity; |
4
|
|
|
|
5
|
|
|
use Drupal\Component\Utility\NestedArray; |
6
|
|
|
use Drupal\field\Entity\FieldConfig; |
7
|
|
|
use Drupal\field\Entity\FieldStorageConfig; |
8
|
|
|
use Drupal\file\Entity\File; |
9
|
|
|
use Drupal\simpletest\ContentTypeCreationTrait; |
10
|
|
|
use Drupal\simpletest\NodeCreationTrait; |
11
|
|
|
use Drupal\Tests\graphql\Kernel\GraphQLFileTestBase; |
12
|
|
|
use Drupal\user\Entity\Role; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Test basic entity fields. |
16
|
|
|
* |
17
|
|
|
* @group graphql_core |
18
|
|
|
*/ |
19
|
|
|
class EntityFieldValueTest extends GraphQLFileTestBase { |
20
|
|
|
use ContentTypeCreationTrait; |
21
|
|
|
use NodeCreationTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var File |
25
|
|
|
*/ |
26
|
|
|
protected $testFile; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var File |
30
|
|
|
*/ |
31
|
|
|
protected $testImage; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public static $modules = [ |
37
|
|
|
'graphql_core', |
38
|
|
|
'node', |
39
|
|
|
'field', |
40
|
|
|
'filter', |
41
|
|
|
'text', |
42
|
|
|
'graphql_core', |
43
|
|
|
'link', |
44
|
|
|
'datetime', |
45
|
|
|
'image', |
46
|
|
|
'file', |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
protected function setUp() { |
53
|
|
|
parent::setUp(); |
54
|
|
|
|
55
|
|
|
$this->installConfig(['node']); |
56
|
|
|
$this->installConfig(['filter']); |
57
|
|
|
$this->installConfig(['text']); |
58
|
|
|
$this->installEntitySchema('node'); |
59
|
|
|
$this->installEntitySchema('file'); |
60
|
|
|
$this->installSchema('file', ['file_usage']); |
61
|
|
|
|
62
|
|
|
$this->createContentType([ |
63
|
|
|
'type' => 'test', |
64
|
|
|
]); |
65
|
|
|
|
66
|
|
|
$this->addField('text', "field_text"); |
67
|
|
|
$this->addField('boolean', "field_boolean"); |
68
|
|
|
$this->addField('link', "field_link"); |
69
|
|
|
$this->addField('integer', "field_integer"); |
70
|
|
|
$this->addField('float', "field_float"); |
71
|
|
|
$this->addField('decimal', "field_decimal"); |
72
|
|
|
$this->addField('datetime', "field_datetime"); |
73
|
|
|
$this->addField('timestamp', "field_timestamp"); |
74
|
|
|
$this->addField('email', "field_email"); |
75
|
|
|
$this->addField('string', "field_string"); |
76
|
|
|
$this->addField('entity_reference', 'field_reference'); |
77
|
|
|
$this->addField('file', 'field_file'); |
78
|
|
|
$this->addField('image', 'field_image'); |
79
|
|
|
|
80
|
|
|
Role::load('anonymous') |
81
|
|
|
->grantPermission('access content') |
82
|
|
|
->grantPermission('access user profiles') |
83
|
|
|
->save(); |
84
|
|
|
|
85
|
|
|
// File 1 |
86
|
|
|
file_put_contents('public://example.txt', $this->randomMachineName()); |
87
|
|
|
$this->testFile = File::create([ |
88
|
|
|
'uri' => 'public://example.txt', |
89
|
|
|
]); |
90
|
|
|
$this->testFile->save(); |
91
|
|
|
|
92
|
|
|
// File 2 |
93
|
|
|
file_put_contents('public://example.png', $this->randomMachineName()); |
94
|
|
|
$this->testImage = File::create([ |
95
|
|
|
'uri' => 'public://example.png', |
96
|
|
|
]); |
97
|
|
|
$this->testImage->save(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Test if the basic fields are available on the interface. |
102
|
|
|
* |
103
|
|
|
* @dataProvider nodeValuesProvider |
104
|
|
|
*/ |
105
|
|
|
public function testRawValues($actualFieldValues, $expectedFieldValues) { |
106
|
|
|
$values = [ |
107
|
|
|
'title' => 'Test', |
108
|
|
|
'type' => 'test', |
109
|
|
|
]; |
110
|
|
|
$node = $this->createNode($values + $actualFieldValues); |
111
|
|
|
|
112
|
|
|
$result = $this->executeQueryFile('raw_field_values.gql', [ |
113
|
|
|
'path' => '/node/' . $node->id(), |
114
|
|
|
]); |
115
|
|
|
$resultNode = NestedArray::getValue($result, ['data', 'route', 'entity']); |
116
|
|
|
|
117
|
|
|
// Workaround for public file urls. |
118
|
|
|
$expectedFieldValues['fieldFile'][0]['entity']['url'] = file_create_url($this->testFile->getFileUri()); |
119
|
|
|
$expectedFieldValues['fieldFile'][1]['entity']['url'] = file_create_url($this->testImage->getFileUri()); |
120
|
|
|
$expectedFieldValues['fieldImage'][0]['entity']['url'] = file_create_url($this->testFile->getFileUri()); |
121
|
|
|
$expectedFieldValues['fieldImage'][1]['entity']['url'] = file_create_url($this->testImage->getFileUri()); |
122
|
|
|
|
123
|
|
|
$this->assertEquals($expectedFieldValues, $resultNode, 'Correct raw node values are returned.'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Data provider for testRawValues. |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
public function nodeValuesProvider() { |
132
|
|
|
$fieldValues = [ |
133
|
|
|
'body' => [ |
134
|
|
|
'value' => 'test', |
135
|
|
|
'summary' => 'test summary', |
136
|
|
|
], |
137
|
|
|
'field_text' => ['a', 'b', 'c'], |
138
|
|
|
'field_boolean' => [TRUE, FALSE], |
139
|
|
|
'field_link' => [ |
140
|
|
|
['title' => 'Internal link', 'uri' => 'internal:/node/1'], |
141
|
|
|
['title' => 'External link', 'uri' => 'http://drupal.org'], |
142
|
|
|
], |
143
|
|
|
'field_integer' => [10, -5], |
144
|
|
|
'field_float' => [3.14145, -8.8], |
145
|
|
|
'field_decimal' => [10.5, -17.22], |
146
|
|
|
'field_datetime' => ['2017-01-01', '1900-01-01'], |
147
|
|
|
'field_timestamp' => [0, 300], |
148
|
|
|
'field_email' => ['[email protected]'], |
149
|
|
|
'field_string' => ['test', '123'], |
150
|
|
|
'field_reference' => [ |
151
|
|
|
['target_id' => 1], |
152
|
|
|
], |
153
|
|
|
'field_file' => [ |
154
|
|
|
[ |
155
|
|
|
'target_id' => 1, |
156
|
|
|
'display' => 0, |
157
|
|
|
'description' => 'description test 1', |
158
|
|
|
], |
159
|
|
|
[ |
160
|
|
|
'target_id' => 2, |
161
|
|
|
'display' => 1, |
162
|
|
|
'description' => 'description test 2', |
163
|
|
|
], |
164
|
|
|
], |
165
|
|
|
'field_image' => [ |
166
|
|
|
[ |
167
|
|
|
'target_id' => 1, |
168
|
|
|
'alt' => 'alt test 1', |
169
|
|
|
'title' => 'title test 1', |
170
|
|
|
'width' => 100, |
171
|
|
|
'height' => 50, |
172
|
|
|
], |
173
|
|
|
[ |
174
|
|
|
'target_id' => 2, |
175
|
|
|
'alt' => 'alt test 2', |
176
|
|
|
'title' => 'title test 2', |
177
|
|
|
'width' => 200, |
178
|
|
|
'height' => 100, |
179
|
|
|
], |
180
|
|
|
], |
181
|
|
|
]; |
182
|
|
|
|
183
|
|
|
$expected = [ |
184
|
|
|
'nid' => 1, |
185
|
|
|
'vid' => 1, |
186
|
|
|
'langcode' => [ |
187
|
|
|
'value' => 'en', |
188
|
|
|
], |
189
|
|
|
'type' => [ |
190
|
|
|
'targetId' => 'test', |
191
|
|
|
], |
192
|
|
|
'uid' => [ |
193
|
|
|
'targetId' => 0, |
194
|
|
|
'entity' => NULL, |
195
|
|
|
], |
196
|
|
|
'title' => 'Test', |
197
|
|
|
'status' => 1, |
198
|
|
|
'promote' => 1, |
199
|
|
|
'sticky' => 0, |
200
|
|
|
'revisionTranslationAffected' => 1, |
201
|
|
|
'body' => [ |
202
|
|
|
'value' => 'test', |
203
|
|
|
'summary' => 'test summary', |
204
|
|
|
'summaryProcessed' => "<p>test summary</p>\n", |
205
|
|
|
'processed' => "<p>test</p>\n", |
206
|
|
|
'format' => NULL, |
207
|
|
|
], |
208
|
|
|
'fieldText' => [ |
209
|
|
|
['value' => 'a'], |
210
|
|
|
['value' => 'b'], |
211
|
|
|
['value' => 'c'], |
212
|
|
|
], |
213
|
|
|
'fieldBoolean' => [ |
214
|
|
|
TRUE, |
215
|
|
|
FALSE, |
216
|
|
|
], |
217
|
|
|
'fieldLink' => [ |
218
|
|
|
['title' => 'Internal link', 'uri' => 'internal:/node/1'], |
219
|
|
|
['title' => 'External link', 'uri' => 'http://drupal.org'], |
220
|
|
|
], |
221
|
|
|
'fieldInteger' => [ |
222
|
|
|
10, |
223
|
|
|
-5, |
224
|
|
|
], |
225
|
|
|
'fieldFloat' => [ |
226
|
|
|
3.14145, |
227
|
|
|
-8.8, |
228
|
|
|
], |
229
|
|
|
'fieldDecimal' => [ |
230
|
|
|
10.5, |
231
|
|
|
-17.22, |
232
|
|
|
], |
233
|
|
|
'fieldDatetime' => [ |
234
|
|
|
['value' => '2017-01-01'], |
235
|
|
|
['value' => '1900-01-01'], |
236
|
|
|
], |
237
|
|
|
'fieldTimestamp' => [ |
238
|
|
|
0, |
239
|
|
|
300, |
240
|
|
|
], |
241
|
|
|
'fieldEmail' => ['[email protected]'], |
242
|
|
|
'fieldString' => [ |
243
|
|
|
'test', |
244
|
|
|
'123', |
245
|
|
|
], |
246
|
|
|
'fieldReference' => [ |
247
|
|
|
[ |
248
|
|
|
'targetId' => 1, |
249
|
|
|
'entity' => [ |
250
|
|
|
'title' => 'Test', |
251
|
|
|
'fieldReference' => [ |
252
|
|
|
[ |
253
|
|
|
'targetId' => 1, |
254
|
|
|
'entity' => [ |
255
|
|
|
'title' => 'Test', |
256
|
|
|
], |
257
|
|
|
], |
258
|
|
|
], |
259
|
|
|
], |
260
|
|
|
], |
261
|
|
|
], |
262
|
|
|
'fieldFile' => [ |
263
|
|
|
[ |
264
|
|
|
'targetId' => 1, |
265
|
|
|
'display' => 0, |
266
|
|
|
'description' => 'description test 1', |
267
|
|
|
'entity' => [ |
268
|
|
|
'uri' => 'public://example.txt' |
269
|
|
|
], |
270
|
|
|
], |
271
|
|
|
[ |
272
|
|
|
'targetId' => 2, |
273
|
|
|
'display' => 1, |
274
|
|
|
'description' => 'description test 2', |
275
|
|
|
'entity' => [ |
276
|
|
|
'uri' => 'public://example.png' |
277
|
|
|
], |
278
|
|
|
], |
279
|
|
|
], |
280
|
|
|
'fieldImage' => [ |
281
|
|
|
[ |
282
|
|
|
'targetId' => 1, |
283
|
|
|
'alt' => 'alt test 1', |
284
|
|
|
'title' => 'title test 1', |
285
|
|
|
'width' => 100, |
286
|
|
|
'height' => 50, |
287
|
|
|
'entity' => [ |
288
|
|
|
'uri' => 'public://example.txt' |
289
|
|
|
], |
290
|
|
|
], |
291
|
|
|
[ |
292
|
|
|
'targetId' => 2, |
293
|
|
|
'alt' => 'alt test 2', |
294
|
|
|
'title' => 'title test 2', |
295
|
|
|
'width' => 200, |
296
|
|
|
'height' => 100, |
297
|
|
|
'entity' => [ |
298
|
|
|
'uri' => 'public://example.png' |
299
|
|
|
], |
300
|
|
|
], |
301
|
|
|
], |
302
|
|
|
]; |
303
|
|
|
|
304
|
|
|
return [ |
305
|
|
|
[$fieldValues, $expected], |
306
|
|
|
]; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Add a field to test content type. |
311
|
|
|
* |
312
|
|
|
* @param string $type |
313
|
|
|
* Field type. |
314
|
|
|
* @param string $fieldName |
315
|
|
|
* Field machine name. |
316
|
|
|
* @param string $label |
317
|
|
|
* Label for the field. |
318
|
|
|
*/ |
319
|
|
|
protected function addField($type, $fieldName, $label = 'Label') { |
320
|
|
|
FieldStorageConfig::create([ |
321
|
|
|
'field_name' => $fieldName, |
322
|
|
|
'entity_type' => 'node', |
323
|
|
|
'type' => $type, |
324
|
|
|
'cardinality' => -1, |
325
|
|
|
])->save(); |
326
|
|
|
FieldConfig::create([ |
327
|
|
|
'entity_type' => 'node', |
328
|
|
|
'bundle' => 'test', |
329
|
|
|
'field_name' => $fieldName, |
330
|
|
|
'label' => $label, |
331
|
|
|
])->save(); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
} |
335
|
|
|
|