Skip to content

Commit 7ca84ee

Browse files
committed
Refactor, clean up, normalize REST API unit tests, and add registered rest field tests
1 parent 6808e36 commit 7ca84ee

File tree

4 files changed

+113
-77
lines changed

4 files changed

+113
-77
lines changed

tests/cmb-rest-tests-base.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,50 @@ abstract class Test_CMB2_Rest_Base extends Test_CMB2 {
1616
* Set up the test fixture
1717
*/
1818
public function setUp() {
19+
$this->reset_instances();
20+
1921
parent::setUp();
2022
update_option( 'permalink_structure', '/%postname%/' );
21-
rest_get_server();
23+
24+
25+
if ( ! did_action( 'rest_api_init' ) ) {
26+
do_action( 'rest_api_init', rest_get_server() );
27+
}
28+
}
29+
30+
/**
31+
* Set up the test fixture
32+
*/
33+
public function set_up_and_init( $metabox_array ) {
34+
$this->metabox_array = $metabox_array;
35+
$this->cmb_id = $metabox_array['id'];
36+
$this->rest_box = new Test_CMB2_REST_Object( new CMB2( $this->metabox_array ) );
37+
38+
self::setUp();
39+
40+
$this->subscriber = $this->factory->user->create( array( 'role' => 'subscriber' ) );
41+
$this->administrator = $this->factory->user->create( array( 'role' => 'administrator' ) );
42+
$this->post_id = $this->factory->post->create();
43+
44+
foreach ( $this->metabox_array['fields'] as $field ) {
45+
update_post_meta( $this->post_id, $field['id'], md5( $field['id'] ) );
46+
}
2247
}
2348

2449
public function tearDown() {
2550
parent::tearDown();
51+
foreach ( $this->metabox_array['fields'] as $field ) {
52+
delete_post_meta( $this->post_id, $field['id'] );
53+
}
54+
CMB2_Boxes::remove( $this->cmb_id );
55+
CMB2_REST::remove( $this->cmb_id );
56+
}
57+
58+
protected function reset_instances() {
59+
foreach ( CMB2_REST::$boxes as $cmb_id => $rest ) {
60+
$rest = new CMB2_REST( $rest->cmb );
61+
$rest->universal_hooks();
62+
}
2663
}
2764

2865
protected function assertResponseStatuses( $url, $statuses, $debug = false ) {

tests/test-cmb-rest-controllers.php

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ class Test_CMB2_REST_Controllers extends Test_CMB2_Rest_Base {
2424
* Set up the test fixture
2525
*/
2626
public function setUp() {
27-
$this->cmb_id = 'test';
28-
$this->metabox_array = array(
29-
'id' => $this->cmb_id,
27+
$this->set_up_and_init( array(
28+
'id' => 'test',
3029
'show_in_rest' => WP_REST_Server::ALLMETHODS,
30+
'object_types' => array( 'post' ),
3131
'fields' => array(
3232
'rest_test' => array(
3333
'name' => 'Name',
@@ -40,29 +40,7 @@ public function setUp() {
4040
'type' => 'text',
4141
),
4242
),
43-
);
44-
45-
$this->cmb = new CMB2( $this->metabox_array );
46-
47-
$this->rest_box = new Test_CMB2_REST_Object( $this->cmb );
48-
$this->rest_box->universal_hooks();
49-
50-
parent::setUp();
51-
52-
$this->subscriber = $this->factory->user->create( array( 'role' => 'subscriber' ) );
53-
$this->administrator = $this->factory->user->create( array( 'role' => 'administrator' ) );
54-
55-
$this->post_id = $this->factory->post->create();
56-
57-
foreach ( $this->metabox_array['fields'] as $field ) {
58-
update_post_meta( $this->post_id, $field['id'], md5( $field['id'] ) );
59-
}
60-
61-
cmb2_bootstrap();
62-
}
63-
64-
public function tearDown() {
65-
parent::tearDown();
43+
) );
6644
}
6745

6846
public function test_get_schema() {
@@ -95,6 +73,7 @@ public function test_read_box() {
9573

9674
$rest = new CMB2_REST( new CMB2( array(
9775
'id' => 'test_read_box_test',
76+
'object_types' => array( 'post' ),
9877
'show_in_rest' => WP_REST_Server::EDITABLE,
9978
) ) );
10079
$rest->universal_hooks();
@@ -107,6 +86,7 @@ public function test_read_box() {
10786

10887
$rest = new CMB2_REST( new CMB2( array(
10988
'id' => 'test_edit_box_test',
89+
'object_types' => array( 'post' ),
11090
'show_in_rest' => WP_REST_Server::READABLE,
11191
) ) );
11292
$rest->universal_hooks();
@@ -124,6 +104,7 @@ public function test_read_box_with_read_permissions_callback() {
124104
$rest = new CMB2_REST( new CMB2( array(
125105
'id' => __FUNCTION__,
126106
'show_in_rest' => WP_REST_Server::ALLMETHODS,
107+
'object_types' => array( 'post' ),
127108
'get_item_permissions_check_cb' => 'wp_die',
128109
) ) );
129110
$rest->universal_hooks();
@@ -153,7 +134,7 @@ public function test_read_box_field() {
153134
foreach ( $mb['fields'] as &$field ) {
154135
$field['show_in_rest'] = WP_REST_Server::EDITABLE;
155136
}
156-
$rest_box2 = new Test_CMB2_REST_Object( new CMB2( $mb ) );
137+
$rest_box2 = new CMB2_REST( new CMB2( $mb ) );
157138
$rest_box2->universal_hooks();
158139

159140
$url = '/' . CMB2_REST::NAME_SPACE . '/boxes/test2/fields/rest_test';
@@ -181,6 +162,7 @@ public function test_read_box_field_with_read_permissions_callback() {
181162
$rest = new CMB2_REST( new CMB2( array(
182163
'id' => __FUNCTION__,
183164
'show_in_rest' => WP_REST_Server::ALLMETHODS,
165+
'object_types' => array( 'post' ),
184166
'get_item_permissions_check_cb' => 'wp_die',
185167
) ) );
186168
$rest->universal_hooks();

tests/test-cmb-rest-registered-fields.php

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,49 +24,86 @@ class Test_CMB2_REST_Registered_Fields extends Test_CMB2_Rest_Base {
2424
* Set up the test fixture
2525
*/
2626
public function setUp() {
27-
parent::setUp();
28-
29-
$this->cmb_id = 'test';
30-
$this->metabox_array = array(
31-
'id' => $this->cmb_id,
27+
$this->set_up_and_init( array(
28+
'id' => strtolower( __CLASS__ ),
3229
'show_in_rest' => WP_REST_Server::ALLMETHODS,
30+
'object_types' => array( 'post' ),
3331
'fields' => array(
34-
'rest_test' => array(
32+
'rest_test_registered_fields' => array(
3533
'name' => 'Name',
36-
'id' => 'rest_test',
34+
'id' => 'rest_test_registered_fields',
3735
'type' => 'text',
3836
),
39-
'rest_test2' => array(
37+
'rest_test2_registered_fields' => array(
4038
'name' => 'Name',
41-
'id' => 'rest_test2',
39+
'id' => 'rest_test2_registered_fields',
4240
'type' => 'text',
4341
),
4442
),
45-
);
43+
) );
44+
}
4645

47-
$this->cmb = new CMB2( $this->metabox_array );
46+
public function test_rest_posts_controller_exists() {
47+
$this->assertTrue( class_exists( 'WP_REST_Posts_Controller' ) );
48+
}
4849

49-
$this->rest_box = new Test_CMB2_REST_Object( $this->cmb );
50-
$this->rest_box->universal_hooks();
50+
public function test_read_post_has_cmb2_data() {
51+
wp_set_current_user( $this->subscriber );
5152

52-
$this->subscriber = $this->factory->user->create( array( 'role' => 'subscriber' ) );
53-
$this->administrator = $this->factory->user->create( array( 'role' => 'administrator' ) );
53+
$expected = array( 'cmb2' => array( $this->cmb_id => array(), ), );
54+
foreach ( $this->metabox_array['fields'] as $field ) {
55+
$expected['cmb2'][ $this->cmb_id ][ $field['id'] ] = md5( $field['id'] );
56+
}
5457

55-
$this->post_id = $this->factory->post->create();
58+
$url = '/wp/v2/posts/' . $this->post_id;
59+
$response = rest_do_request( new WP_REST_Request( 'GET', $url ) );
60+
$this->assertResponseData( $expected, $response );
61+
}
62+
63+
public function test_update_post_cmb2_data_unauthorized() {
64+
wp_set_current_user( $this->subscriber );
65+
66+
$url = '/wp/v2/posts/' . $this->post_id;
5667

68+
$request = new WP_REST_Request( 'POST', $url );
69+
$cmb2 = array();
5770
foreach ( $this->metabox_array['fields'] as $field ) {
58-
update_post_meta( $this->post_id, $field['id'], md5( $field['id'] ) );
71+
$cmb2[ $this->cmb_id ][ $field['id'] ] = $field['id'];
5972
}
6073

61-
cmb2_bootstrap();
62-
}
74+
$request['cmb2'] = $cmb2;
6375

64-
public function tearDown() {
65-
parent::tearDown();
76+
$this->assertResponseStatus( 403, rest_do_request( $request ), 'rest_cannot_edit' );
6677
}
6778

68-
public function test_rest_posts_controller_exists() {
69-
$this->assertTrue( class_exists( 'WP_REST_Posts_Controller' ) );
79+
public function test_update_post_cmb2_data_authorized() {
80+
wp_set_current_user( $this->administrator );
81+
82+
$url = '/wp/v2/posts/' . $this->post_id;
83+
84+
$request = new WP_REST_Request( 'POST', $url );
85+
$cmb2 = array();
86+
foreach ( $this->metabox_array['fields'] as $field ) {
87+
$cmb2[ $this->cmb_id ][ $field['id'] ] = $field['id'];
88+
}
89+
90+
$request['cmb2'] = $cmb2;
91+
92+
$this->assertResponseStatus( 200, rest_do_request( $request ) );
93+
94+
foreach ( $this->metabox_array['fields'] as $field ) {
95+
$this->assertEquals( $field['id'], get_post_meta( $this->post_id, $field['id'], 1 ) );
96+
}
97+
98+
$request = new WP_REST_Request( 'POST', $url );
99+
$request['cmb2'] = array( $this->cmb_id => array(
100+
'rest_test2_registered_fields' => 'new value',
101+
) );
102+
103+
$this->assertResponseStatus( 200, rest_do_request( $request ) );
104+
105+
$this->assertEquals( 'new value', get_post_meta( $this->post_id, 'rest_test2_registered_fields', 1 ) );
106+
$this->assertEquals( 'rest_test_registered_fields', get_post_meta( $this->post_id, 'rest_test_registered_fields', 1 ) );
70107
}
71108

72109
}

tests/test-cmb-rest.php

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ class Test_CMB2_REST extends Test_CMB2_Rest_Base {
2424
* Set up the test fixture
2525
*/
2626
public function setUp() {
27-
parent::setUp();
28-
29-
$this->cmb_id = 'test';
30-
$this->metabox_array = array(
31-
'id' => $this->cmb_id,
27+
$this->set_up_and_init( array(
28+
'id' => strtolower( __CLASS__ ),
3229
'show_in_rest' => WP_REST_Server::ALLMETHODS,
3330
'fields' => array(
3431
'rest_test' => array(
@@ -42,24 +39,7 @@ public function setUp() {
4239
'type' => 'text',
4340
),
4441
),
45-
);
46-
47-
$this->cmb = new CMB2( $this->metabox_array );
48-
$this->rest_box = new Test_CMB2_REST_Object( $this->cmb );
49-
$this->post_id = $this->factory->post->create();
50-
51-
foreach ( $this->metabox_array['fields'] as $field ) {
52-
update_post_meta( $this->post_id, $field['id'], md5( $field['id'] ) );
53-
}
54-
55-
cmb2_bootstrap();
56-
}
57-
58-
public function tearDown() {
59-
parent::tearDown();
60-
61-
global $wp_rest_server;
62-
$wp_rest_server = null;
42+
) );
6343
}
6444

6545
public function test_construction() {
@@ -187,7 +167,7 @@ public function test_get_object_data() {
187167
}
188168

189169
public function test_get_rest_box() {
190-
$this->assertInstanceOf( 'CMB2_REST', CMB2_REST::get_rest_box( 'test' ) );
170+
$this->assertInstanceOf( 'CMB2_REST', CMB2_REST::get_rest_box( strtolower( __CLASS__ ) ) );
191171
}
192172

193173
public function test_get_restable_field_values() {
@@ -219,7 +199,7 @@ public function test_update_restable_field_values() {
219199
$this->cmb_id => $fields,
220200
);
221201

222-
$values = CMB2_REST::update_restable_field_values( $new_values, (object) array( 'ID' => $this->post_id ), 'cmb2' );
202+
$values = CMB2_REST::update_restable_field_values( $new_values, (object) array( 'ID' => $this->post_id ), 'cmb2', new WP_REST_Request, 'post' );
223203

224204
$this->assertEquals( count( $fields ), count( $values[ $this->cmb_id ] ) );
225205
foreach ( $values[ $this->cmb_id ] as $value ) {
@@ -232,7 +212,7 @@ public function test_update_restable_field_values() {
232212
}
233213

234214
protected function confirm_get_restable_field_values( $expected ) {
235-
$values = CMB2_REST::get_restable_field_values( array( 'id' => $this->post_id ), '', new WP_REST_Request );
215+
$values = CMB2_REST::get_restable_field_values( array( 'id' => $this->post_id ), '', new WP_REST_Request, 'post' );
236216
$expected = array( $this->cmb_id => $expected );
237217
$this->assertEquals( $expected, $values );
238218
}

0 commit comments

Comments
 (0)