conditionals.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. <?php
  2. /**
  3. * Test the is_*() functions in query.php across the URL structure
  4. *
  5. * This exercises both query.php and rewrite.php: urls are fed through the rewrite code,
  6. * then we test the effects of each url on the wp_query object.
  7. *
  8. * @group query
  9. * @group rewrite
  10. */
  11. class Tests_Query_Conditionals extends WP_UnitTestCase {
  12. protected $page_ids;
  13. protected $post_ids;
  14. function setUp() {
  15. parent::setUp();
  16. set_current_screen( 'front' );
  17. update_option( 'comments_per_page', 5 );
  18. update_option( 'posts_per_page', 5 );
  19. global $wp_rewrite;
  20. update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' );
  21. create_initial_taxonomies();
  22. $GLOBALS['wp_rewrite']->init();
  23. flush_rewrite_rules();
  24. }
  25. function tearDown() {
  26. parent::tearDown();
  27. $GLOBALS['wp_rewrite']->init();
  28. }
  29. function test_home() {
  30. $this->go_to('/');
  31. $this->assertQueryTrue('is_home');
  32. }
  33. function test_404() {
  34. $this->go_to('/'.rand_str());
  35. $this->assertQueryTrue('is_404');
  36. }
  37. function test_permalink() {
  38. $post_id = $this->factory->post->create( array( 'post_title' => 'hello-world' ) );
  39. $this->go_to( get_permalink( $post_id ) );
  40. $this->assertQueryTrue('is_single', 'is_singular');
  41. }
  42. function test_post_comments_feed() {
  43. $post_id = $this->factory->post->create( array( 'post_title' => 'hello-world' ) );
  44. $this->factory->comment->create_post_comments( $post_id, 2 );
  45. $this->go_to( get_post_comments_feed_link( $post_id ) );
  46. $this->assertQueryTrue('is_feed', 'is_single', 'is_singular', 'is_comment_feed');
  47. }
  48. function test_post_comments_feed_with_no_comments() {
  49. $post_id = $this->factory->post->create( array( 'post_title' => 'hello-world' ) );
  50. $this->go_to( get_post_comments_feed_link( $post_id ) );
  51. $this->assertQueryTrue('is_feed', 'is_single', 'is_singular', 'is_comment_feed');
  52. }
  53. function test_page() {
  54. $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'about' ) );
  55. $this->go_to( get_permalink( $page_id ) );
  56. $this->assertQueryTrue('is_page','is_singular');
  57. }
  58. function test_parent_page() {
  59. $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) );
  60. $this->go_to( get_permalink( $page_id ) );
  61. $this->assertQueryTrue('is_page','is_singular');
  62. }
  63. function test_child_page_1() {
  64. $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) );
  65. $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $page_id ) );
  66. $this->go_to( get_permalink( $page_id ) );
  67. $this->assertQueryTrue('is_page','is_singular');
  68. }
  69. function test_child_page_2() {
  70. $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) );
  71. $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $page_id ) );
  72. $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-2', 'post_parent' => $page_id ) );
  73. $this->go_to( get_permalink( $page_id ) );
  74. $this->assertQueryTrue('is_page','is_singular');
  75. }
  76. // '(about)/trackback/?$' => 'index.php?pagename=$matches[1]&tb=1'
  77. function test_page_trackback() {
  78. $page_ids = array();
  79. $page_ids[] = $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) );
  80. $page_ids[] = $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $page_id ) );
  81. $page_ids[] = $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-2', 'post_parent' => $page_id ) );
  82. foreach ( $page_ids as $page_id ) {
  83. $url = get_permalink( $page_id );
  84. $this->go_to("{$url}trackback/");
  85. // make sure the correct wp_query flags are set
  86. $this->assertQueryTrue('is_page','is_singular','is_trackback');
  87. // make sure the correct page was fetched
  88. global $wp_query;
  89. $this->assertEquals( $page_id, $wp_query->get_queried_object()->ID );
  90. }
  91. }
  92. //'(about)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?pagename=$matches[1]&feed=$matches[2]'
  93. function test_page_feed() {
  94. $page_ids = array();
  95. $page_ids[] = $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) );
  96. $page_ids[] = $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $page_id ) );
  97. $page_ids[] = $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-2', 'post_parent' => $page_id ) );
  98. foreach ( $page_ids as $page_id ) {
  99. $this->factory->comment->create_post_comments( $page_id, 2 );
  100. $url = get_permalink( $page_id );
  101. $this->go_to("{$url}feed/");
  102. // make sure the correct wp_query flags are set
  103. $this->assertQueryTrue('is_page', 'is_singular', 'is_feed', 'is_comment_feed');
  104. // make sure the correct page was fetched
  105. global $wp_query;
  106. $this->assertEquals( $page_id, $wp_query->get_queried_object()->ID );
  107. }
  108. }
  109. function test_page_feed_with_no_comments() {
  110. $page_ids = array();
  111. $page_ids[] = $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) );
  112. $page_ids[] = $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $page_id ) );
  113. $page_ids[] = $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-2', 'post_parent' => $page_id ) );
  114. foreach ( $page_ids as $page_id ) {
  115. $url = get_permalink( $page_id );
  116. $this->go_to("{$url}feed/");
  117. // make sure the correct wp_query flags are set
  118. $this->assertQueryTrue('is_page', 'is_singular', 'is_feed', 'is_comment_feed');
  119. // make sure the correct page was fetched
  120. global $wp_query;
  121. $this->assertEquals( $page_id, $wp_query->get_queried_object()->ID );
  122. }
  123. }
  124. // '(about)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?pagename=$matches[1]&feed=$matches[2]'
  125. function test_page_feed_atom() {
  126. $page_ids = array();
  127. $page_ids[] = $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) );
  128. $page_ids[] = $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $page_id ) );
  129. $page_ids[] = $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-2', 'post_parent' => $page_id ) );
  130. foreach ( $page_ids as $page_id ) {
  131. $this->factory->comment->create_post_comments( $page_id, 2 );
  132. $url = get_permalink( $page_id );
  133. $this->go_to("{$url}feed/atom/");
  134. // make sure the correct wp_query flags are set
  135. $this->assertQueryTrue('is_page', 'is_singular', 'is_feed', 'is_comment_feed');
  136. // make sure the correct page was fetched
  137. global $wp_query;
  138. $this->assertEquals( $page_id, $wp_query->get_queried_object()->ID );
  139. }
  140. }
  141. // '(about)/page/?([0-9]{1,})/?$' => 'index.php?pagename=$matches[1]&paged=$matches[2]'
  142. function test_page_page_2() {
  143. $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'about', 'post_content' => 'Page 1 <!--nextpage--> Page 2' ) );
  144. $this->go_to("/about/page/2/");
  145. // make sure the correct wp_query flags are set
  146. $this->assertQueryTrue('is_page', 'is_singular', 'is_paged');
  147. // make sure the correct page was fetched
  148. global $wp_query;
  149. $this->assertEquals( $page_id, $wp_query->get_queried_object()->ID );
  150. }
  151. // '(about)/page/?([0-9]{1,})/?$' => 'index.php?pagename=$matches[1]&paged=$matches[2]'
  152. function test_page_page_2_no_slash() {
  153. $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'about', 'post_content' => 'Page 1 <!--nextpage--> Page 2' ) );
  154. $this->go_to("/about/page2/");
  155. // make sure the correct wp_query flags are set
  156. $this->assertQueryTrue('is_page', 'is_singular', 'is_paged');
  157. // make sure the correct page was fetched
  158. global $wp_query;
  159. $this->assertEquals( $page_id, $wp_query->get_queried_object()->ID );
  160. }
  161. // FIXME: what is this for?
  162. // '(about)(/[0-9]+)?/?$' => 'index.php?pagename=$matches[1]&page=$matches[2]'
  163. function test_pagination_of_posts_page() {
  164. $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'about', 'post_content' => 'Page 1 <!--nextpage--> Page 2' ) );
  165. update_option( 'show_on_front', 'page' );
  166. update_option( 'page_for_posts', $page_id );
  167. $this->go_to('/about/2/');
  168. $this->assertQueryTrue( 'is_home', 'is_posts_page' );
  169. // make sure the correct page was fetched
  170. global $wp_query;
  171. $this->assertEquals( $page_id, $wp_query->get_queried_object()->ID );
  172. }
  173. // FIXME: no tests for these yet
  174. // 'about/attachment/([^/]+)/?$' => 'index.php?attachment=$matches[1]',
  175. // 'about/attachment/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
  176. // 'about/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
  177. // 'about/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
  178. // 'feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?&feed=$matches[1]',
  179. // '(feed|rdf|rss|rss2|atom)/?$' => 'index.php?&feed=$matches[1]',
  180. function test_main_feed_2() {
  181. $this->factory->post->create(); // @test_404
  182. $feeds = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  183. // long version
  184. foreach ($feeds as $feed) {
  185. $this->go_to("/feed/{$feed}/");
  186. $this->assertQueryTrue('is_feed');
  187. }
  188. // short version
  189. foreach ($feeds as $feed) {
  190. $this->go_to("/{$feed}/");
  191. $this->assertQueryTrue('is_feed');
  192. }
  193. }
  194. function test_main_feed() {
  195. $this->factory->post->create(); // @test_404
  196. $types = array('rss2', 'rss', 'atom');
  197. foreach ($types as $type) {
  198. $this->go_to(get_feed_link($type));
  199. $this->assertQueryTrue('is_feed');
  200. }
  201. }
  202. // 'page/?([0-9]{1,})/?$' => 'index.php?&paged=$matches[1]',
  203. function test_paged() {
  204. $this->factory->post->create_many( 15 );
  205. for ( $i = 2; $i <= 3; $i++ ) {
  206. $this->go_to("/page/{$i}/");
  207. $this->assertQueryTrue('is_home', 'is_paged');
  208. }
  209. }
  210. // 'comments/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?&feed=$matches[1]&withcomments=1',
  211. // 'comments/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?&feed=$matches[1]&withcomments=1',
  212. function test_main_comments_feed() {
  213. $post_id = $this->factory->post->create( array( 'post_title' => 'hello-world' ) );
  214. $this->factory->comment->create_post_comments( $post_id, 2 );
  215. // check the url as generated by get_post_comments_feed_link()
  216. $this->go_to( get_post_comments_feed_link( $post_id ) );
  217. $this->assertQueryTrue('is_feed', 'is_single', 'is_singular', 'is_comment_feed');
  218. // check the long form
  219. $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  220. foreach ($types as $type) {
  221. $this->go_to("/comments/feed/{$type}");
  222. $this->assertQueryTrue('is_feed', 'is_comment_feed');
  223. }
  224. // check the short form
  225. $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  226. foreach ($types as $type) {
  227. $this->go_to("/comments/{$type}");
  228. $this->assertQueryTrue('is_feed', 'is_comment_feed');
  229. }
  230. }
  231. // 'search/(.+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?s=$matches[1]&feed=$matches[2]',
  232. // 'search/(.+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?s=$matches[1]&feed=$matches[2]',
  233. function test_search_feed() {
  234. // check the long form
  235. $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  236. foreach ($types as $type) {
  237. $this->go_to("/search/test/feed/{$type}");
  238. $this->assertQueryTrue('is_feed', 'is_search');
  239. }
  240. // check the short form
  241. $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  242. foreach ($types as $type) {
  243. $this->go_to("/search/test/{$type}");
  244. $this->assertQueryTrue('is_feed', 'is_search');
  245. }
  246. }
  247. // 'search/(.+)/page/?([0-9]{1,})/?$' => 'index.php?s=$matches[1]&paged=$matches[2]',
  248. function test_search_paged() {
  249. $this->factory->post->create_many( 10, array( 'post_title' => 'test' ) );
  250. $this->go_to('/search/test/page/2/');
  251. $this->assertQueryTrue('is_search', 'is_paged');
  252. }
  253. // 'search/(.+)/?$' => 'index.php?s=$matches[1]',
  254. function test_search() {
  255. $this->go_to('/search/test/');
  256. $this->assertQueryTrue('is_search');
  257. }
  258. /**
  259. * @ticket 13961
  260. */
  261. function test_search_encoded_chars() {
  262. $this->go_to('/search/F%C3%BCnf%2Bbar/');
  263. $this->assertEquals( get_query_var( 's' ), 'Fünf+bar' );
  264. }
  265. // 'category/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?category_name=$matches[1]&feed=$matches[2]',
  266. // 'category/(.+?)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?category_name=$matches[1]&feed=$matches[2]',
  267. function test_category_feed() {
  268. $this->factory->term->create( array( 'name' => 'cat-a', 'taxonomy' => 'category' ) );
  269. // check the long form
  270. $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  271. foreach ($types as $type) {
  272. $this->go_to("/category/cat-a/feed/{$type}");
  273. $this->assertQueryTrue('is_archive', 'is_feed', 'is_category');
  274. }
  275. // check the short form
  276. $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  277. foreach ($types as $type) {
  278. $this->go_to("/category/cat-a/{$type}");
  279. $this->assertQueryTrue('is_archive', 'is_feed', 'is_category');
  280. }
  281. }
  282. // 'category/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?category_name=$matches[1]&paged=$matches[2]',
  283. function test_category_paged() {
  284. $this->factory->post->create_many( 10 );
  285. $this->go_to('/category/uncategorized/page/2/');
  286. $this->assertQueryTrue('is_archive', 'is_category', 'is_paged');
  287. }
  288. // 'category/(.+?)/?$' => 'index.php?category_name=$matches[1]',
  289. function test_category() {
  290. $this->factory->term->create( array( 'name' => 'cat-a', 'taxonomy' => 'category' ) );
  291. $this->go_to('/category/cat-a/');
  292. $this->assertQueryTrue('is_archive', 'is_category');
  293. }
  294. // 'tag/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag=$matches[1]&feed=$matches[2]',
  295. // 'tag/(.+?)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag=$matches[1]&feed=$matches[2]',
  296. function test_tag_feed() {
  297. $this->factory->term->create( array( 'name' => 'tag-a', 'taxonomy' => 'post_tag' ) );
  298. // check the long form
  299. $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  300. foreach ($types as $type) {
  301. $this->go_to("/tag/tag-a/feed/{$type}");
  302. $this->assertQueryTrue('is_archive', 'is_feed', 'is_tag');
  303. }
  304. // check the short form
  305. $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  306. foreach ($types as $type) {
  307. $this->go_to("/tag/tag-a/{$type}");
  308. $this->assertQueryTrue('is_archive', 'is_feed', 'is_tag');
  309. }
  310. }
  311. // 'tag/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?tag=$matches[1]&paged=$matches[2]',
  312. function test_tag_paged() {
  313. $post_ids = $this->factory->post->create_many( 10 );
  314. foreach ( $post_ids as $post_id )
  315. $this->factory->term->add_post_terms( $post_id, 'tag-a', 'post_tag' );
  316. $this->go_to('/tag/tag-a/page/2/');
  317. $this->assertQueryTrue('is_archive', 'is_tag', 'is_paged');
  318. }
  319. // 'tag/(.+?)/?$' => 'index.php?tag=$matches[1]',
  320. function test_tag() {
  321. $term_id = $this->factory->term->create( array( 'name' => 'Tag Named A', 'slug' => 'tag-a', 'taxonomy' => 'post_tag' ) );
  322. $this->go_to('/tag/tag-a/');
  323. $this->assertQueryTrue('is_archive', 'is_tag');
  324. $tag = get_term( $term_id, 'post_tag' );
  325. $this->assertTrue( is_tag() );
  326. $this->assertTrue( is_tag( $tag->name ) );
  327. $this->assertTrue( is_tag( $tag->slug ) );
  328. $this->assertTrue( is_tag( $tag->term_id ) );
  329. $this->assertTrue( is_tag( array() ) );
  330. $this->assertTrue( is_tag( array( $tag->name ) ) );
  331. $this->assertTrue( is_tag( array( $tag->slug ) ) );
  332. $this->assertTrue( is_tag( array( $tag->term_id ) ) );
  333. }
  334. // 'author/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?author_name=$matches[1]&feed=$matches[2]',
  335. // 'author/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?author_name=$matches[1]&feed=$matches[2]',
  336. function test_author_feed() {
  337. $this->factory->user->create( array( 'user_login' => 'user-a' ) );
  338. // check the long form
  339. $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  340. foreach ($types as $type) {
  341. $this->go_to("/author/user-a/feed/{$type}");
  342. $this->assertQueryTrue('is_archive', 'is_feed', 'is_author');
  343. }
  344. // check the short form
  345. $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  346. foreach ($types as $type) {
  347. $this->go_to("/author/user-a/{$type}");
  348. $this->assertQueryTrue('is_archive', 'is_feed', 'is_author');
  349. }
  350. }
  351. // 'author/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?author_name=$matches[1]&paged=$matches[2]',
  352. function test_author_paged() {
  353. $user_id = $this->factory->user->create( array( 'user_login' => 'user-a' ) );
  354. $this->factory->post->create_many( 10, array( 'post_author' => $user_id ) );
  355. $this->go_to('/author/user-a/page/2/');
  356. $this->assertQueryTrue('is_archive', 'is_author', 'is_paged');
  357. }
  358. // 'author/([^/]+)/?$' => 'index.php?author_name=$matches[1]',
  359. function test_author() {
  360. $user_id = $this->factory->user->create( array( 'user_login' => 'user-a' ) );
  361. $this->factory->post->create( array( 'post_author' => $user_id ) );
  362. $this->go_to('/author/user-a/');
  363. $this->assertQueryTrue('is_archive', 'is_author');
  364. }
  365. function test_author_with_no_posts() {
  366. $user_id = $this->factory->user->create( array( 'user_login' => 'user-a' ) );
  367. $this->go_to('/author/user-a/');
  368. $this->assertQueryTrue('is_archive', 'is_author');
  369. }
  370. // '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]',
  371. // '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]',
  372. function test_ymd_feed() {
  373. $this->factory->post->create( array( 'post_date' => '2007-09-04 00:00:00' ) );
  374. // check the long form
  375. $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  376. foreach ($types as $type) {
  377. $this->go_to("/2007/09/04/feed/{$type}");
  378. $this->assertQueryTrue('is_archive', 'is_feed', 'is_day', 'is_date');
  379. }
  380. // check the short form
  381. $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  382. foreach ($types as $type) {
  383. $this->go_to("/2007/09/04/{$type}");
  384. $this->assertQueryTrue('is_archive', 'is_feed', 'is_day', 'is_date');
  385. }
  386. }
  387. // '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]',
  388. function test_ymd_paged() {
  389. $this->factory->post->create_many( 10, array( 'post_date' => '2007-09-04 00:00:00' ) );
  390. $this->go_to('/2007/09/04/page/2/');
  391. $this->assertQueryTrue('is_archive', 'is_day', 'is_date', 'is_paged');
  392. }
  393. // '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]',
  394. function test_ymd() {
  395. $this->factory->post->create( array( 'post_date' => '2007-09-04 00:00:00' ) );
  396. $this->go_to('/2007/09/04/');
  397. $this->assertQueryTrue('is_archive', 'is_day', 'is_date');
  398. }
  399. // '([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]',
  400. // '([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]',
  401. function test_ym_feed() {
  402. $this->factory->post->create( array( 'post_date' => '2007-09-04 00:00:00' ) );
  403. // check the long form
  404. $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  405. foreach ($types as $type) {
  406. $this->go_to("/2007/09/feed/{$type}");
  407. $this->assertQueryTrue('is_archive', 'is_feed', 'is_month', 'is_date');
  408. }
  409. // check the short form
  410. $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  411. foreach ($types as $type) {
  412. $this->go_to("/2007/09/{$type}");
  413. $this->assertQueryTrue('is_archive', 'is_feed', 'is_month', 'is_date');
  414. }
  415. }
  416. // '([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]',
  417. function test_ym_paged() {
  418. $this->factory->post->create_many( 10, array( 'post_date' => '2007-09-04 00:00:00' ) );
  419. $this->go_to('/2007/09/page/2/');
  420. $this->assertQueryTrue('is_archive', 'is_date', 'is_month', 'is_paged');
  421. }
  422. // '([0-9]{4})/([0-9]{1,2})/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]',
  423. function test_ym() {
  424. $this->factory->post->create( array( 'post_date' => '2007-09-04 00:00:00' ) );
  425. $this->go_to('/2007/09/');
  426. $this->assertQueryTrue('is_archive', 'is_date', 'is_month');
  427. }
  428. // '([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?year=$matches[1]&feed=$matches[2]',
  429. // '([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?year=$matches[1]&feed=$matches[2]',
  430. function test_y_feed() {
  431. $this->factory->post->create( array( 'post_date' => '2007-09-04 00:00:00' ) );
  432. // check the long form
  433. $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  434. foreach ($types as $type) {
  435. $this->go_to("/2007/feed/{$type}");
  436. $this->assertQueryTrue('is_archive', 'is_feed', 'is_year', 'is_date');
  437. }
  438. // check the short form
  439. $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  440. foreach ($types as $type) {
  441. $this->go_to("/2007/{$type}");
  442. $this->assertQueryTrue('is_archive', 'is_feed', 'is_year', 'is_date');
  443. }
  444. }
  445. // '([0-9]{4})/page/?([0-9]{1,})/?$' => 'index.php?year=$matches[1]&paged=$matches[2]',
  446. function test_y_paged() {
  447. $this->factory->post->create_many( 10, array( 'post_date' => '2007-09-04 00:00:00' ) );
  448. $this->go_to('/2007/page/2/');
  449. $this->assertQueryTrue('is_archive', 'is_date', 'is_year', 'is_paged');
  450. }
  451. // '([0-9]{4})/?$' => 'index.php?year=$matches[1]',
  452. function test_y() {
  453. $this->factory->post->create( array( 'post_date' => '2007-09-04 00:00:00' ) );
  454. $this->go_to('/2007/');
  455. $this->assertQueryTrue('is_archive', 'is_date', 'is_year');
  456. }
  457. // '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/trackback/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&tb=1',
  458. function test_post_trackback() {
  459. $post_id = $this->factory->post->create();
  460. $permalink = get_permalink( $post_id );
  461. $this->go_to("{$permalink}trackback/");
  462. $this->assertQueryTrue('is_single', 'is_singular', 'is_trackback');
  463. }
  464. // '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&feed=$matches[5]',
  465. // '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&feed=$matches[5]',
  466. function test_post_comment_feed() {
  467. $post_id = $this->factory->post->create();
  468. $permalink = get_permalink( $post_id );
  469. $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  470. foreach ($types as $type) {
  471. $this->go_to("{$permalink}feed/{$type}");
  472. $this->assertQueryTrue('is_single', 'is_singular', 'is_feed', 'is_comment_feed');
  473. }
  474. // check the short form
  475. $types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
  476. foreach ($types as $type) {
  477. $this->go_to("{$permalink}{$type}");
  478. $this->assertQueryTrue('is_single', 'is_singular', 'is_feed', 'is_comment_feed');
  479. }
  480. }
  481. // '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)(/[0-9]+)?/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&page=$matches[5]',
  482. function test_post_paged_short() {
  483. $post_id = $this->factory->post->create( array(
  484. 'post_date' => '2007-09-04 00:00:00',
  485. 'post_title' => 'a-post-with-multiple-pages',
  486. 'post_content' => 'Page 1 <!--nextpage--> Page 2'
  487. ) );
  488. $this->go_to( get_permalink( $post_id ) . '2/' );
  489. // should is_paged be true also?
  490. $this->assertQueryTrue('is_single', 'is_singular');
  491. }
  492. // '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/([^/]+)/?$' => 'index.php?attachment=$matches[1]',
  493. function test_post_attachment() {
  494. $post_id = $this->factory->post->create( array( 'post_type' => 'attachment' ) );
  495. $permalink = get_attachment_link( $post_id );
  496. $this->go_to($permalink);
  497. $this->assertQueryTrue('is_single', 'is_attachment', 'is_singular');
  498. }
  499. // '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
  500. // '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
  501. // '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
  502. // '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/?$' => 'index.php?attachment=$matches[1]',
  503. // '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
  504. // '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
  505. // '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
  506. function test_bad_dates() {
  507. $this->go_to( '/2013/13/13/' );
  508. $this->assertQueryTrue( 'is_404' );
  509. $this->go_to( '/2013/11/41/' );
  510. $this->assertQueryTrue( 'is_404' );
  511. }
  512. function test_post_type_archive_with_tax_query() {
  513. delete_option( 'rewrite_rules' );
  514. $cpt_name = 'ptawtq';
  515. register_post_type( $cpt_name, array(
  516. 'taxonomies' => array( 'post_tag', 'category' ),
  517. 'rewrite' => true,
  518. 'has_archive' => true,
  519. 'public' => true
  520. ) );
  521. $tag_id = $this->factory->tag->create( array( 'slug' => 'tag-slug' ) );
  522. $post_id = $this->factory->post->create( array( 'post_type' => $cpt_name ) );
  523. wp_set_object_terms( $post_id, $tag_id, 'post_tag' );
  524. $this->go_to( '/ptawtq/' );
  525. $this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
  526. $this->assertEquals( get_queried_object(), get_post_type_object( $cpt_name ) );
  527. add_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_tax_query' ) );
  528. $this->go_to( '/ptawtq/' );
  529. $this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
  530. $this->assertEquals( get_queried_object(), get_post_type_object( $cpt_name ) );
  531. remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_tax_query' ) );
  532. }
  533. function pre_get_posts_with_tax_query( &$query ) {
  534. $term = get_term_by( 'slug', 'tag-slug', 'post_tag' );
  535. $query->set( 'tax_query', array(
  536. array( 'taxonomy' => 'post_tag', 'field' => 'term_id', 'terms' => $term->term_id )
  537. ) );
  538. }
  539. function test_post_type_array() {
  540. delete_option( 'rewrite_rules' );
  541. $cpt_name = 'thearray';
  542. register_post_type( $cpt_name, array(
  543. 'taxonomies' => array( 'post_tag', 'category' ),
  544. 'rewrite' => true,
  545. 'has_archive' => true,
  546. 'public' => true
  547. ) );
  548. $this->factory->post->create( array( 'post_type' => $cpt_name ) );
  549. $this->go_to( "/$cpt_name/" );
  550. $this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
  551. $this->assertEquals( get_queried_object(), get_post_type_object( $cpt_name ) );
  552. add_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_type_array' ) );
  553. $this->go_to( "/$cpt_name/" );
  554. $this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
  555. $this->assertEquals( get_queried_object(), get_post_type_object( 'post' ) );
  556. remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_type_array' ) );
  557. }
  558. function pre_get_posts_with_type_array( &$query ) {
  559. $query->set( 'post_type', array( 'post', 'thearray' ) );
  560. }
  561. }