Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests;
|
Chris@0
|
4
|
Chris@0
|
5 use Behat\Mink\Exception\ExpectationException;
|
Chris@0
|
6 use Behat\Mink\WebAssert as MinkWebAssert;
|
Chris@0
|
7 use Behat\Mink\Element\TraversableElement;
|
Chris@0
|
8 use Behat\Mink\Exception\ElementNotFoundException;
|
Chris@0
|
9 use Behat\Mink\Session;
|
Chris@0
|
10 use Drupal\Component\Utility\Html;
|
Chris@0
|
11 use Drupal\Core\Url;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Defines a class with methods for asserting presence of elements during tests.
|
Chris@0
|
15 */
|
Chris@0
|
16 class WebAssert extends MinkWebAssert {
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * The absolute URL of the site under test.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @var string
|
Chris@0
|
22 */
|
Chris@0
|
23 protected $baseUrl = '';
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * Constructor.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @param \Behat\Mink\Session $session
|
Chris@0
|
29 * The Behat session object;
|
Chris@0
|
30 * @param string $base_url
|
Chris@0
|
31 * The base URL of the site under test.
|
Chris@0
|
32 */
|
Chris@0
|
33 public function __construct(Session $session, $base_url = '') {
|
Chris@0
|
34 parent::__construct($session);
|
Chris@0
|
35 $this->baseUrl = $base_url;
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * {@inheritdoc}
|
Chris@0
|
40 */
|
Chris@0
|
41 protected function cleanUrl($url) {
|
Chris@0
|
42 if ($url instanceof Url) {
|
Chris@0
|
43 $url = $url->setAbsolute()->toString();
|
Chris@0
|
44 }
|
Chris@0
|
45 // Strip the base URL from the beginning for absolute URLs.
|
Chris@0
|
46 if ($this->baseUrl !== '' && strpos($url, $this->baseUrl) === 0) {
|
Chris@0
|
47 $url = substr($url, strlen($this->baseUrl));
|
Chris@0
|
48 }
|
Chris@0
|
49 // Make sure there is a forward slash at the beginning of relative URLs for
|
Chris@0
|
50 // consistency.
|
Chris@0
|
51 if (parse_url($url, PHP_URL_HOST) === NULL && strpos($url, '/') !== 0) {
|
Chris@0
|
52 $url = "/$url";
|
Chris@0
|
53 }
|
Chris@0
|
54 return parent::cleanUrl($url);
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * Checks that specific button exists on the current page.
|
Chris@0
|
59 *
|
Chris@0
|
60 * @param string $button
|
Chris@0
|
61 * One of id|name|label|value for the button.
|
Chris@0
|
62 * @param \Behat\Mink\Element\TraversableElement $container
|
Chris@0
|
63 * (optional) The document to check against. Defaults to the current page.
|
Chris@0
|
64 *
|
Chris@0
|
65 * @return \Behat\Mink\Element\NodeElement
|
Chris@0
|
66 * The matching element.
|
Chris@0
|
67 *
|
Chris@0
|
68 * @throws \Behat\Mink\Exception\ElementNotFoundException
|
Chris@0
|
69 * When the element doesn't exist.
|
Chris@0
|
70 */
|
Chris@0
|
71 public function buttonExists($button, TraversableElement $container = NULL) {
|
Chris@0
|
72 $container = $container ?: $this->session->getPage();
|
Chris@0
|
73 $node = $container->findButton($button);
|
Chris@0
|
74
|
Chris@0
|
75 if ($node === NULL) {
|
Chris@0
|
76 throw new ElementNotFoundException($this->session, 'button', 'id|name|label|value', $button);
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 return $node;
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * Checks that the specific button does NOT exist on the current page.
|
Chris@0
|
84 *
|
Chris@0
|
85 * @param string $button
|
Chris@0
|
86 * One of id|name|label|value for the button.
|
Chris@0
|
87 * @param \Behat\Mink\Element\TraversableElement $container
|
Chris@0
|
88 * (optional) The document to check against. Defaults to the current page.
|
Chris@0
|
89 *
|
Chris@0
|
90 * @throws \Behat\Mink\Exception\ExpectationException
|
Chris@0
|
91 * When the button exists.
|
Chris@0
|
92 */
|
Chris@0
|
93 public function buttonNotExists($button, TraversableElement $container = NULL) {
|
Chris@0
|
94 $container = $container ?: $this->session->getPage();
|
Chris@0
|
95 $node = $container->findButton($button);
|
Chris@0
|
96
|
Chris@0
|
97 $this->assert(NULL === $node, sprintf('A button "%s" appears on this page, but it should not.', $button));
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 /**
|
Chris@0
|
101 * Checks that specific select field exists on the current page.
|
Chris@0
|
102 *
|
Chris@0
|
103 * @param string $select
|
Chris@0
|
104 * One of id|name|label|value for the select field.
|
Chris@0
|
105 * @param \Behat\Mink\Element\TraversableElement $container
|
Chris@0
|
106 * (optional) The document to check against. Defaults to the current page.
|
Chris@0
|
107 *
|
Chris@0
|
108 * @return \Behat\Mink\Element\NodeElement
|
Chris@0
|
109 * The matching element
|
Chris@0
|
110 *
|
Chris@0
|
111 * @throws \Behat\Mink\Exception\ElementNotFoundException
|
Chris@0
|
112 * When the element doesn't exist.
|
Chris@0
|
113 */
|
Chris@0
|
114 public function selectExists($select, TraversableElement $container = NULL) {
|
Chris@0
|
115 $container = $container ?: $this->session->getPage();
|
Chris@0
|
116 $node = $container->find('named', [
|
Chris@0
|
117 'select',
|
Chris@0
|
118 $this->session->getSelectorsHandler()->xpathLiteral($select),
|
Chris@0
|
119 ]);
|
Chris@0
|
120
|
Chris@0
|
121 if ($node === NULL) {
|
Chris@0
|
122 throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select);
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 return $node;
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 /**
|
Chris@0
|
129 * Checks that specific option in a select field exists on the current page.
|
Chris@0
|
130 *
|
Chris@0
|
131 * @param string $select
|
Chris@0
|
132 * One of id|name|label|value for the select field.
|
Chris@0
|
133 * @param string $option
|
Chris@0
|
134 * The option value.
|
Chris@0
|
135 * @param \Behat\Mink\Element\TraversableElement $container
|
Chris@0
|
136 * (optional) The document to check against. Defaults to the current page.
|
Chris@0
|
137 *
|
Chris@0
|
138 * @return \Behat\Mink\Element\NodeElement
|
Chris@0
|
139 * The matching option element
|
Chris@0
|
140 *
|
Chris@0
|
141 * @throws \Behat\Mink\Exception\ElementNotFoundException
|
Chris@0
|
142 * When the element doesn't exist.
|
Chris@0
|
143 */
|
Chris@0
|
144 public function optionExists($select, $option, TraversableElement $container = NULL) {
|
Chris@0
|
145 $container = $container ?: $this->session->getPage();
|
Chris@0
|
146 $select_field = $container->find('named', [
|
Chris@0
|
147 'select',
|
Chris@0
|
148 $this->session->getSelectorsHandler()->xpathLiteral($select),
|
Chris@0
|
149 ]);
|
Chris@0
|
150
|
Chris@0
|
151 if ($select_field === NULL) {
|
Chris@0
|
152 throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select);
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 $option_field = $select_field->find('named', ['option', $option]);
|
Chris@0
|
156
|
Chris@0
|
157 if ($option_field === NULL) {
|
Chris@0
|
158 throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $option);
|
Chris@0
|
159 }
|
Chris@0
|
160
|
Chris@0
|
161 return $option_field;
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 /**
|
Chris@0
|
165 * Checks that an option in a select field does NOT exist on the current page.
|
Chris@0
|
166 *
|
Chris@0
|
167 * @param string $select
|
Chris@0
|
168 * One of id|name|label|value for the select field.
|
Chris@0
|
169 * @param string $option
|
Chris@0
|
170 * The option value that shoulkd not exist.
|
Chris@0
|
171 * @param \Behat\Mink\Element\TraversableElement $container
|
Chris@0
|
172 * (optional) The document to check against. Defaults to the current page.
|
Chris@0
|
173 *
|
Chris@0
|
174 * @throws \Behat\Mink\Exception\ElementNotFoundException
|
Chris@0
|
175 * When the select element doesn't exist.
|
Chris@0
|
176 */
|
Chris@0
|
177 public function optionNotExists($select, $option, TraversableElement $container = NULL) {
|
Chris@0
|
178 $container = $container ?: $this->session->getPage();
|
Chris@0
|
179 $select_field = $container->find('named', [
|
Chris@0
|
180 'select',
|
Chris@0
|
181 $this->session->getSelectorsHandler()->xpathLiteral($select),
|
Chris@0
|
182 ]);
|
Chris@0
|
183
|
Chris@0
|
184 if ($select_field === NULL) {
|
Chris@0
|
185 throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select);
|
Chris@0
|
186 }
|
Chris@0
|
187
|
Chris@0
|
188 $option_field = $select_field->find('named', ['option', $option]);
|
Chris@0
|
189
|
Chris@0
|
190 $this->assert($option_field === NULL, sprintf('An option "%s" exists in select "%s", but it should not.', $option, $select));
|
Chris@0
|
191 }
|
Chris@0
|
192
|
Chris@0
|
193 /**
|
Chris@0
|
194 * Pass if the page title is the given string.
|
Chris@0
|
195 *
|
Chris@0
|
196 * @param string $expected_title
|
Chris@0
|
197 * The string the page title should be.
|
Chris@0
|
198 *
|
Chris@0
|
199 * @throws \Behat\Mink\Exception\ExpectationException
|
Chris@0
|
200 * Thrown when element doesn't exist, or the title is a different one.
|
Chris@0
|
201 */
|
Chris@0
|
202 public function titleEquals($expected_title) {
|
Chris@0
|
203 $title_element = $this->session->getPage()->find('css', 'title');
|
Chris@0
|
204 if (!$title_element) {
|
Chris@0
|
205 throw new ExpectationException('No title element found on the page', $this->session->getDriver());
|
Chris@0
|
206 }
|
Chris@0
|
207 $actual_title = $title_element->getText();
|
Chris@0
|
208 $this->assert($expected_title === $actual_title, 'Title found');
|
Chris@0
|
209 }
|
Chris@0
|
210
|
Chris@0
|
211 /**
|
Chris@0
|
212 * Passes if a link with the specified label is found.
|
Chris@0
|
213 *
|
Chris@0
|
214 * An optional link index may be passed.
|
Chris@0
|
215 *
|
Chris@0
|
216 * @param string $label
|
Chris@0
|
217 * Text between the anchor tags.
|
Chris@0
|
218 * @param int $index
|
Chris@0
|
219 * Link position counting from zero.
|
Chris@0
|
220 * @param string $message
|
Chris@0
|
221 * (optional) A message to display with the assertion. Do not translate
|
Chris@0
|
222 * messages: use strtr() to embed variables in the message text, not
|
Chris@0
|
223 * t(). If left blank, a default message will be displayed.
|
Chris@0
|
224 *
|
Chris@0
|
225 * @throws \Behat\Mink\Exception\ExpectationException
|
Chris@0
|
226 * Thrown when element doesn't exist, or the link label is a different one.
|
Chris@0
|
227 */
|
Chris@0
|
228 public function linkExists($label, $index = 0, $message = '') {
|
Chris@0
|
229 $message = ($message ? $message : strtr('Link with label %label found.', ['%label' => $label]));
|
Chris@0
|
230 $links = $this->session->getPage()->findAll('named', ['link', $label]);
|
Chris@0
|
231 $this->assert(!empty($links[$index]), $message);
|
Chris@0
|
232 }
|
Chris@0
|
233
|
Chris@0
|
234 /**
|
Chris@0
|
235 * Passes if a link with the exactly specified label is found.
|
Chris@0
|
236 *
|
Chris@0
|
237 * An optional link index may be passed.
|
Chris@0
|
238 *
|
Chris@0
|
239 * @param string $label
|
Chris@0
|
240 * Text between the anchor tags.
|
Chris@0
|
241 * @param int $index
|
Chris@0
|
242 * Link position counting from zero.
|
Chris@0
|
243 * @param string $message
|
Chris@0
|
244 * (optional) A message to display with the assertion. Do not translate
|
Chris@0
|
245 * messages: use strtr() to embed variables in the message text, not
|
Chris@0
|
246 * t(). If left blank, a default message will be displayed.
|
Chris@0
|
247 *
|
Chris@0
|
248 * @throws \Behat\Mink\Exception\ExpectationException
|
Chris@0
|
249 * Thrown when element doesn't exist, or the link label is a different one.
|
Chris@0
|
250 */
|
Chris@0
|
251 public function linkExistsExact($label, $index = 0, $message = '') {
|
Chris@0
|
252 $message = ($message ? $message : strtr('Link with label %label found.', ['%label' => $label]));
|
Chris@0
|
253 $links = $this->session->getPage()->findAll('named_exact', ['link', $label]);
|
Chris@0
|
254 $this->assert(!empty($links[$index]), $message);
|
Chris@0
|
255 }
|
Chris@0
|
256
|
Chris@0
|
257 /**
|
Chris@0
|
258 * Passes if a link with the specified label is not found.
|
Chris@0
|
259 *
|
Chris@0
|
260 * An optional link index may be passed.
|
Chris@0
|
261 *
|
Chris@0
|
262 * @param string $label
|
Chris@0
|
263 * Text between the anchor tags.
|
Chris@0
|
264 * @param string $message
|
Chris@0
|
265 * (optional) A message to display with the assertion. Do not translate
|
Chris@0
|
266 * messages: use strtr() to embed variables in the message text, not
|
Chris@0
|
267 * t(). If left blank, a default message will be displayed.
|
Chris@0
|
268 *
|
Chris@0
|
269 * @throws \Behat\Mink\Exception\ExpectationException
|
Chris@0
|
270 * Thrown when element doesn't exist, or the link label is a different one.
|
Chris@0
|
271 */
|
Chris@0
|
272 public function linkNotExists($label, $message = '') {
|
Chris@0
|
273 $message = ($message ? $message : strtr('Link with label %label not found.', ['%label' => $label]));
|
Chris@0
|
274 $links = $this->session->getPage()->findAll('named', ['link', $label]);
|
Chris@0
|
275 $this->assert(empty($links), $message);
|
Chris@0
|
276 }
|
Chris@0
|
277
|
Chris@0
|
278 /**
|
Chris@0
|
279 * Passes if a link with the exactly specified label is not found.
|
Chris@0
|
280 *
|
Chris@0
|
281 * An optional link index may be passed.
|
Chris@0
|
282 *
|
Chris@0
|
283 * @param string $label
|
Chris@0
|
284 * Text between the anchor tags.
|
Chris@0
|
285 * @param string $message
|
Chris@0
|
286 * (optional) A message to display with the assertion. Do not translate
|
Chris@0
|
287 * messages: use strtr() to embed variables in the message text, not
|
Chris@0
|
288 * t(). If left blank, a default message will be displayed.
|
Chris@0
|
289 *
|
Chris@0
|
290 * @throws \Behat\Mink\Exception\ExpectationException
|
Chris@0
|
291 * Thrown when element doesn't exist, or the link label is a different one.
|
Chris@0
|
292 */
|
Chris@0
|
293 public function linkNotExistsExact($label, $message = '') {
|
Chris@0
|
294 $message = ($message ? $message : strtr('Link with label %label not found.', ['%label' => $label]));
|
Chris@0
|
295 $links = $this->session->getPage()->findAll('named_exact', ['link', $label]);
|
Chris@0
|
296 $this->assert(empty($links), $message);
|
Chris@0
|
297 }
|
Chris@0
|
298
|
Chris@0
|
299 /**
|
Chris@0
|
300 * Passes if a link containing a given href (part) is found.
|
Chris@0
|
301 *
|
Chris@0
|
302 * @param string $href
|
Chris@0
|
303 * The full or partial value of the 'href' attribute of the anchor tag.
|
Chris@0
|
304 * @param int $index
|
Chris@0
|
305 * Link position counting from zero.
|
Chris@0
|
306 * @param string $message
|
Chris@0
|
307 * (optional) A message to display with the assertion. Do not translate
|
Chris@0
|
308 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
|
Chris@0
|
309 * variables in the message text, not t(). If left blank, a default message
|
Chris@0
|
310 * will be displayed.
|
Chris@0
|
311 *
|
Chris@0
|
312 * @throws \Behat\Mink\Exception\ExpectationException
|
Chris@0
|
313 * Thrown when element doesn't exist, or the link label is a different one.
|
Chris@0
|
314 */
|
Chris@0
|
315 public function linkByHrefExists($href, $index = 0, $message = '') {
|
Chris@0
|
316 $xpath = $this->buildXPathQuery('//a[contains(@href, :href)]', [':href' => $href]);
|
Chris@0
|
317 $message = ($message ? $message : strtr('Link containing href %href found.', ['%href' => $href]));
|
Chris@0
|
318 $links = $this->session->getPage()->findAll('xpath', $xpath);
|
Chris@0
|
319 $this->assert(!empty($links[$index]), $message);
|
Chris@0
|
320 }
|
Chris@0
|
321
|
Chris@0
|
322 /**
|
Chris@0
|
323 * Passes if a link containing a given href (part) is not found.
|
Chris@0
|
324 *
|
Chris@0
|
325 * @param string $href
|
Chris@0
|
326 * The full or partial value of the 'href' attribute of the anchor tag.
|
Chris@0
|
327 * @param string $message
|
Chris@0
|
328 * (optional) A message to display with the assertion. Do not translate
|
Chris@0
|
329 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
|
Chris@0
|
330 * variables in the message text, not t(). If left blank, a default message
|
Chris@0
|
331 * will be displayed.
|
Chris@0
|
332 *
|
Chris@0
|
333 * @throws \Behat\Mink\Exception\ExpectationException
|
Chris@0
|
334 * Thrown when element doesn't exist, or the link label is a different one.
|
Chris@0
|
335 */
|
Chris@0
|
336 public function linkByHrefNotExists($href, $message = '') {
|
Chris@0
|
337 $xpath = $this->buildXPathQuery('//a[contains(@href, :href)]', [':href' => $href]);
|
Chris@0
|
338 $message = ($message ? $message : strtr('No link containing href %href found.', ['%href' => $href]));
|
Chris@0
|
339 $links = $this->session->getPage()->findAll('xpath', $xpath);
|
Chris@0
|
340 $this->assert(empty($links), $message);
|
Chris@0
|
341 }
|
Chris@0
|
342
|
Chris@0
|
343 /**
|
Chris@0
|
344 * Builds an XPath query.
|
Chris@0
|
345 *
|
Chris@0
|
346 * Builds an XPath query by replacing placeholders in the query by the value
|
Chris@0
|
347 * of the arguments.
|
Chris@0
|
348 *
|
Chris@0
|
349 * XPath 1.0 (the version supported by libxml2, the underlying XML library
|
Chris@0
|
350 * used by PHP) doesn't support any form of quotation. This function
|
Chris@0
|
351 * simplifies the building of XPath expression.
|
Chris@0
|
352 *
|
Chris@0
|
353 * @param string $xpath
|
Chris@0
|
354 * An XPath query, possibly with placeholders in the form ':name'.
|
Chris@0
|
355 * @param array $args
|
Chris@0
|
356 * An array of arguments with keys in the form ':name' matching the
|
Chris@0
|
357 * placeholders in the query. The values may be either strings or numeric
|
Chris@0
|
358 * values.
|
Chris@0
|
359 *
|
Chris@0
|
360 * @return string
|
Chris@0
|
361 * An XPath query with arguments replaced.
|
Chris@0
|
362 */
|
Chris@0
|
363 public function buildXPathQuery($xpath, array $args = []) {
|
Chris@0
|
364 // Replace placeholders.
|
Chris@0
|
365 foreach ($args as $placeholder => $value) {
|
Chris@0
|
366 if (is_object($value)) {
|
Chris@0
|
367 throw new \InvalidArgumentException('Just pass in scalar values for $args and remove all t() calls from your test.');
|
Chris@0
|
368 }
|
Chris@0
|
369 // XPath 1.0 doesn't support a way to escape single or double quotes in a
|
Chris@0
|
370 // string literal. We split double quotes out of the string, and encode
|
Chris@0
|
371 // them separately.
|
Chris@0
|
372 if (is_string($value)) {
|
Chris@0
|
373 // Explode the text at the quote characters.
|
Chris@0
|
374 $parts = explode('"', $value);
|
Chris@0
|
375
|
Chris@0
|
376 // Quote the parts.
|
Chris@0
|
377 foreach ($parts as &$part) {
|
Chris@0
|
378 $part = '"' . $part . '"';
|
Chris@0
|
379 }
|
Chris@0
|
380
|
Chris@0
|
381 // Return the string.
|
Chris@0
|
382 $value = count($parts) > 1 ? 'concat(' . implode(', \'"\', ', $parts) . ')' : $parts[0];
|
Chris@0
|
383 }
|
Chris@0
|
384
|
Chris@0
|
385 // Use preg_replace_callback() instead of preg_replace() to prevent the
|
Chris@0
|
386 // regular expression engine from trying to substitute backreferences.
|
Chris@0
|
387 $replacement = function ($matches) use ($value) {
|
Chris@0
|
388 return $value;
|
Chris@0
|
389 };
|
Chris@0
|
390 $xpath = preg_replace_callback('/' . preg_quote($placeholder) . '\b/', $replacement, $xpath);
|
Chris@0
|
391 }
|
Chris@0
|
392 return $xpath;
|
Chris@0
|
393 }
|
Chris@0
|
394
|
Chris@0
|
395 /**
|
Chris@0
|
396 * Passes if the raw text IS NOT found escaped on the loaded page.
|
Chris@0
|
397 *
|
Chris@0
|
398 * Raw text refers to the raw HTML that the page generated.
|
Chris@0
|
399 *
|
Chris@0
|
400 * @param string $raw
|
Chris@0
|
401 * Raw (HTML) string to look for.
|
Chris@0
|
402 */
|
Chris@0
|
403 public function assertNoEscaped($raw) {
|
Chris@0
|
404 $this->responseNotContains(Html::escape($raw));
|
Chris@0
|
405 }
|
Chris@0
|
406
|
Chris@0
|
407 /**
|
Chris@0
|
408 * Passes if the raw text IS found escaped on the loaded page.
|
Chris@0
|
409 *
|
Chris@0
|
410 * Raw text refers to the raw HTML that the page generated.
|
Chris@0
|
411 *
|
Chris@0
|
412 * @param string $raw
|
Chris@0
|
413 * Raw (HTML) string to look for.
|
Chris@0
|
414 */
|
Chris@0
|
415 public function assertEscaped($raw) {
|
Chris@0
|
416 $this->responseContains(Html::escape($raw));
|
Chris@0
|
417 }
|
Chris@0
|
418
|
Chris@0
|
419 /**
|
Chris@0
|
420 * Asserts a condition.
|
Chris@0
|
421 *
|
Chris@0
|
422 * The parent method is overridden because it is a private method.
|
Chris@0
|
423 *
|
Chris@0
|
424 * @param bool $condition
|
Chris@0
|
425 * The condition.
|
Chris@0
|
426 * @param string $message
|
Chris@0
|
427 * The success message.
|
Chris@0
|
428 *
|
Chris@0
|
429 * @throws \Behat\Mink\Exception\ExpectationException
|
Chris@0
|
430 * When the condition is not fulfilled.
|
Chris@0
|
431 */
|
Chris@0
|
432 public function assert($condition, $message) {
|
Chris@0
|
433 if ($condition) {
|
Chris@0
|
434 return;
|
Chris@0
|
435 }
|
Chris@0
|
436
|
Chris@0
|
437 throw new ExpectationException($message, $this->session->getDriver());
|
Chris@0
|
438 }
|
Chris@0
|
439
|
Chris@0
|
440 /**
|
Chris@0
|
441 * Checks that a given form field element is disabled.
|
Chris@0
|
442 *
|
Chris@0
|
443 * @param string $field
|
Chris@0
|
444 * One of id|name|label|value for the field.
|
Chris@0
|
445 * @param \Behat\Mink\Element\TraversableElement $container
|
Chris@0
|
446 * (optional) The document to check against. Defaults to the current page.
|
Chris@0
|
447 *
|
Chris@0
|
448 * @return \Behat\Mink\Element\NodeElement
|
Chris@0
|
449 * The matching element.
|
Chris@0
|
450 *
|
Chris@0
|
451 * @throws \Behat\Mink\Exception\ElementNotFoundException
|
Chris@0
|
452 * @throws \Behat\Mink\Exception\ExpectationException
|
Chris@0
|
453 */
|
Chris@0
|
454 public function fieldDisabled($field, TraversableElement $container = NULL) {
|
Chris@0
|
455 $container = $container ?: $this->session->getPage();
|
Chris@0
|
456 $node = $container->findField($field);
|
Chris@0
|
457
|
Chris@0
|
458 if ($node === NULL) {
|
Chris@0
|
459 throw new ElementNotFoundException($this->session->getDriver(), 'field', 'id|name|label|value', $field);
|
Chris@0
|
460 }
|
Chris@0
|
461
|
Chris@0
|
462 if (!$node->hasAttribute('disabled')) {
|
Chris@0
|
463 throw new ExpectationException("Field $field is disabled", $this->session->getDriver());
|
Chris@0
|
464 }
|
Chris@0
|
465
|
Chris@0
|
466 return $node;
|
Chris@0
|
467 }
|
Chris@0
|
468
|
Chris@0
|
469 /**
|
Chris@0
|
470 * Checks that specific hidden field exists.
|
Chris@0
|
471 *
|
Chris@0
|
472 * @param string $field
|
Chris@0
|
473 * One of id|name|value for the hidden field.
|
Chris@0
|
474 * @param \Behat\Mink\Element\TraversableElement $container
|
Chris@0
|
475 * (optional) The document to check against. Defaults to the current page.
|
Chris@0
|
476 *
|
Chris@0
|
477 * @return \Behat\Mink\Element\NodeElement
|
Chris@0
|
478 * The matching element.
|
Chris@0
|
479 *
|
Chris@0
|
480 * @throws \Behat\Mink\Exception\ElementNotFoundException
|
Chris@0
|
481 */
|
Chris@0
|
482 public function hiddenFieldExists($field, TraversableElement $container = NULL) {
|
Chris@0
|
483 $container = $container ?: $this->session->getPage();
|
Chris@0
|
484 if ($node = $container->find('hidden_field_selector', ['hidden_field', $field])) {
|
Chris@0
|
485 return $node;
|
Chris@0
|
486 }
|
Chris@0
|
487 throw new ElementNotFoundException($this->session->getDriver(), 'form hidden field', 'id|name|value', $field);
|
Chris@0
|
488 }
|
Chris@0
|
489
|
Chris@0
|
490 /**
|
Chris@0
|
491 * Checks that specific hidden field does not exists.
|
Chris@0
|
492 *
|
Chris@0
|
493 * @param string $field
|
Chris@0
|
494 * One of id|name|value for the hidden field.
|
Chris@0
|
495 * @param \Behat\Mink\Element\TraversableElement $container
|
Chris@0
|
496 * (optional) The document to check against. Defaults to the current page.
|
Chris@0
|
497 *
|
Chris@0
|
498 * @throws \Behat\Mink\Exception\ExpectationException
|
Chris@0
|
499 */
|
Chris@0
|
500 public function hiddenFieldNotExists($field, TraversableElement $container = NULL) {
|
Chris@0
|
501 $container = $container ?: $this->session->getPage();
|
Chris@0
|
502 $node = $container->find('hidden_field_selector', ['hidden_field', $field]);
|
Chris@0
|
503 $this->assert($node === NULL, "A hidden field '$field' exists on this page, but it should not.");
|
Chris@0
|
504 }
|
Chris@0
|
505
|
Chris@0
|
506 /**
|
Chris@0
|
507 * Checks that specific hidden field have provided value.
|
Chris@0
|
508 *
|
Chris@0
|
509 * @param string $field
|
Chris@0
|
510 * One of id|name|value for the hidden field.
|
Chris@0
|
511 * @param string $value
|
Chris@0
|
512 * The hidden field value that needs to be checked.
|
Chris@0
|
513 * @param \Behat\Mink\Element\TraversableElement $container
|
Chris@0
|
514 * (optional) The document to check against. Defaults to the current page.
|
Chris@0
|
515 *
|
Chris@0
|
516 * @throws \Behat\Mink\Exception\ElementNotFoundException
|
Chris@0
|
517 * @throws \Behat\Mink\Exception\ExpectationException
|
Chris@0
|
518 */
|
Chris@0
|
519 public function hiddenFieldValueEquals($field, $value, TraversableElement $container = NULL) {
|
Chris@0
|
520 $node = $this->hiddenFieldExists($field, $container);
|
Chris@0
|
521 $actual = $node->getValue();
|
Chris@0
|
522 $regex = '/^' . preg_quote($value, '/') . '$/ui';
|
Chris@0
|
523 $message = "The hidden field '$field' value is '$actual', but '$value' expected.";
|
Chris@0
|
524 $this->assert((bool) preg_match($regex, $actual), $message);
|
Chris@0
|
525 }
|
Chris@0
|
526
|
Chris@0
|
527 /**
|
Chris@0
|
528 * Checks that specific hidden field doesn't have the provided value.
|
Chris@0
|
529 *
|
Chris@0
|
530 * @param string $field
|
Chris@0
|
531 * One of id|name|value for the hidden field.
|
Chris@0
|
532 * @param string $value
|
Chris@0
|
533 * The hidden field value that needs to be checked.
|
Chris@0
|
534 * @param \Behat\Mink\Element\TraversableElement $container
|
Chris@0
|
535 * (optional) The document to check against. Defaults to the current page.
|
Chris@0
|
536 *
|
Chris@0
|
537 * @throws \Behat\Mink\Exception\ElementNotFoundException
|
Chris@0
|
538 * @throws \Behat\Mink\Exception\ExpectationException
|
Chris@0
|
539 */
|
Chris@0
|
540 public function hiddenFieldValueNotEquals($field, $value, TraversableElement $container = NULL) {
|
Chris@0
|
541 $node = $this->hiddenFieldExists($field, $container);
|
Chris@0
|
542 $actual = $node->getValue();
|
Chris@0
|
543 $regex = '/^' . preg_quote($value, '/') . '$/ui';
|
Chris@0
|
544 $message = "The hidden field '$field' value is '$actual', but it should not be.";
|
Chris@0
|
545 $this->assert(!preg_match($regex, $actual), $message);
|
Chris@0
|
546 }
|
Chris@0
|
547
|
Chris@0
|
548 }
|