Skip to content

Commit e4e15d1

Browse files
Tests: Correct additional_field_get_callback() parameters in some REST API tests.
The second parameter passed to `get_callback` in `WP_REST_Controller::add_additional_fields_to_object()` is the field name, not the request details. Includes moving the `get_callback` and `update_callback` helper functions next to the tests they are used in. Follow-up to [38832], [43768]. See #56793. git-svn-id: https://develop.svn.wordpress.org/trunk@55102 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a9bd771 commit e4e15d1

8 files changed

+48
-48
lines changed

tests/phpunit/tests/rest-api/rest-attachments-controller.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,16 @@ public function test_additional_field_update_errors() {
16781678
$wp_rest_additional_fields = array();
16791679
}
16801680

1681+
public function additional_field_get_callback( $object, $field_name ) {
1682+
return 123;
1683+
}
1684+
1685+
public function additional_field_update_callback( $value, $attachment ) {
1686+
if ( 'returnError' === $value ) {
1687+
return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
1688+
}
1689+
}
1690+
16811691
public function test_search_item_by_filename() {
16821692
$id1 = self::factory()->attachment->create_object(
16831693
self::$test_file,
@@ -1706,16 +1716,6 @@ public function test_search_item_by_filename() {
17061716
$this->assertSame( 'image/png', $data[0]['mime_type'] );
17071717
}
17081718

1709-
public function additional_field_get_callback( $object, $request ) {
1710-
return 123;
1711-
}
1712-
1713-
public function additional_field_update_callback( $value, $attachment ) {
1714-
if ( 'returnError' === $value ) {
1715-
return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
1716-
}
1717-
}
1718-
17191719
public function test_links_exist() {
17201720

17211721
wp_set_current_user( self::$editor_id );

tests/phpunit/tests/rest-api/rest-autosaves-controller.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,12 @@ public function test_get_additional_field_registration() {
514514
$wp_rest_additional_fields = array();
515515
}
516516

517-
public function additional_field_get_callback( $object ) {
518-
return get_post_meta( $object['id'], 'my_custom_int', true );
517+
public function additional_field_get_callback( $object, $field_name ) {
518+
return get_post_meta( $object['id'], $field_name, true );
519519
}
520520

521-
public function additional_field_update_callback( $value, $post ) {
522-
update_post_meta( $post->ID, 'my_custom_int', $value );
521+
public function additional_field_update_callback( $value, $post, $field_name ) {
522+
update_post_meta( $post->ID, $field_name, $value );
523523
}
524524

525525
protected function check_get_autosave_response( $response, $autosave ) {

tests/phpunit/tests/rest-api/rest-categories-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ public function test_get_additional_field_registration() {
11801180
$wp_rest_additional_fields = array();
11811181
}
11821182

1183-
public function additional_field_get_callback( $object, $request ) {
1183+
public function additional_field_get_callback( $object, $field_name ) {
11841184
return 123;
11851185
}
11861186

tests/phpunit/tests/rest-api/rest-comments-controller.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3298,15 +3298,15 @@ public function test_additional_field_update_errors() {
32983298
$wp_rest_additional_fields = array();
32993299
}
33003300

3301-
public function additional_field_get_callback( $object ) {
3302-
return get_comment_meta( $object['id'], 'my_custom_int', true );
3301+
public function additional_field_get_callback( $object, $field_name ) {
3302+
return get_comment_meta( $object['id'], $field_name, true );
33033303
}
33043304

3305-
public function additional_field_update_callback( $value, $comment ) {
3305+
public function additional_field_update_callback( $value, $comment, $field_name ) {
33063306
if ( 'returnError' === $value ) {
33073307
return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
33083308
}
3309-
update_comment_meta( $comment->comment_ID, 'my_custom_int', $value );
3309+
update_comment_meta( $comment->comment_ID, $field_name, $value );
33103310
}
33113311

33123312
protected function check_comment_data( $data, $context, $links ) {

tests/phpunit/tests/rest-api/rest-posts-controller.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4586,15 +4586,15 @@ public function test_additional_field_update_errors() {
45864586
$wp_rest_additional_fields = array();
45874587
}
45884588

4589-
public function additional_field_get_callback( $object ) {
4590-
return get_post_meta( $object['id'], 'my_custom_int', true );
4589+
public function additional_field_get_callback( $object, $field_name ) {
4590+
return get_post_meta( $object['id'], $field_name, true );
45914591
}
45924592

4593-
public function additional_field_update_callback( $value, $post ) {
4593+
public function additional_field_update_callback( $value, $post, $field_name ) {
45944594
if ( 'returnError' === $value ) {
45954595
return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
45964596
}
4597-
update_post_meta( $post->ID, 'my_custom_int', $value );
4597+
update_post_meta( $post->ID, $field_name, $value );
45984598
}
45994599

46004600
public function test_publish_action_ldo_registered() {

tests/phpunit/tests/rest-api/rest-revisions-controller.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,12 @@ public function test_get_additional_field_registration() {
402402
$wp_rest_additional_fields = array();
403403
}
404404

405-
public function additional_field_get_callback( $object ) {
406-
return get_post_meta( $object['id'], 'my_custom_int', true );
405+
public function additional_field_get_callback( $object, $field_name ) {
406+
return get_post_meta( $object['id'], $field_name, true );
407407
}
408408

409-
public function additional_field_update_callback( $value, $post ) {
410-
update_post_meta( $post->ID, 'my_custom_int', $value );
409+
public function additional_field_update_callback( $value, $post, $field_name ) {
410+
update_post_meta( $post->ID, $field_name, $value );
411411
}
412412

413413
protected function check_get_revision_response( $response, $revision ) {

tests/phpunit/tests/rest-api/rest-tags-controller.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,16 @@ public function test_additional_field_update_errors() {
13241324
$wp_rest_additional_fields = array();
13251325
}
13261326

1327+
public function additional_field_get_callback( $object, $field_name ) {
1328+
return 123;
1329+
}
1330+
1331+
public function additional_field_update_callback( $value, $tag ) {
1332+
if ( 'returnError' === $value ) {
1333+
return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
1334+
}
1335+
}
1336+
13271337
/**
13281338
* @ticket 38504
13291339
*/
@@ -1396,16 +1406,6 @@ public function test_editable_response_uses_edit_context() {
13961406
$this->assertArrayNotHasKey( $view_field, $data );
13971407
}
13981408

1399-
public function additional_field_get_callback( $object, $request ) {
1400-
return 123;
1401-
}
1402-
1403-
public function additional_field_update_callback( $value, $tag ) {
1404-
if ( 'returnError' === $value ) {
1405-
return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
1406-
}
1407-
}
1408-
14091409
protected function check_get_taxonomy_terms_response( $response ) {
14101410
$this->assertSame( 200, $response->get_status() );
14111411
$data = $response->get_data();

tests/phpunit/tests/rest-api/rest-users-controller.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2825,6 +2825,17 @@ public function test_additional_field_update_errors() {
28252825
$wp_rest_additional_fields = array();
28262826
}
28272827

2828+
public function additional_field_get_callback( $object, $field_name ) {
2829+
return get_user_meta( $object['id'], $field_name, true );
2830+
}
2831+
2832+
public function additional_field_update_callback( $value, $user, $field_name ) {
2833+
if ( 'returnError' === $value ) {
2834+
return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
2835+
}
2836+
update_user_meta( $user->ID, $field_name, $value );
2837+
}
2838+
28282839
/**
28292840
* @ticket 39701
28302841
* @group ms-required
@@ -3072,17 +3083,6 @@ public function data_get_default_data() {
30723083
);
30733084
}
30743085

3075-
public function additional_field_get_callback( $object ) {
3076-
return get_user_meta( $object['id'], 'my_custom_int', true );
3077-
}
3078-
3079-
public function additional_field_update_callback( $value, $user ) {
3080-
if ( 'returnError' === $value ) {
3081-
return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
3082-
}
3083-
update_user_meta( $user->ID, 'my_custom_int', $value );
3084-
}
3085-
30863086
protected function check_user_data( $user, $data, $context, $links ) {
30873087
$this->assertSame( $user->ID, $data['id'] );
30883088
$this->assertSame( $user->display_name, $data['name'] );

0 commit comments

Comments
 (0)