Skip to content

Commit 5efb651

Browse files
authored
Fix issue with site still being created using the WP_TESTS_DOMAIN constant (#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
1 parent 082ce20 commit 5efb651

File tree

7 files changed

+82
-9
lines changed

7 files changed

+82
-9
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## v1.4.4 - 2025-01-31
9+
10+
### Fixed
11+
12+
- Ensure that when setting the home URL for testing with `Installation_Manager::with_url()` also sets the test domain.
13+
- When setting a HTTPS home URL for testing, ensure that the site is installed using HTTPS.
14+
815
## v1.4.3 - 2025-01-31
916

1017
### Fixed

src/mantle/testing/class-installation-manager.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,49 @@ public function with_option( string $option, mixed $value ): static {
219219
/**
220220
* Define the site/home URLs to be set after the installation is loaded.
221221
*
222+
* @throws \InvalidArgumentException If the home or site URL is invalid.
223+
*
222224
* @param string|null $home Home URL.
223225
* @param string|null $site Site URL.
226+
* @param bool $set_tests_domain Whether to set WP_TESTS_DOMAIN constant to match the home URL.
224227
*/
225-
public function with_url( ?string $home = null, ?string $site = null ): static {
228+
public function with_url( ?string $home = null, ?string $site = null, bool $set_tests_domain = true ): static {
226229
if ( $home ) {
230+
if ( ! filter_var( $home, FILTER_VALIDATE_URL ) ) {
231+
throw new \InvalidArgumentException( 'Invalid home URL.' );
232+
}
233+
227234
$this->with_option( 'home', $home );
235+
236+
if ( $set_tests_domain ) {
237+
$this->before(
238+
fn () => defined( 'WP_TESTS_DOMAIN' ) || define( 'WP_TESTS_DOMAIN', parse_url( $home, PHP_URL_HOST ) ), // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
239+
);
240+
}
228241
}
229242

230243
if ( $site ) {
244+
if ( ! filter_var( $site, FILTER_VALIDATE_URL ) ) {
245+
throw new \InvalidArgumentException( 'Invalid site URL.' );
246+
}
247+
231248
$this->with_option( 'siteurl', $site );
249+
250+
// Setup the default HTTP_HOST and HTTPS to make sure the site is installed properly.
251+
$this->before( function () use ( $site ): void {
252+
$_SERVER['HTTP_HOST'] = $_SERVER['SERVER_NAME'] = parse_url( $site, PHP_URL_HOST ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
253+
254+
if ( 'https' === parse_url( $site, PHP_URL_SCHEME ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
255+
$_SERVER['HTTPS'] = 'on';
256+
257+
defined( 'WP_TESTS_USE_HTTPS' ) || define( 'WP_TESTS_USE_HTTPS', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound
258+
} else {
259+
unset( $_SERVER['HTTPS'] );
260+
}
261+
} );
232262
}
233263

264+
234265
return $this;
235266
}
236267

src/mantle/testing/class-utils.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,12 @@ public static function reset_server(): void {
120120
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
121121

122122
unset( $_SERVER['HTTP_REFERER'] );
123-
unset( $_SERVER['HTTPS'] );
123+
124+
if ( defined( 'WP_TESTS_USE_HTTPS' ) && WP_TESTS_USE_HTTPS ) {
125+
$_SERVER['HTTPS'] = 'on';
126+
} else {
127+
unset( $_SERVER['HTTPS'] );
128+
}
124129
}
125130

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

235240
defined( 'WP_TESTS_DOMAIN' ) || define( 'WP_TESTS_DOMAIN', 'example.org' );
241+
defined( 'WP_TESTS_USE_HTTPS' ) || define( 'WP_TESTS_USE_HTTPS', false );
236242
defined( 'WP_TESTS_EMAIL' ) || define( 'WP_TESTS_EMAIL', 'admin@example.org' );
237243
defined( 'WP_TESTS_TITLE' ) || define( 'WP_TESTS_TITLE', 'Test Site' );
238244
defined( 'WP_PHP_BINARY' ) || define( 'WP_PHP_BINARY', 'php' );

src/mantle/testing/concerns/trait-phpunit-upgrade-warning.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected function warn_if_test_suite_contains_legacy_test_cases( TestSuite $tes
7676
<strong>🚨 Warning:</strong> You are running PHPUnit 10+ against a test suite that contains legacy test cases.
7777
</div>
7878
<div>
79-
<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.
79+
<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.
8080
<br />
8181
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>.
8282
<br />
@@ -85,7 +85,7 @@ protected function warn_if_test_suite_contains_legacy_test_cases( TestSuite $tes
8585
<div class="ml-2 mt-1 italic">composer require --dev phpunit/phpunit:^9 nunomaduro/collision:^6 -W</div>
8686
</div>
8787
<div>
88-
For more information and tips on how to upgrade your codebase to PHPUnit 10, please refer to the 1.0 Release Changelog:
88+
For more information and tips on how to upgrade your codebase to PHPUnit 11, please refer to the 1.0 Release Changelog:
8989
9090
<div class="ml-2 my-1 italic">
9191
https://github.com/alleyinteractive/mantle-framework/blob/1.x/CHANGELOG.md#phpunit-10-migration

src/mantle/testing/install-wordpress.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?php // phpcs:disable
22
/**
33
* Installs WordPress for the purpose of the unit-tests.
4+
*
5+
* Called from wordpress-bootstrap.php:
6+
*
7+
* php install-wordpress.php [multisite] [domain] [https]
48
*/
59

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

20+
if ( ! empty( $argv[1] ) ) {
21+
putenv( 'WP_MULTISITE=' . $argv[1] );
22+
}
23+
24+
// Set the HTTP_HOST and HTTPS server variables to ensure the site is installed
25+
// properly in the installation subprocess.
26+
if ( ! empty( $argv[2] ) ) {
27+
$_SERVER['HTTP_HOST'] = $argv[2];
28+
29+
defined( 'WP_TESTS_DOMAIN' ) || define('WP_TESTS_DOMAIN', $argv[2] );
30+
}
31+
32+
if ( ! empty( $argv[3] ) ) {
33+
$_SERVER['HTTPS'] = 'on';
34+
35+
defined( 'WP_TESTS_USE_HTTPS' ) || define( 'WP_TESTS_USE_HTTPS', true );
36+
} else {
37+
unset( $_SERVER['HTTPS'] );
38+
}
39+
1640
global $wp_rewrite;
1741

1842
require_once __DIR__ . '/preload.php';
@@ -26,6 +50,8 @@
2650
require_once ABSPATH . '/wp-includes/wp-db.php';
2751
}
2852

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

3157
$wpdb->query( 'SET default_storage_engine = InnoDB' );

src/mantle/testing/wordpress-bootstrap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@
160160
WP_PHP_BINARY,
161161
escapeshellarg( __DIR__ . '/install-wordpress.php' ),
162162
$multisite,
163+
WP_TESTS_DOMAIN,
164+
! empty( $_SERVER['HTTPS'] ) ? '1' : '0',
163165
],
164166
$retval,
165167
);

src/mantle/testing/wp-tests-config-sample.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@
5252

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

55-
define( 'WP_TESTS_DOMAIN', 'example.org' );
56-
define( 'WP_TESTS_EMAIL', 'admin@example.org' );
57-
define( 'WP_TESTS_TITLE', 'Test Site' );
55+
defined( 'WP_TESTS_DOMAIN' ) || define( 'WP_TESTS_DOMAIN', 'example.org' );
56+
defined( 'WP_TESTS_USE_HTTPS' ) || define( 'WP_TESTS_USE_HTTPS', false );
57+
defined( 'WP_TESTS_EMAIL' ) || define( 'WP_TESTS_EMAIL', 'admin@example.org' );
58+
defined( 'WP_TESTS_TITLE' ) || define( 'WP_TESTS_TITLE', 'Test Site' );
5859

59-
define( 'WP_PHP_BINARY', 'php' );
60+
defined( 'WP_PHP_BINARY' ) || define( 'WP_PHP_BINARY', 'php' );
6061

61-
define( 'WPLANG', '' );
62+
defined( 'WPLANG' ) || define( 'WPLANG', '' );

0 commit comments

Comments
 (0)