| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * @group canonical |
| | 5 | * @group foo |
| | 6 | */ |
| | 7 | |
| | 8 | class Tests_GetCanonicalURL extends WP_UnitTestCase { |
| | 9 | public static $post_id; |
| | 10 | |
| | 11 | public static function wpSetUpBeforeClass( $factory ) { |
| | 12 | self::$post_id = $factory->post->create( array( |
| | 13 | 'post_status' => 'publish', |
| | 14 | ) ); |
| | 15 | } |
| | 16 | |
| | 17 | public static function wpTearDownAfterClass() { |
| | 18 | wp_delete_post( self::$post_id, true ); |
| | 19 | } |
| | 20 | |
| | 21 | /** |
| | 22 | * Test for a non existing post. |
| | 23 | */ |
| | 24 | public function test_non_existing_post() { |
| | 25 | $this->assertFalse( wp_get_canonical_url( -1 ) ); |
| | 26 | } |
| | 27 | |
| | 28 | /** |
| | 29 | * Test for a page that is not published. |
| | 30 | */ |
| | 31 | public function test_post_status() { |
| | 32 | $post_id = self::factory()->post->create( array( |
| | 33 | 'post_status' => 'draft', |
| | 34 | ) ); |
| | 35 | |
| | 36 | $this->assertFalse( wp_get_canonical_url( $post_id ) ); |
| | 37 | } |
| | 38 | |
| | 39 | /** |
| | 40 | * Test for a page that is not the queried object. |
| | 41 | */ |
| | 42 | public function test_non_current_page() { |
| | 43 | $this->assertEquals( get_permalink( self::$post_id ), wp_get_canonical_url( self::$post_id ) ); |
| | 44 | } |
| | 45 | |
| | 46 | /** |
| | 47 | * Test non permalink structure page usage. |
| | 48 | */ |
| | 49 | public function test_paged() { |
| | 50 | $link = add_query_arg( array( |
| | 51 | 'page' => 2, |
| | 52 | 'foo' => 'bar', |
| | 53 | ), get_permalink( self::$post_id ) ); |
| | 54 | |
| | 55 | $this->go_to( $link ); |
| | 56 | |
| | 57 | $expected = add_query_arg( array( |
| | 58 | 'page' => 2, |
| | 59 | ), get_permalink( self::$post_id ) ); |
| | 60 | |
| | 61 | $this->assertEquals( $expected, wp_get_canonical_url( self::$post_id ) ); |
| | 62 | } |
| | 63 | |
| | 64 | /** |
| | 65 | * Test permalink structure page usage. |
| | 66 | */ |
| | 67 | public function test_paged_permalink_structure() { |
| | 68 | $this->set_permalink_structure( '/%postname%/' ); |
| | 69 | $page = 2; |
| | 70 | |
| | 71 | $link = add_query_arg( array( |
| | 72 | 'page' => $page, |
| | 73 | 'foo' => 'bar', |
| | 74 | ), get_permalink( self::$post_id ) ); |
| | 75 | |
| | 76 | $this->go_to( $link ); |
| | 77 | |
| | 78 | $expected = trailingslashit( get_permalink( self::$post_id ) ) . user_trailingslashit( $page, 'single_paged' ); |
| | 79 | |
| | 80 | $this->assertEquals( $expected, wp_get_canonical_url( self::$post_id ) ); |
| | 81 | } |
| | 82 | |
| | 83 | /** |
| | 84 | * Test usage of cpage. |
| | 85 | */ |
| | 86 | public function test_cpage() { |
| | 87 | $link = add_query_arg( array( |
| | 88 | 'cpage' => 2, |
| | 89 | ), get_permalink( self::$post_id ) ); |
| | 90 | |
| | 91 | $this->go_to( $link ); |
| | 92 | |
| | 93 | $expected = $link . '#comments'; |
| | 94 | |
| | 95 | $this->assertEquals( $expected , wp_get_canonical_url( self::$post_id ) ); |
| | 96 | } |
| | 97 | |
| | 98 | /** |
| | 99 | * Test calling of filter. |
| | 100 | */ |
| | 101 | public function test_get_canonical_url_filter() { |
| | 102 | add_filter( 'get_canonical_url', array( $this, 'canonical_url_filter' ) ); |
| | 103 | $canonical_url = wp_get_canonical_url( self::$post_id ); |
| | 104 | remove_filter( 'get_canonical_url', array( $this, 'canonical_url_filter' ) ); |
| | 105 | |
| | 106 | $this->assertEquals( $this->canonical_url_filter(), $canonical_url ); |
| | 107 | } |
| | 108 | |
| | 109 | /** |
| | 110 | * Filter callback for testing of filter usage. |
| | 111 | * |
| | 112 | * @return string |
| | 113 | */ |
| | 114 | public function canonical_url_filter() { |
| | 115 | return 'http://canonical.example.org/'; |
| | 116 | } |
| | 117 | } |