Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Env: WP_TESTS_DOMAIN constant in phpunit-wp-config.php is defined to invalid value #41038

Closed
westonruter opened this issue May 13, 2022 · 1 comment · Fixed by #41039
Closed
Labels
[Tool] Env /packages/env [Type] Bug An existing feature does not function as intended

Comments

@westonruter
Copy link
Member

westonruter commented May 13, 2022

Description

Apologies if this is not the right place to report this, but I'm having trouble finding the repo responsible for the /var/www/html/phpunit-wp-config.php in the wp-env Docker container. My file is including the following constants:

define( 'SCRIPT_DEBUG', false );
define( 'WP_ENVIRONMENT_TYPE', 'local' );
define( 'WP_PHP_BINARY', 'php' );
define( 'WP_TESTS_EMAIL', 'admin@example.org' );
define( 'WP_TESTS_TITLE', 'Test Blog' );
define( 'WP_TESTS_DOMAIN', 'http://localhost:8889/' );
define( 'WP_SITEURL', 'http://localhost:8889/' );
define( 'WP_HOME', 'http://localhost:8889/' );
define( 'WP_DEBUG', false );

The WP_TESTS_DOMAIN here is wrong as it should be just the host name, specifically localhost. This is causing my unit tests to fail which are attempting to use $_SERVER['HTTP_HOST'] to construct the current URL, resulting in a malformed http://http://localhost:8889//current-page/ when I expect http://localhost:8889/current-page/. In the WordPress core tests function tests_reset__SERVER(), it does:

$_SERVER['HTTP_HOST']       = WP_TESTS_DOMAIN;

Where are these constants being generated? I'm guessing somehow via build-docker-compose-config.js? I tried overriding this by constructing a .wp-env.json that had:

{
  "config": {
    "WP_TESTS_DOMAIN":  "localhost"
  }
}

But then running start failed with:

✖ Invalid .wp-env.json: config.WP_TESTS_DOMAIN must be a valid URL.

This error appears to be due to this line:

// Update wp config options to include the correct port number in the URL.
updateEnvUrl( 'WP_TESTS_DOMAIN' );

Since WP_TESTS_DOMAIN is in fact not a URL but rather a hostname.

Step-by-step reproduction instructions

Start an environment and check $_SERVER['HTTP_HOST'] and the value of WP_TESTS_DOMAIN defined in the wordpress-tests container file /var/www/html/phpunit-wp-config.php. The string will be erroneously a full URL rather than a hostname.

Screenshots, screen recording, code snippet

No response

Environment info

No response

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@westonruter
Copy link
Member Author

westonruter commented May 13, 2022

Something else I'm seeing is that SERVER_PORT is unexpectedly being set to 80 when it should rather be 8889. This seems to be the fault of tests_reset__SERVER() in core. Maybe that should also be set here:

const updateEnvUrl = ( configKey ) => {
[ 'development', 'tests' ].forEach( ( envKey ) => {
try {
const baseUrl = new URL(
config.env[ envKey ].config[ configKey ]
);
// Don't overwrite the port of WP_HOME when set.
if ( ! ( configKey === 'WP_HOME' && !! baseUrl.port ) ) {
baseUrl.port = config.env[ envKey ].port;
}
config.env[ envKey ].config[ configKey ] = baseUrl.toString();
} catch ( error ) {
throw new ValidationError(
`Invalid .wp-env.json: config.${ configKey } must be a valid URL.`
);
}
} );
};
// Update wp config options to include the correct port number in the URL.
updateEnvUrl( 'WP_TESTS_DOMAIN' );
updateEnvUrl( 'WP_SITEURL' );
updateEnvUrl( 'WP_HOME' );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Tool] Env /packages/env [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant