General: Correct path replacement regex in wp_guess_url.

In `wp_guess_url`, the regex to check for wp-login.php in the URL is slightly too permissive, not escaping `.` in "wp-login.php".  `.` is a token in regex that matches any character.

This change simply escapes the `.` and adds unit test coverage for `wp_guess_url`.

Props cfinke, ocean90, jrf, voldemortensen, jdgrimes, curdin, netweb, petitphp, SergeyBiryukov, costdev.
Fixes #36827.

git-svn-id: https://develop.svn.wordpress.org/trunk@54146 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
David Baumwald
2022-09-13 19:48:45 +00:00
parent 9bde2e9f26
commit 7b9d4cf54a
2 changed files with 39 additions and 1 deletions

View File

@@ -0,0 +1,38 @@
<?php
/**
* Test wp_guess_url().
*
* @group functions.php
* @covers ::wp_guess_url
*/
class Tests_Functions_wpGuessUrl extends WP_UnitTestCase {
/**
* @ticket 36827
*
* @dataProvider data_guess_url_should_return_site_url
*
* @param string $url The URL to navigate to, relative to `site_url()`.
*/
public function test_guess_url_should_return_site_url( $url ) {
$siteurl = site_url();
$this->go_to( site_url( $url ) );
$this->assertSame( $siteurl, wp_guess_url() );
}
/**
* Data provider.
*
* @return array
*/
function data_guess_url_should_return_site_url() {
return array(
'no trailing slash' => array( 'url' => 'wp-admin' ),
'trailing slash' => array( 'url' => 'wp-admin/' ),
'trailing slash, query var' => array( 'url' => 'wp-admin/?foo=bar' ),
'file extension, no trailing slash' => array( 'url' => 'wp-login.php' ),
'file extension, query var, no trailing slash' => array( 'url' => 'wp-login.php?foo=bar' ),
);
}
}