Completed
Pull Request — 8.x-1.x (#40)
by
unknown
01:56
created

ViewsTest::testSortedView()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 96
Code Lines 77

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 1
eloc 77
c 1
b 1
f 1
nc 1
nop 0
dl 0
loc 96
rs 8.5018

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_views\Kernel;
4
5
/**
6
 * Test views support in GraphQL.
7
 *
8
 * @group graphql_views
9
 */
10
class ViewsTest extends ViewsTestBase {
11
12
  /**
13
   * {@inheritdoc}
14
   */
15
  protected function defaultCacheContexts(): array {
16
    return array_merge([
17
      'languages:language_content',
18
      'languages:language_interface',
19
      'user.permissions',
20
      'user.node_grants:view',
21
    ], parent::defaultCacheContexts());
22
  }
23
24
  /**
25
   * Test that the view returns both nodes.
26
   */
27
  public function testSimpleView() {
28
    $schema = <<<GQL
29
      type Query {
30
        graphqlTestSimpleView(page: Int, pageSize: Int): ViewResult
31
      }
32
33
      type ViewResult {
34
        results: [Node],
35
        count: Int
36
      }
37
38
      type Node {
39
        entityLabel: String!
40
      }
41
GQL;
42
43
    $this->setUpSchema($schema);
44
45
    $this->mockResolver('Query', 'graphqlTestSimpleView',
46
      $this->builder->produce('views')
47
        ->map('view_id', $this->builder->fromValue('graphql_test'))
48
        ->map('display_id', $this->builder->fromValue('simple'))
49
        ->map('page', $this->builder->fromArgument('page'))
50
        ->map('page_size', $this->builder->fromArgument('pageSize'))
51
    );
52
53
    $this->mockResolver('Node', 'entityLabel',
54
      $this->builder->produce('entity_label')
55
        ->map('entity', $this->builder->fromParent())
56
    );
57
58
    $query = $this->getQueryFromFile('simple.gql');
59
    $this->assertResults($query, [], [
60
      'graphqlTestSimpleView' => [
61
        'results' => [
62
          [
63
            'entityLabel' => 'Node A',
64
          ], [
65
            'entityLabel' => 'Node B',
66
          ], [
67
            'entityLabel' => 'Node C',
68
          ],
69
        ],
70
      ],
71
    ], $this->defaultCacheMetaData()->addCacheTags([
72
      'config:views.view.graphql_test',
73
      'node:1',
74
      'node:2',
75
      'node:3',
76
      'node_list',
77
    ])->addCacheContexts(['user']));
78
  }
79
80
  /**
81
   * Test paging support.
82
   */
83
  public function testPagedView() {
84
    $schema = <<<GQL
85
      type Query {
86
        graphqlTestPagedView(page: Int, pageSize: Int): ViewResult
87
      }
88
89
      type ViewResult {
90
        results: [Node],
91
        count: Int
92
      }
93
94
      type Node {
95
        entityLabel: String!
96
      }
97
GQL;
98
99
    $this->setUpSchema($schema);
100
101
    $this->mockResolver('Query', 'graphqlTestPagedView',
102
      $this->builder->produce('views')
103
        ->map('view_id', $this->builder->fromValue('graphql_test'))
104
        ->map('display_id', $this->builder->fromValue('paged'))
105
        ->map('page', $this->builder->fromArgument('page'))
106
        ->map('page_size', $this->builder->fromArgument('pageSize'))
107
    );
108
109
    $this->mockResolver('Node', 'entityLabel',
110
      $this->builder->produce('entity_label')
111
        ->map('entity', $this->builder->fromParent())
112
    );
113
114
    $query = $this->getQueryFromFile('paged.gql');
115
    $this->assertResults($query, [], [
116
      'page_one' => [
117
        'count' => count($this->letters),
118
        'results' => [
119
          ['entityLabel' => 'Node A'],
120
          ['entityLabel' => 'Node B'],
121
        ],
122
      ],
123
      'page_two' => [
124
        'count' => count($this->letters),
125
        'results' => [
126
          ['entityLabel' => 'Node C'],
127
          ['entityLabel' => 'Node A'],
128
        ],
129
      ],
130
      'page_three' => [
131
        'count' => count($this->letters),
132
        'results' => [
133
          ['entityLabel' => 'Node A'],
134
          ['entityLabel' => 'Node B'],
135
          ['entityLabel' => 'Node C'],
136
        ],
137
      ],
138
      'page_four' => [
139
        'count' => count($this->letters),
140
        'results' => [
141
          ['entityLabel' => 'Node C'],
142
        ],
143
      ],
144
    ], $this->defaultCacheMetaData()->addCacheTags([
145
      'config:views.view.graphql_test',
146
      'node:1',
147
      'node:2',
148
      'node:3',
149
      'node:4',
150
      'node:7',
151
      'node:8',
152
      'node:9',
153
      'node_list',
154
    ])->addCacheContexts(['user']));
155
  }
156
157
  /**
158
   * Test sorting behavior.
159
   */
160
  public function testSortedView() {
161
    $schema = <<<GQL
162
      type Query {
163
        graphqlTestSortedView(page: Int, pageSize: Int, sortBy: SortBy, sortDirection: SortDirection): ViewResult
164
      }
165
166
      enum SortDirection {
167
        ASC
168
        DESC
169
      }
170
171
      enum SortBy {
172
        TITLE
173
        NID
174
      }
175
176
      type ViewResult {
177
        results: [Node],
178
        count: Int
179
      }
180
181
      type Node {
182
        entityLabel: String!
183
      }
184
GQL;
185
186
    $this->setUpSchema($schema);
187
188
    $this->mockResolver('Query', 'graphqlTestSortedView',
189
      $this->builder->produce('views')
190
        ->map('view_id', $this->builder->fromValue('graphql_test'))
191
        ->map('display_id', $this->builder->fromValue('sorted'))
192
        ->map('sort_direction', $this->builder->fromArgument('sortDirection'))
193
        ->map('sort_by', $this->builder->compose(
194
            $this->builder->fromArgument('sortBy'),
195
            $this->builder->callback(function ($direction) {
196
              $map = ['TITLE' => 'title', 'NID' => 'nid'];
197
              return $map[$direction] ?? NULL;
198
            })
199
        ))
200
    );
201
202
    $this->mockResolver('Node', 'entityLabel',
203
      $this->builder->produce('entity_label')
204
        ->map('entity', $this->builder->fromParent())
205
    );
206
207
    $query = $this->getQueryFromFile('sorted.gql');
208
    $this->assertResults($query, [], [
209
      'default' => [
210
        'results' => [
211
          ['entityLabel' => 'Node A'],
212
          ['entityLabel' => 'Node B'],
213
          ['entityLabel' => 'Node C'],
214
        ],
215
      ],
216
      'asc' => [
217
        'results' => [
218
          ['entityLabel' => 'Node A'],
219
          ['entityLabel' => 'Node A'],
220
          ['entityLabel' => 'Node A'],
221
        ],
222
      ],
223
      'desc' => [
224
        'results' => [
225
          ['entityLabel' => 'Node C'],
226
          ['entityLabel' => 'Node C'],
227
          ['entityLabel' => 'Node C'],
228
        ],
229
      ],
230
      'asc_nid' => [
231
        'results' => [
232
          ['entityLabel' => 'Node A'],
233
          ['entityLabel' => 'Node B'],
234
          ['entityLabel' => 'Node C'],
235
        ],
236
      ],
237
      'desc_nid' => [
238
        'results' => [
239
          ['entityLabel' => 'Node C'],
240
          ['entityLabel' => 'Node B'],
241
          ['entityLabel' => 'Node A'],
242
        ],
243
      ],
244
    ], $this->defaultCacheMetaData()->addCacheTags([
245
      'config:views.view.graphql_test',
246
      'node:1',
247
      'node:2',
248
      'node:3',
249
      'node:4',
250
      'node:6',
251
      'node:7',
252
      'node:8',
253
      'node:9',
254
      'node_list',
255
    ])->addCacheContexts(['user']));
256
  }
257
258
  /**
259
   * Test filter behavior.
260
   */
261
  public function testFilteredView() {
262
    $schema = <<<GQL
263
      type Query {
264
        graphqlTestFilteredView(filter: Filter): ViewResult
265
      }
266
267
      input Filter {
268
        TITLE: String
269
        NID: Int
270
      }
271
272
      type ViewResult {
273
        results: [Node],
274
        count: Int
275
      }
276
277
      type Node {
278
        entityLabel: String!
279
      }
280
GQL;
281
    $this->setUpSchema($schema);
282
283
    $this->mockResolver('Query', 'graphqlTestFilteredView',
284
      $this->builder->produce('views')
285
        ->map('view_id', $this->builder->fromValue('graphql_test'))
286
        ->map('display_id', $this->builder->fromValue('filtered'))
287
        ->map('filter', $this->builder->compose(
288
          $this->builder->fromArgument('filter'),
289
          $this->builder->callback(function ($filter) {
290
            $mapped = [];
291
            $map = ['TITLE' => 'title', 'NID' => 'nid'];
292
            foreach ($filter as $key => $value) {
293
              if (isset($map[$key])) {
294
                $mapped = [$map[$key] => $value];
295
              }
296
            }
297
            return $mapped;
298
          })
299
        ))
300
    );
301
302
    $this->mockResolver('Node', 'entityLabel',
303
      $this->builder->produce('entity_label')
304
        ->map('entity', $this->builder->fromParent())
305
    );
306
307
    $query = <<<GQL
308
query {
309
  default:graphqlTestFilteredView (filter: {TITLE: "A"}) {
310
    results {
311
      entityLabel
312
    }
313
  }
314
}
315
GQL;
316
317
    $this->assertResults($query, [], [
318
      'default' => [
319
        'results' => [
320
          ['entityLabel' => 'Node A'],
321
          ['entityLabel' => 'Node A'],
322
          ['entityLabel' => 'Node A'],
323
        ],
324
      ],
325
    ], $this->defaultCacheMetaData()->addCacheTags([
326
      'config:views.view.graphql_test',
327
      'node:1',
328
      'node:4',
329
      'node:7',
330
      'node_list',
331
    ])->addCacheContexts(['user']));
332
  }
333
334
  /**
335
   * Test filter behavior.
336
   */
337
  public function testMultiValueFilteredView() {
338
    $schema = <<<GQL
339
      type Query {
340
        graphqlTestFilteredView(filter: Filter): ViewResult
341
      }
342
343
      input Filter {
344
        field_tags: [Int]
345
      }
346
347
      type ViewResult {
348
        results: [Node],
349
        count: Int
350
      }
351
352
      type Node {
353
        entityLabel: String!
354
      }
355
GQL;
356
    $this->setUpSchema($schema);
357
358
    $this->mockResolver('Query', 'graphqlTestFilteredView',
359
      $this->builder->produce('views')
360
        ->map('view_id', $this->builder->fromValue('graphql_test'))
361
        ->map('display_id', $this->builder->fromValue('filtered'))
362
        ->map('filter', $this->builder->compose(
363
          $this->builder->fromArgument('filter'),
364
          $this->builder->callback(function ($filter) {
365
            $mapped = [];
366
            $map = ['field_tags' => 'field_tags'];
367
            foreach ($filter as $key => $value) {
368
              if (isset($map[$key])) {
369
                $mapped = [$map[$key] => $value];
370
              }
371
            }
372
            return $mapped;
373
          })
374
        ))
375
    );
376
377
    $this->mockResolver('Node', 'entityLabel',
378
      $this->builder->produce('entity_label')
379
        ->map('entity', $this->builder->fromParent())
380
    );
381
382
    $query = <<<GQL
383
query {
384
  multi:graphqlTestFilteredView (filter: {field_tags: [1, 2]}) {
385
    results {
386
      entityLabel
387
    }
388
  }
389
}
390
GQL;
391
    $this->assertResults($query, [], [
392
      'multi' => [
393
        'results' => [
394
          ['entityLabel' => 'Node A'],
395
          ['entityLabel' => 'Node B'],
396
          ['entityLabel' => 'Node A'],
397
          ['entityLabel' => 'Node B'],
398
          ['entityLabel' => 'Node A'],
399
          ['entityLabel' => 'Node B'],
400
        ],
401
      ],
402
    ], $this->defaultCacheMetaData()->addCacheTags([
403
      'config:views.view.graphql_test',
404
      'node:1',
405
      'node:2',
406
      'node:4',
407
      'node:5',
408
      'node:7',
409
      'node:8',
410
      'node_list',
411
412
    ])->addCacheContexts(['user']));
413
  }
414
415
  /**
416
   * Test complex filters.
417
   */
418
  public function testComplexFilteredView() {
419
    $schema = <<<GQL
420
      type Query {
421
        graphqlTestFilteredView(filter: Filter): ViewResult
422
      }
423
424
      input Filter {
425
        node_type: [String]
426
      }
427
428
      type ViewResult {
429
        results: [Node],
430
        count: Int
431
      }
432
433
      type Node {
434
        entityLabel: String!
435
      }
436
GQL;
437
    $this->setUpSchema($schema);
438
439
    $this->mockResolver('Query', 'graphqlTestFilteredView',
440
      $this->builder->produce('views')
441
        ->map('view_id', $this->builder->fromValue('graphql_test'))
442
        ->map('display_id', $this->builder->fromValue('filtered'))
443
        ->map('filter', $this->builder->compose(
444
          $this->builder->fromArgument('filter'),
445
          $this->builder->callback(function ($filter) {
446
            $mapped = [];
447
            $map = ['node_type' => 'node_type'];
448
            foreach ($filter as $key => $value) {
449
              if (isset($map[$key])) {
450
                $mapped = [$map[$key] => $value];
451
              }
452
            }
453
            return $mapped;
454
          })
455
        ))
456
    );
457
458
    $this->mockResolver('Node', 'entityLabel',
459
      $this->builder->produce('entity_label')
460
        ->map('entity', $this->builder->fromParent())
461
    );
462
463
    $query = <<<GQL
464
query {
465
  complex:graphqlTestFilteredView(filter: {node_type:["test"]}) {
466
    results {
467
      entityLabel
468
    }
469
  }
470
}
471
GQL;
472
    $this->assertResults($query, [], [
473
      'complex' => [
474
        'results' => [
475
          ['entityLabel' => 'Node A'],
476
          ['entityLabel' => 'Node B'],
477
          ['entityLabel' => 'Node C'],
478
          ['entityLabel' => 'Node A'],
479
          ['entityLabel' => 'Node B'],
480
          ['entityLabel' => 'Node C'],
481
          ['entityLabel' => 'Node A'],
482
          ['entityLabel' => 'Node B'],
483
          ['entityLabel' => 'Node C'],
484
        ],
485
      ],
486
    ], $this->defaultCacheMetaData()->addCacheTags([
487
      'config:views.view.graphql_test',
488
      'node:1',
489
      'node:2',
490
      'node:3',
491
      'node:4',
492
      'node:5',
493
      'node:6',
494
      'node:7',
495
      'node:8',
496
      'node:9',
497
      'node_list',
498
    ])->addCacheContexts(['user']));
499
  }
500
501
  /**
502
   * Test the result type for views with a single-value bundle filter.
503
   */
504
  public function testSingleValueBundleFilterView() {
505
    $schema = <<<GQL
506
      type Query {
507
        graphqlBundleTestGraphql1View: ViewResult
508
      }
509
510
      type ViewResult {
511
        results: [NodeTest],
512
        count: Int
513
      }
514
515
      type NodeTest {
516
        entityLabel: String!
517
      }
518
GQL;
519
    $this->setUpSchema($schema);
520
521
    $this->mockResolver('Query', 'graphqlBundleTestGraphql1View',
522
      $this->builder->produce('views')
523
        ->map('view_id', $this->builder->fromValue('graphql_bundle_test'))
524
        ->map('display_id', $this->builder->fromValue('graphql_1'))
525
    );
526
527
    $this->mockResolver('Node', 'entityLabel',
528
      $this->builder->produce('entity_label')
529
        ->map('entity', $this->builder->fromParent())
530
    );
531
532
    $query = $this->getQueryFromFile('single_bundle_filter.gql');
533
    $this->assertResults($query, [], [
534
      'withSingleBundleFilter' => [
535
        'results' => [
536
          0 => [
537
            '__typename' => 'NodeTest',
538
          ],
539
        ],
540
      ],
541
    ], $this->defaultCacheMetaData()->addCacheTags([
542
      'config:views.view.graphql_bundle_test',
543
      'node:1',
544
      'node_list',
545
    ]));
546
  }
547
548
}
549