| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * @group comment |
| | 5 | */ |
| | 6 | class Tests_Comments_Post 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_posting_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_post( $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_posting_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_post( $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_posting_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_post( $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_posting_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_post( $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_posting_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_post( $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_posting_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_post( $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_posting_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_post( $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_posting_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_post( $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_posting_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_post( $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 | public function test_posting_comment_anonymously_to_private_post_returns_error() { |
| | 211 | |
| | 212 | $error = 'not_logged_in'; |
| | 213 | |
| | 214 | $post = $this->factory->post->create_and_get( array( |
| | 215 | 'post_status' => 'private', |
| | 216 | ) ); |
| | 217 | $data = array( |
| | 218 | 'comment_post_ID' => $post->ID, |
| | 219 | ); |
| | 220 | $comment = wp_handle_comment_post( $data ); |
| | 221 | |
| | 222 | $this->assertFalse( is_user_logged_in() ); |
| | 223 | $this->assertWPError( $comment ); |
| | 224 | $this->assertSame( $error, $comment->get_error_code() ); |
| | 225 | |
| | 226 | } |
| | 227 | |
| | 228 | public function test_posting_comment_to_own_private_post_succeeds() { |
| | 229 | |
| | 230 | $user = $this->factory->user->create_and_get(); |
| | 231 | |
| | 232 | wp_set_current_user( $user->ID ); |
| | 233 | |
| | 234 | $post = $this->factory->post->create_and_get( array( |
| | 235 | 'post_status' => 'private', |
| | 236 | 'post_author' => $user->ID, |
| | 237 | ) ); |
| | 238 | $data = array( |
| | 239 | 'comment_post_ID' => $post->ID, |
| | 240 | 'comment' => 'Comment', |
| | 241 | ); |
| | 242 | $comment = wp_handle_comment_post( $data ); |
| | 243 | |
| | 244 | $this->assertTrue( current_user_can( 'read_post', $post->ID ) ); |
| | 245 | $this->assertNotWPError( $comment ); |
| | 246 | $this->assertInstanceOf( 'WP_Comment', $comment ); |
| | 247 | |
| | 248 | } |
| | 249 | |
| | 250 | public function test_posting_comment_to_accessible_private_post_succeeds() { |
| | 251 | |
| | 252 | $author = $this->factory->user->create_and_get( array( |
| | 253 | 'role' => 'author', |
| | 254 | ) ); |
| | 255 | $user = $this->factory->user->create_and_get( array( |
| | 256 | 'role' => 'editor', |
| | 257 | ) ); |
| | 258 | |
| | 259 | wp_set_current_user( $user->ID ); |
| | 260 | |
| | 261 | $post = $this->factory->post->create_and_get( array( |
| | 262 | 'post_status' => 'private', |
| | 263 | 'post_author' => $author->ID, |
| | 264 | ) ); |
| | 265 | $data = array( |
| | 266 | 'comment_post_ID' => $post->ID, |
| | 267 | 'comment' => 'Comment', |
| | 268 | ); |
| | 269 | $comment = wp_handle_comment_post( $data ); |
| | 270 | |
| | 271 | $this->assertTrue( current_user_can( 'read_post', $post->ID ) ); |
| | 272 | $this->assertNotWPError( $comment ); |
| | 273 | $this->assertInstanceOf( 'WP_Comment', $comment ); |
| | 274 | |
| | 275 | } |
| | 276 | |
| | 277 | public function test_anonymous_user_cannot_comment_unfiltered_html() { |
| | 278 | |
| | 279 | $post = $this->factory->post->create_and_get(); |
| | 280 | $data = array( |
| | 281 | 'comment_post_ID' => $post->ID, |
| | 282 | 'comment' => 'Comment <script>alert(document.cookie);</script>', |
| | 283 | 'author' => 'Comment Author', |
| | 284 | 'email' => 'comment@example.org', |
| | 285 | ); |
| | 286 | $comment = wp_handle_comment_post( $data ); |
| | 287 | |
| | 288 | $this->assertNotWPError( $comment ); |
| | 289 | $this->assertInstanceOf( 'WP_Comment', $comment ); |
| | 290 | $this->assertNotContains( '<script', $comment->comment_content ); |
| | 291 | |
| | 292 | } |
| | 293 | |
| | 294 | public function test_unprivileged_user_cannot_comment_unfiltered_html() { |
| | 295 | |
| | 296 | $user = $this->factory->user->create_and_get( array( |
| | 297 | 'role' => 'author', |
| | 298 | ) ); |
| | 299 | wp_set_current_user( $user->ID ); |
| | 300 | |
| | 301 | $this->assertFalse( current_user_can( 'unfiltered_html' ) ); |
| | 302 | |
| | 303 | $post = $this->factory->post->create_and_get(); |
| | 304 | $data = array( |
| | 305 | 'comment_post_ID' => $post->ID, |
| | 306 | 'comment' => 'Comment <script>alert(document.cookie);</script>', |
| | 307 | ); |
| | 308 | $comment = wp_handle_comment_post( $data ); |
| | 309 | |
| | 310 | $this->assertNotWPError( $comment ); |
| | 311 | $this->assertInstanceOf( 'WP_Comment', $comment ); |
| | 312 | $this->assertNotContains( '<script', $comment->comment_content ); |
| | 313 | |
| | 314 | } |
| | 315 | |
| | 316 | public function test_unprivileged_user_cannot_comment_unfiltered_html_even_with_valid_nonce() { |
| | 317 | |
| | 318 | $user = $this->factory->user->create_and_get( array( |
| | 319 | 'role' => 'author', |
| | 320 | ) ); |
| | 321 | wp_set_current_user( $user->ID ); |
| | 322 | |
| | 323 | $this->assertFalse( current_user_can( 'unfiltered_html' ) ); |
| | 324 | |
| | 325 | $post = $this->factory->post->create_and_get(); |
| | 326 | $action = 'unfiltered-html-comment_' . $post->ID; |
| | 327 | $nonce = wp_create_nonce( $action ); |
| | 328 | |
| | 329 | $this->assertNotFalse( wp_verify_nonce( $nonce, $action ) ); |
| | 330 | |
| | 331 | $data = array( |
| | 332 | 'comment_post_ID' => $post->ID, |
| | 333 | 'comment' => 'Comment <script>alert(document.cookie);</script>', |
| | 334 | '_wp_unfiltered_html_comment' => $nonce, |
| | 335 | ); |
| | 336 | $comment = wp_handle_comment_post( $data ); |
| | 337 | |
| | 338 | $this->assertNotWPError( $comment ); |
| | 339 | $this->assertInstanceOf( 'WP_Comment', $comment ); |
| | 340 | $this->assertNotContains( '<script', $comment->comment_content ); |
| | 341 | |
| | 342 | } |
| | 343 | |
| | 344 | public function test_privileged_user_can_comment_unfiltered_html_with_valid_nonce() { |
| | 345 | |
| | 346 | $this->assertFalse( defined( 'DISALLOW_UNFILTERED_HTML' ) ); |
| | 347 | |
| | 348 | $user = $this->factory->user->create_and_get( array( |
| | 349 | 'role' => 'editor', |
| | 350 | ) ); |
| | 351 | |
| | 352 | if ( is_multisite() ) { |
| | 353 | // In multisite, only Super Admins can post unfiltered HTML |
| | 354 | $this->assertFalse( user_can( $user->ID, 'unfiltered_html' ) ); |
| | 355 | grant_super_admin( $user->ID ); |
| | 356 | } |
| | 357 | |
| | 358 | wp_set_current_user( $user->ID ); |
| | 359 | |
| | 360 | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
| | 361 | |
| | 362 | $post = $this->factory->post->create_and_get(); |
| | 363 | $action = 'unfiltered-html-comment_' . $post->ID; |
| | 364 | $nonce = wp_create_nonce( $action ); |
| | 365 | |
| | 366 | $this->assertNotFalse( wp_verify_nonce( $nonce, $action ) ); |
| | 367 | |
| | 368 | $data = array( |
| | 369 | 'comment_post_ID' => $post->ID, |
| | 370 | 'comment' => 'Comment <script>alert(document.cookie);</script>', |
| | 371 | '_wp_unfiltered_html_comment' => $nonce, |
| | 372 | ); |
| | 373 | $comment = wp_handle_comment_post( $data ); |
| | 374 | |
| | 375 | $this->assertNotWPError( $comment ); |
| | 376 | $this->assertInstanceOf( 'WP_Comment', $comment ); |
| | 377 | $this->assertContains( '<script', $comment->comment_content ); |
| | 378 | |
| | 379 | } |
| | 380 | |
| | 381 | public function test_privileged_user_cannot_comment_unfiltered_html_without_valid_nonce() { |
| | 382 | |
| | 383 | $user = $this->factory->user->create_and_get( array( |
| | 384 | 'role' => 'editor', |
| | 385 | ) ); |
| | 386 | |
| | 387 | if ( is_multisite() ) { |
| | 388 | // In multisite, only Super Admins can post unfiltered HTML |
| | 389 | $this->assertFalse( user_can( $user->ID, 'unfiltered_html' ) ); |
| | 390 | grant_super_admin( $user->ID ); |
| | 391 | } |
| | 392 | |
| | 393 | wp_set_current_user( $user->ID ); |
| | 394 | |
| | 395 | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
| | 396 | |
| | 397 | $post = $this->factory->post->create_and_get(); |
| | 398 | $data = array( |
| | 399 | 'comment_post_ID' => $post->ID, |
| | 400 | 'comment' => 'Comment <script>alert(document.cookie);</script>', |
| | 401 | ); |
| | 402 | $comment = wp_handle_comment_post( $data ); |
| | 403 | |
| | 404 | $this->assertNotWPError( $comment ); |
| | 405 | $this->assertInstanceOf( 'WP_Comment', $comment ); |
| | 406 | $this->assertNotContains( '<script', $comment->comment_content ); |
| | 407 | |
| | 408 | } |
| | 409 | |
| | 410 | public function test_posting_comment_as_anonymous_user_when_registration_required_returns_error() { |
| | 411 | |
| | 412 | $error = 'not_logged_in'; |
| | 413 | |
| | 414 | $_comment_registration = get_option( 'comment_registration' ); |
| | 415 | update_option( 'comment_registration', '1' ); |
| | 416 | |
| | 417 | $post = $this->factory->post->create_and_get(); |
| | 418 | $data = array( |
| | 419 | 'comment_post_ID' => $post->ID, |
| | 420 | ); |
| | 421 | $comment = wp_handle_comment_post( $data ); |
| | 422 | |
| | 423 | update_option( 'comment_registration', $_comment_registration ); |
| | 424 | |
| | 425 | $this->assertWPError( $comment ); |
| | 426 | $this->assertSame( $error, $comment->get_error_code() ); |
| | 427 | |
| | 428 | } |
| | 429 | |
| | 430 | public function test_posting_comment_with_no_name_when_name_email_required_returns_error() { |
| | 431 | |
| | 432 | $error = 'require_name_email'; |
| | 433 | |
| | 434 | $_require_name_email = get_option( 'require_name_email' ); |
| | 435 | update_option( 'require_name_email', '1' ); |
| | 436 | |
| | 437 | $post = $this->factory->post->create_and_get(); |
| | 438 | $data = array( |
| | 439 | 'comment_post_ID' => $post->ID, |
| | 440 | 'comment' => 'Comment', |
| | 441 | 'email' => 'comment@example.org', |
| | 442 | ); |
| | 443 | $comment = wp_handle_comment_post( $data ); |
| | 444 | |
| | 445 | update_option( 'require_name_email', $_require_name_email ); |
| | 446 | |
| | 447 | $this->assertWPError( $comment ); |
| | 448 | $this->assertSame( $error, $comment->get_error_code() ); |
| | 449 | |
| | 450 | } |
| | 451 | |
| | 452 | public function test_posting_comment_with_no_email_when_name_email_required_returns_error() { |
| | 453 | |
| | 454 | $error = 'require_name_email'; |
| | 455 | |
| | 456 | $_require_name_email = get_option( 'require_name_email' ); |
| | 457 | update_option( 'require_name_email', '1' ); |
| | 458 | |
| | 459 | $post = $this->factory->post->create_and_get(); |
| | 460 | $data = array( |
| | 461 | 'comment_post_ID' => $post->ID, |
| | 462 | 'comment' => 'Comment', |
| | 463 | 'author' => 'Comment Author', |
| | 464 | ); |
| | 465 | $comment = wp_handle_comment_post( $data ); |
| | 466 | |
| | 467 | update_option( 'require_name_email', $_require_name_email ); |
| | 468 | |
| | 469 | $this->assertWPError( $comment ); |
| | 470 | $this->assertSame( $error, $comment->get_error_code() ); |
| | 471 | |
| | 472 | } |
| | 473 | |
| | 474 | public function test_posting_comment_with_invalid_email_when_name_email_required_returns_error() { |
| | 475 | |
| | 476 | $error = 'require_valid_email'; |
| | 477 | |
| | 478 | $_require_name_email = get_option( 'require_name_email' ); |
| | 479 | update_option( 'require_name_email', '1' ); |
| | 480 | |
| | 481 | $post = $this->factory->post->create_and_get(); |
| | 482 | $data = array( |
| | 483 | 'comment_post_ID' => $post->ID, |
| | 484 | 'comment' => 'Comment', |
| | 485 | 'author' => 'Comment Author', |
| | 486 | 'email' => 'not_an_email', |
| | 487 | ); |
| | 488 | $comment = wp_handle_comment_post( $data ); |
| | 489 | |
| | 490 | update_option( 'require_name_email', $_require_name_email ); |
| | 491 | |
| | 492 | $this->assertWPError( $comment ); |
| | 493 | $this->assertSame( $error, $comment->get_error_code() ); |
| | 494 | |
| | 495 | } |
| | 496 | |
| | 497 | public function test_posting_comment_with_no_comment_content_returns_error() { |
| | 498 | |
| | 499 | $error = 'require_valid_comment'; |
| | 500 | |
| | 501 | $post = $this->factory->post->create_and_get(); |
| | 502 | $data = array( |
| | 503 | 'comment_post_ID' => $post->ID, |
| | 504 | 'comment' => '', |
| | 505 | 'author' => 'Comment Author', |
| | 506 | 'email' => 'comment@example.org', |
| | 507 | ); |
| | 508 | $comment = wp_handle_comment_post( $data ); |
| | 509 | |
| | 510 | $this->assertWPError( $comment ); |
| | 511 | $this->assertSame( $error, $comment->get_error_code() ); |
| | 512 | |
| | 513 | } |
| | 514 | |
| | 515 | } |