| | 195 | register_meta( |
| | 196 | 'post', 'test_boolean_update', array( |
| | 197 | 'single' => true, |
| | 198 | 'type' => 'boolean', |
| | 199 | 'sanitize_callback' => 'absint', |
| | 200 | 'show_in_rest' => true, |
| | 201 | ) |
| | 202 | ); |
| | 203 | |
| | 204 | register_meta( |
| | 205 | 'post', 'test_textured_text_update', array( |
| | 206 | 'single' => true, |
| | 207 | 'type' => 'string', |
| | 208 | 'sanitize_callback' => 'sanitize_text_field', |
| | 209 | 'show_in_rest' => true, |
| | 210 | ) |
| | 211 | ); |
| | 212 | |
| | 213 | register_meta( |
| | 214 | 'post', 'test_json_encoded', array( |
| | 215 | 'single' => true, |
| | 216 | 'type' => 'string', |
| | 217 | 'show_in_rest' => true, |
| | 218 | ) |
| | 219 | ); |
| | 220 | |
| | 221 | register_meta( |
| | 222 | 'post', 'test\'slashed\'key', array( |
| | 223 | 'single' => true, |
| | 224 | 'type' => 'string', |
| | 225 | 'show_in_rest' => true, |
| | 226 | ) |
| | 227 | ); |
| | 228 | |
| | 1292 | /** |
| | 1293 | * @ticket 42069 |
| | 1294 | * @dataProvider data_update_value_return_success_with_same_value |
| | 1295 | */ |
| | 1296 | public function test_update_value_return_success_with_same_value( $meta_key, $meta_value ) { |
| | 1297 | add_post_meta( self::$post_id, $meta_key, $meta_value ); |
| | 1298 | |
| | 1299 | $this->grant_write_permission(); |
| | 1300 | |
| | 1301 | $data = array( |
| | 1302 | 'meta' => array( |
| | 1303 | $meta_key => $meta_value, |
| | 1304 | ), |
| | 1305 | ); |
| | 1306 | |
| | 1307 | $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1308 | $request->set_body_params( $data ); |
| | 1309 | |
| | 1310 | $response = rest_get_server()->dispatch( $request ); |
| | 1311 | |
| | 1312 | $this->assertEquals( 200, $response->get_status() ); |
| | 1313 | } |
| | 1314 | |
| | 1315 | public function data_update_value_return_success_with_same_value() { |
| | 1316 | return array( |
| | 1317 | array( 'test_boolean_update', false ), |
| | 1318 | array( 'test_boolean_update', true ), |
| | 1319 | array( 'test_textured_text_update', 'She said, "What about the > 10,000 penguins in the kitchen?"' ), |
| | 1320 | array( 'test_textured_text_update', "He's about to do something rash..." ), |
| | 1321 | array( 'test_json_encoded', json_encode( array( 'foo' => 'bar' ) ) ), |
| | 1322 | array( 'test\'slashed\'key', 'Hello' ), |
| | 1323 | ); |
| | 1324 | } |
| | 1325 | |
| | 1326 | public function test_slashed_meta_key() { |
| | 1327 | |
| | 1328 | add_post_meta( self::$post_id, 'test\'slashed\'key', 'Hello' ); |
| | 1329 | |
| | 1330 | $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1331 | |
| | 1332 | $response = rest_get_server()->dispatch( $request ); |
| | 1333 | $data = $response->get_data(); |
| | 1334 | |
| | 1335 | $this->assertArrayHasKey( 'test\'slashed\'key', $data['meta'] ); |
| | 1336 | $this->assertEquals( 'Hello', $data['meta']['test\'slashed\'key'] ); |
| | 1337 | } |
| | 1338 | |