Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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:
- Install Drupal using a MySQL database
- 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 - 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
Comment | File | Size | Author |
---|---|---|---|
#12 | 2987124-12.patch | 4.26 KB | alexpott |
#12 | 7-12-interdiff.txt | 1.06 KB | alexpott |
#7 | 2987124-7.patch | 4.08 KB | alexpott |
Comments
Comment #2
alexpottComment #3
alexpottSo 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?
Comment #4
alexpottGiven the importance of knowing that this works I think we should use mocking and reflection to get at the protected method.
Comment #7
alexpottNice - 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.Comment #8
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedI 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?
Comment #9
alexpott@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.Comment #10
mondrakeAFAICS 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
Comment #11
plachGiven 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
Missing phpdoc?
I guess this would override valid environment values, however this shouldn't matter given that this is a unit test.
Comment #12
alexpott@plach thanks for the review. Patch attached improves the test commentary in light of points 2 and 3.
Comment #13
plachCommitted bacd657 and pushed to 8.7.x. Thanks!
Comment #16
alexpottLol rtbc re-test.
Comment #17
catch+1 backporting this to 8.6.x
Comment #19
plachCommitted e3458a7 and pushed to 8.6.x. Thanks!
Comment #20
neclimdulOoooooh 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!