Changeset 47122 for trunk/tests/phpunit/tests/ajax/DeleteComment.php
- Timestamp:
- 01/29/2020 12:43:23 AM (6 years ago)
- File:
-
- 1 edited
-
trunk/tests/phpunit/tests/ajax/DeleteComment.php (modified) (19 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/ajax/DeleteComment.php
r46586 r47122 2 2 3 3 /** 4 * Admin ajax functions to be tested4 * Admin Ajax functions to be tested. 5 5 */ 6 6 require_once( ABSPATH . 'wp-admin/includes/ajax-actions.php' ); 7 7 8 8 /** 9 * Testing ajax comment functionality9 * Testing Ajax comment functionality. 10 10 * 11 11 * @package WordPress … … 63 63 public function _test_as_admin( $comment, $action ) { 64 64 65 // Reset request 66 $this->_clear_post_action(); 67 68 // Become an administrator 65 // Reset request. 66 $this->_clear_post_action(); 67 68 // Become an administrator. 69 69 $this->_setRole( 'administrator' ); 70 70 71 // Set up a default request 71 // Set up a default request. 72 72 $_POST['id'] = $comment->comment_ID; 73 73 $_POST['_ajax_nonce'] = wp_create_nonce( 'delete-comment_' . $comment->comment_ID ); … … 78 78 $_POST['_url'] = admin_url( 'edit-comments.php' ); 79 79 80 // Make the request 80 // Make the request. 81 81 try { 82 82 $this->_handleAjax( 'delete-comment' ); … … 85 85 } 86 86 87 // Get the response 87 // Get the response. 88 88 $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA ); 89 89 90 // Ensure everything is correct 90 // Ensure everything is correct. 91 91 $this->assertEquals( $comment->comment_ID, (string) $xml->response[0]->comment['id'] ); 92 92 $this->assertEquals( 'delete-comment_' . $comment->comment_ID, (string) $xml->response['action'] ); … … 94 94 $this->assertLessThanOrEqual( time(), (int) $xml->response[0]->comment[0]->supplemental[0]->time[0] ); 95 95 96 // trash, spam, delete should make the total go down96 // 'trash', 'spam', 'delete' should make the total go down. 97 97 if ( in_array( $action, array( 'trash', 'spam', 'delete' ), true ) ) { 98 98 $total = $_POST['_total'] - 1; 99 99 100 // unspam, untrash should make the total go up100 // 'unspam', 'untrash' should make the total go up. 101 101 } elseif ( in_array( $action, array( 'untrash', 'unspam' ), true ) ) { 102 102 $total = $_POST['_total'] + 1; 103 103 } 104 104 105 // The total is calculated based on a page break -OR- a random number. Let's look for both possible outcomes105 // The total is calculated based on a page break -OR- a random number. Let's look for both possible outcomes. 106 106 $comment_count = wp_count_comments( 0 ); 107 107 $recalc_total = $comment_count->total_comments; 108 108 109 // Check for either possible total 109 // Check for either possible total. 110 110 $message = sprintf( 'returned value: %1$d $total: %2$d $recalc_total: %3$d', (int) $xml->response[0]->comment[0]->supplemental[0]->total[0], $total, $recalc_total ); 111 111 $this->assertTrue( in_array( (int) $xml->response[0]->comment[0]->supplemental[0]->total[0], array( $total, $recalc_total ), true ), $message ); … … 122 122 public function _test_as_subscriber( $comment, $action ) { 123 123 124 // Reset request 125 $this->_clear_post_action(); 126 127 // Become a subscriber 124 // Reset request. 125 $this->_clear_post_action(); 126 127 // Become a subscriber. 128 128 $this->_setRole( 'subscriber' ); 129 129 130 // Set up the $_POST request 130 // Set up the $_POST request. 131 131 $_POST['id'] = $comment->comment_ID; 132 132 $_POST['_ajax_nonce'] = wp_create_nonce( 'delete-comment_' . $comment->comment_ID ); … … 137 137 $_POST['_url'] = admin_url( 'edit-comments.php' ); 138 138 139 // Make the request 139 // Make the request. 140 140 $this->setExpectedException( 'WPAjaxDieStopException', '-1' ); 141 141 $this->_handleAjax( 'delete-comment' ); … … 153 153 public function _test_with_bad_nonce( $comment, $action ) { 154 154 155 // Reset request 156 $this->_clear_post_action(); 157 158 // Become a subscriber 155 // Reset request. 156 $this->_clear_post_action(); 157 158 // Become a subscriber. 159 159 $this->_setRole( 'administrator' ); 160 160 161 // Set up the $_POST request 161 // Set up the $_POST request. 162 162 $_POST['id'] = $comment->comment_ID; 163 163 $_POST['_ajax_nonce'] = wp_create_nonce( uniqid() ); … … 168 168 $_POST['_url'] = admin_url( 'edit-comments.php' ); 169 169 170 // Make the request 170 // Make the request. 171 171 $this->setExpectedException( 'WPAjaxDieStopException', '-1' ); 172 172 $this->_handleAjax( 'delete-comment' ); … … 183 183 public function _test_with_bad_id( $comment, $action ) { 184 184 185 // Reset request 186 $this->_clear_post_action(); 187 188 // Become a subscriber 185 // Reset request. 186 $this->_clear_post_action(); 187 188 // Become a subscriber. 189 189 $this->_setRole( 'administrator' ); 190 190 191 // Set up the $_POST request 191 // Set up the $_POST request. 192 192 $_POST['id'] = 12346789; 193 193 $_POST['_ajax_nonce'] = wp_create_nonce( 'delete-comment_12346789' ); … … 198 198 $_POST['_url'] = admin_url( 'edit-comments.php' ); 199 199 200 // Make the request, look for a timestamp in the exception 200 // Make the request, look for a timestamp in the exception. 201 201 try { 202 202 $this->_handleAjax( 'delete-comment' ); … … 220 220 public function _test_double_action( $comment, $action ) { 221 221 222 // Reset request 223 $this->_clear_post_action(); 224 225 // Become a subscriber 222 // Reset request. 223 $this->_clear_post_action(); 224 225 // Become a subscriber. 226 226 $this->_setRole( 'administrator' ); 227 227 228 // Set up the $_POST request 228 // Set up the $_POST request. 229 229 $_POST['id'] = $comment->comment_ID; 230 230 $_POST['_ajax_nonce'] = wp_create_nonce( 'delete-comment_' . $comment->comment_ID ); … … 235 235 $_POST['_url'] = admin_url( 'edit-comments.php' ); 236 236 237 // Make the request 237 // Make the request. 238 238 try { 239 239 $this->_handleAjax( 'delete-comment' ); … … 243 243 $this->_last_response = ''; 244 244 245 // Force delete the comment 245 // Force delete the comment. 246 246 if ( 'delete' === $action ) { 247 247 wp_delete_comment( $comment->comment_ID, true ); 248 248 } 249 249 250 // Make the request again, look for a timestamp in the exception 250 // Make the request again, look for a timestamp in the exception. 251 251 try { 252 252 $this->_handleAjax( 'delete-comment' ); … … 266 266 */ 267 267 public function test_ajax_comment_trash_actions_as_administrator() { 268 // Test trash/untrash 268 // Test trash/untrash. 269 269 $this->_test_as_admin( self::$comments[0], 'trash' ); 270 270 $this->_test_as_admin( self::$comments[0], 'untrash' ); 271 271 272 // Test spam/unspam 272 // Test spam/unspam. 273 273 $this->_test_as_admin( self::$comments[1], 'spam' ); 274 274 $this->_test_as_admin( self::$comments[1], 'unspam' ); 275 275 276 // Test delete 276 // Test delete. 277 277 $this->_test_as_admin( self::$comments[2], 'delete' ); 278 278 } … … 284 284 */ 285 285 public function test_ajax_comment_trash_actions_as_subscriber() { 286 // Test trash/untrash 286 // Test trash/untrash. 287 287 $this->_test_as_subscriber( self::$comments[0], 'trash' ); 288 288 $this->_test_as_subscriber( self::$comments[0], 'untrash' ); 289 289 290 // Test spam/unspam 290 // Test spam/unspam. 291 291 $this->_test_as_subscriber( self::$comments[1], 'spam' ); 292 292 $this->_test_as_subscriber( self::$comments[1], 'unspam' ); 293 293 294 // Test delete 294 // Test delete. 295 295 $this->_test_as_subscriber( self::$comments[2], 'delete' ); 296 296 } … … 302 302 */ 303 303 public function test_ajax_trash_comment_no_id() { 304 // Test trash/untrash 304 // Test trash/untrash. 305 305 $this->_test_as_admin( self::$comments[0], 'trash' ); 306 306 $this->_test_as_admin( self::$comments[0], 'untrash' ); 307 307 308 // Test spam/unspam 308 // Test spam/unspam. 309 309 $this->_test_as_admin( self::$comments[1], 'spam' ); 310 310 $this->_test_as_admin( self::$comments[1], 'unspam' ); 311 311 312 // Test delete 312 // Test delete. 313 313 $this->_test_as_admin( self::$comments[2], 'delete' ); 314 314 } … … 320 320 */ 321 321 public function test_ajax_trash_comment_bad_nonce() { 322 // Test trash/untrash 322 // Test trash/untrash. 323 323 $this->_test_with_bad_nonce( self::$comments[0], 'trash' ); 324 324 $this->_test_with_bad_nonce( self::$comments[0], 'untrash' ); 325 325 326 // Test spam/unspam 326 // Test spam/unspam. 327 327 $this->_test_with_bad_nonce( self::$comments[1], 'spam' ); 328 328 $this->_test_with_bad_nonce( self::$comments[1], 'unspam' ); 329 329 330 // Test delete 330 // Test delete. 331 331 $this->_test_with_bad_nonce( self::$comments[2], 'delete' ); 332 332 } … … 338 338 */ 339 339 public function test_ajax_trash_double_action() { 340 // Test trash/untrash 340 // Test trash/untrash. 341 341 $this->_test_double_action( self::$comments[0], 'trash' ); 342 342 $this->_test_double_action( self::$comments[0], 'untrash' ); 343 343 344 // Test spam/unspam 344 // Test spam/unspam. 345 345 $this->_test_double_action( self::$comments[1], 'spam' ); 346 346 $this->_test_double_action( self::$comments[1], 'unspam' ); 347 347 348 // Test delete 348 // Test delete. 349 349 $this->_test_double_action( self::$comments[2], 'delete' ); 350 350 }
Note: See TracChangeset
for help on using the changeset viewer.