| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class \Test\RemotePost |
| 4 |
* |
| 5 |
* @package WPDiscourse |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPDiscourse\Test; |
| 9 |
|
| 10 |
/** |
| 11 |
* Remote posting methods for WPDiscourse unit tests |
| 12 |
*/ |
| 13 |
trait RemotePost { |
| 14 |
/** |
| 15 |
* Build remote post response. |
| 16 |
* |
| 17 |
* @param string $type Type of response. |
| 18 |
* @param string $sub_type Sub-type of response. |
| 19 |
*/ |
| 20 |
protected function build_response( $type, $sub_type = null ) { |
| 21 |
$codes = array( |
| 22 |
'success' => 200, |
| 23 |
'invalid_parameters' => 400, |
| 24 |
'forbidden' => 403, |
| 25 |
'not_found' => 404, |
| 26 |
'unprocessable' => 422, |
| 27 |
); |
| 28 |
$messages = array( |
| 29 |
'success' => 'OK', |
| 30 |
'invalid_parameters' => 'Bad Request', |
| 31 |
'forbidden' => 'Forbidden', |
| 32 |
'not_found' => 'Not found', |
| 33 |
'unprocessable' => 'Unprocessable Entity', |
| 34 |
); |
| 35 |
if ( in_array( $type, array( 'invalid_parameters', 'unprocessable', 'not_found' ), true ) ) { |
| 36 |
$body = $this->response_body_json( $type, $sub_type ); |
| 37 |
} else { |
| 38 |
$body = array( |
| 39 |
'success' => '{}', |
| 40 |
'forbidden' => 'You are not permitted to view the requested resource. The API username or key is invalid.', |
| 41 |
)[ $type ]; |
| 42 |
} |
| 43 |
return array( |
| 44 |
'headers' => array(), |
| 45 |
'body' => $body, |
| 46 |
'response' => array( |
| 47 |
'code' => $codes[ $type ], |
| 48 |
'message' => $messages[ $type ], |
| 49 |
), |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Build JSON of response body. |
| 55 |
* |
| 56 |
* @param string $type Type of response. |
| 57 |
* @param string $sub_type Sub-type of response. |
| 58 |
* @param string $action_type Action type of test. |
| 59 |
*/ |
| 60 |
protected function response_body_json( $type, $sub_type = null, $action_type = 'create_post' ) { |
| 61 |
if ( in_array( $type, array( 'post_create', 'post_update', 'user', 'comments', 'site', 'users', 'sync_sso', 'groups', 'user_create', 'scopes' ), true ) ) { |
| 62 |
return $this->response_body_file( $type ); |
| 63 |
} |
| 64 |
if ( 'unprocessable' === $type ) { |
| 65 |
$messages = array( |
| 66 |
'title' => 'Title seems unclear, most of the words contain the same letters over and over?', |
| 67 |
'embed' => 'Embed url has already been taken', |
| 68 |
); |
| 69 |
$message_type = $sub_type; |
| 70 |
} else { |
| 71 |
$messages = array( |
| 72 |
'invalid_parameters' => "You supplied invalid parameters to the request: $sub_type", |
| 73 |
'not_found' => "Sorry, that resource doesn't exist in our system.", |
| 74 |
'forbidden' => 'You are not permitted to view the requested resource. The API username or key is invalid.', |
| 75 |
); |
| 76 |
$message_type = $type; |
| 77 |
} |
| 78 |
return wp_json_encode( |
| 79 |
array( |
| 80 |
'action' => $action_type, |
| 81 |
'errors' => array( $messages[ $message_type ] ), |
| 82 |
'error_type' => $type, |
| 83 |
) |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get fixture with response body. |
| 89 |
* |
| 90 |
* @param string $file Name of response body file. |
| 91 |
*/ |
| 92 |
protected function response_body_file( $file ) { |
| 93 |
return file_get_contents( __DIR__ . "/../../fixtures/response_body/$file.json" ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Mock remote post response. |
| 98 |
* |
| 99 |
* @param object $first_request First request in tested method. |
| 100 |
* @param object $second_request Second request in tested method. |
| 101 |
*/ |
| 102 |
protected function mock_remote_post( $first_request, $second_request = null ) { |
| 103 |
add_filter( |
| 104 |
'pre_http_request', |
| 105 |
function ( $prempt, $args, $url ) use ( $first_request, $second_request ) { |
| 106 |
$is_sr = ! empty( $second_request ) && ( strpos( $url, $second_request['url'] ) !== false ); |
| 107 |
$request = $is_sr ? $second_request : $first_request; |
| 108 |
|
| 109 |
if ( $request['method'] !== $args['method'] ) { |
| 110 |
return new \WP_Error( 'http_request_failed', 'Incorrect method' ); |
| 111 |
} |
| 112 |
|
| 113 |
if ( isset( $request['headers'] ) && $request['headers'] !== $args['headers'] ) { |
| 114 |
return new \WP_Error( 'http_request_failed', 'Incorrect headers' ); |
| 115 |
} |
| 116 |
|
| 117 |
if ( isset( $request['body'] ) && $request['body'] !== $args['body'] ) { |
| 118 |
return new \WP_Error( 'http_request_failed', 'Incorrect body' ); |
| 119 |
} |
| 120 |
|
| 121 |
return $request['response']; |
| 122 |
}, |
| 123 |
10, |
| 124 |
3 |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Mock remote post success. |
| 130 |
* |
| 131 |
* @param string $type Type of response. |
| 132 |
* @param object $second_request Second request response of second request in tested method. |
| 133 |
*/ |
| 134 |
protected function mock_remote_post_success( $type, $method = 'GET', $second_request = null ) { |
| 135 |
$raw_body = $this->response_body_json( $type ); |
| 136 |
$response = $this->build_response( 'success' ); |
| 137 |
$response['body'] = $raw_body; |
| 138 |
$first_request = array( |
| 139 |
'method' => $method, |
| 140 |
'response' => $response, |
| 141 |
); |
| 142 |
$this->mock_remote_post( $first_request, $second_request ); |
| 143 |
return json_decode( $raw_body ); |
| 144 |
} |
| 145 |
} |
| 146 |
|