\Drupal\user\Tests\UserRegistrationTest::testUuidFormState() has the following line (in 2 places):

$this->assertTrue($user_storage->loadByProperties(['name' => $edit['name']]));

The aim is to check whether a user was successfully loaded with a given name. assertTrue() then does:

  protected function assertTrue($value, $message = '', $group = 'Other') {
    return $this->assert((bool) $value, $message ? $message : SafeMarkup::format('Value @value is TRUE.', array('@value' => var_export($value, TRUE))), $group);
  }

Given that $value is an array of user objects this not only creates a fairly nasty message for the assertion, but also risks circular reference errors.

Specifically, we have used dependency injection to swap out the entity class and we then perform some action in ::postLoad() which reads from a base field on the entity. This causes it to load the field definitions and set ::$typedData, which contains ::$entity - a recursive reference. The var_export() in assertTrue() then uses a huge amount of memory before eventually fatally failing with this trace:

var_export does not handle circular references

var_export(Array, 1)
Drupal\simpletest\TestBase->assertTrue(Array)
Drupal\user\Tests\UserRegistrationTest->testUuidFormState()
Drupal\simpletest\TestBase->run()
_simpletest_batch_operation(Array, '4', Array)
call_user_func_array('_simpletest_batch_operation', Array)
_batch_process()
_batch_do()
_batch_page(Object)
Drupal\system\Controller\BatchController->batchPage(Object)
call_user_func_array(Array, Array)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1)
Stack\StackedHttpKernel->handle(Object, 1, 1)
Drupal\Core\DrupalKernel->handle(Object)
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

andrewbelcher created an issue. See original summary.

dawehner’s picture

Yeah we should totally do assertTrue($object !== FALSE) instead or even fix assertTrue to not show objects?

andrewbelcher’s picture

Status: Active » Needs review
FileSize
1.15 KB

Here's a patch I've written that solves it (for this specific case). @dawehner's suggestion of fixing it in assertTrue() would mean it also solves it for other potential cases, but this one solves it simply by providing $message and therefore avoiding the var_export() (and also making the message nicer).

I'm making use of SafeMarkup::format() as that is what is in assertTrue(), though I'm aware it's deprecated. I wasn't sure what I was supposed to use instead, but if someone wants to point me in the right direction I'm very happy to update the patch.

I don't know if we need test coverage for this, it depends whether using assertTrue() for this kind of case is really supported? If it's needed, let me know where it should live and I can have a bash.

As it's an array, the other option is to do either assertTrue(!empty(...)) or assertTrue(count(...)). Again, happy to re-do patch if an alternative solution is preferred.

yanniboi’s picture

Status: Needs review » Reviewed & tested by the community

Like @andrewbelcher said, fixing assertTrue() would probably be beneficial, however also more risky.

The patch works for me and is fairly non-intrusive, so I'm marking this RTBC. Feel free to change if anyone disagrees.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 3: 2632662-pass-message.patch, failed testing.

andrewbelcher’s picture

Status: Needs work » Needs review

Not sure why that failed having passed multiple times before, try it again...

andrewbelcher’s picture

Status: Needs review » Reviewed & tested by the community

As it passed...

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

re #3

   * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
   *   Use \Drupal\Component\Render\FormattableMarkup.

So we should be using FormattableMarkup instead. Thanks.

andrewbelcher’s picture

Status: Needs work » Needs review
FileSize
1.15 KB
2.35 KB

I've attached to patches. One just replaces SafeMarkup with FormattableMarkup where I'm making changes. The other replaces all uses.

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.

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

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should 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.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should 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.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should 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.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should 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.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should 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.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.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.

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

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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

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

The use of assertTrue in UserRegistrationTest was changed/fixed in #2742585: Deprecate dangerous assertTrue/False() compatibility overrides in PHPUnit Kernel, Functional and FunctionalJavascript tests. The patch also changes two assertRaw, and those changes have already occurred in #2958429: Properly deprecate SafeMarkup::format().

Therefore, closing as outdated. If this is incorrect reopen the issue, by setting the status to 'Active', and add a comment explaining what still needs to be done.

Thanks!