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

EntityFieldValueTest::testText()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
nc 1
nop 0
dl 0
loc 44
rs 8.8571
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
    $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
   * Test if the basic fields are available on the interface.
183
   *
184
   * @dataProvider nodeValuesProvider
185
   *
186
   * @param array $actualFieldValues
187
   * @param array $expectedFieldValues
188
   */
189
  public function testRawValues($actualFieldValues, $expectedFieldValues) {
190
    $this->installEntitySchema('file');
191
    $this->installSchema('file', ['file_usage']);
192
    $this->addField('text', "field_text");
193
    $this->addField('boolean', "field_boolean");
194
    $this->addField('link', "field_link");
195
    $this->addField('integer', "field_integer");
196
    $this->addField('float', "field_float");
197
    $this->addField('decimal', "field_decimal");
198
    $this->addField('datetime', "field_datetime");
199
    $this->addField('timestamp', "field_timestamp");
200
    $this->addField('email', "field_email");
201
    $this->addField('string', "field_string");
202
    $this->addField('entity_reference', 'field_reference');
203
    $this->addField('file', 'field_file');
204
    $this->addField('image', 'field_image');
205
206
    // File 1
207
    file_put_contents('public://example.txt', $this->randomMachineName());
208
    $this->testFile = File::create([
209
      'uri' => 'public://example.txt',
210
    ]);
211
    $this->testFile->save();
212
213
    // File 2
214
    file_put_contents('public://example.png', $this->randomMachineName());
215
    $this->testImage = File::create([
216
      'uri' => 'public://example.png',
217
    ]);
218
    $this->testImage->save();
219
    $values = [
220
      'title' => 'Test',
221
      'type' => 'test',
222
    ];
223
224
    $node = $this->createNode($values + $actualFieldValues);
225
226
    // Workaround for public file urls.
227
    $expectedFieldValues['fieldFile'][0]['entity']['url'] = file_create_url($this->testFile->getFileUri());
228
    $expectedFieldValues['fieldFile'][1]['entity']['url'] = file_create_url($this->testImage->getFileUri());
229
    $expectedFieldValues['fieldImage'][0]['entity']['url'] = file_create_url($this->testFile->getFileUri());
230
    $expectedFieldValues['fieldImage'][1]['entity']['url'] = file_create_url($this->testImage->getFileUri());
231
232
    $metadata = $this->defaultCacheMetaData();
233
    $metadata->addCacheTags([
234
      'config:field.storage.node.body',
235
      'config:field.storage.node.field_text',
236
      'config:field.storage.node.field_boolean',
237
      'config:field.storage.node.field_datetime',
238
      'config:field.storage.node.field_decimal',
239
      'config:field.storage.node.field_email',
240
      'config:field.storage.node.field_float',
241
      'config:field.storage.node.field_file',
242
      'config:field.storage.node.field_image',
243
      'config:field.storage.node.field_integer',
244
      'config:field.storage.node.field_link',
245
      'config:field.storage.node.field_string',
246
      'config:field.storage.node.field_timestamp',
247
      'config:field.storage.node.field_reference',
248
      'entity_field_info',
249
      'node:1',
250
      'file:1',
251
      'file:2',
252
    ]);
253
254
    $this->assertResults($this->getQueryFromFile('raw_field_values.gql'), [
255
      'path' => '/node/' . $node->id(),
256
    ], [
257
      'route' => ['entity' => $expectedFieldValues],
258
    ], $metadata);
259
  }
260
261
  /**
262
   * Data provider for testRawValues.
263
   *
264
   * @return array
265
   */
266
  public function nodeValuesProvider() {
267
    $fieldValues = [
268
      'body' => [
269
        'value' => 'test',
270
        'summary' => 'test summary',
271
      ],
272
      'field_text' => ['a', 'b', 'c'],
273
      'field_boolean' => [TRUE, FALSE],
274
      'field_link' => [
275
        [
276
          'title' => 'Internal link',
277
          'uri' => 'internal:/node/1',
278
          'options' => ['attributes' => ['_target' => 'blank']],
279
        ], [
280
          'title' => 'External link',
281
          'uri' => 'http://drupal.org',
282
          'options' => ['attributes' => ['_target' => 'blank']],
283
        ],
284
      ],
285
      'field_integer' => [10, -5],
286
      'field_float' => [3.14145, -8.8],
287
      'field_decimal' => [10.5, -17.22],
288
      'field_datetime' => ['2017-01-01', '1900-01-01'],
289
      'field_timestamp' => [0, 300],
290
      'field_email' => ['[email protected]'],
291
      'field_string' => ['test', '123'],
292
      'field_reference' =>  [
293
        ['target_id' => 1],
294
      ],
295
      'field_file' => [
296
        [
297
          'target_id' => 1,
298
          'display' => 0,
299
          'description' => 'description test 1',
300
        ],
301
        [
302
          'target_id' => 2,
303
          'display' => 1,
304
          'description' => 'description test 2',
305
        ],
306
      ],
307
      'field_image' => [
308
        [
309
          'target_id' => 1,
310
          'alt' => 'alt test 1',
311
          'title' => 'title test 1',
312
          'width' => 100,
313
          'height' => 50,
314
        ],
315
        [
316
          'target_id' => 2,
317
          'alt' => 'alt test 2',
318
          'title' => 'title test 2',
319
          'width' => 200,
320
          'height' => 100,
321
        ],
322
      ],
323
    ];
324
325
    $expected = [
326
      'nid' => 1,
327
      'vid' => 1,
328
      'langcode' => [
329
        'value' => 'en',
330
      ],
331
      'type' => [
332
        'targetId' => 'test',
333
      ],
334
      'uid' => [
335
        'targetId' => 0,
336
        'entity' => NULL,
337
      ],
338
      'title' => 'Test',
339
      'status' => TRUE,
340
      'promote' => TRUE,
341
      'sticky' => FALSE,
342
      'revisionTranslationAffected' => TRUE,
343
      'body' => [
344
        'value' => 'test',
345
        'summary' => 'test summary',
346
        'summaryProcessed' => "<p>test summary</p>\n",
347
        'processed' => "<p>test</p>\n",
348
        'format' => 'null',
349
      ],
350
      'fieldText' => [
351
        ['value' => 'a'],
352
        ['value' => 'b'],
353
        ['value' => 'c'],
354
      ],
355
      'fieldBoolean' => [
356
        TRUE,
357
        FALSE,
358
      ],
359
      'fieldLink' => [
360
        ['title' => 'Internal link', 'uri' => 'internal:/node/1', 'target' => 'blank', 'url' => ['internal' => '/node/1']],
361
        ['title' => 'External link', 'uri' => 'http://drupal.org', 'target' => 'blank', 'url' => ['external' => 'http://drupal.org']],
362
      ],
363
      'fieldInteger' => [
364
        10,
365
        -5,
366
      ],
367
      'fieldFloat' => [
368
        3.14145,
369
        -8.8,
370
      ],
371
      'fieldDecimal' => [
372
        10.5,
373
        -17.22,
374
      ],
375
      'fieldDatetime' => [
376
        ['value' => '2017-01-01'],
377
        ['value' => '1900-01-01'],
378
      ],
379
      'fieldTimestamp' => [
380
        0,
381
        300,
382
      ],
383
      'fieldEmail' => ['[email protected]'],
384
      'fieldString' => [
385
        'test',
386
        '123',
387
      ],
388
      'fieldReference' => [
389
        [
390
          'targetId' => 1,
391
          'entity' => [
392
            'title' => 'Test',
393
            'fieldReference' => [
394
              [
395
                'targetId' => 1,
396
                'entity' => [
397
                  'title' => 'Test',
398
                ],
399
              ],
400
            ],
401
          ],
402
        ],
403
      ],
404
      'fieldFile' => [
405
        [
406
          'targetId' => 1,
407
          'display' => FALSE,
408
          'description' => 'description test 1',
409
          'entity' => [
410
//            '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...
411
//              'value' => 'public://example.txt',
412
//            ],
413
          ],
414
        ],
415
        [
416
          'targetId' => 2,
417
          'display' => TRUE,
418
          'description' => 'description test 2',
419
          'entity' => [
420
//            '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...
421
//              'value' => 'public://example.png',
422
//            ],
423
          ],
424
        ],
425
      ],
426
      'fieldImage' => [
427
        [
428
          'targetId' => 1,
429
          'alt' => 'alt test 1',
430
          'title' => 'title test 1',
431
          'width' => 100,
432
          'height' => 50,
433
          'entity' => [
434
//            '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...
435
//              'value' => 'public://example.txt',
436
//            ],
437
          ],
438
        ],
439
        [
440
          'targetId' => 2,
441
          'alt' => 'alt test 2',
442
          'title' => 'title test 2',
443
          'width' => 200,
444
          'height' => 100,
445
          'entity' => [
446
//            '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...
447
//              'value' => 'public://example.png',
448
//            ],
449
          ],
450
        ],
451
      ],
452
    ];
453
454
    return [
455
      [$fieldValues, $expected],
456
    ];
457
  }
458
459
}
460