| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group comment |
| 5 | */ |
| 6 | class Tests_Comment_Submission extends WP_UnitTestCase { |
| 7 | |
| 8 | function setUp() { |
| 9 | parent::setUp(); |
| 10 | require_once ABSPATH . WPINC . '/class-phpass.php'; |
| 11 | } |
| 12 | |
| 13 | function tearDown() { |
| 14 | wp_set_current_user( 0 ); |
| 15 | parent::tearDown(); |
| 16 | } |
| 17 | |
| 18 | public function test_submitting_comment_to_invalid_post_returns_error() { |
| 19 | |
| 20 | $error = 'comment_id_not_found'; |
| 21 | |
| 22 | $this->assertSame( 0, did_action( $error ) ); |
| 23 | |
| 24 | $data = array( |
| 25 | 'comment_post_ID' => 0, |
| 26 | ); |
| 27 | $comment = wp_handle_comment_submission( $data ); |
| 28 | |
| 29 | $this->assertSame( 1, did_action( $error ) ); |
| 30 | $this->assertWPError( $comment ); |
| 31 | $this->assertSame( $error, $comment->get_error_code() ); |
| 32 | |
| 33 | } |
| 34 | |
| 35 | public function test_submitting_comment_to_post_with_closed_comments_returns_error() { |
| 36 | |
| 37 | $error = 'comment_closed'; |
| 38 | |
| 39 | $this->assertSame( 0, did_action( $error ) ); |
| 40 | |
| 41 | $post = $this->factory->post->create_and_get( array( |
| 42 | 'comment_status' => 'closed', |
| 43 | ) ); |
| 44 | $data = array( |
| 45 | 'comment_post_ID' => $post->ID, |
| 46 | ); |
| 47 | $comment = wp_handle_comment_submission( $data ); |
| 48 | |
| 49 | $this->assertSame( 1, did_action( $error ) ); |
| 50 | $this->assertWPError( $comment ); |
| 51 | $this->assertSame( $error, $comment->get_error_code() ); |
| 52 | |
| 53 | } |
| 54 | |
| 55 | public function test_submitting_comment_to_trashed_post_returns_error() { |
| 56 | |
| 57 | $error = 'comment_on_trash'; |
| 58 | |
| 59 | $this->assertSame( 0, did_action( $error ) ); |
| 60 | |
| 61 | $post = $this->factory->post->create_and_get(); |
| 62 | wp_trash_post( $post ); |
| 63 | $data = array( |
| 64 | 'comment_post_ID' => $post->ID, |
| 65 | ); |
| 66 | $comment = wp_handle_comment_submission( $data ); |
| 67 | |
| 68 | $this->assertSame( 1, did_action( $error ) ); |
| 69 | $this->assertWPError( $comment ); |
| 70 | $this->assertSame( $error, $comment->get_error_code() ); |
| 71 | |
| 72 | } |
| 73 | |
| 74 | public function test_submitting_comment_to_draft_post_returns_error() { |
| 75 | |
| 76 | $error = 'comment_on_draft'; |
| 77 | |
| 78 | $this->assertSame( 0, did_action( $error ) ); |
| 79 | |
| 80 | $post = $this->factory->post->create_and_get( array( |
| 81 | 'post_status' => 'draft', |
| 82 | ) ); |
| 83 | $data = array( |
| 84 | 'comment_post_ID' => $post->ID, |
| 85 | ); |
| 86 | $comment = wp_handle_comment_submission( $data ); |
| 87 | |
| 88 | $this->assertSame( 1, did_action( $error ) ); |
| 89 | $this->assertWPError( $comment ); |
| 90 | $this->assertSame( $error, $comment->get_error_code() ); |
| 91 | |
| 92 | } |
| 93 | |
| 94 | public function test_submitting_comment_to_scheduled_post_returns_error() { |
| 95 | |
| 96 | // Same error as commenting on a draft |
| 97 | $error = 'comment_on_draft'; |
| 98 | |
| 99 | $this->assertSame( 0, did_action( $error ) ); |
| 100 | |
| 101 | $post = $this->factory->post->create_and_get( array( |
| 102 | 'post_date' => date( 'Y-m-d H:i:s', strtotime( '+1 day' ) ), |
| 103 | ) ); |
| 104 | |
| 105 | $this->assertSame( 'future', $post->post_status ); |
| 106 | |
| 107 | $data = array( |
| 108 | 'comment_post_ID' => $post->ID, |
| 109 | ); |
| 110 | $comment = wp_handle_comment_submission( $data ); |
| 111 | |
| 112 | $this->assertSame( 1, did_action( $error ) ); |
| 113 | $this->assertWPError( $comment ); |
| 114 | $this->assertSame( $error, $comment->get_error_code() ); |
| 115 | |
| 116 | } |
| 117 | |
| 118 | public function test_submitting_comment_to_password_required_post_returns_error() { |
| 119 | |
| 120 | $error = 'comment_on_password_protected'; |
| 121 | |
| 122 | $this->assertSame( 0, did_action( $error ) ); |
| 123 | |
| 124 | $post = $this->factory->post->create_and_get( array( |
| 125 | 'post_password' => 'password', |
| 126 | ) ); |
| 127 | $data = array( |
| 128 | 'comment_post_ID' => $post->ID, |
| 129 | ); |
| 130 | $comment = wp_handle_comment_submission( $data ); |
| 131 | |
| 132 | $this->assertSame( 1, did_action( $error ) ); |
| 133 | $this->assertWPError( $comment ); |
| 134 | $this->assertSame( $error, $comment->get_error_code() ); |
| 135 | |
| 136 | } |
| 137 | |
| 138 | public function test_submitting_comment_to_password_protected_post_succeeds() { |
| 139 | |
| 140 | $password = 'password'; |
| 141 | $hasher = new PasswordHash( 8, true ); |
| 142 | |
| 143 | $_COOKIE['wp-postpass_' . COOKIEHASH] = $hasher->HashPassword( $password ); |
| 144 | |
| 145 | $post = $this->factory->post->create_and_get( array( |
| 146 | 'post_password' => $password, |
| 147 | ) ); |
| 148 | $data = array( |
| 149 | 'comment_post_ID' => $post->ID, |
| 150 | 'comment' => 'Comment', |
| 151 | 'author' => 'Comment Author', |
| 152 | 'email' => 'comment@example.org', |
| 153 | ); |
| 154 | $comment = wp_handle_comment_submission( $data ); |
| 155 | |
| 156 | unset( $_COOKIE['wp-postpass_' . COOKIEHASH] ); |
| 157 | |
| 158 | $this->assertNotWPError( $comment ); |
| 159 | $this->assertInstanceOf( 'WP_Comment', $comment ); |
| 160 | |
| 161 | } |
| 162 | |
| 163 | public function test_submitting_valid_comment_as_logged_in_user_succeeds() { |
| 164 | |
| 165 | $user = $this->factory->user->create_and_get( array( |
| 166 | 'user_url' => 'http://user.example.org' |
| 167 | ) ); |
| 168 | |
| 169 | wp_set_current_user( $user->ID ); |
| 170 | |
| 171 | $post = $this->factory->post->create_and_get(); |
| 172 | $data = array( |
| 173 | 'comment_post_ID' => $post->ID, |
| 174 | 'comment' => 'Comment', |
| 175 | ); |
| 176 | $comment = wp_handle_comment_submission( $data ); |
| 177 | |
| 178 | $this->assertNotWPError( $comment ); |
| 179 | $this->assertInstanceOf( 'WP_Comment', $comment ); |
| 180 | |
| 181 | $this->assertSame( 'Comment', $comment->comment_content); |
| 182 | $this->assertSame( $user->display_name, $comment->comment_author ); |
| 183 | $this->assertSame( $user->user_email, $comment->comment_author_email ); |
| 184 | $this->assertSame( $user->user_url, $comment->comment_author_url ); |
| 185 | |
| 186 | } |
| 187 | |
| 188 | public function test_submitting_valid_comment_anonymously_succeeds() { |
| 189 | |
| 190 | $post = $this->factory->post->create_and_get(); |
| 191 | $data = array( |
| 192 | 'comment_post_ID' => $post->ID, |
| 193 | 'comment' => 'Comment', |
| 194 | 'author' => 'Comment Author', |
| 195 | 'email' => 'comment@example.org', |
| 196 | 'url' => 'user.example.org' |
| 197 | ); |
| 198 | $comment = wp_handle_comment_submission( $data ); |
| 199 | |
| 200 | $this->assertNotWPError( $comment ); |
| 201 | $this->assertInstanceOf( 'WP_Comment', $comment ); |
| 202 | |
| 203 | $this->assertSame( 'Comment', $comment->comment_content); |
| 204 | $this->assertSame( 'Comment Author', $comment->comment_author ); |
| 205 | $this->assertSame( 'comment@example.org', $comment->comment_author_email ); |
| 206 | $this->assertSame( 'http://user.example.org', $comment->comment_author_url ); |
| 207 | |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * wp_handle_comment_submission() expects un-slashed data. |
| 212 | * |
| 213 | * @group slashes |
| 214 | */ |
| 215 | public function test_submitting_comment_handles_slashes_correctly_handles_slashes() { |
| 216 | |
| 217 | $post = $this->factory->post->create_and_get(); |
| 218 | $data = array( |
| 219 | 'comment_post_ID' => $post->ID, |
| 220 | 'comment' => 'Comment with 1 slash: \\', |
| 221 | 'author' => 'Comment Author with 1 slash: \\', |
| 222 | 'email' => 'comment@example.org', |
| 223 | ); |
| 224 | $comment = wp_handle_comment_submission( $data ); |
| 225 | |
| 226 | $this->assertNotWPError( $comment ); |
| 227 | $this->assertInstanceOf( 'WP_Comment', $comment ); |
| 228 | |
| 229 | $this->assertSame( 'Comment with 1 slash: \\', $comment->comment_content); |
| 230 | $this->assertSame( 'Comment Author with 1 slash: \\', $comment->comment_author ); |
| 231 | $this->assertSame( 'comment@example.org', $comment->comment_author_email ); |
| 232 | |
| 233 | } |
| 234 | |
| 235 | public function test_submitting_comment_anonymously_to_private_post_returns_error() { |
| 236 | |
| 237 | $error = 'not_logged_in'; |
| 238 | |
| 239 | $post = $this->factory->post->create_and_get( array( |
| 240 | 'post_status' => 'private', |
| 241 | ) ); |
| 242 | $data = array( |
| 243 | 'comment_post_ID' => $post->ID, |
| 244 | ); |
| 245 | $comment = wp_handle_comment_submission( $data ); |
| 246 | |
| 247 | $this->assertFalse( is_user_logged_in() ); |
| 248 | $this->assertWPError( $comment ); |
| 249 | $this->assertSame( $error, $comment->get_error_code() ); |
| 250 | |
| 251 | } |
| 252 | |
| 253 | public function test_submitting_comment_to_own_private_post_succeeds() { |
| 254 | |
| 255 | $user = $this->factory->user->create_and_get(); |
| 256 | |
| 257 | wp_set_current_user( $user->ID ); |
| 258 | |
| 259 | $post = $this->factory->post->create_and_get( array( |
| 260 | 'post_status' => 'private', |
| 261 | 'post_author' => $user->ID, |
| 262 | ) ); |
| 263 | $data = array( |
| 264 | 'comment_post_ID' => $post->ID, |
| 265 | 'comment' => 'Comment', |
| 266 | ); |
| 267 | $comment = wp_handle_comment_submission( $data ); |
| 268 | |
| 269 | $this->assertTrue( current_user_can( 'read_post', $post->ID ) ); |
| 270 | $this->assertNotWPError( $comment ); |
| 271 | $this->assertInstanceOf( 'WP_Comment', $comment ); |
| 272 | |
| 273 | } |
| 274 | |
| 275 | public function test_submitting_comment_to_accessible_private_post_succeeds() { |
| 276 | |
| 277 | $author = $this->factory->user->create_and_get( array( |
| 278 | 'role' => 'author', |
| 279 | ) ); |
| 280 | $user = $this->factory->user->create_and_get( array( |
| 281 | 'role' => 'editor', |
| 282 | ) ); |
| 283 | |
| 284 | wp_set_current_user( $user->ID ); |
| 285 | |
| 286 | $post = $this->factory->post->create_and_get( array( |
| 287 | 'post_status' => 'private', |
| 288 | 'post_author' => $author->ID, |
| 289 | ) ); |
| 290 | $data = array( |
| 291 | 'comment_post_ID' => $post->ID, |
| 292 | 'comment' => 'Comment', |
| 293 | ); |
| 294 | $comment = wp_handle_comment_submission( $data ); |
| 295 | |
| 296 | $this->assertTrue( current_user_can( 'read_post', $post->ID ) ); |
| 297 | $this->assertNotWPError( $comment ); |
| 298 | $this->assertInstanceOf( 'WP_Comment', $comment ); |
| 299 | |
| 300 | } |
| 301 | |
| 302 | public function test_anonymous_user_cannot_comment_unfiltered_html() { |
| 303 | |
| 304 | $post = $this->factory->post->create_and_get(); |
| 305 | $data = array( |
| 306 | 'comment_post_ID' => $post->ID, |
| 307 | 'comment' => 'Comment <script>alert(document.cookie);</script>', |
| 308 | 'author' => 'Comment Author', |
| 309 | 'email' => 'comment@example.org', |
| 310 | ); |
| 311 | $comment = wp_handle_comment_submission( $data ); |
| 312 | |
| 313 | $this->assertNotWPError( $comment ); |
| 314 | $this->assertInstanceOf( 'WP_Comment', $comment ); |
| 315 | $this->assertNotContains( '<script', $comment->comment_content ); |
| 316 | |
| 317 | } |
| 318 | |
| 319 | public function test_unprivileged_user_cannot_comment_unfiltered_html() { |
| 320 | |
| 321 | $user = $this->factory->user->create_and_get( array( |
| 322 | 'role' => 'author', |
| 323 | ) ); |
| 324 | wp_set_current_user( $user->ID ); |
| 325 | |
| 326 | $this->assertFalse( current_user_can( 'unfiltered_html' ) ); |
| 327 | |
| 328 | $post = $this->factory->post->create_and_get(); |
| 329 | $data = array( |
| 330 | 'comment_post_ID' => $post->ID, |
| 331 | 'comment' => 'Comment <script>alert(document.cookie);</script>', |
| 332 | ); |
| 333 | $comment = wp_handle_comment_submission( $data ); |
| 334 | |
| 335 | $this->assertNotWPError( $comment ); |
| 336 | $this->assertInstanceOf( 'WP_Comment', $comment ); |
| 337 | $this->assertNotContains( '<script', $comment->comment_content ); |
| 338 | |
| 339 | } |
| 340 | |
| 341 | public function test_unprivileged_user_cannot_comment_unfiltered_html_even_with_valid_nonce() { |
| 342 | |
| 343 | $user = $this->factory->user->create_and_get( array( |
| 344 | 'role' => 'author', |
| 345 | ) ); |
| 346 | wp_set_current_user( $user->ID ); |
| 347 | |
| 348 | $this->assertFalse( current_user_can( 'unfiltered_html' ) ); |
| 349 | |
| 350 | $post = $this->factory->post->create_and_get(); |
| 351 | $action = 'unfiltered-html-comment_' . $post->ID; |
| 352 | $nonce = wp_create_nonce( $action ); |
| 353 | |
| 354 | $this->assertNotFalse( wp_verify_nonce( $nonce, $action ) ); |
| 355 | |
| 356 | $data = array( |
| 357 | 'comment_post_ID' => $post->ID, |
| 358 | 'comment' => 'Comment <script>alert(document.cookie);</script>', |
| 359 | '_wp_unfiltered_html_comment' => $nonce, |
| 360 | ); |
| 361 | $comment = wp_handle_comment_submission( $data ); |
| 362 | |
| 363 | $this->assertNotWPError( $comment ); |
| 364 | $this->assertInstanceOf( 'WP_Comment', $comment ); |
| 365 | $this->assertNotContains( '<script', $comment->comment_content ); |
| 366 | |
| 367 | } |
| 368 | |
| 369 | public function test_privileged_user_can_comment_unfiltered_html_with_valid_nonce() { |
| 370 | |
| 371 | $this->assertFalse( defined( 'DISALLOW_UNFILTERED_HTML' ) ); |
| 372 | |
| 373 | $user = $this->factory->user->create_and_get( array( |
| 374 | 'role' => 'editor', |
| 375 | ) ); |
| 376 | |
| 377 | if ( is_multisite() ) { |
| 378 | // In multisite, only Super Admins can post unfiltered HTML |
| 379 | $this->assertFalse( user_can( $user->ID, 'unfiltered_html' ) ); |
| 380 | grant_super_admin( $user->ID ); |
| 381 | } |
| 382 | |
| 383 | wp_set_current_user( $user->ID ); |
| 384 | |
| 385 | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
| 386 | |
| 387 | $post = $this->factory->post->create_and_get(); |
| 388 | $action = 'unfiltered-html-comment_' . $post->ID; |
| 389 | $nonce = wp_create_nonce( $action ); |
| 390 | |
| 391 | $this->assertNotFalse( wp_verify_nonce( $nonce, $action ) ); |
| 392 | |
| 393 | $data = array( |
| 394 | 'comment_post_ID' => $post->ID, |
| 395 | 'comment' => 'Comment <script>alert(document.cookie);</script>', |
| 396 | '_wp_unfiltered_html_comment' => $nonce, |
| 397 | ); |
| 398 | $comment = wp_handle_comment_submission( $data ); |
| 399 | |
| 400 | $this->assertNotWPError( $comment ); |
| 401 | $this->assertInstanceOf( 'WP_Comment', $comment ); |
| 402 | $this->assertContains( '<script', $comment->comment_content ); |
| 403 | |
| 404 | } |
| 405 | |
| 406 | public function test_privileged_user_cannot_comment_unfiltered_html_without_valid_nonce() { |
| 407 | |
| 408 | $user = $this->factory->user->create_and_get( array( |
| 409 | 'role' => 'editor', |
| 410 | ) ); |
| 411 | |
| 412 | if ( is_multisite() ) { |
| 413 | // In multisite, only Super Admins can post unfiltered HTML |
| 414 | $this->assertFalse( user_can( $user->ID, 'unfiltered_html' ) ); |
| 415 | grant_super_admin( $user->ID ); |
| 416 | } |
| 417 | |
| 418 | wp_set_current_user( $user->ID ); |
| 419 | |
| 420 | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
| 421 | |
| 422 | $post = $this->factory->post->create_and_get(); |
| 423 | $data = array( |
| 424 | 'comment_post_ID' => $post->ID, |
| 425 | 'comment' => 'Comment <script>alert(document.cookie);</script>', |
| 426 | ); |
| 427 | $comment = wp_handle_comment_submission( $data ); |
| 428 | |
| 429 | $this->assertNotWPError( $comment ); |
| 430 | $this->assertInstanceOf( 'WP_Comment', $comment ); |
| 431 | $this->assertNotContains( '<script', $comment->comment_content ); |
| 432 | |
| 433 | } |
| 434 | |
| 435 | public function test_submitting_comment_as_anonymous_user_when_registration_required_returns_error() { |
| 436 | |
| 437 | $error = 'not_logged_in'; |
| 438 | |
| 439 | $_comment_registration = get_option( 'comment_registration' ); |
| 440 | update_option( 'comment_registration', '1' ); |
| 441 | |
| 442 | $post = $this->factory->post->create_and_get(); |
| 443 | $data = array( |
| 444 | 'comment_post_ID' => $post->ID, |
| 445 | ); |
| 446 | $comment = wp_handle_comment_submission( $data ); |
| 447 | |
| 448 | update_option( 'comment_registration', $_comment_registration ); |
| 449 | |
| 450 | $this->assertWPError( $comment ); |
| 451 | $this->assertSame( $error, $comment->get_error_code() ); |
| 452 | |
| 453 | } |
| 454 | |
| 455 | public function test_submitting_comment_with_no_name_when_name_email_required_returns_error() { |
| 456 | |
| 457 | $error = 'require_name_email'; |
| 458 | |
| 459 | $_require_name_email = get_option( 'require_name_email' ); |
| 460 | update_option( 'require_name_email', '1' ); |
| 461 | |
| 462 | $post = $this->factory->post->create_and_get(); |
| 463 | $data = array( |
| 464 | 'comment_post_ID' => $post->ID, |
| 465 | 'comment' => 'Comment', |
| 466 | 'email' => 'comment@example.org', |
| 467 | ); |
| 468 | $comment = wp_handle_comment_submission( $data ); |
| 469 | |
| 470 | update_option( 'require_name_email', $_require_name_email ); |
| 471 | |
| 472 | $this->assertWPError( $comment ); |
| 473 | $this->assertSame( $error, $comment->get_error_code() ); |
| 474 | |
| 475 | } |
| 476 | |
| 477 | public function test_submitting_comment_with_no_email_when_name_email_required_returns_error() { |
| 478 | |
| 479 | $error = 'require_name_email'; |
| 480 | |
| 481 | $_require_name_email = get_option( 'require_name_email' ); |
| 482 | update_option( 'require_name_email', '1' ); |
| 483 | |
| 484 | $post = $this->factory->post->create_and_get(); |
| 485 | $data = array( |
| 486 | 'comment_post_ID' => $post->ID, |
| 487 | 'comment' => 'Comment', |
| 488 | 'author' => 'Comment Author', |
| 489 | ); |
| 490 | $comment = wp_handle_comment_submission( $data ); |
| 491 | |
| 492 | update_option( 'require_name_email', $_require_name_email ); |
| 493 | |
| 494 | $this->assertWPError( $comment ); |
| 495 | $this->assertSame( $error, $comment->get_error_code() ); |
| 496 | |
| 497 | } |
| 498 | |
| 499 | public function test_submitting_comment_with_invalid_email_when_name_email_required_returns_error() { |
| 500 | |
| 501 | $error = 'require_valid_email'; |
| 502 | |
| 503 | $_require_name_email = get_option( 'require_name_email' ); |
| 504 | update_option( 'require_name_email', '1' ); |
| 505 | |
| 506 | $post = $this->factory->post->create_and_get(); |
| 507 | $data = array( |
| 508 | 'comment_post_ID' => $post->ID, |
| 509 | 'comment' => 'Comment', |
| 510 | 'author' => 'Comment Author', |
| 511 | 'email' => 'not_an_email', |
| 512 | ); |
| 513 | $comment = wp_handle_comment_submission( $data ); |
| 514 | |
| 515 | update_option( 'require_name_email', $_require_name_email ); |
| 516 | |
| 517 | $this->assertWPError( $comment ); |
| 518 | $this->assertSame( $error, $comment->get_error_code() ); |
| 519 | |
| 520 | } |
| 521 | |
| 522 | public function test_submitting_comment_with_no_comment_content_returns_error() { |
| 523 | |
| 524 | $error = 'require_valid_comment'; |
| 525 | |
| 526 | $post = $this->factory->post->create_and_get(); |
| 527 | $data = array( |
| 528 | 'comment_post_ID' => $post->ID, |
| 529 | 'comment' => '', |
| 530 | 'author' => 'Comment Author', |
| 531 | 'email' => 'comment@example.org', |
| 532 | ); |
| 533 | $comment = wp_handle_comment_submission( $data ); |
| 534 | |
| 535 | $this->assertWPError( $comment ); |
| 536 | $this->assertSame( $error, $comment->get_error_code() ); |
| 537 | |
| 538 | } |
| 539 | |
| 540 | } |