-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Port To
WP_TESTS_DOMAIN
(#49883)
* Added Port Addition/Replacement Function This new function will allow us to add/replace ports in certain `wp-config.php` values. * Refactored WP_SITEURL and WP_HOME Validation * Always Use `WP_SITEURL` When Configuring WordPress 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. * Automatically Add Port To `WP_TESTS_DOMAIN` 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. * Removed Hardcoded `localhost` From PHPUnit Tests To add resiliency to the test suite, we should use `WP_TESTS_DOMAIN` instead of hardcoding the domain and port in the URLs.
- Loading branch information
1 parent
092039b
commit b8733ad
Showing
12 changed files
with
232 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
const { ValidationError } = require( './validate-config' ); | ||
|
||
/** | ||
* Adds or replaces the port to the given domain or URI. | ||
* | ||
* @param {string} input The domain or URI to operate on. | ||
* @param {string} port The port to append. | ||
* @param {boolean} [replace] Indicates whether or not the port should be replaced if one is already present. Defaults to true. | ||
* @return {string} The string with the port added or replaced. | ||
*/ | ||
module.exports = function addOrReplacePort( input, port, replace = true ) { | ||
// This matches both domains and URIs with an optional port and anything | ||
// that remains after. We can use this to build an output string that | ||
// adds or replaces the port without making any other changes to the input. | ||
const matches = input.match( | ||
/^((?:.+:\/\/)?[a-z0-9.\-]+)(?::([0-9]+))?(.*)$/i | ||
); | ||
if ( ! matches ) { | ||
throw new ValidationError( `Invalid domain or uri: ${ input }.` ); | ||
} | ||
|
||
// When a port is already present we will do nothing if the caller doesn't want it to be replaced. | ||
if ( matches[ 2 ] !== undefined && ! replace ) { | ||
return input; | ||
} | ||
|
||
// Place the port in the correct location in the input. | ||
return matches[ 1 ] + ':' + port + matches[ 3 ]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
const addOrReplacePort = require( '../add-or-replace-port.js' ); | ||
|
||
describe( 'addOrReplacePort', () => { | ||
beforeEach( () => { | ||
jest.clearAllMocks(); | ||
} ); | ||
|
||
it( 'should add or replace port with various inputs', () => { | ||
const testMap = [ | ||
// Addition | ||
{ in: 'test', expect: 'test:101' }, | ||
{ in: 'test/test?test#test', expect: 'test:101/test?test#test' }, | ||
{ in: 'http://test.com', expect: 'http://test.com:101' }, | ||
{ | ||
in: 'http://test.com/test?test#test', | ||
expect: 'http://test.com:101/test?test#test', | ||
}, | ||
{ in: 'ssh://test.com', expect: 'ssh://test.com:101' }, | ||
{ in: 'test.com', expect: 'test.com:101' }, | ||
|
||
// Replacement | ||
{ in: 'test:99', expect: 'test:101' }, | ||
{ in: 'test:99/test?test#test', expect: 'test:101/test?test#test' }, | ||
{ in: 'http://test.com:99', expect: 'http://test.com:101' }, | ||
{ | ||
in: 'http://test.com:99/test?test#test', | ||
expect: 'http://test.com:101/test?test#test', | ||
}, | ||
{ in: 'ssh://test.com:99', expect: 'ssh://test.com:101' }, | ||
{ in: 'test.com:99', expect: 'test.com:101' }, | ||
]; | ||
|
||
for ( const test of testMap ) { | ||
const result = addOrReplacePort( test.in, '101' ); | ||
expect( result ).toEqual( test.expect ); | ||
} | ||
} ); | ||
|
||
it( 'should do nothing if port is present but replacement is not requested', () => { | ||
const testMap = [ | ||
{ in: 'test', expect: 'test:103' }, | ||
{ in: 'test:99', expect: 'test:99' }, | ||
]; | ||
|
||
for ( const test of testMap ) { | ||
const result = addOrReplacePort( test.in, '103', false ); | ||
expect( result ).toEqual( test.expect ); | ||
} | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.