| 30 | |
| 31 | function test_wp_get_shortlink() { |
| 32 | $post_id = $this->factory->post->create(); |
| 33 | $post_id2 = $this->factory->post->create(); |
| 34 | |
| 35 | // Since there is no permalink structure set, there is no short link |
| 36 | $this->assertEquals( '', wp_get_shortlink( $post_id, 'post' ) ); |
| 37 | |
| 38 | // Global post is not set |
| 39 | $this->assertEquals( '', wp_get_shortlink( 0, 'post' ) ); |
| 40 | $this->assertEquals( '', wp_get_shortlink( 0 ) ); |
| 41 | $this->assertEquals( '', wp_get_shortlink() ); |
| 42 | |
| 43 | $GLOBALS['post'] = get_post( $post_id ); |
| 44 | |
| 45 | // Global post is set |
| 46 | $this->assertEquals( '', wp_get_shortlink( 0, 'post' ) ); |
| 47 | $this->assertEquals( '', wp_get_shortlink( 0 ) ); |
| 48 | $this->assertEquals( '', wp_get_shortlink() ); |
| 49 | |
| 50 | // Not the global post |
| 51 | $this->assertEquals( '', wp_get_shortlink( $post_id2, 'post' ) ); |
| 52 | |
| 53 | unset( $GLOBALS['post'] ); |
| 54 | |
| 55 | // Global post is not set, once again |
| 56 | $this->assertEquals( '', wp_get_shortlink( 0, 'post' ) ); |
| 57 | $this->assertEquals( '', wp_get_shortlink( 0 ) ); |
| 58 | $this->assertEquals( '', wp_get_shortlink() ); |
| 59 | |
| 60 | global $wp_rewrite; |
| 61 | $wp_rewrite->permalink_structure = ''; |
| 62 | $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); |
| 63 | $wp_rewrite->flush_rules(); |
| 64 | |
| 65 | // With a permalink structure set, we now have a shortlink |
| 66 | $this->assertNotEquals( get_permalink( $post_id ), wp_get_shortlink( $post_id, 'post' ) ); |
| 67 | $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) ); |
| 68 | |
| 69 | // Global post and permalink structure are set |
| 70 | $GLOBALS['post'] = get_post( $post_id ); |
| 71 | $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( 0, 'post' ) ); |
| 72 | $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( 0 ) ); |
| 73 | $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink() ); |
| 74 | |
| 75 | $wp_rewrite->set_permalink_structure( '' ); |
| 76 | $wp_rewrite->flush_rules(); |
| 77 | } |
| 78 | |
| 79 | function test_wp_get_shortlink_with_page() { |
| 80 | $post_id = $this->factory->post->create( array( 'post_type' => 'page' ) ); |
| 81 | |
| 82 | // Basic case |
| 83 | $this->assertEquals( '', wp_get_shortlink( $post_id, 'post' ) ); |
| 84 | |
| 85 | global $wp_rewrite; |
| 86 | $wp_rewrite->permalink_structure = ''; |
| 87 | $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); |
| 88 | $wp_rewrite->flush_rules(); |
| 89 | |
| 90 | $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) ); |
| 91 | |
| 92 | $wp_rewrite->set_permalink_structure( '' ); |
| 93 | $wp_rewrite->flush_rules(); |
| 94 | } |
| 95 | |