-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
TaxonomyTrait.php
164 lines (137 loc) · 3.98 KB
/
TaxonomyTrait.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
<?php
declare(strict_types=1);
namespace DrevOps\BehatSteps;
use Behat\Gherkin\Node\TableNode;
use Drupal\taxonomy\Entity\Vocabulary;
/**
* Trait TaxonomyTrait.
*
* Taxonomy term-related steps.
*
* @package DrevOps\BehatSteps
*/
trait TaxonomyTrait {
/**
* Assert that a vocabulary exist.
*
* @code
* Given vocabulary "topics" with name "Topics" exists
* @endcode
*
* @Given vocabulary :vid with name :name exists
*/
public function taxonomyAssertVocabularyExist(string $name, string $vid): void {
$vocab = Vocabulary::load($vid);
if (!$vocab) {
throw new \Exception(sprintf('"%s" vocabulary does not exist', $vid));
}
if ($vocab->get('name') != $name) {
throw new \Exception(sprintf('"%s" vocabulary name is not "%s"', $vid, $name));
}
}
/**
* Assert that a taxonomy term exist by name.
*
* @code
* Given taxonomy term "Apple" from vocabulary "Fruits" exists
* @endcode
*
* @Given taxonomy term :name from vocabulary :vocabulary_id exists
*/
public function taxonomyAssertTermExistsByName(string $name, string $vid): void {
$vocab = Vocabulary::load($vid);
if (!$vocab) {
throw new \RuntimeException(sprintf('"%s" vocabulary does not exist', $vid));
}
$found = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadByProperties([
'name' => $name,
'vid' => $vid,
]);
if (count($found) == 0) {
throw new \Exception(sprintf('Taxonomy term "%s" from vocabulary "%s" does not exist', $name, $vid));
}
}
/**
* Remove terms from a specified vocabulary.
*
* @code
* Given no "Fruits" terms:
* | Apple |
* | Pear |
* @endcode
*
* @Given no :vocabulary terms:
*/
public function taxonomyDeleteTerms(string $vocabulary, TableNode $termsTable): void {
foreach ($termsTable->getColumn(0) as $name) {
$terms = \Drupal::service('entity_type.manager')->getStorage('taxonomy_term')->loadByProperties([
'name' => $name,
'vid' => $vocabulary,
]);
/** @var \Drupal\taxonomy\Entity\Term $term */
foreach ($terms as $term) {
$term->delete();
}
}
}
/**
* Visit specified vocabulary term page.
*
* @When I visit :vocabulary vocabulary term :name
*/
public function taxonomyVisitTermPageWithName(string $vocabulary, string $name): void {
$tids = $this->taxonomyLoadMultiple($vocabulary, [
'name' => $name,
]);
if (empty($tids)) {
throw new \RuntimeException(sprintf('Unable to find %s term "%s"', $vocabulary, $name));
}
ksort($tids);
$tid = end($tids);
$path = $this->locatePath('/taxonomy/term/' . $tid);
print $path;
$this->getSession()->visit($path);
}
/**
* Visit specified vocabulary term edit page.
*
* @When I edit :vocabulary vocabulary term :name
*/
public function taxonomyEditTermPageWithName(string $vocabulary, string $name): void {
$tids = $this->taxonomyLoadMultiple($vocabulary, [
'name' => $name,
]);
if (empty($tids)) {
throw new \RuntimeException(sprintf('Unable to find %s term "%s"', $vocabulary, $name));
}
ksort($tids);
$tid = end($tids);
$path = $this->locatePath('/taxonomy/term/' . $tid . '/edit');
print $path;
$this->getSession()->visit($path);
}
/**
* Load multiple terms with specified vocabulary and conditions.
*
* @param string $vocabulary
* The term vocabulary.
* @param array<string,string> $conditions
* Conditions keyed by field names.
*
* @return array<int, string>
* Array of term ids.
*/
protected function taxonomyLoadMultiple(string $vocabulary, array $conditions = []): array {
$query = \Drupal::entityQuery('taxonomy_term')
->accessCheck(FALSE)
->condition('vid', $vocabulary);
foreach ($conditions as $k => $v) {
$and = $query->andConditionGroup();
$and->condition($k, $v);
$query->condition($and);
}
return $query->execute();
}
}