1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
<?php
require_once( 'cmb-tests-base.php' );
abstract class Test_CMB2_Rest_Base extends Test_CMB2 {
public function set_up() {
$this->reset_instances();
parent::set_up();
update_option( 'permalink_structure', '/%postname%/' );
if ( ! did_action( 'rest_api_init' ) ) {
do_action( 'rest_api_init', rest_get_server() );
}
}
public function set_up_and_init( $metabox_array ) {
$this->metabox_array = $metabox_array;
$this->cmb_id = $metabox_array['id'];
$this->rest_box = new Test_CMB2_REST_Object( new CMB2( $this->metabox_array ) );
self::set_up();
$this->subscriber = $this->factory->user->create( array(
'role' => 'subscriber',
) );
$this->administrator = $this->factory->user->create( array(
'role' => 'administrator',
) );
$this->post_id = $this->factory->post->create();
foreach ( $this->metabox_array['fields'] as $field ) {
update_post_meta( $this->post_id, $field['id'], md5( $field['id'] ) );
}
CMB2_REST::register_cmb2_fields();
}
public function tear_down() {
parent::tear_down();
if ( ! empty( $this->metabox_array['fields'] ) ) {
foreach ( $this->metabox_array['fields'] as $field ) {
delete_post_meta( $this->post_id, $field['id'] );
}
}
Test_CMB2_REST_Object::reset_boxes();
Test_CMB2_REST_Object::reset_type_boxes();
foreach ( CMB2_Boxes::get_all() as $box ) {
CMB2_Boxes::remove( $box->cmb_id );
}
global $wp_actions, $wp_rest_server;
unset( $wp_rest_server );
unset( $wp_actions['rest_api_init'] );
}
protected function reset_instances() {
foreach ( CMB2_REST::get_all() as $cmb_id => $rest ) {
$rest = new CMB2_REST( $rest->cmb );
$rest->universal_hooks();
}
}
protected function assertResponseStatuses( $url, $statuses, $debug = false ) {
foreach ( $statuses as $method => $status ) {
$error_code = '';
if ( is_array( $status ) ) {
$error_code = current( $status );
$status = key( $status );
}
$this->assertRequestResponseStatus( $method, $url, $status, $error_code, $debug );
}
}
protected function assertRequestResponseStatus( $method, $url, $status, $error_code = '', $debug = false ) {
if ( $debug ) {
error_log( $method . ' $url: ' . print_r( $url, true ) );
}
$request = new WP_REST_Request( $method, $url );
$this->assertResponseStatus( $status, rest_do_request( $request ), $error_code, $debug );
}
protected function assertResponseStatus( $status, $response, $error_code = '', $debug = false ) {
if ( $debug ) {
error_log( '$response->get_data(): ' . print_r( $response->get_data(), true ) );
}
$this->assertEquals( $status, $response->get_status() );
if ( $error_code ) {
$this->assertResponseErrorCode( $error_code, $response );
}
}
protected function assertResponseErrorCode( $error_code, $response ) {
$response_data = $response->get_data();
$this->assertEquals( $error_code, $response_data['code'] );
}
protected function assertResponseData( $data, $response ) {
$response_data = $response->get_data();
$tested_data = array();
foreach ( $data as $key => $value ) {
if ( isset( $response_data[ $key ] ) ) {
$tested_data[ $key ] = $response_data[ $key ];
} else {
$tested_data[ $key ] = null;
}
}
$this->assertEquals( $data, $tested_data );
}
}
if ( ! class_exists( 'Test_CMB2_REST_Object' ) ) {
class Test_CMB2_REST_Object extends CMB2_REST {
public function declare_read_edit_fields() {
return parent::declare_read_edit_fields();
}
public function can_read( $show_in_rest ) {
return parent::can_read( $show_in_rest );
}
public function can_edit( $show_in_rest ) {
return parent::can_edit( $show_in_rest );
}
public static function get_object_id( $object, $object_type = 'post' ) {
return parent::get_object_id( $object, $object_type );
}
public static function reset_boxes() {
parent::$boxes = array();
}
public static function reset_type_boxes() {
parent::$type_boxes = array(
'post' => array(),
'user' => array(),
'comment' => array(),
'term' => array(),
);
}
}
}