comparison core/tests/Drupal/Tests/WebAssert.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children 12f9dff5fda9
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
1 <?php 1 <?php
2 2
3 namespace Drupal\Tests; 3 namespace Drupal\Tests;
4 4
5 use Behat\Mink\Exception\ExpectationException; 5 use Behat\Mink\Exception\ExpectationException;
6 use Behat\Mink\Exception\ResponseTextException;
6 use Behat\Mink\WebAssert as MinkWebAssert; 7 use Behat\Mink\WebAssert as MinkWebAssert;
7 use Behat\Mink\Element\TraversableElement; 8 use Behat\Mink\Element\TraversableElement;
8 use Behat\Mink\Exception\ElementNotFoundException; 9 use Behat\Mink\Exception\ElementNotFoundException;
9 use Behat\Mink\Session; 10 use Behat\Mink\Session;
10 use Drupal\Component\Utility\Html; 11 use Drupal\Component\Utility\Html;
113 */ 114 */
114 public function selectExists($select, TraversableElement $container = NULL) { 115 public function selectExists($select, TraversableElement $container = NULL) {
115 $container = $container ?: $this->session->getPage(); 116 $container = $container ?: $this->session->getPage();
116 $node = $container->find('named', [ 117 $node = $container->find('named', [
117 'select', 118 'select',
118 $this->session->getSelectorsHandler()->xpathLiteral($select), 119 $select,
119 ]); 120 ]);
120 121
121 if ($node === NULL) { 122 if ($node === NULL) {
122 throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select); 123 throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select);
123 } 124 }
143 */ 144 */
144 public function optionExists($select, $option, TraversableElement $container = NULL) { 145 public function optionExists($select, $option, TraversableElement $container = NULL) {
145 $container = $container ?: $this->session->getPage(); 146 $container = $container ?: $this->session->getPage();
146 $select_field = $container->find('named', [ 147 $select_field = $container->find('named', [
147 'select', 148 'select',
148 $this->session->getSelectorsHandler()->xpathLiteral($select), 149 $select,
149 ]); 150 ]);
150 151
151 if ($select_field === NULL) { 152 if ($select_field === NULL) {
152 throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select); 153 throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select);
153 } 154 }
154 155
155 $option_field = $select_field->find('named', ['option', $option]); 156 $option_field = $select_field->find('named', ['option', $option]);
156 157
157 if ($option_field === NULL) { 158 if ($option_field === NULL) {
158 throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $option); 159 throw new ElementNotFoundException($this->session->getDriver(), 'select', 'id|name|label|value', $option);
159 } 160 }
160 161
161 return $option_field; 162 return $option_field;
162 } 163 }
163 164
165 * Checks that an option in a select field does NOT exist on the current page. 166 * Checks that an option in a select field does NOT exist on the current page.
166 * 167 *
167 * @param string $select 168 * @param string $select
168 * One of id|name|label|value for the select field. 169 * One of id|name|label|value for the select field.
169 * @param string $option 170 * @param string $option
170 * The option value that shoulkd not exist. 171 * The option value that should not exist.
171 * @param \Behat\Mink\Element\TraversableElement $container 172 * @param \Behat\Mink\Element\TraversableElement $container
172 * (optional) The document to check against. Defaults to the current page. 173 * (optional) The document to check against. Defaults to the current page.
173 * 174 *
174 * @throws \Behat\Mink\Exception\ElementNotFoundException 175 * @throws \Behat\Mink\Exception\ElementNotFoundException
175 * When the select element doesn't exist. 176 * When the select element doesn't exist.
176 */ 177 */
177 public function optionNotExists($select, $option, TraversableElement $container = NULL) { 178 public function optionNotExists($select, $option, TraversableElement $container = NULL) {
178 $container = $container ?: $this->session->getPage(); 179 $container = $container ?: $this->session->getPage();
179 $select_field = $container->find('named', [ 180 $select_field = $container->find('named', [
180 'select', 181 'select',
181 $this->session->getSelectorsHandler()->xpathLiteral($select), 182 $select,
182 ]); 183 ]);
183 184
184 if ($select_field === NULL) { 185 if ($select_field === NULL) {
185 throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select); 186 throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select);
186 } 187 }
303 * The full or partial value of the 'href' attribute of the anchor tag. 304 * The full or partial value of the 'href' attribute of the anchor tag.
304 * @param int $index 305 * @param int $index
305 * Link position counting from zero. 306 * Link position counting from zero.
306 * @param string $message 307 * @param string $message
307 * (optional) A message to display with the assertion. Do not translate 308 * (optional) A message to display with the assertion. Do not translate
308 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed 309 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
309 * variables in the message text, not t(). If left blank, a default message 310 * variables in the message text, not t(). If left blank, a default message
310 * will be displayed. 311 * will be displayed.
311 * 312 *
312 * @throws \Behat\Mink\Exception\ExpectationException 313 * @throws \Behat\Mink\Exception\ExpectationException
313 * Thrown when element doesn't exist, or the link label is a different one. 314 * Thrown when element doesn't exist, or the link label is a different one.
324 * 325 *
325 * @param string $href 326 * @param string $href
326 * The full or partial value of the 'href' attribute of the anchor tag. 327 * The full or partial value of the 'href' attribute of the anchor tag.
327 * @param string $message 328 * @param string $message
328 * (optional) A message to display with the assertion. Do not translate 329 * (optional) A message to display with the assertion. Do not translate
329 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed 330 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
330 * variables in the message text, not t(). If left blank, a default message 331 * variables in the message text, not t(). If left blank, a default message
331 * will be displayed. 332 * will be displayed.
332 * 333 *
333 * @throws \Behat\Mink\Exception\ExpectationException 334 * @throws \Behat\Mink\Exception\ExpectationException
334 * Thrown when element doesn't exist, or the link label is a different one. 335 * Thrown when element doesn't exist, or the link label is a different one.
486 } 487 }
487 throw new ElementNotFoundException($this->session->getDriver(), 'form hidden field', 'id|name|value', $field); 488 throw new ElementNotFoundException($this->session->getDriver(), 'form hidden field', 'id|name|value', $field);
488 } 489 }
489 490
490 /** 491 /**
491 * Checks that specific hidden field does not exists. 492 * Checks that specific hidden field does not exist.
492 * 493 *
493 * @param string $field 494 * @param string $field
494 * One of id|name|value for the hidden field. 495 * One of id|name|value for the hidden field.
495 * @param \Behat\Mink\Element\TraversableElement $container 496 * @param \Behat\Mink\Element\TraversableElement $container
496 * (optional) The document to check against. Defaults to the current page. 497 * (optional) The document to check against. Defaults to the current page.
543 $regex = '/^' . preg_quote($value, '/') . '$/ui'; 544 $regex = '/^' . preg_quote($value, '/') . '$/ui';
544 $message = "The hidden field '$field' value is '$actual', but it should not be."; 545 $message = "The hidden field '$field' value is '$actual', but it should not be.";
545 $this->assert(!preg_match($regex, $actual), $message); 546 $this->assert(!preg_match($regex, $actual), $message);
546 } 547 }
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
548 } 576 }