Ticket #25272: 25272-tests.patch
File 25272-tests.patch, 7.1 KB (added by , 11 years ago) |
---|
-
tests/phpunit/includes/testcase-ajax.php
42 42 'oembed_cache', 'image-editor', 'delete-comment', 'delete-tag', 'delete-link', 43 43 'delete-meta', 'delete-post', 'trash-post', 'untrash-post', 'delete-page', 'dim-comment', 44 44 'add-link-category', 'add-tag', 'get-tagcloud', 'get-comments', 'replyto-comment', 45 'edit-comment', 'add-menu-item', 'add-meta', 'add-user', ' autosave', 'closed-postboxes',45 'edit-comment', 'add-menu-item', 'add-meta', 'add-user', 'closed-postboxes', 46 46 'hidden-columns', 'update-welcome-panel', 'menu-get-metabox', 'wp-link-ajax', 47 47 'menu-locations-save', 'menu-quick-search', 'meta-box-order', 'get-permalink', 48 48 'sample-permalink', 'inline-save', 'inline-save-tax', 'find_posts', 'widgets-order', 49 49 'save-widget', 'set-post-thumbnail', 'date_format', 'time_format', 'wp-fullscreen-save-post', 50 'wp-remove-post-lock', 'dismiss-wp-pointer', ' nopriv_autosave'50 'wp-remove-post-lock', 'dismiss-wp-pointer', 'heartbeat', 'nopriv_heartbeat', 51 51 ); 52 52 53 53 /** -
tests/phpunit/tests/ajax/Autosave.php
23 23 protected $_post = null; 24 24 25 25 /** 26 * user_id 27 * @var int 28 */ 29 protected $user_id = 0; 30 31 /** 26 32 * Set up the test fixture 27 33 */ 28 34 public function setUp() { 29 35 parent::setUp(); 36 // Set a user so the $post has 'post_author' 37 $this->user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); 38 wp_set_current_user( $this->user_id ); 39 30 40 $post_id = $this->factory->post->create( array( 'post_status' => 'draft' ) ); 31 41 $this->_post = get_post( $post_id ); 32 42 } 33 43 34 44 /** 45 * Tear down the test fixture. 46 * Reset the current user 47 */ 48 public function tearDown() { 49 parent::tearDown(); 50 wp_set_current_user( 0 ); 51 } 52 53 /** 35 54 * Test autosaving a post 36 55 * @return void 37 56 */ 38 57 public function test_autosave_post() { 58 // The original post_author 59 wp_set_current_user( $this->user_id ); 39 60 40 // Become an admin41 $this->_setRole( 'administrator' );42 43 61 // Set up the $_POST request 44 62 $md5 = md5( uniqid() ); 45 63 $_POST = array( 46 'post_id' => $this->_post->ID, 47 'autosavenonce' => wp_create_nonce( 'autosave' ), 48 'post_content' => $this->_post->post_content . PHP_EOL . $md5, 49 'post_type' => 'post', 50 'autosave' => 1, 64 'action' => 'heartbeat', 65 '_nonce' => wp_create_nonce( 'heartbeat-nonce' ), 66 'data' => array( 67 'autosave' => array( 68 'post_id' => $this->_post->ID, 69 '_wpnonce' => wp_create_nonce( 'update-post_' . $this->_post->ID ), 70 'post_content' => $this->_post->post_content . PHP_EOL . $md5, 71 'post_type' => 'post', 72 ), 73 ), 51 74 ); 52 75 53 76 // Make the request 54 77 try { 55 $this->_handleAjax( ' autosave' );78 $this->_handleAjax( 'heartbeat' ); 56 79 } catch ( WPAjaxDieContinueException $e ) { 57 80 unset( $e ); 58 81 } 59 82 60 // Get the response 61 $ xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA);83 // Get the response, it is in heartbeat's response 84 $response = json_decode( $this->_last_response, true ); 62 85 63 86 // Ensure everything is correct 64 $this->assert Equals( $this->_post->ID, (int) $xml->response[0]->autosave['id'] );65 $this->assert Equals( 'autosave_' . $this->_post->ID, (string) $xml->response['action']);87 $this->assertNotEmpty( $response['autosave'] ); 88 $this->assertTrue( $response['autosave']['success'] ); 66 89 67 90 // Check that the edit happened 68 $post = get_post( $this->_post->ID );91 $post = get_post( $this->_post->ID ); 69 92 $this->assertGreaterThanOrEqual( 0, strpos( $post->post_content, $md5 ) ); 70 93 } 71 94 72 95 /** 73 * Test with an invalid nonce96 * Test autosaving a locked post 74 97 * @return void 75 98 */ 76 public function test_with_invalid_nonce( ) { 99 public function test_autosave_locked_post() { 100 // Lock the post to another user 101 $another_user_id = $this->factory->user->create( array( 'role' => 'editor' ) ); 102 wp_set_current_user( $another_user_id ); 103 wp_set_post_lock( $this->_post->ID ); 104 105 wp_set_current_user( $this->user_id ); 77 106 78 // Become an administrator79 $this-> _setRole( 'administrator');107 // Ensure post is locked 108 $this->assertEquals( $another_user_id, wp_check_post_lock( $this->_post->ID ) ); 80 109 81 110 // Set up the $_POST request 111 $md5 = md5( uniqid() ); 82 112 $_POST = array( 83 'post_id' => $this->_post->ID, 84 'autosavenonce' => md5( uniqid() ), 85 'autosave' => 1 113 'action' => 'heartbeat', 114 '_nonce' => wp_create_nonce( 'heartbeat-nonce' ), 115 'data' => array( 116 'autosave' => array( 117 'post_id' => $this->_post->ID, 118 '_wpnonce' => wp_create_nonce( 'update-post_' . $this->_post->ID ), 119 'post_content' => $this->_post->post_content . PHP_EOL . $md5, 120 'post_type' => 'post', 121 ), 122 ), 86 123 ); 87 124 88 125 // Make the request 89 $this->setExpectedException( 'WPAjaxDieStopException', '-1' ); 90 $this->_handleAjax( 'autosave' ); 126 try { 127 $this->_handleAjax( 'heartbeat' ); 128 } catch ( WPAjaxDieContinueException $e ) { 129 unset( $e ); 130 } 131 132 $response = json_decode( $this->_last_response, true ); 133 134 // Ensure everything is correct 135 $this->assertNotEmpty( $response['autosave'] ); 136 $this->assertTrue( $response['autosave']['success'] ); 137 138 // Check that the original post was NOT edited 139 $post = get_post( $this->_post->ID ); 140 $this->assertFalse( strpos( $post->post_content, $md5 ) ); 141 142 // Check if the autosave post was created 143 $autosave = wp_get_post_autosave( $this->_post->ID, get_current_user_id() ); 144 $this->assertNotEmpty( $autosave ); 145 $this->assertGreaterThanOrEqual( 0, strpos( $autosave->post_content, $md5 ) ); 91 146 } 92 147 93 148 /** 94 * Test with a bad post id149 * Test with an invalid nonce 95 150 * @return void 96 151 */ 97 public function test_with_invalid_ post_id( ) {152 public function test_with_invalid_nonce( ) { 98 153 99 // Become an administrator 100 $this->_setRole( 'administrator' ); 154 wp_set_current_user( $this->user_id ); 101 155 102 156 // Set up the $_POST request 103 157 $_POST = array( 104 'post_id' => 0, 105 'autosavenonce' => wp_create_nonce( 'autosave' ), 106 'autosave' => 1, 107 'post_type' => 'post' 158 'action' => 'heartbeat', 159 '_nonce' => wp_create_nonce( 'heartbeat-nonce' ), 160 'data' => array( 161 'autosave' => array( 162 'post_id' => $this->_post->ID, 163 '_wpnonce' => substr( md5( uniqid() ), 0, 10 ), 164 ), 165 ), 108 166 ); 109 167 110 168 // Make the request 111 $this->setExpectedException( 'WPAjaxDieStopException', 'You are not allowed to edit this post.' ); 112 $this->_handleAjax( 'autosave' ); 169 try { 170 $this->_handleAjax( 'heartbeat' ); 171 } catch ( WPAjaxDieContinueException $e ) { 172 unset( $e ); 173 } 174 175 $response = json_decode( $this->_last_response, true ); 176 177 $this->assertNotEmpty( $response['autosave'] ); 178 $this->assertFalse( $response['autosave']['success'] ); 113 179 } 114 180 }