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

EntityFieldValueTest::testRawValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 71
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 56
nc 1
nop 2
dl 0
loc 71
rs 9.1369
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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