|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NuvoleWeb\Drupal\DrupalExtension\Context; |
|
4
|
|
|
|
|
5
|
|
|
use Behat\Mink\Exception\ExpectationException; |
|
6
|
|
|
use Behat\Gherkin\Node\PyStringNode; |
|
7
|
|
|
use function bovigo\assert\assert; |
|
8
|
|
|
use function bovigo\assert\predicate\hasKey; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class ContentContext. |
|
12
|
|
|
* |
|
13
|
|
|
* @package NuvoleWeb\Drupal\DrupalExtension\Context |
|
14
|
|
|
*/ |
|
15
|
|
|
class ContentContext extends RawDrupalContext { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Assert viewing content given its type and title. |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $type |
|
21
|
|
|
* Content type machine name. |
|
22
|
|
|
* @param string $title |
|
23
|
|
|
* Content title. |
|
24
|
|
|
* |
|
25
|
|
|
* @Given I am visiting the :type content :title |
|
26
|
|
|
* @Given I visit the :type content :title |
|
27
|
|
|
*/ |
|
28
|
|
|
public function iAmViewingTheContent($type, $title) { |
|
29
|
|
|
$this->visitContentPage('view', $type, $title); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Assert editing content given its type and title. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $type |
|
36
|
|
|
* Content type machine name. |
|
37
|
|
|
* @param string $title |
|
38
|
|
|
* Content title. |
|
39
|
|
|
* |
|
40
|
|
|
* @Given I am editing the :type content :title |
|
41
|
|
|
* @Given I edit the :type content :title |
|
42
|
|
|
*/ |
|
43
|
|
|
public function iAmEditingTheContent($type, $title) { |
|
44
|
|
|
$this->visitContentPage('edit', $type, $title); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Assert deleting content given its type and title. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $type |
|
51
|
|
|
* Content type machine name. |
|
52
|
|
|
* @param string $title |
|
53
|
|
|
* Content title. |
|
54
|
|
|
* |
|
55
|
|
|
* @Given I am deleting the :type content :title |
|
56
|
|
|
* @Given I delete the :type content :title |
|
57
|
|
|
*/ |
|
58
|
|
|
public function iAmDeletingTheContent($type, $title) { |
|
59
|
|
|
$this->visitContentPage('delete', $type, $title); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Provides a common step definition callback for node pages. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $op |
|
66
|
|
|
* The operation being performed: 'view', 'edit', 'delete'. |
|
67
|
|
|
* @param string $type |
|
68
|
|
|
* The node type either as id or as label. |
|
69
|
|
|
* @param string $title |
|
70
|
|
|
* The node title. |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function visitContentPage($op, $type, $title) { |
|
73
|
|
|
$nid = $this->getCore()->getEntityIdByLabel('node', $type, $title); |
|
|
|
|
|
|
74
|
|
|
$path = [ |
|
75
|
|
|
'view' => "node/$nid", |
|
76
|
|
|
'edit' => "node/$nid/edit", |
|
77
|
|
|
'delete' => "node/$nid/delete", |
|
78
|
|
|
]; |
|
79
|
|
|
$this->visitPath($path[$op]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Assert that given user can perform given operation on given content. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $name |
|
86
|
|
|
* User name. |
|
87
|
|
|
* @param string $op |
|
88
|
|
|
* Operation: view, edit or delete. |
|
89
|
|
|
* @param string $title |
|
90
|
|
|
* Content title. |
|
91
|
|
|
* |
|
92
|
|
|
* @throws \Exception |
|
93
|
|
|
* If user cannot perform given operation on given content. |
|
94
|
|
|
* |
|
95
|
|
|
* @Then :name can :op content :content |
|
96
|
|
|
* @Then :name can :op :content content |
|
97
|
|
|
*/ |
|
98
|
|
View Code Duplication |
public function userCanContent($name, $op, $title) { |
|
|
|
|
|
|
99
|
|
|
$op = strtr($op, ['edit' => 'update']); |
|
100
|
|
|
$node = $this->getCore()->loadNodeByName($title); |
|
|
|
|
|
|
101
|
|
|
$access = $this->getCore()->nodeAccess($op, $name, $node); |
|
|
|
|
|
|
102
|
|
|
if (!$access) { |
|
103
|
|
|
throw new \Exception("{$name} cannot {$op} '{$title}' but it is supposed to."); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Assert that given user cannot perform given operation on given content. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $name |
|
111
|
|
|
* User name. |
|
112
|
|
|
* @param string $op |
|
113
|
|
|
* Operation: view, edit or delete. |
|
114
|
|
|
* @param string $title |
|
115
|
|
|
* Content title. |
|
116
|
|
|
* |
|
117
|
|
|
* @throws \Exception |
|
118
|
|
|
* If user can perform given operation on given content. |
|
119
|
|
|
* |
|
120
|
|
|
* @Then :name can not :op content :content |
|
121
|
|
|
* @Then :name cannot :op content :content |
|
122
|
|
|
* @Then :name cannot :op :content content |
|
123
|
|
|
*/ |
|
124
|
|
View Code Duplication |
public function userCanNotContent($name, $op, $title) { |
|
|
|
|
|
|
125
|
|
|
$op = strtr($op, ['edit' => 'update']); |
|
126
|
|
|
$node = $this->getCore()->loadNodeByName($title); |
|
|
|
|
|
|
127
|
|
|
$access = $this->getCore()->nodeAccess($op, $name, $node); |
|
|
|
|
|
|
128
|
|
|
if ($access) { |
|
129
|
|
|
throw new \Exception("{$name} can {$op} '{$title}' but it is not supposed to."); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Assert presence of content edit link given its name and content title. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $link |
|
137
|
|
|
* Link "name" HTML attribute. |
|
138
|
|
|
* @param string $title |
|
139
|
|
|
* Content title. |
|
140
|
|
|
* |
|
141
|
|
|
* @throws \Behat\Mink\Exception\ExpectationException |
|
142
|
|
|
* If no edit link for given content has been found. |
|
143
|
|
|
* |
|
144
|
|
|
* @Then I should see the link :link to edit content :content |
|
145
|
|
|
* @Then I should see a link :link to edit content :content |
|
146
|
|
|
*/ |
|
147
|
|
|
public function assertContentEditLink($link, $title) { |
|
148
|
|
|
if (!$this->getContentEditLink($link, $title)) { |
|
149
|
|
|
throw new ExpectationException("No '$link' link to edit '$title' has been found.", $this->getSession()); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Assert absence of content edit link given its content title. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $title |
|
157
|
|
|
* Content title. |
|
158
|
|
|
* |
|
159
|
|
|
* @throws \Behat\Mink\Exception\ExpectationException |
|
160
|
|
|
* If edit link for given content has been found. |
|
161
|
|
|
* |
|
162
|
|
|
* @Then I should not see a link to edit content :content |
|
163
|
|
|
* @Then I should not see the link to edit content :content |
|
164
|
|
|
*/ |
|
165
|
|
|
public function assertNoContentEditLink($title) { |
|
166
|
|
|
if ($this->getContentEditLink(NULL, $title)) { |
|
167
|
|
|
throw new ExpectationException("link to edit '$title' has been found.", $this->getSession()); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Create content defined in YAML format. |
|
173
|
|
|
* |
|
174
|
|
|
* @param PyStringNode $string |
|
175
|
|
|
* The text in yaml format that represents the content. |
|
176
|
|
|
* |
|
177
|
|
|
* @Given the following content: |
|
178
|
|
|
*/ |
|
179
|
|
|
public function assertContent(PyStringNode $string) { |
|
180
|
|
|
$values = $this->getYamlParser()->parse($string); |
|
181
|
|
|
assert($values, hasKey('type') |
|
182
|
|
|
->and(hasKey('title')) |
|
183
|
|
|
->and(hasKey('langcode')), |
|
184
|
|
|
__METHOD__ . ": Required fields 'type', 'title' and 'langcode' not found." |
|
185
|
|
|
); |
|
186
|
|
|
$node = $this->getCore()->entityCreate('node', $values); |
|
187
|
|
|
$this->nodes[] = $node; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Assert translation for given content. |
|
192
|
|
|
* |
|
193
|
|
|
* @param string $content_type |
|
194
|
|
|
* The node type for which to add the translation. |
|
195
|
|
|
* @param string $title |
|
196
|
|
|
* The title to identify the content by. |
|
197
|
|
|
* @param PyStringNode $string |
|
198
|
|
|
* The text in yaml format that represents the translation. |
|
199
|
|
|
* |
|
200
|
|
|
* @Given the following translation for :content_type content :title: |
|
201
|
|
|
*/ |
|
202
|
|
|
public function assertTranslation($content_type, $title, PyStringNode $string) { |
|
203
|
|
|
$values = $this->getYamlParser()->parse($string); |
|
204
|
|
|
assert($values, hasKey('langcode'), __METHOD__ . ": Required field 'langcode' not found."); |
|
205
|
|
|
|
|
206
|
|
|
$nid = $this->getCore()->getEntityIdByLabel('node', $content_type, $title); |
|
|
|
|
|
|
207
|
|
|
$source = $this->getCore()->entityLoad('node', $nid); |
|
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
$this->getCore()->entityAddTranslation($source, $values['langcode'], $values); |
|
|
|
|
|
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Get the edit link for a node. |
|
214
|
|
|
* |
|
215
|
|
|
* @param string $link |
|
216
|
|
|
* The link name. |
|
217
|
|
|
* @param string $title |
|
218
|
|
|
* The node title. |
|
219
|
|
|
* |
|
220
|
|
|
* @return \Behat\Mink\Element\NodeElement|null |
|
221
|
|
|
* The link if found. |
|
222
|
|
|
* |
|
223
|
|
|
* @throws \Exception |
|
224
|
|
|
*/ |
|
225
|
|
|
protected function getContentEditLink($link, $title) { |
|
226
|
|
|
$node = $this->getCore()->loadNodeByName($title); |
|
|
|
|
|
|
227
|
|
|
|
|
228
|
|
|
/** @var \Behat\Mink\Element\DocumentElement $element */ |
|
229
|
|
|
$element = $this->getSession()->getPage(); |
|
230
|
|
|
|
|
231
|
|
|
$locator = ($link ? array('link', sprintf("'%s'", $link)) : array('link', ".")); |
|
232
|
|
|
|
|
233
|
|
|
/** @var \Behat\Mink\Element\NodeElement[] $links */ |
|
234
|
|
|
$links = $element->findAll('named', $locator); |
|
235
|
|
|
|
|
236
|
|
|
// Loop over all the links on the page and check for the node edit path. |
|
237
|
|
|
foreach ($links as $result) { |
|
238
|
|
|
$target = $result->getAttribute('href'); |
|
239
|
|
|
if (strpos($target, 'node/' . $this->getCore()->getNodeId($node) . '/edit') !== FALSE) { |
|
|
|
|
|
|
240
|
|
|
return $result; |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
return NULL; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Get the yaml parser from the behat container. |
|
248
|
|
|
* |
|
249
|
|
|
* @return \NuvoleWeb\Drupal\DrupalExtension\Component\PyStringYamlParser |
|
250
|
|
|
* The parser. |
|
251
|
|
|
*/ |
|
252
|
|
|
protected function getYamlParser() { |
|
253
|
|
|
return $this->getContainer()->get('drupal.behat.component.py_string_yaml_parser'); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
} |
|
257
|
|
|
|
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: