| 133 | register_meta( |
| 134 | 'post', 'test_boolean_update', array( |
| 135 | 'single' => true, |
| 136 | 'type' => 'boolean', |
| 137 | 'sanitize_callback' => 'absint', |
| 138 | 'show_in_rest' => true, |
| 139 | ) |
| 140 | ); |
| 141 | |
| 142 | register_meta( |
| 143 | 'post', 'test_textured_text_update', array( |
| 144 | 'single' => true, |
| 145 | 'type' => 'string', |
| 146 | 'sanitize_callback' => 'sanitize_text_field', |
| 147 | 'show_in_rest' => true, |
| 148 | ) |
| 149 | ); |
| 150 | |
| 151 | register_meta( |
| 152 | 'post', 'test_json_encoded', array( |
| 153 | 'single' => true, |
| 154 | 'type' => 'string', |
| 155 | 'show_in_rest' => true, |
| 156 | ) |
| 157 | ); |
| 158 | |
| 159 | register_meta( |
| 160 | 'post', 'test\'slashed\'key', array( |
| 161 | 'single' => true, |
| 162 | 'type' => 'string', |
| 163 | 'show_in_rest' => true, |
| 164 | ) |
| 165 | ); |
| 166 | |
| 1198 | /** |
| 1199 | * @ticket 42069 |
| 1200 | * @dataProvider data_update_value_return_success_with_same_value |
| 1201 | */ |
| 1202 | public function test_update_value_return_success_with_same_value( $meta_key, $meta_value ) { |
| 1203 | add_post_meta( self::$post_id, $meta_key, $meta_value ); |
| 1204 | |
| 1205 | $this->grant_write_permission(); |
| 1206 | |
| 1207 | $data = array( |
| 1208 | 'meta' => array( |
| 1209 | $meta_key => $meta_value, |
| 1210 | ), |
| 1211 | ); |
| 1212 | |
| 1213 | $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| 1214 | $request->set_body_params( $data ); |
| 1215 | |
| 1216 | $response = rest_get_server()->dispatch( $request ); |
| 1217 | |
| 1218 | $this->assertEquals( 200, $response->get_status() ); |
| 1219 | } |
| 1220 | |
| 1221 | public function data_update_value_return_success_with_same_value() { |
| 1222 | return array( |
| 1223 | array( 'test_boolean_update', false ), |
| 1224 | array( 'test_boolean_update', true ), |
| 1225 | array( 'test_textured_text_update', 'She said, "What about the > 10,000 penguins in the kitchen?"' ), |
| 1226 | array( 'test_textured_text_update', "He's about to do something rash..." ), |
| 1227 | array( 'test_json_encoded', json_encode( array( 'foo' => 'bar' ) ) ), |
| 1228 | array( 'test\'slashed\'key', 'Hello' ), |
| 1229 | ); |
| 1230 | } |
| 1231 | |
| 1232 | public function test_slashed_meta_key() { |
| 1233 | |
| 1234 | add_post_meta( self::$post_id, 'test\'slashed\'key', 'Hello' ); |
| 1235 | |
| 1236 | $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| 1237 | |
| 1238 | $response = rest_get_server()->dispatch( $request ); |
| 1239 | $data = $response->get_data(); |
| 1240 | |
| 1241 | $this->assertArrayHasKey( 'test\'slashed\'key', $data['meta'] ); |
| 1242 | $this->assertEquals( 'Hello', $data['meta']['test\'slashed\'key'] ); |
| 1243 | } |
| 1244 | |