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