| | 126 | |
| | 127 | /** |
| | 128 | * @ticket 36711 |
| | 129 | */ |
| | 130 | public function test_should_hit_cache() { |
| | 131 | global $wpdb; |
| | 132 | |
| | 133 | $page = self::factory()->post->create( array( |
| | 134 | 'post_type' => 'page', |
| | 135 | 'post_name' => 'foo', |
| | 136 | ) ); |
| | 137 | |
| | 138 | // Prime cache. |
| | 139 | $found = get_page_by_path( 'foo' ); |
| | 140 | $this->assertSame( $page, $found->ID ); |
| | 141 | |
| | 142 | $num_queries = $wpdb->num_queries; |
| | 143 | |
| | 144 | $found = get_page_by_path( 'foo' ); |
| | 145 | $this->assertSame( $page, $found->ID ); |
| | 146 | $this->assertSame( $num_queries, $wpdb->num_queries ); |
| | 147 | } |
| | 148 | |
| | 149 | /** |
| | 150 | * @ticket 36711 |
| | 151 | */ |
| | 152 | public function test_bad_path_should_be_cached() { |
| | 153 | global $wpdb; |
| | 154 | |
| | 155 | // Prime cache. |
| | 156 | $found = get_page_by_path( 'foo' ); |
| | 157 | $this->assertNull( $found ); |
| | 158 | |
| | 159 | $num_queries = $wpdb->num_queries; |
| | 160 | |
| | 161 | $found = get_page_by_path( 'foo' ); |
| | 162 | $this->assertNull( $found ); |
| | 163 | $this->assertSame( $num_queries, $wpdb->num_queries ); |
| | 164 | } |
| | 165 | |
| | 166 | /** |
| | 167 | * @ticket 36711 |
| | 168 | */ |
| | 169 | public function test_cache_should_not_match_post_in_different_post_type_with_same_path() { |
| | 170 | global $wpdb; |
| | 171 | |
| | 172 | register_post_type( 'wptests_pt' ); |
| | 173 | |
| | 174 | $p1 = self::factory()->post->create( array( |
| | 175 | 'post_type' => 'page', |
| | 176 | 'post_name' => 'foo', |
| | 177 | ) ); |
| | 178 | |
| | 179 | $p2 = self::factory()->post->create( array( |
| | 180 | 'post_type' => 'wptests_pt', |
| | 181 | 'post_name' => 'foo', |
| | 182 | ) ); |
| | 183 | |
| | 184 | // Prime cache for the page. |
| | 185 | $found = get_page_by_path( 'foo' ); |
| | 186 | $this->assertSame( $p1, $found->ID ); |
| | 187 | |
| | 188 | $num_queries = $wpdb->num_queries; |
| | 189 | |
| | 190 | $found = get_page_by_path( 'foo', OBJECT, 'wptests_pt' ); |
| | 191 | $this->assertSame( $p2, $found->ID ); |
| | 192 | $num_queries++; |
| | 193 | $this->assertSame( $num_queries, $wpdb->num_queries ); |
| | 194 | } |
| | 195 | |
| | 196 | /** |
| | 197 | * @ticket 36711 |
| | 198 | */ |
| | 199 | public function test_cache_should_be_invalidated_when_post_name_is_edited() { |
| | 200 | global $wpdb; |
| | 201 | |
| | 202 | $page = self::factory()->post->create( array( |
| | 203 | 'post_type' => 'page', |
| | 204 | 'post_name' => 'foo', |
| | 205 | ) ); |
| | 206 | |
| | 207 | // Prime cache. |
| | 208 | $found = get_page_by_path( 'foo' ); |
| | 209 | $this->assertSame( $page, $found->ID ); |
| | 210 | |
| | 211 | wp_update_post( array( |
| | 212 | 'ID' => $page, |
| | 213 | 'post_name' => 'bar', |
| | 214 | ) ); |
| | 215 | |
| | 216 | $num_queries = $wpdb->num_queries; |
| | 217 | |
| | 218 | $found = get_page_by_path( 'bar' ); |
| | 219 | $this->assertSame( $page, $found->ID ); |
| | 220 | $num_queries++; |
| | 221 | $this->assertSame( $num_queries, $wpdb->num_queries ); |
| | 222 | } |