-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMenuItemsCest.php
157 lines (139 loc) · 4.35 KB
/
MenuItemsCest.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
<?php
use Drupal\Core\Url;
use Faker\Factory;
/**
* Class MenuItemsCest.
*
* @group existingSite
*/
class MenuItemsCest {
/**
* Faker service.
*
* @var \Faker\Generator
*/
protected $faker;
/**
* Test generated links.
*
* @var \Drupal\menu_link_content\MenuLinkContentInterface[]
*/
protected $menuLinks = [];
/**
* Test constructor.
*/
public function __construct() {
$this->faker = Factory::create();
}
/**
* Cleanup menu links after the test.
*/
public function _after(AcceptanceTester $I) {
foreach ($this->menuLinks as $link) {
$link->delete();
}
}
/**
* Every main menu item should not error.
*/
public function testMenuItems(AcceptanceTester $I) {
$I->amOnPage('/');
$I->canSeeResponseCodeIsBetween(200, 403);
$I->setMaxRedirects(5);
foreach ($this->getLinksToCheck($I, '#header a') as $path) {
$I->amOnPage($path);
$I->canSeeResponseCodeIsBetween(200, 404);
}
}
/**
* Path auto settings should work correctly.
*
* @group pathauto
* @group install
*/
public function testPathAuto(AcceptanceTester $I) {
$manual_url = preg_replace('/[^a-z0-9\/]/', '', strtolower('/' . implode('/', $this->faker->words())));;
$I->logInWithRole('administrator');
$auto_alias = $I->createEntity([
'title' => $this->faker->words(2, TRUE),
'type' => 'hs_basic_page',
]);
$I->amOnPage($auto_alias->toUrl('edit-form')->toString());
$I->checkOption('Provide a menu link');
$I->fillField('Menu link title', $auto_alias->label());
$I->click('Save');
$I->canSee($auto_alias->label(), 'h1');
$manual_alias = $I->createEntity([
'title' => $this->faker->words(1, TRUE),
'type' => 'hs_basic_page',
]);
$I->amOnPage($manual_alias->toUrl('edit-form')->toString());
$I->checkOption('Provide a menu link');
$I->fillField('Menu link title', $manual_alias->label());
$I->uncheckOption('Generate automatic URL alias');
$I->fillField('URL alias', $manual_url);
$I->click('Save');
$I->canSee($manual_alias->label(), 'h1');
$I->canSeeInCurrentUrl($manual_url);
/** @var \Drupal\menu_link_content\Entity\MenuLinkContent $nolink */
$nolink = $I->createEntity([
'title' => $this->faker->word,
'link' => 'route:<nolink>',
'weight' => 0,
'menu_name' => 'main',
], 'menu_link_content');
$this->menuLinks[] = $nolink;
$node = $I->createEntity([
'title' => $this->faker->words(2, TRUE),
'type' => 'hs_basic_page',
]);
$I->amOnPage($node->toUrl('edit-form')->toString());
$I->checkOption('Provide a menu link');
$I->fillField('Menu link title', $node->label());
$I->selectOption('Parent item', 'main:menu_link_field:node_field_menulink_' . $auto_alias->uuid() . '_und');
$I->click('Change parent (update list of weights)');
$I->click('Save');
$I->canSee($node->label(), 'h1');
// Save twice to ensure updated alias.
$I->amOnPage($node->toUrl('edit-form')->toString());
$I->click('Save');
$I->canSee($node->label(), 'h1');
$I->canSeeInCurrentUrl($auto_alias->toUrl()->toString() . '/');
$I->amOnPage($node->toUrl('edit-form')->toString());
$I->selectOption('Parent item', 'main:menu_link_field:node_field_menulink_' . $manual_alias->uuid() . '_und');
$I->click('Change parent (update list of weights)');
$I->click('Save');
$I->canSeeInCurrentUrl($manual_url . '/');
$I->amOnPage($node->toUrl('edit-form')->toString());
$I->selectOption('Parent item', 'main:menu_link_content:' . $nolink->uuid());
$I->click('Change parent (update list of weights)');
$I->click('Save');
$I->canSeeInCurrentUrl('/' . $nolink->getTitle() . '/');
}
/**
* Get all relative url paths to test.
*
* @param \AcceptanceTester $I
* Tester.
* @param $selector
* Css selector.
*
* @return string[]
* Array of relative paths.
*/
protected function getLinksToCheck(AcceptanceTester $I, string $selector): array {
$link_urls = $I->grabMultiple($selector, 'href');
$link_urls = array_filter($link_urls, function ($url) {
if (preg_match('/(\/saml_login|\/user|^#)/', $url)) {
return FALSE;
}
try {
Url::fromUserInput($url);
return TRUE;
} catch (\Exception $e) {
return FALSE;
}
});
return $link_urls;
}
}