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

EntityMutationTest::testCreateEntityMutation()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 23

Duplication

Lines 34
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 23
nc 1
nop 0
dl 34
loc 34
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\Annotation\GraphQLMutation;
6
use Drupal\graphql\Plugin\GraphQL\Mutations\MutationPluginBase;
7
use Drupal\graphql_core\Plugin\GraphQL\Mutations\Entity\CreateEntityBase;
8
use Drupal\graphql_core\Plugin\GraphQL\Mutations\Entity\DeleteEntityBase;
9
use Drupal\graphql_core\Plugin\GraphQL\Mutations\Entity\UpdateEntityBase;
10
use Drupal\node\Entity\Node;
11
use Drupal\Tests\graphql_core\Kernel\GraphQLContentTestBase;
12
13
/**
14
 * Test abstract entity mutation classes.
15
 *
16
 * @group graphql_core
17
 */
18
class EntityMutationTest extends GraphQLContentTestBase {
19
20
  /**
21
   * {@inheritdoc}
22
   *
23
   * Allow to modify all nodes.
24
   */
25
  protected function userPermissions() {
26
    $perms = parent::userPermissions();
27
    $perms[] = 'bypass node access';
28
    $perms[] = 'administer nodes';
29
    return $perms;
30
  }
31
32
  /**
33
   * {@inheritdoc}
34
   */
35
  protected function setUp() {
36
    parent::setUp();
37
    $this->mockInputType('node_input', [
38
      'name' => 'NodeInput',
39
      'fields' => [
40
        'title' => 'String',
41
        'body' => 'String',
42
      ],
43
    ]);
44
  }
45
46
  protected function mockMutationFactory($definition, $result = NULL, $builder = NULL) {
47
    $mutation = $this->getMockBuilder([
48
      'createNode' => CreateEntityBase::class,
49
      'updateNode' => UpdateEntityBase::class,
50
      'deleteNode' => DeleteEntityBase::class,
51
    ][$definition['id']])
52
      ->setConstructorArgs([
53
        [],
54
        $definition['id'],
55
        $definition,
56
        $this->container->get('entity_type.manager'),
57
      ])
58
      ->setMethods([
59
        'extractEntityInput',
60
      ])->getMock();
61
62
    if (isset($result)) {
63
      $mutation
64
        ->expects(static::any())
65
        ->method('extractEntityInput')
66
        ->with(static::anything(), static::anything(), static::anything(), static::anything())
67
        ->will($this->toBoundPromise($result, $mutation));
68
    }
69
70
    if (is_callable($builder)) {
71
      $builder($mutation);
72
    }
73
74
    return $mutation;
75
  }
76
77
  /**
78
   * Test entity creation.
79
   */
80 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...
81
    $this->mockMutation('createNode', [
82
      'name' => 'createNode',
83
      'entity_type' => 'node',
84
      'entity_bundle' => 'test',
85
      'arguments' => [
86
        'input' => 'NodeInput',
87
      ],
88
      'type' => 'EntityCrudOutput',
89
    ], 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...
90
        return [
91
          'title' => $args['input']['title'],
92
          'status' => 1,
93
          'body' => [
94
            'value' => $args['input']['body'],
95
          ],
96
        ];
97
    });
98
99
    $this->assertResults('mutation ($node: NodeInput!) { createNode(input: $node) { entity { entityId } } }', [
100
      'node' => [
101
        'title' => 'Test',
102
        'body' => 'This is a test.',
103
      ],
104
    ], [
105
      'createNode' => [
106
        'entity' => [
107
          'entityId' => 1,
108
        ],
109
      ],
110
    ], $this->defaultMutationCacheMetaData());
111
112
    $this->assertEquals('Test', Node::load(1)->getTitle());
113
  }
114
115
  /**
116
   * Test entity creation violations.
117
   */
118 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...
119
    $this->mockMutation('createNode', [
120
      'name' => 'createNode',
121
      'entity_type' => 'node',
122
      'entity_bundle' => 'test',
123
      'arguments' => [
124
        'input' => 'NodeInput',
125
      ],
126
      'type' => 'EntityCrudOutput',
127
    ], 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...
128
      return [
129
        'status' => 1,
130
        'body' => [
131
          'value' => $args['input']['body'],
132
        ],
133
      ];
134
    });
135
136
    $this->assertResults('mutation ($node: NodeInput!) { createNode(input: $node) { violations { message path } } }', [
137
      'node' => [
138
        'title' => 'Test',
139
        'body' => 'This is a test.',
140
      ],
141
    ], [
142
      'createNode' => [
143
        'violations' => [
144
          0 => [
145
            'message' => 'This value should not be null.',
146
            'path' => 'title',
147
          ],
148
        ],
149
      ],
150
    ], $this->defaultMutationCacheMetaData());
151
152
    $this->assertEmpty(Node::load(1));
153
  }
154
155
  /**
156
   * Test entity updates.
157
   */
158
  public function testUpdateEntityMutation() {
159
    $this->mockMutation('updateNode', [
160
      'name' => 'updateNode',
161
      'entity_type' => 'node',
162
      'entity_bundle' => 'test',
163
      'arguments' => [
164
        'id' => 'String',
165
        'input' => 'NodeInput',
166
      ],
167
      'type' => 'EntityCrudOutput',
168
    ], 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...
169
      return [
170
        'title' => $args['input']['title'],
171
      ];
172
    });
173
174
    $this->createNode([
175
      'title' => 'Old title',
176
      'status' => 1,
177
      'type' => 'test',
178
      'body' => [
179
        'value' => 'Old body',
180
      ],
181
    ]);
182
183
    $this->assertResults('mutation ($node: NodeInput!, $nid: String!) { updateNode(id: $nid, input: $node) { entity { entityLabel } } }', [
184
      'nid' => '1',
185
      'node' => [
186
        'title' => 'Test',
187
      ],
188
    ], [
189
      'updateNode' => [
190
        'entity' => [
191
          'entityLabel' => 'Test',
192
        ],
193
      ],
194
    ], $this->defaultMutationCacheMetaData());
195
196
    $this->assertEquals('Test', Node::load(1)->getTitle());
197
  }
198
199
  /**
200
   * Test entity deletion.
201
   */
202
  public function testDeleteEntityMutation() {
203
    $this->mockMutation('deleteNode', [
204
      'name' => 'deleteNode',
205
      'entity_type' => 'node',
206
      'entity_bundle' => 'test',
207
      'arguments' => [
208
        'id' => 'String',
209
      ],
210
      'type' => 'EntityCrudOutput',
211
    ]);
212
213
    $this->createNode([
214
      'title' => 'Test',
215
      'status' => 1,
216
      'type' => 'test',
217
    ]);
218
219
    $this->assertResults('mutation ($nid: String!) { deleteNode(id: $nid) { entity { entityLabel } } }', [
220
      'nid' => '1',
221
    ], [
222
      'deleteNode' => [
223
        'entity' => [
224
          'entityLabel' => 'Test',
225
        ],
226
      ],
227
    ], $this->defaultMutationCacheMetaData());
228
229
    $this->assertEmpty(Node::load(1));
230
  }
231
232
}
233