| 1 | | <?php |
| 2 | | |
| 3 | | /** |
| 4 | | * @group canonical |
| 5 | | * @group rewrite |
| 6 | | * @group query |
| 7 | | */ |
| 8 | | class Tests_Canonical_PermalinkFormat extends WP_UnitTestCase { |
| 9 | | |
| 10 | | /** |
| 11 | | * @ticket 21167 |
| 12 | | */ |
| 13 | | public function test_dotted_formats() { |
| 14 | | global $wp_rewrite; |
| 15 | | |
| 16 | | // Create a sample post |
| 17 | | $cat_id = $this->factory->term->create( array( |
| 18 | | 'name' => 'permalink-test', |
| 19 | | 'taxonomy' => 'category' |
| 20 | | ) ); |
| 21 | | $user_id = $this->factory->user->create( array( |
| 22 | | 'role' => 'editor', |
| 23 | | 'user_login' => 'permalink_user', |
| 24 | | ) ); |
| 25 | | $post_id = $this->factory->post->create( array( |
| 26 | | 'post_title' => 'lorem-ipsum', |
| 27 | | 'post_date' => '2012-08-02 14:15:05', |
| 28 | | 'post_author' => $user_id, |
| 29 | | 'category' => $cat_id |
| 30 | | ) ); |
| 31 | | wp_set_post_categories( $post_id, array( $cat_id ) ); |
| 32 | | |
| 33 | | // Sample permalinks |
| 34 | | $tests = array( |
| 35 | | '/%postname%.%post_id%/ ' => array( |
| 36 | | 'regex' => '([^/]+)\.([0-9]+)(/[0-9]+)?/?$', |
| 37 | | 'url' => 'index.php?name=$1&p=$2&page=$3' |
| 38 | | ), |
| 39 | | '/%year%.%monthnum%.%postname%/' => array( |
| 40 | | 'regex' => '([0-9]{4})\.([0-9]{1,2})\.([^/]+)(/[0-9]+)?/?$', |
| 41 | | 'url' => 'index.php?year=$1&monthnum=$2&name=$3&page=$4' |
| 42 | | ), |
| 43 | | '/%post_id%.%postname%/' => array( |
| 44 | | 'regex' => '([0-9]+)\.([^/]+)(/[0-9]+)?/?$', |
| 45 | | 'url' => 'index.php?p=$1&name=$2&page=$3' |
| 46 | | ), |
| 47 | | '/%postname%.%year%/' => array( |
| 48 | | 'regex' => '([^/]+)\.([0-9]{4})(/[0-9]+)?/?$', |
| 49 | | 'url' => 'index.php?name=$1&year=$2&page=$3' |
| 50 | | ), |
| 51 | | '/$%postname%$/' => array( |
| 52 | | 'regex' => '\$([^/]+)\$(/[0-9]+)?/?$', |
| 53 | | 'url' => 'index.php?name=$1&page=$2' |
| 54 | | ), |
| 55 | | '%year%.+?%monthnum%.+?%day%.+?%hour%.+?%minute%.+?%second%.+?%post_id%.+?%postname%.+?%category%.+?%author%.+?' => array( |
| 56 | | 'regex' => '([0-9]{4})\.\+\?([0-9]{1,2})\.\+\?([0-9]{1,2})\.\+\?([0-9]{1,2})\.\+\?([0-9]{1,2})\.\+\?([0-9]{1,2})\.\+\?([0-9]+)\.\+\?([^/]+)\.\+\?%category%\.\+\?([^/]+)\.\+\?(/[0-9]+)?/?$', |
| 57 | | 'url' => 'index.php?year=$1&monthnum=$2&day=$3&hour=$4&minute=$5&second=$6&p=$7&name=$8&%category%$9&author_name=$10&page=$11' |
| 58 | | ), |
| 59 | | ); |
| 60 | | |
| 61 | | // Test permalinks |
| 62 | | foreach ( $tests as $permalink_format => $expected ) { |
| 63 | | update_option( 'permalink_structure', $permalink_format ); |
| 64 | | |
| 65 | | // Get the rewrite rules |
| 66 | | $rules = $wp_rewrite->generate_rewrite_rules( get_option( 'permalink_structure' ), EP_PERMALINK, false, false, false, false ); |
| 67 | | |
| 68 | | // Filter out only the post rewrite rule |
| 69 | | foreach ( $rules as $regex => $url ) { |
| 70 | | if ( false === strpos( $url, 'attachment=$' ) && false === strpos( $url, 'tb=' ) && false === strpos( $url, 'cpage=$' ) ) { |
| 71 | | break; |
| 72 | | } |
| 73 | | } |
| 74 | | |
| 75 | | // Test that expected === actual |
| 76 | | $this->assertEquals( $regex, $expected['regex'], "Problem with permalink format: $permalink_format" ); |
| 77 | | $this->assertEquals( $url, $expected['url'], "Problem with permalink format: $permalink_format" ); |
| 78 | | } |
| 79 | | } |
| 80 | | } |