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

CreateEntityTest::testCreationWithBodyField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\Tests\graphql_mutation\Kernel;
4
5
use Drupal\simpletest\ContentTypeCreationTrait;
6
use Drupal\Tests\graphql\Kernel\GraphQLFileTestBase;
7
use Drupal\user\Entity\Role;
8
9
/**
10
 * Test entity creation.
11
 *
12
 * @group graphql_mutation
13
 */
14
class CreateEntityTest extends GraphQLFileTestBase {
15
  use ContentTypeCreationTrait;
16
17
  /**
18
   * {@inheritdoc}
19
   */
20
  public static $modules = [
21
    'node',
22
    'field',
23
    'text',
24
    'filter',
25
    'graphql_core',
26
    'graphql_mutation',
27
  ];
28
29
  /**
30
   * {@inheritdoc}
31
   */
32 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...
33
    parent::setUp();
34
    $this->installConfig('node');
35
    $this->installConfig('filter');
36
    $this->installEntitySchema('node');
37
    $this->installSchema('node', 'node_access');
38
    $this->createContentType(['type' => 'test']);
39
40
    Role::load('anonymous')
41
      ->grantPermission('access content')
42
      ->grantPermission('create test content')
43
      ->save();
44
  }
45
46
  /**
47
   * Test creation with a simple text field.
48
   */
49
  public function testCreationWithBodyField() {
50
    $values = [
51
      'title' => 'Test node',
52
      'body' => [
53
        'value' => 'I am fine with my body.',
54
      ],
55
    ];
56
57
    $result = $this->executeQueryFile('create.gql', ['input' => $values]);
58
    $entity = $result['data']['createNodeTest']['entity'];
59
    $errors = $result['data']['createNodeTest']['errors'];
60
    $violations = $result['data']['createNodeTest']['violations'];
61
62
    $this->assertEquals($values['title'], $entity['entityLabel'], 'Created entity with correct title.');
63
    $this->assertEquals($values['body']['value'], $entity['body']['value'], 'Created entity with correct body value.');
64
    $this->assertEmpty($errors, 'Entity creation succeeded without any errors.');
65
    $this->assertEmpty($violations, 'Entity creation succeeded without any constraint violations.');
66
  }
67
68
  /**
69
   * Test creation with missing values for a required field.
70
   */
71
  public function testCreationWithViolations() {
72
    $result = $this->executeQueryFile('create.gql', ['input' => []]);
73
    $errors = $result['data']['createNodeTest']['errors'];
74
    $violations = $result['data']['createNodeTest']['violations'];
75
76
    $this->assertEmpty($errors, 'No errors were thrown.');
77
    $this->assertNotEmpty($violations, 'Entity creation failed due to constraint violations.');
78
    $this->assertEquals([[
79
      'path' => 'title',
80
      'message' => 'This value should not be null.',
81
    ]], $violations, 'Entity creation failed due to constraint violations.');
82
  }
83
84
  /**
85
   * Test creation without the necessary permissions
86
   */
87
  public function testCreationWithoutPermission() {
88
    Role::load('anonymous')
89
      ->revokePermission('create test content')
90
      ->save();
91
92
    $result = $this->executeQueryFile('create.gql', ['input' => []]);
93
    $errors = $result['data']['createNodeTest']['errors'];
94
95
    $this->assertNotEmpty($errors, 'Failed to create entity.');
96
    $this->assertEquals(['You do not have the necessary permissions to create entities of this type.'], $errors, 'Failed to create entity due to missing permissions.');
97
  }
98
99
}
100