protected function WebTestBase::buildXPathQuery

Builds an XPath query.

Builds an XPath query by replacing placeholders in the query by the value of the arguments.

XPath 1.0 (the version supported by libxml2, the underlying XML library used by PHP) doesn't support any form of quotation. This function simplifies the building of XPath expression.

Parameters

$xpath: An XPath query, possibly with placeholders in the form ':name'.

$args: An array of arguments with keys in the form ':name' matching the placeholders in the query. The values may be either strings or numeric values.

Return value

An XPath query with arguments replaced.

8 calls to WebTestBase::buildXPathQuery()
BlockTest::moveBlockToRegion in drupal/core/modules/block/lib/Drupal/block/Tests/BlockTest.php
Moves a block to a given region via the UI and confirms the result.
BlockTest::testBlock in drupal/core/modules/block/lib/Drupal/block/Tests/BlockTest.php
Test configuring and moving a module-define block to specific regions.
FieldWebTest::xpathContent in drupal/core/modules/views/lib/Drupal/views/Tests/Handler/FieldWebTest.php
Performs an xpath search on a certain content.
ForumTest::testForum in drupal/core/modules/forum/lib/Drupal/forum/Tests/ForumTest.php
Tests forum functionality through the admin and user interfaces.
MenuTestBase::assertBreadcrumb in drupal/core/modules/system/lib/Drupal/system/Tests/Menu/MenuTestBase.php
Assert that a given path shows certain breadcrumb links.

... See full list

File

drupal/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php, line 1669
Definition of Drupal\simpletest\WebTestBase.

Class

WebTestBase
Test case for typical Drupal tests.

Namespace

Drupal\simpletest

Code

protected function buildXPathQuery($xpath, array $args = array()) {

  // Replace placeholders.
  foreach ($args as $placeholder => $value) {

    // XPath 1.0 doesn't support a way to escape single or double quotes in a
    // string literal. We split double quotes out of the string, and encode
    // them separately.
    if (is_string($value)) {

      // Explode the text at the quote characters.
      $parts = explode('"', $value);

      // Quote the parts.
      foreach ($parts as &$part) {
        $part = '"' . $part . '"';
      }

      // Return the string.
      $value = count($parts) > 1 ? 'concat(' . implode(', \'"\', ', $parts) . ')' : $parts[0];
    }
    $xpath = preg_replace('/' . preg_quote($placeholder) . '\\b/', $value, $xpath);
  }
  return $xpath;
}