query.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. <?php
  2. /**
  3. * @group query
  4. * @group post
  5. */
  6. class Tests_Post_Query extends WP_UnitTestCase {
  7. /**
  8. * @group taxonomy
  9. */
  10. function test_category__and_var() {
  11. $q = new WP_Query();
  12. $term_id = self::factory()->category->create(
  13. array(
  14. 'slug' => 'woo',
  15. 'name' => 'WOO!',
  16. )
  17. );
  18. $term_id2 = self::factory()->category->create(
  19. array(
  20. 'slug' => 'hoo',
  21. 'name' => 'HOO!',
  22. )
  23. );
  24. $post_id = self::factory()->post->create();
  25. wp_set_post_categories( $post_id, $term_id );
  26. $posts = $q->query( array( 'category__and' => array( $term_id ) ) );
  27. $this->assertEmpty( $q->get( 'category__and' ) );
  28. $this->assertCount( 0, $q->get( 'category__and' ) );
  29. $this->assertNotEmpty( $q->get( 'category__in' ) );
  30. $this->assertCount( 1, $q->get( 'category__in' ) );
  31. $this->assertNotEmpty( $posts );
  32. $this->assertSame( array( $post_id ), wp_list_pluck( $posts, 'ID' ) );
  33. $posts2 = $q->query( array( 'category__and' => array( $term_id, $term_id2 ) ) );
  34. $this->assertNotEmpty( $q->get( 'category__and' ) );
  35. $this->assertCount( 2, $q->get( 'category__and' ) );
  36. $this->assertEmpty( $q->get( 'category__in' ) );
  37. $this->assertCount( 0, $q->get( 'category__in' ) );
  38. $this->assertEmpty( $posts2 );
  39. }
  40. /**
  41. * @ticket 28099
  42. * @group taxonomy
  43. */
  44. function test_empty_category__in() {
  45. $cat_id = self::factory()->category->create();
  46. $post_id = self::factory()->post->create();
  47. wp_set_post_categories( $post_id, $cat_id );
  48. $q1 = get_posts( array( 'category__in' => array( $cat_id ) ) );
  49. $this->assertNotEmpty( $q1 );
  50. $q2 = get_posts( array( 'category__in' => array() ) );
  51. $this->assertNotEmpty( $q2 );
  52. $tag = wp_insert_term( 'woo', 'post_tag' );
  53. $tag_id = $tag['term_id'];
  54. $slug = get_tag( $tag_id )->slug;
  55. wp_set_post_tags( $post_id, $slug );
  56. $q3 = get_posts( array( 'tag__in' => array( $tag_id ) ) );
  57. $this->assertNotEmpty( $q3 );
  58. $q4 = get_posts( array( 'tag__in' => array() ) );
  59. $this->assertNotEmpty( $q4 );
  60. $q5 = get_posts( array( 'tag_slug__in' => array( $slug ) ) );
  61. $this->assertNotEmpty( $q5 );
  62. $q6 = get_posts( array( 'tag_slug__in' => array() ) );
  63. $this->assertNotEmpty( $q6 );
  64. }
  65. /**
  66. * @ticket 22448
  67. */
  68. function test_the_posts_filter() {
  69. // Create posts and clear their caches.
  70. $post_ids = self::factory()->post->create_many( 4 );
  71. foreach ( $post_ids as $post_id ) {
  72. clean_post_cache( $post_id );
  73. }
  74. add_filter( 'the_posts', array( $this, 'the_posts_filter' ) );
  75. $query = new WP_Query(
  76. array(
  77. 'post_type' => 'post',
  78. 'posts_per_page' => 3,
  79. )
  80. );
  81. // Fourth post added in filter.
  82. $this->assertSame( 4, count( $query->posts ) );
  83. $this->assertSame( 4, $query->post_count );
  84. foreach ( $query->posts as $post ) {
  85. // Posts are WP_Post objects.
  86. $this->assertTrue( is_a( $post, 'WP_Post' ) );
  87. // Filters are raw.
  88. $this->assertSame( 'raw', $post->filter );
  89. // Custom data added in the_posts filter is preserved.
  90. $this->assertSame( array( $post->ID, 'custom data' ), $post->custom_data );
  91. }
  92. remove_filter( 'the_posts', array( $this, 'the_posts_filter' ) );
  93. }
  94. /**
  95. * Use with the_posts filter, appends a post and adds some custom data.
  96. */
  97. function the_posts_filter( $posts ) {
  98. $posts[] = clone $posts[0];
  99. // Add some custom data to each post.
  100. foreach ( $posts as $key => $post ) {
  101. $posts[ $key ]->custom_data = array( $post->ID, 'custom data' );
  102. }
  103. return $posts;
  104. }
  105. function test_post__in_ordering() {
  106. $post_id1 = self::factory()->post->create(
  107. array(
  108. 'post_type' => 'page',
  109. 'menu_order' => rand( 1, 100 ),
  110. )
  111. );
  112. $post_id2 = self::factory()->post->create(
  113. array(
  114. 'post_type' => 'page',
  115. 'menu_order' => rand( 1, 100 ),
  116. )
  117. );
  118. $post_id3 = self::factory()->post->create(
  119. array(
  120. 'post_type' => 'page',
  121. 'post_parent' => $post_id2,
  122. 'menu_order' => rand( 1, 100 ),
  123. )
  124. );
  125. $post_id4 = self::factory()->post->create(
  126. array(
  127. 'post_type' => 'page',
  128. 'post_parent' => $post_id2,
  129. 'menu_order' => rand( 1, 100 ),
  130. )
  131. );
  132. $post_id5 = self::factory()->post->create(
  133. array(
  134. 'post_type' => 'page',
  135. 'menu_order' => rand( 1, 100 ),
  136. )
  137. );
  138. $ordered = array( $post_id2, $post_id4, $post_id3, $post_id1, $post_id5 );
  139. $q = new WP_Query(
  140. array(
  141. 'post_type' => 'any',
  142. 'post__in' => $ordered,
  143. 'orderby' => 'post__in',
  144. )
  145. );
  146. $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) );
  147. }
  148. /**
  149. * @ticket 38034
  150. */
  151. public function test_orderby_post__in_array() {
  152. $posts = self::factory()->post->create_many( 4 );
  153. $ordered = array( $posts[2], $posts[0], $posts[3] );
  154. $q = new WP_Query(
  155. array(
  156. 'post_type' => 'any',
  157. 'post__in' => $ordered,
  158. 'orderby' => array( 'post__in' => 'ASC' ),
  159. )
  160. );
  161. $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) );
  162. }
  163. /**
  164. * @ticket 38034
  165. */
  166. public function test_orderby_post__in_array_with_implied_order() {
  167. $posts = self::factory()->post->create_many( 4 );
  168. $ordered = array( $posts[2], $posts[0], $posts[3] );
  169. $q = new WP_Query(
  170. array(
  171. 'post_type' => 'any',
  172. 'post__in' => $ordered,
  173. 'orderby' => 'post__in',
  174. )
  175. );
  176. $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) );
  177. }
  178. function test_post__in_attachment_ordering() {
  179. $post_id = self::factory()->post->create();
  180. $att_ids = array();
  181. $file = DIR_TESTDATA . '/images/canola.jpg';
  182. $att_ids[1] = self::factory()->attachment->create_object(
  183. $file,
  184. $post_id,
  185. array(
  186. 'post_mime_type' => 'image/jpeg',
  187. 'menu_order' => rand( 1, 100 ),
  188. )
  189. );
  190. $att_ids[2] = self::factory()->attachment->create_object(
  191. $file,
  192. $post_id,
  193. array(
  194. 'post_mime_type' => 'image/jpeg',
  195. 'menu_order' => rand( 1, 100 ),
  196. )
  197. );
  198. $att_ids[3] = self::factory()->attachment->create_object(
  199. $file,
  200. $post_id,
  201. array(
  202. 'post_mime_type' => 'image/jpeg',
  203. 'menu_order' => rand( 1, 100 ),
  204. )
  205. );
  206. $att_ids[4] = self::factory()->attachment->create_object(
  207. $file,
  208. $post_id,
  209. array(
  210. 'post_mime_type' => 'image/jpeg',
  211. 'menu_order' => rand( 1, 100 ),
  212. )
  213. );
  214. $att_ids[5] = self::factory()->attachment->create_object(
  215. $file,
  216. $post_id,
  217. array(
  218. 'post_mime_type' => 'image/jpeg',
  219. 'menu_order' => rand( 1, 100 ),
  220. )
  221. );
  222. $ordered = array( $att_ids[5], $att_ids[1], $att_ids[4], $att_ids[3], $att_ids[2] );
  223. $attached = new WP_Query(
  224. array(
  225. 'post__in' => $ordered,
  226. 'post_type' => 'attachment',
  227. 'post_parent' => $post_id,
  228. 'post_mime_type' => 'image',
  229. 'post_status' => 'inherit',
  230. 'posts_per_page' => '-1',
  231. 'orderby' => 'post__in',
  232. )
  233. );
  234. $this->assertSame( $ordered, wp_list_pluck( $attached->posts, 'ID' ) );
  235. }
  236. /**
  237. * @ticket 36515
  238. */
  239. public function test_post_name__in_ordering() {
  240. $post_id1 = self::factory()->post->create(
  241. array(
  242. 'post_name' => 'id-1',
  243. 'post_type' => 'page',
  244. )
  245. );
  246. $post_id2 = self::factory()->post->create(
  247. array(
  248. 'post_name' => 'id-2',
  249. 'post_type' => 'page',
  250. )
  251. );
  252. $post_id3 = self::factory()->post->create(
  253. array(
  254. 'post_name' => 'id-3',
  255. 'post_type' => 'page',
  256. 'post_parent' => $post_id2,
  257. )
  258. );
  259. $ordered = array( 'id-2', 'id-3', 'id-1' );
  260. $q = new WP_Query(
  261. array(
  262. 'post_type' => 'any',
  263. 'post_name__in' => $ordered,
  264. 'orderby' => 'post_name__in',
  265. )
  266. );
  267. $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'post_name' ) );
  268. }
  269. function test_post_status() {
  270. $statuses1 = get_post_stati();
  271. $this->assertContains( 'auto-draft', $statuses1 );
  272. $statuses2 = get_post_stati( array( 'exclude_from_search' => true ) );
  273. $this->assertContains( 'auto-draft', $statuses2 );
  274. $statuses3 = get_post_stati( array( 'exclude_from_search' => false ) );
  275. $this->assertNotContains( 'auto-draft', $statuses3 );
  276. $q1 = new WP_Query( array( 'post_status' => 'any' ) );
  277. $this->assertContains( "post_status <> 'auto-draft'", $q1->request );
  278. $q2 = new WP_Query( array( 'post_status' => 'any, auto-draft' ) );
  279. $this->assertNotContains( "post_status <> 'auto-draft'", $q2->request );
  280. $q3 = new WP_Query( array( 'post_status' => array( 'any', 'auto-draft' ) ) );
  281. $this->assertNotContains( "post_status <> 'auto-draft'", $q3->request );
  282. }
  283. /**
  284. * @ticket 17065
  285. */
  286. function test_orderby_array() {
  287. global $wpdb;
  288. $q1 = new WP_Query(
  289. array(
  290. 'orderby' => array(
  291. 'type' => 'DESC',
  292. 'name' => 'ASC',
  293. ),
  294. )
  295. );
  296. $this->assertContains(
  297. "ORDER BY $wpdb->posts.post_type DESC, $wpdb->posts.post_name ASC",
  298. $q1->request
  299. );
  300. $q2 = new WP_Query( array( 'orderby' => array() ) );
  301. $this->assertNotContains( 'ORDER BY', $q2->request );
  302. $this->assertNotContains( 'ORDER', $q2->request );
  303. $q3 = new WP_Query( array( 'post_type' => 'post' ) );
  304. $this->assertContains(
  305. "ORDER BY $wpdb->posts.post_date DESC",
  306. $q3->request
  307. );
  308. $q4 = new WP_Query( array( 'post_type' => 'post' ) );
  309. $this->assertContains(
  310. "ORDER BY $wpdb->posts.post_date DESC",
  311. $q4->request
  312. );
  313. }
  314. /**
  315. * @ticket 17065
  316. */
  317. function test_order() {
  318. global $wpdb;
  319. $q1 = new WP_Query(
  320. array(
  321. 'orderby' => array(
  322. 'post_type' => 'foo',
  323. ),
  324. )
  325. );
  326. $this->assertContains(
  327. "ORDER BY $wpdb->posts.post_type DESC",
  328. $q1->request
  329. );
  330. $q2 = new WP_Query(
  331. array(
  332. 'orderby' => 'title',
  333. 'order' => 'foo',
  334. )
  335. );
  336. $this->assertContains(
  337. "ORDER BY $wpdb->posts.post_title DESC",
  338. $q2->request
  339. );
  340. $q3 = new WP_Query(
  341. array(
  342. 'order' => 'asc',
  343. )
  344. );
  345. $this->assertContains(
  346. "ORDER BY $wpdb->posts.post_date ASC",
  347. $q3->request
  348. );
  349. }
  350. /**
  351. * @ticket 29629
  352. */
  353. function test_orderby() {
  354. // 'rand' is a valid value.
  355. $q = new WP_Query( array( 'orderby' => 'rand' ) );
  356. $this->assertContains( 'ORDER BY RAND()', $q->request );
  357. $this->assertNotContains( 'ASC', $q->request );
  358. $this->assertNotContains( 'DESC', $q->request );
  359. // This isn't allowed.
  360. $q2 = new WP_Query( array( 'order' => 'rand' ) );
  361. $this->assertContains( 'ORDER BY', $q2->request );
  362. $this->assertNotContains( 'RAND()', $q2->request );
  363. $this->assertContains( 'DESC', $q2->request );
  364. // 'none' is a valid value.
  365. $q3 = new WP_Query( array( 'orderby' => 'none' ) );
  366. $this->assertNotContains( 'ORDER BY', $q3->request );
  367. $this->assertNotContains( 'DESC', $q3->request );
  368. $this->assertNotContains( 'ASC', $q3->request );
  369. // False is a valid value.
  370. $q4 = new WP_Query( array( 'orderby' => false ) );
  371. $this->assertNotContains( 'ORDER BY', $q4->request );
  372. $this->assertNotContains( 'DESC', $q4->request );
  373. $this->assertNotContains( 'ASC', $q4->request );
  374. // Empty array() is a valid value.
  375. $q5 = new WP_Query( array( 'orderby' => array() ) );
  376. $this->assertNotContains( 'ORDER BY', $q5->request );
  377. $this->assertNotContains( 'DESC', $q5->request );
  378. $this->assertNotContains( 'ASC', $q5->request );
  379. }
  380. /**
  381. * @ticket 35692
  382. */
  383. public function test_orderby_rand_with_seed() {
  384. $q = new WP_Query(
  385. array(
  386. 'orderby' => 'RAND(5)',
  387. )
  388. );
  389. $this->assertContains( 'ORDER BY RAND(5)', $q->request );
  390. }
  391. /**
  392. * @ticket 35692
  393. */
  394. public function test_orderby_rand_should_ignore_invalid_seed() {
  395. $q = new WP_Query(
  396. array(
  397. 'orderby' => 'RAND(foo)',
  398. )
  399. );
  400. $this->assertNotContains( 'ORDER BY RAND', $q->request );
  401. }
  402. /**
  403. * @ticket 35692
  404. */
  405. public function test_orderby_rand_with_seed_should_be_case_insensitive() {
  406. $q = new WP_Query(
  407. array(
  408. 'orderby' => 'rand(5)',
  409. )
  410. );
  411. $this->assertContains( 'ORDER BY RAND(5)', $q->request );
  412. }
  413. /**
  414. * Tests the post_name__in attribute of WP_Query.
  415. *
  416. * @ticket 33065
  417. */
  418. public function test_post_name__in() {
  419. $q = new WP_Query();
  420. $post_ids[0] = self::factory()->post->create(
  421. array(
  422. 'post_title' => 'woo',
  423. 'post_date' => '2015-07-23 00:00:00',
  424. )
  425. );
  426. $post_ids[1] = self::factory()->post->create(
  427. array(
  428. 'post_title' => 'hoo',
  429. 'post_date' => '2015-07-23 00:00:00',
  430. )
  431. );
  432. $post_ids[2] = self::factory()->post->create(
  433. array(
  434. 'post_title' => 'test',
  435. 'post_date' => '2015-07-23 00:00:00',
  436. )
  437. );
  438. $post_ids[3] = self::factory()->post->create(
  439. array(
  440. 'post_title' => 'me',
  441. 'post_date' => '2015-07-23 00:00:00',
  442. )
  443. );
  444. $requested = array( $post_ids[0], $post_ids[3] );
  445. $q->query(
  446. array(
  447. 'post_name__in' => array( 'woo', 'me' ),
  448. 'fields' => 'ids',
  449. )
  450. );
  451. $actual_posts = $q->get_posts();
  452. $this->assertSameSets( $requested, $actual_posts );
  453. $requested = array( $post_ids[1], $post_ids[2] );
  454. $q->query(
  455. array(
  456. 'post_name__in' => array( 'hoo', 'test' ),
  457. 'fields' => 'ids',
  458. )
  459. );
  460. $actual_posts = $q->get_posts();
  461. $this->assertSameSets( $requested, $actual_posts );
  462. }
  463. /**
  464. * @ticket 36687
  465. */
  466. public function test_posts_pre_query_filter_should_bypass_database_query() {
  467. global $wpdb;
  468. add_filter( 'posts_pre_query', array( __CLASS__, 'filter_posts_pre_query' ) );
  469. $num_queries = $wpdb->num_queries;
  470. $q = new WP_Query(
  471. array(
  472. 'fields' => 'ids',
  473. 'no_found_rows' => true,
  474. )
  475. );
  476. remove_filter( 'posts_pre_query', array( __CLASS__, 'filter_posts_pre_query' ) );
  477. $this->assertSame( $num_queries, $wpdb->num_queries );
  478. $this->assertSame( array( 12345 ), $q->posts );
  479. }
  480. public static function filter_posts_pre_query( $posts ) {
  481. return array( 12345 );
  482. }
  483. /**
  484. * @ticket 36687
  485. */
  486. public function test_posts_pre_query_filter_should_respect_set_found_posts() {
  487. global $wpdb;
  488. $this->post_id = self::factory()->post->create();
  489. // Prevent the DB query.
  490. add_filter( 'posts_request', '__return_empty_string' );
  491. add_filter( 'found_posts_query', '__return_empty_string' );
  492. // Add the post and found_posts.
  493. add_filter( 'the_posts', array( $this, 'filter_the_posts' ) );
  494. add_filter( 'found_posts', array( $this, 'filter_found_posts' ) );
  495. $q = new WP_Query( array( 'suppress_filters' => false ) );
  496. remove_filter( 'posts_request', '__return_empty_string' );
  497. remove_filter( 'found_posts_query', '__return_empty_string' );
  498. remove_filter( 'the_posts', array( $this, 'filter_the_posts' ) );
  499. remove_filter( 'found_posts', array( $this, 'filter_found_posts' ) );
  500. $this->assertSame( array( $this->post_id ), wp_list_pluck( $q->posts, 'ID' ) );
  501. $this->assertSame( 1, $q->found_posts );
  502. }
  503. public function filter_the_posts() {
  504. return array( get_post( $this->post_id ) );
  505. }
  506. public function filter_found_posts( $posts ) {
  507. return 1;
  508. }
  509. /**
  510. * @ticket 36687
  511. */
  512. public function test_set_found_posts_fields_ids() {
  513. register_post_type( 'wptests_pt' );
  514. $posts = self::factory()->post->create_many( 2, array( 'post_type' => 'wptests_pt' ) );
  515. foreach ( $posts as $p ) {
  516. clean_post_cache( $p );
  517. }
  518. $q = new WP_Query(
  519. array(
  520. 'post_type' => 'wptests_pt',
  521. 'posts_per_page' => 1,
  522. 'fields' => 'ids',
  523. )
  524. );
  525. $this->assertSame( 2, $q->found_posts );
  526. $this->assertEquals( 2, $q->max_num_pages );
  527. }
  528. /**
  529. * @ticket 36687
  530. */
  531. public function test_set_found_posts_fields_idparent() {
  532. register_post_type( 'wptests_pt' );
  533. $posts = self::factory()->post->create_many( 2, array( 'post_type' => 'wptests_pt' ) );
  534. foreach ( $posts as $p ) {
  535. clean_post_cache( $p );
  536. }
  537. $q = new WP_Query(
  538. array(
  539. 'post_type' => 'wptests_pt',
  540. 'posts_per_page' => 1,
  541. 'fields' => 'id=>parent',
  542. )
  543. );
  544. $this->assertSame( 2, $q->found_posts );
  545. $this->assertEquals( 2, $q->max_num_pages );
  546. }
  547. /**
  548. * @ticket 36687
  549. */
  550. public function test_set_found_posts_fields_split_the_query() {
  551. register_post_type( 'wptests_pt' );
  552. $posts = self::factory()->post->create_many( 2, array( 'post_type' => 'wptests_pt' ) );
  553. foreach ( $posts as $p ) {
  554. clean_post_cache( $p );
  555. }
  556. add_filter( 'split_the_query', '__return_true' );
  557. $q = new WP_Query(
  558. array(
  559. 'post_type' => 'wptests_pt',
  560. 'posts_per_page' => 1,
  561. )
  562. );
  563. remove_filter( 'split_the_query', '__return_true' );
  564. $this->assertSame( 2, $q->found_posts );
  565. $this->assertEquals( 2, $q->max_num_pages );
  566. }
  567. /**
  568. * @ticket 36687
  569. */
  570. public function test_set_found_posts_fields_not_split_the_query() {
  571. register_post_type( 'wptests_pt' );
  572. $posts = self::factory()->post->create_many( 2, array( 'post_type' => 'wptests_pt' ) );
  573. foreach ( $posts as $p ) {
  574. clean_post_cache( $p );
  575. }
  576. // ! $split_the_query
  577. add_filter( 'split_the_query', '__return_false' );
  578. $q = new WP_Query(
  579. array(
  580. 'post_type' => 'wptests_pt',
  581. 'posts_per_page' => 1,
  582. )
  583. );
  584. remove_filter( 'split_the_query', '__return_false' );
  585. $this->assertSame( 2, $q->found_posts );
  586. $this->assertEquals( 2, $q->max_num_pages );
  587. }
  588. public function set_found_posts_provider() {
  589. // Count return 0 for null, but 1 for other data you may not expect.
  590. return array(
  591. array( null, 0 ),
  592. array( '', 1 ),
  593. array( "To life, to life, l'chaim", 1 ),
  594. array( false, 1 ),
  595. );
  596. }
  597. /**
  598. * @ticket 42860
  599. *
  600. * @dataProvider set_found_posts_provider
  601. */
  602. public function test_set_found_posts_not_posts_as_an_array( $posts, $expected ) {
  603. $q = new WP_Query(
  604. array(
  605. 'post_type' => 'wptests_pt',
  606. 'posts_per_page' => 1,
  607. )
  608. );
  609. $q->posts = $posts;
  610. $methd = new ReflectionMethod( 'WP_Query', 'set_found_posts' );
  611. $methd->setAccessible( true );
  612. $methd->invoke( $q, array( 'no_found_rows' => false ), array() );
  613. $this->assertSame( $expected, $q->found_posts );
  614. }
  615. /**
  616. * @ticket 42469
  617. */
  618. public function test_found_posts_should_be_integer_not_string() {
  619. $this->post_id = self::factory()->post->create();
  620. $q = new WP_Query(
  621. array(
  622. 'posts_per_page' => 1,
  623. )
  624. );
  625. $this->assertInternalType( 'int', $q->found_posts );
  626. }
  627. /**
  628. * @ticket 42469
  629. */
  630. public function test_found_posts_should_be_integer_even_if_found_posts_filter_returns_string_value() {
  631. $this->post_id = self::factory()->post->create();
  632. add_filter( 'found_posts', '__return_empty_string' );
  633. $q = new WP_Query(
  634. array(
  635. 'posts_per_page' => 1,
  636. )
  637. );
  638. remove_filter( 'found_posts', '__return_empty_string' );
  639. $this->assertInternalType( 'int', $q->found_posts );
  640. }
  641. }