/tests/phpunit/tests/comment/getPageOfComment.php

https://github.com/WordPress/wordpress-develop · PHP · 549 lines · 414 code · 78 blank · 57 comment · 7 complexity · 12cab51b5fd4b44e2300b422e659e3f9 MD5 · raw file

  1. <?php
  2. /**
  3. * @group comment
  4. * @covers ::get_page_of_comment
  5. */
  6. class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
  7. public function test_last_comment() {
  8. $p = self::factory()->post->create();
  9. // Page 4.
  10. $comment_last = self::factory()->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-24 00:00:00' ) );
  11. self::factory()->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-23 00:00:00' ) );
  12. // Page 3.
  13. self::factory()->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-22 00:00:00' ) );
  14. self::factory()->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-21 00:00:00' ) );
  15. self::factory()->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-20 00:00:00' ) );
  16. // Page 2.
  17. self::factory()->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-19 00:00:00' ) );
  18. self::factory()->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-18 00:00:00' ) );
  19. self::factory()->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-17 00:00:00' ) );
  20. // Page 1.
  21. self::factory()->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-16 00:00:00' ) );
  22. self::factory()->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-15 00:00:00' ) );
  23. $comment_first = self::factory()->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-14 00:00:00' ) );
  24. $this->assertSame( 4, get_page_of_comment( $comment_last[0], array( 'per_page' => 3 ) ) );
  25. $this->assertSame( 2, get_page_of_comment( $comment_last[0], array( 'per_page' => 10 ) ) );
  26. $this->assertSame( 1, get_page_of_comment( $comment_first[0], array( 'per_page' => 3 ) ) );
  27. $this->assertSame( 1, get_page_of_comment( $comment_first[0], array( 'per_page' => 10 ) ) );
  28. }
  29. public function test_type_pings() {
  30. $p = self::factory()->post->create();
  31. $now = time();
  32. $trackbacks = array();
  33. for ( $i = 0; $i <= 3; $i++ ) {
  34. $trackbacks[ $i ] = self::factory()->comment->create(
  35. array(
  36. 'comment_post_ID' => $p,
  37. 'comment_type' => 'trackback',
  38. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now ),
  39. )
  40. );
  41. $now -= 10 * $i;
  42. }
  43. $pingbacks = array();
  44. for ( $i = 0; $i <= 6; $i++ ) {
  45. $pingbacks[ $i ] = self::factory()->comment->create(
  46. array(
  47. 'comment_post_ID' => $p,
  48. 'comment_type' => 'pingback',
  49. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now ),
  50. )
  51. );
  52. $now -= 10 * $i;
  53. }
  54. $this->assertSame(
  55. 2,
  56. get_page_of_comment(
  57. $trackbacks[0],
  58. array(
  59. 'per_page' => 2,
  60. 'type' => 'trackback',
  61. )
  62. )
  63. );
  64. $this->assertSame(
  65. 3,
  66. get_page_of_comment(
  67. $pingbacks[0],
  68. array(
  69. 'per_page' => 2,
  70. 'type' => 'pingback',
  71. )
  72. )
  73. );
  74. $this->assertSame(
  75. 5,
  76. get_page_of_comment(
  77. $trackbacks[0],
  78. array(
  79. 'per_page' => 2,
  80. 'type' => 'pings',
  81. )
  82. )
  83. );
  84. }
  85. /**
  86. * @ticket 11334
  87. */
  88. public function test_subsequent_calls_should_hit_cache() {
  89. global $wpdb;
  90. $p = self::factory()->post->create();
  91. $c = self::factory()->comment->create( array( 'comment_post_ID' => $p ) );
  92. // Prime cache.
  93. $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
  94. $num_queries = $wpdb->num_queries;
  95. $page_2 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
  96. $this->assertSame( $page_1, $page_2 );
  97. $this->assertSame( $num_queries, $wpdb->num_queries );
  98. }
  99. /**
  100. * @ticket 11334
  101. */
  102. public function test_cache_hits_should_be_sensitive_to_comment_type() {
  103. global $wpdb;
  104. $p = self::factory()->post->create();
  105. $comment = self::factory()->comment->create(
  106. array(
  107. 'comment_post_ID' => $p,
  108. 'comment_type' => 'comment',
  109. )
  110. );
  111. $now = time();
  112. $trackbacks = array();
  113. for ( $i = 0; $i <= 5; $i++ ) {
  114. $trackbacks[ $i ] = self::factory()->comment->create(
  115. array(
  116. 'comment_post_ID' => $p,
  117. 'comment_type' => 'trackback',
  118. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - ( 10 * $i ) ),
  119. )
  120. );
  121. }
  122. // Prime cache for trackbacks.
  123. $page_trackbacks = get_page_of_comment(
  124. $trackbacks[1],
  125. array(
  126. 'per_page' => 3,
  127. 'type' => 'trackback',
  128. )
  129. );
  130. $this->assertSame( 2, $page_trackbacks );
  131. $num_queries = $wpdb->num_queries;
  132. $page_comments = get_page_of_comment(
  133. $comment,
  134. array(
  135. 'per_page' => 3,
  136. 'type' => 'comment',
  137. )
  138. );
  139. $this->assertSame( 1, $page_comments );
  140. $this->assertNotEquals( $num_queries, $wpdb->num_queries );
  141. }
  142. /**
  143. * @ticket 11334
  144. */
  145. public function test_cache_should_be_invalidated_when_comment_is_approved() {
  146. $p = self::factory()->post->create();
  147. $c = self::factory()->comment->create(
  148. array(
  149. 'comment_post_ID' => $p,
  150. 'comment_approved' => 0,
  151. )
  152. );
  153. // Prime cache.
  154. $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
  155. // Approve comment.
  156. wp_set_comment_status( $c, 'approve' );
  157. $this->assertFalse( wp_cache_get( $c, 'comment_pages' ) );
  158. }
  159. /**
  160. * @ticket 11334
  161. */
  162. public function test_cache_should_be_invalidated_when_comment_is_deleted() {
  163. $p = self::factory()->post->create();
  164. $c = self::factory()->comment->create( array( 'comment_post_ID' => $p ) );
  165. // Prime cache.
  166. $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
  167. // Trash comment.
  168. wp_trash_comment( $c );
  169. $this->assertFalse( wp_cache_get( $c, 'comment_pages' ) );
  170. }
  171. /**
  172. * @ticket 11334
  173. */
  174. public function test_cache_should_be_invalidated_when_comment_is_spammed() {
  175. $p = self::factory()->post->create();
  176. $c = self::factory()->comment->create( array( 'comment_post_ID' => $p ) );
  177. // Prime cache.
  178. $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
  179. // Spam comment.
  180. wp_spam_comment( $c );
  181. $this->assertFalse( wp_cache_get( $c, 'comment_pages' ) );
  182. }
  183. /**
  184. * @ticket 11334
  185. */
  186. public function test_cache_should_be_invalidated_when_older_comment_is_published() {
  187. $now = time();
  188. $p = self::factory()->post->create();
  189. $c1 = self::factory()->comment->create(
  190. array(
  191. 'comment_post_ID' => $p,
  192. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now ),
  193. )
  194. );
  195. $c2 = self::factory()->comment->create(
  196. array(
  197. 'comment_post_ID' => $p,
  198. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 20 ),
  199. )
  200. );
  201. $c3 = self::factory()->comment->create(
  202. array(
  203. 'comment_post_ID' => $p,
  204. 'comment_approved' => 0,
  205. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 30 ),
  206. )
  207. );
  208. $this->assertSame( 1, get_page_of_comment( $c1, array( 'per_page' => 2 ) ) );
  209. wp_set_comment_status( $c3, '1' );
  210. $this->assertSame( 2, get_page_of_comment( $c1, array( 'per_page' => 2 ) ) );
  211. }
  212. /**
  213. * @ticket 34057
  214. */
  215. public function test_query_should_be_limited_to_comments_on_the_proper_post() {
  216. $posts = self::factory()->post->create_many( 2 );
  217. $now = time();
  218. $comments_0 = array();
  219. $comments_1 = array();
  220. for ( $i = 0; $i < 5; $i++ ) {
  221. $comments_0[] = self::factory()->comment->create(
  222. array(
  223. 'comment_post_ID' => $posts[0],
  224. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - ( $i * 60 ) ),
  225. )
  226. );
  227. $comments_1[] = self::factory()->comment->create(
  228. array(
  229. 'comment_post_ID' => $posts[1],
  230. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - ( $i * 60 ) ),
  231. )
  232. );
  233. }
  234. $found_0 = get_page_of_comment( $comments_0[0], array( 'per_page' => 2 ) );
  235. $this->assertSame( 3, $found_0 );
  236. $found_1 = get_page_of_comment( $comments_1[1], array( 'per_page' => 2 ) );
  237. $this->assertSame( 2, $found_1 );
  238. }
  239. /**
  240. * @ticket 13939
  241. */
  242. public function test_only_top_level_comments_should_be_included_in_older_count() {
  243. $post = self::factory()->post->create();
  244. $now = time();
  245. $comment_parents = array();
  246. $comment_children = array();
  247. for ( $i = 0; $i < 5; $i++ ) {
  248. $parent = self::factory()->comment->create(
  249. array(
  250. 'comment_post_ID' => $post,
  251. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - ( $i * 60 ) ),
  252. )
  253. );
  254. $comment_parents[ $i ] = $parent;
  255. $child = self::factory()->comment->create(
  256. array(
  257. 'comment_post_ID' => $post,
  258. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - ( $i * 59 ) ),
  259. 'comment_parent' => $parent,
  260. )
  261. );
  262. $comment_children[ $i ] = $child;
  263. }
  264. $page_1_indicies = array( 2, 3, 4 );
  265. $page_2_indicies = array( 0, 1 );
  266. $args = array(
  267. 'per_page' => 3,
  268. 'max_depth' => 2,
  269. );
  270. foreach ( $page_1_indicies as $p1i ) {
  271. $this->assertSame( 1, (int) get_page_of_comment( $comment_parents[ $p1i ], $args ) );
  272. $this->assertSame( 1, (int) get_page_of_comment( $comment_children[ $p1i ], $args ) );
  273. }
  274. foreach ( $page_2_indicies as $p2i ) {
  275. $this->assertSame( 2, (int) get_page_of_comment( $comment_parents[ $p2i ], $args ) );
  276. $this->assertSame( 2, (int) get_page_of_comment( $comment_children[ $p2i ], $args ) );
  277. }
  278. }
  279. /**
  280. * @ticket 13939
  281. */
  282. public function test_comments_per_page_option_should_be_fallback_when_query_var_is_not_available() {
  283. $now = time();
  284. $p = self::factory()->post->create();
  285. $c1 = self::factory()->comment->create(
  286. array(
  287. 'comment_post_ID' => $p,
  288. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now ),
  289. )
  290. );
  291. $c2 = self::factory()->comment->create(
  292. array(
  293. 'comment_post_ID' => $p,
  294. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 20 ),
  295. )
  296. );
  297. $c3 = self::factory()->comment->create(
  298. array(
  299. 'comment_post_ID' => $p,
  300. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 30 ),
  301. )
  302. );
  303. update_option( 'page_comments', 1 );
  304. update_option( 'comments_per_page', 2 );
  305. $this->assertSame( 2, get_page_of_comment( $c1 ) );
  306. }
  307. /**
  308. * @ticket 31101
  309. * @ticket 39280
  310. */
  311. public function test_should_ignore_comment_order() {
  312. $now = time();
  313. $p = self::factory()->post->create();
  314. $c1 = self::factory()->comment->create(
  315. array(
  316. 'comment_post_ID' => $p,
  317. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now ),
  318. )
  319. );
  320. $c2 = self::factory()->comment->create(
  321. array(
  322. 'comment_post_ID' => $p,
  323. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 20 ),
  324. )
  325. );
  326. $c3 = self::factory()->comment->create(
  327. array(
  328. 'comment_post_ID' => $p,
  329. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 30 ),
  330. )
  331. );
  332. $c4 = self::factory()->comment->create(
  333. array(
  334. 'comment_post_ID' => $p,
  335. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 40 ),
  336. )
  337. );
  338. update_option( 'comment_order', 'desc' );
  339. update_option( 'page_comments', 1 );
  340. update_option( 'comments_per_page', 1 );
  341. $this->assertSame( 2, get_page_of_comment( $c3 ) );
  342. }
  343. /**
  344. * @ticket 31101
  345. * @ticket 39280
  346. */
  347. public function test_should_ignore_default_comment_page() {
  348. $now = time();
  349. $p = self::factory()->post->create();
  350. $c1 = self::factory()->comment->create(
  351. array(
  352. 'comment_post_ID' => $p,
  353. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now ),
  354. )
  355. );
  356. $c2 = self::factory()->comment->create(
  357. array(
  358. 'comment_post_ID' => $p,
  359. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 20 ),
  360. )
  361. );
  362. $c3 = self::factory()->comment->create(
  363. array(
  364. 'comment_post_ID' => $p,
  365. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 30 ),
  366. )
  367. );
  368. $c4 = self::factory()->comment->create(
  369. array(
  370. 'comment_post_ID' => $p,
  371. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 40 ),
  372. )
  373. );
  374. update_option( 'default_comment_page', 'newest' );
  375. update_option( 'page_comments', 1 );
  376. update_option( 'comments_per_page', 1 );
  377. $this->assertSame( 2, get_page_of_comment( $c3 ) );
  378. }
  379. /**
  380. * @ticket 8973
  381. */
  382. public function test_page_number_when_unapproved_comments_are_included_for_current_commenter() {
  383. $post = self::factory()->post->create();
  384. $comment_args = array(
  385. 'comment_post_ID' => $post,
  386. 'comment_approved' => 0,
  387. 'comment_author_email' => 'foo@bar.test',
  388. 'comment_author' => 'Foo',
  389. 'comment_author_url' => 'https://bar.test',
  390. );
  391. for ( $i = 1; $i < 4; $i++ ) {
  392. self::factory()->comment->create(
  393. array_merge(
  394. $comment_args,
  395. array(
  396. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', time() - ( $i * 1000 ) ),
  397. )
  398. )
  399. );
  400. }
  401. $new_unapproved = self::factory()->comment->create(
  402. $comment_args
  403. );
  404. add_filter( 'wp_get_current_commenter', array( $this, 'get_current_commenter' ) );
  405. $page = get_page_of_comment( $new_unapproved, array( 'per_page' => 3 ) );
  406. $comments = get_comments(
  407. array(
  408. 'number' => 3,
  409. 'paged' => $page,
  410. 'post_id' => $post,
  411. 'status' => 'approve',
  412. 'include_unapproved' => array( 'foo@bar.test' ),
  413. 'orderby' => 'comment_date_gmt',
  414. 'order' => 'ASC',
  415. )
  416. );
  417. remove_filter( 'wp_get_current_commenter', array( $this, 'get_current_commenter' ) );
  418. $this->assertContains( $new_unapproved, wp_list_pluck( $comments, 'comment_ID' ) );
  419. }
  420. /**
  421. * @ticket 8973
  422. */
  423. public function test_page_number_when_unapproved_comments_are_included_for_current_user() {
  424. $current_user = get_current_user_id();
  425. $post = self::factory()->post->create();
  426. $user = self::factory()->user->create_and_get();
  427. $comment_args = array(
  428. 'comment_post_ID' => $post,
  429. 'comment_approved' => 0,
  430. 'comment_author_email' => $user->user_email,
  431. 'comment_author' => $user->display_name,
  432. 'comment_author_url' => $user->user_url,
  433. 'user_id' => $user->ID,
  434. );
  435. for ( $i = 1; $i < 4; $i++ ) {
  436. self::factory()->comment->create(
  437. array_merge(
  438. $comment_args,
  439. array(
  440. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', time() - ( $i * 1000 ) ),
  441. )
  442. )
  443. );
  444. }
  445. $new_unapproved = self::factory()->comment->create(
  446. $comment_args
  447. );
  448. wp_set_current_user( $user->ID );
  449. $page = get_page_of_comment( $new_unapproved, array( 'per_page' => 3 ) );
  450. $comments = get_comments(
  451. array(
  452. 'number' => 3,
  453. 'paged' => $page,
  454. 'post_id' => $post,
  455. 'status' => 'approve',
  456. 'include_unapproved' => array( $user->ID ),
  457. 'orderby' => 'comment_date_gmt',
  458. 'order' => 'ASC',
  459. )
  460. );
  461. $this->assertContains( $new_unapproved, wp_list_pluck( $comments, 'comment_ID' ) );
  462. wp_set_current_user( $current_user );
  463. }
  464. public function get_current_commenter() {
  465. return array(
  466. 'comment_author_email' => 'foo@bar.test',
  467. 'comment_author' => 'Foo',
  468. 'comment_author_url' => 'https://bar.test',
  469. );
  470. }
  471. }