Commit a4bf1e9a authored by alexpott's avatar alexpott

Issue #2735045 by klausi, dawehner, jibran: Convert StandardTest to...

Issue #2735045 by klausi, dawehner, jibran: Convert StandardTest to BrowserTestBase by introducing a AssertLegacyTrait and new assertions on the WebAssert
parent 83fce77e
<?php
namespace Drupal\standard\Tests;
namespace Drupal\Tests\standard\Functional;
use Drupal\config\Tests\SchemaCheckTestTrait;
use Drupal\contact\Entity\ContactForm;
use Drupal\Core\Url;
use Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber;
use Drupal\filter\Entity\FilterFormat;
use Drupal\simpletest\WebTestBase;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\Role;
/**
......@@ -15,7 +15,7 @@
*
* @group standard
*/
class StandardTest extends WebTestBase {
class StandardTest extends BrowserTestBase {
use SchemaCheckTestTrait;
......
<?php
namespace Drupal\FunctionalTests;
use Drupal\KernelTests\AssertLegacyTrait as BaseAssertLegacyTrait;
/**
* Provides convenience methods for assertions in browser tests.
*
* @deprecated Scheduled for removal in Drupal 9.0.0. Use the methods on
* \Drupal\Tests\WebAssert instead, for example
* @code
* $this->assertSession()->statusCodeEquals(200);
* @endcode
*/
trait AssertLegacyTrait {
use BaseAssertLegacyTrait;
/**
* Asserts that the element with the given CSS selector is present.
*
* @param string $css_selector
* The CSS selector identifying the element to check.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->elementExists() instead.
*/
protected function assertElementPresent($css_selector) {
$this->assertSession()->elementExists('css', $css_selector);
}
/**
* Asserts that the element with the given CSS selector is not present.
*
* @param string $css_selector
* The CSS selector identifying the element to check.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->elementNotExists() instead.
*/
protected function assertElementNotPresent($css_selector) {
$this->assertSession()->elementNotExists('css', $css_selector);
}
/**
* Passes if the page (with HTML stripped) contains the text.
*
* Note that stripping HTML tags also removes their attributes, such as
* the values of text fields.
*
* @param string $text
* Plain text to look for.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->responseContains() instead.
*/
protected function assertText($text) {
$this->assertSession()->responseContains($text);
}
/**
* Passes if the page (with HTML stripped) does not contains the text.
*
* Note that stripping HTML tags also removes their attributes, such as
* the values of text fields.
*
* @param string $text
* Plain text to look for.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->responseNotContains() instead.
*/
protected function assertNoText($text) {
$this->assertSession()->responseNotContains($text);
}
/**
* Asserts the page responds with the specified response code.
*
* @param int $code
* Response code. For example 200 is a successful page request. For a list
* of all codes see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->statusCodeEquals() instead.
*/
protected function assertResponse($code) {
$this->assertSession()->statusCodeEquals($code);
}
/**
* Asserts that a field exists with the given name and value.
*
* @param string $name
* Name of field to assert.
* @param string $value
* (optional) Value of the field to assert. You may pass in NULL (default)
* to skip checking the actual value, while still checking that the field
* exists.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->fieldExists() or
* $this->assertSession()->fieldValueEquals() instead.
*/
protected function assertFieldByName($name, $value = NULL) {
$this->assertSession()->fieldExists($name);
if ($value !== NULL) {
$this->assertSession()->fieldValueEquals($name, $value);
}
}
/**
* Passes if the raw text IS found on the loaded page, fail otherwise.
*
* Raw text refers to the raw HTML that the page generated.
*
* @param string $raw
* Raw (HTML) string to look for.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->responseContains() instead.
*/
protected function assertRaw($raw) {
$this->assertSession()->responseContains($raw);
}
/**
* Pass if the page title is the given string.
*
* @param string $expected_title
* The string the page title should be.
*/
protected function assertTitle($expected_title) {
return $this->assertSession()->titleEquals($expected_title);
}
/**
* Passes if a link with the specified label is found.
*
* An optional link index may be passed.
*
* @param string|\Drupal\Component\Render\MarkupInterface $label
* Text between the anchor tags.
* @param int $index
* Link position counting from zero.
*/
public function assertLink($label, $index = 0) {
return $this->assertSession()->linkExists($label, $index);
}
}
......@@ -1221,6 +1221,9 @@ public function __sleep() {
* {@inheritdoc}
*/
public static function assertEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE) {
// Cast objects implementing MarkupInterface to string instead of
// relying on PHP casting them to string depending on what they are being
// comparing with.
$expected = static::castSafeStrings($expected);
$actual = static::castSafeStrings($actual);
parent::assertEquals($expected, $actual, $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
......
This diff is collapsed.
......@@ -2,9 +2,11 @@
namespace Drupal\Tests;
use Behat\Mink\Exception\ExpectationException;
use Behat\Mink\WebAssert as MinkWebAssert;
use Behat\Mink\Element\TraversableElement;
use Behat\Mink\Exception\ElementNotFoundException;
use Drupal\Component\Utility\Html;
/**
* Defines a class with methods for asserting presence of elements during tests.
......@@ -64,4 +66,83 @@ public function selectExists($select, TraversableElement $container = NULL) {
return $node;
}
/**
* Pass if the page title is the given string.
*
* @param string $expected_title
* The string the page title should be.
*
* @throws \Behat\Mink\Exception\ExpectationException
* Thrown when element doesn't exist, or the title is a different one.
*/
public function titleEquals($expected_title) {
$title_element = $this->session->getPage()->find('css', 'title');
if (!$title_element) {
throw new ExpectationException('No title element found on the page', $this->session);
}
$actual_title = $title_element->getText();
$this->assert($expected_title === $actual_title, 'Title found');
}
/**
* Passes if a link with the specified label is found.
*
* An optional link index may be passed.
*
* @param string|\Drupal\Component\Render\MarkupInterface $label
* Text between the anchor tags.
* @param int $index
* Link position counting from zero.
* @param string $message
* (optional) A message to display with the assertion. Do not translate
* messages: use strtr() to embed variables in the message text, not
* t(). If left blank, a default message will be displayed.
*
* @throws \Behat\Mink\Exception\ExpectationException
* Thrown when element doesn't exist, or the link label is a different one.
*/
public function linkExists($label, $index = 0, $message = '') {
// Cast MarkupInterface objects to string.
$label = (string) $label;
$message = ($message ? $message : strtr('Link with label %label found.', ['%label' => $label]));
$links = $this->session->getPage()->findAll('named', ['link', $label]);
if (empty($links[$index])) {
throw new ExpectationException($message);
}
$this->assert($links[$index] !== NULL, $message);
}
/**
* Passes if the raw text IS NOT found escaped on the loaded page.
*
* Raw text refers to the raw HTML that the page generated.
*
* @param string $raw
* Raw (HTML) string to look for.
*/
public function assertNoEscaped($raw) {
$this->pageTextNotContains(Html::escape($raw));
}
/**
* Asserts a condition.
*
* The parent method is overridden because it is a private method.
*
* @param bool $condition
* The condition.
* @param string $message
* The success message.
*
* @throws \Behat\Mink\Exception\ExpectationException
* When the condition is not fulfilled.
*/
public function assert($condition, $message) {
if ($condition) {
return;
}
throw new ExpectationException($message, $this->session->getDriver());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment