Completed
Push — 8.x-3.x ( 97a7eb...1f8764 )
by Philipp
04:36
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
  protected function defaultCacheContexts() {
33
    return array_merge([
34
      'languages:language_content',
35
    ], parent::defaultCacheContexts());
36
  }
37
38
  /**
39
   * {@inheritdoc}
40
   */
41
  protected function defaultCacheTags() {
42
    return array_merge([
43
      'config:field.storage.node.body',
44
    ], parent::defaultCacheTags());
45
  }
46
47
  /**
48
   * {@inheritdoc}
49
   *
50
   * Add the 'access content' permission to the mocked account.
51
   */
52
  protected function userPermissions() {
53
    $perms = parent::userPermissions();
54
    $perms[] = 'access content';
55
    $perms[] = 'access user profiles';
56
    return $perms;
57
  }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62
  protected function setUp() {
63
    parent::setUp();
64
65
    $this->installConfig(['node', 'filter', 'text']);
66
    $this->installEntitySchema('node');
67
68
    $this->installSchema('node', 'node_access');
69
    $this->installSchema('system', 'sequences');
70
71
    $this->createContentType([
72
      'type' => 'test',
73
    ]);
74
  }
75
76
  /**
77
   * Mock a field that emits a test node.
78
   *
79
   * ```
80
   * query {
81
   *   node {
82
   *     title
83
   *   }
84
   * }
85
   * ```
86
   *
87
   * @param mixed $values
88
   *   Additional node values.
89
   * @param string $title
90
   *   An optional title. Will default to "Test".
91
   */
92
  protected function mockNode($values, $title = 'Test') {
93
    $this->mockField('node', [
94
      'name' => 'node',
95
      'type' => 'entity:node:test',
96
    ], Node::create([
97
      'title' => $title,
98
      'type' => 'test',
99
    ] + $values));
100
  }
101
102
  /**
103
   * Add a field to test content type.
104
   *
105
   * @param string $type
106
   *   Field type.
107
   * @param string $fieldName
108
   *   Field machine name.
109
   * @param string $label
110
   *   Label for the field.
111
   */
112
  protected function addField($type, $fieldName, $multi = TRUE, $label = 'Label', $bundle = 'test') {
113
    FieldStorageConfig::create([
114
      'field_name' => $fieldName,
115
      'entity_type' => 'node',
116
      'type' => $type,
117
      'cardinality' => $multi ? -1 : 1,
118
    ])->save();
119
120
    FieldConfig::create([
121
      'entity_type' => 'node',
122
      'bundle' => $bundle,
123
      'field_name' => $fieldName,
124
      'label' => $label,
125
    ])->save();
126
  }
127
128
}
129