Problem/Motivation

The documentation on how to run phpunit with a different user on Linux is incorrect.
The exported variables are nor preserved when switching to the different user, so the tests do fail:

Exception: You must provide a SIMPLETEST_BASE_URL environment variable to run some PHPUnit based functional tests.

The documented way:

> export SIMPLETEST_DB='mysql://root@localhost/dev_d8'
> export SIMPLETEST_BASE_URL='http://d8.dev'
> sudo -u www-data ./vendor/bin/phpunit -c core --testsuite functional
> sudo -u www-data ./vendor/bin/phpunit -c core --testsuite functional-javascript

This fails, as the declared environment variables $SIMPLETEST_DB and $SIMPLETEST_BASE_URL are not preserved when switching to www-data. The following command checks for the existence of the environment variables after switching the user:

> export SIMPLETEST_DB='mysql://root@localhost/dev_d8'
> export SIMPLETEST_BASE_URL='http://d8.dev'
> sudo -u www-data printenv | grep 'SIMPLETEST_DB\|SIMPLETEST_BASE_URL'

(Nothing is found)

Proposed resolution

Preserve the environment varibles when switching to the different user:

> export SIMPLETEST_DB='mysql://root@localhost/dev_d8'
> export SIMPLETEST_BASE_URL='http://d8.dev'
> sudo -u www-data -E ./vendor/bin/phpunit -c core --testsuite functional
> sudo -u www-data -E ./vendor/bin/phpunit -c core --testsuite functional-javascript

The following command checks for the existence of the environment variables after switching the user and preserving the environmment:

> export SIMPLETEST_DB='mysql://root@localhost/dev_d8'
> export SIMPLETEST_BASE_URL='http://d8.dev'
> sudo -u www-data -E printenv | grep 'SIMPLETEST_DB\|SIMPLETEST_BASE_URL'
SIMPLETEST_DB=mysql://root@localhost/dev_d8
SIMPLETEST_BASE_URL=http://d8.dev

or more explicit:

> export SIMPLETEST_DB='mysql://root@localhost/dev_d8'
> export SIMPLETEST_BASE_URL='http://d8.dev'
> sudo -u www-data SIMPLETEST_BASE_URL=$SIMPLETEST_BASE_URL SIMPLETEST_DB=$SIMPLETEST_DB ./vendor/bin/phpunit -c core --testsuite functional
> sudo -u www-data SIMPLETEST_BASE_URL=$SIMPLETEST_BASE_URL SIMPLETEST_DB=$SIMPLETEST_DB ./vendor/bin/phpunit -c core --testsuite functional-javascript

The following command checks for the existence of the environment variables after switching the user and explicitly passing the exported environment variables:

> export SIMPLETEST_DB='mysql://root@localhost/dev_d8'
> export SIMPLETEST_BASE_URL='http://d8.dev'
> sudo -u www-data SIMPLETEST_BASE_URL=$SIMPLETEST_BASE_URL SIMPLETEST_DB=$SIMPLETEST_DB printenv | grep 'SIMPLETEST_DB\|SIMPLETEST_BASE_URL'
SIMPLETEST_BASE_URL=http://d8.dev
SIMPLETEST_DB=mysql://root@localhost/dev_d8

Remaining tasks

review

User interface changes

none

API changes

none

Data model changes

none

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sanduhrs created an issue. See original summary.

sanduhrs’s picture

Title: Documentad way of running phpunit with a different user does not preserve environment » Environment is not preserved running phpunit with a different user
sanduhrs’s picture

The attached patch preserves the full environment.

sanduhrs’s picture

Status: Active » Needs review
dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Interesting! Personally I'm lazy, so I copy the phpunit.xml.dist file to phpunit.xml and set the environment variables there. I guess though for continues integration systems that's not really a good option.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

As a docs only change this is rc eligible.

Committed and pushed bff6791 to 8.3.x and 1c31625 to 8.2.x. Thanks!

  • alexpott committed bff6791 on 8.3.x
    Issue #2799053 by sanduhrs: Environment is not preserved running phpunit...

  • alexpott committed 1c31625 on 8.2.x
    Issue #2799053 by sanduhrs: Environment is not preserved running phpunit...
alexpott’s picture

BTW I do this differently and set up env_keep in my sudoers...

Defaults        env_keep += "SIMPLETEST_BASE_URL"
Defaults        env_keep += "SIMPLETEST_DB"
Defaults        env_keep += "BROWSERTEST_OUTPUT_DIRECTORY"

Status: Fixed » Needs work

The last submitted patch, 3: environment_is_not-2799053-3.patch, failed testing.

  • alexpott committed bff6791 on 8.4.x
    Issue #2799053 by sanduhrs: Environment is not preserved running phpunit...

  • alexpott committed bff6791 on 8.4.x
    Issue #2799053 by sanduhrs: Environment is not preserved running phpunit...

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.

sun’s picture

Status: Needs work » Fixed

This change has been performed for recent versions already:
http://cgit.drupalcode.org/drupal/tree/core/tests/README.md#n46

There is no need to backport this further.

Status: Fixed » Closed (fixed)

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