-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
ContentTrait.php
203 lines (175 loc) · 5.2 KB
/
ContentTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
declare(strict_types=1);
namespace DrevOps\BehatSteps;
use Behat\Gherkin\Node\TableNode;
/**
* Trait ContentTrait.
*
* Content-related steps.
*
* @package DrevOps\BehatSteps
*/
trait ContentTrait {
/**
* Delete content type.
*
* @code
* Given no "article" content type
* @endcode
*
* @Given no :type content type
*/
public function contentRemoveContentType(string $type): void {
$content_type_entity = \Drupal::entityTypeManager()->getStorage('node_type')->load($type);
if ($content_type_entity) {
$content_type_entity->delete();
}
}
/**
* Navigate to page with specified type and title.
*
* @code
* When I visit "article" "Test article"
* @endcode
*
* @When I visit :type :title
*/
public function contentVisitPageWithTitle(string $type, string $title): void {
$nids = $this->contentLoadMultiple($type, [
'title' => $title,
]);
if (empty($nids)) {
throw new \RuntimeException(sprintf('Unable to find %s page "%s"', $type, $title));
}
ksort($nids);
$nid = end($nids);
$path = $this->locatePath('/node/' . $nid);
print $path;
$this->getSession()->visit($path);
}
/**
* Navigate to edit page with specified type and title.
*
* @code
* When I edit "article" "Test article"
* @endcode
*
* @When I edit :type :title
*/
public function contentEditPageWithTitle(string $type, string $title): void {
$nids = $this->contentLoadMultiple($type, [
'title' => $title,
]);
if (empty($nids)) {
throw new \RuntimeException(sprintf('Unable to find %s page "%s"', $type, $title));
}
$nid = current($nids);
$path = $this->locatePath('/node/' . $nid) . '/edit';
print $path;
$this->getSession()->visit($path);
}
/**
* Navigate to delete page with specified type and title.
*
* @When I delete :type :title
*/
public function contentDeletePageWithTitle(string $type, string $title): void {
$nids = $this->contentLoadMultiple($type, [
'title' => $title,
]);
if (empty($nids)) {
throw new \RuntimeException(sprintf('Unable to find %s page "%s"', $type, $title));
}
$nid = current($nids);
$path = $this->locatePath('/node/' . $nid) . '/delete';
print $path;
$this->getSession()->visit($path);
}
/**
* Remove content defined by provided properties.
*
* @code
* Given no "article" content:
* | title |
* | Test article |
* | Another test article |
* @endcode
*
* @Given /^no ([a-zA-z0-9_-]+) content:$/
*/
public function contentDelete(string $type, TableNode $nodesTable): void {
foreach ($nodesTable->getHash() as $nodeHash) {
$nids = $this->contentLoadMultiple($type, $nodeHash);
$controller = \Drupal::entityTypeManager()->getStorage('node');
$entities = $controller->loadMultiple($nids);
$controller->delete($entities);
}
}
/**
* Change moderation state of a content with specified title.
*
* @code
* When the moderation state of "article" "Test article" changes from "draft" to "published"
* @endcode
*
* @When the moderation state of :type :title changes from :old_state to :new_state
*/
public function contentModeratePageWithTitle(string $type, string $title, string $old_state, string $new_state): void {
$nodes = \Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties([
'title' => $title,
'type' => $type,
]);
if (empty($nodes)) {
throw new \Exception(sprintf('Unable to find %s page "%s"', $type, $title));
}
/** @var \Drupal\node\Entity\Node $node */
$node = current($nodes);
$current_old_state = $node->get('moderation_state')->first()->getString();
if ($current_old_state != $old_state) {
throw new \Exception(sprintf('The current state "%s" is different from "%s"', $current_old_state, $old_state));
}
$node->set('moderation_state', $new_state);
$node->save();
}
/**
* Visit scheduled-transition page for node with title.
*
* @When I visit :type :title scheduled transitions
*/
public function contentVisitScheduledTransitionsPageWithTitle(string $type, string $title): void {
$nids = $this->contentLoadMultiple($type, [
'title' => $title,
]);
if (empty($nids)) {
throw new \RuntimeException(sprintf('Unable to find %s page "%s"', $type, $title));
}
$nid = current($nids);
$path = $this->locatePath('/node/' . $nid) . '/scheduled-transitions';
print $path;
$this->getSession()->visit($path);
}
/**
* Load multiple nodes with specified type and conditions.
*
* @param string $type
* The node type.
* @param array<string, mixed> $conditions
* Conditions keyed by field names.
*
* @return array<int, string>
* Array of node ids.
*/
protected function contentLoadMultiple(string $type, array $conditions = []): array {
$query = \Drupal::entityQuery('node')
->accessCheck(FALSE)
->condition('type', $type);
foreach ($conditions as $k => $v) {
$and = $query->andConditionGroup();
$and->condition($k, $v);
$query->condition($and);
}
return $query->execute();
}
}