| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Test wp_get_referer(). |
| 5 | * |
| 6 | * @group functions.php |
| 7 | */ |
| 8 | class Tests_Functions_Referer extends WP_UnitTestCase { |
| 9 | |
| 10 | private $request = array(); |
| 11 | private $server = array(); |
| 12 | |
| 13 | function setUp() { |
| 14 | parent::setUp(); |
| 15 | |
| 16 | $this->server = $_SERVER; |
| 17 | $this->request = $_REQUEST; |
| 18 | } |
| 19 | |
| 20 | function tearDown() { |
| 21 | parent::tearDown(); |
| 22 | |
| 23 | $_SERVER = $this->server; |
| 24 | $_REQUEST = $this->request; |
| 25 | |
| 26 | remove_filter( 'site_url', array( 'Tests_Functions_Referer', '_fake_subfolder_install' ) ); |
| 27 | } |
| 28 | |
| 29 | static function _fake_subfolder_install() { |
| 30 | return 'http://example.org/subfolder'; |
| 31 | } |
| 32 | |
| 33 | function test_wp_get_referer_from_request_same_url() { |
| 34 | $_REQUEST['_wp_http_referer'] = addslashes( 'http://example.org/test.php?id=123' ); |
| 35 | $_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' ); |
| 36 | $this->assertFalse( wp_get_referer() ); |
| 37 | } |
| 38 | |
| 39 | function test_wp_get_referer_from_request_different_resource() { |
| 40 | $_REQUEST['_wp_http_referer'] = addslashes( 'http://example.org/another.php?id=123' ); |
| 41 | $_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' ); |
| 42 | $this->assertEquals( 'http://example.org/another.php?id=123', wp_get_referer() ); |
| 43 | } |
| 44 | |
| 45 | function test_wp_get_referer_from_request_different_query_args() { |
| 46 | $_REQUEST['_wp_http_referer'] = addslashes( 'http://example.org/test.php?another=555' ); |
| 47 | $_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' ); |
| 48 | $this->assertEquals( 'http://example.org/test.php?another=555', wp_get_referer() ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @ticket 19856 |
| 53 | */ |
| 54 | function test_subfolder_wp_get_referer_from_request_same_url() { |
| 55 | add_filter( 'site_url', array( 'Tests_Functions_Referer', '_fake_subfolder_install' ) ); |
| 56 | |
| 57 | $_REQUEST['_wp_http_referer'] = addslashes( 'http://example.org/subfolder/test.php?id=123' ); |
| 58 | $_SERVER['REQUEST_URI'] = addslashes( '/subfolder/test.php?id=123' ); |
| 59 | $this->assertFalse( wp_get_referer() ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @ticket 19856 |
| 64 | */ |
| 65 | function test_subfolder_wp_get_referer_from_request_different_resource() { |
| 66 | add_filter( 'site_url', array( 'Tests_Functions_Referer', '_fake_subfolder_install' ) ); |
| 67 | |
| 68 | $_REQUEST['_wp_http_referer'] = addslashes( 'http://example.org/subfolder/another.php?id=123' ); |
| 69 | $_SERVER['REQUEST_URI'] = addslashes( '/subfolder/test.php?id=123' ); |
| 70 | $this->assertEquals( 'http://example.org/subfolder/another.php?id=123', wp_get_referer() ); |
| 71 | } |
| 72 | |
| 73 | function test_wp_get_referer_from_server_same_url() { |
| 74 | $_SERVER['HTTP_REFERER'] = addslashes( 'http://example.org/test.php?id=123' ); |
| 75 | $_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' ); |
| 76 | $this->assertFalse( wp_get_referer() ); |
| 77 | } |
| 78 | |
| 79 | function test_wp_get_referer_from_server_different_resource() { |
| 80 | $_SERVER['HTTP_REFERER'] = addslashes( 'http://example.org/another.php?id=123' ); |
| 81 | $_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' ); |
| 82 | $this->assertEquals( 'http://example.org/another.php?id=123', wp_get_referer() ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @ticket 19856 |
| 87 | * @ticket 27152 |
| 88 | */ |
| 89 | function test_wp_get_referer_different_server() { |
| 90 | $_SERVER['HTTP_REFERER'] = addslashes( 'http://another.example.org/test.php?id=123' ); |
| 91 | $_SERVER['REQUEST_URI'] = addslashes( '/test.php?id=123' ); |
| 92 | $this->assertEquals( 'http://another.example.org/test.php?id=123', wp_get_referer() ); |
| 93 | } |
| 94 | |
| 95 | } |