2016-06-14 01:58:45 +00:00
< ? php
2019-01-21 16:02:28 +00:00
/**
* File for the WC_Tests_API_Functions class .
*
* @ package WooCommerce\Tests\API
*/
2016-06-14 01:58:45 +00:00
/**
* REST API Functions .
* @ since 2.6 . 0
*/
class WC_Tests_API_Functions extends WC_Unit_Test_Case {
2019-01-21 16:02:28 +00:00
/**
* @ var string path to the WP upload dir .
*/
private $upload_dir_path ;
/**
* @ var string WP upload dir URL .
*/
private $upload_dir_url ;
/**
* @ var string Name of the file used in wc_rest_upload_image_from_url () tests .
*/
private $file_name ;
/**
* Run setup code for unit tests .
*/
public function setUp () {
parent :: setUp ();
// Callback used by WP_HTTP_TestCase to decide whether to perform HTTP requests or to provide a mocked response.
$this -> http_responder = array ( $this , 'mock_http_responses' );
$upload_dir_info = wp_upload_dir ();
$this -> upload_dir_path = $upload_dir_info [ 'path' ];
$this -> upload_dir_url = $upload_dir_info [ 'url' ];
$this -> file_name = 'Dr1Bczxq4q.png' ;
}
/**
* Run tear down code for unit tests .
*/
public function tearDown () {
parent :: tearDown ();
// remove files created in the wc_rest_upload_image_from_url() tests.
$file_path = $this -> upload_dir_path . '/' . $this -> file_name ;
if ( file_exists ( $file_path ) ) {
unlink ( $file_path );
}
}
2016-06-14 01:58:45 +00:00
/**
* Test wc_rest_prepare_date_response () .
*
* @ since 2.6 . 0
*/
public function test_wc_rest_prepare_date_response () {
$this -> assertEquals ( '2016-06-06T06:06:06' , wc_rest_prepare_date_response ( '2016-06-06 06:06:06' ) );
}
/**
2019-01-21 16:02:28 +00:00
* Test wc_rest_upload_image_from_url () should return error when unable to download image .
*/
public function test_wc_rest_upload_image_from_url_should_return_error_when_unable_to_download_image () {
$expected_error_message = 'Error getting remote image http://somedomain.com/nonexistent-image.png. Error: Not found.' ;
$result = wc_rest_upload_image_from_url ( 'http://somedomain.com/nonexistent-image.png' );
2019-07-19 13:37:03 +00:00
$this -> assertWPError ( $result );
2019-01-21 16:02:28 +00:00
$this -> assertEquals ( $expected_error_message , $result -> get_error_message () );
}
/**
* Test wc_rest_upload_image_from_url () should return error when invalid image is passed .
2019-01-21 18:02:55 +00:00
*
* @ requires PHP 5.4
2019-01-21 16:02:28 +00:00
*/
public function test_wc_rest_upload_image_from_url_should_return_error_when_invalid_image_is_passed () {
// empty file.
2020-01-15 14:17:49 +00:00
if ( version_compare ( get_bloginfo ( 'version' ), '5.4-alpha' , '>=' ) ) {
$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 file or by post_max_size being defined as smaller than upload_max_filesize in php.ini.' ;
} else {
$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.' ;
}
$result = wc_rest_upload_image_from_url ( 'http://somedomain.com/invalid-image-1.png' );
2019-01-21 16:02:28 +00:00
2019-07-19 13:37:03 +00:00
$this -> assertWPError ( $result );
2019-01-21 16:02:28 +00:00
$this -> assertEquals ( $expected_error_message , $result -> get_error_message () );
// unsupported mime type.
$expected_error_message = 'Invalid image: Sorry, this file type is not permitted for security reasons.' ;
$result = wc_rest_upload_image_from_url ( 'http://somedomain.com/invalid-image-2.png' );
2019-07-19 13:37:03 +00:00
$this -> assertWPError ( $result );
2019-01-21 16:02:28 +00:00
$this -> assertEquals ( $expected_error_message , $result -> get_error_message () );
}
/**
* Test wc_rest_upload_image_from_url () should download image and return an array containing
* information about it .
2019-01-21 18:02:55 +00:00
*
* @ requires PHP 5.4
2016-06-14 01:58:45 +00:00
*/
2019-01-21 16:02:28 +00:00
public function test_wc_rest_upload_image_from_url_should_download_image_and_return_array () {
$expected_result = array (
'file' => $this -> upload_dir_path . '/' . $this -> file_name ,
'url' => $this -> upload_dir_url . '/' . $this -> file_name ,
'type' => 'image/png' ,
);
$result = wc_rest_upload_image_from_url ( 'http://somedomain.com/' . $this -> file_name );
$this -> assertEquals ( $expected_result , $result );
2016-06-14 01:58:45 +00:00
}
/**
* Test wc_rest_set_uploaded_image_as_attachment () .
*
* @ since 2.6 . 0
*/
public function test_wc_rest_set_uploaded_image_as_attachment () {
2019-01-21 16:02:28 +00:00
$this -> assertInternalType (
'int' ,
wc_rest_set_uploaded_image_as_attachment (
array (
'file' => '' ,
'url' => '' ,
)
)
);
2016-06-14 01:58:45 +00:00
}
/**
* Test wc_rest_validate_reports_request_arg () .
*
* @ since 2.6 . 0
*/
public function test_wc_rest_validate_reports_request_arg () {
2019-01-21 16:02:28 +00:00
$request = new WP_REST_Request (
'GET' ,
'/wc/v3/foo' ,
array (
'args' => array (
'date' => array (
'type' => 'string' ,
'format' => 'date' ,
),
2016-06-14 01:58:45 +00:00
),
2019-01-21 16:02:28 +00:00
)
);
2016-06-14 01:58:45 +00:00
// Success.
$this -> assertTrue ( wc_rest_validate_reports_request_arg ( '2016-06-06' , $request , 'date' ) );
// Error.
$error = wc_rest_validate_reports_request_arg ( 'foo' , $request , 'date' );
$this -> assertEquals ( 'The date you provided is invalid.' , $error -> get_error_message () );
}
/**
* Test wc_rest_urlencode_rfc3986 () .
*
* @ since 2.6 . 0
*/
public function test_wc_rest_urlencode_rfc3986 () {
2017-06-06 20:43:40 +00:00
$this -> assertEquals ( 'https%3A%2F%2Fwoocommerce.com%2F' , wc_rest_urlencode_rfc3986 ( 'https://woocommerce.com/' ) );
2016-06-14 01:58:45 +00:00
}
/**
* Test wc_rest_check_post_permissions () .
*
* @ since 2.6 . 0
*/
public function test_wc_rest_check_post_permissions () {
2017-07-13 04:06:38 +00:00
$this -> assertFalse ( wc_rest_check_post_permissions ( 'shop_order' ) );
2016-06-14 01:58:45 +00:00
}
/**
* Test wc_rest_check_user_permissions () .
*
* @ since 2.6 . 0
*/
public function test_wc_rest_check_user_permissions () {
2017-07-13 04:06:38 +00:00
$this -> assertFalse ( wc_rest_check_user_permissions () );
2016-06-14 01:58:45 +00:00
}
/**
* Test wc_rest_check_product_term_permissions () .
*
* @ since 2.6 . 0
*/
public function test_wc_rest_check_product_term_permissions () {
2017-07-13 04:06:38 +00:00
$this -> assertFalse ( wc_rest_check_product_term_permissions ( 'product_cat' ) );
2016-06-14 01:58:45 +00:00
}
/**
* Test wc_rest_check_manager_permissions () .
*
* @ since 2.6 . 0
*/
public function test_wc_rest_check_manager_permissions () {
2017-07-13 04:06:38 +00:00
$this -> assertFalse ( wc_rest_check_manager_permissions ( 'reports' ) );
2016-06-14 01:58:45 +00:00
}
2019-01-21 16:02:28 +00:00
/**
* Helper method to define mocked HTTP responses using WP_HTTP_TestCase .
* Thanks to WP_HTTP_TestCase , it is not necessary to perform a regular request
* to an external server which would significantly slow down the tests .
*
* This function is called by WP_HTTP_TestCase :: http_request_listner () .
*
* @ param array $request Request arguments .
* @ param string $url URL of the request .
*
* @ return array | false mocked response or false to let WP perform a regular request .
*/
protected function mock_http_responses ( $request , $url ) {
$mocked_response = false ;
if ( 'http://somedomain.com/nonexistent-image.png' === $url ) {
$mocked_response = array (
'response' => array (
'code' => 404 ,
'message' => 'Not found.' ,
),
);
} elseif ( 'http://somedomain.com/invalid-image-1.png' === $url ) {
// empty image.
$mocked_response = array (
'response' => array ( 'code' => 200 ),
);
} elseif ( 'http://somedomain.com/invalid-image-2.png' === $url ) {
// image with an unsupported mime type.
// we need to manually copy the file as we are mocking the request. without this an empty file is created.
2020-05-06 12:40:17 +00:00
self :: file_copy ( WC_Unit_Tests_Bootstrap :: instance () -> tests_dir . '/data/file.txt' , $request [ 'filename' ] );
2019-01-21 16:02:28 +00:00
$mocked_response = array (
'response' => array ( 'code' => 200 ),
);
} elseif ( 'http://somedomain.com/' . $this -> file_name === $url ) {
// we need to manually copy the file as we are mocking the request. without this an empty file is created.
2020-05-06 12:40:17 +00:00
self :: file_copy ( WC_Unit_Tests_Bootstrap :: instance () -> tests_dir . '/data/Dr1Bczxq4q.png' , $request [ 'filename' ] );
2019-01-21 16:02:28 +00:00
$mocked_response = array (
'response' => array ( 'code' => 200 ),
);
}
return $mocked_response ;
}
2016-06-14 01:58:45 +00:00
}