Follow-up to #1938068: Convert UnitTestBase to PHPUnit

Problem/Motivation

When I use a KernelTestBase and I use $this->xpath, I get a fatal error: Call to undefined method Drupal\system\Tests\Theme\ImageTest::getUrl() in /data/disk/o1/static/drupal8/core/modules/simpletest/src/AssertContentTrait.php on line 130

Reason is that $this->getRawContent() doesn't contain valid HTML, it only contains the output of $this->render.

CommentFileSizeAuthor
#11 2335057-11.patch762 bytesjhedstrom
#8 i2335057-8.patch1.71 KBattiks
#5 i2335057-5.patch919 bytesattiks

Comments

vijaycs85’s picture

Title: KernelTestBase does not support xpath and parse » Undefined method getUrl in AssertContentTrait::parse causes fatal error
Category: Task » Bug report
Status: Fixed » Active
vijaycs85’s picture

Looks like #2257519: Move content assertion methods into a trait, so DUTB can consume it, too has introduced the trait with undefined method and it works when we extend WebTestBase, but not with KernelTestBase.

vijaycs85’s picture

So this is common problem when we use trait. "if trait calls methods that are not part of trait, but assuming that the class uses trait will have" can produce this type of regressions. one option is adding abstract methods as per http://stackoverflow.com/a/14866251/1366019

--- a/core/modules/simpletest/src/AssertContentTrait.php
+++ b/core/modules/simpletest/src/AssertContentTrait.php
@@ -1240,4 +1240,49 @@ protected function constructFieldXpath($attribute, $value) {
     return $this->buildXPathQuery($xpath, array(':value' => $value));
   }

+  abstract protected function pass($message = NULL, $group = 'Other');
+
+  abstract protected function fail($message = NULL, $group = 'Other');
+
+  abstract protected function getUrl();
 }

for this issue point of view, we can remove getUrl as it is not valid for KernalTestBase.

diff --git a/core/modules/simpletest/src/AssertContentTrait.php b/core/modules/simpletest/src/AssertContentTrait.php
index 4b9a4a7..828debf 100644
--- a/core/modules/simpletest/src/AssertContentTrait.php
+++ b/core/modules/simpletest/src/AssertContentTrait.php
@@ -127,7 +127,7 @@ protected function parse() {
       $html_dom = new \DOMDocument();
       @$html_dom->loadHTML('<?xml encoding="UTF-8">' . $this->getRawContent());
       if ($html_dom) {
-        $this->pass(String::format('Valid HTML found on "@path"', array('@path' => $this->getUrl())), 'Browser');
+        $this->pass(String::format('Given page has valid HTML', array()), 'Browser');
         // It's much easier to work with simplexml than DOM, luckily enough
         // we can just simply import our DOM tree.
         $this->elements = simplexml_import_dom($html_dom);
attiks’s picture

We could also make sure that $this->getRawContent() contains valid html, we could wrap the rendered output inside <html><body>, see KernelTestBase->render

attiks’s picture

Status: Active » Needs review
StatusFileSize
new919 bytes

Patch to see how testbot will react on the removal

Status: Needs review » Needs work

The last submitted patch, 5: i2335057-5.patch, failed testing.

vijaycs85’s picture

Nice, so we do test out tests :)

attiks’s picture

Status: Needs work » Needs review
StatusFileSize
new1.71 KB

Let's see if this works

Status: Needs review » Needs work

The last submitted patch, 8: i2335057-8.patch, failed testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jhedstrom’s picture

Version: 8.1.x-dev » 8.2.x-dev
Status: Needs work » Needs review
StatusFileSize
new762 bytes

A related fatal error occurs when using AssertContentTrait with BrowserTestBase tests:

1) Drupal\Tests\message_subscribe_email\Functional\ViewsTest::testViews
Error: Call to undefined method Drupal\Tests\message_subscribe_email\Functional\ViewsTest::pass()

The patch from #8 no longer applies, so here's a partial reroll. PHPUnit doesn't appear to have a related method to 'pass', so just removing it seems easiest. Alternatively, we'd need to add this method into our base classes.

jhedstrom’s picture

That patch just moves on to a different error. I don't think the existing AssertContentTrait is going to be compatible with BrowserTestBase.

Status: Needs review » Needs work

The last submitted patch, 11: 2335057-11.patch, failed testing.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

quietone’s picture

Component: simpletest.module » phpunit

Triaging issues in simpletest.module as part of the Bug Smash Initiative to determine if they should be in the Simpletest Project or core.

This looks like it a Phpunit issue, changing component.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

longwave’s picture

Status: Needs work » Closed (outdated)
Issue tags: +Bug Smash Initiative

The original report here was solved by #2387019: String field formatters cannot link to their parent entity, see comment #11 where getUrl() is explicitly mentioned.

Since then this has been refactored away and following #3135077: Remove usage of AssertLegacyTrait::pass() from traits getUrl() is no longer called at all from AssertContentTrait.

Therefore, closing as outdated.