Completed
Pull Request — 8.x-3.x (#409)
by Sebastian
02:31
created

DeleteEntityTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 73
Duplicated Lines 17.81 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 13
loc 73
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 13 13 1
A testDeletion() 0 9 1
A testDeletionWithoutPermission() 0 12 1
A testDeletionOfNonExistentEntity() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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() {
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...
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