| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Test wp_get_referer(). |
| 5 | * |
| 6 | * @group functions.php |
| 7 | */ |
| 8 | class Tests_Functions_Referer extends WP_UnitTestCase { |
| 9 | private $request = array(); |
| 10 | private $server = array(); |
| 11 | |
| 12 | public function setUp() { |
| 13 | parent::setUp(); |
| 14 | |
| 15 | $this->server = $_SERVER; |
| 16 | $this->request = $_REQUEST; |
| 17 | } |
| 18 | |
| 19 | public function tearDown() { |
| 20 | parent::tearDown(); |
| 21 | |
| 22 | $_SERVER = $this->server; |
| 23 | $_REQUEST = $this->request; |
| 24 | } |
| 25 | |
| 26 | public function _fake_subfolder_install() { |
| 27 | return 'http://example.org/subfolder'; |
| 28 | } |
| 29 | |
| 30 | public function filter_allowed_redirect_hosts( $hosts ) { |
| 31 | $hosts[] = 'another.example.org'; |
| 32 | |
| 33 | return $hosts; |
| 34 | } |
| 35 | |
| 36 | public function test_from_request_same_url() { |
| 37 | $_REQUEST['_wp_http_referer'] = addslashes( 'http://example.org/test.php?id=123' ); |
| 38 | $_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' ); |
| 39 | $this->assertFalse( wp_get_referer() ); |
| 40 | } |
| 41 | |
| 42 | public function test_from_request_different_resource() { |
| 43 | $_REQUEST['_wp_http_referer'] = addslashes( 'http://example.org/another.php?id=123' ); |
| 44 | $_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' ); |
| 45 | $this->assertSame( 'http://example.org/another.php?id=123', wp_get_referer() ); |
| 46 | } |
| 47 | |
| 48 | public function test_from_request_different_query_args() { |
| 49 | $_REQUEST['_wp_http_referer'] = addslashes( 'http://example.org/test.php?another=555' ); |
| 50 | $_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' ); |
| 51 | $this->assertSame( 'http://example.org/test.php?another=555', wp_get_referer() ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @ticket 19856 |
| 56 | */ |
| 57 | public function test_from_request_subfolder_install() { |
| 58 | add_filter( 'site_url', array( $this, '_fake_subfolder_install' ) ); |
| 59 | |
| 60 | $_REQUEST['_wp_http_referer'] = addslashes( 'http://example.org/subfolder/test.php?id=123' ); |
| 61 | $_SERVER['REQUEST_URI'] = addslashes( '/subfolder/test.php?id=123' ); |
| 62 | $this->assertFalse( wp_get_referer() ); |
| 63 | |
| 64 | remove_filter( 'site_url', array( $this, '_fake_subfolder_install' ) ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @ticket 19856 |
| 69 | */ |
| 70 | public function test_from_request_subfolder_install_different_resource() { |
| 71 | add_filter( 'site_url', array( $this, '_fake_subfolder_install' ) ); |
| 72 | |
| 73 | $_REQUEST['_wp_http_referer'] = addslashes( 'http://example.org/subfolder/another.php?id=123' ); |
| 74 | $_SERVER['REQUEST_URI'] = addslashes( '/subfolder/test.php?id=123' ); |
| 75 | $this->assertSame( 'http://example.org/subfolder/another.php?id=123', wp_get_referer() ); |
| 76 | |
| 77 | remove_filter( 'site_url', array( $this, '_fake_subfolder_install' ) ); |
| 78 | } |
| 79 | |
| 80 | public function test_same_url() { |
| 81 | $_SERVER['HTTP_REFERER'] = addslashes( 'http://example.org/test.php?id=123' ); |
| 82 | $_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' ); |
| 83 | $this->assertFalse( wp_get_referer() ); |
| 84 | } |
| 85 | |
| 86 | public function test_different_resource() { |
| 87 | $_SERVER['HTTP_REFERER'] = addslashes( 'http://example.org/another.php?id=123' ); |
| 88 | $_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' ); |
| 89 | $this->assertSame( 'http://example.org/another.php?id=123', wp_get_referer() ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @ticket 19856 |
| 94 | * @ticket 27152 |
| 95 | */ |
| 96 | public function test_different_server() { |
| 97 | $_SERVER['HTTP_REFERER'] = addslashes( 'http://another.example.org/test.php?id=123' ); |
| 98 | $_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' ); |
| 99 | $this->assertFalse( wp_get_referer() ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @ticket 19856 |
| 104 | * @ticket 27152 |
| 105 | */ |
| 106 | public function test_different_server_allowed_redirect_host() { |
| 107 | add_filter( 'allowed_redirect_hosts', array( $this, 'filter_allowed_redirect_hosts' ) ); |
| 108 | $_SERVER['HTTP_REFERER'] = addslashes( 'http://another.example.org/test.php?id=123' ); |
| 109 | $_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' ); |
| 110 | $this->assertSame( 'http://another.example.org/test.php?id=123', wp_get_referer() ); |
| 111 | remove_filter( 'allowed_redirect_hosts', array( $this, 'filter_allowed_redirect_hosts' ) ); |
| 112 | } |
| 113 | } |