Commit 7e9fbc3c authored by catch's avatar catch
Browse files

Issue #3139405 by mondrake, jungle, longwave: Replace usages of...

Issue #3139405 by mondrake, jungle, longwave: Replace usages of AssertLegacyTrait::assert(No)UniqueText, that is deprecated
parent d91112e0
......@@ -113,7 +113,7 @@ public function _testColor($theme, $test_values) {
$this->drupalLogin($this->bigUser);
$this->drupalGet($settings_path);
$this->assertSession()->statusCodeEquals(200);
$this->assertUniqueText('Color set');
$this->assertSession()->pageTextContainsOnce('Color set');
$edit['scheme'] = '';
$edit[$test_values['palette_input']] = '#123456';
$this->drupalPostForm($settings_path, $edit, t('Save configuration'));
......
......@@ -186,7 +186,7 @@ public function testSiteWideContact() {
$this->assertEqual($langcode, $default_langcode);
// Make sure the newly created form is included in the list of forms.
$this->assertNoUniqueText($label, 'New form included in forms list.');
$this->assertSession()->pageTextMatchesCount(2, '/' . $label . '/');
// Ensure that the recipient email is escaped on the listing.
$this->drupalGet('admin/structure/contact');
......
......@@ -739,7 +739,7 @@ protected function widgetAlterTest($hook, $widget) {
$this->rebuildAll();
$this->drupalGet('entity_test/add');
$this->assertUniqueText("From $hook(): prefix on $field_name parent element.");
$this->assertSession()->pageTextMatchesCount(1, '/From ' . $hook . '.* prefix on ' . $field_name . ' parent element\./');
if ($widget === 'test_field_widget_multiple_single_value') {
$suffix_text = "From $hook(): suffix on $field_name child element.";
$this->assertEqual($field_storage['cardinality'], substr_count($this->getTextContent(), $suffix_text), "'$suffix_text' was found {$field_storage['cardinality']} times using widget $widget");
......
......@@ -218,7 +218,7 @@ public function testPluralEditDateFormatter() {
$this->drupalPostForm($path, $edit, t('Save translations'));
// User interface input for translating seconds should not be duplicated
$this->assertUniqueText('@count seconds', 'Interface translation input for @count seconds only appears once.');
$this->assertSession()->pageTextContainsOnce('@count seconds');
// Member for time should be translated. Change the created time to ensure
// that the we're dealing in multiple seconds and it can't be exactly 1
......
......@@ -35,17 +35,17 @@ public function testDrupalMessengerService() {
$this->assertRaw(t('Second message with <em>markup!</em> (not removed).'));
// Ensure duplicate messages are handled as expected.
$this->assertUniqueText('Non Duplicated message');
$this->assertNoUniqueText('Duplicated message');
$this->assertSession()->pageTextMatchesCount(1, '/Non Duplicated message/');
$this->assertSession()->pageTextMatchesCount(3, '/Duplicated message/');
// Ensure Markup objects are rendered as expected.
$this->assertRaw('Markup with <em>markup!</em>');
$this->assertUniqueText('Markup with markup!');
$this->assertSession()->pageTextMatchesCount(1, '/Markup with markup!/');
$this->assertRaw('Markup2 with <em>markup!</em>');
// Ensure when the same message is of different types it is not duplicated.
$this->assertUniqueText('Non duplicate Markup / string.');
$this->assertNoUniqueText('Duplicate Markup / string.');
$this->assertSession()->pageTextMatchesCount(1, '$Non duplicate Markup / string.$');
$this->assertSession()->pageTextMatchesCount(2, '$Duplicate Markup / string.$');
// Ensure that strings that are not marked as safe are escaped.
$this->assertSession()->assertEscaped('<em>This<span>markup will be</span> escaped</em>.');
......
......@@ -140,7 +140,7 @@ protected function doTestMenuLinksDiscoveredAlter() {
// Make sure that rebuilding the menu tree does not produce duplicates of
// links added by hook_menu_links_discovered_alter().
$this->drupalGet('menu-test');
$this->assertUniqueText('Custom link', 'Menu links added by hook_menu_links_discovered_alter() do not duplicate after a menu rebuild.');
$this->assertSession()->pageTextContainsOnce('Custom link');
}
/**
......
......@@ -220,9 +220,11 @@ public function testTaxonomyNode() {
// Preview the node.
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview'));
$this->assertUniqueText($term2->getName(), 'Term is displayed when previewing the node.');
// Ensure that term is displayed when previewing the node.
$this->assertSession()->pageTextContainsOnce($term2->getName());
$this->drupalPostForm('node/' . $node->id() . '/edit', NULL, t('Preview'));
$this->assertUniqueText($term2->getName(), 'Term is displayed when previewing the node again.');
// Ensure that term is displayed when previewing the node again.
$this->assertSession()->pageTextContainsOnce($term2->getName());
}
/**
......
......@@ -478,7 +478,7 @@ public function testUpdateBrokenFetchURL() {
$this->assertText(t('Up to date'));
// We're expecting the report to say most projects are up to date, so we
// hope that 'Up to date' is not unique.
$this->assertNoUniqueText(t('Up to date'));
$this->assertSession()->pageTextMatchesCount(3, '/Up to date/');
// It should say we failed to get data, not that we're missing an update.
$this->assertNoText('Update available');
......@@ -488,8 +488,8 @@ public function testUpdateBrokenFetchURL() {
$this->assertRaw('<div class="project-update__status">' . t('Failed to get available update data'));
// We should see the output messages from fetching manually.
$this->assertUniqueText(t('Checked available update data for 3 projects.'));
$this->assertUniqueText(t('Failed to get available update data for one project.'));
$this->assertSession()->pageTextContainsOnce('Checked available update data for 3 projects.');
$this->assertSession()->pageTextContainsOnce('Failed to get available update data for one project.');
// The other two should be listed as projects.
$this->assertRaw(Link::fromTextAndUrl(t('AAA Update test'), Url::fromUri('http://example.com/project/aaa_update_test'))->toString(), 'Link to aaa_update_test project appears.');
......
......@@ -768,7 +768,7 @@ public function testServiceUnavailable() {
$this->refreshUpdateStatus([], '503-error');
// Ensure that no "Warning: SimpleXMLElement..." parse errors are found.
$this->assertNoText('SimpleXMLElement');
$this->assertUniqueText(t('Failed to get available update data for one project.'));
$this->assertSession()->pageTextContainsOnce('Failed to get available update data for one project.');
}
/**
......
......@@ -172,12 +172,13 @@ protected function assertTextHelper($text, $not_exists = TRUE) {
* messages with t(). If left blank, a default message will be displayed.
*
* @deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use
* $this->getSession()->getPage()->getText() and substr_count() instead.
* $this->getSession()->pageTextContainsOnce() or
* $this->getSession()->pageTextMatchesCount() instead.
*
* @see https://www.drupal.org/node/3129738
*/
protected function assertUniqueText($text, $message = NULL) {
@trigger_error('AssertLegacyTrait::assertUniqueText() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use $this->getSession()->getPage()->getText() and substr_count() instead. See https://www.drupal.org/node/3129738', E_USER_DEPRECATED);
@trigger_error('AssertLegacyTrait::assertUniqueText() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use $this->getSession()->pageTextContainsOnce() or $this->getSession()->pageTextMatchesCount() instead. See https://www.drupal.org/node/3129738', E_USER_DEPRECATED);
// Cast MarkupInterface objects to string.
$text = (string) $text;
......@@ -200,13 +201,15 @@ protected function assertUniqueText($text, $message = NULL) {
* (optional) A message to display with the assertion. Do not translate
* messages with t(). If left blank, a default message will be displayed.
*
* @deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use
* $this->getSession()->getPage()->getText() and substr_count() instead.
* @deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Instead,
* use $this->getSession()->pageTextMatchesCount() if you know the
* cardinality in advance, or $this->getSession()->getPage()->getText()
* and substr_count().
*
* @see https://www.drupal.org/node/3129738
*/
protected function assertNoUniqueText($text, $message = '') {
@trigger_error('AssertLegacyTrait::assertNoUniqueText() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use $this->getSession()->getPage()->getText() and substr_count() instead. See https://www.drupal.org/node/3129738', E_USER_DEPRECATED);
@trigger_error('AssertLegacyTrait::assertNoUniqueText() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Instead, use $this->getSession()->pageTextMatchesCount() if you know the cardinality in advance, or $this->getSession()->getPage()->getText() and substr_count(). See https://www.drupal.org/node/3129738', E_USER_DEPRECATED);
// Cast MarkupInterface objects to string.
$text = (string) $text;
......
......@@ -4,6 +4,7 @@
use Drupal\Tests\BrowserTestBase;
use Behat\Mink\Exception\ExpectationException;
use Behat\Mink\Exception\ResponseTextException;
use PHPUnit\Framework\AssertionFailedError;
/**
......@@ -82,4 +83,34 @@ public function testAddressEqualsDistinguishesQuerystrings() {
$this->assertSession()->addressEquals('admin/reports/dblog');
}
/**
* @covers ::pageTextMatchesCount
*/
public function testPageTextMatchesCount() {
$this->drupalLogin($this->drupalCreateUser());
// Visit a Drupal page that requires login.
$this->drupalGet('test-page');
$this->assertSession()->pageTextMatchesCount(1, '/Test page text\./');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage("Failed asserting that the page matches the pattern '/does-not-exist/' 1 time(s), 0 found.");
$this->assertSession()->pageTextMatchesCount(1, '/does-not-exist/');
}
/**
* @covers ::pageTextContainsOnce
*/
public function testPageTextContainsOnce() {
$this->drupalLogin($this->drupalCreateUser());
// Visit a Drupal page that requires login.
$this->drupalGet('test-page');
$this->assertSession()->pageTextContainsOnce('Test page text.');
$this->expectException(ResponseTextException::class);
$this->expectExceptionMessage("Failed asserting that the page matches the pattern '/does\\-not\\-exist/ui' 1 time(s), 0 found.");
$this->assertSession()->pageTextContainsOnce('does-not-exist');
}
}
......@@ -56,6 +56,7 @@ public function setUp(): void {
/**
* @covers ::assertUniqueText
* @expectedDeprecation AssertLegacyTrait::assertUniqueText() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use $this->getSession()->pageTextContainsOnce() or $this->getSession()->pageTextMatchesCount() instead. See https://www.drupal.org/node/3129738
*/
public function testAssertUniqueText() {
$this->page->getText()->willReturn('foo bar bar');
......@@ -64,6 +65,7 @@ public function testAssertUniqueText() {
/**
* @covers ::assertUniqueText
* @expectedDeprecation AssertLegacyTrait::assertUniqueText() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use $this->getSession()->pageTextContainsOnce() or $this->getSession()->pageTextMatchesCount() instead. See https://www.drupal.org/node/3129738
*/
public function testAssertUniqueTextFail() {
$this->page->getText()->willReturn('foo bar bar');
......@@ -73,6 +75,7 @@ public function testAssertUniqueTextFail() {
/**
* @covers ::assertUniqueText
* @expectedDeprecation AssertLegacyTrait::assertUniqueText() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use $this->getSession()->pageTextContainsOnce() or $this->getSession()->pageTextMatchesCount() instead. See https://www.drupal.org/node/3129738
*/
public function testAssertUniqueTextUnknown() {
$this->page->getText()->willReturn('foo bar bar');
......@@ -82,6 +85,7 @@ public function testAssertUniqueTextUnknown() {
/**
* @covers ::assertUniqueText
* @expectedDeprecation AssertLegacyTrait::assertUniqueText() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use $this->getSession()->pageTextContainsOnce() or $this->getSession()->pageTextMatchesCount() instead. See https://www.drupal.org/node/3129738
*/
public function testAssertUniqueTextMarkup() {
$this->page->getText()->willReturn('foo bar bar');
......@@ -92,6 +96,7 @@ public function testAssertUniqueTextMarkup() {
/**
* @covers ::assertNoUniqueText
* @expectedDeprecation AssertLegacyTrait::assertNoUniqueText() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Instead, use $this->getSession()->pageTextMatchesCount() if you know the cardinality in advance, or $this->getSession()->getPage()->getText() and substr_count(). See https://www.drupal.org/node/3129738
*/
public function testAssertNoUniqueText() {
$this->page->getText()->willReturn('foo bar bar');
......@@ -100,6 +105,7 @@ public function testAssertNoUniqueText() {
/**
* @covers ::assertNoUniqueText
* @expectedDeprecation AssertLegacyTrait::assertNoUniqueText() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Instead, use $this->getSession()->pageTextMatchesCount() if you know the cardinality in advance, or $this->getSession()->getPage()->getText() and substr_count(). See https://www.drupal.org/node/3129738
*/
public function testAssertNoUniqueTextFail() {
$this->page->getText()->willReturn('foo bar bar');
......@@ -109,6 +115,7 @@ public function testAssertNoUniqueTextFail() {
/**
* @covers ::assertNoUniqueText
* @expectedDeprecation AssertLegacyTrait::assertNoUniqueText() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Instead, use $this->getSession()->pageTextMatchesCount() if you know the cardinality in advance, or $this->getSession()->getPage()->getText() and substr_count(). See https://www.drupal.org/node/3129738
*/
public function testAssertNoUniqueTextUnknown() {
$this->page->getText()->willReturn('foo bar bar');
......@@ -118,6 +125,7 @@ public function testAssertNoUniqueTextUnknown() {
/**
* @covers ::assertNoUniqueText
* @expectedDeprecation AssertLegacyTrait::assertNoUniqueText() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Instead, use $this->getSession()->pageTextMatchesCount() if you know the cardinality in advance, or $this->getSession()->getPage()->getText() and substr_count(). See https://www.drupal.org/node/3129738
*/
public function testAssertNoUniqueTextMarkup() {
$this->page->getText()->willReturn('foo bar bar');
......
......@@ -127,8 +127,6 @@ public static function getSkippedDeprecations() {
'AssertLegacyTrait::assertText() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use $this->assertSession()->responseContains() or $this->assertSession()->pageTextContains() instead. See https://www.drupal.org/node/3129738',
'AssertLegacyTrait::assertNoText() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use $this->assertSession()->responseNotContains() or $this->assertSession()->pageTextNotContains() instead. See https://www.drupal.org/node/3129738',
'AssertLegacyTrait::assertText() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use $this->assertSession()->pageTextContains() or $this->assertSession()->pageTextNotContains() instead. See https://www.drupal.org/node/3129738',
'AssertLegacyTrait::assertUniqueText() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use $this->getSession()->getPage()->getText() and substr_count() instead. See https://www.drupal.org/node/3129738',
'AssertLegacyTrait::assertNoUniqueText() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use $this->getSession()->getPage()->getText() and substr_count() instead. See https://www.drupal.org/node/3129738',
'AssertLegacyTrait::assertFieldByName() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use $this->assertSession()->fieldExists() or $this->assertSession()->buttonExists() or $this->assertSession()->fieldValueEquals() instead. See https://www.drupal.org/node/3129738',
'AssertLegacyTrait::assertNoFieldByName() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use $this->assertSession()->fieldNotExists() or $this->assertSession()->buttonNotExists() or $this->assertSession()->fieldValueNotEquals() instead. See https://www.drupal.org/node/3129738',
'AssertLegacyTrait::assertFieldById() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use $this->assertSession()->fieldExists() or $this->assertSession()->buttonExists() or $this->assertSession()->fieldValueEquals() instead. See https://www.drupal.org/node/3129738',
......
......@@ -12,7 +12,9 @@
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Constraint\ArrayHasKey;
use PHPUnit\Framework\Constraint\IsIdentical;
use PHPUnit\Framework\Constraint\LogicalNot;
/**
......@@ -101,6 +103,26 @@ public function responseHeaderDoesNotExist(string $name, string $message = ''):
Assert::assertThat($headers, $constraint, $message);
}
/**
* Asserts that the current page text matches regex a number of times.
*
* @param int $count
* The number of times the pattern is expected to be present.
* @param string $regex
* The regex pattern.
* @param string $message
* (Optional) the failure message.
*/
public function pageTextMatchesCount(int $count, string $regex, string $message = ''): void {
$actual = preg_replace('/\s+/u', ' ', $this->session->getPage()->getText());
$matches = preg_match_all($regex, $actual);
if ($message === '') {
$message = "Failed asserting that the page matches the pattern '$regex' $count time(s), $matches found.";
}
$constraint = new IsIdentical($count);
Assert::assertThat($matches, $constraint, $message);
}
/**
* Checks that specific button exists on the current page.
*
......@@ -705,22 +727,13 @@ public function pageTextContainsOnce($text) {
if (func_num_args() > 1) {
@trigger_error('Calling ' . __METHOD__ . ' with more than one argument is deprecated in drupal:9.1.0 and will throw an \InvalidArgumentException in drupal:10.0.0. See https://www.drupal.org/node/3162537', E_USER_DEPRECATED);
}
$actual = $this->session->getPage()->getText();
$actual = preg_replace('/\s+/u', ' ', $actual);
$regex = '/' . preg_quote($text, '/') . '/ui';
$count = preg_match_all($regex, $actual);
if ($count === 1) {
return;
try {
$this->pageTextMatchesCount(1, $regex);
}
if ($count > 1) {
$message = sprintf('The text "%s" appears in the text of this page more than once, but it should not.', $text);
}
else {
$message = sprintf('The text "%s" was not found anywhere in the text of the current page.', $text);
catch (AssertionFailedError $e) {
throw new ResponseTextException($e->getMessage(), $this->session->getDriver());
}
throw new ResponseTextException($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