From ec1e0b11105cb3a559c9f2d805cedd2192d6c110 Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Thu, 7 May 2020 23:07:15 +0530 Subject: [PATCH 1/8] Add unit test for trash status for products --- unit-tests/Tests/Version3/products.php | 53 ++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/unit-tests/Tests/Version3/products.php b/unit-tests/Tests/Version3/products.php index 10f876d07ee..22351897134 100644 --- a/unit-tests/Tests/Version3/products.php +++ b/unit-tests/Tests/Version3/products.php @@ -58,6 +58,59 @@ class WC_Tests_API_Product extends WC_REST_Unit_Test_Case { $this->assertEquals( 'DUMMY EXTERNAL SKU', $products[1]['sku'] ); } + /** + * Test getting trashed products. + */ + public function test_get_trashed_products() { + wp_set_current_user( $this->user ); + $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); + $data_store = WC_Data_Store::load( 'product' ); + $data_store->delete( $product ); + $request = new WP_REST_Request( 'GET', '/wc/v3/products' ); + $request->set_query_params( array( 'status' => 'trash' ) ); + $response = $this->server->dispatch( $request ); + $products = $response->get_data(); + + $this->assertEquals( 200, $response->get_status() ); + $this->assertEquals( 1, count( $products ) ); + $this->assertEquals( $product->get_name(), $products[0]['name'] ); + $this->assertEquals( $product->get_id(), $products[0]['id'] ); + } + + /** + * Trashed products should not be returned by default. + */ + public function test_get_trashed_products_not_returned_by_default() { + wp_set_current_user( $this->user ); + $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); + $data_store = WC_Data_Store::load( 'product' ); + $data_store->delete( $product ); + + $response = $this->server->dispatch( + new WP_REST_Request( 'GET', '/wc/v3/products' ) + ); + $products = $response->get_data(); + + $this->assertEquals( 200, $response->get_status() ); + $this->assertEquals( 0, count( $products ) ); + } + + /** + * Trashed product can be fetched directly. + */ + public function test_get_trashed_products_returned_by_id() { + wp_set_current_user( $this->user ); + $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); + $data_store = WC_Data_Store::load( 'product' ); + $data_store->delete( $product ); + + $response = $this->server->dispatch( + new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() ) + ); + + $this->assertEquals( 200, $response->get_status() ); + } + /** * Test getting products without permission. * From 57b59271bf6c0e6a58b10fde9a7cddd88fd14b15 Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Thu, 7 May 2020 23:07:35 +0530 Subject: [PATCH 2/8] Change file path in response to change in core --- unit-tests/Bootstrap.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/unit-tests/Bootstrap.php b/unit-tests/Bootstrap.php index d4658968ce8..b10c7f8eae5 100755 --- a/unit-tests/Bootstrap.php +++ b/unit-tests/Bootstrap.php @@ -113,11 +113,11 @@ class Bootstrap { require_once $this->wp_tests_dir . '/includes/bootstrap.php'; // WooCommerce Core Testing Framework. - require_once $this->wc_tests_dir . '/framework/class-wc-unit-test-factory.php'; - require_once $this->wc_tests_dir . '/framework/vendor/class-wp-test-spy-rest-server.php'; - require_once $this->wc_tests_dir . '/includes/wp-http-testcase.php'; - require_once $this->wc_tests_dir . '/framework/class-wc-unit-test-case.php'; - require_once $this->wc_tests_dir . '/framework/class-wc-rest-unit-test-case.php'; + require_once $this->wc_tests_dir . '/legacy/framework/class-wc-unit-test-factory.php'; + require_once $this->wc_tests_dir . '/legacy/framework/vendor/class-wp-test-spy-rest-server.php'; + require_once $this->wc_tests_dir . '/legacy/includes/wp-http-testcase.php'; + require_once $this->wc_tests_dir . '/legacy/framework/class-wc-unit-test-case.php'; + require_once $this->wc_tests_dir . '/legacy/framework/class-wc-rest-unit-test-case.php'; require_once $this->tests_dir . '/Helpers/AdminNotesHelper.php'; require_once $this->tests_dir . '/Helpers/CouponHelper.php'; From f4e093d52816d3743c36b9b89638dbb45b712935 Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Fri, 8 May 2020 03:20:32 +0530 Subject: [PATCH 3/8] Fix unit test --- unit-tests/Tests/Version3/functions.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/unit-tests/Tests/Version3/functions.php b/unit-tests/Tests/Version3/functions.php index 3f6f96404de..88522c341d7 100644 --- a/unit-tests/Tests/Version3/functions.php +++ b/unit-tests/Tests/Version3/functions.php @@ -82,7 +82,11 @@ class WC_Tests_API_Functions extends WC_Unit_Test_Case { */ public function test_wc_rest_upload_image_from_url_should_return_error_when_invalid_image_is_passed() { // empty file. - $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.'; + 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' ); $this->assertWPError( $result ); From 02aa89614e6b8fcb01a79faf47d05dfd8b0d38d4 Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Fri, 8 May 2020 03:35:25 +0530 Subject: [PATCH 4/8] Applied coding standards --- .../Version2/class-wc-rest-products-v2-controller.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Controllers/Version2/class-wc-rest-products-v2-controller.php b/src/Controllers/Version2/class-wc-rest-products-v2-controller.php index d6942fc2826..a274c88fa3b 100644 --- a/src/Controllers/Version2/class-wc-rest-products-v2-controller.php +++ b/src/Controllers/Version2/class-wc-rest-products-v2-controller.php @@ -419,9 +419,9 @@ class WC_REST_Products_V2_Controller extends WC_REST_CRUD_Controller { $images[] = array( 'id' => 0, 'date_created' => wc_rest_prepare_date_response( current_time( 'mysql' ), false ), // Default to now. - 'date_created_gmt' => wc_rest_prepare_date_response( current_time( 'timestamp', true ) ), // Default to now. + 'date_created_gmt' => wc_rest_prepare_date_response( time() ), // Default to now. 'date_modified' => wc_rest_prepare_date_response( current_time( 'mysql' ), false ), - 'date_modified_gmt' => wc_rest_prepare_date_response( current_time( 'timestamp', true ) ), + 'date_modified_gmt' => wc_rest_prepare_date_response( time() ), 'src' => wc_placeholder_img_src(), 'name' => __( 'Placeholder', 'woocommerce-rest-api' ), 'alt' => __( 'Placeholder', 'woocommerce-rest-api' ), From 569e656459aba212d9c90a415df6e344161ac01a Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Mon, 11 May 2020 14:31:58 +0530 Subject: [PATCH 5/8] Rephrase the test so that is more resilient and error-prone --- unit-tests/Tests/Version3/functions.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/unit-tests/Tests/Version3/functions.php b/unit-tests/Tests/Version3/functions.php index 88522c341d7..3b3b27eb5c9 100644 --- a/unit-tests/Tests/Version3/functions.php +++ b/unit-tests/Tests/Version3/functions.php @@ -82,15 +82,11 @@ class WC_Tests_API_Functions extends WC_Unit_Test_Case { */ public function test_wc_rest_upload_image_from_url_should_return_error_when_invalid_image_is_passed() { // empty file. - 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.'; - } + $expected_error_message = 'Invalid image: File is empty.'; $result = wc_rest_upload_image_from_url( 'http://somedomain.com/invalid-image-1.png' ); $this->assertWPError( $result ); - $this->assertEquals( $expected_error_message, $result->get_error_message() ); + $this->assertStringStartsWith( $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.'; From f7eab9db6e3991360bff54b8e4ffdbd95c82880a Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Thu, 7 May 2020 23:06:33 +0530 Subject: [PATCH 6/8] Enable support trash status for products when its passed --- .../Version2/class-wc-rest-products-v2-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controllers/Version2/class-wc-rest-products-v2-controller.php b/src/Controllers/Version2/class-wc-rest-products-v2-controller.php index a274c88fa3b..f38ecb7dfb9 100644 --- a/src/Controllers/Version2/class-wc-rest-products-v2-controller.php +++ b/src/Controllers/Version2/class-wc-rest-products-v2-controller.php @@ -2115,7 +2115,7 @@ class WC_REST_Products_V2_Controller extends WC_REST_CRUD_Controller { 'default' => 'any', 'description' => __( 'Limit result set to products assigned a specific status.', 'woocommerce-rest-api' ), 'type' => 'string', - 'enum' => array_merge( array( 'any', 'future' ), array_keys( get_post_statuses() ) ), + 'enum' => array_merge( array( 'any', 'future', 'trash' ), array_keys( get_post_statuses() ) ), 'sanitize_callback' => 'sanitize_key', 'validate_callback' => 'rest_validate_request_arg', ); From c792dfb192e4e274fda0520beeef1619f155cfa1 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 16 Jan 2020 14:23:01 -0300 Subject: [PATCH 7/8] Fix unit tests to match new tested method behavior This commit updates two unit tests that were failing (https://travis-ci.org/woocommerce/woocommerce-rest-api/jobs/637678251#L707), after the behavior of the method WC_REST_Shipping_Zones_V2_Controller::register_routes() changed in PR #104. --- unit-tests/Tests/Version2/shipping-zones.php | 2 +- unit-tests/Tests/Version3/shipping-zones.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/unit-tests/Tests/Version2/shipping-zones.php b/unit-tests/Tests/Version2/shipping-zones.php index c6e2c4a102e..1d9ea5368d3 100644 --- a/unit-tests/Tests/Version2/shipping-zones.php +++ b/unit-tests/Tests/Version2/shipping-zones.php @@ -55,7 +55,7 @@ class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case { public function test_register_routes() { $routes = $this->server->get_routes(); $this->assertArrayHasKey( '/wc/v2/shipping/zones', $routes ); - $this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P[\d-]+)', $routes ); + $this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P[\d]+)', $routes ); $this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P[\d]+)/locations', $routes ); $this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P[\d]+)/methods', $routes ); $this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P[\d]+)/methods/(?P[\d]+)', $routes ); diff --git a/unit-tests/Tests/Version3/shipping-zones.php b/unit-tests/Tests/Version3/shipping-zones.php index 580391d1b33..a735357b41f 100644 --- a/unit-tests/Tests/Version3/shipping-zones.php +++ b/unit-tests/Tests/Version3/shipping-zones.php @@ -57,7 +57,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case { public function test_register_routes() { $routes = $this->server->get_routes(); $this->assertArrayHasKey( '/wc/v3/shipping/zones', $routes ); - $this->assertArrayHasKey( '/wc/v3/shipping/zones/(?P[\d-]+)', $routes ); + $this->assertArrayHasKey( '/wc/v3/shipping/zones/(?P[\d]+)', $routes ); $this->assertArrayHasKey( '/wc/v3/shipping/zones/(?P[\d]+)/locations', $routes ); $this->assertArrayHasKey( '/wc/v3/shipping/zones/(?P[\d]+)/methods', $routes ); $this->assertArrayHasKey( '/wc/v3/shipping/zones/(?P[\d]+)/methods/(?P[\d]+)', $routes ); From 7c9fd1ab0c2b7b1c0892cd35ed231a2ef358d86c Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Mon, 11 May 2020 17:37:14 +0530 Subject: [PATCH 8/8] Version update and min requirement bump --- package.json | 2 +- src/Package.php | 2 +- woocommerce-rest-api.php | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index c3842219a49..54554e3a408 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "woocommerce-rest-api", "title": "WooCommerce REST API", - "version": "1.0.5", + "version": "1.0.8", "homepage": "https://woocommerce.com/", "repository": { "type": "git", diff --git a/src/Package.php b/src/Package.php index e524c51bcef..13c5ff30588 100644 --- a/src/Package.php +++ b/src/Package.php @@ -19,7 +19,7 @@ class Package { * * @var string */ - const VERSION = '1.0.7'; + const VERSION = '1.0.8'; /** * Init the package - load the REST API Server class. diff --git a/woocommerce-rest-api.php b/woocommerce-rest-api.php index 296dac73707..10b7ea38a29 100644 --- a/woocommerce-rest-api.php +++ b/woocommerce-rest-api.php @@ -5,8 +5,8 @@ * Description: The WooCommerce core REST API, installed as a feature plugin for development and testing purposes. Requires WooCommerce 3.7+ and PHP 5.3+. * Author: Automattic * Author URI: https://woocommerce.com - * Version: 1.0.7 - * Requires PHP: 5.6 + * Version: 1.0.8 + * Requires PHP: 7.0 * License: GPLv3 * * @package Automattic/WooCommerce/RestApi @@ -15,7 +15,7 @@ defined( 'ABSPATH' ) || exit; -if ( version_compare( PHP_VERSION, '5.6.0', '<' ) ) { +if ( version_compare( PHP_VERSION, '7.0.0', '<' ) ) { return; }