| 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 | $this->server = $_SERVER; |
| 16 | $this->request = $_REQUEST; |
| 17 | } |
| 18 | |
| 19 | function tearDown() { |
| 20 | parent::tearDown(); |
| 21 | $_SERVER = $this->server; |
| 22 | $_REQUEST = $this->request; |
| 23 | } |
| 24 | |
| 25 | function test_wp_get_referer_from_request_same_url() { |
| 26 | $_REQUEST['_wp_http_referer'] = get_site_url() . '/test.php?id=123'; |
| 27 | $_SERVER['REQUEST_URI'] = '/test.php?id=123'; |
| 28 | $this->assertFalse( wp_get_referer() ); |
| 29 | } |
| 30 | |
| 31 | function test_wp_get_referer_from_request_different_resource() { |
| 32 | $_REQUEST['_wp_http_referer'] = get_site_url() . '/another.php?id=123'; |
| 33 | $_SERVER['REQUEST_URI'] = '/test.php?id=123'; |
| 34 | $this->assertEquals( get_site_url() . '/another.php?id=123', wp_get_referer() ); |
| 35 | } |
| 36 | |
| 37 | function test_wp_get_referer_from_request_different_query_args() { |
| 38 | $_REQUEST['_wp_http_referer'] = get_site_url() . '/test.php?another=555'; |
| 39 | $_SERVER['REQUEST_URI'] = '/test.php?id=123'; |
| 40 | $this->assertEquals( get_site_url() . '/test.php?another=555', wp_get_referer() ); |
| 41 | } |
| 42 | |
| 43 | function test_wp_get_referer_from_server_same_url() { |
| 44 | $_SERVER['HTTP_REFERER'] = get_site_url() . '/test.php?id=123'; |
| 45 | $_SERVER['REQUEST_URI'] = '/test.php?id=123'; |
| 46 | $this->assertFalse( wp_get_referer() ); |
| 47 | } |
| 48 | |
| 49 | function test_wp_get_referer_from_server_different_resource() { |
| 50 | $_SERVER['HTTP_REFERER'] = get_site_url() . '/another.php?id=123'; |
| 51 | $_SERVER['REQUEST_URI'] = '/test.php?id=123'; |
| 52 | $this->assertEquals( get_site_url() . '/another.php?id=123', wp_get_referer() ); |
| 53 | } |
| 54 | |
| 55 | function test_wp_get_referer_different_server() { |
| 56 | $_SERVER['HTTP_REFERER'] = 'http://another.example.org/test.php?id=123'; |
| 57 | $_SERVER['REQUEST_URI'] = '/test.php?id=123'; |
| 58 | $this->assertEquals( 'http://another.example.org/test.php?id=123', wp_get_referer() ); |
| 59 | } |
| 60 | } |