WordPress.org

Make WordPress Core

Changeset 512 in tests


Ignore:
Timestamp:
02/02/12 00:49:18 (2 years ago)
Author:
kurtpayne
Message:

Making this a generic ajax test case base class.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-testcase/test_admin_includes_ajax_actions.php

    r506 r512  
    1212 */ 
    1313include_once(DIR_TESTDATA . '/sample_blogs.php'); 
    14  
    15 /** 
    16  * Admin ajax functions to be tested 
    17  */ 
    18 include_once( ABSPATH . 'wp-admin/includes/ajax-actions.php' ); 
    1914 
    2015/** 
     
    3429 * @since      3.4.0 
    3530 */ 
    36 class AjaxTest extends _WPDataset2 { 
     31class WPAjaxTestCase extends _WPDataset2 { 
    3732     
    3833    /** 
     
    119114     * If you're testing ajax and you expect wp_die(<anything>), you must use: 
    120115     * <code> 
    121      * $this->setExpectedException('WPAjaxDieException'); 
    122      * $this->assertEquals(<anything>, $this->_last_message); 
     116     * $this->setExpectedException('WPAjaxDieException', '<anything>'); 
    123117     * </code> 
    124118     * @param string $message  
     
    168162    } 
    169163} 
    170  
    171 /** 
    172  * Testing ajax comment functionality 
    173  * 
    174  * @package    WordPress 
    175  * @subpackage Unit Tests 
    176  * @since      3.4.0 
    177  */ 
    178 class TestAjaxComments extends AjaxTest { 
    179      
    180     /** 
    181      * List of comments 
    182      * @var array 
    183      */ 
    184     protected $_comments = array(); 
    185  
    186     /** 
    187      * Set up the test fixture 
    188      */ 
    189     public function setUp() { 
    190         parent::setUp(); 
    191         $this->_comments = get_comments(array( 
    192             'status'  => 'all', 
    193             'search'  => '', 
    194             'user_id' => '', 
    195             'offset'  => 2, 
    196             'number'  => 20, 
    197             'post_id' => 0, 
    198             'type'    => '', 
    199             'orderby' => '', 
    200             'order'   => '' 
    201         )); 
    202     } 
    203  
    204     /** 
    205      * Delete a comment as an administrator (expects success) 
    206      */ 
    207     public function test_ajax_trash_comment_as_admin() { 
    208  
    209         // Become an administrator 
    210         $this->_setRole( 'administrator' ); 
    211  
    212         // Set up the $_POST request 
    213         $comment              = $this->_comments[0]; 
    214         $_POST['id']          = $comment->comment_ID; 
    215         $_POST['_ajax_nonce'] = wp_create_nonce( 'delete-comment_' . $comment->comment_ID ); 
    216         $_POST['trash']       = 1; 
    217         $_POST['_total']      = wp_count_comments( 0 ); 
    218         $_POST['_per_page']   = 20; 
    219         $_POST['_page']       = 2; 
    220         $_POST['_url']        = admin_url( 'edit-comments.php' ); 
    221          
    222         // Make the request 
    223         $this->_handleAjax( 'delete-comment' ); 
    224  
    225         // Get the response 
    226         $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA ); 
    227          
    228         // Ensure everything is correct 
    229         $this->assertEquals( (string) $xml->response[0]->comment['id'], $comment->comment_ID ); 
    230         $this->assertEquals( (string) $xml->response['action'], 'delete-comment_' . $comment->comment_ID ); 
    231         $this->assertGreaterThanOrEqual( time() - 10, (int) $xml->response[0]->comment[0]->supplemental[0]->time[0] ); 
    232         $this->assertLessThanOrEqual( time(), (int) $xml->response[0]->comment[0]->supplemental[0]->time[0] ); 
    233     } 
    234  
    235     /** 
    236      * Delete a comment as a subscriber (expects permission denied) 
    237      */ 
    238     public function test_ajax_trash_comment_as_subscriber() { 
    239      
    240         // Become a subscriber       
    241         $this->_setRole( 'subscriber' ); 
    242  
    243         // Set up the $_POST request 
    244         $comment              = $this->_comments[1]; 
    245         $_POST['id']          = $comment->comment_ID; 
    246         $_POST['_ajax_nonce'] = wp_create_nonce( 'delete-comment_' . $comment->comment_ID ); 
    247         $_POST['trash']       = 1; 
    248         $_POST['_total']      = wp_count_comments( 0 ); 
    249         $_POST['_per_page']   = 20; 
    250         $_POST['_page']       = 2; 
    251         $_POST['_url']        = admin_url( 'edit-comments.php' ); 
    252          
    253         // Make the request 
    254         $this->setExpectedException( 'WPAjaxDieException' ); 
    255         $this->_handleAjax( 'delete-comment' ); 
    256  
    257         // Get the response 
    258         $this->assertEquals( -1, $this->_last_response ); 
    259     } 
    260  
    261     /** 
    262      * @todo wp_ajax_dim_comment() 
    263      * @todo wp_ajax_get_comments() 
    264      * @todo wp_ajax_replyto_comment() 
    265      * @todo wp_ajax_edit_comment() 
    266      */ 
    267 } 
Note: See TracChangeset for help on using the changeset viewer.