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

Add Port To WP_TESTS_DOMAIN #49883

Merged
merged 9 commits into from
Apr 20, 2023
Merged

Add Port To WP_TESTS_DOMAIN #49883

merged 9 commits into from
Apr 20, 2023

Conversation

ObliviousHarmony
Copy link
Contributor

What?

This PR adds the configured port to WP_TESTS_DOMAIN so that tests referencing 'http://' . WP_TESTS_DOMAIN . /path will work with a port other than 80.

Why?

I am currently working on running unit tests within the container, however, I have run into problems because the URLs generated by WordPress contain the port while WP_TESTS_DOMAIN does not. By adding the port to the constant we are able to make tests more resilient and better support environment customization.

I have looked at WordPress' tests and there doesn't seem to be anything wrong with just adding the port to the constant. This also removes the need for the port in Gutenberg's tests, which this PR removes.

How?

This pull request refactors the way that ports are added to configuration options. This let me write some nice tests for different cases and handle inputs that aren't URLs. To keep the validation in place I have added the URL validation to the config validation function instead.

In the process of making these changes I also noticed that the --url being passed to WordPress' installation command has the port included twice. There's no reason to use anything other than WP_SITEURL since the constant will override it anyway. It will have the correct port and is the most accurate input to use here.

Testing Instructions

  1. Passing CI is a great indicator that the test-related functionality in this PR works.
  2. Try running the PHPUnit suite locally with a .wp-env.override.json file that changes the port of the test container.
  3. Make sure npm run env start works with different ports and that the containers work as you'd expect them to (correct ports in URLs, etc).

This new function will allow us to add/replace ports in
certain `wp-config.php` values.
Since `WP_SITEURL` already contains the port in both the
development and test environments, there is no reason to
try and construct the URL with the port. Additionally, it is
more accurate to use the test environment's `WP_SITEURL`
since that's what WordPress will expect due to the constant.
Tests should use the `WP_TESTS_DOMAIN` constant to
construct URLs in tests. This poses a problem, however,
because the port will be part of the URL in test environments.
In my review of WordPress Core and WooCommerce, adding
the port to `WP_TESTS_DOMAIN` should not break anything
and will make the container more resilient to port customizations.
To add resiliency to the test suite, we should use `WP_TESTS_DOMAIN`
instead of hardcoding the domain and port in the URLs.
@@ -14,10 +14,10 @@ exports[`readConfig config file should match snapshot 1`] = `
"TEST_VAL3": false,
"WP_DEBUG": true,
"WP_ENVIRONMENT_TYPE": "local",
"WP_HOME": "http://localhost:2000/",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, we aren't supposed to put trailing slashes on these constants. They're actually removed automatically by WordPress.. Since my new port addition function doesn't add a trailing slash, we don't have one here, but that's okay.

@@ -44,20 +44,7 @@ async function checkDatabaseConnection( { dockerComposeConfigPath, debug } ) {
* @param {Object} spinner A CLI spinner which indicates progress.
*/
async function configureWordPress( environment, config, spinner ) {
const url = ( () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it just me or was this previously setting http://localhost:8888:8888 for the development instance? Technically the port is already on WP_SITEURL here because of updateEnvUrl right? For what it's worth, in my testing of WP_TESTS_DOMAIN with a port (without this PR), it seems like WordPress ignores the second port entirely. Resiliency!

@noahtallen
Copy link
Member

I think this might conflict with a previous PR here: #49082:

I found that unit tests were being run with WP_TESTS_DOMAIN (and thus $_SERVER['HTTP_HOST']) being set to a URL http://localhost:8889/ when it should be limited to just the hostname: localhost.

That suggests we shouldn't add a port to wp_tests_domain 🤔

@ObliviousHarmony
Copy link
Contributor Author

Looking at the PR that I think you meant to link to, of note is the example construction that they gave:

$url = ( is_ssl() ? 'https://' : 'http:// ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

The problem was that URLs given by the site ended up being http://http://localhost/whatever. This was only because WP_TESTS_DOMAIN was being used to set the site URL for test containers, but, I'll note that even now the port is still being added (otherwise the URLs wouldn't work!). This just makes the addition of the port transparent and ensures any tests relying on WP_TESTS_DOMAIN have the full URL being used by WordPress instead of just the hostname.

@@ -18,14 +18,14 @@ class Tests_Blocks_Render_File extends WP_UnitTestCase {
*/
public function test_render_block_core_file() {
$attributes = array(
'href' => 'http://localhost:8889/wp-content/uploads/2021/04/yolo.pdf',
'href' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2021/04/yolo.pdf',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice change!

Copy link
Member

@noahtallen noahtallen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I'm very happy that this makes our URL handling much more resilient and consistent.

@noahtallen noahtallen merged commit b8733ad into WordPress:trunk Apr 20, 2023
@github-actions github-actions bot added this to the Gutenberg 15.7 milestone Apr 20, 2023
@ObliviousHarmony ObliviousHarmony deleted the fix/wp-env-tests-domain branch April 20, 2023 23:22
@bph bph added [Type] Bug An existing feature does not function as intended and removed [Type] Enhancement A suggestion for improvement. labels Apr 26, 2023
@Luehrsen
Copy link
Contributor

Luehrsen commented May 3, 2023

@noahtallen

I think this PR broke all wp-env instances that actually use port 80. The fronend wants 'localhost:80' as URL, but the browser removes port 80 by default, leading to a redirect loop.

@HeikoMamerow
Copy link

Hi @ObliviousHarmony, I had also just worked in parallel on an improvement for wp-env. My improvement prevents the "port is already allocated" issue. It's about port handling: #49843 .

I will now try to adapt my changes in your massive changes and hope it works :-)

FYI: I wrote a script that outputs a secure (unused) port number.

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 this pull request may close these issues.

5 participants