Problem/Motivation

The site-under-test will use the database settings in settings.php and not those from the SIMPLETEST_DB. This is a major bug because you think you are testing against one database and in reality your testing against another. If you have no existing settings.php the correct database is used - which is fortunate because that's how DrupalCI is working.

Steps to reproduce:

  1. Install Drupal using a MySQL database
  2. Run SIMPLETEST_DB="pgsql://username:password@127.0.0.1/drupal8alt" sudo -u _www ./vendor/bin/phpunit -v -c core core/modules/system/tests/src/Functional/System/StatusTest.php --filter testStatusPage ensuring that browser output is set up correctly
  3. View last browser output page generated by the test - you will see the site status report and the DB Driver will be MySQL.

Delete your settings.php and repeats steps to reproduce and you'll see that Postgres is now being used!??!?!?

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alexpott created an issue. See original summary.

alexpott’s picture

Issue summary: View changes
alexpott’s picture

Status: Active » Needs review
FileSize
795 bytes

So fixing it is simple - and makes sense if you look at the \Drupal\Core\Database\Database::addConnectionInfo() code - it won't add anything if the connection is already there.

How to test?

alexpott’s picture

The last submitted patch, 3: 2987124-3.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 4: 2987124-4.patch, failed testing. View results

alexpott’s picture

Nice - our upgrade path tests only work because whilst we call changeDatabasePrefix() twice because we're not removing the db connection everything works. We shouldn't be calling this twice. In HEAD we do once from \Drupal\FunctionalTests\Update\UpdatePathTestBase::setUp() and then later in the same method we call prepareEnvironment() which also calls it.

amateescu’s picture

+++ b/core/lib/Drupal/Core/Test/TestSetupTrait.php
@@ -158,6 +158,9 @@ protected function changeDatabasePrefix() {
       Database::addConnectionInfo('default', 'default', $database);

I wonder if it would be cleaner to use a 'test_run' database connection rather than 'default', and then call Database::setActiveConnection('test_run');.

What do you think?

alexpott’s picture

@amateescu that feels like way too big a change. For example if we did that any call to \Drupal\Core\Database\Database::getConnectionInfo() in tests will need to cater for this.

mondrake’s picture

AFAICS this is good to go. It's a bit of an hidden issue, but I could figure out in TravisCI builds where I install Drupal before running tests that a) the issue was there (I install Drupal with a db driver, than change the db driver to run tests and, in case of failure of functional tests, then the backtrace was indicating the db driver in use was the installation one) and b) this patch forces the test installer to use the driver setup passed by the SIMPLETEST_DB variable.

RTBC

plach’s picture

  1. +++ b/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBase.php
    @@ -161,7 +161,15 @@ protected function setUp() {
    +    $this->prepareEnvironment();
    

    Given what ::prepareEnvironment() is doing, it might be even beneficial to move it here, as we will get a pristine environment and possibly better error logging during DB setup.

    +1

  2. +++ b/core/tests/Drupal/Tests/Core/Test/TestSetupTraitTest.php
    @@ -0,0 +1,42 @@
    +  public function testChangeDatabasePrefix() {
    

    Missing phpdoc?

  3. +++ b/core/tests/Drupal/Tests/Core/Test/TestSetupTraitTest.php
    @@ -0,0 +1,42 @@
    +    putenv('SIMPLETEST_DB=pgsql://user:pass@127.0.0.1/db');
    

    I guess this would override valid environment values, however this shouldn't matter given that this is a unit test.

alexpott’s picture

@plach thanks for the review. Patch attached improves the test commentary in light of points 2 and 3.

plach’s picture

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

Committed bacd657 and pushed to 8.7.x. Thanks!

  • plach committed bacd657 on 8.7.x
    Issue #2987124 by alexpott, mondrake: Functional tests don't use...

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 12: 2987124-12.patch, failed testing. View results

alexpott’s picture

Status: Needs work » Reviewed & tested by the community

Lol rtbc re-test.

catch’s picture

+1 backporting this to 8.6.x

  • plach committed e3458a7 on 8.6.x
    Issue #2987124 by alexpott, mondrake: Functional tests don't use...
plach’s picture

Status: Reviewed & tested by the community » Fixed

Committed e3458a7 and pushed to 8.6.x. Thanks!

neclimdul’s picture

Ooooooh I thought I saw some tests running in the wrong database a while back but it didn't cause enough problem for me to dig in to it. It suddenly makes sense, it was broken! Awesome catch, thanks!

Status: Fixed » Closed (fixed)

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