Completed
Push — 8.x-3.x ( 962268...8b362e )
by Philipp
02:31
created

EntityFieldValueTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 465
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 465
rs 10
wmc 6
lcom 1
cbo 1

6 Methods

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

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.

Loading history...
432
//              'value' => 'public://example.txt',
433
//            ],
434
          ],
435
        ],
436
        [
437
          'targetId' => 2,
438
          'display' => TRUE,
439
          'description' => 'description test 2',
440
          'entity' => [
441
//            'uri' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
442
//              'value' => 'public://example.png',
443
//            ],
444
          ],
445
        ],
446
      ],
447
      'fieldImage' => [
448
        [
449
          'targetId' => 1,
450
          'alt' => 'alt test 1',
451
          'title' => 'title test 1',
452
          'width' => 100,
453
          'height' => 50,
454
          'entity' => [
455
//            'uri' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
456
//              'value' => 'public://example.txt',
457
//            ],
458
          ],
459
        ],
460
        [
461
          'targetId' => 2,
462
          'alt' => 'alt test 2',
463
          'title' => 'title test 2',
464
          'width' => 200,
465
          'height' => 100,
466
          'entity' => [
467
//            'uri' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
468
//              'value' => 'public://example.png',
469
//            ],
470
          ],
471
        ],
472
      ],
473
    ];
474
475
    return [
476
      [$fieldValues, $expected],
477
    ];
478
  }
479
480
}
481