-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
MenuTrait.php
222 lines (193 loc) · 5.81 KB
/
MenuTrait.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
declare(strict_types=1);
namespace DrevOps\BehatSteps;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Gherkin\Node\TableNode;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\system\Entity\Menu;
use Drupal\system\MenuInterface;
/**
* Trait MenuTrait.
*
* Menu-related steps.
*
* @package DrevOps\BehatSteps
*/
trait MenuTrait {
/**
* Menus.
*
* @var \Drupal\system\Entity\Menu[]
*/
protected $menus = [];
/**
* Menu links.
*
* @var \Drupal\menu_link_content\Entity\MenuLinkContent[]
*/
protected $menuLinks = [];
/**
* Remove menu by menu name.
*
* Provide menu labels in the following format:
* | Fish Menu |
* | ... |
*
* @Given no menus:
*/
public function menuDelete(TableNode $table): void {
foreach ($table->getColumn(0) as $label) {
$menu = $this->loadMenuByLabel($label);
if ($menu instanceof MenuInterface) {
$menu->delete();
}
}
}
/**
* Create a menu if one does not exist.
*
* Provide menu data in the following format:
*
* | label | description |
* | Fish Menu | Menu of fish |
* | ... | ... |
*
* @Given menus:
*/
public function menuCreate(TableNode $table): void {
foreach ($table->getHash() as $menu_hash) {
if (empty($menu_hash['id'])) {
// Create menu id if one was not provided.
$menu_id = strtolower((string) $menu_hash['label']);
$menu_id = preg_replace('/[^a-z0-9_]+/', '_', $menu_id);
$menu_id = preg_replace('/_+/', '_', (string) $menu_id);
$menu_hash['id'] = $menu_id;
}
$menu = Menu::create($menu_hash);
$menu->save();
$this->menus[] = $menu;
}
}
/**
* Remove menu links by title.
*
* Provide menu link titles in the following format:
* | Test Menu |
* | ... |
*
* @Given no :menu_name menu_links:
*/
public function menuLinksDelete(string $menu_name, TableNode $table): void {
foreach ($table->getColumn(0) as $title) {
$menu_link = $this->loadMenuLinkByTitle($title, $menu_name);
if ($menu_link instanceof MenuLinkContent) {
$menu_link->delete();
}
}
}
/**
* Create menu links.
*
* Provide menu link data in the following format:
*
* | title | enabled | uri | parent |
* | Parent Link | 1 | https://www.example.com | |
* | Child Link | 1 | https://www.example.com | Parent Link |
* | ... | ... | ... | ... |
*
* @Given :menu_name menu_links:
*/
public function menuLinksCreate(string $menu_name, TableNode $table): void {
$menu = $this->loadMenuByLabel($menu_name);
if (!$menu instanceof MenuInterface) {
throw new \RuntimeException(sprintf('Menu "%s" not found', $menu_name));
}
foreach ($table->getHash() as $menu_link_hash) {
$menu_link_hash['menu_name'] = $menu->id();
// Add uri to correct property.
$menu_link_hash['link']['uri'] = $menu_link_hash['uri'];
unset($menu_link_hash['uri']);
// Create parent property in format required.
if (!empty($menu_link_hash['parent'])) {
$parent_link = $this->loadMenuLinkByTitle($menu_link_hash['parent'], $menu_name);
$menu_link_hash['parent'] = 'menu_link_content:' . $parent_link->uuid();
}
else {
unset($menu_link_hash['parent']);
}
$menu_link = MenuLinkContent::create($menu_link_hash);
$menu_link->save();
$this->menuLinks[] = $menu_link;
}
}
/**
* Remove all menu items after scenario run.
*
* @AfterScenario
*/
public function menuCleanAll(AfterScenarioScope $scope): void {
// Allow to skip this by adding a tag.
if ($scope->getScenario()->hasTag('behat-steps-skip:' . __FUNCTION__)) {
return;
}
foreach ($this->menuLinks as $menu_link) {
$menu_link->delete();
}
$this->menuLinks = [];
foreach ($this->menus as $menu) {
$menu->delete();
}
$this->menus = [];
}
/**
* Gets a menu by label.
*
* @param string $label
* The label of the menu.
*
* @return \Drupal\system\MenuInterface|null
* The menu or NULL if not found.
*/
protected function loadMenuByLabel(string $label): ?MenuInterface {
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
$entity_type_manager = \Drupal::getContainer()->get('entity_type.manager');
$menu_ids = $entity_type_manager->getStorage('menu')->getQuery()
->accessCheck(FALSE)
->condition('label', $label)
->execute();
if (empty($menu_ids)) {
return NULL;
}
$menu_id = reset($menu_ids);
return Menu::load($menu_id);
}
/**
* Gets a menu link by title and menu name.
*
* @param string $title
* The title of the menu link.
* @param string $menu_name
* The name of the menu.
*
* @return \Drupal\menu_link_content\Entity\MenuLinkContent|null
* The menu link or NULL if not found.
*/
protected function loadMenuLinkByTitle(string $title, string $menu_name): ?MenuLinkContent {
$menu = $this->loadMenuByLabel($menu_name);
if (!$menu instanceof MenuInterface) {
return NULL;
}
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
$entity_type_manager = \Drupal::getContainer()->get('entity_type.manager');
$menu_link_ids = $entity_type_manager->getStorage('menu_link_content')->getQuery()
->accessCheck(FALSE)
->condition('menu_name', $menu->id())
->condition('title', $title)
->execute();
if (empty($menu_link_ids)) {
return NULL;
}
$menu_link_id = reset($menu_link_ids);
return MenuLinkContent::load($menu_link_id);
}
}