| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * @group canonical |
|---|
| 5 | * @group rewrite |
|---|
| 6 | * @ticket 12456 |
|---|
| 7 | */ |
|---|
| 8 | class Tests_Canonical_redirectPostId extends WP_Canonical_UnitTestCase { |
|---|
| 9 | protected $post_id; |
|---|
| 10 | |
|---|
| 11 | public function setUp() { |
|---|
| 12 | parent::setUp(); |
|---|
| 13 | |
|---|
| 14 | $this->post_id = self::factory()->post->create( |
|---|
| 15 | array( |
|---|
| 16 | 'post_title' => 'Foo Bar', |
|---|
| 17 | 'post_name' => 'foo-bar', |
|---|
| 18 | ) |
|---|
| 19 | ); |
|---|
| 20 | |
|---|
| 21 | $this->set_permalink_structure( '/%post_id%/%postname%/' ); |
|---|
| 22 | |
|---|
| 23 | flush_rewrite_rules(); |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | public function tearDown() { |
|---|
| 27 | $this->post_id = null; |
|---|
| 28 | |
|---|
| 29 | parent::tearDown(); |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | public function test_old_slug_redirect() { |
|---|
| 33 | $old_permalink = user_trailingslashit( get_site_url().'/'.$this->post_id ); |
|---|
| 34 | |
|---|
| 35 | $permalink = user_trailingslashit( get_permalink( $this->post_id ) ); |
|---|
| 36 | |
|---|
| 37 | $this->go_to( $old_permalink ); |
|---|
| 38 | |
|---|
| 39 | $can_url = $this->get_canonical( $old_permalink ); |
|---|
| 40 | |
|---|
| 41 | $this->assertSame( $permalink, $can_url ); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * Get the canonical URL given a raw URL. |
|---|
| 46 | * |
|---|
| 47 | * @param string $test_url Should be absolute |
|---|
| 48 | * ie http://example.com/category/uncategorized/ |
|---|
| 49 | * @return $can_url Returns the original $test_url if no canonical can be generated, otherwise returns |
|---|
| 50 | * the fully-qualified URL as generated by redirect_canonical(). |
|---|
| 51 | */ |
|---|
| 52 | public function get_canonical( $test_url ) { |
|---|
| 53 | $can_url = redirect_canonical( $test_url, false ); |
|---|
| 54 | if ( ! $can_url ) { |
|---|
| 55 | return $test_url; // No redirect will take place for this request. |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | return $can_url; |
|---|
| 59 | } |
|---|
| 60 | } |
|---|