Skip to content

Commit

Permalink
Add tests for #269 and #380
Browse files Browse the repository at this point in the history
  • Loading branch information
benlk committed Sep 16, 2024
1 parent 5864728 commit 4708e63
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions tests/php/test-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,4 +682,96 @@ function( $requested_path, $redirected_to, $status_code ) use ( &$redirect_to, &
remove_filter('srm_match_query_params', '__return_true');
}

/**
* Test that redirection to an external domain works with a regular expression without substitution
*
* @link https://github.com/10up/safe-redirect-manager/issues/269
* @since 2.1.2
*/
public function testRedirectToExternalDomainWithNonSubstitutingRegex269() {
$_SERVER['REQUEST_URI'] = '/be/hhhh';
$redirected = true;
$redirect_to = 'http://xu-osp-plugins.local/404-regex';
$expected = 'http://xu-osp-plugins.local/404-regex';

srm_create_redirect( '(go|be)\/h{0,}$', $redirect_to, 301, true );

add_action(
'srm_do_redirect',
function( $requested_path, $redirected_to, $status_code ) use ( &$redirect_to, &$redirected, &$expected ) {
if ( $redirected_to === $expected ) {
$redirected = true;
}
},
10,
3
);

SRM_Redirect::factory()->maybe_redirect();
$this->assertTrue( $redirected, 'Expected that a non-substituting regular expression would trigger a redirect to http://xu-osp-plugins.local/404-regex, but instead redirected to ' . $redirect_to );
}

/**
* Test that redirection to an external domain works with a regular expression with substitution
*
* @link https://github.com/10up/safe-redirect-manager/issues/380
* @since 2.2.0
*/
public function testRedirectToExternalDomainWithSubstitutingRegex380() {
$_SERVER['REQUEST_URI'] = '/test/1234';
$redirected = true;
$redirect_from = '/test/(.*)';
$redirect_to = 'http://example.org/$1';
$expected = 'http://example.org/1234';

srm_create_redirect( $redirect_from, $redirect_to, 301, true );

add_action(
'srm_do_redirect',
function( $requested_path, $redirected_to, $status_code ) use ( &$redirect_to, &$redirected, &$expected ) {
if ( $redirected_to === $expected ) {
$redirected = true;
} else {
$redirect_to = $redirected_to;
}
},
10,
3
);

SRM_Redirect::factory()->maybe_redirect();
$this->assertTrue( $redirected, 'Expected that a substituting regular expression would trigger a redirect to http://example.org/1234, but instead redirected to ' . $redirect_to );
}

/**
* Test that redirection to a local URL works with a regular expression with substitution
*
* @link https://github.com/10up/safe-redirect-manager/issues/380
* @since 2.2.0
*/
public function testRedirectToPathWithSubstitutingRegex380() {
$_SERVER['REQUEST_URI'] = '/test/1234';
$redirected = true;
$redirect_from = '/test/(.*)';
$redirect_to = '/result/$1';
$expected = '/result/1234';

srm_create_redirect( $redirect_from, $redirect_to, 301, true );

add_action(
'srm_do_redirect',
function( $requested_path, $redirected_to, $status_code ) use ( &$redirect_to, &$redirected, &$expected ) {
if ( $redirected_to === $expected ) {
$redirected = true;
} else {
$redirect_to = $redirected_to;
}
},
10,
3
);

SRM_Redirect::factory()->maybe_redirect();
$this->assertTrue( $redirected, 'Expected that a substituting regular expression would trigger a redirect to /result/1234, but instead redirected to ' . $redirect_to );
}
}

0 comments on commit 4708e63

Please sign in to comment.