1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\Tests\graphql_mutation\Kernel; |
4
|
|
|
|
5
|
|
|
use Drupal\simpletest\ContentTypeCreationTrait; |
6
|
|
|
use Drupal\simpletest\NodeCreationTrait; |
7
|
|
|
use Drupal\Tests\graphql\Kernel\GraphQLFileTestBase; |
8
|
|
|
use Drupal\user\Entity\Role; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Test entity deletion. |
12
|
|
|
* |
13
|
|
|
* @group graphql_content_mutation |
14
|
|
|
*/ |
15
|
|
|
class DeleteEntityTest extends GraphQLFileTestBase { |
16
|
|
|
use ContentTypeCreationTrait; |
17
|
|
|
use NodeCreationTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
public static $modules = [ |
23
|
|
|
'node', |
24
|
|
|
'field', |
25
|
|
|
'text', |
26
|
|
|
'filter', |
27
|
|
|
'graphql_core', |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
protected function setUp() { |
|
|
|
|
34
|
|
|
parent::setUp(); |
35
|
|
|
$this->installConfig('node'); |
36
|
|
|
$this->installConfig('filter'); |
37
|
|
|
$this->installEntitySchema('node'); |
38
|
|
|
$this->installSchema('node', 'node_access'); |
39
|
|
|
$this->createContentType(['type' => 'test']); |
40
|
|
|
|
41
|
|
|
Role::load('anonymous') |
42
|
|
|
->grantPermission('access content') |
43
|
|
|
->grantPermission('delete any test content') |
44
|
|
|
->save(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Test deletion. |
49
|
|
|
*/ |
50
|
|
|
public function testDeletion() { |
51
|
|
|
$node = $this->createNode(['type' => 'test']); |
52
|
|
|
$result = $this->executeQueryFile('delete.gql', ['id' => $node->id()]); |
53
|
|
|
$entity = $result['data']['deleteNode']['entity']; |
54
|
|
|
$errors = $result['data']['deleteNode']['errors']; |
55
|
|
|
|
56
|
|
|
$this->assertEquals($node->id(), $entity['entityId'], 'Deleted entity with correct id.'); |
57
|
|
|
$this->assertEmpty($errors, 'Entity deletion succeeded without any errors.'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Test deletion without the necessary permissions |
62
|
|
|
*/ |
63
|
|
|
public function testDeletionWithoutPermission() { |
64
|
|
|
Role::load('anonymous') |
65
|
|
|
->revokePermission('delete any test content') |
66
|
|
|
->save(); |
67
|
|
|
|
68
|
|
|
$node = $this->createNode(['type' => 'test']); |
69
|
|
|
$result = $this->executeQueryFile('delete.gql', ['id' => $node->id()]); |
70
|
|
|
$errors = $result['data']['deleteNode']['errors']; |
71
|
|
|
|
72
|
|
|
$this->assertNotEmpty($errors, 'Failed to delete entity.'); |
73
|
|
|
$this->assertEquals(['You do not have the necessary permissions to delete this entity.'], $errors, 'Failed to delete entity due to missing permissions.'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Test deletion of a non-existent entity. |
78
|
|
|
*/ |
79
|
|
|
public function testDeletionOfNonExistentEntity() { |
80
|
|
|
$result = $this->executeQueryFile('delete.gql', ['id' => '123']); |
81
|
|
|
$errors = $result['data']['deleteNode']['errors']; |
82
|
|
|
|
83
|
|
|
$this->assertNotEmpty($errors, 'Failed to delete entity.'); |
84
|
|
|
$this->assertEquals(['The requested entity could not be loaded.'], $errors, 'Failed to delete non-existent entity.'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
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.