-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathEckTrait.php
204 lines (178 loc) · 5.99 KB
/
EckTrait.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
204
<?php
declare(strict_types=1);
namespace DrevOps\BehatSteps;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Gherkin\Node\TableNode;
use Drupal\eck\EckEntityInterface;
/**
* Trait EckTrait.
*
* Entity Contraction Kit-related steps.
*
* @package DrevOps\BehatSteps
*/
trait EckTrait {
/**
* Custom eck content entities organised by entity type.
*
* @var array<string, array<int, \Drupal\eck\EckEntityInterface>>
*/
protected $eckEntities = [];
/**
* Create eck entities.
*
* Provide entity data in the following format:
* | title | field_marine_animal | field_fish_type | ... |
* | Snook | Fish | Marine fish | 10 |
* | ... | ... | ... | ... |
*
* @Given :bundle :entity_type entities:
*/
public function eckEntitiesCreate(string $bundle, string $entity_type, TableNode $table): void {
$filtered_table = TableNode::fromList($table->getColumn(0));
// Delete entities before creating them.
$this->eckDeleteEntities($bundle, $entity_type, $filtered_table);
$this->eckCreateEntities($entity_type, $bundle, $table);
}
/**
* Remove custom entities by field.
*
* Provide custom entity data in the following format:
*
* | field | value |
* | field_a | Entity label |
*
* @Given no :bundle :entity_type entities:
*/
public function eckDeleteEntities(string $bundle, string $entity_type, TableNode $table): void {
foreach ($table->getHash() as $nodeHash) {
$entity_ids = $this->eckLoadMultiple($entity_type, $bundle, $nodeHash);
$controller = \Drupal::entityTypeManager()->getStorage($entity_type);
$entities = $controller->loadMultiple($entity_ids);
foreach ($entities as $entity) {
$entity->delete();
}
}
}
/**
* Remove ECK types and entities.
*
* @AfterScenario
*/
public function eckEntitiesCleanAll(AfterScenarioScope $scope): void {
// Allow to skip this by adding a tag.
if ($scope->getScenario()->hasTag('behat-steps-skip:' . __FUNCTION__)) {
return;
}
$entity_ids_by_type = [];
foreach ($this->eckEntities as $entity_type => $content_entities) {
/** @var \Drupal\eck\EckEntityInterface $content_entity */
foreach ($content_entities as $content_entity) {
$entity_ids_by_type[$entity_type][] = $content_entity->id();
}
}
foreach ($entity_ids_by_type as $entity_type => $entity_ids) {
$controller = \Drupal::entityTypeManager()->getStorage($entity_type);
$entities = $controller->loadMultiple($entity_ids);
$controller->delete($entities);
}
$this->eckEntities = [];
}
/**
* Load multiple entities with specified type and conditions.
*
* @param string $entity_type
* The entity type.
* @param string $bundle
* The entity bundle.
* @param array<string, string> $conditions
* Conditions keyed by field names.
*
* @return array<int, string>
* Array of entity ids.
*/
protected function eckLoadMultiple(string $entity_type, string $bundle, array $conditions = []): array {
$query = \Drupal::entityQuery($entity_type)
->accessCheck(FALSE)
->condition('type', $bundle);
foreach ($conditions as $k => $v) {
$and = $query->andConditionGroup();
$and->condition($k, $v);
$query->condition($and);
}
return $query->execute();
}
/**
* Create custom content entities.
*
* @param string $entity_type
* The content entity type.
* @param string $bundle
* The content entity bundle.
* @param \Behat\Gherkin\Node\TableNode $table
* The TableNode of entity data.
*/
protected function eckCreateEntities(string $entity_type, string $bundle, TableNode $table): void {
foreach ($table->getHash() as $entity_hash) {
$entity = (object) $entity_hash;
$entity->type = $bundle;
$this->eckCreateEntity($entity_type, $entity);
}
}
/**
* Create a single content entity.
*/
protected function eckCreateEntity(string $entity_type, \StdClass $entity): void {
$this->parseEntityFields($entity_type, $entity);
$saved = $this->getDriver()->createEntity($entity_type, $entity);
if ($saved instanceof EckEntityInterface) {
$this->eckEntities[$entity_type][] = $saved;
}
}
/**
* Navigate to edit eck entity page with specified type and title.
*
* @code
* When I edit "contact" "contact_type" with title "Test contact"
* @endcode
*
* @When I edit :bundle :entity_type with title :label
*/
public function eckEditEntityWithTitle(string $bundle, string $entity_type, string $label): void {
$entity_type_manager = \Drupal::entityTypeManager();
$entity_ids = $this->eckLoadMultiple($entity_type, $bundle, [
'title' => $label,
]);
if (empty($entity_ids)) {
throw new \RuntimeException(sprintf('Unable to find %s page "%s"', $entity_type, $label));
}
$entity_id = current($entity_ids);
$entity = $entity_type_manager->getStorage($entity_type)->load($entity_id);
$path = $entity->toUrl('edit-form')->toString();
print $path;
$this->getSession()->visit($path);
}
/**
* Navigate to view entity page with specified type and title.
*
* @code
* When I visit "contact" "contact_type" with title "Test contact"
* @endcode
*
* @When I visit :bundle :entity_type with title :label
*/
public function eckVisitEntityPageWithTitle(string $bundle, string $entity_type, string $label): void {
$entity_type_manager = \Drupal::entityTypeManager();
$entity_ids = $this->eckLoadMultiple($entity_type, $bundle, [
'title' => $label,
]);
if (empty($entity_ids)) {
throw new \RuntimeException(sprintf('Unable to find %s page "%s"', $entity_type, $label));
}
$entity_id = current($entity_ids);
$entity = $entity_type_manager->getStorage($entity_type)->load($entity_id);
$path = $entity->toUrl('canonical')->toString();
print $path;
$this->getSession()->visit($path);
}
}