|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\graphql_core\Kernel\EntityMutation; |
| 4 | + |
| 5 | +use Drupal\graphql\Annotation\GraphQLMutation; |
| 6 | +use Drupal\graphql_core\Plugin\GraphQL\Mutations\Entity\CreateEntityBase; |
| 7 | +use Drupal\graphql_core\Plugin\GraphQL\Mutations\Entity\DeleteEntityBase; |
| 8 | +use Drupal\graphql_core\Plugin\GraphQL\Mutations\Entity\UpdateEntityBase; |
| 9 | +use Drupal\node\Entity\Node; |
| 10 | +use Drupal\Tests\graphql_core\Kernel\GraphQLContentTestBase; |
| 11 | + |
| 12 | +/** |
| 13 | + * Test abstract entity mutation classes. |
| 14 | + */ |
| 15 | +class EntityMutationTest extends GraphQLContentTestBase { |
| 16 | + |
| 17 | + /** |
| 18 | + * {@inheritdoc} |
| 19 | + * |
| 20 | + * Allow to modify all nodes. |
| 21 | + */ |
| 22 | + protected function userPermissions() { |
| 23 | + $perms = parent::userPermissions(); |
| 24 | + $perms[] = 'bypass node access'; |
| 25 | + $perms[] = 'administer nodes'; |
| 26 | + return $perms; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * {@inheritdoc} |
| 31 | + */ |
| 32 | + protected function setUp() { |
| 33 | + parent::setUp(); |
| 34 | + $this->mockInputType('node_input', [ |
| 35 | + 'name' => 'NodeInput', |
| 36 | + 'fields' => [ |
| 37 | + 'title' => 'String', |
| 38 | + 'body' => 'String', |
| 39 | + ], |
| 40 | + ]); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Test entity creation. |
| 45 | + */ |
| 46 | + public function testCreateEntityMutation() { |
| 47 | + $definition = $this->getTypeSystemPluginDefinition(GraphQLMutation::class, [ |
| 48 | + 'id' => 'createNode', |
| 49 | + 'name' => 'createNode', |
| 50 | + 'entity_type' => 'node', |
| 51 | + 'entity_bundle' => 'test', |
| 52 | + 'arguments' => [ |
| 53 | + 'input' => 'NodeInput', |
| 54 | + ], |
| 55 | + 'type' => 'EntityCrudOutput', |
| 56 | + ]); |
| 57 | + |
| 58 | + $mutation = $this->getMockBuilder(CreateEntityBase::class) |
| 59 | + ->setConstructorArgs([ |
| 60 | + [], |
| 61 | + 'createNode', |
| 62 | + $definition, |
| 63 | + $this->container->get('entity_type.manager'), |
| 64 | + ]) |
| 65 | + ->setMethods([ |
| 66 | + 'extractEntityInput', |
| 67 | + ])->getMock(); |
| 68 | + |
| 69 | + $mutation |
| 70 | + ->expects(static::any()) |
| 71 | + ->method('extractEntityInput') |
| 72 | + ->with($this->anything(), $this->anything()) |
| 73 | + ->will($this->returnCallback(function ($args, $info) { |
| 74 | + return [ |
| 75 | + 'title' => $args['input']['title'], |
| 76 | + 'status' => 1, |
| 77 | + 'body' => [ |
| 78 | + 'value' => $args['input']['body'], |
| 79 | + ], |
| 80 | + ]; |
| 81 | + })); |
| 82 | + |
| 83 | + $this->addTypeSystemPlugin($mutation); |
| 84 | + |
| 85 | + $metadata = $this->defaultCacheMetaData(); |
| 86 | + $metadata->setCacheMaxAge(0); |
| 87 | + $metadata->addCacheTags(['node:1']); |
| 88 | + |
| 89 | + $this->assertResults('mutation ($node: NodeInput!) { createNode(input: $node) { entity { entityId } } }', [ |
| 90 | + 'node' => [ |
| 91 | + 'title' => 'Test', |
| 92 | + 'body' => 'This is a test.', |
| 93 | + ], |
| 94 | + ], [ |
| 95 | + 'createNode' => [ |
| 96 | + 'entity' => [ |
| 97 | + 'entityId' => 1, |
| 98 | + ], |
| 99 | + ], |
| 100 | + ], $metadata); |
| 101 | + |
| 102 | + $this->assertEquals('Test', Node::load(1)->getTitle()); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Test entity creation violations. |
| 107 | + */ |
| 108 | + public function testCreateEntityMutationViolation() { |
| 109 | + $definition = $this->getTypeSystemPluginDefinition(GraphQLMutation::class, [ |
| 110 | + 'id' => 'createNode', |
| 111 | + 'name' => 'createNode', |
| 112 | + 'entity_type' => 'node', |
| 113 | + 'entity_bundle' => 'test', |
| 114 | + 'arguments' => [ |
| 115 | + 'input' => 'NodeInput', |
| 116 | + ], |
| 117 | + 'type' => 'EntityCrudOutput', |
| 118 | + ]); |
| 119 | + |
| 120 | + $mutation = $this->getMockBuilder(CreateEntityBase::class) |
| 121 | + ->setConstructorArgs([ |
| 122 | + [], |
| 123 | + 'createNode', |
| 124 | + $definition, |
| 125 | + $this->container->get('entity_type.manager'), |
| 126 | + ]) |
| 127 | + ->setMethods([ |
| 128 | + 'extractEntityInput', |
| 129 | + ])->getMock(); |
| 130 | + |
| 131 | + $mutation |
| 132 | + ->expects(static::any()) |
| 133 | + ->method('extractEntityInput') |
| 134 | + ->with($this->anything(), $this->anything()) |
| 135 | + ->will($this->returnCallback(function ($args, $info) { |
| 136 | + return [ |
| 137 | + 'status' => 1, |
| 138 | + 'body' => [ |
| 139 | + 'value' => $args['input']['body'], |
| 140 | + ], |
| 141 | + ]; |
| 142 | + })); |
| 143 | + |
| 144 | + $this->addTypeSystemPlugin($mutation); |
| 145 | + |
| 146 | + $metadata = $this->defaultCacheMetaData(); |
| 147 | + $metadata->setCacheMaxAge(0); |
| 148 | + |
| 149 | + $this->assertResults('mutation ($node: NodeInput!) { createNode(input: $node) { violations { message path } } }', [ |
| 150 | + 'node' => [ |
| 151 | + 'title' => 'Test', |
| 152 | + 'body' => 'This is a test.', |
| 153 | + ], |
| 154 | + ], [ |
| 155 | + 'createNode' => [ |
| 156 | + 'violations' => [ |
| 157 | + 0 => [ |
| 158 | + 'message' => 'This value should not be null.', |
| 159 | + 'path' => 'title', |
| 160 | + ], |
| 161 | + ], |
| 162 | + ], |
| 163 | + ], $metadata); |
| 164 | + |
| 165 | + $this->assertEmpty(Node::load(1)); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Test entity updates. |
| 170 | + */ |
| 171 | + public function testUpdateEntityMutation() { |
| 172 | + $definition = $this->getTypeSystemPluginDefinition(GraphQLMutation::class, [ |
| 173 | + 'id' => 'updateNode', |
| 174 | + 'name' => 'updateNode', |
| 175 | + 'entity_type' => 'node', |
| 176 | + 'entity_bundle' => 'test', |
| 177 | + 'arguments' => [ |
| 178 | + 'id' => 'String', |
| 179 | + 'input' => 'NodeInput', |
| 180 | + ], |
| 181 | + 'type' => 'EntityCrudOutput', |
| 182 | + ]); |
| 183 | + |
| 184 | + $mutation = $this->getMockBuilder(UpdateEntityBase::class) |
| 185 | + ->setConstructorArgs([ |
| 186 | + [], |
| 187 | + 'updateNode', |
| 188 | + $definition, |
| 189 | + $this->container->get('entity_type.manager'), |
| 190 | + ]) |
| 191 | + ->setMethods([ |
| 192 | + 'extractEntityInput', |
| 193 | + ])->getMock(); |
| 194 | + |
| 195 | + $mutation |
| 196 | + ->expects(static::any()) |
| 197 | + ->method('extractEntityInput') |
| 198 | + ->with($this->anything(), $this->anything()) |
| 199 | + ->will($this->returnCallback(function ($args, $info) { |
| 200 | + return [ |
| 201 | + 'title' => $args['input']['title'], |
| 202 | + ]; |
| 203 | + })); |
| 204 | + |
| 205 | + $this->addTypeSystemPlugin($mutation); |
| 206 | + |
| 207 | + $this->createNode([ |
| 208 | + 'title' => 'Old title', |
| 209 | + 'status' => 1, |
| 210 | + 'type' => 'test', |
| 211 | + 'body' => [ |
| 212 | + 'value' => 'Old body', |
| 213 | + ], |
| 214 | + ]); |
| 215 | + |
| 216 | + $metadata = $this->defaultCacheMetaData(); |
| 217 | + $metadata->setCacheMaxAge(0); |
| 218 | + $metadata->addCacheTags(['node:1']); |
| 219 | + |
| 220 | + $this->assertResults('mutation ($node: NodeInput!, $nid: String!) { updateNode(id: $nid, input: $node) { entity { entityLabel } } }', [ |
| 221 | + 'nid' => 1, |
| 222 | + 'node' => [ |
| 223 | + 'title' => 'Test', |
| 224 | + ], |
| 225 | + ], [ |
| 226 | + 'updateNode' => [ |
| 227 | + 'entity' => [ |
| 228 | + 'entityLabel' => 'Test', |
| 229 | + ], |
| 230 | + ], |
| 231 | + ], $metadata); |
| 232 | + |
| 233 | + $this->assertEquals('Test', Node::load(1)->getTitle()); |
| 234 | + } |
| 235 | + |
| 236 | + /** |
| 237 | + * Test entity deletion. |
| 238 | + */ |
| 239 | + public function testDeleteEntityMutation() { |
| 240 | + $definition = $this->getTypeSystemPluginDefinition(GraphQLMutation::class, [ |
| 241 | + 'id' => 'deleteNode', |
| 242 | + 'name' => 'deleteNode', |
| 243 | + 'entity_type' => 'node', |
| 244 | + 'arguments' => [ |
| 245 | + 'id' => 'String', |
| 246 | + ], |
| 247 | + 'type' => 'EntityCrudOutput', |
| 248 | + ]); |
| 249 | + |
| 250 | + $mutation = $this->getMockForAbstractClass(DeleteEntityBase::class, [ |
| 251 | + [], |
| 252 | + 'deleteNode', |
| 253 | + $definition, |
| 254 | + $this->container->get('entity_type.manager'), |
| 255 | + ]); |
| 256 | + |
| 257 | + $this->addTypeSystemPlugin($mutation); |
| 258 | + |
| 259 | + $this->createNode([ |
| 260 | + 'title' => 'Test', |
| 261 | + 'status' => 1, |
| 262 | + 'type' => 'test', |
| 263 | + ]); |
| 264 | + |
| 265 | + $metadata = $this->defaultCacheMetaData(); |
| 266 | + $metadata->setCacheMaxAge(0); |
| 267 | + $metadata->addCacheTags(['node:1']); |
| 268 | + |
| 269 | + $this->assertResults('mutation ($nid: String!) { deleteNode(id: $nid) { entity { entityLabel } } }', [ |
| 270 | + 'nid' => 1, |
| 271 | + ], [ |
| 272 | + 'deleteNode' => [ |
| 273 | + 'entity' => [ |
| 274 | + 'entityLabel' => 'Test', |
| 275 | + ], |
| 276 | + ], |
| 277 | + ], $metadata); |
| 278 | + |
| 279 | + $this->assertEmpty(Node::load(1)); |
| 280 | + } |
| 281 | + |
| 282 | +} |
0 commit comments