You are here

public function BrowserTest::testXPathEscaping in Drupal 8

Tests XPath escaping.

File

core/modules/simpletest/src/Tests/BrowserTest.php, line 63

Class

BrowserTest
Tests the internal browser of the testing framework.

Namespace

Drupal\simpletest\Tests

Code

public function testXPathEscaping() {
  $testpage = <<<EOF
<html>
<body>
<a href="link1">A "weird" link, just to bother the dumb "XPath 1.0"</a>
<a href="link2">A second "even more weird" link, in memory of George O'Malley</a>
<a href="link3">A \$third\$ link, so weird it's worth \$1 million</a>
<a href="link4">A fourth link, containing alternative \\1 regex backreferences \\2</a>
</body>
</html>
EOF;
  $this
    ->setRawContent($testpage);

  // Matches the first link.
  $urls = $this
    ->xpath('//a[text()=:text]', [
    ':text' => 'A "weird" link, just to bother the dumb "XPath 1.0"',
  ]);
  $this
    ->assertEqual($urls[0]['href'], 'link1', 'Match with quotes.');
  $urls = $this
    ->xpath('//a[text()=:text]', [
    ':text' => 'A second "even more weird" link, in memory of George O\'Malley',
  ]);
  $this
    ->assertEqual($urls[0]['href'], 'link2', 'Match with mixed single and double quotes.');
  $urls = $this
    ->xpath('//a[text()=:text]', [
    ':text' => 'A $third$ link, so weird it\'s worth $1 million',
  ]);
  $this
    ->assertEqual($urls[0]['href'], 'link3', 'Match with a regular expression back reference symbol (dollar sign).');
  $urls = $this
    ->xpath('//a[text()=:text]', [
    ':text' => 'A fourth link, containing alternative \\1 regex backreferences \\2',
  ]);
  $this
    ->assertEqual($urls[0]['href'], 'link4', 'Match with another regular expression back reference symbol (double backslash).');
}