|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NuvoleWeb\Drupal\DrupalExtension\Context; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\menu_link_content\Entity\MenuLinkContent; |
|
6
|
|
|
use Drupal\system\Entity\Menu; |
|
7
|
|
|
use Behat\Gherkin\Node\TableNode; |
|
8
|
|
|
use Behat\Behat\Hook\Scope\AfterScenarioScope; |
|
9
|
|
|
use Behat\Mink\Exception\ExpectationException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class MenuContext. |
|
13
|
|
|
* |
|
14
|
|
|
* @package NuvoleWeb\Drupal\DrupalExtension\Context |
|
15
|
|
|
*/ |
|
16
|
|
|
class MenuContext extends RawDrupalContext { |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Menu links created during test execution. |
|
21
|
|
|
* |
|
22
|
|
|
* @var \Drupal\menu_link_content\Entity\MenuLinkContent[] |
|
23
|
|
|
*/ |
|
24
|
|
|
private $menuLinks = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Create menu structure for nodes. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $menu_name |
|
30
|
|
|
* Menu machine name. |
|
31
|
|
|
* @param \Behat\Gherkin\Node\TableNode $table |
|
32
|
|
|
* Table representing the menu structure to be specified as follows: |
|
33
|
|
|
* | title | parent | |
|
34
|
|
|
* | Page 1 | | |
|
35
|
|
|
* | Page 2 | Page 1 | |
|
36
|
|
|
* | Page 3 | Page 2 |. |
|
37
|
|
|
* |
|
38
|
|
|
* @throws \Behat\Mink\Exception\ExpectationException |
|
39
|
|
|
* Throws exception if menu not found. |
|
40
|
|
|
* |
|
41
|
|
|
* @Given the following :menu_name menu structure for content: |
|
42
|
|
|
*/ |
|
43
|
|
|
public function assertMenuStructureForContent($menu_name, TableNode $table) { |
|
44
|
|
|
$menu_items = $table->getColumnsHash(); |
|
45
|
|
|
foreach ($menu_items as $key => $menu_item) { |
|
46
|
|
|
$node = $this->getCore()->loadNodeByName($menu_item['title']); |
|
|
|
|
|
|
47
|
|
|
$menu_items[$key]['uri'] = "entity:node/{$this->getCore()->getNodeId($node)}"; |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
|
|
$this->menuLinks = array_merge($this->menuLinks, $this->getCore()->createMenuStructure($menu_name, $menu_items)); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
catch (\InvalidArgumentException $e) { |
|
54
|
|
|
throw new ExpectationException($e->getMessage(), $this->getSession()); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Create menu structure my adding menu links. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $menu_name |
|
62
|
|
|
* Menu machine name. |
|
63
|
|
|
* @param \Behat\Gherkin\Node\TableNode $table |
|
64
|
|
|
* Table representing the menu structure to be specified as follows: |
|
65
|
|
|
* | title | uri | parent | |
|
66
|
|
|
* | Link 1 | internal:/ | | |
|
67
|
|
|
* | Link 2 | internal:/ | Link 1 | |
|
68
|
|
|
* | Link 3 | internal:/ | Link 1 |. |
|
69
|
|
|
* |
|
70
|
|
|
* @throws \Behat\Mink\Exception\ExpectationException |
|
71
|
|
|
* Throws exception if menu not found. |
|
72
|
|
|
* |
|
73
|
|
|
* @Given the following :menu_name menu structure: |
|
74
|
|
|
*/ |
|
75
|
|
|
public function assertMenuStructure($menu_name, TableNode $table) { |
|
76
|
|
|
try { |
|
77
|
|
|
$this->menuLinks = array_merge($this->menuLinks, $this->getCore()->createMenuStructure($menu_name, $table->getColumnsHash())); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
catch (\InvalidArgumentException $e) { |
|
80
|
|
|
throw new ExpectationException($e->getMessage(), $this->getSession()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Assert clean Watchdog after every step. |
|
87
|
|
|
* |
|
88
|
|
|
* @param \Behat\Behat\Hook\Scope\AfterScenarioScope $event |
|
89
|
|
|
* Event object. |
|
90
|
|
|
* |
|
91
|
|
|
* @AfterScenario |
|
92
|
|
|
*/ |
|
93
|
|
|
public function deleteMenuLinks(AfterScenarioScope $event) { |
|
|
|
|
|
|
94
|
|
|
if ($this->menuLinks) { |
|
|
|
|
|
|
95
|
|
|
foreach ($this->menuLinks as $menu_link) { |
|
96
|
|
|
$this->getCore()->entityDelete($menu_link); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$this->getCore()->clearMenuCache(); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: