Changeset 26995 for trunk/tests/phpunit/tests/ajax/Autosave.php
- Timestamp:
- 01/22/2014 04:55:37 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/ajax/Autosave.php
r25002 r26995 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 ); 42 } 43 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 ); 32 51 } 33 52 … … 37 56 */ 38 57 public function test_autosave_post() { 39 40 // Become an admin 41 $this->_setRole( 'administrator' ); 58 // The original post_author 59 wp_set_current_user( $this->user_id ); 42 60 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 'wp_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['wp_autosave'] ); 88 $this->assertTrue( $response['wp_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 ) ); 93 } 94 95 /** 96 * Test autosaving a locked post 97 * @return void 98 */ 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 ); 106 107 // Ensure post is locked 108 $this->assertEquals( $another_user_id, wp_check_post_lock( $this->_post->ID ) ); 109 110 // Set up the $_POST request 111 $md5 = md5( uniqid() ); 112 $_POST = array( 113 'action' => 'heartbeat', 114 '_nonce' => wp_create_nonce( 'heartbeat-nonce' ), 115 'data' => array( 116 'wp_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 ), 123 ); 124 125 // Make the request 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['wp_autosave'] ); 136 $this->assertTrue( $response['wp_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 ) ); 70 146 } 71 147 … … 76 152 public function test_with_invalid_nonce( ) { 77 153 78 // Become an administrator 79 $this->_setRole( 'administrator' ); 154 wp_set_current_user( $this->user_id ); 80 155 81 156 // Set up the $_POST request 82 157 $_POST = array( 83 'post_id' => $this->_post->ID, 84 'autosavenonce' => md5( uniqid() ), 85 'autosave' => 1 158 'action' => 'heartbeat', 159 '_nonce' => wp_create_nonce( 'heartbeat-nonce' ), 160 'data' => array( 161 'wp_autosave' => array( 162 'post_id' => $this->_post->ID, 163 '_wpnonce' => substr( md5( uniqid() ), 0, 10 ), 164 ), 165 ), 86 166 ); 87 167 88 168 // Make the request 89 $this->setExpectedException( 'WPAjaxDieStopException', '-1' ); 90 $this->_handleAjax( 'autosave' ); 91 } 169 try { 170 $this->_handleAjax( 'heartbeat' ); 171 } catch ( WPAjaxDieContinueException $e ) { 172 unset( $e ); 173 } 92 174 93 /** 94 * Test with a bad post id 95 * @return void 96 */ 97 public function test_with_invalid_post_id( ) { 175 $response = json_decode( $this->_last_response, true ); 98 176 99 // Become an administrator 100 $this->_setRole( 'administrator' ); 101 102 // Set up the $_POST request 103 $_POST = array( 104 'post_id' => 0, 105 'autosavenonce' => wp_create_nonce( 'autosave' ), 106 'autosave' => 1, 107 'post_type' => 'post' 108 ); 109 110 // Make the request 111 $this->setExpectedException( 'WPAjaxDieStopException', 'You are not allowed to edit this post.' ); 112 $this->_handleAjax( 'autosave' ); 177 $this->assertNotEmpty( $response['wp_autosave'] ); 178 $this->assertFalse( $response['wp_autosave']['success'] ); 113 179 } 114 180 }
Note: See TracChangeset
for help on using the changeset viewer.