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

UpdateEntityTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 108
Duplicated Lines 12.04 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 13 13 1
B testUpdateWithBodyField() 0 26 1
A testUpdateWithViolations() 0 20 1
A testUpdateWithoutPermission() 0 16 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 update.
12
 *
13
 * @group graphql_mutation
14
 */
15
class UpdateEntityTest 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
    'graphql_mutation',
29
  ];
30
31
  /**
32
   * {@inheritdoc}
33
   */
34 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...
35
    parent::setUp();
36
    $this->installConfig('node');
37
    $this->installConfig('filter');
38
    $this->installEntitySchema('node');
39
    $this->installSchema('node', 'node_access');
40
    $this->createContentType(['type' => 'test']);
41
42
    Role::load('anonymous')
43
      ->grantPermission('access content')
44
      ->grantPermission('edit any test content')
45
      ->save();
46
  }
47
48
  /**
49
   * Test update with a simple text field.
50
   */
51
  public function testUpdateWithBodyField() {
52
    $node = $this->createNode([
53
      'type' => 'test',
54
      'title' => 'Test node (original)',
55
      'body' => [
56
        'value' => 'I am fine with my body.',
57
      ],
58
    ]);
59
60
    $values = [
61
      'title' => 'Test node (updated)',
62
      'body' => [
63
        'value' => 'I am fine with my body, still.',
64
      ],
65
    ];
66
67
    $result = $this->executeQueryFile('update.gql', ['id' => $node->id(), 'input' => $values]);
68
    $entity = $result['data']['updateNodeTest']['entity'];
69
    $errors = $result['data']['updateNodeTest']['errors'];
70
    $violations = $result['data']['updateNodeTest']['violations'];
71
72
    $this->assertEquals($values['title'], $entity['entityLabel'], 'Update entity with correct title.');
73
    $this->assertEquals($values['body']['value'], $entity['body']['value'], 'Update entity with correct body value.');
74
    $this->assertEmpty($errors, 'Entity update succeeded without any errors.');
75
    $this->assertEmpty($violations, 'Entity update succeeded without any constraint violations.');
76
  }
77
78
  /**
79
   * Test update with missing values for a required field.
80
   */
81
  public function testUpdateWithViolations() {
82
    $node = $this->createNode([
83
      'type' => 'test',
84
      'title' => 'Test node (original)',
85
    ]);
86
87
    $result = $this->executeQueryFile('update.gql', ['id' => $node->id(), 'input' => [
88
      'title' => NULL,
89
    ]]);
90
91
    $errors = $result['data']['updateNodeTest']['errors'];
92
    $violations = $result['data']['updateNodeTest']['violations'];
93
94
    $this->assertEmpty($errors, 'No errors were thrown.');
95
    $this->assertNotEmpty($violations, 'Entity update failed due to constraint violations.');
96
    $this->assertEquals([[
97
      'path' => 'title',
98
      'message' => 'This value should not be null.',
99
    ]], $violations, 'Entity update failed due to constraint violations.');
100
  }
101
102
  /**
103
   * Test update without the necessary permissions
104
   */
105
  public function testUpdateWithoutPermission() {
106
    $node = $this->createNode([
107
      'type' => 'test',
108
      'title' => 'Test node (original)',
109
    ]);
110
111
    Role::load('anonymous')
112
      ->revokePermission('edit any test content')
113
      ->save();
114
115
    $result = $this->executeQueryFile('update.gql', ['id' => $node->id(), 'input' => []]);
116
    $errors = $result['data']['updateNodeTest']['errors'];
117
118
    $this->assertNotEmpty($errors, 'Failed to update entity.');
119
    $this->assertEquals(['You do not have the necessary permissions to update this test.'], $errors, 'Failed to update entity due to missing permissions.');
120
  }
121
122
}
123