1
0
Fork 0
mirror of https://github.com/buddypress/buddypress.git synced 2026-07-22 20:56:55 +08:00
buddypress/tests/phpunit/testcases/core/functions/bpGetRefererPath.php
Boone B Gorges 7a95bc63d8 Improve AJAX referer determination during URI parsing.
When parsing referer URLs during `bp_core_set_uri_globals()`, BP has
historically used `bp_core_referrer()` to generate a "current URL" relative to
the current web root. This path is then passed to the 'bp_uri' filter before
being parsed. However, `bp_core_referrer()` incorrectly returns a URL without
a leading slash, making it a relative path rather than a webroot-absolute path.
The parsing logic later in `bp_core_set_uri_globals()` makes it so that the
error does not matter from the point of BP core, but plugins filtering 'bp_uri'
will receive a potentially incorrect URL path.

This changeset deprecates the unreliable `bp_core_referrer()` in favor of
`bp_get_referer_path()`. The latter function correctly returns URL paths with a
leading slash. `bp_get_referer_path()` is then used instead of
`bp_core_referrer()` in `bp_core_set_uri_globals()`.

Props mechter for an initial patch.
Fixes #6252.

git-svn-id: https://buddypress.svn.wordpress.org/trunk@9559 cdf35c40-ae34-48e0-9cc9-0c9da1808c22
2015-02-25 14:56:18 +00:00

79 lines
2.1 KiB
PHP

<?php
/**
* @group core
* @group functions
* @group bp_get_referer_path
*/
class BP_Tests_Core_Functions_BPGetRefererPath extends BP_UnitTestCase {
private $_wp_http_referer = '';
private $http_referer = '';
public function setUp() {
parent::setUp();
$this->_wp_http_referer = '';
$this->http_referer = '';
if ( isset( $_REQUEST['_wp_http_referer'] ) ) {
$this->_wp_http_referer = $_REQUEST['_wp_http_referer'];
}
if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
$this->http_referer = $_SERVER['HTTP_REFERER'];
}
}
public function tearDown() {
if ( isset( $_REQUEST['_wp_http_referer'] ) ) {
unset( $_REQUEST['_wp_http_referer'] );
}
if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
unset( $_SERVER['HTTP_REFERER'] );
}
if ( $this->_wp_http_referer ) {
$_REQUEST['_wp_http_referer'] = $this->_wp_http_referer;
}
if ( $this->http_referer ) {
$_SERVER['HTTP_REFERER'] = $this->http_referer;
}
parent::tearDown();
}
public function test_from__wp_http_referer_fully_qualified_uri() {
$home = get_option( 'home' );
$_REQUEST['_wp_http_referer'] = trailingslashit( $home ) . 'foo/';
$found = bp_get_referer_path();
$this->assertSame( '/foo/', bp_get_referer_path() );
}
public function test_from__wp_http_referer_absolute_path() {
$_REQUEST['_wp_http_referer'] = '/foo/';
$found = bp_get_referer_path();
$this->assertSame( '/foo/', bp_get_referer_path() );
}
public function test_from_server_request_uri_fully_qualified_uri() {
$home = get_option( 'home' );
$_SERVER['HTTP_REFERER'] = trailingslashit( $home ) . 'foo/';
$found = bp_get_referer_path();
$this->assertSame( '/foo/', bp_get_referer_path() );
}
public function test_from_server_request_uri_absolute_path() {
$_SERVER['HTTP_REFERER'] = '/foo/';
$found = bp_get_referer_path();
$this->assertSame( '/foo/', bp_get_referer_path() );
}
public function test__wp_http_referer_should_take_precedence_over_server_superglobal() {
$_SERVER['HTTP_REFERER'] = '/foo/';
$_REQUEST['_wp_http_referer'] = '/bar/';
$found = bp_get_referer_path();
$this->assertSame( '/bar/', bp_get_referer_path() );
}
}