@@ -1364,6 +1364,18 @@ public function test_create_item_with_terms() {
13641364 * @covers WP_REST_Posts_Controller::handle_featured_media()
13651365 */
13661366 public function test_create_update_post_with_featured_media () {
1367+ $ bad_post_id = self ::factory ()->post ->create ();
1368+ wp_delete_post ( $ bad_post_id , true );
1369+ $ file = DIR_TESTDATA . '/images/canola.jpg ' ;
1370+ $ attachment_id = self ::factory ()->attachment ->create_object (
1371+ $ file ,
1372+ 0 ,
1373+ array (
1374+ 'post_mime_type ' => 'image/jpeg ' ,
1375+ 'menu_order ' => rand ( 1 , 100 ),
1376+ )
1377+ );
1378+
13671379 // Add support for thumbnails on all attachment types to avoid incorrect-usage notice.
13681380 add_post_type_support ( 'attachment ' , 'thumbnail ' );
13691381
@@ -1382,28 +1394,24 @@ public function test_create_update_post_with_featured_media() {
13821394 );
13831395 $ request ->set_header ( 'Content-MD5 ' , md5_file ( self ::$ test_file ) );
13841396
1385- $ file = DIR_TESTDATA . '/images/canola.jpg ' ;
1386- $ attachment_id = self ::factory ()->attachment ->create_object (
1387- $ file ,
1388- 0 ,
1389- array (
1390- 'post_mime_type ' => 'image/jpeg ' ,
1391- 'menu_order ' => rand ( 1 , 100 ),
1392- )
1393- );
1397+ // Create media with bad featured media.
1398+ $ request ->set_param ( 'featured_media ' , $ bad_post_id );
1399+ $ response = rest_get_server ()->dispatch ( $ request );
1400+ $ this ->assertSame ( 400 , $ response ->get_status () );
13941401
1402+ // Create media with good featured media.
1403+ if ( ! file_exists ( self ::$ test_file ) ) {
1404+ copy ( DIR_TESTDATA . '/images/canola.jpg ' , self ::$ test_file );
1405+ }
13951406 $ request ->set_param ( 'featured_media ' , $ attachment_id );
1396-
13971407 $ response = rest_get_server ()->dispatch ( $ request );
13981408 $ data = $ response ->get_data ();
1399-
14001409 $ this ->assertSame ( 201 , $ response ->get_status () );
1401-
14021410 $ new_attachment = get_post ( $ data ['id ' ] );
1403-
14041411 $ this ->assertSame ( $ attachment_id , get_post_thumbnail_id ( $ new_attachment ->ID ) );
14051412 $ this ->assertSame ( $ attachment_id , $ data ['featured_media ' ] );
14061413
1414+ // Try deleting.
14071415 $ request = new WP_REST_Request ( 'PUT ' , '/wp/v2/media/ ' . $ new_attachment ->ID );
14081416 $ params = $ this ->set_post_data (
14091417 array (
@@ -1417,6 +1425,21 @@ public function test_create_update_post_with_featured_media() {
14171425 $ this ->assertSame ( 0 , $ data ['featured_media ' ] );
14181426 $ this ->assertSame ( 0 , get_post_thumbnail_id ( $ new_attachment ->ID ) );
14191427
1428+ // Try setting it back.
1429+ $ request = new WP_REST_Request ( 'PUT ' , '/wp/v2/media/ ' . $ new_attachment ->ID );
1430+ $ params = $ this ->set_post_data (
1431+ array (
1432+ 'featured_media ' => $ attachment_id ,
1433+ )
1434+ );
1435+ $ request ->set_body_params ( $ params );
1436+ $ response = rest_get_server ()->dispatch ( $ request );
1437+ $ this ->assertSame ( 200 , $ response ->get_status (), $ response ->is_error () ? 'Error: ' . $ response ->as_error ()->get_error_message () : '' );
1438+ $ data = $ response ->get_data ();
1439+ $ this ->assertSame ( $ attachment_id , $ data ['featured_media ' ] );
1440+ $ this ->assertSame ( $ attachment_id , get_post_thumbnail_id ( $ new_attachment ->ID ) );
1441+
1442+ // Try setting it again (for idempotency).
14201443 $ request = new WP_REST_Request ( 'PUT ' , '/wp/v2/media/ ' . $ new_attachment ->ID );
14211444 $ params = $ this ->set_post_data (
14221445 array (
@@ -1429,6 +1452,18 @@ public function test_create_update_post_with_featured_media() {
14291452 $ data = $ response ->get_data ();
14301453 $ this ->assertSame ( $ attachment_id , $ data ['featured_media ' ] );
14311454 $ this ->assertSame ( $ attachment_id , get_post_thumbnail_id ( $ new_attachment ->ID ) );
1455+
1456+ // Try updating it to something incorrect.
1457+ $ request = new WP_REST_Request ( 'PUT ' , '/wp/v2/media/ ' . $ new_attachment ->ID );
1458+ $ params = $ this ->set_post_data (
1459+ array (
1460+ 'featured_media ' => $ bad_post_id ,
1461+ )
1462+ );
1463+ $ request ->set_body_params ( $ params );
1464+ $ response = rest_get_server ()->dispatch ( $ request );
1465+ $ this ->assertSame ( 400 , $ response ->get_status () );
1466+ $ this ->assertSame ( $ attachment_id , get_post_thumbnail_id ( $ new_attachment ->ID ) );
14321467 }
14331468
14341469 public function test_update_item () {
0 commit comments