PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/query/results.php

https://github.com/WP-Panda/es-wp-query
PHP | 474 lines | 361 code | 81 blank | 32 comment | 0 complexity | a285e33a72e2af8163856d87b8da3142 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * Test various query vars and make sure the WP_Query class selects the correct posts.
  5. * We're testing against a known data set, so we can check that specific posts are included in the output.
  6. *
  7. * @group query
  8. */
  9. class Tests_Query_Results extends WP_UnitTestCase {
  10. protected $q;
  11. function setUp() {
  12. parent::setUp();
  13. $cat_a = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-a' ) );
  14. $cat_b = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-b' ) );
  15. $cat_c = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-c' ) );
  16. $this->factory->post->create( array( 'post_title' => 'tag-נ', 'tags_input' => array( 'tag-נ' ), 'post_date' => '2008-11-01 00:00:00' ) );
  17. $this->factory->post->create( array( 'post_title' => 'cats-a-b-c', 'post_date' => '2008-12-01 00:00:00', 'post_category' => array( $cat_a, $cat_b, $cat_c ) ) );
  18. $this->factory->post->create( array( 'post_title' => 'cats-a-and-b', 'post_date' => '2009-01-01 00:00:00', 'post_category' => array( $cat_a, $cat_b ) ) );
  19. $this->factory->post->create( array( 'post_title' => 'cats-b-and-c', 'post_date' => '2009-02-01 00:00:00', 'post_category' => array( $cat_b, $cat_c ) ) );
  20. $this->factory->post->create( array( 'post_title' => 'cats-a-and-c', 'post_date' => '2009-03-01 00:00:00', 'post_category' => array( $cat_a, $cat_c ) ) );
  21. $this->factory->post->create( array( 'post_title' => 'cat-a', 'post_date' => '2009-04-01 00:00:00', 'post_category' => array( $cat_a ) ) );
  22. $this->factory->post->create( array( 'post_title' => 'cat-b', 'post_date' => '2009-05-01 00:00:00', 'post_category' => array( $cat_b ) ) );
  23. $this->factory->post->create( array( 'post_title' => 'cat-c', 'post_date' => '2009-06-01 00:00:00', 'post_category' => array( $cat_c ) ) );
  24. $this->factory->post->create( array( 'post_title' => 'lorem-ipsum', 'post_date' => '2009-07-01 00:00:00' ) );
  25. $this->factory->post->create( array( 'post_title' => 'comment-test', 'post_date' => '2009-08-01 00:00:00' ) );
  26. $this->factory->post->create( array( 'post_title' => 'one-trackback', 'post_date' => '2009-09-01 00:00:00' ) );
  27. $this->factory->post->create( array( 'post_title' => 'many-trackbacks', 'post_date' => '2009-10-01 00:00:00' ) );
  28. $this->factory->post->create( array( 'post_title' => 'no-comments', 'post_date' => '2009-10-02 00:00:00' ) );
  29. $this->factory->post->create( array( 'post_title' => 'one-comment', 'post_date' => '2009-11-01 00:00:00' ) );
  30. $this->factory->post->create( array( 'post_title' => 'contributor-post-approved', 'post_date' => '2009-12-01 00:00:00' ) );
  31. $this->factory->post->create( array( 'post_title' => 'embedded-video', 'post_date' => '2010-01-01 00:00:00' ) );
  32. $this->factory->post->create( array( 'post_title' => 'simple-markup-test', 'post_date' => '2010-02-01 00:00:00' ) );
  33. $this->factory->post->create( array( 'post_title' => 'raw-html-code', 'post_date' => '2010-03-01 00:00:00' ) );
  34. $this->factory->post->create( array( 'post_title' => 'tags-a-b-c', 'tags_input' => array( 'tag-a', 'tag-b', 'tag-c' ), 'post_date' => '2010-04-01 00:00:00' ) );
  35. $this->factory->post->create( array( 'post_title' => 'tag-a', 'tags_input' => array( 'tag-a' ), 'post_date' => '2010-05-01 00:00:00' ) );
  36. $this->factory->post->create( array( 'post_title' => 'tag-b', 'tags_input' => array( 'tag-b' ), 'post_date' => '2010-06-01 00:00:00' ) );
  37. $this->factory->post->create( array( 'post_title' => 'tag-c', 'tags_input' => array( 'tag-c' ), 'post_date' => '2010-07-01 00:00:00' ) );
  38. $this->factory->post->create( array( 'post_title' => 'tags-a-and-b', 'tags_input' => array( 'tag-a', 'tag-b' ), 'post_date' => '2010-08-01 00:00:00' ) );
  39. $this->factory->post->create( array( 'post_title' => 'tags-b-and-c', 'tags_input' => array( 'tag-b', 'tag-c' ), 'post_date' => '2010-09-01 00:00:00' ) );
  40. $this->factory->post->create( array( 'post_title' => 'tags-a-and-c', 'tags_input' => array( 'tag-a', 'tag-c' ), 'post_date' => '2010-10-01 00:00:00' ) );
  41. $this->parent_one = $this->factory->post->create( array( 'post_title' => 'parent-one', 'post_date' => '2007-01-01 00:00:00' ) );
  42. $this->parent_two = $this->factory->post->create( array( 'post_title' => 'parent-two', 'post_date' => '2007-01-01 00:00:00' ) );
  43. $this->parent_three = $this->factory->post->create( array( 'post_title' => 'parent-three', 'post_date' => '2007-01-01 00:00:00' ) );
  44. $this->factory->post->create( array( 'post_title' => 'child-one', 'post_parent' => $this->parent_one, 'post_date' => '2007-01-01 00:00:01' ) );
  45. $this->factory->post->create( array( 'post_title' => 'child-two', 'post_parent' => $this->parent_one, 'post_date' => '2007-01-01 00:00:02' ) );
  46. $this->factory->post->create( array( 'post_title' => 'child-three', 'post_parent' => $this->parent_two, 'post_date' => '2007-01-01 00:00:03' ) );
  47. $this->factory->post->create( array( 'post_title' => 'child-four', 'post_parent' => $this->parent_two, 'post_date' => '2007-01-01 00:00:04' ) );
  48. es_wp_query_index_test_data();
  49. unset( $this->q );
  50. $this->q = new ES_WP_Query();
  51. }
  52. function test_query_default() {
  53. $posts = $this->q->query('');
  54. // the output should be the most recent 10 posts as listed here
  55. $expected = array(
  56. 0 => 'tags-a-and-c',
  57. 1 => 'tags-b-and-c',
  58. 2 => 'tags-a-and-b',
  59. 3 => 'tag-c',
  60. 4 => 'tag-b',
  61. 5 => 'tag-a',
  62. 6 => 'tags-a-b-c',
  63. 7 => 'raw-html-code',
  64. 8 => 'simple-markup-test',
  65. 9 => 'embedded-video',
  66. );
  67. $this->assertEquals( $expected, wp_list_pluck( $posts, 'post_name' ) );
  68. }
  69. function test_query_tag_a() {
  70. $posts = $this->q->query('tag=tag-a');
  71. // there are 4 posts with Tag A
  72. $this->assertCount( 4, $posts );
  73. $this->assertEquals( 'tags-a-and-c', $posts[0]->post_name );
  74. $this->assertEquals( 'tags-a-and-b', $posts[1]->post_name );
  75. $this->assertEquals( 'tag-a', $posts[2]->post_name );
  76. $this->assertEquals( 'tags-a-b-c', $posts[3]->post_name );
  77. }
  78. function test_query_tag_b() {
  79. $posts = $this->q->query('tag=tag-b');
  80. // there are 4 posts with Tag A
  81. $this->assertCount( 4, $posts );
  82. $this->assertEquals( 'tags-b-and-c', $posts[0]->post_name );
  83. $this->assertEquals( 'tags-a-and-b', $posts[1]->post_name );
  84. $this->assertEquals( 'tag-b', $posts[2]->post_name );
  85. $this->assertEquals( 'tags-a-b-c', $posts[3]->post_name );
  86. }
  87. /**
  88. * @ticket 21779
  89. */
  90. function test_query_tag_nun() {
  91. $posts = $this->q->query('tag=tag-נ');
  92. // there is 1 post with Tag נ
  93. $this->assertCount( 1, $posts );
  94. $this->assertEquals( 'tag-%d7%a0', $posts[0]->post_name );
  95. }
  96. function test_query_tag_id() {
  97. $tag = tag_exists('tag-a');
  98. $posts = $this->q->query( "tag_id=" . $tag['term_id'] );
  99. // there are 4 posts with Tag A
  100. $this->assertCount( 4, $posts );
  101. $this->assertEquals( 'tags-a-and-c', $posts[0]->post_name );
  102. $this->assertEquals( 'tags-a-and-b', $posts[1]->post_name );
  103. $this->assertEquals( 'tag-a', $posts[2]->post_name );
  104. $this->assertEquals( 'tags-a-b-c', $posts[3]->post_name );
  105. }
  106. function test_query_tag_slug__in() {
  107. $posts = $this->q->query("tag_slug__in[]=tag-b&tag_slug__in[]=tag-c");
  108. // there are 4 posts with either Tag B or Tag C
  109. $this->assertCount( 6, $posts );
  110. $this->assertEquals( 'tags-a-and-c', $posts[0]->post_name );
  111. $this->assertEquals( 'tags-b-and-c', $posts[1]->post_name );
  112. $this->assertEquals( 'tags-a-and-b', $posts[2]->post_name );
  113. $this->assertEquals( 'tag-c', $posts[3]->post_name );
  114. $this->assertEquals( 'tag-b', $posts[4]->post_name );
  115. $this->assertEquals( 'tags-a-b-c', $posts[5]->post_name );
  116. }
  117. function test_query_tag__in() {
  118. $tag_a = tag_exists('tag-a');
  119. $tag_b = tag_exists('tag-b');
  120. $posts = $this->q->query( "tag__in[]=". $tag_a['term_id'] . "&tag__in[]=" . $tag_b['term_id'] );
  121. // there are 6 posts with either Tag A or Tag B
  122. $this->assertCount( 6, $posts );
  123. $this->assertEquals( 'tags-a-and-c', $posts[0]->post_name );
  124. $this->assertEquals( 'tags-b-and-c', $posts[1]->post_name );
  125. $this->assertEquals( 'tags-a-and-b', $posts[2]->post_name );
  126. $this->assertEquals( 'tag-b', $posts[3]->post_name );
  127. $this->assertEquals( 'tag-a', $posts[4]->post_name );
  128. $this->assertEquals( 'tags-a-b-c', $posts[5]->post_name );
  129. }
  130. function test_query_tag__not_in() {
  131. $tag_a = tag_exists('tag-a');
  132. $posts = $this->q->query( "tag__not_in[]=" . $tag_a['term_id'] );
  133. // the most recent 10 posts with Tag A excluded
  134. // (note the different between this and test_query_default)
  135. $expected = array (
  136. 0 => 'tags-b-and-c',
  137. 1 => 'tag-c',
  138. 2 => 'tag-b',
  139. 3 => 'raw-html-code',
  140. 4 => 'simple-markup-test',
  141. 5 => 'embedded-video',
  142. 6 => 'contributor-post-approved',
  143. 7 => 'one-comment',
  144. 8 => 'no-comments',
  145. 9 => 'many-trackbacks',
  146. );
  147. $this->assertEquals( $expected, wp_list_pluck( $posts, 'post_name' ) );
  148. }
  149. function test_query_tag__in_but__not_in() {
  150. $tag_a = tag_exists('tag-a');
  151. $tag_b = tag_exists('tag-b');
  152. $posts = $this->q->query( "tag__in[]=" . $tag_a['term_id'] . "&tag__not_in[]=" . $tag_b['term_id'] );
  153. // there are 4 posts with Tag A, only 2 when we exclude Tag B
  154. $this->assertCount( 2, $posts );
  155. $this->assertEquals( 'tags-a-and-c', $posts[0]->post_name );
  156. $this->assertEquals( 'tag-a', $posts[1]->post_name );
  157. }
  158. function test_query_category_name() {
  159. $posts = $this->q->query('category_name=cat-a');
  160. // there are 4 posts with Cat A, we'll check for them by name
  161. $this->assertCount( 4, $posts );
  162. $this->assertEquals( 'cat-a', $posts[0]->post_name );
  163. $this->assertEquals( 'cats-a-and-c', $posts[1]->post_name );
  164. $this->assertEquals( 'cats-a-and-b', $posts[2]->post_name );
  165. $this->assertEquals( 'cats-a-b-c', $posts[3]->post_name );
  166. }
  167. function test_query_cat() {
  168. $cat = category_exists('cat-b');
  169. $posts = $this->q->query("cat=$cat");
  170. // there are 4 posts with Cat B
  171. $this->assertCount( 4, $posts );
  172. $this->assertEquals( 'cat-b', $posts[0]->post_name );
  173. $this->assertEquals( 'cats-b-and-c', $posts[1]->post_name );
  174. $this->assertEquals( 'cats-a-and-b', $posts[2]->post_name );
  175. $this->assertEquals( 'cats-a-b-c', $posts[3]->post_name );
  176. }
  177. function test_query_posts_per_page() {
  178. $posts = $this->q->query('posts_per_page=5');
  179. $expected = array (
  180. 0 => 'tags-a-and-c',
  181. 1 => 'tags-b-and-c',
  182. 2 => 'tags-a-and-b',
  183. 3 => 'tag-c',
  184. 4 => 'tag-b',
  185. );
  186. $this->assertCount( 5, $posts );
  187. $this->assertEquals( $expected, wp_list_pluck( $posts, 'post_name' ) );
  188. }
  189. function test_query_offset() {
  190. $posts = $this->q->query('offset=2');
  191. $expected = array (
  192. 0 => 'tags-a-and-b',
  193. 1 => 'tag-c',
  194. 2 => 'tag-b',
  195. 3 => 'tag-a',
  196. 4 => 'tags-a-b-c',
  197. 5 => 'raw-html-code',
  198. 6 => 'simple-markup-test',
  199. 7 => 'embedded-video',
  200. 8 => 'contributor-post-approved',
  201. 9 => 'one-comment',
  202. );
  203. $this->assertCount( 10, $posts );
  204. $this->assertEquals( $expected, wp_list_pluck( $posts, 'post_name' ) );
  205. }
  206. function test_query_paged() {
  207. $posts = $this->q->query('paged=2');
  208. $expected = array (
  209. 0 => 'contributor-post-approved',
  210. 1 => 'one-comment',
  211. 2 => 'no-comments',
  212. 3 => 'many-trackbacks',
  213. 4 => 'one-trackback',
  214. 5 => 'comment-test',
  215. 6 => 'lorem-ipsum',
  216. 7 => 'cat-c',
  217. 8 => 'cat-b',
  218. 9 => 'cat-a',
  219. );
  220. $this->assertCount( 10, $posts );
  221. $this->assertTrue( $this->q->is_paged() );
  222. $this->assertEquals( $expected, wp_list_pluck( $posts, 'post_name' ) );
  223. }
  224. function test_query_paged_and_posts_per_page() {
  225. $posts = $this->q->query('paged=4&posts_per_page=4');
  226. $expected = array (
  227. 0 => 'no-comments',
  228. 1 => 'many-trackbacks',
  229. 2 => 'one-trackback',
  230. 3 => 'comment-test',
  231. );
  232. $this->assertCount( 4, $posts );
  233. $this->assertTrue( $this->q->is_paged() );
  234. $this->assertEquals( $expected, wp_list_pluck( $posts, 'post_name' ) );
  235. }
  236. /**
  237. * @ticket 18897
  238. */
  239. function test_query_offset_and_paged() {
  240. $posts = $this->q->query('paged=2&offset=3');
  241. $expected = array (
  242. 0 => 'many-trackbacks',
  243. 1 => 'one-trackback',
  244. 2 => 'comment-test',
  245. 3 => 'lorem-ipsum',
  246. 4 => 'cat-c',
  247. 5 => 'cat-b',
  248. 6 => 'cat-a',
  249. 7 => 'cats-a-and-c',
  250. 8 => 'cats-b-and-c',
  251. 9 => 'cats-a-and-b',
  252. );
  253. $this->assertCount( 10, $posts );
  254. $this->assertTrue( $this->q->is_paged() );
  255. $this->assertEquals( $expected, wp_list_pluck( $posts, 'post_name' ) );
  256. }
  257. /**
  258. * @ticket 11056
  259. */
  260. function test_query_post_parent__in() {
  261. // Query for first parent's children
  262. $posts = $this->q->query( array(
  263. 'post_parent__in' => array( $this->parent_one ),
  264. 'orderby' => 'date',
  265. 'order' => 'asc',
  266. ) );
  267. $this->assertEquals( array(
  268. 'child-one',
  269. 'child-two',
  270. ), wp_list_pluck( $posts, 'post_title' ) );
  271. // Second parent's children
  272. $posts = $this->q->query( array(
  273. 'post_parent__in' => array( $this->parent_two ),
  274. 'orderby' => 'date',
  275. 'order' => 'asc',
  276. ) );
  277. $this->assertEquals( array(
  278. 'child-three',
  279. 'child-four',
  280. ), wp_list_pluck( $posts, 'post_title' ) );
  281. // Both first and second parent's children
  282. $posts = $this->q->query( array(
  283. 'post_parent__in' => array( $this->parent_one, $this->parent_two ),
  284. 'orderby' => 'date',
  285. 'order' => 'asc',
  286. ) );
  287. $this->assertEquals( array(
  288. 'child-one',
  289. 'child-two',
  290. 'child-three',
  291. 'child-four',
  292. ), wp_list_pluck( $posts, 'post_title' ) );
  293. // Third parent's children
  294. $posts = $this->q->query( array(
  295. 'post_parent__in' => array( $this->parent_three ),
  296. ) );
  297. $this->assertEquals( array(), wp_list_pluck( $posts, 'post_title' ) );
  298. }
  299. function test_exlude_from_search_empty() {
  300. global $wp_post_types;
  301. foreach ( array_keys( $wp_post_types ) as $slug )
  302. $wp_post_types[$slug]->exclude_from_search = true;
  303. $posts = $this->q->query( array( 'post_type' => 'any' ) );
  304. $this->assertEmpty( $posts );
  305. foreach ( array_keys( $wp_post_types ) as $slug )
  306. $wp_post_types[$slug]->exclude_from_search = false;
  307. $posts2 = $this->q->query( array( 'post_type' => 'any' ) );
  308. $this->assertNotEmpty( $posts2 );
  309. }
  310. function test_query_author_vars() {
  311. $author_1 = $this->factory->user->create( array( 'user_login' => 'admin1', 'user_pass' => rand_str(), 'role' => 'author' ) );
  312. $post_1 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_1, 'post_date' => '2007-01-01 00:00:00' ) );
  313. $author_2 = $this->factory->user->create( array( 'user_login' => rand_str(), 'user_pass' => rand_str(), 'role' => 'author' ) );
  314. $post_2 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_2, 'post_date' => '2007-01-01 00:00:00' ) );
  315. $author_3 = $this->factory->user->create( array( 'user_login' => rand_str(), 'user_pass' => rand_str(), 'role' => 'author' ) );
  316. $post_3 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_3, 'post_date' => '2007-01-01 00:00:00' ) );
  317. $author_4 = $this->factory->user->create( array( 'user_login' => rand_str(), 'user_pass' => rand_str(), 'role' => 'author' ) );
  318. $post_4 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_4, 'post_date' => '2007-01-01 00:00:00' ) );
  319. es_wp_query_index_test_data();
  320. $posts = $this->q->query( array(
  321. 'author' => '',
  322. 'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
  323. ) );
  324. $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
  325. $this->assertEqualSets( array( $author_1, $author_2, $author_3, $author_4 ), $author_ids );
  326. $posts = $this->q->query( array(
  327. 'author' => 0,
  328. 'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
  329. ) );
  330. $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
  331. $this->assertEqualSets( array( $author_1, $author_2, $author_3, $author_4 ), $author_ids );
  332. $posts = $this->q->query( array(
  333. 'author' => '0',
  334. 'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
  335. ) );
  336. $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
  337. $this->assertEqualSets( array( $author_1, $author_2, $author_3, $author_4 ), $author_ids );
  338. $posts = $this->q->query( array(
  339. 'author' => $author_1,
  340. 'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
  341. ) );
  342. $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
  343. $this->assertEqualSets( array( $author_1 ), $author_ids );
  344. $posts = $this->q->query( array(
  345. 'author' => "$author_1",
  346. 'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
  347. ) );
  348. $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
  349. $this->assertEqualSets( array( $author_1 ), $author_ids );
  350. $posts = $this->q->query( array(
  351. 'author' => "{$author_1},{$author_2}",
  352. 'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
  353. ) );
  354. $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
  355. $this->assertEqualSets( array( $author_1, $author_2 ), $author_ids );
  356. $posts = $this->q->query( array(
  357. 'author' => "-{$author_1},{$author_2}",
  358. 'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
  359. ) );
  360. $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
  361. $this->assertEqualSets( array( $author_2, $author_3, $author_4 ), $author_ids );
  362. $posts = $this->q->query( array(
  363. 'author' => "{$author_1},-{$author_2}",
  364. 'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
  365. ) );
  366. $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
  367. $this->assertEqualSets( array( $author_1, $author_3, $author_4 ), $author_ids );
  368. $posts = $this->q->query( array(
  369. 'author' => "-{$author_1},-{$author_2}",
  370. 'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
  371. ) );
  372. $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
  373. $this->assertEqualSets( array( $author_3, $author_4 ), $author_ids );
  374. $posts = $this->q->query( array(
  375. 'author__in' => array( $author_1, $author_2 ),
  376. 'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
  377. ) );
  378. $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
  379. $this->assertEqualSets( array( $author_1, $author_2 ), $author_ids );
  380. $posts = $this->q->query( array(
  381. 'author__not_in' => array( $author_1, $author_2 ),
  382. 'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
  383. ) );
  384. $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
  385. $this->assertEqualSets( array( $author_3, $author_4 ), $author_ids );
  386. $posts = $this->q->query( array(
  387. 'author_name' => 'admin1',
  388. 'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
  389. ) );
  390. $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
  391. $this->assertEqualSets( array( $author_1 ), $author_ids );
  392. }
  393. }