getPages.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * @group post
  4. */
  5. class Tests_Post_getPages extends WP_UnitTestCase {
  6. function setUp() {
  7. parent::setUp();
  8. }
  9. /**
  10. * @ticket 23167
  11. */
  12. function test_get_pages_cache() {
  13. global $wpdb;
  14. $this->factory->post->create_many( 15, array( 'post_type' => 'page' ) );
  15. wp_cache_delete( 'last_changed', 'posts' );
  16. $this->assertFalse( wp_cache_get( 'last_changed', 'posts' ) );
  17. $pages = get_pages();
  18. $this->assertEquals( 15, count( $pages ) );
  19. $this->assertNotEmpty( $time1 = wp_cache_get( 'last_changed', 'posts' ) );
  20. $num_queries = $wpdb->num_queries;
  21. foreach ( $pages as $page )
  22. $this->assertInstanceOf( 'WP_Post', $page );
  23. // Again. num_queries and last_changed should remain the same.
  24. $pages = get_pages();
  25. $this->assertEquals( 15, count( $pages ) );
  26. $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
  27. $this->assertEquals( $num_queries, $wpdb->num_queries );
  28. foreach ( $pages as $page )
  29. $this->assertInstanceOf( 'WP_Post', $page );
  30. // Again with different args. last_changed should not increment because of
  31. // different args to get_pages(). num_queries should bump by 1.
  32. $pages = get_pages( array( 'number' => 10 ) );
  33. $this->assertEquals( 10, count( $pages ) );
  34. $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
  35. $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
  36. foreach ( $pages as $page )
  37. $this->assertInstanceOf( 'WP_Post', $page );
  38. $num_queries = $wpdb->num_queries;
  39. // Again. num_queries and last_changed should remain the same.
  40. $pages = get_pages( array( 'number' => 10 ) );
  41. $this->assertEquals( 10, count( $pages ) );
  42. $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
  43. $this->assertEquals( $num_queries, $wpdb->num_queries );
  44. foreach ( $pages as $page )
  45. $this->assertInstanceOf( 'WP_Post', $page );
  46. // Do the first query again. The interim queries should not affect it.
  47. $pages = get_pages();
  48. $this->assertEquals( 15, count( $pages ) );
  49. $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
  50. $this->assertEquals( $num_queries, $wpdb->num_queries );
  51. foreach ( $pages as $page )
  52. $this->assertInstanceOf( 'WP_Post', $page );
  53. // Force last_changed to increment.
  54. clean_post_cache( $pages[0]->ID );
  55. $this->assertNotEquals( $time1, $time2 = wp_cache_get( 'last_changed', 'posts' ) );
  56. $num_queries = $wpdb->num_queries;
  57. // last_changed bumped so num_queries should increment.
  58. $pages = get_pages( array( 'number' => 10 ) );
  59. $this->assertEquals( 10, count( $pages ) );
  60. $this->assertEquals( $time2, wp_cache_get( 'last_changed', 'posts' ) );
  61. $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
  62. foreach ( $pages as $page )
  63. $this->assertInstanceOf( 'WP_Post', $page );
  64. $last_changed = wp_cache_get( 'last_changed', 'posts' );
  65. // This should bump last_changed.
  66. wp_delete_post( $pages[0]->ID );
  67. $this->assertGreaterThan( $last_changed, wp_cache_get( 'last_changed', 'posts' ) );
  68. $num_queries = $wpdb->num_queries;
  69. $last_changed = wp_cache_get( 'last_changed', 'posts' );
  70. // num_queries should bump after wp_delete_post() bumps last_changed.
  71. $pages = get_pages();
  72. $this->assertEquals( 14, count( $pages ) );
  73. $this->assertEquals( $last_changed, wp_cache_get( 'last_changed', 'posts' ) );
  74. $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
  75. foreach ( $pages as $page )
  76. $this->assertInstanceOf( 'WP_Post', $page );
  77. }
  78. /**
  79. * @ticket 20376
  80. */
  81. function test_get_pages_meta() {
  82. $posts = $this->factory->post->create_many( 3, array( 'post_type' => 'page' ) );
  83. add_post_meta( $posts[0], 'some-meta-key', '0' );
  84. add_post_meta( $posts[1], 'some-meta-key', '' );
  85. add_post_meta( $posts[2], 'some-meta-key', '1' );
  86. $this->assertEquals( 1, count( get_pages( array( 'meta_key' => 'some-meta-key', 'meta_value' => '0' ) ) ) );
  87. $this->assertEquals( 1, count( get_pages( array( 'meta_key' => 'some-meta-key', 'meta_value' => '1' ) ) ) );
  88. $this->assertEquals( 3, count( get_pages( array( 'meta_key' => 'some-meta-key' ) ) ) );
  89. }
  90. /**
  91. * @ticket 22074
  92. */
  93. function test_get_pages_include_exclude() {
  94. $page_ids = array();
  95. foreach ( range( 1, 20 ) as $i )
  96. $page_ids[] = $this->factory->post->create( array( 'post_type' => 'page' ) );
  97. $inc = array_slice( $page_ids, 0, 10 );
  98. sort( $inc );
  99. $exc = array_slice( $page_ids, 10 );
  100. sort( $exc );
  101. $include = get_pages( array( 'include' => $inc ) );
  102. $inc_result = wp_list_pluck( $include, 'ID' );
  103. sort( $inc_result );
  104. $this->assertEquals( $inc, $inc_result );
  105. $exclude = get_pages( array( 'exclude' => $exc ) );
  106. $exc_result = wp_list_pluck( $exclude, 'ID' );
  107. sort( $exc_result );
  108. $this->assertEquals( $inc, $exc_result );
  109. }
  110. /**
  111. * @ticket 9470
  112. */
  113. function test_get_pages_parent() {
  114. $page_id1 = $this->factory->post->create( array( 'post_type' => 'page' ) );
  115. $page_id2 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id1 ) );
  116. $page_id3 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id2 ) );
  117. $page_id4 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id1 ) );
  118. $pages = get_pages( array( 'parent' => 0, 'hierarchical' => false ) );
  119. $this->assertEqualSets( array( $page_id1 ), wp_list_pluck( $pages, 'ID' ) );
  120. $pages = get_pages( array( 'parent' => $page_id1, 'hierarchical' => false ) );
  121. $this->assertEqualSets( array( $page_id2, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
  122. $pages = get_pages( array( 'parent' => array( $page_id1, $page_id2 ), 'hierarchical' => false ) );
  123. $this->assertEqualSets( array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
  124. $pages = get_pages( array( 'parent' => 0 ) );
  125. $this->assertEqualSets( array( $page_id1 ), wp_list_pluck( $pages, 'ID' ) );
  126. $pages = get_pages( array( 'parent' => $page_id1 ) );
  127. $this->assertEqualSets( array( $page_id2, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
  128. $pages = get_pages( array( 'parent' => array( $page_id1, $page_id2 ) ) );
  129. $this->assertEqualSets( array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
  130. }
  131. /**
  132. * @ticket 22389
  133. */
  134. function test_wp_dropdown_pages() {
  135. $posts = $this->factory->post->create_many( 5, array( 'post_type' => 'page' ) );
  136. preg_match_all( '#<option#', wp_dropdown_pages( 'echo=0' ), $matches );
  137. $this->assertEquals( 5, count( $matches[0] ) );
  138. }
  139. /**
  140. * @ticket 22208
  141. */
  142. function test_get_chidren_fields_ids() {
  143. $post_id = $this->factory->post->create();
  144. $child_ids = $this->factory->post->create_many( 5, array( 'post_parent' => $post_id ) );
  145. $post_ids = get_children( array( 'fields' => 'ids', 'post_parent' => $post_id ) );
  146. $this->assertEqualSets( $child_ids, $post_ids );
  147. }
  148. /**
  149. * @ticket 25750
  150. */
  151. function test_get_pages_hierarchical_and_no_parent() {
  152. global $wpdb;
  153. $page_1 = $this->factory->post->create( array( 'post_type' => 'page' ) );
  154. $page_2 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
  155. $page_3 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
  156. $page_4 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_2 ) );
  157. $pages = get_pages(); // Defaults: hierarchical = true, parent = -1
  158. $pages_default_args = get_pages( array( 'hierarchical' => true, 'parent' => -1 ) );
  159. // Confirm the defaults.
  160. $this->assertEquals( $pages, $pages_default_args );
  161. /*
  162. * Here's the tree we are testing:
  163. *
  164. * page 1
  165. * - page 2
  166. * -- page 4
  167. * - page 3
  168. *
  169. * If hierarchical => true works, the order will be 1,2,4,3.
  170. * If it doesn't, they will be in the creation order, 1,2,3,4.
  171. */
  172. $this->assertEqualSets( array( $page_1, $page_2, $page_4, $page_3 ), wp_list_pluck( $pages, 'ID' ) );
  173. }
  174. }