You are here

protected function AssertContentTrait::buildXPathQuery in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/simpletest/src/AssertContentTrait.php \Drupal\simpletest\AssertContentTrait::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

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

array $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

string An XPath query with arguments replaced.

11 calls to AssertContentTrait::buildXPathQuery()
AssertContentTrait::constructFieldXpath in core/modules/simpletest/src/AssertContentTrait.php
Helper: Constructs an XPath for the given set of attributes and value.
AssertContentTrait::xpath in core/modules/simpletest/src/AssertContentTrait.php
Performs an xpath search on the contents of the internal browser.
BlockTest::moveBlockToRegion in core/modules/block/src/Tests/BlockTest.php
Moves a block to a given region via the UI and confirms the result.
BlockTest::testBlock in core/modules/block/src/Tests/BlockTest.php
Test configuring and moving a module-define block to specific regions.
EntityReferenceAdminTest::assertFieldSelectOptions in core/modules/field/src/Tests/EntityReference/EntityReferenceAdminTest.php
Checks if a select element contains the specified options.

... See full list

File

core/modules/simpletest/src/AssertContentTrait.php, line 178
Contains \Drupal\simpletest\AssertContentTrait.

Class

AssertContentTrait
Provides test methods to assert content.

Namespace

Drupal\simpletest

Code

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

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

    // Cast MarkupInterface objects to string.
    if (is_object($value)) {
      $value = (string) $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];
    }

    // Use preg_replace_callback() instead of preg_replace() to prevent the
    // regular expression engine from trying to substitute backreferences.
    $replacement = function ($matches) use ($value) {
      return $value;
    };
    $xpath = preg_replace_callback('/' . preg_quote($placeholder) . '\\b/', $replacement, $xpath);
  }
  return $xpath;
}