| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * @group canonical |
| | 5 | */ |
| | 6 | |
| | 7 | class Tests_Get_Canonical_URL extends WP_UnitTestCase { |
| | 8 | |
| | 9 | /** |
| | 10 | * Cleaning up |
| | 11 | */ |
| | 12 | public function tearDown() { |
| | 13 | parent::tearDown(); |
| | 14 | |
| | 15 | delete_option('permalink_structure'); |
| | 16 | |
| | 17 | remove_filter( 'get_canonical_url', array($this, 'apply_canonical_filter' ) ); |
| | 18 | } |
| | 19 | |
| | 20 | /** |
| | 21 | * Test for a non existing post |
| | 22 | */ |
| | 23 | public function test_non_existing_post() { |
| | 24 | $subject = get_canonical_url(1); |
| | 25 | |
| | 26 | $this->assertFalse( $subject ); |
| | 27 | } |
| | 28 | |
| | 29 | /** |
| | 30 | * Test for a page that is not published |
| | 31 | */ |
| | 32 | public function test_post_status() { |
| | 33 | $post = new stdClass(); |
| | 34 | $post->post_status = 'draft'; |
| | 35 | |
| | 36 | $subject = get_canonical_url( $post ); |
| | 37 | |
| | 38 | $this->assertFalse( $subject ); |
| | 39 | } |
| | 40 | |
| | 41 | /** |
| | 42 | * Test for a page that is not the queried object |
| | 43 | */ |
| | 44 | public function test_non_current_page() { |
| | 45 | $post_id = self::factory()->post->create( array( |
| | 46 | 'post_status' => 'publish', |
| | 47 | ) ); |
| | 48 | |
| | 49 | $subject = get_canonical_url( $post_id ); |
| | 50 | $this->assertEquals( get_permalink( $post_id ), $subject ); |
| | 51 | } |
| | 52 | |
| | 53 | /** |
| | 54 | * Test non permalink structure page usage |
| | 55 | */ |
| | 56 | public function test_paged() { |
| | 57 | global $wp_query; |
| | 58 | |
| | 59 | $page = 2; |
| | 60 | $post_id = self::factory()->post->create( array( |
| | 61 | 'post_status' => 'publish', |
| | 62 | ) ); |
| | 63 | |
| | 64 | // Should add page to the URL. |
| | 65 | $wp_query->query_vars['page'] = $page; |
| | 66 | |
| | 67 | // Set up queried object. |
| | 68 | $wp_query->queried_object = get_post($post_id); |
| | 69 | $wp_query->queried_object_id = $post_id; |
| | 70 | |
| | 71 | $url = get_permalink( $post_id ); |
| | 72 | $test = add_query_arg( 'page', $page, $url ); |
| | 73 | |
| | 74 | $subject = get_canonical_url( $post_id ); |
| | 75 | |
| | 76 | $this->assertEquals( $test, $subject ); |
| | 77 | } |
| | 78 | |
| | 79 | /** |
| | 80 | * Test permalink structure page usage |
| | 81 | */ |
| | 82 | public function test_paged_permalink_structure() { |
| | 83 | global $wp_query; |
| | 84 | |
| | 85 | update_option('permalink_structure', '1'); |
| | 86 | |
| | 87 | $page = 2; |
| | 88 | $post_id = self::factory()->post->create( array( |
| | 89 | 'post_status' => 'publish', |
| | 90 | ) ); |
| | 91 | |
| | 92 | // Should add page to the URL. |
| | 93 | $wp_query->query_vars['page'] = $page; |
| | 94 | |
| | 95 | // Set up queried object. |
| | 96 | $wp_query->queried_object = get_post($post_id); |
| | 97 | $wp_query->queried_object_id = $post_id; |
| | 98 | |
| | 99 | $url = get_permalink( $post_id ); |
| | 100 | $test = trailingslashit( $url ) . user_trailingslashit( $page, 'single_paged' ); |
| | 101 | |
| | 102 | $subject = get_canonical_url( $post_id ); |
| | 103 | |
| | 104 | $this->assertEquals( $test, $subject ); |
| | 105 | } |
| | 106 | |
| | 107 | /** |
| | 108 | * Test usage of cpage |
| | 109 | */ |
| | 110 | public function test_cpage() { |
| | 111 | global $wp_query; |
| | 112 | |
| | 113 | $cpage = 1; |
| | 114 | |
| | 115 | $post_id = self::factory()->post->create( array( |
| | 116 | 'post_status' => 'publish', |
| | 117 | ) ); |
| | 118 | |
| | 119 | // Set cpage value. |
| | 120 | $wp_query->query_vars['cpage'] = $cpage; |
| | 121 | |
| | 122 | // Set up queried object. |
| | 123 | $wp_query->queried_object = get_post($post_id); |
| | 124 | $wp_query->queried_object_id = $post_id; |
| | 125 | |
| | 126 | $subject = get_canonical_url( $post_id ); |
| | 127 | |
| | 128 | $this->assertEquals( get_comments_pagenum_link( $cpage ), $subject ); |
| | 129 | } |
| | 130 | |
| | 131 | /** |
| | 132 | * Test calling of filter |
| | 133 | */ |
| | 134 | public function test_filter_applied() { |
| | 135 | $post_id = self::factory()->post->create( array( |
| | 136 | 'post_status' => 'publish', |
| | 137 | ) ); |
| | 138 | |
| | 139 | // Apply filter |
| | 140 | add_filter( 'get_canonical_url', array($this, 'apply_canonical_filter' ), 10, 2 ); |
| | 141 | |
| | 142 | $subject = get_canonical_url( $post_id ); |
| | 143 | |
| | 144 | $this->assertEquals( $this->apply_canonical_filter( get_permalink( $post_id ), get_post( $post_id ) ), $subject ); |
| | 145 | } |
| | 146 | |
| | 147 | /** |
| | 148 | * Filter callback for testing of filter usage |
| | 149 | * |
| | 150 | * @param $url |
| | 151 | * @param $post |
| | 152 | * |
| | 153 | * @return string |
| | 154 | */ |
| | 155 | public function apply_canonical_filter( $url, $post ) { |
| | 156 | return 'http://filtered.canonical.com'; |
| | 157 | } |
| | 158 | } |