1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File for the WC_Tests_API_Functions class. |
4
|
|
|
* |
5
|
|
|
* @package WooCommerce\Tests\API |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* REST API Functions. |
10
|
|
|
* @since 2.6.0 |
11
|
|
|
*/ |
12
|
|
|
class WC_Tests_API_Functions extends WC_Unit_Test_Case { |
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string path to the WP upload dir. |
16
|
|
|
*/ |
17
|
|
|
private $upload_dir_path; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string WP upload dir URL. |
21
|
|
|
*/ |
22
|
|
|
private $upload_dir_url; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string Name of the file used in wc_rest_upload_image_from_url() tests. |
26
|
|
|
*/ |
27
|
|
|
private $file_name; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Run setup code for unit tests. |
31
|
|
|
*/ |
32
|
|
|
public function setUp() { |
33
|
|
|
parent::setUp(); |
34
|
|
|
|
35
|
|
|
// Callback used by WP_HTTP_TestCase to decide whether to perform HTTP requests or to provide a mocked response. |
36
|
|
|
$this->http_responder = array( $this, 'mock_http_responses' ); |
|
|
|
|
37
|
|
|
|
38
|
|
|
$upload_dir_info = wp_upload_dir(); |
39
|
|
|
$this->upload_dir_path = $upload_dir_info['path']; |
40
|
|
|
$this->upload_dir_url = $upload_dir_info['url']; |
41
|
|
|
$this->file_name = 'Dr1Bczxq4q.png'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Run tear down code for unit tests. |
46
|
|
|
*/ |
47
|
|
|
public function tearDown() { |
48
|
|
|
parent::tearDown(); |
49
|
|
|
|
50
|
|
|
// remove files created in the wc_rest_upload_image_from_url() tests. |
51
|
|
|
$file_path = $this->upload_dir_path . '/' . $this->file_name; |
52
|
|
|
|
53
|
|
|
if ( file_exists( $file_path ) ) { |
54
|
|
|
unlink( $file_path ); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Test wc_rest_prepare_date_response(). |
60
|
|
|
* |
61
|
|
|
* @since 2.6.0 |
62
|
|
|
*/ |
63
|
|
|
public function test_wc_rest_prepare_date_response() { |
64
|
|
|
$this->assertEquals( '2016-06-06T06:06:06', wc_rest_prepare_date_response( '2016-06-06 06:06:06' ) ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Test wc_rest_upload_image_from_url() should return error when unable to download image. |
69
|
|
|
*/ |
70
|
|
|
public function test_wc_rest_upload_image_from_url_should_return_error_when_unable_to_download_image() { |
71
|
|
|
$expected_error_message = 'Error getting remote image http://somedomain.com/nonexistent-image.png. Error: Not found.'; |
72
|
|
|
$result = wc_rest_upload_image_from_url( 'http://somedomain.com/nonexistent-image.png' ); |
73
|
|
|
|
74
|
|
|
$this->assertWPError( $result ); |
75
|
|
|
$this->assertEquals( $expected_error_message, $result->get_error_message() ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Test wc_rest_upload_image_from_url() should return error when invalid image is passed. |
80
|
|
|
* |
81
|
|
|
* @requires PHP 5.4 |
82
|
|
|
*/ |
83
|
|
|
public function test_wc_rest_upload_image_from_url_should_return_error_when_invalid_image_is_passed() { |
84
|
|
|
// empty file. |
85
|
|
|
$expected_error_message = 'Invalid image: File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.'; |
86
|
|
|
$result = wc_rest_upload_image_from_url( 'http://somedomain.com/invalid-image-1.png' ); |
87
|
|
|
|
88
|
|
|
$this->assertWPError( $result ); |
89
|
|
|
$this->assertEquals( $expected_error_message, $result->get_error_message() ); |
90
|
|
|
|
91
|
|
|
// unsupported mime type. |
92
|
|
|
$expected_error_message = 'Invalid image: Sorry, this file type is not permitted for security reasons.'; |
93
|
|
|
$result = wc_rest_upload_image_from_url( 'http://somedomain.com/invalid-image-2.png' ); |
94
|
|
|
|
95
|
|
|
$this->assertWPError( $result ); |
96
|
|
|
$this->assertEquals( $expected_error_message, $result->get_error_message() ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Test wc_rest_upload_image_from_url() should download image and return an array containing |
101
|
|
|
* information about it. |
102
|
|
|
* |
103
|
|
|
* @requires PHP 5.4 |
104
|
|
|
*/ |
105
|
|
|
public function test_wc_rest_upload_image_from_url_should_download_image_and_return_array() { |
106
|
|
|
$expected_result = array( |
107
|
|
|
'file' => $this->upload_dir_path . '/' . $this->file_name, |
108
|
|
|
'url' => $this->upload_dir_url . '/' . $this->file_name, |
109
|
|
|
'type' => 'image/png', |
110
|
|
|
); |
111
|
|
|
$result = wc_rest_upload_image_from_url( 'http://somedomain.com/' . $this->file_name ); |
112
|
|
|
|
113
|
|
|
$this->assertEquals( $expected_result, $result ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Test wc_rest_set_uploaded_image_as_attachment(). |
118
|
|
|
* |
119
|
|
|
* @since 2.6.0 |
120
|
|
|
*/ |
121
|
|
|
public function test_wc_rest_set_uploaded_image_as_attachment() { |
122
|
|
|
$this->assertInternalType( |
123
|
|
|
'int', |
124
|
|
|
wc_rest_set_uploaded_image_as_attachment( |
125
|
|
|
array( |
126
|
|
|
'file' => '', |
127
|
|
|
'url' => '', |
128
|
|
|
) |
129
|
|
|
) |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Test wc_rest_validate_reports_request_arg(). |
135
|
|
|
* |
136
|
|
|
* @since 2.6.0 |
137
|
|
|
*/ |
138
|
|
|
public function test_wc_rest_validate_reports_request_arg() { |
139
|
|
|
$request = new WP_REST_Request( |
140
|
|
|
'GET', |
141
|
|
|
'/wc/v3/foo', |
142
|
|
|
array( |
143
|
|
|
'args' => array( |
144
|
|
|
'date' => array( |
145
|
|
|
'type' => 'string', |
146
|
|
|
'format' => 'date', |
147
|
|
|
), |
148
|
|
|
), |
149
|
|
|
) |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
// Success. |
153
|
|
|
$this->assertTrue( wc_rest_validate_reports_request_arg( '2016-06-06', $request, 'date' ) ); |
154
|
|
|
|
155
|
|
|
// Error. |
156
|
|
|
$error = wc_rest_validate_reports_request_arg( 'foo', $request, 'date' ); |
157
|
|
|
$this->assertEquals( 'The date you provided is invalid.', $error->get_error_message() ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Test wc_rest_urlencode_rfc3986(). |
162
|
|
|
* |
163
|
|
|
* @since 2.6.0 |
164
|
|
|
*/ |
165
|
|
|
public function test_wc_rest_urlencode_rfc3986() { |
166
|
|
|
$this->assertEquals( 'https%3A%2F%2Fwoocommerce.com%2F', wc_rest_urlencode_rfc3986( 'https://woocommerce.com/' ) ); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Test wc_rest_check_post_permissions(). |
171
|
|
|
* |
172
|
|
|
* @since 2.6.0 |
173
|
|
|
*/ |
174
|
|
|
public function test_wc_rest_check_post_permissions() { |
175
|
|
|
$this->assertFalse( wc_rest_check_post_permissions( 'shop_order' ) ); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Test wc_rest_check_user_permissions(). |
180
|
|
|
* |
181
|
|
|
* @since 2.6.0 |
182
|
|
|
*/ |
183
|
|
|
public function test_wc_rest_check_user_permissions() { |
184
|
|
|
$this->assertFalse( wc_rest_check_user_permissions() ); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Test wc_rest_check_product_term_permissions(). |
189
|
|
|
* |
190
|
|
|
* @since 2.6.0 |
191
|
|
|
*/ |
192
|
|
|
public function test_wc_rest_check_product_term_permissions() { |
193
|
|
|
$this->assertFalse( wc_rest_check_product_term_permissions( 'product_cat' ) ); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Test wc_rest_check_manager_permissions(). |
198
|
|
|
* |
199
|
|
|
* @since 2.6.0 |
200
|
|
|
*/ |
201
|
|
|
public function test_wc_rest_check_manager_permissions() { |
202
|
|
|
$this->assertFalse( wc_rest_check_manager_permissions( 'reports' ) ); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Helper method to define mocked HTTP responses using WP_HTTP_TestCase. |
207
|
|
|
* Thanks to WP_HTTP_TestCase, it is not necessary to perform a regular request |
208
|
|
|
* to an external server which would significantly slow down the tests. |
209
|
|
|
* |
210
|
|
|
* This function is called by WP_HTTP_TestCase::http_request_listner(). |
211
|
|
|
* |
212
|
|
|
* @param array $request Request arguments. |
213
|
|
|
* @param string $url URL of the request. |
214
|
|
|
* |
215
|
|
|
* @return array|false mocked response or false to let WP perform a regular request. |
216
|
|
|
*/ |
217
|
|
|
protected function mock_http_responses( $request, $url ) { |
218
|
|
|
$mocked_response = false; |
219
|
|
|
|
220
|
|
|
if ( 'http://somedomain.com/nonexistent-image.png' === $url ) { |
221
|
|
|
$mocked_response = array( |
222
|
|
|
'response' => array( |
223
|
|
|
'code' => 404, |
224
|
|
|
'message' => 'Not found.', |
225
|
|
|
), |
226
|
|
|
); |
227
|
|
|
} elseif ( 'http://somedomain.com/invalid-image-1.png' === $url ) { |
228
|
|
|
// empty image. |
229
|
|
|
$mocked_response = array( |
230
|
|
|
'response' => array( 'code' => 200 ), |
231
|
|
|
); |
232
|
|
|
} elseif ( 'http://somedomain.com/invalid-image-2.png' === $url ) { |
233
|
|
|
// image with an unsupported mime type. |
234
|
|
|
// we need to manually copy the file as we are mocking the request. without this an empty file is created. |
235
|
|
|
copy( Automattic\WooCommerce\RestApi\UnitTests\Bootstrap::instance()->get_dir() . '/data/file.txt', $request['filename'] ); |
236
|
|
|
|
237
|
|
|
$mocked_response = array( |
238
|
|
|
'response' => array( 'code' => 200 ), |
239
|
|
|
); |
240
|
|
|
} elseif ( 'http://somedomain.com/' . $this->file_name === $url ) { |
241
|
|
|
// we need to manually copy the file as we are mocking the request. without this an empty file is created. |
242
|
|
|
copy( Automattic\WooCommerce\RestApi\UnitTests\Bootstrap::instance()->get_dir() . '/data/Dr1Bczxq4q.png', $request['filename'] ); |
243
|
|
|
|
244
|
|
|
$mocked_response = array( |
245
|
|
|
'response' => array( 'code' => 200 ), |
246
|
|
|
); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return $mocked_response; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths