| | 2267 | /** |
| | 2268 | * @ticket 48264 |
| | 2269 | */ |
| | 2270 | public function test_update_array_of_ints_meta() { |
| | 2271 | $this->grant_write_permission(); |
| | 2272 | register_post_meta( |
| | 2273 | 'post', |
| | 2274 | 'items', |
| | 2275 | array( |
| | 2276 | 'single' => true, |
| | 2277 | 'type' => 'array', |
| | 2278 | 'show_in_rest' => array( |
| | 2279 | 'schema' => array( |
| | 2280 | 'items' => array( |
| | 2281 | 'type' => 'integer', |
| | 2282 | ), |
| | 2283 | ), |
| | 2284 | ), |
| | 2285 | ) |
| | 2286 | ); |
| | 2287 | |
| | 2288 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 2289 | $request->set_body_params( |
| | 2290 | array( |
| | 2291 | 'meta' => array( |
| | 2292 | 'items' => array( 1, 2, 3 ), |
| | 2293 | ), |
| | 2294 | ) |
| | 2295 | ); |
| | 2296 | |
| | 2297 | rest_get_server()->dispatch( $request ); |
| | 2298 | $response = rest_get_server()->dispatch( $request ); |
| | 2299 | $this->assertEquals( 200, $response->get_status() ); |
| | 2300 | } |
| | 2301 | |
| | 2302 | /** |
| | 2303 | * @ticket 48264 |
| | 2304 | */ |
| | 2305 | public function test_update_array_of_ints_meta_stored_strings_are_updated() { |
| | 2306 | $this->grant_write_permission(); |
| | 2307 | register_post_meta( |
| | 2308 | 'post', |
| | 2309 | 'items', |
| | 2310 | array( |
| | 2311 | 'single' => true, |
| | 2312 | 'type' => 'array', |
| | 2313 | 'show_in_rest' => array( |
| | 2314 | 'schema' => array( |
| | 2315 | 'items' => array( |
| | 2316 | 'type' => 'integer', |
| | 2317 | ), |
| | 2318 | ), |
| | 2319 | ), |
| | 2320 | ) |
| | 2321 | ); |
| | 2322 | |
| | 2323 | update_post_meta( self::$post_id, 'items', array( '1', '2', '3' ) ); |
| | 2324 | |
| | 2325 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 2326 | $request->set_body_params( |
| | 2327 | array( |
| | 2328 | 'meta' => array( |
| | 2329 | 'items' => array( 1, 2, 3 ), |
| | 2330 | ), |
| | 2331 | ) |
| | 2332 | ); |
| | 2333 | |
| | 2334 | $response = rest_get_server()->dispatch( $request ); |
| | 2335 | $this->assertEquals( 200, $response->get_status() ); |
| | 2336 | $this->assertSame( array( 1, 2, 3 ), get_post_meta( self::$post_id, 'items', true ) ); |
| | 2337 | } |
| | 2338 | |
| | 2339 | /** |
| | 2340 | * @ticket 48264 |
| | 2341 | */ |
| | 2342 | public function test_update_array_of_ints_meta_string_request_data_is_set_as_ints() { |
| | 2343 | $this->grant_write_permission(); |
| | 2344 | register_post_meta( |
| | 2345 | 'post', |
| | 2346 | 'items', |
| | 2347 | array( |
| | 2348 | 'single' => true, |
| | 2349 | 'type' => 'array', |
| | 2350 | 'show_in_rest' => array( |
| | 2351 | 'schema' => array( |
| | 2352 | 'items' => array( |
| | 2353 | 'type' => 'integer', |
| | 2354 | ), |
| | 2355 | ), |
| | 2356 | ), |
| | 2357 | ) |
| | 2358 | ); |
| | 2359 | |
| | 2360 | update_post_meta( self::$post_id, 'items', array( 1, 2, 3 ) ); |
| | 2361 | |
| | 2362 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 2363 | $request->set_body_params( |
| | 2364 | array( |
| | 2365 | 'meta' => array( |
| | 2366 | 'items' => array( '1', '2', '3' ), |
| | 2367 | ), |
| | 2368 | ) |
| | 2369 | ); |
| | 2370 | |
| | 2371 | $response = rest_get_server()->dispatch( $request ); |
| | 2372 | $this->assertEquals( 200, $response->get_status() ); |
| | 2373 | $this->assertSame( array( 1, 2, 3 ), get_post_meta( self::$post_id, 'items', true ) ); |
| | 2374 | } |
| | 2375 | |
| | 2376 | /** |
| | 2377 | * @ticket 48264 |
| | 2378 | */ |
| | 2379 | public function test_update_array_of_ints_meta_string_request_data_and_string_stored_data() { |
| | 2380 | $this->grant_write_permission(); |
| | 2381 | register_post_meta( |
| | 2382 | 'post', |
| | 2383 | 'items', |
| | 2384 | array( |
| | 2385 | 'single' => true, |
| | 2386 | 'type' => 'array', |
| | 2387 | 'show_in_rest' => array( |
| | 2388 | 'schema' => array( |
| | 2389 | 'items' => array( |
| | 2390 | 'type' => 'integer', |
| | 2391 | ), |
| | 2392 | ), |
| | 2393 | ), |
| | 2394 | ) |
| | 2395 | ); |
| | 2396 | |
| | 2397 | update_post_meta( self::$post_id, 'items', array( '1', '2', '3' ) ); |
| | 2398 | |
| | 2399 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 2400 | $request->set_body_params( |
| | 2401 | array( |
| | 2402 | 'meta' => array( |
| | 2403 | 'items' => array( '1', '2', '3' ), |
| | 2404 | ), |
| | 2405 | ) |
| | 2406 | ); |
| | 2407 | |
| | 2408 | $response = rest_get_server()->dispatch( $request ); |
| | 2409 | $this->assertEquals( 200, $response->get_status() ); |
| | 2410 | $this->assertSame( array( 1, 2, 3 ), get_post_meta( self::$post_id, 'items', true ) ); |
| | 2411 | } |
| | 2412 | |
| | 2413 | /** |
| | 2414 | * @ticket 48264 |
| | 2415 | */ |
| | 2416 | public function test_update_array_of_bools_meta() { |
| | 2417 | $this->grant_write_permission(); |
| | 2418 | register_post_meta( |
| | 2419 | 'post', |
| | 2420 | 'items', |
| | 2421 | array( |
| | 2422 | 'single' => true, |
| | 2423 | 'type' => 'array', |
| | 2424 | 'show_in_rest' => array( |
| | 2425 | 'schema' => array( |
| | 2426 | 'items' => array( |
| | 2427 | 'type' => 'boolean', |
| | 2428 | ), |
| | 2429 | ), |
| | 2430 | ), |
| | 2431 | ) |
| | 2432 | ); |
| | 2433 | |
| | 2434 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 2435 | $request->set_body_params( |
| | 2436 | array( |
| | 2437 | 'meta' => array( |
| | 2438 | 'items' => array( true, false ), |
| | 2439 | ), |
| | 2440 | ) |
| | 2441 | ); |
| | 2442 | |
| | 2443 | rest_get_server()->dispatch( $request ); |
| | 2444 | $response = rest_get_server()->dispatch( $request ); |
| | 2445 | $this->assertEquals( 200, $response->get_status() ); |
| | 2446 | } |
| | 2447 | |
| | 2448 | /** |
| | 2449 | * @ticket 48264 |
| | 2450 | */ |
| | 2451 | public function test_update_array_of_bools_meta_stored_strings_are_updated() { |
| | 2452 | $this->grant_write_permission(); |
| | 2453 | register_post_meta( |
| | 2454 | 'post', |
| | 2455 | 'items', |
| | 2456 | array( |
| | 2457 | 'single' => true, |
| | 2458 | 'type' => 'array', |
| | 2459 | 'show_in_rest' => array( |
| | 2460 | 'schema' => array( |
| | 2461 | 'items' => array( |
| | 2462 | 'type' => 'boolean', |
| | 2463 | ), |
| | 2464 | ), |
| | 2465 | ), |
| | 2466 | ) |
| | 2467 | ); |
| | 2468 | |
| | 2469 | update_post_meta( self::$post_id, 'items', array( '1', '0' ) ); |
| | 2470 | |
| | 2471 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 2472 | $request->set_body_params( |
| | 2473 | array( |
| | 2474 | 'meta' => array( |
| | 2475 | 'items' => array( true, false ), |
| | 2476 | ), |
| | 2477 | ) |
| | 2478 | ); |
| | 2479 | |
| | 2480 | $response = rest_get_server()->dispatch( $request ); |
| | 2481 | $this->assertEquals( 200, $response->get_status() ); |
| | 2482 | $this->assertSame( array( true, false ), get_post_meta( self::$post_id, 'items', true ) ); |
| | 2483 | } |
| | 2484 | |
| | 2485 | /** |
| | 2486 | * @ticket 48264 |
| | 2487 | */ |
| | 2488 | public function test_update_array_of_bools_meta_string_request_data_is_set_as_bools() { |
| | 2489 | $this->grant_write_permission(); |
| | 2490 | register_post_meta( |
| | 2491 | 'post', |
| | 2492 | 'items', |
| | 2493 | array( |
| | 2494 | 'single' => true, |
| | 2495 | 'type' => 'array', |
| | 2496 | 'show_in_rest' => array( |
| | 2497 | 'schema' => array( |
| | 2498 | 'items' => array( |
| | 2499 | 'type' => 'boolean', |
| | 2500 | ), |
| | 2501 | ), |
| | 2502 | ), |
| | 2503 | ) |
| | 2504 | ); |
| | 2505 | |
| | 2506 | update_post_meta( self::$post_id, 'items', array( true, false ) ); |
| | 2507 | |
| | 2508 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 2509 | $request->set_body_params( |
| | 2510 | array( |
| | 2511 | 'meta' => array( |
| | 2512 | 'items' => array( '1', '0' ), |
| | 2513 | ), |
| | 2514 | ) |
| | 2515 | ); |
| | 2516 | |
| | 2517 | $response = rest_get_server()->dispatch( $request ); |
| | 2518 | $this->assertEquals( 200, $response->get_status() ); |
| | 2519 | $this->assertSame( array( true, false ), get_post_meta( self::$post_id, 'items', true ) ); |
| | 2520 | } |
| | 2521 | |
| | 2522 | /** |
| | 2523 | * @ticket 48264 |
| | 2524 | */ |
| | 2525 | public function test_update_array_of_bools_meta_string_request_data_and_string_stored_data() { |
| | 2526 | $this->grant_write_permission(); |
| | 2527 | register_post_meta( |
| | 2528 | 'post', |
| | 2529 | 'items', |
| | 2530 | array( |
| | 2531 | 'single' => true, |
| | 2532 | 'type' => 'array', |
| | 2533 | 'show_in_rest' => array( |
| | 2534 | 'schema' => array( |
| | 2535 | 'items' => array( |
| | 2536 | 'type' => 'boolean', |
| | 2537 | ), |
| | 2538 | ), |
| | 2539 | ), |
| | 2540 | ) |
| | 2541 | ); |
| | 2542 | |
| | 2543 | update_post_meta( self::$post_id, 'items', array( '1', '0' ) ); |
| | 2544 | |
| | 2545 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 2546 | $request->set_body_params( |
| | 2547 | array( |
| | 2548 | 'meta' => array( |
| | 2549 | 'items' => array( '1', '0' ), |
| | 2550 | ), |
| | 2551 | ) |
| | 2552 | ); |
| | 2553 | |
| | 2554 | $response = rest_get_server()->dispatch( $request ); |
| | 2555 | $this->assertEquals( 200, $response->get_status() ); |
| | 2556 | $this->assertSame( array( true, false ), get_post_meta( self::$post_id, 'items', true ) ); |
| | 2557 | } |
| | 2558 | |