function WebAssert::buildXPathQuery

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/WebAssert.php \Drupal\Tests\WebAssert::buildXPathQuery()
  2. 10 core/tests/Drupal/Tests/WebAssert.php \Drupal\Tests\WebAssert::buildXPathQuery()
  3. 11.x core/tests/Drupal/Tests/WebAssert.php \Drupal\Tests\WebAssert::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.

4 calls to WebAssert::buildXPathQuery()
JSWebAssert::buildJavascriptStatusMessageSelector in core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php
Builds a xpath selector for a message with given type and text.
WebAssert::buildStatusMessageSelector in core/tests/Drupal/Tests/WebAssert.php
Builds a xpath selector for a message with given type and text.
WebAssert::linkByHrefExists in core/tests/Drupal/Tests/WebAssert.php
Passes if a link containing a given href (part) is found.
WebAssert::linkByHrefNotExists in core/tests/Drupal/Tests/WebAssert.php
Passes if a link containing a given href (part) is not found.

File

core/tests/Drupal/Tests/WebAssert.php, line 450

Class

WebAssert
Defines a class with methods for asserting presence of elements during tests.

Namespace

Drupal\Tests

Code

public function buildXPathQuery($xpath, array $args = []) {
    // Replace placeholders.
    foreach ($args as $placeholder => $value) {
        if (is_object($value)) {
            throw new \InvalidArgumentException('Just pass in scalar values for $args and remove all t() calls from your test.');
        }
        // 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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.