Completed
Pull Request — 8.x-3.x (#525)
by Sebastian
05:14
created

testCreateEntityMutationViolation()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 24

Duplication

Lines 36
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 24
nc 1
nop 0
dl 36
loc 36
rs 8.8571
c 2
b 0
f 0
1
<?php
2
3
namespace Drupal\Tests\graphql_core\Kernel\EntityMutation;
4
5
use Drupal\graphql_core\Plugin\GraphQL\Mutations\Entity\CreateEntityBase;
6
use Drupal\graphql_core\Plugin\GraphQL\Mutations\Entity\DeleteEntityBase;
7
use Drupal\graphql_core\Plugin\GraphQL\Mutations\Entity\UpdateEntityBase;
8
use Drupal\node\Entity\Node;
9
use Drupal\Tests\graphql_core\Kernel\GraphQLContentTestBase;
10
11
/**
12
 * Test abstract entity mutation classes.
13
 *
14
 * @group graphql_core
15
 */
16
class EntityMutationTest extends GraphQLContentTestBase {
17
18
  /**
19
   * {@inheritdoc}
20
   *
21
   * Allow to modify all nodes.
22
   */
23
  protected function userPermissions() {
24
    $perms = parent::userPermissions();
25
    $perms[] = 'bypass node access';
26
    $perms[] = 'administer nodes';
27
    return $perms;
28
  }
29
30
  /**
31
   * {@inheritdoc}
32
   */
33
  protected function setUp() {
34
    parent::setUp();
35
    $this->mockInputType('node_input', [
36
      'name' => 'NodeInput',
37
      'fields' => [
38
        'title' => 'String',
39
        'body' => 'String',
40
      ],
41
    ]);
42
  }
43
44
  protected function mockMutationFactory($definition, $result = NULL, $builder = NULL) {
45
    $mutation = $this->getMockBuilder([
46
      'createNode' => CreateEntityBase::class,
47
      'updateNode' => UpdateEntityBase::class,
48
      'deleteNode' => DeleteEntityBase::class,
49
    ][$definition['id']])
50
      ->setConstructorArgs([
51
        [],
52
        $definition['id'],
53
        $definition,
54
        $this->container->get('entity_type.manager'),
55
      ])
56
      ->setMethods([
57
        'extractEntityInput',
58
      ])->getMock();
59
60
    if (isset($result)) {
61
      $mutation
62
        ->expects(static::any())
63
        ->method('extractEntityInput')
64
        ->with(static::anything(), static::anything(), static::anything(), static::anything())
65
        ->will($this->toBoundPromise($result, $mutation));
66
    }
67
68
    if (is_callable($builder)) {
69
      $builder($mutation);
70
    }
71
72
    return $mutation;
73
  }
74
75
  /**
76
   * Test entity creation.
77
   */
78 View Code Duplication
  public function testCreateEntityMutation() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    $this->mockMutation('createNode', [
80
      'name' => 'createNode',
81
      'entity_type' => 'node',
82
      'entity_bundle' => 'test',
83
      'arguments' => [
84
        'input' => 'NodeInput',
85
      ],
86
      'type' => 'EntityCrudOutput',
87
    ], function ($source, $args, $context, $info) {
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $info is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
        return [
89
          'title' => $args['input']['title'],
90
          'status' => 1,
91
          'body' => [
92
            'value' => $args['input']['body'],
93
          ],
94
        ];
95
    });
96
97
    $this->assertResults('mutation ($node: NodeInput!) { createNode(input: $node) { entity { entityId } } }', [
98
      'node' => [
99
        'title' => 'Test',
100
        'body' => 'This is a test.',
101
      ],
102
    ], [
103
      'createNode' => [
104
        'entity' => [
105
          'entityId' => 1,
106
        ],
107
      ],
108
    ], $this->defaultMutationCacheMetaData());
109
110
    $this->assertEquals('Test', Node::load(1)->getTitle());
111
  }
112
113
  /**
114
   * Test entity creation violations.
115
   */
116 View Code Duplication
  public function testCreateEntityMutationViolation() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    $this->mockMutation('createNode', [
118
      'name' => 'createNode',
119
      'entity_type' => 'node',
120
      'entity_bundle' => 'test',
121
      'arguments' => [
122
        'input' => 'NodeInput',
123
      ],
124
      'type' => 'EntityCrudOutput',
125
    ], function ($source, $args, $context, $info) {
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $info is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
126
      return [
127
        'status' => 1,
128
        'body' => [
129
          'value' => $args['input']['body'],
130
        ],
131
      ];
132
    });
133
134
    $this->assertResults('mutation ($node: NodeInput!) { createNode(input: $node) { violations { message path } } }', [
135
      'node' => [
136
        'title' => 'Test',
137
        'body' => 'This is a test.',
138
      ],
139
    ], [
140
      'createNode' => [
141
        'violations' => [
142
          0 => [
143
            'message' => 'This value should not be null.',
144
            'path' => 'title',
145
          ],
146
        ],
147
      ],
148
    ], $this->defaultMutationCacheMetaData());
149
150
    $this->assertEmpty(Node::load(1));
151
  }
152
153
  /**
154
   * Test entity updates.
155
   */
156
  public function testUpdateEntityMutation() {
157
    $this->mockMutation('updateNode', [
158
      'name' => 'updateNode',
159
      'entity_type' => 'node',
160
      'entity_bundle' => 'test',
161
      'arguments' => [
162
        'id' => 'String',
163
        'input' => 'NodeInput',
164
      ],
165
      'type' => 'EntityCrudOutput',
166
    ], function ($source, $args, $context, $info) {
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $info is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
167
      return [
168
        'title' => $args['input']['title'],
169
      ];
170
    });
171
172
    $this->createNode([
173
      'title' => 'Old title',
174
      'status' => 1,
175
      'type' => 'test',
176
      'body' => [
177
        'value' => 'Old body',
178
      ],
179
    ]);
180
181
    $this->assertResults('mutation ($node: NodeInput!, $nid: String!) { updateNode(id: $nid, input: $node) { entity { entityLabel } } }', [
182
      'nid' => '1',
183
      'node' => [
184
        'title' => 'Test',
185
      ],
186
    ], [
187
      'updateNode' => [
188
        'entity' => [
189
          'entityLabel' => 'Test',
190
        ],
191
      ],
192
    ], $this->defaultMutationCacheMetaData());
193
194
    $this->assertEquals('Test', Node::load(1)->getTitle());
195
  }
196
197
  /**
198
   * Test entity deletion.
199
   */
200
  public function testDeleteEntityMutation() {
201
    $this->mockMutation('deleteNode', [
202
      'name' => 'deleteNode',
203
      'entity_type' => 'node',
204
      'entity_bundle' => 'test',
205
      'arguments' => [
206
        'id' => 'String',
207
      ],
208
      'type' => 'EntityCrudOutput',
209
    ]);
210
211
    $this->createNode([
212
      'title' => 'Test',
213
      'status' => 1,
214
      'type' => 'test',
215
    ]);
216
217
    $this->assertResults('mutation ($nid: String!) { deleteNode(id: $nid) { entity { entityLabel } } }', [
218
      'nid' => '1',
219
    ], [
220
      'deleteNode' => [
221
        'entity' => [
222
          'entityLabel' => 'Test',
223
        ],
224
      ],
225
    ], $this->defaultMutationCacheMetaData());
226
227
    $this->assertEmpty(Node::load(1));
228
  }
229
230
}
231