Completed
Pull Request — 8.x-3.x (#550)
by Philipp
02:06
created

GraphQLContentTestBase::defaultCacheContexts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 9.4285
1
<?php
2
3
namespace Drupal\Tests\graphql_core\Kernel;
4
5
use Drupal\field\Entity\FieldConfig;
6
use Drupal\field\Entity\FieldStorageConfig;
7
use Drupal\node\Entity\Node;
8
use Drupal\Tests\graphql_core\Traits\RevisionsTestTrait;
9
use Drupal\simpletest\ContentTypeCreationTrait;
10
use Drupal\simpletest\NodeCreationTrait;
11
use Drupal\simpletest\UserCreationTrait;
12
13
/**
14
 * Base class for node based tests.
15
 */
16
class GraphQLContentTestBase extends GraphQLCoreTestBase {
17
  use ContentTypeCreationTrait;
18
  use UserCreationTrait;
19
  use NodeCreationTrait;
20
  use RevisionsTestTrait;
21
22
  /**
23
   * {@inheritdoc}
24
   */
25
  public static $modules = [
26
    'node',
27
    'field',
28
    'filter',
29
    'text',
30
  ];
31
32
  /**
33
   * {@inheritdoc}
34
   */
35
  protected function defaultCacheTags() {
36
    return array_merge([
37
      'config:field.storage.node.body',
38
    ], parent::defaultCacheTags());
39
  }
40
41
  /**
42
   * {@inheritdoc}
43
   *
44
   * Add the 'access content' permission to the mocked account.
45
   */
46
  protected function userPermissions() {
47
    $perms = parent::userPermissions();
48
    $perms[] = 'access content';
49
    $perms[] = 'access user profiles';
50
    return $perms;
51
  }
52
53
  /**
54
   * {@inheritdoc}
55
   */
56
  protected function setUp() {
57
    parent::setUp();
58
59
    $this->installConfig(['node', 'filter', 'text']);
60
    $this->installEntitySchema('node');
61
62
    $this->installSchema('node', 'node_access');
63
    $this->installSchema('system', 'sequences');
64
65
    $this->createContentType([
66
      'type' => 'test',
67
    ]);
68
  }
69
70
  /**
71
   * Mock a field that emits a test node.
72
   *
73
   * ```
74
   * query {
75
   *   node {
76
   *     title
77
   *   }
78
   * }
79
   * ```
80
   *
81
   * @param mixed $values
82
   *   Additional node values.
83
   * @param string $title
84
   *   An optional title. Will default to "Test".
85
   */
86
  protected function mockNode($values, $title = 'Test') {
87
    $this->mockField('node', [
88
      'name' => 'node',
89
      'type' => 'entity:node:test',
90
    ], Node::create([
91
      'title' => $title,
92
      'type' => 'test',
93
    ] + $values));
94
  }
95
96
  /**
97
   * Add a field to test content type.
98
   *
99
   * @param string $type
100
   *   Field type.
101
   * @param string $fieldName
102
   *   Field machine name.
103
   * @param string $label
104
   *   Label for the field.
105
   */
106
  protected function addField($type, $fieldName, $multi = TRUE, $label = 'Label', $bundle = 'test') {
107
    FieldStorageConfig::create([
108
      'field_name' => $fieldName,
109
      'entity_type' => 'node',
110
      'type' => $type,
111
      'cardinality' => $multi ? -1 : 1,
112
    ])->save();
113
114
    FieldConfig::create([
115
      'entity_type' => 'node',
116
      'bundle' => $bundle,
117
      'field_name' => $fieldName,
118
      'label' => $label,
119
    ])->save();
120
  }
121
122
}
123