EntityFieldValueTest::testFieldAssignment()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 17
rs 9.9
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Drupal\Tests\graphql_core\Kernel\Entity;
4
5
use Drupal\file\Entity\File;
0 ignored issues
show
Bug introduced by
The type Drupal\file\Entity\File was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Drupal\Tests\graphql_core\Kernel\GraphQLContentTestBase;
7
8
/**
9
 * Test basic entity fields.
10
 *
11
 * @group graphql_core
12
 */
13
class EntityFieldValueTest extends GraphQLContentTestBase {
14
15
  /**
16
   * @var \Drupal\file\Entity\File
17
   */
18
  protected $testFile;
19
20
  /**
21
   * @var \Drupal\file\Entity\File
22
   */
23
  protected $testImage;
24
25
  /**
26
   * {@inheritdoc}
27
   */
28
  public static $modules = [
29
    'link',
30
    'datetime',
31
    'image',
32
    'file',
33
  ];
34
35
  /**
36
   * {@inheritdoc}
37
   */
38
  protected function setUp(): void {
39
    parent::setUp();
40
  }
41
42
  /**
43
   * Test boolean fields.
44
   */
45
  public function testBoolean() {
46
    $this->addField('boolean', "field_boolean", FALSE);
47
48
    $this->mockNode([
49
      'field_boolean' => TRUE,
50
    ]);
51
52
    $this->assertGraphQLFields([
53
      ['NodeTest', 'fieldBoolean', 'Boolean'],
54
    ]);
55
56
    $query = <<<GQL
57
query {
58
  node {
59
    fieldBoolean
60
  }
61
}
62
GQL;
63
64
    $metadata = $this->defaultCacheMetaData();
65
66
    $this->assertResults($query, [], [
67
      'node' => [
68
        'fieldBoolean' => TRUE,
69
      ],
70
    ], $metadata);
71
  }
72
73
  /**
74
   * Test a simple text field.
75
   */
76
  public function testText() {
77
    $this->addField('text', "field_text", FALSE);
78
    $this->mockNode([
79
      'field_text' => [
80
        'value' => 'Foo',
81
      ],
82
    ]);
83
84
    $this->assertGraphQLFields([
85
      ['NodeTest', 'fieldText', 'FieldNodeTestFieldText'],
86
      ['FieldNodeTestFieldText', 'value', 'String'],
87
      ['FieldNodeTestFieldText', 'processed', 'String'],
88
      ['FieldNodeTestFieldText', 'format', 'String'],
89
    ]);
90
91
    $query = <<<GQL
92
query {
93
  node {
94
    fieldText {
95
      value
96
      processed
97
      format
98
    }
99
  }
100
}
101
GQL;
102
103
    $metadata = $this->defaultCacheMetaData();
104
105
    $this->assertResults($query, [], [
106
      'node' => [
107
        'fieldText' => [
108
          'value' => 'Foo',
109
          'processed' => "<p>Foo</p>\n",
110
          'format' => NULL,
111
        ],
112
      ],
113
    ], $metadata);
114
115
  }
116
117
  /**
118
   * Test filtered text fields.
119
   */
120
  public function testFilteredText() {
121
    $this->mockNode([
122
      'body' => [
123
        'value' => 'http://www.drupal.org',
124
        'format' => 'plain_text',
125
      ],
126
    ]);
127
128
    $this->assertGraphQLFields([
129
      ['NodeTest', 'body', 'FieldNodeTestBody'],
130
      ['FieldNodeTestBody', 'format', 'String'],
131
      ['FieldNodeTestBody', 'value', 'String'],
132
      ['FieldNodeTestBody', 'processed', 'String'],
133
      ['FieldNodeTestBody', 'summary', 'String'],
134
      ['FieldNodeTestBody', 'summaryProcessed', 'String'],
135
    ]);
136
137
    $query = <<<GQL
138
query {
139
  node {
140
    body {
141
      value
142
      processed
143
      summary
144
      summaryProcessed
145
    }
146
  }
147
}
148
GQL;
149
150
    $metadata = $this->defaultCacheMetaData();
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
    ], $metadata);
162
  }
163
164
  /**
165
   * Verify that fields are assigned correctly among bundles.
166
   */
167
  public function testFieldAssignment() {
168
    $this->createContentType(['type' => 'a']);
169
    $this->createContentType(['type' => 'b']);
170
    $this->addField('boolean', 'field_a', FALSE, 'A', 'a');
171
    $this->addField('boolean', 'field_b', FALSE, 'B', 'b');
172
173
    // Verify that the fields for a given bundle are there.
174
    $this->assertGraphQLFields([
175
      ['NodeA', 'fieldA', 'Boolean'],
176
      ['NodeB', 'fieldB', 'Boolean'],
177
    ]);
178
179
    // Verify that the fields of another bundle are *not* there.
180
    $this->assertGraphQLFields([
181
      ['NodeA', 'fieldB', 'Boolean'],
182
      ['NodeB', 'fieldA', 'Boolean'],
183
    ], TRUE);
184
  }
185
186
  /**
187
   * Test if the basic fields are available on the interface.
188
   *
189
   * @dataProvider nodeValuesProvider
190
   *
191
   * @param array $actualFieldValues
192
   * @param array $expectedFieldValues
193
   */
194
  public function testRawValues($actualFieldValues, $expectedFieldValues) {
195
    $this->installEntitySchema('file');
196
    $this->installSchema('file', ['file_usage']);
197
    $this->addField('text', "field_text");
198
    $this->addField('boolean', "field_boolean");
199
    $this->addField('link', "field_link");
200
    $this->addField('integer', "field_integer");
201
    $this->addField('float', "field_float");
202
    $this->addField('decimal', "field_decimal");
203
    $this->addField('datetime', "field_datetime");
204
    $this->addField('timestamp', "field_timestamp");
205
    $this->addField('email', "field_email");
206
    $this->addField('string', "field_string");
207
    $this->addField('entity_reference', 'field_reference');
208
    $this->addField('file', 'field_file');
209
    $this->addField('image', 'field_image');
210
211
    // File 1.
212
    file_put_contents('public://example.txt', $this->randomMachineName());
213
    $this->testFile = File::create([
214
      'uri' => 'public://example.txt',
215
    ]);
216
    $this->testFile->save();
217
218
    // File 2.
219
    file_put_contents('public://example.png', $this->randomMachineName());
220
    $this->testImage = File::create([
221
      'uri' => 'public://example.png',
222
    ]);
223
    $this->testImage->save();
224
    $values = [
225
      'title' => 'Test',
226
      'type' => 'test',
227
    ];
228
229
    $node = $this->createNode($values + $actualFieldValues);
230
231
    // Workaround for public file urls.
232
    $expectedFieldValues['fieldFile'][0]['entity']['url'] = \Drupal::service('file_url_generator')
233
      ->generateAbsoluteString($this->testFile->getFileUri());
234
    $expectedFieldValues['fieldFile'][1]['entity']['url'] = \Drupal::service('file_url_generator')
235
      ->generateAbsoluteString($this->testImage->getFileUri());
236
    $expectedFieldValues['fieldImage'][0]['entity']['url'] = \Drupal::service('file_url_generator')
237
      ->generateAbsoluteString($this->testFile->getFileUri());
238
    $expectedFieldValues['fieldImage'][1]['entity']['url'] = \Drupal::service('file_url_generator')
239
      ->generateAbsoluteString($this->testImage->getFileUri());
240
241
    $metadata = $this->defaultCacheMetaData();
242
    $metadata->addCacheTags([
243
      'node:1',
244
      'user:0',
245
      'file:1',
246
      'file:2',
247
    ]);
248
249
    $this->assertResults($this->getQueryFromFile('raw_field_values.gql'), [
250
      'path' => '/node/' . $node->id(),
251
    ], [
252
      'route' => ['entity' => $expectedFieldValues],
253
    ], $metadata);
254
  }
255
256
  /**
257
   * Data provider for testRawValues.
258
   *
259
   * @return array
260
   */
261
  public function nodeValuesProvider() {
262
    $fieldValues = [
263
      'body' => [
264
        'value' => 'test',
265
        'summary' => 'test summary',
266
      ],
267
      'field_text' => ['a', 'b', 'c'],
268
      'field_boolean' => [TRUE, FALSE],
269
      'field_link' => [
270
        [
271
          'title' => 'Internal link',
272
          'uri' => 'internal:/node/1',
273
          'options' => ['attributes' => ['_target' => 'blank']],
274
        ],
275
        [
276
          'title' => 'External link',
277
          'uri' => 'http://drupal.org',
278
          'options' => ['attributes' => ['_target' => 'blank']],
279
        ],
280
      ],
281
      'field_integer' => [10, -5],
282
      'field_float' => [3.14145, -8.8],
283
      'field_decimal' => [10.5, -17.22],
284
      'field_datetime' => ['2017-01-01', '1900-01-01'],
285
      'field_timestamp' => [0, 300],
286
      'field_email' => ['[email protected]'],
287
      'field_string' => ['test', '123'],
288
      'field_reference' => [
289
        ['target_id' => 1],
290
      ],
291
      'field_file' => [
292
        [
293
          'target_id' => 1,
294
          'display' => 0,
295
          'description' => 'description test 1',
296
        ],
297
        [
298
          'target_id' => 2,
299
          'display' => 1,
300
          'description' => 'description test 2',
301
        ],
302
      ],
303
      'field_image' => [
304
        [
305
          'target_id' => 1,
306
          'alt' => 'alt test 1',
307
          'title' => 'title test 1',
308
          'width' => 100,
309
          'height' => 50,
310
        ],
311
        [
312
          'target_id' => 2,
313
          'alt' => 'alt test 2',
314
          'title' => 'title test 2',
315
          'width' => 200,
316
          'height' => 100,
317
        ],
318
      ],
319
    ];
320
321
    $expected = [
322
      'nid' => 1,
323
      'vid' => 1,
324
      'langcode' => [
325
        'value' => 'en',
326
      ],
327
      'type' => [
328
        'targetId' => 'test',
329
      ],
330
      'uid' => [
331
        'targetId' => 0,
332
        'entity' => [
333
          'name' => '',
334
        ],
335
      ],
336
      'title' => 'Test',
337
      'status' => TRUE,
338
      'promote' => TRUE,
339
      'sticky' => FALSE,
340
      'revisionTranslationAffected' => TRUE,
341
      'body' => [
342
        'value' => 'test',
343
        'summary' => 'test summary',
344
        'summaryProcessed' => "<p>test summary</p>\n",
345
        'processed' => "<p>test</p>\n",
346
        'format' => NULL,
347
      ],
348
      'fieldText' => [
349
        ['value' => 'a'],
350
        ['value' => 'b'],
351
        ['value' => 'c'],
352
      ],
353
      'fieldBoolean' => [
354
        TRUE,
355
        FALSE,
356
      ],
357
      'fieldLink' => [
358
        [
359
          'title' => 'Internal link',
360
          'uri' => 'internal:/node/1',
361
          'target' => 'blank',
362
          'url' => ['internal' => '/node/1'],
363
        ],
364
        [
365
          'title' => 'External link',
366
          'uri' => 'http://drupal.org',
367
          'target' => 'blank',
368
          'url' => ['external' => 'http://drupal.org'],
369
        ],
370
      ],
371
      'fieldInteger' => [
372
        10,
373
        -5,
374
      ],
375
      'fieldFloat' => [
376
        3.14145,
377
        -8.8,
378
      ],
379
      'fieldDecimal' => [
380
        10.5,
381
        -17.22,
382
      ],
383
      'fieldDatetime' => [
384
        ['value' => '2017-01-01'],
385
        ['value' => '1900-01-01'],
386
      ],
387
      'fieldTimestamp' => [
388
        0,
389
        300,
390
      ],
391
      'fieldEmail' => ['[email protected]'],
392
      'fieldString' => [
393
        'test',
394
        '123',
395
      ],
396
      'fieldReference' => [
397
        [
398
          'targetId' => 1,
399
          'entity' => [
400
            'title' => 'Test',
401
            'fieldReference' => [
402
              [
403
                'targetId' => 1,
404
                'entity' => [
405
                  'title' => 'Test',
406
                ],
407
              ],
408
            ],
409
          ],
410
        ],
411
      ],
412
      'fieldFile' => [
413
        [
414
          'targetId' => 1,
415
          'display' => FALSE,
416
          'description' => 'description test 1',
417
          'entity' => [
418
            // 'uri' => [
419
            //              'value' => 'public://example.txt',
420
            //            ],
421
          ],
422
        ],
423
        [
424
          'targetId' => 2,
425
          'display' => TRUE,
426
          'description' => 'description test 2',
427
          'entity' => [
428
            // 'uri' => [
429
            //              'value' => 'public://example.png',
430
            //            ],
431
          ],
432
        ],
433
      ],
434
      'fieldImage' => [
435
        [
436
          'targetId' => 1,
437
          'alt' => 'alt test 1',
438
          'title' => 'title test 1',
439
          'width' => 100,
440
          'height' => 50,
441
          'entity' => [
442
            // 'uri' => [
443
            //              'value' => 'public://example.txt',
444
            //            ],
445
          ],
446
        ],
447
        [
448
          'targetId' => 2,
449
          'alt' => 'alt test 2',
450
          'title' => 'title test 2',
451
          'width' => 200,
452
          'height' => 100,
453
          'entity' => [
454
            // 'uri' => [
455
            //              'value' => 'public://example.png',
456
            //            ],
457
          ],
458
        ],
459
      ],
460
    ];
461
462
    return [
463
      [$fieldValues, $expected],
464
    ];
465
  }
466
467
}
468