getPages.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. <?php
  2. /**
  3. * @group post
  4. */
  5. class Tests_Post_GetPages extends WP_UnitTestCase {
  6. /**
  7. * @ticket 23167
  8. */
  9. function test_get_pages_cache() {
  10. global $wpdb;
  11. self::factory()->post->create_many( 3, array( 'post_type' => 'page' ) );
  12. wp_cache_delete( 'last_changed', 'posts' );
  13. $this->assertFalse( wp_cache_get( 'last_changed', 'posts' ) );
  14. $pages = get_pages();
  15. $this->assertSame( 3, count( $pages ) );
  16. $time1 = wp_cache_get( 'last_changed', 'posts' );
  17. $this->assertNotEmpty( $time1 );
  18. $num_queries = $wpdb->num_queries;
  19. foreach ( $pages as $page ) {
  20. $this->assertInstanceOf( 'WP_Post', $page );
  21. }
  22. // Again. num_queries and last_changed should remain the same.
  23. $pages = get_pages();
  24. $this->assertSame( 3, count( $pages ) );
  25. $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) );
  26. $this->assertSame( $num_queries, $wpdb->num_queries );
  27. foreach ( $pages as $page ) {
  28. $this->assertInstanceOf( 'WP_Post', $page );
  29. }
  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' => 2 ) );
  33. $this->assertSame( 2, count( $pages ) );
  34. $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) );
  35. $this->assertSame( $num_queries + 1, $wpdb->num_queries );
  36. foreach ( $pages as $page ) {
  37. $this->assertInstanceOf( 'WP_Post', $page );
  38. }
  39. $num_queries = $wpdb->num_queries;
  40. // Again. num_queries and last_changed should remain the same.
  41. $pages = get_pages( array( 'number' => 2 ) );
  42. $this->assertSame( 2, count( $pages ) );
  43. $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) );
  44. $this->assertSame( $num_queries, $wpdb->num_queries );
  45. foreach ( $pages as $page ) {
  46. $this->assertInstanceOf( 'WP_Post', $page );
  47. }
  48. // Do the first query again. The interim queries should not affect it.
  49. $pages = get_pages();
  50. $this->assertSame( 3, count( $pages ) );
  51. $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) );
  52. $this->assertSame( $num_queries, $wpdb->num_queries );
  53. foreach ( $pages as $page ) {
  54. $this->assertInstanceOf( 'WP_Post', $page );
  55. }
  56. // Force last_changed to increment.
  57. clean_post_cache( $pages[0]->ID );
  58. $this->assertNotEquals( $time1, $time2 = wp_cache_get( 'last_changed', 'posts' ) );
  59. $num_queries = $wpdb->num_queries;
  60. // last_changed bumped so num_queries should increment.
  61. $pages = get_pages( array( 'number' => 2 ) );
  62. $this->assertSame( 2, count( $pages ) );
  63. $this->assertSame( $time2, wp_cache_get( 'last_changed', 'posts' ) );
  64. $this->assertSame( $num_queries + 1, $wpdb->num_queries );
  65. foreach ( $pages as $page ) {
  66. $this->assertInstanceOf( 'WP_Post', $page );
  67. }
  68. $last_changed = wp_cache_get( 'last_changed', 'posts' );
  69. // This should bump last_changed.
  70. wp_delete_post( $pages[0]->ID );
  71. $old_changed_float = $this->_microtime_to_float( $last_changed );
  72. $new_changed_float = $this->_microtime_to_float( wp_cache_get( 'last_changed', 'posts' ) );
  73. $this->assertGreaterThan( $old_changed_float, $new_changed_float );
  74. $num_queries = $wpdb->num_queries;
  75. $last_changed = wp_cache_get( 'last_changed', 'posts' );
  76. // num_queries should bump after wp_delete_post() bumps last_changed.
  77. $pages = get_pages();
  78. $this->assertSame( 2, count( $pages ) );
  79. $this->assertSame( $last_changed, wp_cache_get( 'last_changed', 'posts' ) );
  80. $this->assertSame( $num_queries + 1, $wpdb->num_queries );
  81. foreach ( $pages as $page ) {
  82. $this->assertInstanceOf( 'WP_Post', $page );
  83. }
  84. }
  85. /**
  86. * @ticket 40669
  87. */
  88. public function test_cache_should_be_invalidated_by_add_post_meta() {
  89. $posts = self::factory()->post->create_many(
  90. 2,
  91. array(
  92. 'post_type' => 'page',
  93. )
  94. );
  95. add_post_meta( $posts[0], 'foo', 'bar' );
  96. $cached = get_pages(
  97. array(
  98. 'meta_key' => 'foo',
  99. 'meta_value' => 'bar',
  100. )
  101. );
  102. $cached_ids = wp_list_pluck( $cached, 'ID' );
  103. $this->assertSameSets( array( $posts[0] ), $cached_ids );
  104. add_post_meta( $posts[1], 'foo', 'bar' );
  105. $found = get_pages(
  106. array(
  107. 'meta_key' => 'foo',
  108. 'meta_value' => 'bar',
  109. )
  110. );
  111. $found_ids = wp_list_pluck( $found, 'ID' );
  112. $this->assertSameSets( $posts, $found_ids );
  113. }
  114. /**
  115. * @ticket 40669
  116. */
  117. public function test_cache_should_be_invalidated_by_update_post_meta() {
  118. $posts = self::factory()->post->create_many(
  119. 2,
  120. array(
  121. 'post_type' => 'page',
  122. )
  123. );
  124. add_post_meta( $posts[0], 'foo', 'bar' );
  125. add_post_meta( $posts[1], 'foo', 'bar' );
  126. $cached = get_pages(
  127. array(
  128. 'meta_key' => 'foo',
  129. 'meta_value' => 'bar',
  130. )
  131. );
  132. $cached_ids = wp_list_pluck( $cached, 'ID' );
  133. $this->assertSameSets( $posts, $cached_ids );
  134. update_post_meta( $posts[1], 'foo', 'baz' );
  135. $found = get_pages(
  136. array(
  137. 'meta_key' => 'foo',
  138. 'meta_value' => 'bar',
  139. )
  140. );
  141. $found_ids = wp_list_pluck( $found, 'ID' );
  142. $this->assertSameSets( array( $posts[0] ), $found_ids );
  143. }
  144. /**
  145. * @ticket 40669
  146. */
  147. public function test_cache_should_be_invalidated_by_delete_post_meta() {
  148. $posts = self::factory()->post->create_many(
  149. 2,
  150. array(
  151. 'post_type' => 'page',
  152. )
  153. );
  154. add_post_meta( $posts[0], 'foo', 'bar' );
  155. add_post_meta( $posts[1], 'foo', 'bar' );
  156. $cached = get_pages(
  157. array(
  158. 'meta_key' => 'foo',
  159. 'meta_value' => 'bar',
  160. )
  161. );
  162. $cached_ids = wp_list_pluck( $cached, 'ID' );
  163. $this->assertSameSets( $posts, $cached_ids );
  164. delete_post_meta( $posts[1], 'foo' );
  165. $found = get_pages(
  166. array(
  167. 'meta_key' => 'foo',
  168. 'meta_value' => 'bar',
  169. )
  170. );
  171. $found_ids = wp_list_pluck( $found, 'ID' );
  172. $this->assertSameSets( array( $posts[0] ), $found_ids );
  173. }
  174. /**
  175. * @ticket 40669
  176. */
  177. public function test_cache_should_be_invalidated_by_delete_post_meta_by_key() {
  178. $posts = self::factory()->post->create_many(
  179. 2,
  180. array(
  181. 'post_type' => 'page',
  182. )
  183. );
  184. add_post_meta( $posts[0], 'foo', 'bar' );
  185. add_post_meta( $posts[1], 'foo', 'bar' );
  186. $cached = get_pages(
  187. array(
  188. 'meta_key' => 'foo',
  189. 'meta_value' => 'bar',
  190. )
  191. );
  192. $cached_ids = wp_list_pluck( $cached, 'ID' );
  193. $this->assertSameSets( $posts, $cached_ids );
  194. delete_post_meta_by_key( 'foo' );
  195. $found = get_pages(
  196. array(
  197. 'meta_key' => 'foo',
  198. 'meta_value' => 'bar',
  199. )
  200. );
  201. $found_ids = wp_list_pluck( $found, 'ID' );
  202. $this->assertSameSets( array(), $found_ids );
  203. }
  204. /**
  205. * @ticket 20376
  206. */
  207. function test_get_pages_meta() {
  208. $posts = self::factory()->post->create_many( 3, array( 'post_type' => 'page' ) );
  209. add_post_meta( $posts[0], 'some-meta-key', '0' );
  210. add_post_meta( $posts[1], 'some-meta-key', '' );
  211. add_post_meta( $posts[2], 'some-meta-key', '1' );
  212. $this->assertSame(
  213. 1,
  214. count(
  215. get_pages(
  216. array(
  217. 'meta_key' => 'some-meta-key',
  218. 'meta_value' => '0',
  219. )
  220. )
  221. )
  222. );
  223. $this->assertSame(
  224. 1,
  225. count(
  226. get_pages(
  227. array(
  228. 'meta_key' => 'some-meta-key',
  229. 'meta_value' => '1',
  230. )
  231. )
  232. )
  233. );
  234. $this->assertSame( 3, count( get_pages( array( 'meta_key' => 'some-meta-key' ) ) ) );
  235. }
  236. /**
  237. * @ticket 22074
  238. */
  239. function test_get_pages_include_exclude() {
  240. $page_ids = array();
  241. foreach ( range( 1, 20 ) as $i ) {
  242. $page_ids[] = self::factory()->post->create( array( 'post_type' => 'page' ) );
  243. }
  244. $inc = array_slice( $page_ids, 0, 10 );
  245. sort( $inc );
  246. $exc = array_slice( $page_ids, 10 );
  247. sort( $exc );
  248. $include = get_pages( array( 'include' => $inc ) );
  249. $inc_result = wp_list_pluck( $include, 'ID' );
  250. sort( $inc_result );
  251. $this->assertSame( $inc, $inc_result );
  252. $exclude = get_pages( array( 'exclude' => $exc ) );
  253. $exc_result = wp_list_pluck( $exclude, 'ID' );
  254. sort( $exc_result );
  255. $this->assertSame( $inc, $exc_result );
  256. }
  257. /**
  258. * @ticket 9470
  259. */
  260. function test_get_pages_parent() {
  261. $page_id1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
  262. $page_id2 = self::factory()->post->create(
  263. array(
  264. 'post_type' => 'page',
  265. 'post_parent' => $page_id1,
  266. )
  267. );
  268. $page_id3 = self::factory()->post->create(
  269. array(
  270. 'post_type' => 'page',
  271. 'post_parent' => $page_id2,
  272. )
  273. );
  274. $page_id4 = self::factory()->post->create(
  275. array(
  276. 'post_type' => 'page',
  277. 'post_parent' => $page_id1,
  278. )
  279. );
  280. $pages = get_pages(
  281. array(
  282. 'parent' => 0,
  283. 'hierarchical' => false,
  284. )
  285. );
  286. $this->assertSameSets( array( $page_id1 ), wp_list_pluck( $pages, 'ID' ) );
  287. $pages = get_pages(
  288. array(
  289. 'parent' => $page_id1,
  290. 'hierarchical' => false,
  291. )
  292. );
  293. $this->assertSameSets( array( $page_id2, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
  294. $pages = get_pages(
  295. array(
  296. 'parent' => array( $page_id1, $page_id2 ),
  297. 'hierarchical' => false,
  298. )
  299. );
  300. $this->assertSameSets( array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
  301. $pages = get_pages( array( 'parent' => 0 ) );
  302. $this->assertSameSets( array( $page_id1 ), wp_list_pluck( $pages, 'ID' ) );
  303. $pages = get_pages( array( 'parent' => $page_id1 ) );
  304. $this->assertSameSets( array( $page_id2, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
  305. $pages = get_pages( array( 'parent' => array( $page_id1, $page_id2 ) ) );
  306. $this->assertSameSets( array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
  307. }
  308. /**
  309. * @ticket 22389
  310. */
  311. function test_wp_dropdown_pages() {
  312. self::factory()->post->create_many( 5, array( 'post_type' => 'page' ) );
  313. preg_match_all( '#<option#', wp_dropdown_pages( 'echo=0' ), $matches );
  314. $this->assertSame( 5, count( $matches[0] ) );
  315. }
  316. /**
  317. * @ticket 22208
  318. */
  319. function test_get_chidren_fields_ids() {
  320. $post_id = self::factory()->post->create();
  321. $child_ids = self::factory()->post->create_many( 5, array( 'post_parent' => $post_id ) );
  322. $post_ids = get_children(
  323. array(
  324. 'fields' => 'ids',
  325. 'post_parent' => $post_id,
  326. )
  327. );
  328. $this->assertSameSets( $child_ids, $post_ids );
  329. }
  330. /**
  331. * @ticket 25750
  332. */
  333. function test_get_pages_hierarchical_and_no_parent() {
  334. global $wpdb;
  335. $page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
  336. $page_2 = self::factory()->post->create(
  337. array(
  338. 'post_type' => 'page',
  339. 'post_parent' => $page_1,
  340. )
  341. );
  342. $page_3 = self::factory()->post->create(
  343. array(
  344. 'post_type' => 'page',
  345. 'post_parent' => $page_1,
  346. )
  347. );
  348. $page_4 = self::factory()->post->create(
  349. array(
  350. 'post_type' => 'page',
  351. 'post_parent' => $page_2,
  352. )
  353. );
  354. $pages = get_pages(); // Defaults: hierarchical = true, parent = -1.
  355. $pages_default_args = get_pages(
  356. array(
  357. 'hierarchical' => true,
  358. 'parent' => -1,
  359. )
  360. );
  361. // Confirm the defaults.
  362. $this->assertEquals( $pages, $pages_default_args );
  363. /*
  364. * Here's the tree we are testing:
  365. *
  366. * page 1
  367. * - page 2
  368. * -- page 4
  369. * - page 3
  370. *
  371. * If hierarchical => true works, the order will be 1,2,4,3.
  372. * If it doesn't, they will be in the creation order, 1,2,3,4.
  373. */
  374. $this->assertSameSets( array( $page_1, $page_2, $page_4, $page_3 ), wp_list_pluck( $pages, 'ID' ) );
  375. }
  376. /**
  377. * @ticket 18701
  378. */
  379. public function test_get_pages_hierarchical_empty_child_of() {
  380. $page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
  381. $page_2 = self::factory()->post->create( array( 'post_type' => 'page' ) );
  382. $page_3 = self::factory()->post->create(
  383. array(
  384. 'post_type' => 'page',
  385. 'post_parent' => $page_1,
  386. )
  387. );
  388. $page_4 = self::factory()->post->create(
  389. array(
  390. 'post_type' => 'page',
  391. 'post_parent' => $page_1,
  392. )
  393. );
  394. $pages = get_pages(); // Defaults: hierarchical = true, child_of = '', parent = -1.
  395. $default_args = get_pages(
  396. array(
  397. 'hierarchical' => true,
  398. 'child_of' => '',
  399. )
  400. );
  401. $this->assertEquals( $pages, $default_args );
  402. /*
  403. * Page tree:
  404. *
  405. * page 1 (parent 0)
  406. * – page 3 (parent 1)
  407. * – page 4 (parent 1)
  408. * page 2 (parent 0)
  409. *
  410. * With default arguments, if child_of is empty (normalized to 0), only pages with a matching
  411. * post_parent will be returned, in the order they were created: 1, 2.
  412. */
  413. $found_pages = wp_list_filter( $pages, array( 'post_parent' => 0 ) );
  414. $this->assertSameSets( array( $page_1, $page_2 ), wp_list_pluck( $found_pages, 'ID' ) );
  415. }
  416. /**
  417. * @ticket 18701
  418. */
  419. public function test_get_pages_non_hierarchical_empty_child_of() {
  420. $page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
  421. $page_2 = self::factory()->post->create( array( 'post_type' => 'page' ) );
  422. $page_3 = self::factory()->post->create(
  423. array(
  424. 'post_type' => 'page',
  425. 'post_parent' => $page_1,
  426. )
  427. );
  428. $page_4 = self::factory()->post->create(
  429. array(
  430. 'post_type' => 'page',
  431. 'post_parent' => $page_1,
  432. )
  433. );
  434. $pages = get_pages( array( 'hierarchical' => false ) ); // child_of = '', parent = -1.
  435. /*
  436. * Page tree:
  437. *
  438. * page 1 (parent 0)
  439. * – page 3 (parent 1)
  440. * – page 4 (parent 1)
  441. * page 2 (parent 0)
  442. *
  443. * If hierarchical is false and child_of is empty (normalized to 0), pages will be returned
  444. * in order of creation: 1, 2, 3, 4, regardless of parent.
  445. */
  446. $this->assertSameSets( array( $page_1, $page_2, $page_3, $page_4 ), wp_list_pluck( $pages, 'ID' ) );
  447. }
  448. /**
  449. * @ticket 18701
  450. */
  451. public function test_get_pages_hierarchical_non_empty_child_of() {
  452. $page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
  453. $page_2 = self::factory()->post->create( array( 'post_type' => 'page' ) );
  454. $page_3 = self::factory()->post->create(
  455. array(
  456. 'post_type' => 'page',
  457. 'post_parent' => $page_1,
  458. )
  459. );
  460. $page_4 = self::factory()->post->create(
  461. array(
  462. 'post_type' => 'page',
  463. 'post_parent' => $page_3,
  464. )
  465. );
  466. $page_5 = self::factory()->post->create(
  467. array(
  468. 'post_type' => 'page',
  469. 'post_parent' => $page_1,
  470. )
  471. );
  472. $pages = get_pages( array( 'child_of' => $page_1 ) ); // Defaults: hierarchical = true, parent = -1.
  473. /*
  474. * Page tree:
  475. *
  476. * page 1 (parent 0)
  477. * – page 3 (parent 1)
  478. * –– page 4 (parent 3)
  479. * – page 5 (parent 1)
  480. * page 2 (parent 0)
  481. *
  482. * If hierarchical is true (default), and child_of is not empty, pages will be returned
  483. * hierarchically in order of creation: 3, 4, 5.
  484. */
  485. $this->assertSameSets( array( $page_3, $page_4, $page_5 ), wp_list_pluck( $pages, 'ID' ) );
  486. }
  487. /**
  488. * @ticket 18701
  489. */
  490. public function test_get_pages_non_hierarchical_non_empty_child_of() {
  491. $page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
  492. $page_2 = self::factory()->post->create( array( 'post_type' => 'page' ) );
  493. $page_3 = self::factory()->post->create(
  494. array(
  495. 'post_type' => 'page',
  496. 'post_parent' => $page_1,
  497. )
  498. );
  499. $page_4 = self::factory()->post->create(
  500. array(
  501. 'post_type' => 'page',
  502. 'post_parent' => $page_3,
  503. )
  504. );
  505. $page_5 = self::factory()->post->create(
  506. array(
  507. 'post_type' => 'page',
  508. 'post_parent' => $page_1,
  509. )
  510. );
  511. $pages = get_pages(
  512. array(
  513. 'hierarchical' => false,
  514. 'child_of' => $page_1,
  515. )
  516. );
  517. /*
  518. * Page tree:
  519. *
  520. * page 1 (parent 0)
  521. * – page 3 (parent 1)
  522. * –– page 4 (parent 3)
  523. * – page 5 (parent 1)
  524. * page 2 (parent 0)
  525. *
  526. * If hierarchical is false, and child_of is not empty, pages will (apparently) be returned
  527. * hierarchically anyway in order of creation: 3, 4, 5.
  528. */
  529. $this->assertSameSets( array( $page_3, $page_4, $page_5 ), wp_list_pluck( $pages, 'ID' ) );
  530. // How it should work.
  531. $found_pages = wp_list_filter( $pages, array( 'post_parent' => $page_1 ) );
  532. $this->assertSameSets( array( $page_3, $page_5 ), wp_list_pluck( $found_pages, 'ID' ) );
  533. }
  534. function test_wp_list_pages_classes() {
  535. $type = 'taco';
  536. register_post_type(
  537. $type,
  538. array(
  539. 'hierarchical' => true,
  540. 'public' => true,
  541. )
  542. );
  543. $posts = self::factory()->post->create_many( 2, array( 'post_type' => $type ) );
  544. $post_id = reset( $posts );
  545. $this->go_to( "/?p=$post_id&post_type=$type" );
  546. $this->assertSame( $post_id, get_queried_object_id() );
  547. $output = wp_list_pages(
  548. array(
  549. 'echo' => false,
  550. 'title_li' => '',
  551. 'post_type' => $type,
  552. )
  553. );
  554. $this->assertNotEmpty( $output );
  555. $this->assertSame( 2, substr_count( $output, 'class="page_item ' ) );
  556. $this->assertContains( 'current_page_item', $output );
  557. $this->assertSame( 1, substr_count( $output, 'current_page_item' ) );
  558. _unregister_post_type( $type );
  559. }
  560. function test_exclude_tree() {
  561. $post_id1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
  562. $post_id2 = self::factory()->post->create(
  563. array(
  564. 'post_type' => 'page',
  565. 'post_parent' => $post_id1,
  566. )
  567. );
  568. $post_id3 = self::factory()->post->create( array( 'post_type' => 'page' ) );
  569. $post_id4 = self::factory()->post->create(
  570. array(
  571. 'post_type' => 'page',
  572. 'post_parent' => $post_id3,
  573. )
  574. );
  575. $all = get_pages();
  576. $this->assertCount( 4, $all );
  577. $exclude1 = get_pages( "exclude_tree=$post_id1" );
  578. $this->assertCount( 2, $exclude1 );
  579. $exclude2 = get_pages( array( 'exclude_tree' => $post_id1 ) );
  580. $this->assertCount( 2, $exclude2 );
  581. $exclude3 = get_pages( array( 'exclude_tree' => array( $post_id1 ) ) );
  582. $this->assertCount( 2, $exclude3 );
  583. $exclude4 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id2 ) ) );
  584. $this->assertCount( 2, $exclude4 );
  585. $exclude5 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) );
  586. $this->assertCount( 0, $exclude5 );
  587. $post_id5 = self::factory()->post->create( array( 'post_type' => 'page' ) );
  588. $post_id6 = self::factory()->post->create(
  589. array(
  590. 'post_type' => 'page',
  591. 'post_parent' => $post_id5,
  592. )
  593. );
  594. $exclude6 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) );
  595. $this->assertCount( 2, $exclude6 );
  596. }
  597. /**
  598. * @ticket 43514
  599. */
  600. function test_get_pages_cache_empty() {
  601. global $wpdb;
  602. wp_cache_delete( 'last_changed', 'posts' );
  603. $this->assertFalse( wp_cache_get( 'last_changed', 'posts' ) );
  604. $num_queries = $wpdb->num_queries;
  605. $pages = get_pages(); // Database gets queried.
  606. $this->assertSame( $num_queries + 1, $wpdb->num_queries );
  607. $num_queries = $wpdb->num_queries;
  608. $pages = get_pages(); // Database should not get queried.
  609. $this->assertSame( $num_queries, $wpdb->num_queries );
  610. }
  611. }