Completed
Pull Request — 8.x-3.x (#399)
by Sebastian
04:43 queued 02:12
created

ContextualViewsTest::assertFieldExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\Tests\graphql_core\Kernel\Views;
4
5
use Drupal\graphql\GraphQL\Utility\TypeCollector;
6
use Drupal\Tests\graphql_core\Kernel\Views\ViewsTestBase;
7
8
/**
9
 * Test contextual views support in GraphQL.
10
 *
11
 * @group graphql_views
12
 */
13
class ContextualViewsTest extends ViewsTestBase {
14
15
  /**
16
   * The GraphQL schema.
17
   *
18
   * @var \Youshido\GraphQL\Schema\AbstractSchema
19
   */
20
  protected $schema;
21
22
  /**
23
   * The types collected from the GraphQL schema.
24
   *
25
   * @var \Youshido\GraphQL\Type\TypeInterface[]
26
   */
27
  protected $types;
28
29
  /**
30
   * {@inheritdoc}
31
   */
32
  protected function setUp() {
33
    parent::setUp();
34
    $this->createContentType(['type' => 'test2']);
35
    $this->schema = \Drupal::service('plugin.manager.graphql.schema')->createInstance('test');
36
    $this->types = TypeCollector::collectTypes($this->schema);
37
  }
38
39
  /**
40
   * Test if view contextual filters are set properly.
41
   */
42
  public function testContextualViewArgs() {
43
    $test2Node = $this->createNode(['type' => 'test2']);
44
    $this->executeQueryFile('Views/contextual.gql', [
45
      'test2NodeId' => $test2Node->id(),
46
    ]);
47
48
    $this->assertEquals(drupal_static('graphql_views_test:view:args'), [
49
      'graphql_test:contextual_title_arg' => [
50
        0 => [NULL],
51
        1 => ['X'],
52
      ],
53
      'graphql_test:contextual_node' => [
54
        0 => [NULL],
55
        1 => ['X'],
56
        2 => ['1'],
57
        3 => ['X'],
58
        4 => ['1'],
59
        5 => ['X'],
60
      ],
61
      'graphql_test:contextual_nodetest' => [
62
        0 => [NULL],
63
        1 => ['X'],
64
        2 => ['1'],
65
        3 => ['X'],
66
      ],
67
      'graphql_test:contextual_node_and_nodetest' => [
68
        0 => [NULL, NULL],
69
        1 => ['X', 'X'],
70
        2 => [$test2Node->id(), NULL],
71
        3 => ['X', 'X'],
72
        4 => ['1', '1'],
73
        5 => ['X', 'X'],
74
      ],
75
    ]);
76
  }
77
78
  /**
79
   * Test if view fields are attached to correct types.
80
   */
81
  public function testContextualViewFields() {
82
    $field = 'graphqlTestContextualTitleArgView';
83
    $this->assertFieldExists('Root', $field);
84
    $this->assertFieldNotExists('Node', $field);
85
    $this->assertFieldNotExists('NodeTest', $field);
86
87
    $field = 'graphqlTestContextualNodeView';
88
    $this->assertFieldExists('Root', $field);
89
    $this->assertFieldExists('Node', $field);
90
    $this->assertFieldExists('NodeTest', $field);
91
92
    $field = 'graphqlTestContextualNodetestView';
93
    $this->assertFieldExists('Root', $field);
94
    $this->assertFieldNotExists('Node', $field);
95
    $this->assertFieldExists('NodeTest', $field);
96
97
    $field = 'graphqlTestContextualNodeAndNodetestView';
98
    $this->assertFieldExists('Root', $field);
99
    $this->assertFieldExists('Node', $field);
100
    $this->assertFieldExists('NodeTest', $field);
101
  }
102
103
  /**
104
   * Assert that field exists on a GraphQL type.
105
   *
106
   * @param string $type
107
   *   GraphQL type name.
108
   * @param string $fieldName
109
   *   GraphQL field name.
110
   */
111
  protected function assertFieldExists($type, $fieldName) {
112
    $this->assertArrayHasKey($fieldName, $this->getFields($type), "Field {$fieldName} exists on {$type} type.");
113
  }
114
115
  /**
116
   * Assert that field does not exist on a GraphQL type.
117
   *
118
   * @param string $type
119
   *   GraphQL type name.
120
   * @param string $fieldName
121
   *   GraphQL field name.
122
   */
123
  protected function assertFieldNotExists($type, $fieldName) {
124
    $this->assertArrayNotHasKey($fieldName, $this->getFields($type), "Field {$fieldName} does not exist on {$type} type.");
125
  }
126
127
  /**
128
   * Returns list of GraphQL fields attached to a type.
129
   *
130
   * @param string $type
131
   *   GraphQL type name.
132
   *
133
   * @return \Youshido\GraphQL\Field\Field[]
134
   */
135
  protected function getFields($type) {
136
    return $type === 'Root'
137
      ? $this->schema->getQueryType()->getFields()
138
      : $this->types[$type]->getConfig()->get('fields');
139
  }
140
141
}
142