comment-submission.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. <?php
  2. /**
  3. * @group comment
  4. */
  5. class Tests_Comment_Submission extends WP_UnitTestCase {
  6. protected static $post;
  7. protected static $author_id;
  8. protected static $editor_id;
  9. protected $preprocess_comment_data = array();
  10. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  11. self::$post = $factory->post->create_and_get();
  12. self::$author_id = $factory->user->create(
  13. array(
  14. 'role' => 'author',
  15. )
  16. );
  17. self::$editor_id = $factory->user->create(
  18. array(
  19. 'role' => 'editor',
  20. )
  21. );
  22. }
  23. public static function wpTearDownAfterClass() {
  24. wp_delete_post( self::$post->ID, true );
  25. self::delete_user( self::$author_id );
  26. self::delete_user( self::$editor_id );
  27. }
  28. function setUp() {
  29. parent::setUp();
  30. require_once ABSPATH . WPINC . '/class-phpass.php';
  31. }
  32. public function test_submitting_comment_to_invalid_post_returns_error() {
  33. $error = 'comment_id_not_found';
  34. $this->assertSame( 0, did_action( $error ) );
  35. $data = array(
  36. 'comment_post_ID' => 0,
  37. );
  38. $comment = wp_handle_comment_submission( $data );
  39. $this->assertSame( 1, did_action( $error ) );
  40. $this->assertWPError( $comment );
  41. $this->assertSame( $error, $comment->get_error_code() );
  42. }
  43. public function test_submitting_comment_to_post_with_closed_comments_returns_error() {
  44. $error = 'comment_closed';
  45. $this->assertSame( 0, did_action( $error ) );
  46. $post = self::factory()->post->create_and_get(
  47. array(
  48. 'comment_status' => 'closed',
  49. )
  50. );
  51. $data = array(
  52. 'comment_post_ID' => $post->ID,
  53. );
  54. $comment = wp_handle_comment_submission( $data );
  55. $this->assertSame( 1, did_action( $error ) );
  56. $this->assertWPError( $comment );
  57. $this->assertSame( $error, $comment->get_error_code() );
  58. }
  59. public function test_submitting_comment_to_trashed_post_returns_error() {
  60. $error = 'comment_on_trash';
  61. $this->assertSame( 0, did_action( $error ) );
  62. wp_trash_post( self::$post->ID );
  63. $data = array(
  64. 'comment_post_ID' => self::$post->ID,
  65. );
  66. $comment = wp_handle_comment_submission( $data );
  67. wp_untrash_post( self::$post->ID );
  68. $this->assertSame( 1, did_action( $error ) );
  69. $this->assertWPError( $comment );
  70. $this->assertSame( $error, $comment->get_error_code() );
  71. }
  72. public function test_submitting_comment_to_draft_post_returns_error() {
  73. $error = 'comment_on_draft';
  74. $this->assertSame( 0, did_action( $error ) );
  75. $post = self::factory()->post->create_and_get(
  76. array(
  77. 'post_status' => 'draft',
  78. )
  79. );
  80. $data = array(
  81. 'comment_post_ID' => $post->ID,
  82. );
  83. $comment = wp_handle_comment_submission( $data );
  84. $this->assertSame( 1, did_action( $error ) );
  85. $this->assertWPError( $comment );
  86. $this->assertSame( $error, $comment->get_error_code() );
  87. $this->assertEmpty( $comment->get_error_message() );
  88. }
  89. /**
  90. * @ticket 39650
  91. */
  92. public function test_submitting_comment_to_draft_post_returns_error_message_for_user_with_correct_caps() {
  93. $error = 'comment_on_draft';
  94. wp_set_current_user( self::$author_id );
  95. $this->assertSame( 0, did_action( $error ) );
  96. $post = self::factory()->post->create_and_get(
  97. array(
  98. 'post_status' => 'draft',
  99. 'post_author' => self::$author_id,
  100. )
  101. );
  102. $data = array(
  103. 'comment_post_ID' => $post->ID,
  104. );
  105. $comment = wp_handle_comment_submission( $data );
  106. $this->assertSame( 1, did_action( $error ) );
  107. $this->assertWPError( $comment );
  108. $this->assertSame( $error, $comment->get_error_code() );
  109. $this->assertNotEmpty( $comment->get_error_message() );
  110. }
  111. public function test_submitting_comment_to_scheduled_post_returns_error() {
  112. // Same error as commenting on a draft.
  113. $error = 'comment_on_draft';
  114. $this->assertSame( 0, did_action( $error ) );
  115. $post = self::factory()->post->create_and_get(
  116. array(
  117. 'post_date' => gmdate( 'Y-m-d H:i:s', strtotime( '+1 day' ) ),
  118. )
  119. );
  120. $this->assertSame( 'future', $post->post_status );
  121. $data = array(
  122. 'comment_post_ID' => $post->ID,
  123. );
  124. $comment = wp_handle_comment_submission( $data );
  125. $this->assertSame( 1, did_action( $error ) );
  126. $this->assertWPError( $comment );
  127. $this->assertSame( $error, $comment->get_error_code() );
  128. }
  129. public function test_submitting_comment_to_password_required_post_returns_error() {
  130. $error = 'comment_on_password_protected';
  131. $this->assertSame( 0, did_action( $error ) );
  132. $post = self::factory()->post->create_and_get(
  133. array(
  134. 'post_password' => 'password',
  135. )
  136. );
  137. $data = array(
  138. 'comment_post_ID' => $post->ID,
  139. );
  140. $comment = wp_handle_comment_submission( $data );
  141. $this->assertSame( 1, did_action( $error ) );
  142. $this->assertWPError( $comment );
  143. $this->assertSame( $error, $comment->get_error_code() );
  144. }
  145. public function test_submitting_comment_to_password_protected_post_succeeds() {
  146. $password = 'password';
  147. $hasher = new PasswordHash( 8, true );
  148. $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] = $hasher->HashPassword( $password );
  149. $post = self::factory()->post->create_and_get(
  150. array(
  151. 'post_password' => $password,
  152. )
  153. );
  154. $data = array(
  155. 'comment_post_ID' => $post->ID,
  156. 'comment' => 'Comment',
  157. 'author' => 'Comment Author',
  158. 'email' => 'comment@example.org',
  159. );
  160. $comment = wp_handle_comment_submission( $data );
  161. unset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
  162. $this->assertNotWPError( $comment );
  163. $this->assertInstanceOf( 'WP_Comment', $comment );
  164. }
  165. public function test_submitting_valid_comment_as_logged_in_user_succeeds() {
  166. $user = self::factory()->user->create_and_get(
  167. array(
  168. 'user_url' => 'http://user.example.org',
  169. )
  170. );
  171. wp_set_current_user( $user->ID );
  172. $data = array(
  173. 'comment_post_ID' => self::$post->ID,
  174. 'comment' => 'Comment',
  175. );
  176. $comment = wp_handle_comment_submission( $data );
  177. $this->assertNotWPError( $comment );
  178. $this->assertInstanceOf( 'WP_Comment', $comment );
  179. $this->assertSame( 'Comment', $comment->comment_content );
  180. $this->assertSame( $user->display_name, $comment->comment_author );
  181. $this->assertSame( $user->user_email, $comment->comment_author_email );
  182. $this->assertSame( $user->user_url, $comment->comment_author_url );
  183. $this->assertSame( $user->ID, (int) $comment->user_id );
  184. }
  185. public function test_submitting_valid_comment_anonymously_succeeds() {
  186. $data = array(
  187. 'comment_post_ID' => self::$post->ID,
  188. 'comment' => 'Comment',
  189. 'author' => 'Comment Author',
  190. 'email' => 'comment@example.org',
  191. 'url' => 'user.example.org',
  192. );
  193. $comment = wp_handle_comment_submission( $data );
  194. $this->assertNotWPError( $comment );
  195. $this->assertInstanceOf( 'WP_Comment', $comment );
  196. $this->assertSame( 'Comment', $comment->comment_content );
  197. $this->assertSame( 'Comment Author', $comment->comment_author );
  198. $this->assertSame( 'comment@example.org', $comment->comment_author_email );
  199. $this->assertSame( 'http://user.example.org', $comment->comment_author_url );
  200. $this->assertSame( '0', $comment->user_id );
  201. }
  202. /**
  203. * wp_handle_comment_submission() expects un-slashed data.
  204. *
  205. * @group slashes
  206. */
  207. public function test_submitting_comment_handles_slashes_correctly_handles_slashes() {
  208. $data = array(
  209. 'comment_post_ID' => self::$post->ID,
  210. 'comment' => 'Comment with 1 slash: \\',
  211. 'author' => 'Comment Author with 1 slash: \\',
  212. 'email' => 'comment@example.org',
  213. );
  214. $comment = wp_handle_comment_submission( $data );
  215. $this->assertNotWPError( $comment );
  216. $this->assertInstanceOf( 'WP_Comment', $comment );
  217. $this->assertSame( 'Comment with 1 slash: \\', $comment->comment_content );
  218. $this->assertSame( 'Comment Author with 1 slash: \\', $comment->comment_author );
  219. $this->assertSame( 'comment@example.org', $comment->comment_author_email );
  220. }
  221. public function test_submitting_comment_anonymously_to_private_post_returns_error() {
  222. $error = 'comment_id_not_found';
  223. $post = self::factory()->post->create_and_get(
  224. array(
  225. 'post_status' => 'private',
  226. )
  227. );
  228. $data = array(
  229. 'comment_post_ID' => $post->ID,
  230. );
  231. $comment = wp_handle_comment_submission( $data );
  232. $this->assertFalse( is_user_logged_in() );
  233. $this->assertWPError( $comment );
  234. $this->assertSame( $error, $comment->get_error_code() );
  235. }
  236. public function test_submitting_comment_as_logged_in_user_to_inaccessible_private_post_returns_error() {
  237. $error = 'comment_id_not_found';
  238. $user = self::factory()->user->create_and_get(
  239. array(
  240. 'role' => 'author',
  241. )
  242. );
  243. wp_set_current_user( $user->ID );
  244. $post = self::factory()->post->create_and_get(
  245. array(
  246. 'post_status' => 'private',
  247. 'post_author' => self::$author_id,
  248. )
  249. );
  250. $data = array(
  251. 'comment_post_ID' => $post->ID,
  252. );
  253. $comment = wp_handle_comment_submission( $data );
  254. $this->assertFalse( current_user_can( 'read_post', $post->ID ) );
  255. $this->assertWPError( $comment );
  256. $this->assertSame( $error, $comment->get_error_code() );
  257. }
  258. public function test_submitting_comment_to_private_post_with_closed_comments_returns_correct_error() {
  259. $error = 'comment_id_not_found';
  260. $user = self::factory()->user->create_and_get(
  261. array(
  262. 'role' => 'author',
  263. )
  264. );
  265. wp_set_current_user( $user->ID );
  266. $post = self::factory()->post->create_and_get(
  267. array(
  268. 'post_status' => 'private',
  269. 'post_author' => self::$author_id,
  270. 'comment_status' => 'closed',
  271. )
  272. );
  273. $data = array(
  274. 'comment_post_ID' => $post->ID,
  275. );
  276. $comment = wp_handle_comment_submission( $data );
  277. $this->assertFalse( current_user_can( 'read_post', $post->ID ) );
  278. $this->assertWPError( $comment );
  279. $this->assertSame( $error, $comment->get_error_code() );
  280. }
  281. public function test_submitting_comment_to_own_private_post_succeeds() {
  282. wp_set_current_user( self::$author_id );
  283. $post = self::factory()->post->create_and_get(
  284. array(
  285. 'post_status' => 'private',
  286. 'post_author' => self::$author_id,
  287. )
  288. );
  289. $data = array(
  290. 'comment_post_ID' => $post->ID,
  291. 'comment' => 'Comment',
  292. );
  293. $comment = wp_handle_comment_submission( $data );
  294. $this->assertTrue( current_user_can( 'read_post', $post->ID ) );
  295. $this->assertNotWPError( $comment );
  296. $this->assertInstanceOf( 'WP_Comment', $comment );
  297. }
  298. public function test_submitting_comment_to_accessible_private_post_succeeds() {
  299. wp_set_current_user( self::$editor_id );
  300. $post = self::factory()->post->create_and_get(
  301. array(
  302. 'post_status' => 'private',
  303. 'post_author' => self::$author_id,
  304. )
  305. );
  306. $data = array(
  307. 'comment_post_ID' => $post->ID,
  308. 'comment' => 'Comment',
  309. );
  310. $comment = wp_handle_comment_submission( $data );
  311. $this->assertTrue( current_user_can( 'read_post', $post->ID ) );
  312. $this->assertNotWPError( $comment );
  313. $this->assertInstanceOf( 'WP_Comment', $comment );
  314. }
  315. public function test_anonymous_user_cannot_comment_unfiltered_html() {
  316. $data = array(
  317. 'comment_post_ID' => self::$post->ID,
  318. 'comment' => 'Comment <script>alert(document.cookie);</script>',
  319. 'author' => 'Comment Author',
  320. 'email' => 'comment@example.org',
  321. );
  322. $comment = wp_handle_comment_submission( $data );
  323. $this->assertNotWPError( $comment );
  324. $this->assertInstanceOf( 'WP_Comment', $comment );
  325. $this->assertNotContains( '<script', $comment->comment_content );
  326. }
  327. public function test_unprivileged_user_cannot_comment_unfiltered_html() {
  328. wp_set_current_user( self::$author_id );
  329. $this->assertFalse( current_user_can( 'unfiltered_html' ) );
  330. $data = array(
  331. 'comment_post_ID' => self::$post->ID,
  332. 'comment' => 'Comment <script>alert(document.cookie);</script>',
  333. );
  334. $comment = wp_handle_comment_submission( $data );
  335. $this->assertNotWPError( $comment );
  336. $this->assertInstanceOf( 'WP_Comment', $comment );
  337. $this->assertNotContains( '<script', $comment->comment_content );
  338. }
  339. public function test_unprivileged_user_cannot_comment_unfiltered_html_even_with_valid_nonce() {
  340. wp_set_current_user( self::$author_id );
  341. $this->assertFalse( current_user_can( 'unfiltered_html' ) );
  342. $action = 'unfiltered-html-comment_' . self::$post->ID;
  343. $nonce = wp_create_nonce( $action );
  344. $this->assertNotEmpty( wp_verify_nonce( $nonce, $action ) );
  345. $data = array(
  346. 'comment_post_ID' => self::$post->ID,
  347. 'comment' => 'Comment <script>alert(document.cookie);</script>',
  348. '_wp_unfiltered_html_comment' => $nonce,
  349. );
  350. $comment = wp_handle_comment_submission( $data );
  351. $this->assertNotWPError( $comment );
  352. $this->assertInstanceOf( 'WP_Comment', $comment );
  353. $this->assertNotContains( '<script', $comment->comment_content );
  354. }
  355. public function test_privileged_user_can_comment_unfiltered_html_with_valid_nonce() {
  356. $this->assertFalse( defined( 'DISALLOW_UNFILTERED_HTML' ) );
  357. if ( is_multisite() ) {
  358. // In multisite, only Super Admins can post unfiltered HTML.
  359. $this->assertFalse( user_can( self::$editor_id, 'unfiltered_html' ) );
  360. grant_super_admin( self::$editor_id );
  361. }
  362. wp_set_current_user( self::$editor_id );
  363. $this->assertTrue( current_user_can( 'unfiltered_html' ) );
  364. $action = 'unfiltered-html-comment_' . self::$post->ID;
  365. $nonce = wp_create_nonce( $action );
  366. $this->assertNotEmpty( wp_verify_nonce( $nonce, $action ) );
  367. $data = array(
  368. 'comment_post_ID' => self::$post->ID,
  369. 'comment' => 'Comment <script>alert(document.cookie);</script>',
  370. '_wp_unfiltered_html_comment' => $nonce,
  371. );
  372. $comment = wp_handle_comment_submission( $data );
  373. $this->assertNotWPError( $comment );
  374. $this->assertInstanceOf( 'WP_Comment', $comment );
  375. $this->assertContains( '<script', $comment->comment_content );
  376. }
  377. public function test_privileged_user_cannot_comment_unfiltered_html_without_valid_nonce() {
  378. if ( is_multisite() ) {
  379. // In multisite, only Super Admins can post unfiltered HTML.
  380. $this->assertFalse( user_can( self::$editor_id, 'unfiltered_html' ) );
  381. grant_super_admin( self::$editor_id );
  382. }
  383. wp_set_current_user( self::$editor_id );
  384. $this->assertTrue( current_user_can( 'unfiltered_html' ) );
  385. $data = array(
  386. 'comment_post_ID' => self::$post->ID,
  387. 'comment' => 'Comment <script>alert(document.cookie);</script>',
  388. );
  389. $comment = wp_handle_comment_submission( $data );
  390. $this->assertNotWPError( $comment );
  391. $this->assertInstanceOf( 'WP_Comment', $comment );
  392. $this->assertNotContains( '<script', $comment->comment_content );
  393. }
  394. public function test_submitting_comment_as_anonymous_user_when_registration_required_returns_error() {
  395. $error = 'not_logged_in';
  396. $_comment_registration = get_option( 'comment_registration' );
  397. update_option( 'comment_registration', '1' );
  398. $data = array(
  399. 'comment_post_ID' => self::$post->ID,
  400. );
  401. $comment = wp_handle_comment_submission( $data );
  402. update_option( 'comment_registration', $_comment_registration );
  403. $this->assertWPError( $comment );
  404. $this->assertSame( $error, $comment->get_error_code() );
  405. }
  406. public function test_submitting_comment_with_no_name_when_name_email_required_returns_error() {
  407. $error = 'require_name_email';
  408. $_require_name_email = get_option( 'require_name_email' );
  409. update_option( 'require_name_email', '1' );
  410. $data = array(
  411. 'comment_post_ID' => self::$post->ID,
  412. 'comment' => 'Comment',
  413. 'email' => 'comment@example.org',
  414. );
  415. $comment = wp_handle_comment_submission( $data );
  416. update_option( 'require_name_email', $_require_name_email );
  417. $this->assertWPError( $comment );
  418. $this->assertSame( $error, $comment->get_error_code() );
  419. }
  420. public function test_submitting_comment_with_no_email_when_name_email_required_returns_error() {
  421. $error = 'require_name_email';
  422. $_require_name_email = get_option( 'require_name_email' );
  423. update_option( 'require_name_email', '1' );
  424. $data = array(
  425. 'comment_post_ID' => self::$post->ID,
  426. 'comment' => 'Comment',
  427. 'author' => 'Comment Author',
  428. );
  429. $comment = wp_handle_comment_submission( $data );
  430. update_option( 'require_name_email', $_require_name_email );
  431. $this->assertWPError( $comment );
  432. $this->assertSame( $error, $comment->get_error_code() );
  433. }
  434. public function test_submitting_comment_with_invalid_email_when_name_email_required_returns_error() {
  435. $error = 'require_valid_email';
  436. $_require_name_email = get_option( 'require_name_email' );
  437. update_option( 'require_name_email', '1' );
  438. $data = array(
  439. 'comment_post_ID' => self::$post->ID,
  440. 'comment' => 'Comment',
  441. 'author' => 'Comment Author',
  442. 'email' => 'not_an_email',
  443. );
  444. $comment = wp_handle_comment_submission( $data );
  445. update_option( 'require_name_email', $_require_name_email );
  446. $this->assertWPError( $comment );
  447. $this->assertSame( $error, $comment->get_error_code() );
  448. }
  449. public function test_submitting_comment_with_no_comment_content_returns_error() {
  450. $error = 'require_valid_comment';
  451. $data = array(
  452. 'comment_post_ID' => self::$post->ID,
  453. 'comment' => '',
  454. 'author' => 'Comment Author',
  455. 'email' => 'comment@example.org',
  456. );
  457. $comment = wp_handle_comment_submission( $data );
  458. $this->assertWPError( $comment );
  459. $this->assertSame( $error, $comment->get_error_code() );
  460. }
  461. /**
  462. * @ticket 10377
  463. */
  464. public function test_submitting_comment_with_content_too_long_returns_error() {
  465. $error = 'comment_content_column_length';
  466. $data = array(
  467. 'comment_post_ID' => self::$post->ID,
  468. 'comment' => rand_long_str( 65536 ),
  469. 'author' => 'Comment Author',
  470. 'email' => 'comment@example.org',
  471. );
  472. $comment = wp_handle_comment_submission( $data );
  473. $this->assertWPError( $comment );
  474. $this->assertSame( $error, $comment->get_error_code() );
  475. }
  476. /**
  477. * @ticket 10377
  478. */
  479. public function test_submitting_comment_with_author_too_long_returns_error() {
  480. $error = 'comment_author_column_length';
  481. $data = array(
  482. 'comment_post_ID' => self::$post->ID,
  483. 'comment' => 'Comment',
  484. 'author' => rand_long_str( 255 ),
  485. 'email' => 'comment@example.org',
  486. );
  487. $comment = wp_handle_comment_submission( $data );
  488. $this->assertWPError( $comment );
  489. $this->assertSame( $error, $comment->get_error_code() );
  490. }
  491. /**
  492. * @ticket 10377
  493. */
  494. public function test_submitting_comment_with_email_too_long_returns_error() {
  495. $error = 'comment_author_email_column_length';
  496. $data = array(
  497. 'comment_post_ID' => self::$post->ID,
  498. 'comment' => 'Comment',
  499. 'author' => 'Comment Author',
  500. 'email' => rand_long_str( 90 ) . '@example.com',
  501. );
  502. $comment = wp_handle_comment_submission( $data );
  503. $this->assertWPError( $comment );
  504. $this->assertSame( $error, $comment->get_error_code() );
  505. }
  506. /**
  507. * @ticket 10377
  508. */
  509. public function test_submitting_comment_with_url_too_long_returns_error() {
  510. $error = 'comment_author_url_column_length';
  511. $data = array(
  512. 'comment_post_ID' => self::$post->ID,
  513. 'comment' => 'Comment',
  514. 'author' => 'Comment Author',
  515. 'email' => 'comment@example.org',
  516. 'url' => rand_long_str( 201 ),
  517. );
  518. $comment = wp_handle_comment_submission( $data );
  519. $this->assertWPError( $comment );
  520. $this->assertSame( $error, $comment->get_error_code() );
  521. }
  522. /**
  523. * @ticket 49236
  524. */
  525. public function test_submitting_comment_with_empty_type_results_in_correct_type() {
  526. $data = array(
  527. 'comment_post_ID' => self::$post->ID,
  528. 'comment' => 'Comment',
  529. 'author' => 'Comment Author',
  530. 'email' => 'comment@example.org',
  531. 'comment_type' => '',
  532. );
  533. $comment = wp_handle_comment_submission( $data );
  534. $this->assertNotWPError( $comment );
  535. $this->assertInstanceOf( 'WP_Comment', $comment );
  536. $this->assertSame( 'comment', $comment->comment_type );
  537. }
  538. /**
  539. * @ticket 49236
  540. */
  541. public function test_inserting_comment_with_empty_type_results_in_correct_type() {
  542. $data = array(
  543. 'comment_post_ID' => self::$post->ID,
  544. 'comment' => 'Comment',
  545. 'author' => 'Comment Author',
  546. 'email' => 'comment@example.org',
  547. 'comment_type' => '',
  548. );
  549. $comment_id = wp_insert_comment( $data );
  550. $comment = get_comment( $comment_id );
  551. $this->assertNotWPError( $comment );
  552. $this->assertInstanceOf( 'WP_Comment', $comment );
  553. $this->assertSame( 'comment', $comment->comment_type );
  554. }
  555. /**
  556. * @ticket 34997
  557. */
  558. public function test_comment_submission_sends_all_expected_parameters_to_preprocess_comment_filter() {
  559. $user = get_userdata( self::$author_id );
  560. wp_set_current_user( $user->ID );
  561. $data = array(
  562. 'comment_post_ID' => self::$post->ID,
  563. 'comment' => 'Comment',
  564. );
  565. add_filter( 'preprocess_comment', array( $this, 'filter_preprocess_comment' ) );
  566. $comment = wp_handle_comment_submission( $data );
  567. remove_filter( 'preprocess_comment', array( $this, 'filter_preprocess_comment' ) );
  568. $this->assertNotWPError( $comment );
  569. $this->assertSame(
  570. array(
  571. 'comment_post_ID' => self::$post->ID,
  572. 'comment_author' => $user->display_name,
  573. 'comment_author_email' => $user->user_email,
  574. 'comment_author_url' => $user->user_url,
  575. 'comment_content' => $data['comment'],
  576. 'comment_type' => 'comment',
  577. 'comment_parent' => 0,
  578. 'user_ID' => $user->ID,
  579. 'user_id' => $user->ID,
  580. 'comment_author_IP' => '127.0.0.1',
  581. 'comment_agent' => '',
  582. ),
  583. $this->preprocess_comment_data
  584. );
  585. }
  586. public function filter_preprocess_comment( $commentdata ) {
  587. $this->preprocess_comment_data = $commentdata;
  588. return $commentdata;
  589. }
  590. /**
  591. * @ticket 36901
  592. */
  593. public function test_submitting_duplicate_comments() {
  594. $data = array(
  595. 'comment_post_ID' => self::$post->ID,
  596. 'comment' => 'Did I say that?',
  597. 'author' => 'Repeat myself',
  598. 'email' => 'mail@example.com',
  599. );
  600. $first_comment = wp_handle_comment_submission( $data );
  601. $second_comment = wp_handle_comment_submission( $data );
  602. $this->assertWPError( $second_comment );
  603. $this->assertSame( 'comment_duplicate', $second_comment->get_error_code() );
  604. }
  605. /**
  606. * @ticket 36901
  607. */
  608. public function test_comments_flood() {
  609. $data = array(
  610. 'comment_post_ID' => self::$post->ID,
  611. 'comment' => 'Did I say that?',
  612. 'author' => 'Repeat myself',
  613. 'email' => 'mail@example.com',
  614. );
  615. $first_comment = wp_handle_comment_submission( $data );
  616. $data['comment'] = 'Wow! I am quick!';
  617. $second_comment = wp_handle_comment_submission( $data );
  618. $this->assertWPError( $second_comment );
  619. $this->assertSame( 'comment_flood', $second_comment->get_error_code() );
  620. }
  621. /**
  622. * @ticket 36901
  623. */
  624. public function test_comments_flood_user_is_admin() {
  625. $user = self::factory()->user->create_and_get(
  626. array(
  627. 'role' => 'administrator',
  628. )
  629. );
  630. wp_set_current_user( $user->ID );
  631. $data = array(
  632. 'comment_post_ID' => self::$post->ID,
  633. 'comment' => 'Did I say that?',
  634. 'author' => 'Repeat myself',
  635. 'email' => 'mail@example.com',
  636. );
  637. $first_comment = wp_handle_comment_submission( $data );
  638. $data['comment'] = 'Wow! I am quick!';
  639. $second_comment = wp_handle_comment_submission( $data );
  640. $this->assertNotWPError( $second_comment );
  641. $this->assertEquals( self::$post->ID, $second_comment->comment_post_ID );
  642. }
  643. }