Commit 58256f9c authored by alexpott's avatar alexpott

Issue #2736109 by klausi, dawehner: Convert web tests to browser tests for action module

parent f90b3df9
<?php
namespace Drupal\action\Tests;
namespace Drupal\Tests\action\Functional;
use Drupal\simpletest\WebTestBase;
use Drupal\Tests\BrowserTestBase;
/**
* Tests that uninstalling actions does not remove other module's actions.
......@@ -10,7 +10,7 @@
* @group action
* @see \Drupal\action\Plugin\views\field\BulkForm
*/
class ActionUninstallTest extends WebTestBase {
class ActionUninstallTest extends BrowserTestBase {
/**
* Modules to install.
......
<?php
namespace Drupal\action\Tests;
namespace Drupal\Tests\action\Functional;
use Drupal\simpletest\WebTestBase;
use Drupal\Tests\BrowserTestBase;
use Drupal\views\Views;
/**
......@@ -11,7 +11,7 @@
* @group action
* @see \Drupal\action\Plugin\views\field\BulkForm
*/
class BulkFormTest extends WebTestBase {
class BulkFormTest extends BrowserTestBase {
/**
* Modules to install.
......@@ -123,7 +123,7 @@ public function testBulkForm() {
// Check the default title.
$this->drupalGet('test_bulk_form');
$result = $this->xpath('//label[@for="edit-action"]');
$this->assertEqual('With selection', (string) $result[0]);
$this->assertEqual('With selection', $result[0]->getText());
// Setup up a different bulk form title.
$view = Views::getView('test_bulk_form');
......@@ -133,7 +133,7 @@ public function testBulkForm() {
$this->drupalGet('test_bulk_form');
$result = $this->xpath('//label[@for="edit-action"]');
$this->assertEqual('Test title', (string) $result[0]);
$this->assertEqual('Test title', $result[0]->getText());
$this->drupalGet('test_bulk_form');
// Call the node delete action.
......
<?php
namespace Drupal\action\Tests;
namespace Drupal\Tests\action\Functional;
use Drupal\Component\Utility\Crypt;
use Drupal\simpletest\WebTestBase;
use Drupal\system\Entity\Action;
use Drupal\Tests\BrowserTestBase;
/**
* Tests complex actions configuration by adding, editing, and deleting a
......@@ -12,7 +12,7 @@
*
* @group action
*/
class ConfigurationTest extends WebTestBase {
class ConfigurationTest extends BrowserTestBase {
/**
* Modules to install.
......
......@@ -53,10 +53,19 @@ protected function assertElementNotPresent($css_selector) {
* Plain text to look for.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->responseContains() instead.
* Use $this->assertSession()->pageTextContains() or
* $this->assertSession()->responseContains() instead.
*/
protected function assertText($text) {
$this->assertSession()->responseContains($text);
$content_type = $this->getSession()->getResponseHeader('Content-type');
// In case of a Non-HTML response (example: XML) check the original
// response.
if (strpos($content_type, 'html') === FALSE) {
$this->assertSession()->responseContains($text);
}
else {
$this->assertSession()->pageTextContains($text);
}
}
/**
......@@ -69,10 +78,19 @@ protected function assertText($text) {
* Plain text to look for.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->responseNotContains() instead.
* Use $this->assertSession()->pageTextNotContains() or
* $this->assertSession()->responseNotContains() instead.
*/
protected function assertNoText($text) {
$this->assertSession()->responseNotContains($text);
$content_type = $this->getSession()->getResponseHeader('Content-type');
// In case of a Non-HTML response (example: XML) check the original
// response.
if (strpos($content_type, 'html') === FALSE) {
$this->assertSession()->responseNotContains($text);
}
else {
$this->assertSession()->pageTextNotContains($text);
}
}
/**
......@@ -110,6 +128,25 @@ protected function assertFieldByName($name, $value = NULL) {
}
}
/**
* Asserts that a field exists with the given ID and value.
*
* @param string $id
* ID of field to assert.
* @param string|\Drupal\Component\Render\MarkupInterface $value
* (optional) Value for the field to assert. You may pass in NULL to skip
* checking the value, while still checking that the field exists.
* However, the default value ('') asserts that the field value is an empty
* string.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->fieldExists() or
* $this->assertSession()->fieldValueEquals() instead.
*/
protected function assertFieldById($id, $value = NULL) {
$this->assertFieldByName($id, $value);
}
/**
* Passes if the raw text IS found on the loaded page, fail otherwise.
*
......@@ -149,4 +186,47 @@ protected function assertLink($label, $index = 0) {
return $this->assertSession()->linkExists($label, $index);
}
/**
* Asserts that a select option in the current page exists.
*
* @param string $id
* ID of select field to assert.
* @param string $option
* Option to assert.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->optionExists() instead.
*/
protected function assertOption($id, $option) {
return $this->assertSession()->optionExists($id, $option);
}
/**
* Asserts that a select option does NOT exist in the current page.
*
* @param string $id
* ID of select field to assert.
* @param string $option
* Option to assert.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->optionNotExists() instead.
*/
protected function assertNoOption($id, $option) {
return $this->assertSession()->optionNotExists($id, $option);
}
/**
* Passes if the internal browser's URL matches the given path.
*
* @param string $path
* The expected system path.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->addressEquals() instead.
*/
protected function assertUrl($path) {
$this->assertSession()->addressEquals($path);
}
}
......@@ -22,6 +22,7 @@
use Drupal\Core\Test\TestDatabase;
use Drupal\FunctionalTests\AssertLegacyTrait;
use Drupal\simpletest\AssertHelperTrait;
use Drupal\simpletest\ContentTypeCreationTrait;
use Drupal\simpletest\BlockCreationTrait;
use Drupal\simpletest\NodeCreationTrait;
use Drupal\user\Entity\Role;
......@@ -47,6 +48,9 @@ abstract class BrowserTestBase extends \PHPUnit_Framework_TestCase {
use NodeCreationTrait {
createNode as drupalCreateNode;
}
use ContentTypeCreationTrait {
createContentType as drupalCreateContentType;
}
/**
* Class loader.
......@@ -552,7 +556,7 @@ public function getSession($name = NULL) {
* A new web-assert option for asserting the presence of elements with.
*/
public function assertSession($name = NULL) {
return new WebAssert($this->getSession($name));
return new WebAssert($this->getSession($name), $this->baseUrl);
}
/**
......@@ -1763,6 +1767,16 @@ protected function drupalGetHeader($name) {
return $this->getSession()->getResponseHeader($name);
}
/**
* Get the current URL from the browser.
*
* @return string
* The current URL.
*/
protected function getUrl() {
return $this->getSession()->getCurrentUrl();
}
/**
* {@inheritdoc}
*/
......
......@@ -6,6 +6,7 @@
use Behat\Mink\WebAssert as MinkWebAssert;
use Behat\Mink\Element\TraversableElement;
use Behat\Mink\Exception\ElementNotFoundException;
use Behat\Mink\Session;
use Drupal\Component\Utility\Html;
/**
......@@ -13,6 +14,42 @@
*/
class WebAssert extends MinkWebAssert {
/**
* The absolute URL of the site under test.
*
* @var string
*/
protected $baseUrl = '';
/**
* Constructor.
*
* @param \Behat\Mink\Session $session
* The Behat session object;
* @param string $base_url
* The base URL of the site under test.
*/
public function __construct(Session $session, $base_url = '') {
parent::__construct($session);
$this->baseUrl = $base_url;
}
/**
* {@inheritdoc}
*/
protected function cleanUrl($url) {
// Strip the base URL from the beginning for absolute URLs.
if ($this->baseUrl !== '' && strpos($url, $this->baseUrl) === 0) {
$url = substr($url, strlen($this->baseUrl));
}
// Make sure there is a forward slash at the beginning of relative URLs for
// consistency.
if (parse_url($url, PHP_URL_HOST) === NULL && strpos($url, '/') !== 0) {
$url = "/$url";
}
return parent::cleanUrl($url);
}
/**
* Checks that specific button exists on the current page.
*
......@@ -66,6 +103,71 @@ public function selectExists($select, TraversableElement $container = NULL) {
return $node;
}
/**
* Checks that specific option in a select field exists on the current page.
*
* @param string $select
* One of id|name|label|value for the select field.
* @param string $option
* The option value.
* @param \Behat\Mink\Element\TraversableElement $container
* (optional) The document to check against. Defaults to the current page.
*
* @return \Behat\Mink\Element\NodeElement
* The matching option element
*
* @throws \Behat\Mink\Exception\ElementNotFoundException
* When the element doesn't exist.
*/
public function optionExists($select, $option, TraversableElement $container = NULL) {
$container = $container ?: $this->session->getPage();
$select_field = $container->find('named', array(
'select',
$this->session->getSelectorsHandler()->xpathLiteral($select),
));
if ($select_field === NULL) {
throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select);
}
$option_field = $select_field->find('named', array('option', $option));
if ($option_field === NULL) {
throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $option);
}
return $option_field;
}
/**
* Checks that an option in a select field does NOT exist on the current page.
*
* @param string $select
* One of id|name|label|value for the select field.
* @param string $option
* The option value that shoulkd not exist.
* @param \Behat\Mink\Element\TraversableElement $container
* (optional) The document to check against. Defaults to the current page.
*
* @throws \Behat\Mink\Exception\ElementNotFoundException
* When the select element doesn't exist.
*/
public function optionNotExists($select, $option, TraversableElement $container = NULL) {
$container = $container ?: $this->session->getPage();
$select_field = $container->find('named', array(
'select',
$this->session->getSelectorsHandler()->xpathLiteral($select),
));
if ($select_field === NULL) {
throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select);
}
$option_field = $select_field->find('named', array('option', $option));
$this->assert($option_field === NULL, sprintf('An option "%s" exists in select "%s", but it should not.', $option, $select));
}
/**
* Pass if the page title is the given string.
*
......
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