numericSlugs.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. /**
  3. * @group rewrite
  4. * @ticket 5305
  5. */
  6. class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase {
  7. private $old_current_user;
  8. public function setUp() {
  9. parent::setUp();
  10. $this->author_id = self::factory()->user->create( array( 'role' => 'editor' ) );
  11. // Override the post/archive slug collision prevention in `wp_unique_post_slug()`.
  12. add_filter( 'wp_unique_post_slug', array( $this, 'filter_unique_post_slug' ), 10, 6 );
  13. }
  14. public function test_go_to_year_segment_collision_without_title() {
  15. global $wpdb;
  16. $this->set_permalink_structure( '/%postname%/' );
  17. $id = self::factory()->post->create(
  18. array(
  19. 'post_author' => $this->author_id,
  20. 'post_status' => 'publish',
  21. 'post_content' => rand_str(),
  22. 'post_title' => '',
  23. 'post_name' => '2015',
  24. 'post_date' => '2015-02-01 01:00:00',
  25. )
  26. );
  27. // Force an ID that resembles a year format.
  28. $wpdb->update(
  29. $wpdb->posts,
  30. array(
  31. 'ID' => '2015',
  32. 'guid' => 'http://' . WP_TESTS_DOMAIN . '/?p=2015',
  33. ),
  34. array( 'ID' => $id )
  35. );
  36. $this->go_to( get_permalink( '2015' ) );
  37. $this->assertQueryTrue( 'is_single', 'is_singular' );
  38. }
  39. public function test_url_to_postid_year_segment_collision_without_title() {
  40. global $wpdb;
  41. $this->set_permalink_structure( '/%postname%/' );
  42. $id = self::factory()->post->create(
  43. array(
  44. 'post_author' => $this->author_id,
  45. 'post_status' => 'publish',
  46. 'post_content' => rand_str(),
  47. 'post_title' => '',
  48. 'post_name' => '2015',
  49. 'post_date' => '2015-02-01 01:00:00',
  50. )
  51. );
  52. // Force an ID that resembles a year format.
  53. $wpdb->update(
  54. $wpdb->posts,
  55. array(
  56. 'ID' => '2015',
  57. 'guid' => 'http://' . WP_TESTS_DOMAIN . '/?p=2015',
  58. ),
  59. array( 'ID' => $id )
  60. );
  61. $this->assertSame( 2015, url_to_postid( get_permalink( '2015' ) ) );
  62. }
  63. public function test_go_to_year_segment_collision_with_title() {
  64. $this->set_permalink_structure( '/%postname%/' );
  65. $id = self::factory()->post->create(
  66. array(
  67. 'post_author' => $this->author_id,
  68. 'post_status' => 'publish',
  69. 'post_content' => rand_str(),
  70. 'post_title' => '2015',
  71. 'post_date' => '2015-02-01 01:00:00',
  72. )
  73. );
  74. $this->go_to( get_permalink( $id ) );
  75. $this->assertQueryTrue( 'is_single', 'is_singular' );
  76. }
  77. public function test_url_to_postid_year_segment_collision_with_title() {
  78. $this->set_permalink_structure( '/%postname%/' );
  79. $id = self::factory()->post->create(
  80. array(
  81. 'post_author' => $this->author_id,
  82. 'post_status' => 'publish',
  83. 'post_content' => rand_str(),
  84. 'post_title' => '2015',
  85. 'post_date' => '2015-02-01 01:00:00',
  86. )
  87. );
  88. $this->assertSame( $id, url_to_postid( get_permalink( $id ) ) );
  89. }
  90. public function test_go_to_month_segment_collision_without_title() {
  91. $this->set_permalink_structure( '/%year%/%postname%/' );
  92. $id = self::factory()->post->create(
  93. array(
  94. 'post_author' => $this->author_id,
  95. 'post_status' => 'publish',
  96. 'post_content' => rand_str(),
  97. 'post_title' => '',
  98. 'post_name' => '02',
  99. 'post_date' => '2015-02-01 01:00:00',
  100. )
  101. );
  102. $this->go_to( get_permalink( $id ) );
  103. $this->assertQueryTrue( 'is_single', 'is_singular' );
  104. }
  105. public function test_url_to_postid_month_segment_collision_without_title() {
  106. $this->set_permalink_structure( '/%year%/%postname%/' );
  107. $id = self::factory()->post->create(
  108. array(
  109. 'post_author' => $this->author_id,
  110. 'post_status' => 'publish',
  111. 'post_content' => rand_str(),
  112. 'post_title' => '',
  113. 'post_name' => '02',
  114. 'post_date' => '2015-02-01 01:00:00',
  115. )
  116. );
  117. $this->assertSame( $id, url_to_postid( get_permalink( $id ) ) );
  118. }
  119. public function test_go_to_month_segment_collision_without_title_no_leading_zero() {
  120. $this->set_permalink_structure( '/%year%/%postname%/' );
  121. $id = self::factory()->post->create(
  122. array(
  123. 'post_author' => $this->author_id,
  124. 'post_status' => 'publish',
  125. 'post_content' => rand_str(),
  126. 'post_title' => '',
  127. 'post_name' => '2',
  128. 'post_date' => '2015-02-01 01:00:00',
  129. )
  130. );
  131. $this->go_to( get_permalink( $id ) );
  132. $this->assertQueryTrue( 'is_single', 'is_singular' );
  133. }
  134. public function test_url_to_postid_month_segment_collision_without_title_no_leading_zero() {
  135. $this->set_permalink_structure( '/%year%/%postname%/' );
  136. $id = self::factory()->post->create(
  137. array(
  138. 'post_author' => $this->author_id,
  139. 'post_status' => 'publish',
  140. 'post_content' => rand_str(),
  141. 'post_title' => '',
  142. 'post_name' => '2',
  143. 'post_date' => '2015-02-01 01:00:00',
  144. )
  145. );
  146. $this->assertSame( $id, url_to_postid( get_permalink( $id ) ) );
  147. }
  148. public function test_go_to_month_segment_collision_with_title() {
  149. $this->set_permalink_structure( '/%year%/%postname%/' );
  150. $id = self::factory()->post->create(
  151. array(
  152. 'post_author' => $this->author_id,
  153. 'post_status' => 'publish',
  154. 'post_content' => rand_str(),
  155. 'post_title' => '02',
  156. 'post_date' => '2015-02-01 01:00:00',
  157. )
  158. );
  159. $this->go_to( get_permalink( $id ) );
  160. $this->assertQueryTrue( 'is_single', 'is_singular' );
  161. }
  162. public function test_url_to_postid_month_segment_collision_with_title() {
  163. $this->set_permalink_structure( '/%year%/%postname%/' );
  164. $id = self::factory()->post->create(
  165. array(
  166. 'post_author' => $this->author_id,
  167. 'post_status' => 'publish',
  168. 'post_content' => rand_str(),
  169. 'post_title' => '02',
  170. 'post_date' => '2015-02-01 01:00:00',
  171. )
  172. );
  173. $this->assertSame( $id, url_to_postid( get_permalink( $id ) ) );
  174. }
  175. public function test_go_to_month_segment_collision_with_title_no_leading_zero() {
  176. $this->set_permalink_structure( '/%year%/%postname%/' );
  177. $id = self::factory()->post->create(
  178. array(
  179. 'post_author' => $this->author_id,
  180. 'post_status' => 'publish',
  181. 'post_content' => rand_str(),
  182. 'post_title' => '2',
  183. 'post_date' => '2015-02-01 01:00:00',
  184. )
  185. );
  186. $this->go_to( get_permalink( $id ) );
  187. $this->assertQueryTrue( 'is_single', 'is_singular' );
  188. }
  189. public function test_url_to_postid_month_segment_collision_with_title_no_leading_zero() {
  190. $this->set_permalink_structure( '/%year%/%postname%/' );
  191. $id = self::factory()->post->create(
  192. array(
  193. 'post_author' => $this->author_id,
  194. 'post_status' => 'publish',
  195. 'post_content' => rand_str(),
  196. 'post_title' => '2',
  197. 'post_date' => '2015-02-01 01:00:00',
  198. )
  199. );
  200. $this->assertSame( $id, url_to_postid( get_permalink( $id ) ) );
  201. }
  202. public function test_go_to_day_segment_collision_without_title() {
  203. $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
  204. $id = self::factory()->post->create(
  205. array(
  206. 'post_author' => $this->author_id,
  207. 'post_status' => 'publish',
  208. 'post_content' => rand_str(),
  209. 'post_title' => '',
  210. 'post_name' => '01',
  211. 'post_date' => '2015-02-01 01:00:00',
  212. )
  213. );
  214. $this->go_to( get_permalink( $id ) );
  215. $this->assertQueryTrue( 'is_single', 'is_singular' );
  216. }
  217. public function test_url_to_postid_day_segment_collision_without_title() {
  218. $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
  219. $id = self::factory()->post->create(
  220. array(
  221. 'post_author' => $this->author_id,
  222. 'post_status' => 'publish',
  223. 'post_content' => rand_str(),
  224. 'post_title' => '',
  225. 'post_name' => '01',
  226. 'post_date' => '2015-02-01 01:00:00',
  227. )
  228. );
  229. $this->assertSame( $id, url_to_postid( get_permalink( $id ) ) );
  230. }
  231. public function test_go_to_day_segment_collision_with_title() {
  232. $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
  233. $id = self::factory()->post->create(
  234. array(
  235. 'post_author' => $this->author_id,
  236. 'post_status' => 'publish',
  237. 'post_content' => rand_str(),
  238. 'post_title' => '01',
  239. 'post_date' => '2015-02-01 01:00:00',
  240. )
  241. );
  242. $this->go_to( get_permalink( $id ) );
  243. $this->assertQueryTrue( 'is_single', 'is_singular' );
  244. }
  245. public function test_url_to_postid_day_segment_collision_with_title() {
  246. $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
  247. $id = self::factory()->post->create(
  248. array(
  249. 'post_author' => $this->author_id,
  250. 'post_status' => 'publish',
  251. 'post_content' => rand_str(),
  252. 'post_title' => '01',
  253. 'post_date' => '2015-02-01 01:00:00',
  254. )
  255. );
  256. $this->assertSame( $id, url_to_postid( get_permalink( $id ) ) );
  257. }
  258. public function test_numeric_slug_permalink_conflicts_should_only_be_resolved_for_the_main_query() {
  259. $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
  260. $id = self::factory()->post->create(
  261. array(
  262. 'post_author' => $this->author_id,
  263. 'post_status' => 'publish',
  264. 'post_content' => rand_str(),
  265. 'post_title' => '01',
  266. 'post_date' => '2015-02-01 01:00:00',
  267. )
  268. );
  269. $q = new WP_Query(
  270. array(
  271. 'year' => '2015',
  272. 'monthnum' => '02',
  273. 'day' => '01',
  274. )
  275. );
  276. $this->assertTrue( $q->is_day );
  277. $this->assertFalse( $q->is_single );
  278. }
  279. public function test_month_slug_collision_should_resolve_to_date_archive_when_year_does_not_match_post_year() {
  280. $this->set_permalink_structure( '/%year%/%postname%/' );
  281. // Make sure a post is published in 2013/02, to avoid 404s.
  282. self::factory()->post->create(
  283. array(
  284. 'post_author' => $this->author_id,
  285. 'post_status' => 'publish',
  286. 'post_content' => 'foo',
  287. 'post_title' => 'bar',
  288. 'post_date' => '2013-02-01 01:00:00',
  289. )
  290. );
  291. $id = self::factory()->post->create(
  292. array(
  293. 'post_author' => $this->author_id,
  294. 'post_status' => 'publish',
  295. 'post_content' => 'foo',
  296. 'post_title' => '02',
  297. 'post_date' => '2015-02-01 01:00:00',
  298. )
  299. );
  300. $permalink = get_permalink( $id );
  301. $permalink = str_replace( '/2015/', '/2013/', $permalink );
  302. $this->go_to( $permalink );
  303. $this->assertTrue( is_month() );
  304. }
  305. public function test_day_slug_collision_should_resolve_to_date_archive_when_monthnum_does_not_match_post_month() {
  306. $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
  307. // Make sure a post is published on 2015/01/01, to avoid 404s.
  308. self::factory()->post->create(
  309. array(
  310. 'post_author' => $this->author_id,
  311. 'post_status' => 'publish',
  312. 'post_content' => 'foo',
  313. 'post_title' => 'bar',
  314. 'post_date' => '2015-01-02 01:00:00',
  315. )
  316. );
  317. $id = self::factory()->post->create(
  318. array(
  319. 'post_author' => $this->author_id,
  320. 'post_status' => 'publish',
  321. 'post_content' => 'foo',
  322. 'post_title' => '02',
  323. 'post_date' => '2015-02-02 01:00:00',
  324. )
  325. );
  326. $permalink = get_permalink( $id );
  327. $permalink = str_replace( '/2015/02/', '/2015/01/', $permalink );
  328. $this->go_to( $permalink );
  329. $this->assertTrue( is_day() );
  330. }
  331. public function test_date_slug_collision_should_distinguish_valid_pagination_from_date() {
  332. $this->set_permalink_structure( '/%year%/%postname%/' );
  333. $id = self::factory()->post->create(
  334. array(
  335. 'post_author' => $this->author_id,
  336. 'post_status' => 'publish',
  337. 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3',
  338. 'post_title' => '02',
  339. 'post_date' => '2015-02-01 01:00:00',
  340. )
  341. );
  342. $this->go_to( get_permalink( $id ) . '1' );
  343. $this->assertFalse( is_day() );
  344. }
  345. public function test_date_slug_collision_should_distinguish_too_high_pagination_from_date() {
  346. $this->set_permalink_structure( '/%year%/%postname%/' );
  347. $id = self::factory()->post->create(
  348. array(
  349. 'post_author' => $this->author_id,
  350. 'post_status' => 'publish',
  351. 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3',
  352. 'post_title' => '02',
  353. 'post_date' => '2015-02-05 01:00:00',
  354. )
  355. );
  356. $this->go_to( get_permalink( $id ) . '5' );
  357. $this->assertTrue( is_day() );
  358. }
  359. public function test_date_slug_collision_should_not_require_pagination_query_var() {
  360. $this->set_permalink_structure( '/%year%/%postname%/' );
  361. $id = self::factory()->post->create(
  362. array(
  363. 'post_author' => $this->author_id,
  364. 'post_status' => 'publish',
  365. 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3',
  366. 'post_title' => '02',
  367. 'post_date' => '2015-02-05 01:00:00',
  368. )
  369. );
  370. $this->go_to( get_permalink( $id ) );
  371. $this->assertQueryTrue( 'is_single', 'is_singular' );
  372. $this->assertFalse( is_date() );
  373. }
  374. public function test_date_slug_collision_should_be_ignored_when_pagination_var_is_present_but_post_does_not_have_multiple_pages() {
  375. $this->set_permalink_structure( '/%year%/%postname%/' );
  376. $id = self::factory()->post->create(
  377. array(
  378. 'post_author' => $this->author_id,
  379. 'post_status' => 'publish',
  380. 'post_content' => 'This post does not have pagination.',
  381. 'post_title' => '02',
  382. 'post_date' => '2015-02-05 01:00:00',
  383. )
  384. );
  385. $this->go_to( get_permalink( $id ) . '5' );
  386. $this->assertTrue( is_day() );
  387. }
  388. public function filter_unique_post_slug( $slug, $post_id, $post_status, $post_type, $post_parent, $original_slug ) {
  389. return $original_slug;
  390. }
  391. }