WordPress.org

Make WordPress Core


Ignore:
Timestamp:
08/15/13 20:08:06 (4 years ago)
Author:
ryan
Message:

wp_get_shortlink() improvements.

  • Return shortlinks for pages and public CPTs.
  • Return shortlinks even when cruft-free links are not enabled.
  • Unit tests

Props sillybean, layotte, cais
fixes #18632
see #14760

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/tests/link.php

    r25002 r25030  
    33 * @group link 
    44 */ 
    5 class Tests_Link_Functions extends WP_UnitTestCase { 
     5class Tests_Link extends WP_UnitTestCase { 
    66 
    77    function _get_pagenum_link_cb( $url ) { 
     
    2828        $_SERVER['REQUEST_URI'] = $old_req_uri; 
    2929    } 
     30 
     31    function test_wp_get_shortlink() { 
     32        $post_id = $this->factory->post->create(); 
     33        $post_id2 = $this->factory->post->create(); 
     34 
     35        // Basic case 
     36        $this->assertEquals( get_permalink( $post_id ), 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( get_permalink( $post_id ), wp_get_shortlink( 0, 'post' ) ); 
     47        $this->assertEquals( get_permalink( $post_id ), wp_get_shortlink( 0 ) ); 
     48        $this->assertEquals( get_permalink( $post_id ), wp_get_shortlink() ); 
     49 
     50        // Not the global post 
     51        $this->assertEquals( get_permalink( $post_id2 ), 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, get_permalink() will no longer match. 
     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        // Don't test against get_permalink() since it uses ?page_id= for pages. 
     84        $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) ); 
     85 
     86        global $wp_rewrite; 
     87        $wp_rewrite->permalink_structure = ''; 
     88        $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); 
     89        $wp_rewrite->flush_rules(); 
     90 
     91        $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) ); 
     92 
     93        $wp_rewrite->set_permalink_structure( '' ); 
     94        $wp_rewrite->flush_rules(); 
     95    } 
     96 
    3097} 
Note: See TracChangeset for help on using the changeset viewer.