Completed
Pull Request — 8.x-3.x (#525)
by Philipp
02:33
created

EntityFieldValueTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
    $this->assertResults($query, [], [
68
      'node' => [
69
        'fieldBoolean' => TRUE,
70
      ],
71
    ], $this->defaultCacheMetaData());
72
  }
73
74
  /**
75
   * Test a simple text field.
76
   */
77
  public function testText() {
78
    $this->addField('text', "field_text", FALSE);
79
    $this->mockNode([
80
      'field_text' => [
81
        'value' => 'Foo',
82
      ],
83
    ]);
84
85
    $this->assertGraphQLFields([
86
      ['NodeTest', 'fieldText', 'FieldNodeFieldText'],
87
      ['FieldNodeFieldText', 'value', 'String'],
88
      ['FieldNodeFieldText', 'processed', 'String'],
89
      ['FieldNodeFieldText', 'format', 'String'],
90
    ]);
91
92
    $query = <<<GQL
93
query {
94
  node {
95
    fieldText {
96
      value
97
      processed
98
      format
99
    }
100
  }
101
}
102
GQL;
103
104
    $this->assertResults($query, [], [
105
      'node' => [
106
        'fieldText' => [
107
          0 => [
108
            'value' => 'Foo',
109
            'processed' => "<p>Foo</p>\n",
110
            'format' => 'null',
111
          ],
112
        ],
113
      ],
114
    ], $this->defaultCacheMetaData());
115
116
  }
117
118
  /**
119
   * Test filtered text fields.
120
   */
121
  public function testFilteredText() {
122
    $this->mockNode([
123
      'body' => [
124
        'value' => 'http://www.drupal.org',
125
        'format' => 'plain_text',
126
      ],
127
    ]);
128
129
    $this->assertGraphQLFields([
130
      ['NodeTest', 'body', 'FieldNodeBody'],
131
      ['FieldNodeBody', 'format', 'String'],
132
      ['FieldNodeBody', 'value', 'String'],
133
      ['FieldNodeBody', 'processed', 'String'],
134
      ['FieldNodeBody', 'summary', 'String'],
135
      ['FieldNodeBody', 'summaryProcessed', 'String'],
136
    ]);
137
138
    $query = <<<GQL
139
query {
140
  node {
141
    body {
142
      value
143
      processed
144
      summary
145
      summaryProcessed
146
    }
147
  }
148
}
149
GQL;
150
151
152
    $this->assertResults($query, [], [
153
      'node' => [
154
        'body' => [
155
          'value' => 'http://www.drupal.org',
156
          'processed' => "<p><a href=\"http://www.drupal.org\">http://www.drupal.org</a></p>\n",
157
          'summary' => 'null',
158
          'summaryProcessed' => '',
159
        ],
160
      ],
161
    ], $this->defaultCacheMetaData());
162
  }
163
164
  /**
165
   * Test if the basic fields are available on the interface.
166
   *
167
   * @dataProvider nodeValuesProvider
168
   *
169
   * @param array $actualFieldValues
170
   * @param array $expectedFieldValues
171
   */
172
  public function testRawValues($actualFieldValues, $expectedFieldValues) {
173
    $this->installEntitySchema('file');
174
    $this->installSchema('file', ['file_usage']);
175
    $this->addField('text', "field_text");
176
    $this->addField('boolean', "field_boolean");
177
    $this->addField('link', "field_link");
178
    $this->addField('integer', "field_integer");
179
    $this->addField('float', "field_float");
180
    $this->addField('decimal', "field_decimal");
181
    $this->addField('datetime', "field_datetime");
182
    $this->addField('timestamp', "field_timestamp");
183
    $this->addField('email', "field_email");
184
    $this->addField('string', "field_string");
185
    $this->addField('entity_reference', 'field_reference');
186
    $this->addField('file', 'field_file');
187
    $this->addField('image', 'field_image');
188
189
    // File 1
190
    file_put_contents('public://example.txt', $this->randomMachineName());
191
    $this->testFile = File::create([
192
      'uri' => 'public://example.txt',
193
    ]);
194
    $this->testFile->save();
195
196
    // File 2
197
    file_put_contents('public://example.png', $this->randomMachineName());
198
    $this->testImage = File::create([
199
      'uri' => 'public://example.png',
200
    ]);
201
    $this->testImage->save();
202
    $values = [
203
      'title' => 'Test',
204
      'type' => 'test',
205
    ];
206
207
    $node = $this->createNode($values + $actualFieldValues);
208
209
    // Workaround for public file urls.
210
    $expectedFieldValues['fieldFile'][0]['entity']['url'] = file_create_url($this->testFile->getFileUri());
211
    $expectedFieldValues['fieldFile'][1]['entity']['url'] = file_create_url($this->testImage->getFileUri());
212
    $expectedFieldValues['fieldImage'][0]['entity']['url'] = file_create_url($this->testFile->getFileUri());
213
    $expectedFieldValues['fieldImage'][1]['entity']['url'] = file_create_url($this->testImage->getFileUri());
214
215
    $metadata = $this->defaultCacheMetaData();
216
217
    $metadata->addCacheTags([
218
      'entity_bundles',
219
      'entity_field_info',
220
      'entity_types',
221
      'node:1',
222
      'config:field.storage.node.body',
223
      'config:field.storage.node.field_text',
224
      'config:field.storage.node.field_boolean',
225
      'config:field.storage.node.field_datetime',
226
      'config:field.storage.node.field_decimal',
227
      'config:field.storage.node.field_email',
228
      'config:field.storage.node.field_float',
229
      'config:field.storage.node.field_file',
230
      'config:field.storage.node.field_image',
231
      'config:field.storage.node.field_integer',
232
      'config:field.storage.node.field_link',
233
      'config:field.storage.node.field_string',
234
      'config:field.storage.node.field_timestamp',
235
      'config:field.storage.node.field_reference',
236
      'entity_bundles',
237
      'entity_field_info',
238
      'entity_types',
239
      'file:1',
240
      'file:2',
241
    ]);
242
243
    $this->assertResults($this->getQueryFromFile('raw_field_values.gql'), [
244
      'path' => '/node/' . $node->id(),
245
    ], [
246
      'route' => ['entity' => $expectedFieldValues],
247
    ], $metadata);
248
  }
249
250
  /**
251
   * Data provider for testRawValues.
252
   *
253
   * @return array
254
   */
255
  public function nodeValuesProvider() {
256
    $fieldValues = [
257
      'body' => [
258
        'value' => 'test',
259
        'summary' => 'test summary',
260
      ],
261
      'field_text' => ['a', 'b', 'c'],
262
      'field_boolean' => [TRUE, FALSE],
263
      'field_link' => [
264
        [
265
          'title' => 'Internal link',
266
          'uri' => 'internal:/node/1',
267
          'options' => ['attributes' => ['_target' => 'blank']],
268
        ], [
269
          'title' => 'External link',
270
          'uri' => 'http://drupal.org',
271
          'options' => ['attributes' => ['_target' => 'blank']],
272
        ],
273
      ],
274
      'field_integer' => [10, -5],
275
      'field_float' => [3.14145, -8.8],
276
      'field_decimal' => [10.5, -17.22],
277
      'field_datetime' => ['2017-01-01', '1900-01-01'],
278
      'field_timestamp' => [0, 300],
279
      'field_email' => ['[email protected]'],
280
      'field_string' => ['test', '123'],
281
      'field_reference' =>  [
282
        ['target_id' => 1],
283
      ],
284
      'field_file' => [
285
        [
286
          'target_id' => 1,
287
          'display' => 0,
288
          'description' => 'description test 1',
289
        ],
290
        [
291
          'target_id' => 2,
292
          'display' => 1,
293
          'description' => 'description test 2',
294
        ],
295
      ],
296
      'field_image' => [
297
        [
298
          'target_id' => 1,
299
          'alt' => 'alt test 1',
300
          'title' => 'title test 1',
301
          'width' => 100,
302
          'height' => 50,
303
        ],
304
        [
305
          'target_id' => 2,
306
          'alt' => 'alt test 2',
307
          'title' => 'title test 2',
308
          'width' => 200,
309
          'height' => 100,
310
        ],
311
      ],
312
    ];
313
314
    $expected = [
315
      'nid' => 1,
316
      'vid' => 1,
317
      'langcode' => [
318
        'value' => 'en',
319
      ],
320
      'type' => [
321
        'targetId' => 'test',
322
      ],
323
      'uid' => [
324
        'targetId' => 0,
325
        'entity' => NULL,
326
      ],
327
      'title' => 'Test',
328
      'status' => TRUE,
329
      'promote' => TRUE,
330
      'sticky' => FALSE,
331
      'revisionTranslationAffected' => TRUE,
332
      'body' => [
333
        'value' => 'test',
334
        'summary' => 'test summary',
335
        'summaryProcessed' => "<p>test summary</p>\n",
336
        'processed' => "<p>test</p>\n",
337
        'format' => 'null',
338
      ],
339
      'fieldText' => [
340
        ['value' => 'a'],
341
        ['value' => 'b'],
342
        ['value' => 'c'],
343
      ],
344
      'fieldBoolean' => [
345
        TRUE,
346
        FALSE,
347
      ],
348
      'fieldLink' => [
349
        ['title' => 'Internal link', 'uri' => 'internal:/node/1', 'target' => 'blank', 'url' => ['internal' => '/node/1']],
350
        ['title' => 'External link', 'uri' => 'http://drupal.org', 'target' => 'blank', 'url' => ['external' => 'http://drupal.org']],
351
      ],
352
      'fieldInteger' => [
353
        10,
354
        -5,
355
      ],
356
      'fieldFloat' => [
357
        3.14145,
358
        -8.8,
359
      ],
360
      'fieldDecimal' => [
361
        10.5,
362
        -17.22,
363
      ],
364
      'fieldDatetime' => [
365
        ['value' => '2017-01-01'],
366
        ['value' => '1900-01-01'],
367
      ],
368
      'fieldTimestamp' => [
369
        0,
370
        300,
371
      ],
372
      'fieldEmail' => ['[email protected]'],
373
      'fieldString' => [
374
        'test',
375
        '123',
376
      ],
377
      'fieldReference' => [
378
        [
379
          'targetId' => 1,
380
          'entity' => [
381
            'title' => 'Test',
382
            'fieldReference' => [
383
              [
384
                'targetId' => 1,
385
                'entity' => [
386
                  'title' => 'Test',
387
                ],
388
              ],
389
            ],
390
          ],
391
        ],
392
      ],
393
      'fieldFile' => [
394
        [
395
          'targetId' => 1,
396
          'display' => 0,
397
          'description' => 'description test 1',
398
          'entity' => [
399
            'uri' => 'public://example.txt',
400
          ],
401
        ],
402
        [
403
          'targetId' => 2,
404
          'display' => 1,
405
          'description' => 'description test 2',
406
          'entity' => [
407
            'uri' => 'public://example.png',
408
          ],
409
        ],
410
      ],
411
      'fieldImage' => [
412
        [
413
          'targetId' => 1,
414
          'alt' => 'alt test 1',
415
          'title' => 'title test 1',
416
          'width' => 100,
417
          'height' => 50,
418
          'entity' => [
419
            'uri' => 'public://example.txt',
420
          ],
421
        ],
422
        [
423
          'targetId' => 2,
424
          'alt' => 'alt test 2',
425
          'title' => 'title test 2',
426
          'width' => 200,
427
          'height' => 100,
428
          'entity' => [
429
            'uri' => 'public://example.png',
430
          ],
431
        ],
432
      ],
433
    ];
434
435
    return [
436
      [$fieldValues, $expected],
437
    ];
438
  }
439
440
}
441