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

EntityFieldValueTest::testText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 27
nc 1
nop 0
dl 0
loc 49
rs 9.2258
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
    $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
      'user.node_grants:view',
272
    ]);
273
274
    $this->assertResults($this->getQueryFromFile('raw_field_values.gql'), [
275
      'path' => '/node/' . $node->id(),
276
    ], [
277
      'route' => ['entity' => $expectedFieldValues],
278
    ], $metadata);
279
  }
280
281
  /**
282
   * Data provider for testRawValues.
283
   *
284
   * @return array
285
   */
286
  public function nodeValuesProvider() {
287
    $fieldValues = [
288
      'body' => [
289
        'value' => 'test',
290
        'summary' => 'test summary',
291
      ],
292
      'field_text' => ['a', 'b', 'c'],
293
      'field_boolean' => [TRUE, FALSE],
294
      'field_link' => [
295
        [
296
          'title' => 'Internal link',
297
          'uri' => 'internal:/node/1',
298
          'options' => ['attributes' => ['_target' => 'blank']],
299
        ], [
300
          'title' => 'External link',
301
          'uri' => 'http://drupal.org',
302
          'options' => ['attributes' => ['_target' => 'blank']],
303
        ],
304
      ],
305
      'field_integer' => [10, -5],
306
      'field_float' => [3.14145, -8.8],
307
      'field_decimal' => [10.5, -17.22],
308
      'field_datetime' => ['2017-01-01', '1900-01-01'],
309
      'field_timestamp' => [0, 300],
310
      'field_email' => ['[email protected]'],
311
      'field_string' => ['test', '123'],
312
      'field_reference' =>  [
313
        ['target_id' => 1],
314
      ],
315
      'field_file' => [
316
        [
317
          'target_id' => 1,
318
          'display' => 0,
319
          'description' => 'description test 1',
320
        ],
321
        [
322
          'target_id' => 2,
323
          'display' => 1,
324
          'description' => 'description test 2',
325
        ],
326
      ],
327
      'field_image' => [
328
        [
329
          'target_id' => 1,
330
          'alt' => 'alt test 1',
331
          'title' => 'title test 1',
332
          'width' => 100,
333
          'height' => 50,
334
        ],
335
        [
336
          'target_id' => 2,
337
          'alt' => 'alt test 2',
338
          'title' => 'title test 2',
339
          'width' => 200,
340
          'height' => 100,
341
        ],
342
      ],
343
    ];
344
345
    $expected = [
346
      'nid' => 1,
347
      'vid' => 1,
348
      'langcode' => [
349
        'value' => 'en',
350
      ],
351
      'type' => [
352
        'targetId' => 'test',
353
      ],
354
      'uid' => [
355
        'targetId' => 0,
356
        'entity' => NULL,
357
      ],
358
      'title' => 'Test',
359
      'status' => TRUE,
360
      'promote' => TRUE,
361
      'sticky' => FALSE,
362
      'revisionTranslationAffected' => TRUE,
363
      'body' => [
364
        'value' => 'test',
365
        'summary' => 'test summary',
366
        'summaryProcessed' => "<p>test summary</p>\n",
367
        'processed' => "<p>test</p>\n",
368
        'format' => 'null',
369
      ],
370
      'fieldText' => [
371
        ['value' => 'a'],
372
        ['value' => 'b'],
373
        ['value' => 'c'],
374
      ],
375
      'fieldBoolean' => [
376
        TRUE,
377
        FALSE,
378
      ],
379
      'fieldLink' => [
380
        ['title' => 'Internal link', 'uri' => 'internal:/node/1', 'target' => 'blank', 'url' => ['internal' => '/node/1']],
381
        ['title' => 'External link', 'uri' => 'http://drupal.org', 'target' => 'blank', 'url' => ['external' => 'http://drupal.org']],
382
      ],
383
      'fieldInteger' => [
384
        10,
385
        -5,
386
      ],
387
      'fieldFloat' => [
388
        3.14145,
389
        -8.8,
390
      ],
391
      'fieldDecimal' => [
392
        10.5,
393
        -17.22,
394
      ],
395
      'fieldDatetime' => [
396
        ['value' => '2017-01-01'],
397
        ['value' => '1900-01-01'],
398
      ],
399
      'fieldTimestamp' => [
400
        0,
401
        300,
402
      ],
403
      'fieldEmail' => ['[email protected]'],
404
      'fieldString' => [
405
        'test',
406
        '123',
407
      ],
408
      'fieldReference' => [
409
        [
410
          'targetId' => 1,
411
          'entity' => [
412
            'title' => 'Test',
413
            'fieldReference' => [
414
              [
415
                'targetId' => 1,
416
                'entity' => [
417
                  'title' => 'Test',
418
                ],
419
              ],
420
            ],
421
          ],
422
        ],
423
      ],
424
      'fieldFile' => [
425
        [
426
          'targetId' => 1,
427
          'display' => FALSE,
428
          'description' => 'description test 1',
429
          'entity' => [
430
//            '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...
431
//              'value' => 'public://example.txt',
432
//            ],
433
          ],
434
        ],
435
        [
436
          'targetId' => 2,
437
          'display' => TRUE,
438
          'description' => 'description test 2',
439
          'entity' => [
440
//            '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...
441
//              'value' => 'public://example.png',
442
//            ],
443
          ],
444
        ],
445
      ],
446
      'fieldImage' => [
447
        [
448
          'targetId' => 1,
449
          'alt' => 'alt test 1',
450
          'title' => 'title test 1',
451
          'width' => 100,
452
          'height' => 50,
453
          'entity' => [
454
//            '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...
455
//              'value' => 'public://example.txt',
456
//            ],
457
          ],
458
        ],
459
        [
460
          'targetId' => 2,
461
          'alt' => 'alt test 2',
462
          'title' => 'title test 2',
463
          'width' => 200,
464
          'height' => 100,
465
          'entity' => [
466
//            '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...
467
//              'value' => 'public://example.png',
468
//            ],
469
          ],
470
        ],
471
      ],
472
    ];
473
474
    return [
475
      [$fieldValues, $expected],
476
    ];
477
  }
478
479
}
480