Completed
Pull Request — 8.x-3.x (#501)
by Philipp
03:58
created

EntityFieldValueTest::testRawValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 38
nc 1
nop 2
dl 0
loc 47
rs 9.0303
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\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 File
17
   */
18
  protected $testFile;
19
20
  /**
21
   * @var 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() {
39
    parent::setUp();
40
41
    $this->installEntitySchema('file');
42
    $this->installSchema('file', ['file_usage']);
43
    $this->addField('text', "field_text");
44
    $this->addField('boolean', "field_boolean");
45
    $this->addField('link', "field_link");
46
    $this->addField('integer', "field_integer");
47
    $this->addField('float', "field_float");
48
    $this->addField('decimal', "field_decimal");
49
    $this->addField('datetime', "field_datetime");
50
    $this->addField('timestamp', "field_timestamp");
51
    $this->addField('email', "field_email");
52
    $this->addField('string', "field_string");
53
    $this->addField('entity_reference', 'field_reference');
54
    $this->addField('file', 'field_file');
55
    $this->addField('image', 'field_image');
56
57
    // File 1
58
    file_put_contents('public://example.txt', $this->randomMachineName());
59
    $this->testFile = File::create([
60
      'uri' => 'public://example.txt',
61
    ]);
62
    $this->testFile->save();
63
64
    // File 2
65
    file_put_contents('public://example.png', $this->randomMachineName());
66
    $this->testImage = File::create([
67
      'uri' => 'public://example.png',
68
    ]);
69
    $this->testImage->save();
70
  }
71
72
  /**
73
   * Test if the basic fields are available on the interface.
74
   *
75
   * @dataProvider nodeValuesProvider
76
   */
77
  public function testRawValues($actualFieldValues, $expectedFieldValues) {
78
    $values = [
79
      'title' => 'Test',
80
      'type' => 'test',
81
    ];
82
    $node = $this->createNode($values + $actualFieldValues);
83
84
    // Workaround for public file urls.
85
    $expectedFieldValues['fieldFile'][0]['entity']['url'] = file_create_url($this->testFile->getFileUri());
86
    $expectedFieldValues['fieldFile'][1]['entity']['url'] = file_create_url($this->testImage->getFileUri());
87
    $expectedFieldValues['fieldImage'][0]['entity']['url'] = file_create_url($this->testFile->getFileUri());
88
    $expectedFieldValues['fieldImage'][1]['entity']['url'] = file_create_url($this->testImage->getFileUri());
89
90
    $metadata = $this->defaultCacheMetaData();
91
92
    $metadata->addCacheTags([
93
      'entity_bundles',
94
      'entity_field_info',
95
      'entity_types',
96
      'node:1',
97
      'config:field.storage.node.body',
98
      'config:field.storage.node.field_text',
99
      'config:field.storage.node.field_boolean',
100
      'config:field.storage.node.field_datetime',
101
      'config:field.storage.node.field_decimal',
102
      'config:field.storage.node.field_email',
103
      'config:field.storage.node.field_float',
104
      'config:field.storage.node.field_file',
105
      'config:field.storage.node.field_image',
106
      'config:field.storage.node.field_integer',
107
      'config:field.storage.node.field_link',
108
      'config:field.storage.node.field_string',
109
      'config:field.storage.node.field_timestamp',
110
      'config:field.storage.node.field_reference',
111
      'entity_bundles',
112
      'entity_field_info',
113
      'entity_types',
114
      'file:1',
115
      'file:2',
116
    ]);
117
118
    $this->assertResults($this->getQueryFromFile('raw_field_values.gql'), [
119
      'path' => '/node/' . $node->id(),
120
    ], [
121
      'route' => ['entity' => $expectedFieldValues],
122
    ], $metadata);
123
  }
124
125
  /**
126
   * Data provider for testRawValues.
127
   *
128
   * @return array
129
   */
130
  public function nodeValuesProvider() {
131
    $fieldValues = [
132
      'body' => [
133
        'value' => 'test',
134
        'summary' => 'test summary',
135
      ],
136
      'field_text' => ['a', 'b', 'c'],
137
      'field_boolean' => [TRUE, FALSE],
138
      'field_link' => [
139
        [
140
          'title' => 'Internal link',
141
          'uri' => 'internal:/node/1',
142
          'options' => ['attributes' => ['_target' => 'blank']],
143
        ], [
144
          'title' => 'External link',
145
          'uri' => 'http://drupal.org',
146
          'options' => ['attributes' => ['_target' => 'blank']],
147
        ],
148
      ],
149
      'field_integer' => [10, -5],
150
      'field_float' => [3.14145, -8.8],
151
      'field_decimal' => [10.5, -17.22],
152
      'field_datetime' => ['2017-01-01', '1900-01-01'],
153
      'field_timestamp' => [0, 300],
154
      'field_email' => ['[email protected]'],
155
      'field_string' => ['test', '123'],
156
      'field_reference' => [
157
        ['target_id' => 1],
158
      ],
159
      'field_file' => [
160
        [
161
          'target_id' => 1,
162
          'display' => 0,
163
          'description' => 'description test 1',
164
        ],
165
        [
166
          'target_id' => 2,
167
          'display' => 1,
168
          'description' => 'description test 2',
169
        ],
170
      ],
171
      'field_image' => [
172
        [
173
          'target_id' => 1,
174
          'alt' => 'alt test 1',
175
          'title' => 'title test 1',
176
          'width' => 100,
177
          'height' => 50,
178
        ],
179
        [
180
          'target_id' => 2,
181
          'alt' => 'alt test 2',
182
          'title' => 'title test 2',
183
          'width' => 200,
184
          'height' => 100,
185
        ],
186
      ],
187
    ];
188
189
    $expected = [
190
      'nid' => 1,
191
      'vid' => 1,
192
      'langcode' => [
193
        'value' => 'en',
194
      ],
195
      'type' => [
196
        'targetId' => 'test',
197
      ],
198
      'uid' => [
199
        'targetId' => 0,
200
        'entity' => NULL,
201
      ],
202
      'title' => 'Test',
203
      'status' => 1,
204
      'promote' => 1,
205
      'sticky' => 0,
206
      'revisionTranslationAffected' => 1,
207
      'body' => [
208
        'value' => 'test',
209
        'summary' => 'test summary',
210
        'summaryProcessed' => "<p>test summary</p>\n",
211
        'processed' => "<p>test</p>\n",
212
        'format' => NULL,
213
      ],
214
      'fieldText' => [
215
        ['value' => 'a'],
216
        ['value' => 'b'],
217
        ['value' => 'c'],
218
      ],
219
      'fieldBoolean' => [
220
        TRUE,
221
        FALSE,
222
      ],
223
      'fieldLink' => [
224
        ['title' => 'Internal link', 'uri' => 'internal:/node/1', 'target' => 'blank', 'url' => ['internal' => '/node/1']],
225
        ['title' => 'External link', 'uri' => 'http://drupal.org', 'target' => 'blank', 'url' => ['external' => 'http://drupal.org']],
226
      ],
227
      'fieldInteger' => [
228
        10,
229
        -5,
230
      ],
231
      'fieldFloat' => [
232
        3.14145,
233
        -8.8,
234
      ],
235
      'fieldDecimal' => [
236
        10.5,
237
        -17.22,
238
      ],
239
      'fieldDatetime' => [
240
        ['value' => '2017-01-01'],
241
        ['value' => '1900-01-01'],
242
      ],
243
      'fieldTimestamp' => [
244
        0,
245
        300,
246
      ],
247
      'fieldEmail' => ['[email protected]'],
248
      'fieldString' => [
249
        'test',
250
        '123',
251
      ],
252
      'fieldReference' => [
253
        [
254
          'targetId' => 1,
255
          'entity' => [
256
            'title' => 'Test',
257
            'fieldReference' => [
258
              [
259
                'targetId' => 1,
260
                'entity' => [
261
                  'title' => 'Test',
262
                ],
263
              ],
264
            ],
265
          ],
266
        ],
267
      ],
268
      'fieldFile' => [
269
        [
270
          'targetId' => 1,
271
          'display' => 0,
272
          'description' => 'description test 1',
273
          'entity' => [
274
            'uri' => 'public://example.txt',
275
          ],
276
        ],
277
        [
278
          'targetId' => 2,
279
          'display' => 1,
280
          'description' => 'description test 2',
281
          'entity' => [
282
            'uri' => 'public://example.png',
283
          ],
284
        ],
285
      ],
286
      'fieldImage' => [
287
        [
288
          'targetId' => 1,
289
          'alt' => 'alt test 1',
290
          'title' => 'title test 1',
291
          'width' => 100,
292
          'height' => 50,
293
          'entity' => [
294
            'uri' => 'public://example.txt',
295
          ],
296
        ],
297
        [
298
          'targetId' => 2,
299
          'alt' => 'alt test 2',
300
          'title' => 'title test 2',
301
          'width' => 200,
302
          'height' => 100,
303
          'entity' => [
304
            'uri' => 'public://example.png',
305
          ],
306
        ],
307
      ],
308
    ];
309
310
    return [
311
      [$fieldValues, $expected],
312
    ];
313
  }
314
315
}
316