/tests/phpunit/tests/comment/commentsTemplate.php

https://gitlab.com/morganestes/wordpress-develop · PHP · 916 lines · 726 code · 111 blank · 79 comment · 0 complexity · 559f54849c82f1c029e4cf14a254f66d MD5 · raw file

  1. <?php
  2. /**
  3. * @group comment
  4. *
  5. * Testing items that are only testable by grabbing the markup of `comments_template()` from the output buffer.
  6. */
  7. class Tests_Comment_CommentsTemplate extends WP_UnitTestCase {
  8. /**
  9. * @ticket 8071
  10. */
  11. public function test_should_respect_comment_order_asc_when_default_comments_page_is_newest() {
  12. $now = time();
  13. $p = self::factory()->post->create();
  14. $comment_1 = self::factory()->comment->create(
  15. array(
  16. 'comment_post_ID' => $p,
  17. 'comment_content' => '1',
  18. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
  19. )
  20. );
  21. $comment_2 = self::factory()->comment->create(
  22. array(
  23. 'comment_post_ID' => $p,
  24. 'comment_content' => '2',
  25. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
  26. )
  27. );
  28. update_option( 'comment_order', 'asc' );
  29. update_option( 'default_comments_page', 'newest' );
  30. $this->go_to( get_permalink( $p ) );
  31. $found = get_echo( 'comments_template' );
  32. // Life in the fast lane.
  33. $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches );
  34. $found_cids = array_map( 'intval', $matches[1] );
  35. $this->assertSame( array( $comment_2, $comment_1 ), $found_cids );
  36. }
  37. /**
  38. * @ticket 8071
  39. */
  40. public function test_should_respect_comment_order_desc_when_default_comments_page_is_newest() {
  41. $now = time();
  42. $p = self::factory()->post->create();
  43. $comment_1 = self::factory()->comment->create(
  44. array(
  45. 'comment_post_ID' => $p,
  46. 'comment_content' => '1',
  47. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
  48. )
  49. );
  50. $comment_2 = self::factory()->comment->create(
  51. array(
  52. 'comment_post_ID' => $p,
  53. 'comment_content' => '2',
  54. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
  55. )
  56. );
  57. update_option( 'comment_order', 'desc' );
  58. update_option( 'default_comments_page', 'newest' );
  59. $this->go_to( get_permalink( $p ) );
  60. $found = get_echo( 'comments_template' );
  61. // Life in the fast lane.
  62. $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches );
  63. $found_cids = array_map( 'intval', $matches[1] );
  64. $this->assertSame( array( $comment_1, $comment_2 ), $found_cids );
  65. }
  66. /**
  67. * @ticket 8071
  68. */
  69. public function test_should_respect_comment_order_asc_when_default_comments_page_is_oldest() {
  70. $now = time();
  71. $p = self::factory()->post->create();
  72. $comment_1 = self::factory()->comment->create(
  73. array(
  74. 'comment_post_ID' => $p,
  75. 'comment_content' => '1',
  76. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
  77. )
  78. );
  79. $comment_2 = self::factory()->comment->create(
  80. array(
  81. 'comment_post_ID' => $p,
  82. 'comment_content' => '2',
  83. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
  84. )
  85. );
  86. update_option( 'comment_order', 'asc' );
  87. update_option( 'default_comments_page', 'oldest' );
  88. $this->go_to( get_permalink( $p ) );
  89. $found = get_echo( 'comments_template' );
  90. // Life in the fast lane.
  91. $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches );
  92. $found_cids = array_map( 'intval', $matches[1] );
  93. $this->assertSame( array( $comment_2, $comment_1 ), $found_cids );
  94. }
  95. /**
  96. * @ticket 8071
  97. */
  98. public function test_should_respect_comment_order_desc_when_default_comments_page_is_oldest() {
  99. $now = time();
  100. $p = self::factory()->post->create();
  101. $comment_1 = self::factory()->comment->create(
  102. array(
  103. 'comment_post_ID' => $p,
  104. 'comment_content' => '1',
  105. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
  106. )
  107. );
  108. $comment_2 = self::factory()->comment->create(
  109. array(
  110. 'comment_post_ID' => $p,
  111. 'comment_content' => '2',
  112. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
  113. )
  114. );
  115. update_option( 'comment_order', 'desc' );
  116. update_option( 'default_comments_page', 'oldest' );
  117. $this->go_to( get_permalink( $p ) );
  118. $found = get_echo( 'comments_template' );
  119. // Life in the fast lane.
  120. $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches );
  121. $found_cids = array_map( 'intval', $matches[1] );
  122. $this->assertSame( array( $comment_1, $comment_2 ), $found_cids );
  123. }
  124. /**
  125. * @ticket 8071
  126. */
  127. public function test_should_respect_comment_order_asc_when_default_comments_page_is_newest_on_subsequent_pages() {
  128. $now = time();
  129. $p = self::factory()->post->create();
  130. $comment_1 = self::factory()->comment->create(
  131. array(
  132. 'comment_post_ID' => $p,
  133. 'comment_content' => '1',
  134. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
  135. )
  136. );
  137. $comment_2 = self::factory()->comment->create(
  138. array(
  139. 'comment_post_ID' => $p,
  140. 'comment_content' => '2',
  141. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
  142. )
  143. );
  144. $comment_3 = self::factory()->comment->create(
  145. array(
  146. 'comment_post_ID' => $p,
  147. 'comment_content' => '3',
  148. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
  149. )
  150. );
  151. $comment_4 = self::factory()->comment->create(
  152. array(
  153. 'comment_post_ID' => $p,
  154. 'comment_content' => '4',
  155. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ),
  156. )
  157. );
  158. $comment_5 = self::factory()->comment->create(
  159. array(
  160. 'comment_post_ID' => $p,
  161. 'comment_content' => '3',
  162. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 500 ),
  163. )
  164. );
  165. $comment_6 = self::factory()->comment->create(
  166. array(
  167. 'comment_post_ID' => $p,
  168. 'comment_content' => '4',
  169. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 600 ),
  170. )
  171. );
  172. update_option( 'comment_order', 'asc' );
  173. update_option( 'default_comments_page', 'newest' );
  174. update_option( 'page_comments', '1' );
  175. $link = add_query_arg(
  176. array(
  177. 'cpage' => 2,
  178. 'comments_per_page' => 2,
  179. ), get_permalink( $p )
  180. );
  181. $this->go_to( $link );
  182. $found = get_echo( 'comments_template' );
  183. // Life in the fast lane.
  184. $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches );
  185. $found_cids = array_map( 'intval', $matches[1] );
  186. $this->assertSame( array( $comment_4, $comment_3 ), $found_cids );
  187. }
  188. /**
  189. * @ticket 8071
  190. */
  191. public function test_should_respect_comment_order_desc_when_default_comments_page_is_newest_on_subsequent_pages() {
  192. $now = time();
  193. $p = self::factory()->post->create();
  194. $comment_1 = self::factory()->comment->create(
  195. array(
  196. 'comment_post_ID' => $p,
  197. 'comment_content' => '1',
  198. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
  199. )
  200. );
  201. $comment_2 = self::factory()->comment->create(
  202. array(
  203. 'comment_post_ID' => $p,
  204. 'comment_content' => '2',
  205. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
  206. )
  207. );
  208. $comment_3 = self::factory()->comment->create(
  209. array(
  210. 'comment_post_ID' => $p,
  211. 'comment_content' => '3',
  212. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
  213. )
  214. );
  215. $comment_4 = self::factory()->comment->create(
  216. array(
  217. 'comment_post_ID' => $p,
  218. 'comment_content' => '4',
  219. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ),
  220. )
  221. );
  222. $comment_5 = self::factory()->comment->create(
  223. array(
  224. 'comment_post_ID' => $p,
  225. 'comment_content' => '3',
  226. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 500 ),
  227. )
  228. );
  229. $comment_6 = self::factory()->comment->create(
  230. array(
  231. 'comment_post_ID' => $p,
  232. 'comment_content' => '4',
  233. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 600 ),
  234. )
  235. );
  236. update_option( 'comment_order', 'desc' );
  237. update_option( 'default_comments_page', 'newest' );
  238. update_option( 'page_comments', '1' );
  239. $link = add_query_arg(
  240. array(
  241. 'cpage' => 2,
  242. 'comments_per_page' => 2,
  243. ), get_permalink( $p )
  244. );
  245. $this->go_to( $link );
  246. $found = get_echo( 'comments_template' );
  247. // Life in the fast lane.
  248. $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches );
  249. $found_cids = array_map( 'intval', $matches[1] );
  250. $this->assertSame( array( $comment_3, $comment_4 ), $found_cids );
  251. }
  252. /**
  253. * @ticket 8071
  254. */
  255. public function test_should_respect_comment_order_asc_when_default_comments_page_is_oldest_on_subsequent_pages() {
  256. $now = time();
  257. $p = self::factory()->post->create();
  258. $comment_1 = self::factory()->comment->create(
  259. array(
  260. 'comment_post_ID' => $p,
  261. 'comment_content' => '1',
  262. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
  263. )
  264. );
  265. $comment_2 = self::factory()->comment->create(
  266. array(
  267. 'comment_post_ID' => $p,
  268. 'comment_content' => '2',
  269. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
  270. )
  271. );
  272. $comment_3 = self::factory()->comment->create(
  273. array(
  274. 'comment_post_ID' => $p,
  275. 'comment_content' => '3',
  276. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
  277. )
  278. );
  279. $comment_4 = self::factory()->comment->create(
  280. array(
  281. 'comment_post_ID' => $p,
  282. 'comment_content' => '4',
  283. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ),
  284. )
  285. );
  286. update_option( 'comment_order', 'asc' );
  287. update_option( 'default_comments_page', 'oldest' );
  288. update_option( 'page_comments', '1' );
  289. $link = add_query_arg(
  290. array(
  291. 'cpage' => 2,
  292. 'comments_per_page' => 2,
  293. ), get_permalink( $p )
  294. );
  295. $this->go_to( $link );
  296. $found = get_echo( 'comments_template' );
  297. // Life in the fast lane.
  298. $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches );
  299. $found_cids = array_map( 'intval', $matches[1] );
  300. $this->assertSame( array( $comment_2, $comment_1 ), $found_cids );
  301. }
  302. /**
  303. * @ticket 8071
  304. */
  305. public function test_should_respect_comment_order_desc_when_default_comments_page_is_oldest_on_subsequent_pages() {
  306. $now = time();
  307. $p = self::factory()->post->create();
  308. $comment_1 = self::factory()->comment->create(
  309. array(
  310. 'comment_post_ID' => $p,
  311. 'comment_content' => '1',
  312. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
  313. )
  314. );
  315. $comment_2 = self::factory()->comment->create(
  316. array(
  317. 'comment_post_ID' => $p,
  318. 'comment_content' => '2',
  319. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
  320. )
  321. );
  322. $comment_3 = self::factory()->comment->create(
  323. array(
  324. 'comment_post_ID' => $p,
  325. 'comment_content' => '3',
  326. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
  327. )
  328. );
  329. $comment_4 = self::factory()->comment->create(
  330. array(
  331. 'comment_post_ID' => $p,
  332. 'comment_content' => '4',
  333. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ),
  334. )
  335. );
  336. update_option( 'comment_order', 'desc' );
  337. update_option( 'default_comments_page', 'oldest' );
  338. update_option( 'page_comments', '1' );
  339. $link = add_query_arg(
  340. array(
  341. 'cpage' => 2,
  342. 'comments_per_page' => 2,
  343. ), get_permalink( $p )
  344. );
  345. $this->go_to( $link );
  346. $found = get_echo( 'comments_template' );
  347. // Life in the fast lane.
  348. $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches );
  349. $found_cids = array_map( 'intval', $matches[1] );
  350. $this->assertSame( array( $comment_1, $comment_2 ), $found_cids );
  351. }
  352. /**
  353. * @ticket 8071
  354. * @ticket 34073
  355. * @ticket 29462
  356. */
  357. public function test_last_page_of_comments_should_be_full_when_default_comment_page_is_newest() {
  358. $now = time();
  359. $p = self::factory()->post->create();
  360. $comment_1 = self::factory()->comment->create(
  361. array(
  362. 'comment_post_ID' => $p,
  363. 'comment_content' => '1',
  364. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
  365. )
  366. );
  367. $comment_2 = self::factory()->comment->create(
  368. array(
  369. 'comment_post_ID' => $p,
  370. 'comment_content' => '2',
  371. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
  372. )
  373. );
  374. $comment_3 = self::factory()->comment->create(
  375. array(
  376. 'comment_post_ID' => $p,
  377. 'comment_content' => '3',
  378. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
  379. )
  380. );
  381. update_option( 'default_comments_page', 'newest' );
  382. update_option( 'comment_order', 'desc' );
  383. update_option( 'page_comments', '1' );
  384. $link = add_query_arg(
  385. array(
  386. 'cpage' => 1,
  387. 'comments_per_page' => 2,
  388. ), get_permalink( $p )
  389. );
  390. $this->go_to( $link );
  391. $found = get_echo( 'comments_template' );
  392. $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches );
  393. $found_cids = array_map( 'intval', $matches[1] );
  394. $this->assertSame( array( $comment_2, $comment_3 ), $found_cids );
  395. }
  396. /**
  397. * @ticket 8071
  398. * @ticket 34073
  399. * @ticket 29462
  400. */
  401. public function test_first_page_of_comments_should_have_remainder_when_default_comments_page_is_newest() {
  402. $now = time();
  403. $p = self::factory()->post->create();
  404. $comment_1 = self::factory()->comment->create(
  405. array(
  406. 'comment_post_ID' => $p,
  407. 'comment_content' => '1',
  408. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
  409. )
  410. );
  411. $comment_2 = self::factory()->comment->create(
  412. array(
  413. 'comment_post_ID' => $p,
  414. 'comment_content' => '2',
  415. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
  416. )
  417. );
  418. $comment_3 = self::factory()->comment->create(
  419. array(
  420. 'comment_post_ID' => $p,
  421. 'comment_content' => '3',
  422. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
  423. )
  424. );
  425. update_option( 'default_comments_page', 'newest' );
  426. update_option( 'comment_order', 'desc' );
  427. update_option( 'page_comments', '1' );
  428. $link = add_query_arg(
  429. array(
  430. 'cpage' => 2,
  431. 'comments_per_page' => 2,
  432. ), get_permalink( $p )
  433. );
  434. $this->go_to( $link );
  435. $found = get_echo( 'comments_template' );
  436. $comments = preg_match_all( '/id="comment-([0-9]+)"/', $found, $matches );
  437. $found_cids = array_map( 'intval', $matches[1] );
  438. $this->assertSame( array( $comment_1 ), $found_cids );
  439. }
  440. /**
  441. * @ticket 34073
  442. */
  443. public function test_comment_permalinks_should_be_correct_when_using_default_display_callback_with_default_comment_page_oldest() {
  444. $now = time();
  445. $p = self::factory()->post->create();
  446. $comment_1 = self::factory()->comment->create(
  447. array(
  448. 'comment_post_ID' => $p,
  449. 'comment_content' => '1',
  450. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
  451. )
  452. );
  453. $comment_2 = self::factory()->comment->create(
  454. array(
  455. 'comment_post_ID' => $p,
  456. 'comment_content' => '2',
  457. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
  458. )
  459. );
  460. $comment_3 = self::factory()->comment->create(
  461. array(
  462. 'comment_post_ID' => $p,
  463. 'comment_content' => '3',
  464. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
  465. )
  466. );
  467. $comment_4 = self::factory()->comment->create(
  468. array(
  469. 'comment_post_ID' => $p,
  470. 'comment_content' => '4',
  471. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ),
  472. )
  473. );
  474. update_option( 'comment_order', 'desc' );
  475. update_option( 'default_comments_page', 'oldest' );
  476. update_option( 'page_comments', '1' );
  477. $link_p1 = add_query_arg(
  478. array(
  479. 'comments_per_page' => 2,
  480. ), get_permalink( $p )
  481. );
  482. $this->go_to( $link_p1 );
  483. $found_p1 = get_echo( 'comments_template' );
  484. // Find the comment permalinks.
  485. preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p1, $matches );
  486. // This is the main post page, so we don't expect any cpage param.
  487. foreach ( $matches[1] as $m ) {
  488. $this->assertNotContains( 'cpage', $m );
  489. }
  490. $link_p2 = add_query_arg(
  491. array(
  492. 'cpage' => 2,
  493. 'comments_per_page' => 2,
  494. ), get_permalink( $p )
  495. );
  496. $this->go_to( $link_p2 );
  497. $found_p2 = get_echo( 'comments_template' );
  498. // Find the comment permalinks.
  499. preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p2, $matches );
  500. // They should all be on page 2.
  501. foreach ( $matches[1] as $m ) {
  502. $this->assertContains( 'cpage=2', $m );
  503. }
  504. }
  505. /**
  506. * @ticket 34073
  507. */
  508. public function test_comment_permalinks_should_be_correct_when_using_default_display_callback_with_default_comment_page_newest() {
  509. $now = time();
  510. $p = self::factory()->post->create();
  511. $comment_1 = self::factory()->comment->create(
  512. array(
  513. 'comment_post_ID' => $p,
  514. 'comment_content' => '1',
  515. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
  516. )
  517. );
  518. $comment_2 = self::factory()->comment->create(
  519. array(
  520. 'comment_post_ID' => $p,
  521. 'comment_content' => '2',
  522. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
  523. )
  524. );
  525. $comment_3 = self::factory()->comment->create(
  526. array(
  527. 'comment_post_ID' => $p,
  528. 'comment_content' => '3',
  529. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
  530. )
  531. );
  532. $comment_4 = self::factory()->comment->create(
  533. array(
  534. 'comment_post_ID' => $p,
  535. 'comment_content' => '4',
  536. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ),
  537. )
  538. );
  539. $comment_5 = self::factory()->comment->create(
  540. array(
  541. 'comment_post_ID' => $p,
  542. 'comment_content' => '4',
  543. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 500 ),
  544. )
  545. );
  546. $comment_6 = self::factory()->comment->create(
  547. array(
  548. 'comment_post_ID' => $p,
  549. 'comment_content' => '4',
  550. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 600 ),
  551. )
  552. );
  553. update_option( 'comment_order', 'desc' );
  554. update_option( 'default_comments_page', 'newest' );
  555. update_option( 'page_comments', '1' );
  556. $link_p0 = add_query_arg(
  557. array(
  558. 'comments_per_page' => 2,
  559. ), get_permalink( $p )
  560. );
  561. $this->go_to( $link_p0 );
  562. $found_p0 = get_echo( 'comments_template' );
  563. // Find the comment permalinks.
  564. preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p0, $matches );
  565. foreach ( $matches[1] as $m ) {
  566. $this->assertContains( 'cpage=3', $m );
  567. }
  568. $link_p2 = add_query_arg(
  569. array(
  570. 'cpage' => 2,
  571. 'comments_per_page' => 2,
  572. ), get_permalink( $p )
  573. );
  574. $this->go_to( $link_p2 );
  575. $found_p2 = get_echo( 'comments_template' );
  576. // Find the comment permalinks.
  577. preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p2, $matches );
  578. // They should all be on page 2.
  579. foreach ( $matches[1] as $m ) {
  580. $this->assertContains( 'cpage=2', $m );
  581. }
  582. // p1 is the last page (neat!).
  583. $link_p1 = add_query_arg(
  584. array(
  585. 'cpage' => 1,
  586. 'comments_per_page' => 2,
  587. ), get_permalink( $p )
  588. );
  589. $this->go_to( $link_p1 );
  590. $found_p1 = get_echo( 'comments_template' );
  591. // Find the comment permalinks.
  592. preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p1, $matches );
  593. // They should all be on page 2.
  594. foreach ( $matches[1] as $m ) {
  595. $this->assertContains( 'cpage=1', $m );
  596. }
  597. }
  598. /**
  599. * @ticket 35068
  600. */
  601. public function test_query_offset_should_not_include_unapproved_comments() {
  602. $now = time();
  603. $p = self::factory()->post->create();
  604. $comment_1 = self::factory()->comment->create(
  605. array(
  606. 'comment_post_ID' => $p,
  607. 'comment_content' => '1',
  608. 'comment_approved' => '0',
  609. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
  610. )
  611. );
  612. $comment_2 = self::factory()->comment->create(
  613. array(
  614. 'comment_post_ID' => $p,
  615. 'comment_content' => '2',
  616. 'comment_approved' => '0',
  617. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
  618. )
  619. );
  620. $comment_3 = self::factory()->comment->create(
  621. array(
  622. 'comment_post_ID' => $p,
  623. 'comment_content' => '3',
  624. 'comment_approved' => '0',
  625. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
  626. )
  627. );
  628. $comment_4 = self::factory()->comment->create(
  629. array(
  630. 'comment_post_ID' => $p,
  631. 'comment_content' => '4',
  632. 'comment_approved' => '1',
  633. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ),
  634. )
  635. );
  636. update_option( 'comment_order', 'asc' );
  637. update_option( 'default_comments_page', 'newest' );
  638. update_option( 'page_comments', 1 );
  639. update_option( 'comments_per_page', 2 );
  640. $this->go_to( get_permalink( $p ) );
  641. $found = get_echo( 'comments_template' );
  642. // Find the found comments in the markup.
  643. preg_match_all( '|id="comment-([0-9]+)|', $found, $matches );
  644. $found_cids = array_map( 'intval', $matches[1] );
  645. $this->assertSame( array( $comment_4 ), $found_cids );
  646. }
  647. /**
  648. * @ticket 35068
  649. */
  650. public function test_query_offset_should_include_unapproved_comments() {
  651. $comment_author_email = 'foo@example.com';
  652. $now = time();
  653. $p = self::factory()->post->create();
  654. $comment_1 = self::factory()->comment->create(
  655. array(
  656. 'comment_post_ID' => $p,
  657. 'comment_content' => '1',
  658. 'comment_approved' => '0',
  659. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
  660. )
  661. );
  662. $comment_2 = self::factory()->comment->create(
  663. array(
  664. 'comment_post_ID' => $p,
  665. 'comment_content' => '2',
  666. 'comment_approved' => '0',
  667. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
  668. )
  669. );
  670. $comment_3 = self::factory()->comment->create(
  671. array(
  672. 'comment_post_ID' => $p,
  673. 'comment_content' => '3',
  674. 'comment_approved' => '0',
  675. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
  676. 'comment_author_email' => $comment_author_email,
  677. )
  678. );
  679. $comment_4 = self::factory()->comment->create(
  680. array(
  681. 'comment_post_ID' => $p,
  682. 'comment_content' => '4',
  683. 'comment_approved' => '0',
  684. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
  685. 'comment_author_email' => $comment_author_email,
  686. )
  687. );
  688. $comment_5 = self::factory()->comment->create(
  689. array(
  690. 'comment_post_ID' => $p,
  691. 'comment_content' => '5',
  692. 'comment_approved' => '0',
  693. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
  694. 'comment_author_email' => $comment_author_email,
  695. )
  696. );
  697. $comment_6 = self::factory()->comment->create(
  698. array(
  699. 'comment_post_ID' => $p,
  700. 'comment_content' => '6',
  701. 'comment_approved' => '1',
  702. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ),
  703. )
  704. );
  705. update_option( 'comment_order', 'asc' );
  706. update_option( 'default_comments_page', 'newest' );
  707. update_option( 'page_comments', 1 );
  708. update_option( 'comments_per_page', 2 );
  709. add_filter( 'wp_get_current_commenter', array( $this, 'fake_current_commenter' ) );
  710. $this->go_to( get_permalink( $p ) );
  711. $found = get_echo( 'comments_template' );
  712. remove_filter( 'wp_get_current_commenter', array( $this, 'fake_current_commenter' ) );
  713. // Find the found comments in the markup.
  714. preg_match_all( '|id="comment-([0-9]+)|', $found, $matches );
  715. $found_cids = array_map( 'intval', $matches[1] );
  716. $this->assertSame( array( $comment_4, $comment_3 ), $found_cids );
  717. }
  718. public function fake_current_commenter( $commenter ) {
  719. $commenter['comment_author_email'] = 'foo@example.com';
  720. return $commenter;
  721. }
  722. /**
  723. * @ticket 35378
  724. */
  725. public function test_hierarchy_should_be_ignored_when_threading_is_disabled() {
  726. $now = time();
  727. $p = self::factory()->post->create();
  728. $comment_1 = self::factory()->comment->create(
  729. array(
  730. 'comment_post_ID' => $p,
  731. 'comment_content' => '1',
  732. 'comment_approved' => '1',
  733. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
  734. )
  735. );
  736. $comment_2 = self::factory()->comment->create(
  737. array(
  738. 'comment_post_ID' => $p,
  739. 'comment_content' => '2',
  740. 'comment_approved' => '1',
  741. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
  742. )
  743. );
  744. $comment_3 = self::factory()->comment->create(
  745. array(
  746. 'comment_post_ID' => $p,
  747. 'comment_content' => '3',
  748. 'comment_approved' => '1',
  749. 'comment_parent' => $comment_1,
  750. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
  751. )
  752. );
  753. update_option( 'comment_order', 'asc' );
  754. update_option( 'thread_comments', 0 );
  755. $this->go_to( get_permalink( $p ) );
  756. $found = get_echo( 'comments_template' );
  757. // Find the found comments in the markup.
  758. preg_match_all( '|id="comment-([0-9]+)|', $found, $matches );
  759. $found_cids = array_map( 'intval', $matches[1] );
  760. $this->assertSame( array( $comment_2, $comment_3, $comment_1 ), $found_cids );
  761. }
  762. /**
  763. * @ticket 35419
  764. */
  765. public function test_pagination_calculation_should_ignore_comment_hierarchy_when_threading_is_disabled() {
  766. $now = time();
  767. $p = self::factory()->post->create();
  768. $comment_1 = self::factory()->comment->create(
  769. array(
  770. 'comment_post_ID' => $p,
  771. 'comment_content' => '1',
  772. 'comment_approved' => '1',
  773. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
  774. )
  775. );
  776. $comment_2 = self::factory()->comment->create(
  777. array(
  778. 'comment_post_ID' => $p,
  779. 'comment_content' => '2',
  780. 'comment_approved' => '1',
  781. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
  782. )
  783. );
  784. $comment_3 = self::factory()->comment->create(
  785. array(
  786. 'comment_post_ID' => $p,
  787. 'comment_content' => '3',
  788. 'comment_approved' => '1',
  789. 'comment_parent' => $comment_1,
  790. 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
  791. )
  792. );
  793. update_option( 'thread_comments', 0 );
  794. update_option( 'comment_order', 'asc' );
  795. update_option( 'comments_per_page', 2 );
  796. update_option( 'page_comments', 1 );
  797. update_option( 'default_comments_page', 'newest' );
  798. $this->go_to( get_permalink( $p ) );
  799. $found = get_echo( 'comments_template' );
  800. // Find the found comments in the markup.
  801. preg_match_all( '|id="comment-([0-9]+)|', $found, $matches );
  802. $found_cids = array_map( 'intval', $matches[1] );
  803. $this->assertSame( array( $comment_3 ), $found_cids );
  804. }
  805. }