wordpress-develop/tests/phpunit/tests/functions/referer.php
Sergey Biryukov ddb409edca Build/Test Tools: Implement use of the void solution.
> PHPUnit 8.0.0 introduced a `void` return type declaration to the "fixture" methods – `setUpBeforeClass()`, `setUp()`, `tearDown()` and `tearDownAfterClass()`. As the `void` return type was not introduced until PHP 7.1, this makes it more difficult to create cross-version compatible tests when using fixtures, due to signature mismatches.
>
> The `Yoast\PHPUnitPolyfills\TestCases\TestCase` overcomes the signature mismatch by having two versions. The correct one will be loaded depending on the PHPUnit version being used.
>
> When using this TestCase, if an individual test, or another TestCase which extends this TestCase, needs to overload any of the "fixture" methods, it should do so by using a snake_case variant of the original fixture method name, i.e. `set_up_before_class()`, `set_up()`, `assert_pre_conditions()`, `assert_post_conditions()`, `tear_down()`, and `tear_down_after_class()`.
>
> The snake_case methods will automatically be called by PHPUnit.
>
> > IMPORTANT: The snake_case methods should not call the PHPUnit parent, i.e. do not use `parent::setUp()` from within an overloaded `set_up()` method. If necessary, DO call `parent::set_up()`.

Reference: https://github.com/Yoast/PHPUnit-Polyfills#testcases

This commit renames all declared fixture methods, and calls to parent versions of those fixture methods, from camelCase to snake_case.

Follow-up to [51559-51567].

Props jrf, hellofromTonya, johnbillion, netweb, dd32, pputzer, SergeyBiryukov.
See #46149.

git-svn-id: https://develop.svn.wordpress.org/trunk@51568 602fd350-edb4-49c9-b593-d223f7449a82
2021-08-07 10:29:41 +00:00

160 lines
5.3 KiB
PHP

<?php
/**
* Test wp_get_referer().
*
* @group functions.php
* @covers ::wp_get_referer
* @covers ::wp_get_raw_referer
*/
class Tests_Functions_Referer extends WP_UnitTestCase {
public function set_up() {
parent::set_up();
$_SERVER['HTTP_REFERER'] = '';
$_SERVER['REQUEST_URI'] = '';
$_REQUEST['_wp_http_referer'] = '';
}
public function tear_down() {
$_SERVER['HTTP_REFERER'] = '';
$_SERVER['REQUEST_URI'] = '';
$_REQUEST['_wp_http_referer'] = '';
parent::tear_down();
}
public function _fake_subfolder_install() {
return 'http://' . WP_TESTS_DOMAIN . '/subfolder';
}
public function filter_allowed_redirect_hosts( $hosts ) {
$hosts[] = 'another.' . WP_TESTS_DOMAIN;
return $hosts;
}
public function test_from_request_relative_referrer() {
$_REQUEST['_wp_http_referer'] = addslashes( '/test.php?id=123' );
$_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' );
$this->assertFalse( wp_get_referer() );
}
public function test_from_request_same_url() {
$_REQUEST['_wp_http_referer'] = addslashes( 'http://' . WP_TESTS_DOMAIN . '/test.php?id=123' );
$_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' );
$this->assertFalse( wp_get_referer() );
}
public function test_from_request_different_resource() {
$_REQUEST['_wp_http_referer'] = addslashes( 'http://' . WP_TESTS_DOMAIN . '/another.php?id=123' );
$_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' );
$this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/another.php?id=123', wp_get_referer() );
}
public function test_from_request_different_query_args() {
$_REQUEST['_wp_http_referer'] = addslashes( 'http://' . WP_TESTS_DOMAIN . '/test.php?another=555' );
$_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' );
$this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/test.php?another=555', wp_get_referer() );
}
/**
* @ticket 19856
*/
public function test_from_request_subfolder_install() {
add_filter( 'site_url', array( $this, '_fake_subfolder_install' ) );
$_REQUEST['_wp_http_referer'] = addslashes( 'http://' . WP_TESTS_DOMAIN . '/subfolder/test.php?id=123' );
$_SERVER['REQUEST_URI'] = addslashes( '/subfolder/test.php?id=123' );
$this->assertFalse( wp_get_referer() );
remove_filter( 'site_url', array( $this, '_fake_subfolder_install' ) );
}
/**
* @ticket 19856
*/
public function test_from_request_subfolder_install_different_resource() {
add_filter( 'site_url', array( $this, '_fake_subfolder_install' ) );
$_REQUEST['_wp_http_referer'] = addslashes( 'http://' . WP_TESTS_DOMAIN . '/subfolder/another.php?id=123' );
$_SERVER['REQUEST_URI'] = addslashes( '/subfolder/test.php?id=123' );
$this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/subfolder/another.php?id=123', wp_get_referer() );
remove_filter( 'site_url', array( $this, '_fake_subfolder_install' ) );
}
public function test_relative_referrer() {
$_REQUEST['HTTP_REFERER'] = addslashes( '/test.php?id=123' );
$_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' );
$this->assertFalse( wp_get_referer() );
}
public function test_same_url() {
$_SERVER['HTTP_REFERER'] = addslashes( 'http://' . WP_TESTS_DOMAIN . '/test.php?id=123' );
$_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' );
$this->assertFalse( wp_get_referer() );
}
public function test_different_resource() {
$_SERVER['HTTP_REFERER'] = addslashes( 'http://' . WP_TESTS_DOMAIN . '/another.php?id=123' );
$_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' );
$this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/another.php?id=123', wp_get_referer() );
}
/**
* @ticket 19856
* @ticket 27152
*/
public function test_different_server() {
$_SERVER['HTTP_REFERER'] = addslashes( 'http://another.' . WP_TESTS_DOMAIN . '/test.php?id=123' );
$_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' );
$this->assertFalse( wp_get_referer() );
}
/**
* @ticket 19856
* @ticket 27152
*/
public function test_different_server_allowed_redirect_host() {
add_filter( 'allowed_redirect_hosts', array( $this, 'filter_allowed_redirect_hosts' ) );
$_SERVER['HTTP_REFERER'] = addslashes( 'http://another.' . WP_TESTS_DOMAIN . '/test.php?id=123' );
$_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' );
$this->assertSame( 'http://another.' . WP_TESTS_DOMAIN . '/test.php?id=123', wp_get_referer() );
remove_filter( 'allowed_redirect_hosts', array( $this, 'filter_allowed_redirect_hosts' ) );
}
/**
* @ticket 27152
*/
public function test_raw_referer_empty() {
$this->assertFalse( wp_get_raw_referer() );
}
/**
* @ticket 27152
*/
public function test_raw_referer() {
$_SERVER['HTTP_REFERER'] = addslashes( 'http://example.com/foo?bar' );
$this->assertSame( 'http://example.com/foo?bar', wp_get_raw_referer() );
}
/**
* @ticket 27152
*/
public function test_raw_referer_from_request() {
$_REQUEST['_wp_http_referer'] = addslashes( 'http://foo.bar/baz' );
$this->assertSame( 'http://foo.bar/baz', wp_get_raw_referer() );
}
/**
* @ticket 27152
*/
public function test_raw_referer_both() {
$_SERVER['HTTP_REFERER'] = addslashes( 'http://example.com/foo?bar' );
$_REQUEST['_wp_http_referer'] = addslashes( 'http://foo.bar/baz' );
$this->assertSame( 'http://foo.bar/baz', wp_get_raw_referer() );
}
}