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