-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
noahtallen
merged 9 commits into
WordPress:trunk
from
ObliviousHarmony:fix/wp-env-tests-domain
Apr 20, 2023
Merged
Add Port To WP_TESTS_DOMAIN
#49883
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f8bfb1a
Added Port Addition/Replacement Function
ObliviousHarmony a4dc90f
Refactored WP_SITEURL and WP_HOME Validation
ObliviousHarmony 0f6388d
Always Use `WP_SITEURL` When Configuring WordPress
ObliviousHarmony b893665
Automatically Add Port To `WP_TESTS_DOMAIN`
ObliviousHarmony 0ddc704
Removed Hardcoded `localhost` From PHPUnit Tests
ObliviousHarmony bf4fee3
Merge branch 'trunk' into fix/wp-env-tests-domain
ObliviousHarmony 6ce58ff
Style Fixes
ObliviousHarmony 3aa9e17
Update CHANGELOG.md
ObliviousHarmony f3aadee
Merge branch 'trunk' into fix/wp-env-tests-domain
ObliviousHarmony File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.