comment.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. <?php
  2. /**
  3. * @group comment
  4. */
  5. class Tests_Comment extends WP_UnitTestCase {
  6. protected static $user_id;
  7. protected static $post_id;
  8. protected static $notify_message = '';
  9. public function setUp() {
  10. parent::setUp();
  11. reset_phpmailer_instance();
  12. }
  13. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  14. self::$user_id = $factory->user->create(
  15. array(
  16. 'role' => 'author',
  17. 'user_login' => 'test_wp_user_get',
  18. 'user_pass' => 'password',
  19. 'user_email' => 'test@test.com',
  20. )
  21. );
  22. self::$post_id = $factory->post->create(
  23. array(
  24. 'post_author' => self::$user_id,
  25. )
  26. );
  27. }
  28. public function test_wp_update_comment() {
  29. $post = self::factory()->post->create_and_get(
  30. array(
  31. 'post_title' => 'some-post',
  32. 'post_type' => 'post',
  33. )
  34. );
  35. $post2 = self::factory()->post->create_and_get(
  36. array(
  37. 'post_title' => 'some-post-2',
  38. 'post_type' => 'post',
  39. )
  40. );
  41. $comments = self::factory()->comment->create_post_comments( $post->ID, 5 );
  42. $result = wp_update_comment(
  43. array(
  44. 'comment_ID' => $comments[0],
  45. 'comment_parent' => $comments[1],
  46. )
  47. );
  48. $this->assertSame( 1, $result );
  49. $comment = get_comment( $comments[0] );
  50. $this->assertEquals( $comments[1], $comment->comment_parent );
  51. $result = wp_update_comment(
  52. array(
  53. 'comment_ID' => $comments[0],
  54. 'comment_parent' => $comments[1],
  55. )
  56. );
  57. $this->assertSame( 0, $result );
  58. $result = wp_update_comment(
  59. array(
  60. 'comment_ID' => $comments[0],
  61. 'comment_post_ID' => $post2->ID,
  62. )
  63. );
  64. $comment = get_comment( $comments[0] );
  65. $this->assertEquals( $post2->ID, $comment->comment_post_ID );
  66. }
  67. /**
  68. * @ticket 30627
  69. */
  70. public function test_wp_update_comment_updates_comment_type() {
  71. $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
  72. wp_update_comment(
  73. array(
  74. 'comment_ID' => $comment_id,
  75. 'comment_type' => 'pingback',
  76. )
  77. );
  78. $comment = get_comment( $comment_id );
  79. $this->assertSame( 'pingback', $comment->comment_type );
  80. }
  81. /**
  82. * @ticket 36784
  83. */
  84. public function test_wp_update_comment_updates_comment_meta() {
  85. $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
  86. wp_update_comment(
  87. array(
  88. 'comment_ID' => $comment_id,
  89. 'comment_meta' => array(
  90. 'food' => 'taco',
  91. 'sauce' => 'fire',
  92. ),
  93. )
  94. );
  95. $this->assertSame( 'fire', get_comment_meta( $comment_id, 'sauce', true ) );
  96. }
  97. /**
  98. * @ticket 30307
  99. */
  100. public function test_wp_update_comment_updates_user_id() {
  101. $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
  102. wp_update_comment(
  103. array(
  104. 'comment_ID' => $comment_id,
  105. 'user_id' => 1,
  106. )
  107. );
  108. $comment = get_comment( $comment_id );
  109. $this->assertEquals( 1, $comment->user_id );
  110. }
  111. /**
  112. * @ticket 34954
  113. */
  114. public function test_wp_update_comment_with_no_post_id() {
  115. $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => 0 ) );
  116. $updated_comment_text = 'I should be able to update a comment with a Post ID of zero';
  117. $update = wp_update_comment(
  118. array(
  119. 'comment_ID' => $comment_id,
  120. 'comment_content' => $updated_comment_text,
  121. 'comment_post_ID' => 0,
  122. )
  123. );
  124. $this->assertSame( 1, $update );
  125. $comment = get_comment( $comment_id );
  126. $this->assertSame( $updated_comment_text, $comment->comment_content );
  127. }
  128. /**
  129. * @ticket 39732
  130. */
  131. public function test_wp_update_comment_returns_false_for_invalid_comment_or_post_id() {
  132. $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
  133. $update = wp_update_comment(
  134. array(
  135. 'comment_ID' => -1,
  136. 'comment_post_ID' => self::$post_id,
  137. )
  138. );
  139. $this->assertFalse( $update );
  140. $update = wp_update_comment(
  141. array(
  142. 'comment_ID' => $comment_id,
  143. 'comment_post_ID' => -1,
  144. )
  145. );
  146. $this->assertFalse( $update );
  147. }
  148. /**
  149. * @ticket 39732
  150. */
  151. public function test_wp_update_comment_is_wp_error() {
  152. $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
  153. add_filter( 'wp_update_comment_data', array( $this, '_wp_update_comment_data_filter' ), 10, 3 );
  154. $result = wp_update_comment(
  155. array(
  156. 'comment_ID' => $comment_id,
  157. 'comment_type' => 'pingback',
  158. ),
  159. true
  160. );
  161. remove_filter( 'wp_update_comment_data', array( $this, '_wp_update_comment_data_filter' ), 10, 3 );
  162. $this->assertWPError( $result );
  163. }
  164. /**
  165. * Blocks comments from being updated by returning WP_Error.
  166. */
  167. public function _wp_update_comment_data_filter( $data, $comment, $commentarr ) {
  168. return new WP_Error( 'comment_wrong', 'wp_update_comment_data filter fails for this comment.', 500 );
  169. }
  170. public function test_get_approved_comments() {
  171. $ca1 = self::factory()->comment->create(
  172. array(
  173. 'comment_post_ID' => self::$post_id,
  174. 'comment_approved' => '1',
  175. )
  176. );
  177. $ca2 = self::factory()->comment->create(
  178. array(
  179. 'comment_post_ID' => self::$post_id,
  180. 'comment_approved' => '1',
  181. )
  182. );
  183. $ca3 = self::factory()->comment->create(
  184. array(
  185. 'comment_post_ID' => self::$post_id,
  186. 'comment_approved' => '0',
  187. )
  188. );
  189. $c2 = self::factory()->comment->create(
  190. array(
  191. 'comment_post_ID' => self::$post_id,
  192. 'comment_approved' => '1',
  193. 'comment_type' => 'pingback',
  194. )
  195. );
  196. $c3 = self::factory()->comment->create(
  197. array(
  198. 'comment_post_ID' => self::$post_id,
  199. 'comment_approved' => '1',
  200. 'comment_type' => 'trackback',
  201. )
  202. );
  203. $c4 = self::factory()->comment->create(
  204. array(
  205. 'comment_post_ID' => self::$post_id,
  206. 'comment_approved' => '1',
  207. 'comment_type' => 'mario',
  208. )
  209. );
  210. $c5 = self::factory()->comment->create(
  211. array(
  212. 'comment_post_ID' => self::$post_id,
  213. 'comment_approved' => '1',
  214. 'comment_type' => 'luigi',
  215. )
  216. );
  217. $found = get_approved_comments( self::$post_id );
  218. // All comment types will be returned.
  219. $this->assertEquals( array( $ca1, $ca2, $c2, $c3, $c4, $c5 ), wp_list_pluck( $found, 'comment_ID' ) );
  220. }
  221. /**
  222. * @ticket 30412
  223. */
  224. public function test_get_approved_comments_with_post_id_0_should_return_empty_array() {
  225. $ca1 = self::factory()->comment->create(
  226. array(
  227. 'comment_post_ID' => self::$post_id,
  228. 'comment_approved' => '1',
  229. )
  230. );
  231. $found = get_approved_comments( 0 );
  232. $this->assertSame( array(), $found );
  233. }
  234. /**
  235. * @ticket 14279
  236. */
  237. public function test_wp_new_comment_respects_dates() {
  238. $data = array(
  239. 'comment_post_ID' => self::$post_id,
  240. 'comment_author' => 'Comment Author',
  241. 'comment_author_url' => '',
  242. 'comment_author_email' => '',
  243. 'comment_type' => '',
  244. 'comment_content' => 'Comment',
  245. 'comment_date' => '2011-01-01 10:00:00',
  246. 'comment_date_gmt' => '2011-01-01 10:00:00',
  247. );
  248. $id = wp_new_comment( $data );
  249. $comment = get_comment( $id );
  250. $this->assertSame( $data['comment_date'], $comment->comment_date );
  251. $this->assertSame( $data['comment_date_gmt'], $comment->comment_date_gmt );
  252. }
  253. /**
  254. * @ticket 14601
  255. */
  256. public function test_wp_new_comment_respects_author_ip() {
  257. $data = array(
  258. 'comment_post_ID' => self::$post_id,
  259. 'comment_author' => 'Comment Author',
  260. 'comment_author_IP' => '192.168.1.1',
  261. 'comment_author_url' => '',
  262. 'comment_author_email' => '',
  263. 'comment_type' => '',
  264. 'comment_content' => 'Comment',
  265. );
  266. $id = wp_new_comment( $data );
  267. $comment = get_comment( $id );
  268. $this->assertSame( $data['comment_author_IP'], $comment->comment_author_IP );
  269. }
  270. /**
  271. * @ticket 14601
  272. */
  273. public function test_wp_new_comment_respects_author_ip_empty_string() {
  274. $data = array(
  275. 'comment_post_ID' => self::$post_id,
  276. 'comment_author' => 'Comment Author',
  277. 'comment_author_IP' => '',
  278. 'comment_author_url' => '',
  279. 'comment_author_email' => '',
  280. 'comment_type' => '',
  281. 'comment_content' => 'Comment',
  282. );
  283. $id = wp_new_comment( $data );
  284. $comment = get_comment( $id );
  285. $this->assertSame( $data['comment_author_IP'], $comment->comment_author_IP );
  286. }
  287. /**
  288. * @ticket 14601
  289. */
  290. public function test_wp_new_comment_respects_comment_agent() {
  291. $data = array(
  292. 'comment_post_ID' => self::$post_id,
  293. 'comment_author' => 'Comment Author',
  294. 'comment_author_IP' => '',
  295. 'comment_author_url' => '',
  296. 'comment_author_email' => '',
  297. 'comment_agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53',
  298. 'comment_type' => '',
  299. 'comment_content' => 'Comment',
  300. );
  301. $id = wp_new_comment( $data );
  302. $comment = get_comment( $id );
  303. $this->assertSame( $data['comment_agent'], $comment->comment_agent );
  304. }
  305. /**
  306. * @ticket 14601
  307. */
  308. public function test_wp_new_comment_should_trim_provided_comment_agent_to_254_chars() {
  309. $data = array(
  310. 'comment_post_ID' => self::$post_id,
  311. 'comment_author' => 'Comment Author',
  312. 'comment_author_IP' => '',
  313. 'comment_author_url' => '',
  314. 'comment_author_email' => '',
  315. 'comment_agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53 Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16 Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en; rv:1.8.1.4pre) Gecko/20070511 Camino/1.6pre',
  316. 'comment_type' => '',
  317. 'comment_content' => 'Comment',
  318. );
  319. $id = wp_new_comment( $data );
  320. $comment = get_comment( $id );
  321. $this->assertSame( 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53 Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16 Mozilla/5.0 (Macintosh; U; PPC Mac OS ', $comment->comment_agent );
  322. }
  323. /**
  324. * @ticket 14601
  325. */
  326. public function test_wp_new_comment_respects_comment_agent_empty_string() {
  327. $data = array(
  328. 'comment_post_ID' => self::$post_id,
  329. 'comment_author' => 'Comment Author',
  330. 'comment_author_IP' => '',
  331. 'comment_author_url' => '',
  332. 'comment_author_email' => '',
  333. 'comment_agent' => '',
  334. 'comment_type' => '',
  335. 'comment_content' => 'Comment',
  336. );
  337. $id = wp_new_comment( $data );
  338. $comment = get_comment( $id );
  339. $this->assertSame( $data['comment_agent'], $comment->comment_agent );
  340. }
  341. public function test_comment_field_lengths() {
  342. $data = array(
  343. 'comment_post_ID' => self::$post_id,
  344. 'comment_author' => 'Comment Author',
  345. 'comment_author_url' => '',
  346. 'comment_author_email' => '',
  347. 'comment_type' => '',
  348. 'comment_content' => str_repeat( 'A', 65536 ),
  349. 'comment_date' => '2011-01-01 10:00:00',
  350. 'comment_date_gmt' => '2011-01-01 10:00:00',
  351. );
  352. $id = wp_new_comment( $data );
  353. $comment = get_comment( $id );
  354. $this->assertSame( strlen( $comment->comment_content ), 65535 );
  355. }
  356. /**
  357. * @ticket 32566
  358. */
  359. public function test_wp_notify_moderator_should_not_throw_notice_when_post_author_is_0() {
  360. $p = self::factory()->post->create(
  361. array(
  362. 'post_author' => 0,
  363. )
  364. );
  365. $c = self::factory()->comment->create(
  366. array(
  367. 'comment_post_ID' => $p,
  368. )
  369. );
  370. $this->assertTrue( wp_notify_moderator( $c ) );
  371. }
  372. public function test_wp_new_comment_notify_postauthor_should_send_email_when_comment_is_approved() {
  373. $c = self::factory()->comment->create(
  374. array(
  375. 'comment_post_ID' => self::$post_id,
  376. )
  377. );
  378. $sent = wp_new_comment_notify_postauthor( $c );
  379. $this->assertTrue( $sent );
  380. }
  381. public function test_wp_new_comment_notify_postauthor_should_not_send_email_when_comment_is_unapproved() {
  382. $c = self::factory()->comment->create(
  383. array(
  384. 'comment_post_ID' => self::$post_id,
  385. 'comment_approved' => '0',
  386. )
  387. );
  388. $sent = wp_new_comment_notify_postauthor( $c );
  389. $this->assertFalse( $sent );
  390. }
  391. /**
  392. * @ticket 33587
  393. */
  394. public function test_wp_new_comment_notify_postauthor_should_not_send_email_when_comment_has_been_marked_as_spam() {
  395. $c = self::factory()->comment->create(
  396. array(
  397. 'comment_post_ID' => self::$post_id,
  398. 'comment_approved' => 'spam',
  399. )
  400. );
  401. $sent = wp_new_comment_notify_postauthor( $c );
  402. $this->assertFalse( $sent );
  403. }
  404. /**
  405. * @ticket 35006
  406. */
  407. public function test_wp_new_comment_notify_postauthor_should_not_send_email_when_comment_has_been_trashed() {
  408. $c = self::factory()->comment->create(
  409. array(
  410. 'comment_post_ID' => self::$post_id,
  411. 'comment_approved' => 'trash',
  412. )
  413. );
  414. $sent = wp_new_comment_notify_postauthor( $c );
  415. $this->assertFalse( $sent );
  416. }
  417. /**
  418. * @ticket 43805
  419. */
  420. public function test_wp_new_comment_notify_postauthor_content_should_include_link_to_parent() {
  421. $c1 = self::factory()->comment->create(
  422. array(
  423. 'comment_post_ID' => self::$post_id,
  424. )
  425. );
  426. $c2 = self::factory()->comment->create(
  427. array(
  428. 'comment_post_ID' => self::$post_id,
  429. 'comment_parent' => $c1,
  430. )
  431. );
  432. add_filter( 'comment_notification_text', array( $this, 'save_comment_notification_text' ) );
  433. wp_new_comment_notify_postauthor( $c2 );
  434. remove_filter( 'comment_notification_text', array( $this, 'save_comment_notification_text' ) );
  435. $this->assertContains( admin_url( "comment.php?action=editcomment&c={$c1}" ), self::$notify_message );
  436. }
  437. /**
  438. * @ticket 43805
  439. */
  440. public function test_wp_new_comment_notify_moderator_content_should_include_link_to_parent() {
  441. $c1 = self::factory()->comment->create(
  442. array(
  443. 'comment_post_ID' => self::$post_id,
  444. )
  445. );
  446. $c2 = self::factory()->comment->create(
  447. array(
  448. 'comment_post_ID' => self::$post_id,
  449. 'comment_parent' => $c1,
  450. 'comment_approved' => '0',
  451. )
  452. );
  453. add_filter( 'comment_moderation_text', array( $this, 'save_comment_notification_text' ) );
  454. wp_new_comment_notify_moderator( $c2 );
  455. remove_filter( 'comment_moderation_text', array( $this, 'save_comment_notification_text' ) );
  456. $this->assertContains( admin_url( "comment.php?action=editcomment&c={$c1}" ), self::$notify_message );
  457. }
  458. /**
  459. * Callback for the `comment_notification_text` & `comment_moderation_text` filters.
  460. *
  461. * @param string $notify_message The comment notification or moderation email text.
  462. * @return string
  463. */
  464. public function save_comment_notification_text( $notify_message = '' ) {
  465. self::$notify_message = $notify_message;
  466. return $notify_message;
  467. }
  468. /**
  469. * @ticket 12431
  470. */
  471. public function test_wp_new_comment_with_meta() {
  472. $c = self::factory()->comment->create(
  473. array(
  474. 'comment_approved' => '1',
  475. 'comment_meta' => array(
  476. 'food' => 'taco',
  477. 'sauce' => 'fire',
  478. ),
  479. )
  480. );
  481. $this->assertSame( 'fire', get_comment_meta( $c, 'sauce', true ) );
  482. }
  483. /**
  484. * @ticket 8071
  485. */
  486. public function test_wp_comment_get_children_should_fill_children() {
  487. $c1 = self::factory()->comment->create(
  488. array(
  489. 'comment_post_ID' => self::$post_id,
  490. 'comment_approved' => '1',
  491. )
  492. );
  493. $c2 = self::factory()->comment->create(
  494. array(
  495. 'comment_post_ID' => self::$post_id,
  496. 'comment_approved' => '1',
  497. 'comment_parent' => $c1,
  498. )
  499. );
  500. $c3 = self::factory()->comment->create(
  501. array(
  502. 'comment_post_ID' => self::$post_id,
  503. 'comment_approved' => '1',
  504. 'comment_parent' => $c2,
  505. )
  506. );
  507. $c4 = self::factory()->comment->create(
  508. array(
  509. 'comment_post_ID' => self::$post_id,
  510. 'comment_approved' => '1',
  511. 'comment_parent' => $c1,
  512. )
  513. );
  514. $c5 = self::factory()->comment->create(
  515. array(
  516. 'comment_post_ID' => self::$post_id,
  517. 'comment_approved' => '1',
  518. )
  519. );
  520. $c6 = self::factory()->comment->create(
  521. array(
  522. 'comment_post_ID' => self::$post_id,
  523. 'comment_approved' => '1',
  524. 'comment_parent' => $c5,
  525. )
  526. );
  527. $comment = get_comment( $c1 );
  528. $children = $comment->get_children();
  529. // Direct descendants of $c1.
  530. $this->assertEqualSets( array( $c2, $c4 ), array_values( wp_list_pluck( $children, 'comment_ID' ) ) );
  531. // Direct descendants of $c2.
  532. $this->assertEqualSets( array( $c3 ), array_values( wp_list_pluck( $children[ $c2 ]->get_children(), 'comment_ID' ) ) );
  533. }
  534. /**
  535. * @ticket 27571
  536. */
  537. public function test_post_properties_should_be_lazyloaded() {
  538. $c = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
  539. $post = get_post( self::$post_id );
  540. $comment = get_comment( $c );
  541. $post_fields = array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_content_filtered', 'post_parent', 'guid', 'menu_order', 'post_type', 'post_mime_type', 'comment_count' );
  542. foreach ( $post_fields as $pf ) {
  543. $this->assertTrue( isset( $comment->$pf ), $pf );
  544. $this->assertSame( $post->$pf, $comment->$pf, $pf );
  545. }
  546. }
  547. /**
  548. * Helper function to set up comment for 761 tests.
  549. *
  550. * @since 4.4.0
  551. * @access public
  552. */
  553. public function setup_notify_comment() {
  554. /**
  555. * Prevent flood alert from firing.
  556. */
  557. add_filter( 'comment_flood_filter', '__return_false' );
  558. /**
  559. * Set up a comment for testing.
  560. */
  561. $post = $this->factory->post->create(
  562. array(
  563. 'post_author' => self::$user_id,
  564. )
  565. );
  566. $comment = $this->factory->comment->create(
  567. array(
  568. 'comment_post_ID' => $post,
  569. )
  570. );
  571. return array(
  572. 'post' => $post,
  573. 'comment' => $comment,
  574. );
  575. }
  576. /**
  577. * @ticket 761
  578. */
  579. public function test_wp_notify_moderator_filter_moderation_notify_option_true_filter_false() {
  580. $comment_data = $this->setup_notify_comment();
  581. /**
  582. * Test with moderator notification setting on, filter set to off.
  583. * Should not send a notification.
  584. */
  585. update_option( 'moderation_notify', 1 );
  586. add_filter( 'notify_moderator', '__return_false' );
  587. $notification_sent = $this->try_sending_moderator_notification( $comment_data['comment'], $comment_data['post'] );
  588. $this->assertFalse( $notification_sent, 'Moderator notification setting on, filter set to off' );
  589. remove_filter( 'notify_moderator', '__return_false' );
  590. remove_filter( 'comment_flood_filter', '__return_false' );
  591. }
  592. /**
  593. * @ticket 761
  594. */
  595. public function test_wp_notify_moderator_filter_moderation_notify_option_false_filter_true() {
  596. $comment_data = $this->setup_notify_comment();
  597. /**
  598. * Test with moderator notification setting off, filter set to on.
  599. * Should send a notification.
  600. */
  601. update_option( 'moderation_notify', 0 );
  602. add_filter( 'notify_moderator', '__return_true' );
  603. $notification_sent = $this->try_sending_moderator_notification( $comment_data['comment'], $comment_data['post'] );
  604. $this->assertTrue( $notification_sent, 'Moderator notification setting off, filter set to on' );
  605. remove_filter( 'notify_moderator', '__return_true' );
  606. remove_filter( 'comment_flood_filter', '__return_false' );
  607. }
  608. /**
  609. * @ticket 761
  610. */
  611. public function test_wp_notify_post_author_filter_comments_notify_option_true_filter_false() {
  612. $comment_data = $this->setup_notify_comment();
  613. /**
  614. * Test with author notification setting on, filter set to off.
  615. * Should not send a notification.
  616. */
  617. update_option( 'comments_notify', 1 );
  618. add_filter( 'notify_post_author', '__return_false' );
  619. $notification_sent = $this->try_sending_author_notification( $comment_data['comment'], $comment_data['post'] );
  620. $this->assertFalse( $notification_sent, 'Test with author notification setting on, filter set to off' );
  621. remove_filter( 'notify_post_author', '__return_false' );
  622. remove_filter( 'comment_flood_filter', '__return_false' );
  623. }
  624. /**
  625. * @ticket 761
  626. */
  627. public function test_wp_notify_post_author_filter_comments_notify_option_false_filter_true() {
  628. $comment_data = $this->setup_notify_comment();
  629. /**
  630. * Test with author notification setting off, filter set to on.
  631. * Should send a notification.
  632. */
  633. update_option( 'comments_notify', 0 );
  634. add_filter( 'notify_post_author', '__return_true' );
  635. $notification_sent = $this->try_sending_author_notification( $comment_data['comment'], $comment_data['post'] );
  636. $this->assertTrue( $notification_sent, 'Test with author notification setting off, filter set to on' );
  637. remove_filter( 'notify_post_author', '__return_true' );
  638. remove_filter( 'comment_flood_filter', '__return_false' );
  639. }
  640. /**
  641. * Helper function to test moderator notifications.
  642. *
  643. * @since 4.4.0
  644. * @access public
  645. */
  646. public function try_sending_moderator_notification( $comment, $post ) {
  647. // Don't approve comments, triggering notifications.
  648. add_filter( 'pre_comment_approved', '__return_false' );
  649. // Moderators are notified when a new comment is added.
  650. $data = array(
  651. 'comment_post_ID' => $post,
  652. 'comment_author' => 'Comment Author',
  653. 'comment_author_url' => '',
  654. 'comment_author_email' => '',
  655. 'comment_type' => '',
  656. 'comment_content' => 'Comment',
  657. );
  658. wp_new_comment( $data );
  659. // Check to see if a notification email was sent to the moderator `admin@example.org`.
  660. if ( isset( $GLOBALS['phpmailer']->mock_sent )
  661. && ! empty( $GLOBALS['phpmailer']->mock_sent )
  662. && WP_TESTS_EMAIL === $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]
  663. ) {
  664. $email_sent_when_comment_added = true;
  665. reset_phpmailer_instance();
  666. } else {
  667. $email_sent_when_comment_added = false;
  668. }
  669. return $email_sent_when_comment_added;
  670. }
  671. /**
  672. * Helper function to test sending author notifications.
  673. *
  674. * @since 4.4.0
  675. * @access public
  676. */
  677. public function try_sending_author_notification( $comment, $post ) {
  678. // Approve comments, triggering notifications.
  679. add_filter( 'pre_comment_approved', '__return_true' );
  680. // Post authors possibly notified when a comment is approved on their post.
  681. wp_set_comment_status( $comment, 'approve' );
  682. // Check to see if a notification email was sent to the post author `test@test.com`.
  683. if ( isset( $GLOBALS['phpmailer']->mock_sent )
  684. && ! empty( $GLOBALS['phpmailer']->mock_sent )
  685. && 'test@test.com' === $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]
  686. ) {
  687. $email_sent_when_comment_approved = true;
  688. } else {
  689. $email_sent_when_comment_approved = false;
  690. }
  691. reset_phpmailer_instance();
  692. // Post authors are notified when a new comment is added to their post.
  693. $data = array(
  694. 'comment_post_ID' => $post,
  695. 'comment_author' => 'Comment Author',
  696. 'comment_author_url' => '',
  697. 'comment_author_email' => '',
  698. 'comment_type' => '',
  699. 'comment_content' => 'Comment',
  700. );
  701. wp_new_comment( $data );
  702. // Check to see if a notification email was sent to the post author `test@test.com`.
  703. if ( isset( $GLOBALS['phpmailer']->mock_sent ) &&
  704. ! empty( $GLOBALS['phpmailer']->mock_sent ) &&
  705. 'test@test.com' === $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] ) {
  706. $email_sent_when_comment_added = true;
  707. reset_phpmailer_instance();
  708. } else {
  709. $email_sent_when_comment_added = false;
  710. }
  711. return $email_sent_when_comment_approved || $email_sent_when_comment_added;
  712. }
  713. public function test_close_comments_for_old_post() {
  714. update_option( 'close_comments_for_old_posts', true );
  715. // Close comments more than one day old.
  716. update_option( 'close_comments_days_old', 1 );
  717. $old_date = strtotime( '-25 hours' );
  718. $old_post_id = self::factory()->post->create( array( 'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $old_date ) ) );
  719. $old_post_comment_status = _close_comments_for_old_post( true, $old_post_id );
  720. $this->assertFalse( $old_post_comment_status );
  721. $new_post_comment_status = _close_comments_for_old_post( true, self::$post_id );
  722. $this->assertTrue( $new_post_comment_status );
  723. }
  724. public function test_close_comments_for_old_post_undated_draft() {
  725. $draft_id = self::factory()->post->create(
  726. array(
  727. 'post_status' => 'draft',
  728. 'post_type' => 'post',
  729. )
  730. );
  731. $draft_comment_status = _close_comments_for_old_post( true, $draft_id );
  732. $this->assertTrue( $draft_comment_status );
  733. }
  734. /**
  735. * @ticket 35276
  736. */
  737. public function test_wp_update_comment_author_id_and_agent() {
  738. $default_data = array(
  739. 'comment_post_ID' => self::$post_id,
  740. 'comment_author' => 'Comment Author',
  741. 'comment_author_IP' => '192.168.0.1',
  742. 'comment_agent' => 'WRONG_AGENT',
  743. 'comment_author_url' => '',
  744. 'comment_author_email' => '',
  745. 'comment_type' => '',
  746. 'comment_content' => 'Comment',
  747. );
  748. $comment_id = wp_new_comment( $default_data );
  749. // Confirm that the IP and Agent are correct on initial save.
  750. $save = get_comment( $comment_id );
  751. $this->assertSame( $default_data['comment_author_IP'], $save->comment_author_IP );
  752. $this->assertSame( $default_data['comment_agent'], $save->comment_agent );
  753. // Update the comment.
  754. wp_update_comment(
  755. array(
  756. 'comment_ID' => $comment_id,
  757. 'comment_author_IP' => '111.111.1.1',
  758. 'comment_agent' => 'SHIELD_AGENT',
  759. )
  760. );
  761. // Retrieve and check the new values.
  762. $updated = get_comment( $comment_id );
  763. $this->assertSame( '111.111.1.1', $updated->comment_author_IP );
  764. $this->assertSame( 'SHIELD_AGENT', $updated->comment_agent );
  765. }
  766. public function test_wp_get_comment_fields_max_lengths() {
  767. $expected = array(
  768. 'comment_author' => 245,
  769. 'comment_author_email' => 100,
  770. 'comment_author_url' => 200,
  771. 'comment_content' => 65525,
  772. );
  773. $lengths = wp_get_comment_fields_max_lengths();
  774. foreach ( $lengths as $field => $length ) {
  775. $this->assertSame( $expected[ $field ], $length );
  776. }
  777. }
  778. /**
  779. * The `wp_comments_personal_data_eraser()` function should erase user's comments.
  780. *
  781. * @group privacy
  782. * @ticket 43442
  783. */
  784. public function test_wp_comments_personal_data_eraser() {
  785. $post_id = self::factory()->post->create();
  786. $user_id = self::factory()->user->create();
  787. $args = array(
  788. 'user_id' => $user_id,
  789. 'comment_post_ID' => $post_id,
  790. 'comment_author' => 'Comment Author',
  791. 'comment_author_email' => 'personal@local.host',
  792. 'comment_author_url' => 'https://local.host/',
  793. 'comment_author_IP' => '192.168.0.1',
  794. 'comment_date' => '2018-04-14 17:20:00',
  795. 'comment_agent' => 'COMMENT_AGENT',
  796. 'comment_content' => 'Comment Content',
  797. );
  798. $comment_id = self::factory()->comment->create( $args );
  799. wp_comments_personal_data_eraser( $args['comment_author_email'] );
  800. $comment = get_comment( $comment_id );
  801. $actual = array(
  802. 'comment_ID' => $comment->comment_ID,
  803. 'user_id' => $comment->user_id,
  804. 'comment_author' => $comment->comment_author,
  805. 'comment_author_email' => $comment->comment_author_email,
  806. 'comment_author_url' => $comment->comment_author_url,
  807. 'comment_author_IP' => $comment->comment_author_IP,
  808. 'comment_date' => $comment->comment_date,
  809. 'comment_date_gmt' => $comment->comment_date_gmt,
  810. 'comment_agent' => $comment->comment_agent,
  811. 'comment_content' => $comment->comment_content,
  812. );
  813. $expected = array(
  814. 'comment_ID' => (string) $comment_id,
  815. 'user_id' => '0', // Anonymized.
  816. 'comment_author' => 'Anonymous', // Anonymized.
  817. 'comment_author_email' => '', // Anonymized.
  818. 'comment_author_url' => '', // Anonymized.
  819. 'comment_author_IP' => '192.168.0.0', // Anonymized.
  820. 'comment_date' => '2018-04-14 17:20:00',
  821. 'comment_date_gmt' => '2018-04-14 17:20:00',
  822. 'comment_agent' => '', // Anonymized.
  823. 'comment_content' => 'Comment Content',
  824. );
  825. $this->assertSame( $expected, $actual );
  826. }
  827. /**
  828. * Testing the `wp_comments_personal_data_eraser()` function's output on an empty first page.
  829. *
  830. * @group privacy
  831. * @ticket 43442
  832. */
  833. public function test_wp_comments_personal_data_eraser_empty_first_page_output() {
  834. $actual = wp_comments_personal_data_eraser( 'nocommentsfound@local.host' );
  835. $expected = array(
  836. 'items_removed' => false,
  837. 'items_retained' => false,
  838. 'messages' => array(),
  839. 'done' => true,
  840. );
  841. $this->assertSame( $expected, $actual );
  842. }
  843. /**
  844. * Testing the `wp_comments_personal_data_eraser()` function's output, for the non-empty first page.
  845. *
  846. * @group privacy
  847. * @ticket 43442
  848. */
  849. public function test_wp_comments_personal_data_eraser_non_empty_first_page_output() {
  850. $post_id = self::factory()->post->create();
  851. $args = array(
  852. 'comment_post_ID' => $post_id,
  853. 'comment_author' => 'Comment Author',
  854. 'comment_author_email' => 'personal@local.host',
  855. 'comment_author_url' => 'https://local.host/',
  856. 'comment_author_IP' => '192.168.0.1',
  857. 'comment_date' => '2018-04-14 17:20:00',
  858. 'comment_agent' => 'COMMENT_AGENT',
  859. 'comment_content' => 'Comment Content',
  860. );
  861. self::factory()->comment->create( $args );
  862. $actual = wp_comments_personal_data_eraser( $args['comment_author_email'] );
  863. $expected = array(
  864. 'items_removed' => true,
  865. 'items_retained' => false,
  866. 'messages' => array(),
  867. 'done' => true,
  868. );
  869. $this->assertSame( $expected, $actual );
  870. }
  871. /**
  872. * Testing the `wp_comments_personal_data_eraser()` function's output, for an empty second page.
  873. *
  874. * @group privacy
  875. * @ticket 43442
  876. */
  877. public function test_wp_comments_personal_data_eraser_empty_second_page_output() {
  878. $post_id = self::factory()->post->create();
  879. $args = array(
  880. 'comment_post_ID' => $post_id,
  881. 'comment_author' => 'Comment Author',
  882. 'comment_author_email' => 'personal@local.host',
  883. 'comment_author_url' => 'https://local.host/',
  884. 'comment_author_IP' => '192.168.0.1',
  885. 'comment_date' => '2018-04-14 17:20:00',
  886. 'comment_agent' => 'COMMENT_AGENT',
  887. 'comment_content' => 'Comment Content',
  888. );
  889. self::factory()->comment->create( $args );
  890. $actual = wp_comments_personal_data_eraser( $args['comment_author_email'], 2 );
  891. $expected = array(
  892. 'items_removed' => false,
  893. 'items_retained' => false,
  894. 'messages' => array(),
  895. 'done' => true,
  896. );
  897. $this->assertSame( $expected, $actual );
  898. }
  899. /**
  900. * Testing the `wp_anonymize_comment` filter, to prevent comment anonymization.
  901. *
  902. * @group privacy
  903. * @ticket 43442
  904. */
  905. public function test_wp_anonymize_comment_filter_to_prevent_comment_anonymization() {
  906. $post_id = self::factory()->post->create();
  907. $args = array(
  908. 'comment_post_ID' => $post_id,
  909. 'comment_author' => 'Comment Author',
  910. 'comment_author_email' => 'personal@local.host',
  911. 'comment_author_url' => 'https://local.host/',
  912. 'comment_author_IP' => '192.168.0.1',
  913. 'comment_date' => '2018-04-14 17:20:00',
  914. 'comment_agent' => 'COMMENT_AGENT',
  915. 'comment_content' => 'Comment Content',
  916. );
  917. $comment_id = self::factory()->comment->create( $args );
  918. add_filter( 'wp_anonymize_comment', '__return_false' );
  919. $actual = wp_comments_personal_data_eraser( $args['comment_author_email'] );
  920. remove_filter( 'wp_anonymize_comment', '__return_false' );
  921. $message = sprintf( 'Comment %d contains personal data but could not be anonymized.', $comment_id );
  922. $expected = array(
  923. 'items_removed' => false,
  924. 'items_retained' => true,
  925. 'messages' => array( $message ),
  926. 'done' => true,
  927. );
  928. $this->assertSame( $expected, $actual );
  929. }
  930. /**
  931. * Testing the `wp_anonymize_comment` filter, to prevent comment anonymization, with a custom message.
  932. *
  933. * @group privacy
  934. * @ticket 43442
  935. */
  936. public function test_wp_anonymize_comment_filter_to_prevent_comment_anonymization_with_custom_message() {
  937. $post_id = self::factory()->post->create();
  938. $args = array(
  939. 'comment_post_ID' => $post_id,
  940. 'comment_author' => 'Comment Author',
  941. 'comment_author_email' => 'personal@local.host',
  942. 'comment_author_url' => 'https://local.host/',
  943. 'comment_author_IP' => '192.168.0.1',
  944. 'comment_date' => '2018-04-14 17:20:00',
  945. 'comment_agent' => 'COMMENT_AGENT',
  946. 'comment_content' => 'Comment Content',
  947. );
  948. $comment_id = self::factory()->comment->create( $args );
  949. add_filter( 'wp_anonymize_comment', array( $this, 'wp_anonymize_comment_custom_message' ), 10, 3 );
  950. $actual = wp_comments_personal_data_eraser( $args['comment_author_email'] );
  951. remove_filter( 'wp_anonymize_comment', array( $this, 'wp_anonymize_comment_custom_message' ) );
  952. $message = sprintf( 'Some custom message for comment %d.', $comment_id );
  953. $expected = array(
  954. 'items_removed' => false,
  955. 'items_retained' => true,
  956. 'messages' => array( $message ),
  957. 'done' => true,
  958. );
  959. $this->assertSame( $expected, $actual );
  960. }
  961. /**
  962. * Callback for the `wp_anonymize_comment` filter.
  963. *
  964. * @param bool|string $anonymize Whether to apply the comment anonymization (bool).
  965. * Custom prevention message (string). Default true.
  966. * @param WP_Comment $comment WP_Comment object.
  967. * @param array $anonymized_comment Anonymized comment data.
  968. * @return string
  969. */
  970. public function wp_anonymize_comment_custom_message( $anonymize, $comment, $anonymized_comment ) {
  971. return sprintf( 'Some custom message for comment %d.', $comment->comment_ID );
  972. }
  973. public function test_update_should_invalidate_comment_cache() {
  974. global $wpdb;
  975. $c = self::factory()->comment->create( array( 'comment_author' => 'Foo' ) );
  976. $comment = get_comment( $c );
  977. $this->assertSame( 'Foo', $comment->comment_author );
  978. wp_update_comment(
  979. array(
  980. 'comment_ID' => $c,
  981. 'comment_author' => 'Bar',
  982. )
  983. );
  984. $comment = get_comment( $c );
  985. $this->assertSame( 'Bar', $comment->comment_author );
  986. }
  987. public function test_trash_should_invalidate_comment_cache() {
  988. global $wpdb;
  989. $c = self::factory()->comment->create();
  990. $comment = get_comment( $c );
  991. wp_trash_comment( $c );
  992. $comment = get_comment( $c );
  993. $this->assertSame( 'trash', $comment->comment_approved );
  994. }
  995. public function test_untrash_should_invalidate_comment_cache() {
  996. global $wpdb;
  997. $c = self::factory()->comment->create();
  998. wp_trash_comment( $c );
  999. $comment = get_comment( $c );
  1000. $this->assertSame( 'trash', $comment->comment_approved );
  1001. wp_untrash_comment( $c );
  1002. $comment = get_comment( $c );
  1003. $this->assertSame( '1', $comment->comment_approved );
  1004. }
  1005. public function test_spam_should_invalidate_comment_cache() {
  1006. global $wpdb;
  1007. $c = self::factory()->comment->create();
  1008. $comment = get_comment( $c );
  1009. wp_spam_comment( $c );
  1010. $comment = get_comment( $c );
  1011. $this->assertSame( 'spam', $comment->comment_approved );
  1012. }
  1013. public function test_unspam_should_invalidate_comment_cache() {
  1014. global $wpdb;
  1015. $c = self::factory()->comment->create();
  1016. wp_spam_comment( $c );
  1017. $comment = get_comment( $c );
  1018. $this->assertSame( 'spam', $comment->comment_approved );
  1019. wp_unspam_comment( $c );
  1020. $comment = get_comment( $c );
  1021. $this->assertSame( '1', $comment->comment_approved );
  1022. }
  1023. /**
  1024. * Testing the `wp_comments_personal_data_exporter()` function.
  1025. *
  1026. * @group privacy
  1027. * @ticket 43440
  1028. */
  1029. public function test_wp_comments_personal_data_exporter() {
  1030. $args = array(
  1031. 'comment_post_ID' => self::$post_id,
  1032. 'comment_author' => 'Comment Author',
  1033. 'comment_author_email' => 'personal@local.host',
  1034. 'comment_author_url' => 'https://local.host/',
  1035. 'comment_author_IP' => '192.168.0.1',
  1036. 'comment_agent' => 'SOME_AGENT',
  1037. 'comment_date' => '2018-03-28 20:05:00',
  1038. 'comment_content' => 'Comment',
  1039. );
  1040. $comment_id = self::factory()->comment->create( $args );
  1041. $actual = wp_comments_personal_data_exporter( $args['comment_author_email'] );
  1042. $expected = $args;
  1043. $this->assertTrue( $actual['done'] );
  1044. // Number of exported comments.
  1045. $this->assertSame( 1, count( $actual['data'] ) );
  1046. // Number of exported comment properties.
  1047. $this->assertSame( 8, count( $actual['data'][0]['data'] ) );
  1048. // Exported group.
  1049. $this->assertSame( 'comments', $actual['data'][0]['group_id'] );
  1050. $this->assertSame( 'Comments', $actual['data'][0]['group_label'] );
  1051. // Exported comment properties.
  1052. $this->assertSame( $expected['comment_author'], $actual['data'][0]['data'][0]['value'] );
  1053. $this->assertSame( $expected['comment_author_email'], $actual['data'][0]['data'][1]['value'] );
  1054. $this->assertSame( $expected['comment_author_url'], $actual['data'][0]['data'][2]['value'] );
  1055. $this->assertSame( $expected['comment_author_IP'], $actual['data'][0]['data'][3]['value'] );
  1056. $this->assertSame( $expected['comment_agent'], $actual['data'][0]['data'][4]['value'] );
  1057. $this->assertSame( $expected['comment_date'], $actual['data'][0]['data'][5]['value'] );
  1058. $this->assertSame( $expected['comment_content'], $actual['data'][0]['data'][6]['value'] );
  1059. $this->assertSame( esc_html( get_comment_link( $comment_id ) ), strip_tags( $actual['data'][0]['data'][7]['value'] ) );
  1060. }
  1061. /**
  1062. * Testing the `wp_comments_personal_data_exporter()` function for no comments found.
  1063. *
  1064. * @group privacy
  1065. * @ticket 43440
  1066. */
  1067. public function test_wp_comments_personal_data_exporter_no_comments_found() {
  1068. $actual = wp_comments_personal_data_exporter( 'nocommentsfound@local.host' );
  1069. $expected = array(
  1070. 'data' => array(),
  1071. 'done' => true,
  1072. );
  1073. $this->assertSame( $expected, $actual );
  1074. }
  1075. /**
  1076. * Testing the `wp_comments_personal_data_exporter()` function for an empty comment property.
  1077. *
  1078. * @group privacy
  1079. * @ticket 43440
  1080. */
  1081. public function test_wp_comments_personal_data_exporter_empty_comment_prop() {
  1082. $args = array(
  1083. 'comment_post_ID' => self::$post_id,
  1084. 'comment_author' => 'Comment Author',
  1085. 'comment_author_email' => 'personal@local.host',
  1086. 'comment_author_url' => 'https://local.host/',
  1087. 'comment_author_IP' => '192.168.0.1',
  1088. 'comment_date' => '2018-03-28 20:05:00',
  1089. 'comment_agent' => '',
  1090. 'comment_content' => 'Comment',
  1091. );
  1092. $c = self::factory()->comment->create( $args );
  1093. $actual = wp_comments_personal_data_exporter( $args['comment_author_email'] );
  1094. $this->assertTrue( $actual['done'] );
  1095. // Number of exported comments.
  1096. $this->assertSame( 1, count( $actual['data'] ) );
  1097. // Number of exported comment properties.
  1098. $this->assertSame( 7, count( $actual['data'][0]['data'] ) );
  1099. }
  1100. /**
  1101. * Testing the `wp_comments_personal_data_exporter()` function with an empty second page.
  1102. *
  1103. * @group privacy
  1104. * @ticket 43440
  1105. */
  1106. public function test_wp_comments_personal_data_exporter_empty_second_page() {
  1107. $args = array(
  1108. 'comment_post_ID' => self::$post_id,
  1109. 'comment_author' => 'Comment Author',
  1110. 'comment_author_email' => 'personal@local.host',
  1111. 'comment_author_url' => 'https://local.host/',
  1112. 'comment_author_IP' => '192.168.0.1',
  1113. 'comment_date' => '2018-03-28 20:05:00',
  1114. 'comment_agent' => 'SOME_AGENT',
  1115. 'comment_content' => 'Comment',
  1116. );
  1117. $c = self::factory()->comment->create( $args );
  1118. $actual = wp_comments_personal_data_exporter( $args['comment_author_email'], 2 );
  1119. $this->assertTrue( $actual['done'] );
  1120. // Number of exported comments.
  1121. $this->assertSame( 0, count( $actual['data'] ) );
  1122. }
  1123. }