permalinkFormats.php 2.8 KB

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