Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Chi created an issue. See original summary.

claudiu.cristea’s picture

Status: Needs review » Needs work
+++ b/core/modules/simpletest/src/AssertContentTrait.php
@@ -220,7 +220,7 @@ protected function buildXPathQuery($xpath, array $args = array()) {
+   * @return \SimpleXMLElement[]|bool
    *   The return value of the xpath search. For details on the xpath string
    *   format and return values see the SimpleXML documentation,
    *   http://php.net/manual/function.simplexml-element-xpath.php.

The let's adjust also the description of the return value, telling that we might return also FALSE.

If we already touch the function, we might want to cleanup also the function?

@@ -234,9 +234,7 @@
       // to an empty array to allow foreach(...) constructions.
       return $result ? $result : array();
     }
-    else {
-      return FALSE;
+    return FALSE;
-    }
   }
Chi’s picture

Chi’s picture

Title: Wrong return type on AssertContentTrait::xpath() » Cleanup AssertContentTrait::xpath()
Category: Bug report » Task
Status: Needs work » Needs review
claudiu.cristea’s picture

  1. +++ b/core/modules/simpletest/src/AssertContentTrait.php
    @@ -220,23 +220,21 @@ protected function buildXPathQuery($xpath, array $args = array()) {
    +   * @return \SimpleXMLElement[]
        *   The return value of the xpath search. For details on the xpath string
        *   format and return values see the SimpleXML documentation,
        *   http://php.net/manual/function.simplexml-element-xpath.php.
    

    The first concern was not addressed. The help txt needs to tell also about the FALSE return.

  2. +++ b/core/modules/simpletest/src/AssertContentTrait.php
    @@ -220,23 +220,21 @@ protected function buildXPathQuery($xpath, array $args = array()) {
    +      return $result ? : [];
    

    "? :" should be "?:" (no space between them).

claudiu.cristea’s picture

Status: Needs review » Needs work
+++ b/core/modules/simpletest/src/AssertContentTrait.php
@@ -220,23 +220,21 @@ protected function buildXPathQuery($xpath, array $args = array()) {
+    return [];

Oh, no. We cannot change this from FALSE to empty array. This may break a lot of tests. We still need FALSE there. We are not changing the interface here, we just cleanup docs, and additionally the code.

Chi’s picture

This may break a lot of tests.

testbot stated it will not

PHP 5.5 & MySQL 5.5 14,214 pass

The comment inside the function says:

Forcefully convert any falsish values to an empty array to allow foreach(...) constructions.

Despite the comment the function still returns FALSE in some cases. That makes it inconsistent. Why not fix it at once?

claudiu.cristea’s picture

Despite the comment the function still returns FALSE in some cases. That makes it inconsistent. Why not fix it at once?

That comment is inside if(), so it doesn't cover the case of FALSE.

Chi’s picture

I meant that it is not possible to pass the return value directly to foreach construction because it might be boolean. That makes the conversion to array useless. The caller must always check the type of return value before looping. We could simplify this a little.

claudiu.cristea’s picture

Sorry, FALSE is different than []. First is telling that a we have a failure (aka "we weren't able to parse"), the 2nd tells that a successful search occurred but there are no results. Totally different cases.

Chi’s picture

Totally different cases.

I agree, but for caller there is no way to predict which case will happen. So that the caller code should always check if return value is not FALSE before looping. That makes the first array conversion useless.

However since the matter is of a little consequence, I made patches for both points.

claudiu.cristea’s picture

Status: Needs review » Needs work

The review is against 2587229-boolean.patch.

  1. +++ b/core/modules/simpletest/src/AssertContentTrait.php
    @@ -220,23 +220,23 @@ protected function buildXPathQuery($xpath, array $args = array()) {
    +   * @return \SimpleXMLElement[]|boolean
    

    The data type should be declared as bool (not boolean). See https://www.drupal.org/coding-standards/docs#types.

  2. +++ b/core/modules/simpletest/src/AssertContentTrait.php
    @@ -220,23 +220,23 @@ protected function buildXPathQuery($xpath, array $args = array()) {
    +   *   The return value of the xpath search or FALSE on failure.
    +   *   For details on the xpath string format and return values see
    +   *   the SimpleXML documentation.
    

    The wrapping is incorrect. The second phrase should continue right when the first ends. The existing wrapping is correct. Don't break lines after phrase ends, but only on 80 chars.

claudiu.cristea’s picture

Status: Needs review » Reviewed & tested by the community

Looks good. Thank you!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 14: 2587229-14-boolean.patch, failed testing.

The last submitted patch, 14: 2587229-14-boolean.patch, failed testing.

Chi’s picture

Status: Needs work » Needs review
dawehner’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +rc eligible

nice small cleanup

This is rc eligible as it just changes tests.

  • alexpott committed 7b4baac on 8.1.x
    Issue #2587229 by Chi, claudiu.cristea: Cleanup AssertContentTrait::...
alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Based on @xjm's post release triage document, committed 01387f8 and pushed to 8.0.x and 8.1.x. Thanks!

  • alexpott committed 01387f8 on 8.0.x
    Issue #2587229 by Chi, claudiu.cristea: Cleanup AssertContentTrait::...

Status: Fixed » Closed (fixed)

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