| | 2366 | public function test_rest_autosave_published_post() { |
| | 2367 | wp_set_current_user( self::$editor_id ); |
| | 2368 | |
| | 2369 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 2370 | $request->add_header( 'content-type', 'application/json' ); |
| | 2371 | |
| | 2372 | $autosave_data = $this->set_post_data( |
| | 2373 | array( |
| | 2374 | 'id' => self::$post_id, |
| | 2375 | 'is_autosave' => true, |
| | 2376 | 'content' => 'Updated post content', |
| | 2377 | ) |
| | 2378 | ); |
| | 2379 | |
| | 2380 | $request->set_body( wp_json_encode( $autosave_data ) ); |
| | 2381 | $response = $this->server->dispatch( $request ); |
| | 2382 | |
| | 2383 | $this->check_update_post_response( $response ); |
| | 2384 | $new_data = $response->get_data(); |
| | 2385 | |
| | 2386 | // The published post shouldn't change. |
| | 2387 | $current_post = get_post( self::$post_id ); |
| | 2388 | $this->assertEquals( $current_post->ID, $new_data['id'] ); |
| | 2389 | $this->assertEquals( $current_post->post_title, $new_data['title']['raw'] ); |
| | 2390 | $this->assertEquals( $current_post->post_content, $new_data['content']['raw'] ); |
| | 2391 | $this->assertEquals( $current_post->post_excerpt, $new_data['excerpt']['raw'] ); |
| | 2392 | |
| | 2393 | $autosave_post = wp_get_post_autosave( self::$post_id ); |
| | 2394 | $this->assertEquals( $autosave_data['title'], $autosave_post->post_title ); |
| | 2395 | $this->assertEquals( $autosave_data['content'], $autosave_post->post_content ); |
| | 2396 | $this->assertEquals( $autosave_data['excerpt'], $autosave_post->post_excerpt ); |
| | 2397 | } |
| | 2398 | |
| | 2399 | public function test_rest_autosave_draft_post_same_author() { |
| | 2400 | wp_set_current_user( self::$editor_id ); |
| | 2401 | |
| | 2402 | $post_data = array( |
| | 2403 | 'post_content' => 'Test post content', |
| | 2404 | 'post_title' => 'Test post title', |
| | 2405 | 'post_excerpt' => 'Test post excerpt', |
| | 2406 | ); |
| | 2407 | $post_id = wp_insert_post( $post_data ); |
| | 2408 | |
| | 2409 | $autosave_data = array( |
| | 2410 | 'id' => $post_id, |
| | 2411 | 'is_autosave' => true, |
| | 2412 | 'content' => 'Updated post content', |
| | 2413 | ); |
| | 2414 | |
| | 2415 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) ); |
| | 2416 | $request->add_header( 'content-type', 'application/json' ); |
| | 2417 | $request->set_body( wp_json_encode( $autosave_data ) ); |
| | 2418 | $response = $this->server->dispatch( $request ); |
| | 2419 | |
| | 2420 | $this->check_update_post_response( $response ); |
| | 2421 | $new_data = $response->get_data(); |
| | 2422 | |
| | 2423 | // The draft post should be updated. |
| | 2424 | $this->assertEquals( $post_id, $new_data['id'] ); |
| | 2425 | $this->assertEquals( $autosave_data['content'], $new_data['content']['raw'] ); |
| | 2426 | |
| | 2427 | $post = get_post( $post_id ); |
| | 2428 | $this->assertEquals( $post_data['post_title'], $post->post_title ); |
| | 2429 | $this->assertEquals( $autosave_data['content'], $post->post_content ); |
| | 2430 | $this->assertEquals( $post_data['post_excerpt'], $post->post_excerpt ); |
| | 2431 | |
| | 2432 | wp_delete_post( $post_id ); |
| | 2433 | } |
| | 2434 | |
| | 2435 | public function test_rest_autosave_draft_post_different_author() { |
| | 2436 | wp_set_current_user( self::$editor_id ); |
| | 2437 | |
| | 2438 | $post_data = array( |
| | 2439 | 'post_content' => 'Test post content', |
| | 2440 | 'post_title' => 'Test post title', |
| | 2441 | 'post_excerpt' => 'Test post excerpt', |
| | 2442 | 'post_author' => ++self::$editor_id, |
| | 2443 | ); |
| | 2444 | $post_id = wp_insert_post( $post_data ); |
| | 2445 | |
| | 2446 | $autosave_data = array( |
| | 2447 | 'id' => $post_id, |
| | 2448 | 'is_autosave' => true, |
| | 2449 | 'content' => 'Updated post content', |
| | 2450 | ); |
| | 2451 | |
| | 2452 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) ); |
| | 2453 | $request->add_header( 'content-type', 'application/json' ); |
| | 2454 | $request->set_body( wp_json_encode( $autosave_data ) ); |
| | 2455 | $response = $this->server->dispatch( $request ); |
| | 2456 | |
| | 2457 | $this->check_update_post_response( $response ); |
| | 2458 | $new_data = $response->get_data(); |
| | 2459 | |
| | 2460 | |
| | 2461 | // The draft post shouldn't change. |
| | 2462 | $current_post = get_post( $post_id ); |
| | 2463 | $this->assertEquals( $current_post->ID, $new_data['id'] ); |
| | 2464 | $this->assertEquals( $current_post->post_title, $new_data['title']['raw'] ); |
| | 2465 | $this->assertEquals( $current_post->post_content, $new_data['content']['raw'] ); |
| | 2466 | $this->assertEquals( $current_post->post_excerpt, $new_data['excerpt']['raw'] ); |
| | 2467 | |
| | 2468 | $autosave_post = wp_get_post_autosave( $post_id ); |
| | 2469 | // No changes |
| | 2470 | $this->assertEquals( $current_post->post_title, $autosave_post->post_title ); |
| | 2471 | $this->assertEquals( $current_post->post_excerpt, $autosave_post->post_excerpt ); |
| | 2472 | |
| | 2473 | // Has changes |
| | 2474 | $this->assertEquals( $autosave_data['content'], $autosave_post->post_content ); |
| | 2475 | |
| | 2476 | wp_delete_post( $post_id ); |
| | 2477 | } |
| | 2478 | |