Problem/Motivation

\Drupal\Tests\WebAssert::buildXPathQuery has some protection against non scalar values passed into $args.
There are bits in core which passes in the result of t() calls, which is an object now.

Proposed resolution

Cast the object to a string when

 (method_exists($arg, '__toString')) {
         (string) $arg;
      }

Remaining tasks

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner created an issue. See original summary.

GoZ’s picture

Status: Active » Needs review
FileSize
1.27 KB

I think you are talking about value of args. $args is supposed to be an array, but values of args can be an object (and shouldn't).

dawehner’s picture

At some point we actually decided to put these things into the BC layer only, so we keep our behaviour as vanilla mink as possible. Most of the time actually calling to t() in tests don't really make sense anyway, and you can use the literal string.

Lendude’s picture

@dawehner yeah I agree, moving this fix to the BC layer makes sense to me if that is an option here.

dawehner’s picture

So what should we do about this one?

klausi’s picture

Hm, I think we should close this as won't fix. Instead of polluting our buildXPathQuery() method we should remove the t() call in the actual test.

dawehner’s picture

I agree, maybe though we could improve documentation to make it clear what/how we support things?

klausi’s picture

Title: Cast Markup objects to a string in \Drupal\Tests\WebAssert::buildXPathQuery » Document that only plain string values are allowed in \Drupal\Tests\WebAssert::buildXPathQuery
Status: Needs review » Needs work

OK, documentation improvement sounds good.

valthebald’s picture

WebAssert::buildXPathQuery() annotation already specifies that type of $xpath is string. Any other documentation change needed?

klausi’s picture

Yes, the exception message should say something like "Just pass in scalar values for $xpath and remove all t() calls from your test."

cebasqueira’s picture

Status: Needs work » Needs review
Issue tags: +ciandt-contrib
FileSize
954 bytes
klausi’s picture

Status: Needs review » Needs work
+++ b/core/tests/Drupal/Tests/WebAssert.php
@@ -320,7 +320,13 @@ public function buildXPathQuery($xpath, array $args = array()) {
+        // Cast Markup objects to a string.
+        if (method_exists($value, '__toString')) {
+          $value = (string) $value;
+        }

no, we should no cast the markup object. This patch should only update the exception message, right?

cebasqueira’s picture

cebasqueira’s picture

Status: Needs work » Needs review
klausi’s picture

Status: Needs review » Needs work
+++ b/core/tests/Drupal/Tests/WebAssert.php
@@ -320,7 +320,7 @@ public function buildXPathQuery($xpath, array $args = array()) {
     foreach ($args as $placeholder => $value) {
       if (is_object($value)) {
-        throw new \InvalidArgumentException('Just pass in scalar values.');
+        throw new \InvalidArgumentException('Just pass in scalar values for $xpath and remove all t() calls from your test.');

Ah sorry, of course we are iterating over $args and not $xpath. So it should be "Just pass in scalar values for $args and remove all t() calls from your test."

cebasqueira’s picture

Status: Needs work » Needs review
FileSize
778 bytes
dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Works for me.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 16ddd38 and pushed to 8.3.x. Thanks!

Less unnecessary t() calls in tests will be a nice thing.

  • alexpott committed 16ddd38 on 8.3.x
    Issue #2795543 by cebasqueira, GoZ: Document that only plain string...
alexpott’s picture

  • alexpott committed 16ddd38 on 8.4.x
    Issue #2795543 by cebasqueira, GoZ: Document that only plain string...

  • alexpott committed 16ddd38 on 8.4.x
    Issue #2795543 by cebasqueira, GoZ: Document that only plain string...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.