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