Skip to content

Commit

Permalink
Fix issue with site still being created using the WP_TESTS_DOMAIN con…
Browse files Browse the repository at this point in the history
…stant (#632)

* Fix issue with site still being created using the WP_TESTS_DOMAIN constant

* Lint fix

* Introduce new constant to control https status

* Remove debug

* Set the constant

* Remove debug

* Linting fixes

* Document WP_TESTS_USE_HTTPS

* CHANGELOG

* Clarify

* Validate the URL

* Change message
  • Loading branch information
srtfisher authored Feb 3, 2025
1 parent 082ce20 commit 5efb651
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## v1.4.4 - 2025-01-31

### Fixed

- Ensure that when setting the home URL for testing with `Installation_Manager::with_url()` also sets the test domain.
- When setting a HTTPS home URL for testing, ensure that the site is installed using HTTPS.

## v1.4.3 - 2025-01-31

### Fixed
Expand Down
33 changes: 32 additions & 1 deletion src/mantle/testing/class-installation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,49 @@ public function with_option( string $option, mixed $value ): static {
/**
* Define the site/home URLs to be set after the installation is loaded.
*
* @throws \InvalidArgumentException If the home or site URL is invalid.
*
* @param string|null $home Home URL.
* @param string|null $site Site URL.
* @param bool $set_tests_domain Whether to set WP_TESTS_DOMAIN constant to match the home URL.
*/
public function with_url( ?string $home = null, ?string $site = null ): static {
public function with_url( ?string $home = null, ?string $site = null, bool $set_tests_domain = true ): static {
if ( $home ) {
if ( ! filter_var( $home, FILTER_VALIDATE_URL ) ) {
throw new \InvalidArgumentException( 'Invalid home URL.' );
}

$this->with_option( 'home', $home );

if ( $set_tests_domain ) {
$this->before(
fn () => defined( 'WP_TESTS_DOMAIN' ) || define( 'WP_TESTS_DOMAIN', parse_url( $home, PHP_URL_HOST ) ), // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
);
}
}

if ( $site ) {
if ( ! filter_var( $site, FILTER_VALIDATE_URL ) ) {
throw new \InvalidArgumentException( 'Invalid site URL.' );
}

$this->with_option( 'siteurl', $site );

// Setup the default HTTP_HOST and HTTPS to make sure the site is installed properly.
$this->before( function () use ( $site ): void {
$_SERVER['HTTP_HOST'] = $_SERVER['SERVER_NAME'] = parse_url( $site, PHP_URL_HOST ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url

if ( 'https' === parse_url( $site, PHP_URL_SCHEME ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
$_SERVER['HTTPS'] = 'on';

defined( 'WP_TESTS_USE_HTTPS' ) || define( 'WP_TESTS_USE_HTTPS', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound
} else {
unset( $_SERVER['HTTPS'] );
}
} );
}


return $this;
}

Expand Down
8 changes: 7 additions & 1 deletion src/mantle/testing/class-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ public static function reset_server(): void {
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';

unset( $_SERVER['HTTP_REFERER'] );
unset( $_SERVER['HTTPS'] );

if ( defined( 'WP_TESTS_USE_HTTPS' ) && WP_TESTS_USE_HTTPS ) {
$_SERVER['HTTPS'] = 'on';
} else {
unset( $_SERVER['HTTPS'] );
}
}

/**
Expand Down Expand Up @@ -233,6 +238,7 @@ public static function setup_configuration(): void {
$table_prefix = 'wptests_'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited

defined( 'WP_TESTS_DOMAIN' ) || define( 'WP_TESTS_DOMAIN', 'example.org' );
defined( 'WP_TESTS_USE_HTTPS' ) || define( 'WP_TESTS_USE_HTTPS', false );
defined( 'WP_TESTS_EMAIL' ) || define( 'WP_TESTS_EMAIL', '[email protected]' );
defined( 'WP_TESTS_TITLE' ) || define( 'WP_TESTS_TITLE', 'Test Site' );
defined( 'WP_PHP_BINARY' ) || define( 'WP_PHP_BINARY', 'php' );
Expand Down
4 changes: 2 additions & 2 deletions src/mantle/testing/concerns/trait-phpunit-upgrade-warning.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected function warn_if_test_suite_contains_legacy_test_cases( TestSuite $tes
<strong>🚨 Warning:</strong> You are running PHPUnit 10+ against a test suite that contains legacy test cases.
</div>
<div>
<span class="text-blue-300">Mantle Testing Framework 1.1</span> includes <span class="text-yellow-500 font-bold">✨ PHPUnit 11 ✨</span> which requires test cases to follow PSR-4 standards.
<span class="text-blue-300">Mantle Testing Framework 1.0</span> includes <span class="text-yellow-500 font-bold">✨ PHPUnit 11 ✨</span> which requires test cases to follow PSR-4 standards.
<br />
For example, that would be <span class="italic">tests/Feature/MyExampleTest.php</span> instead of <span class="italic">tests/feature/test-my-example.php</span>.
<br />
Expand All @@ -85,7 +85,7 @@ protected function warn_if_test_suite_contains_legacy_test_cases( TestSuite $tes
<div class="ml-2 mt-1 italic">composer require --dev phpunit/phpunit:^9 nunomaduro/collision:^6 -W</div>
</div>
<div>
For more information and tips on how to upgrade your codebase to PHPUnit 10, please refer to the 1.0 Release Changelog:
For more information and tips on how to upgrade your codebase to PHPUnit 11, please refer to the 1.0 Release Changelog:
<div class="ml-2 my-1 italic">
https://github.com/alleyinteractive/mantle-framework/blob/1.x/CHANGELOG.md#phpunit-10-migration
Expand Down
26 changes: 26 additions & 0 deletions src/mantle/testing/install-wordpress.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php // phpcs:disable
/**
* Installs WordPress for the purpose of the unit-tests.
*
* Called from wordpress-bootstrap.php:
*
* php install-wordpress.php [multisite] [domain] [https]
*/

use Mantle\Testing\Utils;
Expand All @@ -13,6 +17,26 @@
$GLOBALS['PHP_SELF'] = '/index.php';
$_SERVER['PHP_SELF'] = '/index.php';

if ( ! empty( $argv[1] ) ) {
putenv( 'WP_MULTISITE=' . $argv[1] );
}

// Set the HTTP_HOST and HTTPS server variables to ensure the site is installed
// properly in the installation subprocess.
if ( ! empty( $argv[2] ) ) {
$_SERVER['HTTP_HOST'] = $argv[2];

defined( 'WP_TESTS_DOMAIN' ) || define('WP_TESTS_DOMAIN', $argv[2] );
}

if ( ! empty( $argv[3] ) ) {
$_SERVER['HTTPS'] = 'on';

defined( 'WP_TESTS_USE_HTTPS' ) || define( 'WP_TESTS_USE_HTTPS', true );
} else {
unset( $_SERVER['HTTPS'] );
}

global $wp_rewrite;

require_once __DIR__ . '/preload.php';
Expand All @@ -26,6 +50,8 @@
require_once ABSPATH . '/wp-includes/wp-db.php';
}

// Define the multisite variable. Unable to move this variable up the file as
// wordpress-bootstrap.php will unset it.
$multisite = ! empty( $argv[1] );

$wpdb->query( 'SET default_storage_engine = InnoDB' );
Expand Down
2 changes: 2 additions & 0 deletions src/mantle/testing/wordpress-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@
WP_PHP_BINARY,
escapeshellarg( __DIR__ . '/install-wordpress.php' ),
$multisite,
WP_TESTS_DOMAIN,
! empty( $_SERVER['HTTPS'] ) ? '1' : '0',
],
$retval,
);
Expand Down
11 changes: 6 additions & 5 deletions src/mantle/testing/wp-tests-config-sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@

$table_prefix = 'wptests_'; // Only numbers, letters, and underscores please!

define( 'WP_TESTS_DOMAIN', 'example.org' );
define( 'WP_TESTS_EMAIL', '[email protected]' );
define( 'WP_TESTS_TITLE', 'Test Site' );
defined( 'WP_TESTS_DOMAIN' ) || define( 'WP_TESTS_DOMAIN', 'example.org' );
defined( 'WP_TESTS_USE_HTTPS' ) || define( 'WP_TESTS_USE_HTTPS', false );
defined( 'WP_TESTS_EMAIL' ) || define( 'WP_TESTS_EMAIL', '[email protected]' );
defined( 'WP_TESTS_TITLE' ) || define( 'WP_TESTS_TITLE', 'Test Site' );

define( 'WP_PHP_BINARY', 'php' );
defined( 'WP_PHP_BINARY' ) || define( 'WP_PHP_BINARY', 'php' );

define( 'WPLANG', '' );
defined( 'WPLANG' ) || define( 'WPLANG', '' );

0 comments on commit 5efb651

Please sign in to comment.