Commit df1fe181 authored by xjm's avatar xjm

Issue #2759879 by klausi, claudiu.cristea, dawehner, xjm, Lendude, jibran:...

Issue #2759879 by klausi, claudiu.cristea, dawehner, xjm, Lendude, jibran: Additional assertions for WebAssert and AssertLegacyTrait, part 2
parent 011d2c29
......@@ -93,6 +93,58 @@ protected function assertNoText($text) {
}
}
/**
* Passes if the text is found ONLY ONCE on the text version of the page.
*
* The text version is the equivalent of what a user would see when viewing
* through a web browser. In other words the HTML has been filtered out of
* the contents.
*
* @param string|\Drupal\Component\Render\MarkupInterface $text
* Plain text to look for.
* @param string $message
* (optional) A message to display with the assertion. Do not translate
* messages with t(). If left blank, a default message will be displayed.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->getSession()->getPage()->getText() and substr_count() instead.
*/
protected function assertUniqueText($text, $message = NULL) {
// Cast MarkupInterface objects to string.
$text = (string) $text;
$message = $message ?: "'$text' found only once on the page";
$page_text = $this->getSession()->getPage()->getText();
$nr_found = substr_count($page_text, $text);
$this->assertSame(1, $nr_found, $message);
}
/**
* Passes if the text is found MORE THAN ONCE on the text version of the page.
*
* The text version is the equivalent of what a user would see when viewing
* through a web browser. In other words the HTML has been filtered out of
* the contents.
*
* @param string|\Drupal\Component\Render\MarkupInterface $text
* Plain text to look for.
* @param string $message
* (optional) A message to display with the assertion. Do not translate
* messages with t(). If left blank, a default message will be displayed.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->getSession()->getPage()->getText() and substr_count() instead.
*/
protected function assertNoUniqueText($text, $message = '') {
// Cast MarkupInterface objects to string.
$text = (string) $text;
$message = $message ?: "'$text' found more than once on the page";
$page_text = $this->getSession()->getPage()->getText();
$nr_found = substr_count($page_text, $text);
$this->assertGreaterThan(1, $nr_found, $message);
}
/**
* Asserts the page responds with the specified response code.
*
......@@ -128,6 +180,27 @@ protected function assertFieldByName($name, $value = NULL) {
}
}
/**
* 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()->fieldNotExists() or
* $this->assertSession()->fieldValueNotEquals() instead.
*/
protected function assertNoFieldByName($name, $value = NULL) {
$this->assertSession()->fieldNotExists($name);
if ($value !== NULL) {
$this->assertSession()->fieldValueNotEquals($name, (string) $value);
}
}
/**
* Asserts that a field exists with the given ID and value.
*
......@@ -343,6 +416,53 @@ protected function assertNoOption($id, $option) {
return $this->assertSession()->optionNotExists($id, $option);
}
/**
* Asserts that a select option in the current page is checked.
*
* @param string $id
* ID of select field to assert.
* @param string $option
* Option to assert.
* @param string $message
* (optional) A message to display with the assertion. Do not translate
* messages with t(). If left blank, a default message will be displayed.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->optionExists() instead and check the
* "selected" attribute yourself.
*/
protected function assertOptionSelected($id, $option, $message = NULL) {
$option_field = $this->assertSession()->optionExists($id, $option);
$message = $message ?: "Option $option for field $id is selected.";
$this->assertTrue($option_field->hasAttribute('selected'), $message);
}
/**
* Asserts that a checkbox field in the current page is checked.
*
* @param string $id
* ID of field to assert.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->checkboxChecked() instead.
*/
protected function assertFieldChecked($id) {
$this->assertSession()->checkboxChecked($id);
}
/**
* Asserts that a checkbox field in the current page is not checked.
*
* @param string $id
* ID of field to assert.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->checkboxNotChecked() instead.
*/
protected function assertNoFieldChecked($id) {
$this->assertSession()->checkboxNotChecked($id);
}
/**
* Passes if the raw text IS found escaped on the loaded page, fail otherwise.
*
......@@ -373,6 +493,19 @@ protected function assertNoEscaped($raw) {
$this->assertSession()->assertNoEscaped($raw);
}
/**
* Triggers a pass if the Perl regex pattern is found in the raw content.
*
* @param string $pattern
* Perl regex to look for including the regex delimiters.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->responseMatches() instead.
*/
protected function assertPattern($pattern) {
$this->assertSession()->responseMatches($pattern);
}
/**
* Asserts whether an expected cache tag was present in the last response.
*
......
<?php
namespace Drupal\Tests\Core\Assert;
use Behat\Mink\Element\DocumentElement;
use Behat\Mink\Element\NodeElement;
use Behat\Mink\Session;
use Drupal\Component\Render\MarkupInterface;
use Drupal\FunctionalTests\AssertLegacyTrait;
use Drupal\Tests\UnitTestCase;
use Drupal\Tests\WebAssert;
use PHPUnit_Framework_ExpectationFailedException;
/**
* @coversDefaultClass \Drupal\FunctionalTests\AssertLegacyTrait
* @group Assert
*/
class AssertLegacyTraitTest extends UnitTestCase {
use AssertLegacyTrait;
/**
* The mocked Mink session object used for testing.
*
* @var \Behat\Mink\Session|\Prophecy\Prophecy\ObjectProphecy
*/
protected $session;
/**
* The mocked page element used for testing.
*
* @var Behat\Mink\Element\DocumentElement|\Prophecy\Prophecy\ObjectProphecy
*/
protected $page;
/**
* The mocked web assert class.
*
* @var \Drupal\Tests\WebAssert|\Prophecy\Prophecy\ObjectProphecy
*/
protected $webAssert;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->page = $this->prophesize(DocumentElement::class);
$this->session = $this->prophesize(Session::class);
$this->session->getPage()->willReturn($this->page->reveal());
$this->webAssert = $this->prophesize(WebAssert::class);
}
/**
* @covers ::assertUniqueText
*/
public function testAssertUniqueText() {
$this->page->getText()->willReturn('foo bar bar');
$this->assertUniqueText('foo');
}
/**
* @covers ::assertUniqueText
*/
public function testAssertUniqueTextFail() {
$this->page->getText()->willReturn('foo bar bar');
$this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
$this->assertUniqueText('bar');
}
/**
* @covers ::assertUniqueText
*/
public function testAssertUniqueTextUnknown() {
$this->page->getText()->willReturn('foo bar bar');
$this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
$this->assertUniqueText('alice');
}
/**
* @covers ::assertUniqueText
*/
public function testAssertUniqueTextMarkup() {
$this->page->getText()->willReturn('foo bar bar');
$markupObject = $this->prophesize(MarkupInterface::class);
$markupObject->__toString()->willReturn('foo');
$this->assertUniqueText($markupObject->reveal());
}
/**
* @covers ::assertNoUniqueText
*/
public function testAssertNoUniqueText() {
$this->page->getText()->willReturn('foo bar bar');
$this->assertNoUniqueText('bar');
}
/**
* @covers ::assertNoUniqueText
*/
public function testAssertNoUniqueTextFail() {
$this->page->getText()->willReturn('foo bar bar');
$this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
$this->assertNoUniqueText('foo');
}
/**
* @covers ::assertNoUniqueText
*/
public function testAssertNoUniqueTextUnknown() {
$this->page->getText()->willReturn('foo bar bar');
$this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
$this->assertNoUniqueText('alice');
}
/**
* @covers ::assertNoUniqueText
*/
public function testAssertNoUniqueTextMarkup() {
$this->page->getText()->willReturn('foo bar bar');
$markupObject = $this->prophesize(MarkupInterface::class);
$markupObject->__toString()->willReturn('bar');
$this->assertNoUniqueText($markupObject->reveal());
}
/**
* @covers ::assertOptionSelected
*/
public function testAssertOptionSelected() {
$option_field = $this->prophesize(NodeElement::class);
$option_field->hasAttribute('selected')->willReturn(TRUE);
$this->webAssert
->optionExists('myselect', 'two')
->willReturn($option_field->reveal());
$this->assertOptionSelected('myselect', 'two');
}
/**
* @covers ::assertOptionSelected
*/
public function testAssertOptionSelectedFail() {
$option_field = $this->prophesize(NodeElement::class);
$option_field->hasAttribute('selected')->willReturn(FALSE);
$this->webAssert
->optionExists('myselect', 'two')
->willReturn($option_field->reveal());
$this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
$this->assertOptionSelected('myselect', 'two');
}
/**
* Returns a mocked behat session object.
*
* @return \Behat\Mink\Session
* The mocked session.
*/
protected function getSession() {
return $this->session->reveal();
}
/**
* {@inheritdoc}
*/
public function assertSession($name = NULL) {
return $this->webAssert->reveal();
}
}
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