diff --git a/.gitignore b/.gitignore index 390c3cf4ca9..1ce3cf02ae8 100644 --- a/.gitignore +++ b/.gitignore @@ -45,7 +45,8 @@ tests/cli/vendor # Composer /vendor/ -/packages/ +/packages/* +!/packages/README.md contributors.md # Screenshots for e2e tests failures diff --git a/bin/package-update.sh b/bin/package-update.sh index 8f1c34af1a5..dbcb4ab9978 100644 --- a/bin/package-update.sh +++ b/bin/package-update.sh @@ -30,7 +30,7 @@ done # Autoloader output 3 "Updating autoloader classmaps..." -composer dump-autoload --no-dev +composer dump-autoload output 2 "Done" # Convert textdomains diff --git a/composer.json b/composer.json index ff69df8b377..d6105eb6e86 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ "composer/installers": "1.6.0", "woocommerce/woocommerce-rest-api": "dev-test/jetpack-autoloader", "woocommerce/woocommerce-blocks": "dev-build/2.2.0-dev", - "automattic/jetpack-autoloader": "^1" + "automattic/jetpack-autoloader": "1.2.0" }, "require-dev": { "apigen/apigen": "4.1.2", @@ -28,7 +28,10 @@ "exclude-from-classmap": [ "includes/legacy", "includes/libraries" - ] + ], + "psr-4": { + "Automattic\\WooCommerce\\": "src/" + } }, "scripts": { "post-install-cmd": [ diff --git a/composer.lock b/composer.lock index 317087e9408..c6f61c4e02e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,20 +4,20 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e52a74aadbfa3411e205ffd3295a9045", + "content-hash": "d8f9be15c0fa21c448d9727aaf83e7e1", "packages": [ { "name": "automattic/jetpack-autoloader", - "version": "v1.1.0", + "version": "v1.2.0", "source": { "type": "git", "url": "https://github.com/Automattic/jetpack-autoloader.git", - "reference": "cf08e893a481e71feec6e61164efff0eeee98ccd" + "reference": "4ad9631e68e9da8b8a764615766287becfb27f81" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Automattic/jetpack-autoloader/zipball/cf08e893a481e71feec6e61164efff0eeee98ccd", - "reference": "cf08e893a481e71feec6e61164efff0eeee98ccd", + "url": "https://api.github.com/repos/Automattic/jetpack-autoloader/zipball/4ad9631e68e9da8b8a764615766287becfb27f81", + "reference": "4ad9631e68e9da8b8a764615766287becfb27f81", "shasum": "" }, "require": { @@ -40,7 +40,7 @@ "GPL-2.0-or-later" ], "description": "Creates a custom autoloader for a plugin or theme.", - "time": "2019-06-17T13:03:00+00:00" + "time": "2019-06-24T15:13:23+00:00" }, { "name": "composer/installers", diff --git a/includes/class-woocommerce.php b/includes/class-woocommerce.php index d237265de37..181fc6ab96f 100644 --- a/includes/class-woocommerce.php +++ b/includes/class-woocommerce.php @@ -180,7 +180,6 @@ final class WooCommerce { register_shutdown_function( array( $this, 'log_errors' ) ); add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ), -1 ); - add_action( 'plugins_loaded', array( $this, 'init_packages' ) ); add_action( 'after_setup_theme', array( $this, 'setup_environment' ) ); add_action( 'after_setup_theme', array( $this, 'include_template_functions' ), 11 ); add_action( 'init', array( $this, 'init' ), 0 ); @@ -192,21 +191,6 @@ final class WooCommerce { add_action( 'deactivated_plugin', array( $this, 'deactivated_plugin' ) ); } - /** - * Load any WooCommerce packages. - * - * Most packages will have their own init routines, so this is used early once plugins are loaded. - * - * @since 3.7.0 - */ - public function init_packages() { - if ( version_compare( PHP_VERSION, '5.6.0', '<' ) ) { - return; - } - \Automattic\WooCommerce\Blocks\Package::init(); - \Automattic\WooCommerce\RestApi\Package::init(); - } - /** * Ensures fatal errors are logged so they can be picked up in the status report. * diff --git a/packages/README.md b/packages/README.md new file mode 100644 index 00000000000..b1f3d47b6fe --- /dev/null +++ b/packages/README.md @@ -0,0 +1,96 @@ +# External Packages + +This directory holds Composer packages containing functionality developed outside of WooCommerce core. + +## Installing Composer + +You need Composer to use the packages. If you don't have it installed, go and check how to [install Composer](https://github.com/woocommerce/woocommerce/wiki/How-to-set-up-WooCommerce-development-environment) and then continue here. + +## Developing new packages + +To create a package and/or feature plugin for core, you can base your plugin on the example package here: + +https://github.com/woocommerce/woocommerce-example-package + +Packages require a Package class which inits the package and returns version information, and Packages also require that you use the `jetpack-autoloader` package which prevents version conflicts should the same package be used by multiple plugins at once. This is shown in the example package above. + +## Publishing a package + +Your package should be published to Packagist. For example: + +https://packagist.org/packages/woocommerce/woocommerce-example-package + +The package name in this case is `woocommerce/woocommerce-example-package`. + +## Including packages in core + +Edit `composer.json` in the root directory and add the package and package version under the "require" section. For example: + +```json +{ + "name": "woocommerce/woocommerce", + "description": "An eCommerce toolkit that helps you sell anything. Beautifully.", + "homepage": "https://woocommerce.com/", + "type": "wordpress-plugin", + "license": "GPL-3.0-or-later", + "prefer-stable": true, + "minimum-stability": "dev", + "require": { + "composer/installers": "1.6.0", + "woocommerce/woocommerce-rest-api": "dev-test/jetpack-autoloader", + "woocommerce/woocommerce-blocks": "dev-build/2.2.0-dev", + "automattic/jetpack-autoloader": "1.2.0", + "woocommerce/woocommerce-example-package": "1.0.0" + }, + ... +``` + +Next, if your package contains user translatable strings you'll need to edit `bin/package-update.sh` and instruct it to change your package textdomain to the `woocommerce` textdomain. For example: + +``` +find ./packages/woocommerce-example-package -iname '*.php' -exec sed -i.bak -e "s/, 'woocommerce-example-package'/, 'woocommerce'/g" {} \; +``` + +Finally, you will need to tell core to load your package. Edit `src/Packages.php` and add your package to the list of packages there: + +```php + protected static $packages = [ + 'woocommerce-blocks' => '\\Automattic\\WooCommerce\\Blocks\\Package', + 'woocommerce-rest-api' => '\\Automattic\\WooCommerce\\RestApi\\Package', + 'woocommerce-example-package' => '\\Automattic\\WooCommerce\\ExamplePackage\\Package', + ]; +``` + +You can add tests to ensure your package is loaded to the WooCommerce unit-tests. Some tests exist in `unit-tests/tests/packages/packages.php` which you can use as an example. + +## Installing packages + +Once you have defined your package requirements, run + +``` +composer install +``` + +and that will install the required Composer packages. + +### Using packages + +To use something from a package, you have to declare it at the top of the file before any other instruction, and then use it in the code. For example: + +```php +use Automattic\WooCommerce\ExamplePackage\ExampleClass; + +// other code... + +$class = new ExampleClass(); +``` + +If you need to rule out conflicts, you can alias it: + +```php +use Automattic\WooCommerce\ExamplePackage\ExampleClass as Example_Class_Alias; + +// other code... + +$class = new Example_Class_Alias(); +``` diff --git a/phpcs.xml b/phpcs.xml index c0fbbb1f0b1..83eb0fd1a52 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -34,6 +34,7 @@ includes/**/abstract-*.php tests/* + src/* @@ -46,5 +47,6 @@ i18n/ + src/ diff --git a/src/Autoloader.php b/src/Autoloader.php new file mode 100644 index 00000000000..df56062b10e --- /dev/null +++ b/src/Autoloader.php @@ -0,0 +1,71 @@ + +
+

+ ', + '' + ); + ?> +

+
+ '\\Automattic\\WooCommerce\\Blocks\\Package', + 'woocommerce-rest-api' => '\\Automattic\\WooCommerce\\RestApi\\Package', + ]; + + /** + * Init the package loader. + * + * @since 3.7.0 + */ + public static function init() { + add_action( 'plugins_loaded', array( __CLASS__, 'on_init' ) ); + } + + /** + * Callback for WordPress init hook. + */ + public static function on_init() { + self::load_packages(); + } + + /** + * Checks a package exists by looking for it's directory. + * + * @param string $package Package name. + * @return boolean + */ + public static function package_exists( $package ) { + return file_exists( dirname( __DIR__ ) . '/packages/' . $package ); + } + + /** + * Loads packages after plugins_loaded hook. + * + * Each package should include an init file which loads the package so it can be used by core. + */ + protected static function load_packages() { + foreach ( self::$packages as $package_name => $package_class ) { + if ( ! self::package_exists( $package_name ) ) { + self::missing_package( $package_name ); + continue; + } + call_user_func( [ $package_class, 'init' ] ); + } + } + + /** + * If a package is missing, add an admin notice. + * + * @param string $package Package name. + */ + protected static function missing_package( $package ) { + if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { + error_log( // phpcs:ignore + sprintf( + /* Translators: %s package name. */ + esc_html__( 'Missing the WooCommerce %s package', 'woocommerce' ), + '' . esc_html( $package ) . '' + ) . ' - ' . esc_html__( 'Your installation of WooCommerce is incomplete. If you installed WooCommerce from GitHub, please refer to this document to set up your development environment: https://github.com/woocommerce/woocommerce/wiki/How-to-set-up-WooCommerce-development-environment', 'woocommerce' ) + ); + } + add_action( + 'admin_notices', + function() use ( $package ) { + ?> +
+

+ + ' . esc_html( $package ) . '' + ); + ?> + +
+ ', + '' + ); + ?> +

+
+ endpoint = new WC_REST_Coupons_Controller(); - $this->user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * @since 3.5.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v3/coupons', $routes ); - $this->assertArrayHasKey( '/wc/v3/coupons/(?P[\d]+)', $routes ); - $this->assertArrayHasKey( '/wc/v3/coupons/batch', $routes ); - } - - /** - * Test getting coupons. - * @since 3.5.0 - */ - public function test_get_coupons() { - wp_set_current_user( $this->user ); - - $coupon_1 = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' ); - $post_1 = get_post( $coupon_1->get_id() ); - $coupon_2 = WC_Helper_Coupon::create_coupon( 'dummycoupon-2' ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/coupons' ) ); - $coupons = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 2, count( $coupons ) ); - $this->assertContains( - array( - 'id' => $coupon_1->get_id(), - 'code' => 'dummycoupon-1', - 'amount' => '1.00', - 'date_created' => wc_rest_prepare_date_response( $post_1->post_date_gmt, false ), - 'date_created_gmt' => wc_rest_prepare_date_response( $post_1->post_date_gmt ), - 'date_modified' => wc_rest_prepare_date_response( $post_1->post_modified_gmt, false ), - 'date_modified_gmt' => wc_rest_prepare_date_response( $post_1->post_modified_gmt ), - 'discount_type' => 'fixed_cart', - 'description' => 'This is a dummy coupon', - 'date_expires' => '', - 'date_expires_gmt' => '', - 'usage_count' => 0, - 'individual_use' => false, - 'product_ids' => array(), - 'excluded_product_ids' => array(), - 'usage_limit' => '', - 'usage_limit_per_user' => '', - 'limit_usage_to_x_items' => null, - 'free_shipping' => false, - 'product_categories' => array(), - 'excluded_product_categories' => array(), - 'exclude_sale_items' => false, - 'minimum_amount' => '0.00', - 'maximum_amount' => '0.00', - 'email_restrictions' => array(), - 'used_by' => array(), - 'meta_data' => array(), - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v3/coupons/' . $coupon_1->get_id() ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v3/coupons' ), - ), - ), - ), - ), - $coupons - ); - } - - /** - * Test getting coupons without valid permissions. - * @since 3.5.0 - */ - public function test_get_coupons_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/coupons' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test getting a single coupon. - * @since 3.5.0 - */ - public function test_get_coupon() { - wp_set_current_user( $this->user ); - $coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' ); - $post = get_post( $coupon->get_id() ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/coupons/' . $coupon->get_id() ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( - array( - 'id' => $coupon->get_id(), - 'code' => 'dummycoupon-1', - 'amount' => '1.00', - 'date_created' => wc_rest_prepare_date_response( $post->post_date_gmt, false ), - 'date_created_gmt' => wc_rest_prepare_date_response( $post->post_date_gmt ), - 'date_modified' => wc_rest_prepare_date_response( $post->post_modified_gmt, false ), - 'date_modified_gmt' => wc_rest_prepare_date_response( $post->post_modified_gmt ), - 'discount_type' => 'fixed_cart', - 'description' => 'This is a dummy coupon', - 'date_expires' => null, - 'date_expires_gmt' => null, - 'usage_count' => 0, - 'individual_use' => false, - 'product_ids' => array(), - 'excluded_product_ids' => array(), - 'usage_limit' => null, - 'usage_limit_per_user' => null, - 'limit_usage_to_x_items' => null, - 'free_shipping' => false, - 'product_categories' => array(), - 'excluded_product_categories' => array(), - 'exclude_sale_items' => false, - 'minimum_amount' => '0.00', - 'maximum_amount' => '0.00', - 'email_restrictions' => array(), - 'used_by' => array(), - 'meta_data' => array(), - ), - $data - ); - } - - /** - * Test getting a single coupon with an invalid ID. - * @since 3.5.0 - */ - public function test_get_coupon_invalid_id() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/coupons/0' ) ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test getting a single coupon without valid permissions. - * @since 3.5.0 - */ - public function test_get_coupon_without_permission() { - wp_set_current_user( 0 ); - $coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/coupons/' . $coupon->get_id() ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test creating a single coupon. - * @since 3.5.0 - */ - public function test_create_coupon() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'POST', '/wc/v3/coupons' ); - $request->set_body_params( - array( - 'code' => 'test', - 'amount' => '5.00', - 'discount_type' => 'fixed_product', - 'description' => 'Test', - 'usage_limit' => 10, - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 201, $response->get_status() ); - $this->assertEquals( - array( - 'id' => $data['id'], - 'code' => 'test', - 'amount' => '5.00', - 'date_created' => $data['date_created'], - 'date_created_gmt' => $data['date_created_gmt'], - 'date_modified' => $data['date_modified'], - 'date_modified_gmt' => $data['date_modified_gmt'], - 'discount_type' => 'fixed_product', - 'description' => 'Test', - 'date_expires' => null, - 'date_expires_gmt' => null, - 'usage_count' => 0, - 'individual_use' => false, - 'product_ids' => array(), - 'excluded_product_ids' => array(), - 'usage_limit' => 10, - 'usage_limit_per_user' => null, - 'limit_usage_to_x_items' => null, - 'free_shipping' => false, - 'product_categories' => array(), - 'excluded_product_categories' => array(), - 'exclude_sale_items' => false, - 'minimum_amount' => '0.00', - 'maximum_amount' => '0.00', - 'email_restrictions' => array(), - 'used_by' => array(), - 'meta_data' => array(), - ), - $data - ); - } - - /** - * Test creating a single coupon with invalid fields. - * @since 3.5.0 - */ - public function test_create_coupon_invalid_fields() { - wp_set_current_user( $this->user ); - - // test no code... - $request = new WP_REST_Request( 'POST', '/wc/v3/coupons' ); - $request->set_body_params( - array( - 'amount' => '5.00', - 'discount_type' => 'fixed_product', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 400, $response->get_status() ); - } - - /** - * Test creating a single coupon without valid permissions. - * @since 3.5.0 - */ - public function test_create_coupon_without_permission() { - wp_set_current_user( 0 ); - - // test no code... - $request = new WP_REST_Request( 'POST', '/wc/v3/coupons' ); - $request->set_body_params( - array( - 'code' => 'fail', - 'amount' => '5.00', - 'discount_type' => 'fixed_product', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test updating a single coupon. - * @since 3.5.0 - */ - public function test_update_coupon() { - wp_set_current_user( $this->user ); - $coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' ); - $post = get_post( $coupon->get_id() ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/coupons/' . $coupon->get_id() ) ); - $data = $response->get_data(); - $this->assertEquals( 'This is a dummy coupon', $data['description'] ); - $this->assertEquals( 'fixed_cart', $data['discount_type'] ); - $this->assertEquals( '1.00', $data['amount'] ); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/coupons/' . $coupon->get_id() ); - $request->set_body_params( - array( - 'amount' => '10.00', - 'description' => 'New description', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( '10.00', $data['amount'] ); - $this->assertEquals( 'New description', $data['description'] ); - $this->assertEquals( 'fixed_cart', $data['discount_type'] ); - } - - /** - * Test updating a single coupon with an invalid ID. - * @since 3.5.0 - */ - public function test_update_coupon_invalid_id() { - wp_set_current_user( $this->user ); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/coupons/0' ); - $request->set_body_params( - array( - 'code' => 'tester', - 'amount' => '10.00', - 'description' => 'New description', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 400, $response->get_status() ); - } - - /** - * Test updating a single coupon without valid permissions. - * @since 3.5.0 - */ - public function test_update_coupon_without_permission() { - wp_set_current_user( 0 ); - $coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' ); - $post = get_post( $coupon->get_id() ); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/coupons/' . $coupon->get_id() ); - $request->set_body_params( - array( - 'amount' => '10.00', - 'description' => 'New description', - ) - ); - $response = $this->server->dispatch( $request ); - - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test deleting a single coupon. - * @since 3.5.0 - */ - public function test_delete_coupon() { - wp_set_current_user( $this->user ); - $coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' ); - $request = new WP_REST_Request( 'DELETE', '/wc/v3/coupons/' . $coupon->get_id() ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - } - - /** - * Test deleting a single coupon with an invalid ID. - * @since 3.5.0 - */ - public function test_delete_coupon_invalid_id() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'DELETE', '/wc/v3/coupons/0' ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test deleting a single coupon without valid permissions. - * @since 3.5.0 - */ - public function test_delete_coupon_without_permission() { - wp_set_current_user( 0 ); - $coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' ); - $request = new WP_REST_Request( 'DELETE', '/wc/v3/coupons/' . $coupon->get_id() ); - $response = $this->server->dispatch( $request ); - - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test batch operations on coupons. - * @since 3.5.0 - */ - public function test_batch_coupon() { - wp_set_current_user( $this->user ); - - $coupon_1 = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' ); - $coupon_2 = WC_Helper_Coupon::create_coupon( 'dummycoupon-2' ); - $coupon_3 = WC_Helper_Coupon::create_coupon( 'dummycoupon-3' ); - $coupon_4 = WC_Helper_Coupon::create_coupon( 'dummycoupon-4' ); - - $request = new WP_REST_Request( 'POST', '/wc/v3/coupons/batch' ); - $request->set_body_params( - array( - 'update' => array( - array( - 'id' => $coupon_1->get_id(), - 'amount' => '5.15', - ), - ), - 'delete' => array( - $coupon_2->get_id(), - $coupon_3->get_id(), - ), - 'create' => array( - array( - 'code' => 'new-coupon', - 'amount' => '11.00', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( '5.15', $data['update'][0]['amount'] ); - $this->assertEquals( '11.00', $data['create'][0]['amount'] ); - $this->assertEquals( 'new-coupon', $data['create'][0]['code'] ); - $this->assertEquals( $coupon_2->get_id(), $data['delete'][0]['id'] ); - $this->assertEquals( $coupon_3->get_id(), $data['delete'][1]['id'] ); - - $request = new WP_REST_Request( 'GET', '/wc/v3/coupons' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 3, count( $data ) ); - } - - /** - * Test coupon schema. - * @since 3.5.0 - */ - public function test_coupon_schema() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/coupons' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 27, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'code', $properties ); - $this->assertArrayHasKey( 'date_created', $properties ); - $this->assertArrayHasKey( 'date_created_gmt', $properties ); - $this->assertArrayHasKey( 'date_modified', $properties ); - $this->assertArrayHasKey( 'date_modified_gmt', $properties ); - $this->assertArrayHasKey( 'description', $properties ); - $this->assertArrayHasKey( 'discount_type', $properties ); - $this->assertArrayHasKey( 'amount', $properties ); - $this->assertArrayHasKey( 'date_expires', $properties ); - $this->assertArrayHasKey( 'date_expires_gmt', $properties ); - $this->assertArrayHasKey( 'usage_count', $properties ); - $this->assertArrayHasKey( 'individual_use', $properties ); - $this->assertArrayHasKey( 'product_ids', $properties ); - $this->assertArrayHasKey( 'excluded_product_ids', $properties ); - $this->assertArrayHasKey( 'usage_limit', $properties ); - $this->assertArrayHasKey( 'usage_limit_per_user', $properties ); - $this->assertArrayHasKey( 'limit_usage_to_x_items', $properties ); - $this->assertArrayHasKey( 'free_shipping', $properties ); - $this->assertArrayHasKey( 'product_categories', $properties ); - $this->assertArrayHasKey( 'excluded_product_categories', $properties ); - $this->assertArrayHasKey( 'exclude_sale_items', $properties ); - $this->assertArrayHasKey( 'minimum_amount', $properties ); - $this->assertArrayHasKey( 'maximum_amount', $properties ); - $this->assertArrayHasKey( 'email_restrictions', $properties ); - $this->assertArrayHasKey( 'used_by', $properties ); - } -} diff --git a/tests/unit-tests/api/customers.php b/tests/unit-tests/api/customers.php deleted file mode 100644 index 5995045bb85..00000000000 --- a/tests/unit-tests/api/customers.php +++ /dev/null @@ -1,634 +0,0 @@ -endpoint = new WC_REST_Customers_Controller(); - } - - /** - * Test route registration. - * - * @since 3.5.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - - $this->assertArrayHasKey( '/wc/v3/customers', $routes ); - $this->assertArrayHasKey( '/wc/v3/customers/(?P[\d]+)', $routes ); - $this->assertArrayHasKey( '/wc/v3/customers/batch', $routes ); - } - - /** - * Test getting customers. - * - * @since 3.5.0 - */ - public function test_get_customers() { - wp_set_current_user( 1 ); - - $customer_1 = WC_Helper_Customer::create_customer(); - WC_Helper_Customer::create_customer( 'test2', 'test2', 'test2@woo.local' ); - - $request = new WP_REST_Request( 'GET', '/wc/v3/customers' ); - $request->set_query_params( - array( - 'orderby' => 'id', - ) - ); - $response = $this->server->dispatch( $request ); - $customers = $response->get_data(); - $date_created = get_date_from_gmt( date( 'Y-m-d H:i:s', strtotime( $customer_1->get_date_created() ) ) ); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 2, count( $customers ) ); - - $this->assertContains( - array( - 'id' => $customer_1->get_id(), - 'date_created' => wc_rest_prepare_date_response( $date_created, false ), - 'date_created_gmt' => wc_rest_prepare_date_response( $date_created ), - 'date_modified' => wc_rest_prepare_date_response( $customer_1->get_date_modified(), false ), - 'date_modified_gmt' => wc_rest_prepare_date_response( $customer_1->get_date_modified() ), - 'email' => 'test@woo.local', - 'first_name' => 'Justin', - 'last_name' => '', - 'role' => 'customer', - 'username' => 'testcustomer', - 'billing' => array( - 'first_name' => '', - 'last_name' => '', - 'company' => '', - 'address_1' => '123 South Street', - 'address_2' => 'Apt 1', - 'city' => 'Philadelphia', - 'state' => 'PA', - 'postcode' => '19123', - 'country' => 'US', - 'email' => '', - 'phone' => '', - ), - 'shipping' => array( - 'first_name' => '', - 'last_name' => '', - 'company' => '', - 'address_1' => '123 South Street', - 'address_2' => 'Apt 1', - 'city' => 'Philadelphia', - 'state' => 'PA', - 'postcode' => '19123', - 'country' => 'US', - ), - 'is_paying_customer' => false, - 'avatar_url' => $customer_1->get_avatar_url(), - 'meta_data' => array(), - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v3/customers/' . $customer_1->get_id() . '' ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v3/customers' ), - ), - ), - ), - ), - $customers - ); - - update_option( 'timezone_tring', 'America/New York' ); - $customer_3 = WC_Helper_Customer::create_customer( 'timezonetest', 'timezonetest', 'timezonetest@woo.local' ); - - $request = new WP_REST_Request( 'GET', '/wc/v3/customers' ); - $request->set_query_params( - array( - 'orderby' => 'id', - ) - ); - $response = $this->server->dispatch( $request ); - $customers = $response->get_data(); - $date_created = get_date_from_gmt( date( 'Y-m-d H:i:s', strtotime( $customer_3->get_date_created() ) ) ); - - $this->assertEquals( 200, $response->get_status() ); - - $this->assertContains( - array( - 'id' => $customer_3->get_id(), - 'date_created' => wc_rest_prepare_date_response( $date_created, false ), - 'date_created_gmt' => wc_rest_prepare_date_response( $date_created ), - 'date_modified' => wc_rest_prepare_date_response( $customer_3->get_date_modified(), false ), - 'date_modified_gmt' => wc_rest_prepare_date_response( $customer_3->get_date_modified() ), - 'email' => 'timezonetest@woo.local', - 'first_name' => 'Justin', - 'last_name' => '', - 'role' => 'customer', - 'username' => 'timezonetest', - 'billing' => array( - 'first_name' => '', - 'last_name' => '', - 'company' => '', - 'address_1' => '123 South Street', - 'address_2' => 'Apt 1', - 'city' => 'Philadelphia', - 'state' => 'PA', - 'postcode' => '19123', - 'country' => 'US', - 'email' => '', - 'phone' => '', - ), - 'shipping' => array( - 'first_name' => '', - 'last_name' => '', - 'company' => '', - 'address_1' => '123 South Street', - 'address_2' => 'Apt 1', - 'city' => 'Philadelphia', - 'state' => 'PA', - 'postcode' => '19123', - 'country' => 'US', - ), - 'is_paying_customer' => false, - 'avatar_url' => $customer_3->get_avatar_url(), - 'meta_data' => array(), - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v3/customers/' . $customer_3->get_id() . '' ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v3/customers' ), - ), - ), - ), - ), - $customers - ); - - } - - /** - * Test getting customers without valid permissions. - * - * @since 3.5.0 - */ - public function test_get_customers_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test creating a new customer. - * - * @since 3.5.0 - */ - public function test_create_customer() { - wp_set_current_user( 1 ); - - // Test just the basics first.. - $request = new WP_REST_Request( 'POST', '/wc/v3/customers' ); - $request->set_body_params( - array( - 'username' => 'create_customer_test', - 'password' => 'test123', - 'email' => 'create_customer_test@woo.local', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 201, $response->get_status() ); - $this->assertEquals( - array( - 'id' => $data['id'], - 'date_created' => $data['date_created'], - 'date_created_gmt' => $data['date_created_gmt'], - 'date_modified' => $data['date_modified'], - 'date_modified_gmt' => $data['date_modified_gmt'], - 'email' => 'create_customer_test@woo.local', - 'first_name' => '', - 'last_name' => '', - 'role' => 'customer', - 'username' => 'create_customer_test', - 'billing' => array( - 'first_name' => '', - 'last_name' => '', - 'company' => '', - 'address_1' => '', - 'address_2' => '', - 'city' => '', - 'state' => '', - 'postcode' => '', - 'country' => '', - 'email' => '', - 'phone' => '', - ), - 'shipping' => array( - 'first_name' => '', - 'last_name' => '', - 'company' => '', - 'address_1' => '', - 'address_2' => '', - 'city' => '', - 'state' => '', - 'postcode' => '', - 'country' => '', - ), - 'is_paying_customer' => false, - 'meta_data' => array(), - 'avatar_url' => $data['avatar_url'], - ), - $data - ); - - // Test extra data. - $request = new WP_REST_Request( 'POST', '/wc/v3/customers' ); - $request->set_body_params( - array( - 'username' => 'create_customer_test2', - 'password' => 'test123', - 'email' => 'create_customer_test2@woo.local', - 'first_name' => 'Test', - 'last_name' => 'McTestFace', - 'billing' => array( - 'country' => 'US', - 'state' => 'WA', - ), - 'shipping' => array( - 'state' => 'CA', - 'country' => 'US', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 201, $response->get_status() ); - $this->assertEquals( - array( - 'id' => $data['id'], - 'date_created' => $data['date_created'], - 'date_created_gmt' => $data['date_created_gmt'], - 'date_modified' => $data['date_modified'], - 'date_modified_gmt' => $data['date_modified_gmt'], - 'email' => 'create_customer_test2@woo.local', - 'first_name' => 'Test', - 'last_name' => 'McTestFace', - 'role' => 'customer', - 'username' => 'create_customer_test2', - 'billing' => array( - 'first_name' => '', - 'last_name' => '', - 'company' => '', - 'address_1' => '', - 'address_2' => '', - 'city' => '', - 'state' => 'WA', - 'postcode' => '', - 'country' => 'US', - 'email' => '', - 'phone' => '', - ), - 'shipping' => array( - 'first_name' => '', - 'last_name' => '', - 'company' => '', - 'address_1' => '', - 'address_2' => '', - 'city' => '', - 'state' => 'CA', - 'postcode' => '', - 'country' => 'US', - ), - 'is_paying_customer' => false, - 'meta_data' => array(), - 'avatar_url' => $data['avatar_url'], - ), - $data - ); - - // Test without required field. - $request = new WP_REST_Request( 'POST', '/wc/v3/customers' ); - $request->set_body_params( - array( - 'username' => 'create_customer_test3', - 'first_name' => 'Test', - 'last_name' => 'McTestFace', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 400, $response->get_status() ); - } - - /** - * Test creating customers without valid permissions. - * - * @since 3.5.0 - */ - public function test_create_customer_without_permission() { - wp_set_current_user( 0 ); - $request = new WP_REST_Request( 'POST', '/wc/v3/customers' ); - $request->set_body_params( - array( - 'username' => 'create_customer_test_without_permission', - 'password' => 'test123', - 'email' => 'create_customer_test_without_permission@woo.local', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test getting a single customer. - * - * @since 3.5.0 - */ - public function test_get_customer() { - wp_set_current_user( 1 ); - $customer = WC_Helper_Customer::create_customer( 'get_customer_test', 'test123', 'get_customer_test@woo.local' ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/' . $customer->get_id() ) ); - $data = $response->get_data(); - - $this->assertEquals( - array( - 'id' => $data['id'], - 'date_created' => $data['date_created'], - 'date_created_gmt' => $data['date_created_gmt'], - 'date_modified' => $data['date_modified'], - 'date_modified_gmt' => $data['date_modified_gmt'], - 'email' => 'get_customer_test@woo.local', - 'first_name' => 'Justin', - 'billing' => array( - 'first_name' => '', - 'last_name' => '', - 'company' => '', - 'address_1' => '123 South Street', - 'address_2' => 'Apt 1', - 'city' => 'Philadelphia', - 'state' => 'PA', - 'postcode' => '19123', - 'country' => 'US', - 'email' => '', - 'phone' => '', - ), - 'shipping' => array( - 'first_name' => '', - 'last_name' => '', - 'company' => '', - 'address_1' => '123 South Street', - 'address_2' => 'Apt 1', - 'city' => 'Philadelphia', - 'state' => 'PA', - 'postcode' => '19123', - 'country' => 'US', - ), - 'is_paying_customer' => false, - 'meta_data' => array(), - 'last_name' => '', - 'role' => 'customer', - 'username' => 'get_customer_test', - 'avatar_url' => $data['avatar_url'], - ), - $data - ); - } - - /** - * Test getting a single customer without valid permissions. - * - * @since 3.5.0 - */ - public function test_get_customer_without_permission() { - wp_set_current_user( 0 ); - $customer = WC_Helper_Customer::create_customer( 'get_customer_test_without_permission', 'test123', 'get_customer_test_without_permission@woo.local' ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/' . $customer->get_id() ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test getting a single customer with an invalid ID. - * - * @since 3.5.0 - */ - public function test_get_customer_invalid_id() { - wp_set_current_user( 1 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/0' ) ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test updating a customer. - * - * @since 3.5.0 - */ - public function test_update_customer() { - wp_set_current_user( 1 ); - $customer = WC_Helper_Customer::create_customer( 'update_customer_test', 'test123', 'update_customer_test@woo.local' ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/' . $customer->get_id() ) ); - $data = $response->get_data(); - $this->assertEquals( 'update_customer_test', $data['username'] ); - $this->assertEquals( 'update_customer_test@woo.local', $data['email'] ); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/customers/' . $customer->get_id() ); - $request->set_body_params( - array( - 'email' => 'updated_email@woo.local', - 'first_name' => 'UpdatedTest', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'updated_email@woo.local', $data['email'] ); - $this->assertEquals( 'UpdatedTest', $data['first_name'] ); - } - - /** - * Test updating a customer without valid permissions. - * - * @since 3.5.0 - */ - public function test_update_customer_without_permission() { - wp_set_current_user( 0 ); - $customer = WC_Helper_Customer::create_customer( 'update_customer_test_without_permission', 'test123', 'update_customer_test_without_permission@woo.local' ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/' . $customer->get_id() ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test updating a customer with an invalid ID. - * - * @since 3.5.0 - */ - public function test_update_customer_invalid_id() { - wp_set_current_user( 1 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/0' ) ); - $this->assertEquals( 404, $response->get_status() ); - } - - - /** - * Test deleting a customer. - * - * @since 3.5.0 - */ - public function test_delete_customer() { - wp_set_current_user( 1 ); - $customer = WC_Helper_Customer::create_customer( 'delete_customer_test', 'test123', 'delete_customer_test@woo.local' ); - $request = new WP_REST_Request( 'DELETE', '/wc/v3/customers/' . $customer->get_id() ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - } - - /** - * Test deleting a customer with an invalid ID. - * - * @since 3.5.0 - */ - public function test_delete_customer_invalid_id() { - wp_set_current_user( 1 ); - $request = new WP_REST_Request( 'DELETE', '/wc/v3/customers/0' ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - } - - /** - * Test deleting a customer without valid permissions. - * - * @since 3.5.0 - */ - public function test_delete_customer_without_permission() { - wp_set_current_user( 0 ); - $customer = WC_Helper_Customer::create_customer( 'delete_customer_test_without_permission', 'test123', 'delete_customer_test_without_permission@woo.local' ); - $request = new WP_REST_Request( 'DELETE', '/wc/v3/customers/' . $customer->get_id() ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test customer batch endpoint. - * - * @since 3.5.0 - */ - public function test_batch_customer() { - wp_set_current_user( 1 ); - - $customer_1 = WC_Helper_Customer::create_customer( 'test_batch_customer', 'test123', 'test_batch_customer@woo.local' ); - $customer_2 = WC_Helper_Customer::create_customer( 'test_batch_customer2', 'test123', 'test_batch_customer2@woo.local' ); - $customer_3 = WC_Helper_Customer::create_customer( 'test_batch_customer3', 'test123', 'test_batch_customer3@woo.local' ); - $customer_4 = WC_Helper_Customer::create_customer( 'test_batch_customer4', 'test123', 'test_batch_customer4@woo.local' ); - - $request = new WP_REST_Request( 'POST', '/wc/v3/customers/batch' ); - $request->set_body_params( - array( - 'update' => array( - array( - 'id' => $customer_1->get_id(), - 'last_name' => 'McTest', - ), - ), - 'delete' => array( - $customer_2->get_id(), - $customer_3->get_id(), - ), - 'create' => array( - array( - 'username' => 'newuser', - 'password' => 'test123', - 'email' => 'newuser@woo.local', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'McTest', $data['update'][0]['last_name'] ); - $this->assertEquals( 'newuser', $data['create'][0]['username'] ); - $this->assertEmpty( $data['create'][0]['last_name'] ); - $this->assertEquals( $customer_2->get_id(), $data['delete'][0]['id'] ); - $this->assertEquals( $customer_3->get_id(), $data['delete'][1]['id'] ); - - $request = new WP_REST_Request( 'GET', '/wc/v3/customers' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 3, count( $data ) ); - } - - /** - * Test customer schema. - * - * @since 3.5.0 - */ - public function test_customer_schema() { - wp_set_current_user( 1 ); - $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/customers' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 16, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'date_created', $properties ); - $this->assertArrayHasKey( 'date_created_gmt', $properties ); - $this->assertArrayHasKey( 'date_modified', $properties ); - $this->assertArrayHasKey( 'date_modified_gmt', $properties ); - $this->assertArrayHasKey( 'email', $properties ); - $this->assertArrayHasKey( 'first_name', $properties ); - $this->assertArrayHasKey( 'last_name', $properties ); - $this->assertArrayHasKey( 'role', $properties ); - $this->assertArrayHasKey( 'username', $properties ); - $this->assertArrayHasKey( 'password', $properties ); - $this->assertArrayHasKey( 'avatar_url', $properties ); - $this->assertArrayHasKey( 'billing', $properties ); - $this->assertArrayHasKey( 'first_name', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'last_name', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'company', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'address_1', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'address_2', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'city', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'state', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'postcode', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'country', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'email', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'phone', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'shipping', $properties ); - $this->assertArrayHasKey( 'first_name', $properties['shipping']['properties'] ); - $this->assertArrayHasKey( 'last_name', $properties['shipping']['properties'] ); - $this->assertArrayHasKey( 'company', $properties['shipping']['properties'] ); - $this->assertArrayHasKey( 'address_1', $properties['shipping']['properties'] ); - $this->assertArrayHasKey( 'address_2', $properties['shipping']['properties'] ); - $this->assertArrayHasKey( 'city', $properties['shipping']['properties'] ); - $this->assertArrayHasKey( 'state', $properties['shipping']['properties'] ); - $this->assertArrayHasKey( 'postcode', $properties['shipping']['properties'] ); - $this->assertArrayHasKey( 'country', $properties['shipping']['properties'] ); - } -} diff --git a/tests/unit-tests/api/orders.php b/tests/unit-tests/api/orders.php deleted file mode 100644 index d34a97bb80d..00000000000 --- a/tests/unit-tests/api/orders.php +++ /dev/null @@ -1,665 +0,0 @@ -endpoint = new WC_REST_Orders_Controller(); - $this->user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * @since 3.5.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v3/orders', $routes ); - $this->assertArrayHasKey( '/wc/v3/orders/batch', $routes ); - $this->assertArrayHasKey( '/wc/v3/orders/(?P[\d]+)', $routes ); - } - - /** - * Test getting all orders. - * @since 3.5.0 - */ - public function test_get_items() { - wp_set_current_user( $this->user ); - - // Create 10 orders. - for ( $i = 0; $i < 10; $i++ ) { - $this->orders[] = WC_Helper_Order::create_order( $this->user ); - } - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders' ) ); - $orders = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 10, count( $orders ) ); - } - - /** - * Tests to make sure orders cannot be viewed without valid permissions. - * - * @since 3.5.0 - */ - public function test_get_items_without_permission() { - wp_set_current_user( 0 ); - $this->orders[] = WC_Helper_Order::create_order(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Tests getting a single order. - * @since 3.5.0 - */ - public function test_get_item() { - wp_set_current_user( $this->user ); - $order = WC_Helper_Order::create_order(); - $order->add_meta_data( 'key', 'value' ); - $order->add_meta_data( 'key2', 'value2' ); - $order->save(); - $this->orders[] = $order; - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders/' . $order->get_id() ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( $order->get_id(), $data['id'] ); - - // Test meta data is set. - $this->assertEquals( 'key', $data['meta_data'][0]->key ); - $this->assertEquals( 'value', $data['meta_data'][0]->value ); - $this->assertEquals( 'key2', $data['meta_data'][1]->key ); - $this->assertEquals( 'value2', $data['meta_data'][1]->value ); - } - - /** - * Tests getting a single order without the correct permissions. - * @since 3.5.0 - */ - public function test_get_item_without_permission() { - wp_set_current_user( 0 ); - $order = WC_Helper_Order::create_order(); - $this->orders[] = $order; - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders/' . $order->get_id() ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Tests getting an order with an invalid ID. - * @since 3.5.0 - */ - public function test_get_item_invalid_id() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders/99999999' ) ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Tests getting an order with an invalid ID. - * @since 3.5.0 - */ - public function test_get_item_refund_id() { - wp_set_current_user( $this->user ); - $order = WC_Helper_Order::create_order(); - $refund = wc_create_refund( - array( - 'order_id' => $order->get_id(), - ) - ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders/' . $refund->get_id() ) ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Tests creating an order. - * @since 3.5.0 - */ - public function test_create_order() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'POST', '/wc/v3/orders' ); - $request->set_body_params( - array( - 'payment_method' => 'bacs', - 'payment_method_title' => 'Direct Bank Transfer', - 'set_paid' => true, - 'billing' => array( - 'first_name' => 'John', - 'last_name' => 'Doe', - 'address_1' => '969 Market', - 'address_2' => '', - 'city' => 'San Francisco', - 'state' => 'CA', - 'postcode' => '94103', - 'country' => 'US', - 'email' => 'john.doe@example.com', - 'phone' => '(555) 555-5555', - ), - 'shipping' => array( - 'first_name' => 'John', - 'last_name' => 'Doe', - 'address_1' => '969 Market', - 'address_2' => '', - 'city' => 'San Francisco', - 'state' => 'CA', - 'postcode' => '94103', - 'country' => 'US', - ), - 'line_items' => array( - array( - 'product_id' => $product->get_id(), - 'quantity' => 2, - ), - ), - 'shipping_lines' => array( - array( - 'method_id' => 'flat_rate', - 'method_title' => 'Flat rate', - 'total' => '10', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $order = wc_get_order( $data['id'] ); - $this->assertEquals( 201, $response->get_status() ); - $this->assertEquals( $order->get_payment_method(), $data['payment_method'] ); - $this->assertEquals( $order->get_payment_method_title(), $data['payment_method_title'] ); - $this->assertEquals( $order->get_billing_first_name(), $data['billing']['first_name'] ); - $this->assertEquals( $order->get_billing_last_name(), $data['billing']['last_name'] ); - $this->assertEquals( '', $data['billing']['company'] ); - $this->assertEquals( $order->get_billing_address_1(), $data['billing']['address_1'] ); - $this->assertEquals( $order->get_billing_address_2(), $data['billing']['address_2'] ); - $this->assertEquals( $order->get_billing_city(), $data['billing']['city'] ); - $this->assertEquals( $order->get_billing_state(), $data['billing']['state'] ); - $this->assertEquals( $order->get_billing_postcode(), $data['billing']['postcode'] ); - $this->assertEquals( $order->get_billing_country(), $data['billing']['country'] ); - $this->assertEquals( $order->get_billing_email(), $data['billing']['email'] ); - $this->assertEquals( $order->get_billing_phone(), $data['billing']['phone'] ); - $this->assertEquals( $order->get_shipping_first_name(), $data['shipping']['first_name'] ); - $this->assertEquals( $order->get_shipping_last_name(), $data['shipping']['last_name'] ); - $this->assertEquals( '', $data['shipping']['company'] ); - $this->assertEquals( $order->get_shipping_address_1(), $data['shipping']['address_1'] ); - $this->assertEquals( $order->get_shipping_address_2(), $data['shipping']['address_2'] ); - $this->assertEquals( $order->get_shipping_city(), $data['shipping']['city'] ); - $this->assertEquals( $order->get_shipping_state(), $data['shipping']['state'] ); - $this->assertEquals( $order->get_shipping_postcode(), $data['shipping']['postcode'] ); - $this->assertEquals( $order->get_shipping_country(), $data['shipping']['country'] ); - $this->assertEquals( 1, count( $data['line_items'] ) ); - $this->assertEquals( 1, count( $data['shipping_lines'] ) ); - } - - /** - * Test the sanitization of the payment_method_title field through the API. - * - * @since 3.5.2 - */ - public function test_create_update_order_payment_method_title_sanitize() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - - // Test when creating order. - $request = new WP_REST_Request( 'POST', '/wc/v3/orders' ); - $request->set_body_params( - array( - 'payment_method' => 'bacs', - 'payment_method_title' => '

Sanitize this

', - 'set_paid' => true, - 'billing' => array( - 'first_name' => 'John', - 'last_name' => 'Doe', - 'address_1' => '969 Market', - 'address_2' => '', - 'city' => 'San Francisco', - 'state' => 'CA', - 'postcode' => '94103', - 'country' => 'US', - 'email' => 'john.doe@example.com', - 'phone' => '(555) 555-5555', - ), - 'shipping' => array( - 'first_name' => 'John', - 'last_name' => 'Doe', - 'address_1' => '969 Market', - 'address_2' => '', - 'city' => 'San Francisco', - 'state' => 'CA', - 'postcode' => '94103', - 'country' => 'US', - ), - 'line_items' => array( - array( - 'product_id' => $product->get_id(), - 'quantity' => 2, - ), - ), - 'shipping_lines' => array( - array( - 'method_id' => 'flat_rate', - 'method_title' => 'Flat rate', - 'total' => '10', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $order = wc_get_order( $data['id'] ); - $this->assertEquals( 201, $response->get_status() ); - $this->assertEquals( $order->get_payment_method(), $data['payment_method'] ); - $this->assertEquals( $order->get_payment_method_title(), 'Sanitize this' ); - - // Test when updating order. - $request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $data['id'] ); - $request->set_body_params( - array( - 'payment_method' => 'bacs', - 'payment_method_title' => '

Sanitize this too

', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $order = wc_get_order( $data['id'] ); - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( $order->get_payment_method(), $data['payment_method'] ); - $this->assertEquals( $order->get_payment_method_title(), 'Sanitize this too' ); - } - - /** - * Tests creating an order without required fields. - * @since 3.5.0 - */ - public function test_create_order_invalid_fields() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - - // Non-existent customer. - $request = new WP_REST_Request( 'POST', '/wc/v3/orders' ); - $request->set_body_params( - array( - 'payment_method' => 'bacs', - 'payment_method_title' => 'Direct Bank Transfer', - 'set_paid' => true, - 'customer_id' => 99999, - 'billing' => array( - 'first_name' => 'John', - 'last_name' => 'Doe', - 'address_1' => '969 Market', - 'address_2' => '', - 'city' => 'San Francisco', - 'state' => 'CA', - 'postcode' => '94103', - 'country' => 'US', - 'email' => 'john.doe@example.com', - 'phone' => '(555) 555-5555', - ), - 'shipping' => array( - 'first_name' => 'John', - 'last_name' => 'Doe', - 'address_1' => '969 Market', - 'address_2' => '', - 'city' => 'San Francisco', - 'state' => 'CA', - 'postcode' => '94103', - 'country' => 'US', - ), - 'line_items' => array( - array( - 'product_id' => $product->get_id(), - 'quantity' => 2, - ), - ), - 'shipping_lines' => array( - array( - 'method_id' => 'flat_rate', - 'method_title' => 'Flat rate', - 'total' => 10, - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( 400, $response->get_status() ); - } - - /** - * Tests updating an order. - * - * @since 3.5.0 - */ - public function test_update_order() { - wp_set_current_user( $this->user ); - $order = WC_Helper_Order::create_order(); - $request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() ); - $request->set_body_params( - array( - 'payment_method' => 'test-update', - 'billing' => array( - 'first_name' => 'Fish', - 'last_name' => 'Face', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 'test-update', $data['payment_method'] ); - $this->assertEquals( 'Fish', $data['billing']['first_name'] ); - $this->assertEquals( 'Face', $data['billing']['last_name'] ); - } - - /** - * Tests updating an order and removing items. - * - * @since 3.5.0 - */ - public function test_update_order_remove_items() { - wp_set_current_user( $this->user ); - $order = WC_Helper_Order::create_order(); - $fee = new WC_Order_Item_Fee(); - $fee->set_props( - array( - 'name' => 'Some Fee', - 'tax_status' => 'taxable', - 'total' => '100', - 'tax_class' => '', - ) - ); - $order->add_item( $fee ); - $order->save(); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() ); - $fee_data = current( $order->get_items( 'fee' ) ); - - $request->set_body_params( - array( - 'fee_lines' => array( - array( - 'id' => $fee_data->get_id(), - 'name' => null, - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertTrue( empty( $data['fee_lines'] ) ); - } - - /** - * Tests updating an order and adding a coupon. - * - * @since 3.5.0 - */ - public function test_update_order_add_coupons() { - wp_set_current_user( $this->user ); - - $order = WC_Helper_Order::create_order(); - $order_item = current( $order->get_items() ); - $coupon = WC_Helper_Coupon::create_coupon( 'fake-coupon' ); - $coupon->set_amount( 5 ); - $coupon->save(); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() ); - $request->set_body_params( - array( - 'coupon_lines' => array( - array( - 'code' => 'fake-coupon', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertCount( 1, $data['coupon_lines'] ); - $this->assertEquals( '45.00', $data['total'] ); - } - - /** - * Tests updating an order and removing a coupon. - * - * @since 3.5.0 - */ - public function test_update_order_remove_coupons() { - wp_set_current_user( $this->user ); - $order = WC_Helper_Order::create_order(); - $order_item = current( $order->get_items() ); - $coupon = WC_Helper_Coupon::create_coupon( 'fake-coupon' ); - $coupon->set_amount( 5 ); - $coupon->save(); - - $order->apply_coupon( $coupon ); - $order->save(); - - // Check that the coupon is applied. - $this->assertEquals( '45.00', $order->get_total() ); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() ); - $coupon_data = current( $order->get_items( 'coupon' ) ); - - $request->set_body_params( - array( - 'coupon_lines' => array( - array( - 'id' => $coupon_data->get_id(), - 'code' => null, - ), - ), - 'line_items' => array( - array( - 'id' => $order_item->get_id(), - 'product_id' => $order_item->get_product_id(), - 'total' => '40.00', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertTrue( empty( $data['coupon_lines'] ) ); - $this->assertEquals( '50.00', $data['total'] ); - } - - /** - * Tests updating an order with an invalid coupon. - * - * @since 3.5.0 - */ - public function test_invalid_coupon() { - wp_set_current_user( $this->user ); - $order = WC_Helper_Order::create_order(); - $request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() ); - - $request->set_body_params( - array( - 'coupon_lines' => array( - array( - 'code' => 'NON_EXISTING_COUPON', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 400, $response->get_status() ); - $this->assertEquals( 'woocommerce_rest_invalid_coupon', $data['code'] ); - $this->assertEquals( 'Coupon "non_existing_coupon" does not exist!', $data['message'] ); - } - - /** - * Tests updating an order without the correct permissions. - * - * @since 3.5.0 - */ - public function test_update_order_without_permission() { - wp_set_current_user( 0 ); - $order = WC_Helper_Order::create_order(); - $request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() ); - $request->set_body_params( - array( - 'payment_method' => 'test-update', - 'billing' => array( - 'first_name' => 'Fish', - 'last_name' => 'Face', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Tests that updating an order with an invalid id fails. - * - * @since 3.5.0 - */ - public function test_update_order_invalid_id() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'POST', '/wc/v3/orders/999999' ); - $request->set_body_params( - array( - 'payment_method' => 'test-update', - 'billing' => array( - 'first_name' => 'Fish', - 'last_name' => 'Face', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - } - - /** - * Test deleting an order. - * - * @since 3.5.0 - */ - public function test_delete_order() { - wp_set_current_user( $this->user ); - $order = WC_Helper_Order::create_order(); - $request = new WP_REST_Request( 'DELETE', '/wc/v3/orders/' . $order->get_id() ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( null, get_post( $order->get_id() ) ); - } - - /** - * Test deleting an order without permission/creds. - * - * @since 3.5.0 - */ - public function test_delete_order_without_permission() { - wp_set_current_user( 0 ); - $order = WC_Helper_Order::create_order(); - $request = new WP_REST_Request( 'DELETE', '/wc/v3/orders/' . $order->get_id() ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test deleting an order with an invalid id. - * - * @since 3.5.0 - */ - public function test_delete_order_invalid_id() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'DELETE', '/wc/v3/orders/9999999' ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test batch managing product reviews. - * - * @since 3.5.0 - */ - public function test_orders_batch() { - wp_set_current_user( $this->user ); - - $order1 = WC_Helper_Order::create_order(); - $order2 = WC_Helper_Order::create_order(); - $order3 = WC_Helper_Order::create_order(); - - $request = new WP_REST_Request( 'POST', '/wc/v3/orders/batch' ); - $request->set_body_params( - array( - 'update' => array( - array( - 'id' => $order1->get_id(), - 'payment_method' => 'updated', - ), - ), - 'delete' => array( - $order2->get_id(), - $order3->get_id(), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'updated', $data['update'][0]['payment_method'] ); - $this->assertEquals( $order2->get_id(), $data['delete'][0]['id'] ); - $this->assertEquals( $order3->get_id(), $data['delete'][1]['id'] ); - - $request = new WP_REST_Request( 'GET', '/wc/v3/orders' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( 1, count( $data ) ); - } - - /** - * Test the order schema. - * - * @since 3.5.0 - */ - public function test_order_schema() { - wp_set_current_user( $this->user ); - $order = WC_Helper_Order::create_order(); - $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/orders/' . $order->get_id() ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 42, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - } -} diff --git a/tests/unit-tests/api/payment-gateways.php b/tests/unit-tests/api/payment-gateways.php deleted file mode 100644 index f9a43b64e5f..00000000000 --- a/tests/unit-tests/api/payment-gateways.php +++ /dev/null @@ -1,345 +0,0 @@ -endpoint = new WC_REST_Payment_Gateways_Controller(); - $this->user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * - * @since 3.5.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v3/payment_gateways', $routes ); - $this->assertArrayHasKey( '/wc/v3/payment_gateways/(?P[\w-]+)', $routes ); - } - - /** - * Test getting all payment gateways. - * - * @since 3.5.0 - */ - public function test_get_payment_gateways() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways' ) ); - $gateways = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertContains( - array( - 'id' => 'cheque', - 'title' => 'Check payments', - 'description' => 'Please send a check to Store Name, Store Street, Store Town, Store State / County, Store Postcode.', - 'order' => '', - 'enabled' => false, - 'method_title' => 'Check payments', - 'method_description' => 'Take payments in person via checks. This offline gateway can also be useful to test purchases.', - 'method_supports' => array( - 'products', - ), - 'settings' => array_diff_key( - $this->get_settings( 'WC_Gateway_Cheque' ), - array( - 'enabled' => false, - 'description' => false, - ) - ), - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v3/payment_gateways/cheque' ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v3/payment_gateways' ), - ), - ), - ), - ), - $gateways - ); - } - - /** - * Tests to make sure payment gateways cannot viewed without valid permissions. - * - * @since 3.5.0 - */ - public function test_get_payment_gateways_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test getting a single payment gateway. - * - * @since 3.5.0 - */ - public function test_get_payment_gateway() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways/paypal' ) ); - $paypal = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( - array( - 'id' => 'paypal', - 'title' => 'PayPal', - 'description' => "Pay via PayPal; you can pay with your credit card if you don't have a PayPal account.", - 'order' => '', - 'enabled' => false, - 'method_title' => 'PayPal', - 'method_description' => 'PayPal Standard redirects customers to PayPal to enter their payment information.', - 'method_supports' => array( - 'products', - 'refunds', - ), - 'settings' => array_diff_key( - $this->get_settings( 'WC_Gateway_Paypal' ), - array( - 'enabled' => false, - 'description' => false, - ) - ), - ), - $paypal - ); - } - - /** - * Test getting a payment gateway without valid permissions. - * - * @since 3.5.0 - */ - public function test_get_payment_gateway_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways/paypal' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test getting a payment gateway with an invalid id. - * - * @since 3.5.0 - */ - public function test_get_payment_gateway_invalid_id() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways/totally_fake_method' ) ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test updating a single payment gateway. - * - * @since 3.5.0 - */ - public function test_update_payment_gateway() { - wp_set_current_user( $this->user ); - - // Test defaults - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways/paypal' ) ); - $paypal = $response->get_data(); - - $this->assertEquals( 'PayPal', $paypal['settings']['title']['value'] ); - $this->assertEquals( 'admin@example.org', $paypal['settings']['email']['value'] ); - $this->assertEquals( 'no', $paypal['settings']['testmode']['value'] ); - - // test updating single setting - $request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' ); - $request->set_body_params( - array( - 'settings' => array( - 'email' => 'woo@woo.local', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $paypal = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 'PayPal', $paypal['settings']['title']['value'] ); - $this->assertEquals( 'woo@woo.local', $paypal['settings']['email']['value'] ); - $this->assertEquals( 'no', $paypal['settings']['testmode']['value'] ); - - // test updating multiple settings - $request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' ); - $request->set_body_params( - array( - 'settings' => array( - 'testmode' => 'yes', - 'title' => 'PayPal - New Title', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $paypal = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 'PayPal - New Title', $paypal['settings']['title']['value'] ); - $this->assertEquals( 'woo@woo.local', $paypal['settings']['email']['value'] ); - $this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] ); - - // Test other parameters, and recheck settings - $request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' ); - $request->set_body_params( - array( - 'enabled' => false, - 'order' => 2, - ) - ); - $response = $this->server->dispatch( $request ); - $paypal = $response->get_data(); - - $this->assertFalse( $paypal['enabled'] ); - $this->assertEquals( 2, $paypal['order'] ); - $this->assertEquals( 'PayPal - New Title', $paypal['settings']['title']['value'] ); - $this->assertEquals( 'woo@woo.local', $paypal['settings']['email']['value'] ); - $this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] ); - - // test bogus - $request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' ); - $request->set_body_params( - array( - 'settings' => array( - 'paymentaction' => 'afasfasf', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - - $request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' ); - $request->set_body_params( - array( - 'settings' => array( - 'paymentaction' => 'authorization', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $paypal = $response->get_data(); - $this->assertEquals( 'authorization', $paypal['settings']['paymentaction']['value'] ); - } - - /** - * Test updating a payment gateway without valid permissions. - * - * @since 3.5.0 - */ - public function test_update_payment_gateway_without_permission() { - wp_set_current_user( 0 ); - $request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' ); - $request->set_body_params( - array( - 'settings' => array( - 'testmode' => 'yes', - 'title' => 'PayPal - New Title', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test updating a payment gateway with an invalid id. - * - * @since 3.5.0 - */ - public function test_update_payment_gateway_invalid_id() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/totally_fake_method' ); - $request->set_body_params( - array( - 'enabled' => true, - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test the payment gateway schema. - * - * @since 3.5.0 - */ - public function test_payment_gateway_schema() { - wp_set_current_user( $this->user ); - - $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/payment_gateways' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 9, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'title', $properties ); - $this->assertArrayHasKey( 'description', $properties ); - $this->assertArrayHasKey( 'order', $properties ); - $this->assertArrayHasKey( 'enabled', $properties ); - $this->assertArrayHasKey( 'method_title', $properties ); - $this->assertArrayHasKey( 'method_description', $properties ); - $this->assertArrayHasKey( 'method_supports', $properties ); - $this->assertArrayHasKey( 'settings', $properties ); - } - - /** - * Loads a particular gateway's settings so we can correctly test API output. - * - * @since 3.5.0 - * @param string $gateway_class Name of WC_Payment_Gateway class. - */ - private function get_settings( $gateway_class ) { - $gateway = new $gateway_class(); - $settings = array(); - $gateway->init_form_fields(); - foreach ( $gateway->form_fields as $id => $field ) { - // Make sure we at least have a title and type - if ( empty( $field['title'] ) || empty( $field['type'] ) ) { - continue; - } - // Ignore 'enabled' and 'description', to be in line with \WC_REST_Payment_Gateways_Controller::get_settings. - if ( in_array( $id, array( 'enabled', 'description' ), true ) ) { - continue; - } - $data = array( - 'id' => $id, - 'label' => empty( $field['label'] ) ? $field['title'] : $field['label'], - 'description' => empty( $field['description'] ) ? '' : $field['description'], - 'type' => $field['type'], - 'value' => $gateway->settings[ $id ], - 'default' => empty( $field['default'] ) ? '' : $field['default'], - 'tip' => empty( $field['description'] ) ? '' : $field['description'], - 'placeholder' => empty( $field['placeholder'] ) ? '' : $field['placeholder'], - ); - if ( ! empty( $field['options'] ) ) { - $data['options'] = $field['options']; - } - $settings[ $id ] = $data; - } - return $settings; - } - -} diff --git a/tests/unit-tests/api/product-reviews.php b/tests/unit-tests/api/product-reviews.php deleted file mode 100644 index f34fd15626c..00000000000 --- a/tests/unit-tests/api/product-reviews.php +++ /dev/null @@ -1,470 +0,0 @@ -user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * - * @since 3.5.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v3/products/reviews', $routes ); - $this->assertArrayHasKey( '/wc/v3/products/reviews/(?P[\d]+)', $routes ); - $this->assertArrayHasKey( '/wc/v3/products/reviews/batch', $routes ); - } - - /** - * Test getting all product reviews. - * - * @since 3.5.0 - */ - public function test_get_product_reviews() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - // Create 10 products reviews for the product - for ( $i = 0; $i < 10; $i++ ) { - $review_id = WC_Helper_Product::create_product_review( $product->get_id() ); - } - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews' ) ); - $product_reviews = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 10, count( $product_reviews ) ); - $this->assertContains( - array( - 'id' => $review_id, - 'date_created' => $product_reviews[0]['date_created'], - 'date_created_gmt' => $product_reviews[0]['date_created_gmt'], - 'product_id' => $product->get_id(), - 'status' => 'approved', - 'reviewer' => 'admin', - 'reviewer_email' => 'woo@woo.local', - 'review' => "

Review content here

\n", - 'rating' => 0, - 'verified' => false, - 'reviewer_avatar_urls' => $product_reviews[0]['reviewer_avatar_urls'], - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v3/products/reviews/' . $review_id ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v3/products/reviews' ), - ), - ), - 'up' => array( - array( - 'href' => rest_url( '/wc/v3/products/' . $product->get_id() ), - ), - ), - ), - ), - $product_reviews - ); - } - - /** - * Tests to make sure product reviews cannot be viewed without valid permissions. - * - * @since 3.5.0 - */ - public function test_get_product_reviews_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_simple_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Tests to make sure an error is returned when an invalid product is loaded. - * - * @since 3.5.0 - */ - public function test_get_product_reviews_invalid_product() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/0/reviews' ) ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Tests getting a single product review. - * - * @since 3.5.0 - */ - public function test_get_product_review() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $product_review_id = WC_Helper_Product::create_product_review( $product->get_id() ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/' . $product_review_id ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( - array( - 'id' => $product_review_id, - 'date_created' => $data['date_created'], - 'date_created_gmt' => $data['date_created_gmt'], - 'product_id' => $product->get_id(), - 'status' => 'approved', - 'reviewer' => 'admin', - 'reviewer_email' => 'woo@woo.local', - 'review' => "

Review content here

\n", - 'rating' => 0, - 'verified' => false, - 'reviewer_avatar_urls' => $data['reviewer_avatar_urls'], - ), - $data - ); - } - - /** - * Tests getting a single product review without the correct permissions. - * - * @since 3.5.0 - */ - public function test_get_product_review_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_simple_product(); - $product_review_id = WC_Helper_Product::create_product_review( $product->get_id() ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/' . $product_review_id ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Tests getting a product review with an invalid ID. - * - * @since 3.5.0 - */ - public function test_get_product_review_invalid_id() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/0' ) ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Tests creating a product review. - * - * @since 3.5.0 - */ - public function test_create_product_review() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' ); - $request->set_body_params( - array( - 'review' => 'Hello world.', - 'reviewer' => 'Admin', - 'reviewer_email' => 'woo@woo.local', - 'rating' => '5', - 'product_id' => $product->get_id(), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 201, $response->get_status() ); - $this->assertEquals( - array( - 'id' => $data['id'], - 'date_created' => $data['date_created'], - 'date_created_gmt' => $data['date_created_gmt'], - 'product_id' => $product->get_id(), - 'status' => 'approved', - 'reviewer' => 'Admin', - 'reviewer_email' => 'woo@woo.local', - 'review' => 'Hello world.', - 'rating' => 5, - 'verified' => false, - 'reviewer_avatar_urls' => $data['reviewer_avatar_urls'], - ), - $data - ); - } - - /** - * Tests creating a product review without required fields. - * - * @since 3.5.0 - */ - public function test_create_product_review_invalid_fields() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - - // missing review - $request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' ); - $request->set_body_params( - array( - 'reviewer' => 'Admin', - 'reviewer_email' => 'woo@woo.local', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 400, $response->get_status() ); - - // Missing reviewer. - $request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' ); - $request->set_body_params( - array( - 'review' => 'Hello world.', - 'reviewer_email' => 'woo@woo.local', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 400, $response->get_status() ); - - // missing reviewer_email - $request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' ); - $request->set_body_params( - array( - 'review' => 'Hello world.', - 'reviewer' => 'Admin', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 400, $response->get_status() ); - } - - /** - * Tests updating a product review. - * - * @since 3.5.0 - */ - public function test_update_product_review() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $product_review_id = WC_Helper_Product::create_product_review( $product->get_id() ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/' . $product_review_id ) ); - $data = $response->get_data(); - $this->assertEquals( "

Review content here

\n", $data['review'] ); - $this->assertEquals( 'admin', $data['reviewer'] ); - $this->assertEquals( 'woo@woo.local', $data['reviewer_email'] ); - $this->assertEquals( 0, $data['rating'] ); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/products/reviews/' . $product_review_id ); - $request->set_body_params( - array( - 'review' => 'Hello world - updated.', - 'reviewer' => 'Justin', - 'reviewer_email' => 'woo2@woo.local', - 'rating' => 3, - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( 'Hello world - updated.', $data['review'] ); - $this->assertEquals( 'Justin', $data['reviewer'] ); - $this->assertEquals( 'woo2@woo.local', $data['reviewer_email'] ); - $this->assertEquals( 3, $data['rating'] ); - } - - /** - * Tests updating a product review without the correct permissions. - * - * @since 3.5.0 - */ - public function test_update_product_review_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_simple_product(); - $product_review_id = WC_Helper_Product::create_product_review( $product->get_id() ); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/products/reviews/' . $product_review_id ); - $request->set_body_params( - array( - 'review' => 'Hello world.', - 'reviewer' => 'Admin', - 'reviewer_email' => 'woo@woo.dev', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Tests that updating a product review with an invalid id fails. - * - * @since 3.5.0 - */ - public function test_update_product_review_invalid_id() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/products/reviews/0' ); - $request->set_body_params( - array( - 'review' => 'Hello world.', - 'reviewer' => 'Admin', - 'reviewer_email' => 'woo@woo.dev', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test deleting a product review. - * - * @since 3.5.0 - */ - public function test_delete_product_review() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $product_review_id = WC_Helper_Product::create_product_review( $product->get_id() ); - - $request = new WP_REST_Request( 'DELETE', '/wc/v3/products/reviews/' . $product_review_id ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - } - - /** - * Test deleting a product review without permission/creds. - * - * @since 3.5.0 - */ - public function test_delete_product_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_simple_product(); - $product_review_id = WC_Helper_Product::create_product_review( $product->get_id() ); - - $request = new WP_REST_Request( 'DELETE', '/wc/v3/products/reviews/' . $product_review_id ); - $response = $this->server->dispatch( $request ); - - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test deleting a product review with an invalid id. - * - * @since 3.5.0 - */ - public function test_delete_product_review_invalid_id() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $product_review_id = WC_Helper_Product::create_product_review( $product->get_id() ); - - $request = new WP_REST_Request( 'DELETE', '/wc/v3/products/reviews/0' ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test batch managing product reviews. - * - * @since 3.5.0 - */ - public function test_product_reviews_batch() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - - $review_1_id = WC_Helper_Product::create_product_review( $product->get_id() ); - $review_2_id = WC_Helper_Product::create_product_review( $product->get_id() ); - $review_3_id = WC_Helper_Product::create_product_review( $product->get_id() ); - $review_4_id = WC_Helper_Product::create_product_review( $product->get_id() ); - - $request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews/batch' ); - $request->set_body_params( - array( - 'update' => array( - array( - 'id' => $review_1_id, - 'review' => 'Updated review.', - ), - ), - 'delete' => array( - $review_2_id, - $review_3_id, - ), - 'create' => array( - array( - 'review' => 'New review.', - 'reviewer' => 'Justin', - 'reviewer_email' => 'woo3@woo.local', - 'product_id' => $product->get_id(), - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'Updated review.', $data['update'][0]['review'] ); - $this->assertEquals( 'New review.', $data['create'][0]['review'] ); - $this->assertEquals( $review_2_id, $data['delete'][0]['previous']['id'] ); - $this->assertEquals( $review_3_id, $data['delete'][1]['previous']['id'] ); - - $request = new WP_REST_Request( 'GET', '/wc/v3/products/reviews' ); - $request->set_param( 'product', $product->get_id() ); - - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 3, count( $data ) ); - } - - /** - * Test the product review schema. - * - * @since 3.5.0 - */ - public function test_product_review_schema() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/products/reviews' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 11, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'date_created', $properties ); - $this->assertArrayHasKey( 'date_created_gmt', $properties ); - $this->assertArrayHasKey( 'product_id', $properties ); - $this->assertArrayHasKey( 'status', $properties ); - $this->assertArrayHasKey( 'reviewer', $properties ); - $this->assertArrayHasKey( 'reviewer_email', $properties ); - $this->assertArrayHasKey( 'review', $properties ); - $this->assertArrayHasKey( 'rating', $properties ); - $this->assertArrayHasKey( 'verified', $properties ); - - if ( get_option( 'show_avatars' ) ) { - $this->assertArrayHasKey( 'reviewer_avatar_urls', $properties ); - } - } -} diff --git a/tests/unit-tests/api/product-variations.php b/tests/unit-tests/api/product-variations.php deleted file mode 100644 index ab1b810be43..00000000000 --- a/tests/unit-tests/api/product-variations.php +++ /dev/null @@ -1,474 +0,0 @@ -endpoint = new WC_REST_Product_Variations_Controller(); - $this->user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * - * @since 3.5.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v3/products/(?P[\d]+)/variations', $routes ); - $this->assertArrayHasKey( '/wc/v3/products/(?P[\d]+)/variations/(?P[\d]+)', $routes ); - $this->assertArrayHasKey( '/wc/v3/products/(?P[\d]+)/variations/batch', $routes ); - } - - /** - * Test getting variations. - * - * @since 3.5.0 - */ - public function test_get_variations() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_variation_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' ) ); - $variations = $response->get_data(); - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 2, count( $variations ) ); - $this->assertEquals( 'DUMMY SKU VARIABLE LARGE', $variations[0]['sku'] ); - $this->assertEquals( 'size', $variations[0]['attributes'][0]['name'] ); - } - - /** - * Test getting variations without permission. - * - * @since 3.5.0 - */ - public function test_get_variations_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_variation_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test getting a single variation. - * - * @since 3.5.0 - */ - public function test_get_variation() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_variation_product(); - $children = $product->get_children(); - $variation_id = $children[0]; - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id ) ); - $variation = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( $variation_id, $variation['id'] ); - $this->assertEquals( 'size', $variation['attributes'][0]['name'] ); - } - - /** - * Test getting single variation without permission. - * - * @since 3.5.0 - */ - public function test_get_variation_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_variation_product(); - $children = $product->get_children(); - $variation_id = $children[0]; - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test deleting a single variation. - * - * @since 3.5.0 - */ - public function test_delete_variation() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_variation_product(); - $children = $product->get_children(); - $variation_id = $children[0]; - - $request = new WP_REST_Request( 'DELETE', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' ) ); - $variations = $response->get_data(); - $this->assertEquals( 1, count( $variations ) ); - } - - /** - * Test deleting a single variation without permission. - * - * @since 3.5.0 - */ - public function test_delete_variation_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_variation_product(); - $children = $product->get_children(); - $variation_id = $children[0]; - - $request = new WP_REST_Request( 'DELETE', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test deleting a single variation with an invalid ID. - * - * @since 3.5.0 - */ - public function test_delete_variation_with_invalid_id() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_variation_product(); - $request = new WP_REST_Request( 'DELETE', '/wc/v3/products/' . $product->get_id() . '/variations/0' ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test editing a single variation. - * - * @since 3.5.0 - */ - public function test_update_variation() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_variation_product(); - $children = $product->get_children(); - $variation_id = $children[0]; - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id ) ); - $variation = $response->get_data(); - - $this->assertEquals( 'DUMMY SKU VARIABLE SMALL', $variation['sku'] ); - $this->assertEquals( 10, $variation['regular_price'] ); - $this->assertEmpty( $variation['sale_price'] ); - $this->assertEquals( 'small', $variation['attributes'][0]['option'] ); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id ); - $request->set_body_params( - array( - 'sku' => 'FIXED-\'SKU', - 'sale_price' => '8', - 'description' => 'O_O', - 'image' => array( - 'position' => 0, - 'src' => 'http://cldup.com/Dr1Bczxq4q.png', - 'alt' => 'test upload image', - ), - 'attributes' => array( - array( - 'name' => 'pa_size', - 'option' => 'medium', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $variation = $response->get_data(); - - $this->assertTrue( isset( $variation['description'] ), print_r( $variation, true ) ); - $this->assertContains( 'O_O', $variation['description'], print_r( $variation, true ) ); - $this->assertEquals( '8', $variation['price'], print_r( $variation, true ) ); - $this->assertEquals( '8', $variation['sale_price'], print_r( $variation, true ) ); - $this->assertEquals( '10', $variation['regular_price'], print_r( $variation, true ) ); - $this->assertEquals( 'FIXED-\'SKU', $variation['sku'], print_r( $variation, true ) ); - $this->assertEquals( 'medium', $variation['attributes'][0]['option'], print_r( $variation, true ) ); - $this->assertContains( 'Dr1Bczxq4q', $variation['image']['src'], print_r( $variation, true ) ); - $this->assertContains( 'test upload image', $variation['image']['alt'], print_r( $variation, true ) ); - } - - /** - * Test updating a single variation without permission. - * - * @since 3.5.0 - */ - public function test_update_variation_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_variation_product(); - $children = $product->get_children(); - $variation_id = $children[0]; - - $request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id ); - $request->set_body_params( - array( - 'sku' => 'FIXED-SKU-NO-PERMISSION', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test updating a single variation with an invalid ID. - * - * @since 3.5.0 - */ - public function test_update_variation_with_invalid_id() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_variation_product(); - $request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() . '/variations/0' ); - $request->set_body_params( - array( - 'sku' => 'FIXED-SKU-NO-PERMISSION', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - } - - /** - * Test creating a single variation. - * - * @since 3.5.0 - */ - public function test_create_variation() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_variation_product(); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' ) ); - $variations = $response->get_data(); - $this->assertEquals( 2, count( $variations ) ); - - $request = new WP_REST_Request( 'POST', '/wc/v3/products/' . $product->get_id() . '/variations' ); - $request->set_body_params( - array( - 'sku' => 'DUMMY SKU VARIABLE MEDIUM', - 'regular_price' => '12', - 'description' => 'A medium size.', - 'attributes' => array( - array( - 'name' => 'pa_size', - 'option' => 'medium', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $variation = $response->get_data(); - - $this->assertContains( 'A medium size.', $variation['description'] ); - $this->assertEquals( '12', $variation['price'] ); - $this->assertEquals( '12', $variation['regular_price'] ); - $this->assertTrue( $variation['purchasable'] ); - $this->assertEquals( 'DUMMY SKU VARIABLE MEDIUM', $variation['sku'] ); - $this->assertEquals( 'medium', $variation['attributes'][0]['option'] ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' ) ); - $variations = $response->get_data(); - $this->assertEquals( 3, count( $variations ) ); - } - - /** - * Test creating a single variation without permission. - * - * @since 3.5.0 - */ - public function test_create_variation_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_variation_product(); - - $request = new WP_REST_Request( 'POST', '/wc/v3/products/' . $product->get_id() . '/variations' ); - $request->set_body_params( - array( - 'sku' => 'DUMMY SKU VARIABLE MEDIUM', - 'regular_price' => '12', - 'description' => 'A medium size.', - 'attributes' => array( - array( - 'name' => 'pa_size', - 'option' => 'medium', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test batch managing product variations. - * - * @since 3.5.0 - */ - public function test_product_variations_batch() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_variation_product(); - $children = $product->get_children(); - $request = new WP_REST_Request( 'POST', '/wc/v3/products/' . $product->get_id() . '/variations/batch' ); - $request->set_body_params( - array( - 'update' => array( - array( - 'id' => $children[0], - 'description' => 'Updated description.', - 'image' => array( - 'position' => 0, - 'src' => 'http://cldup.com/Dr1Bczxq4q.png', - 'alt' => 'test upload image', - ), - ), - ), - 'delete' => array( - $children[1], - ), - 'create' => array( - array( - 'sku' => 'DUMMY SKU VARIABLE MEDIUM', - 'regular_price' => '12', - 'description' => 'A medium size.', - 'attributes' => array( - array( - 'name' => 'pa_size', - 'option' => 'medium', - ), - ), - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertContains( 'Updated description.', $data['update'][0]['description'] ); - $this->assertEquals( 'DUMMY SKU VARIABLE MEDIUM', $data['create'][0]['sku'] ); - $this->assertEquals( 'medium', $data['create'][0]['attributes'][0]['option'] ); - $this->assertEquals( $children[1], $data['delete'][0]['id'] ); - - $request = new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 2, count( $data ) ); - } - - /** - * Test variation schema. - * - * @since 3.5.0 - */ - public function test_variation_schema() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/products/' . $product->get_id() . '/variations' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 37, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'date_created', $properties ); - $this->assertArrayHasKey( 'date_modified', $properties ); - $this->assertArrayHasKey( 'description', $properties ); - $this->assertArrayHasKey( 'permalink', $properties ); - $this->assertArrayHasKey( 'sku', $properties ); - $this->assertArrayHasKey( 'price', $properties ); - $this->assertArrayHasKey( 'regular_price', $properties ); - $this->assertArrayHasKey( 'sale_price', $properties ); - $this->assertArrayHasKey( 'date_on_sale_from', $properties ); - $this->assertArrayHasKey( 'date_on_sale_to', $properties ); - $this->assertArrayHasKey( 'on_sale', $properties ); - $this->assertArrayHasKey( 'purchasable', $properties ); - $this->assertArrayHasKey( 'virtual', $properties ); - $this->assertArrayHasKey( 'downloadable', $properties ); - $this->assertArrayHasKey( 'downloads', $properties ); - $this->assertArrayHasKey( 'download_limit', $properties ); - $this->assertArrayHasKey( 'download_expiry', $properties ); - $this->assertArrayHasKey( 'tax_status', $properties ); - $this->assertArrayHasKey( 'tax_class', $properties ); - $this->assertArrayHasKey( 'manage_stock', $properties ); - $this->assertArrayHasKey( 'stock_quantity', $properties ); - $this->assertArrayHasKey( 'stock_status', $properties ); - $this->assertArrayHasKey( 'backorders', $properties ); - $this->assertArrayHasKey( 'backorders_allowed', $properties ); - $this->assertArrayHasKey( 'backordered', $properties ); - $this->assertArrayHasKey( 'weight', $properties ); - $this->assertArrayHasKey( 'dimensions', $properties ); - $this->assertArrayHasKey( 'shipping_class', $properties ); - $this->assertArrayHasKey( 'shipping_class_id', $properties ); - $this->assertArrayHasKey( 'image', $properties ); - $this->assertArrayHasKey( 'attributes', $properties ); - $this->assertArrayHasKey( 'menu_order', $properties ); - $this->assertArrayHasKey( 'meta_data', $properties ); - } - - /** - * Test updating a variation stock. - * - * @since 3.5.0 - */ - public function test_update_variation_manage_stock() { - wp_set_current_user( $this->user ); - - $product = WC_Helper_Product::create_variation_product(); - $product->set_manage_stock( false ); - $product->save(); - - $children = $product->get_children(); - $variation_id = $children[0]; - - // Set stock to true. - $request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id ); - $request->set_body_params( - array( - 'manage_stock' => true, - ) - ); - - $response = $this->server->dispatch( $request ); - $variation = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( true, $variation['manage_stock'] ); - - // Set stock to false. - $request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id ); - $request->set_body_params( - array( - 'manage_stock' => false, - ) - ); - - $response = $this->server->dispatch( $request ); - $variation = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( false, $variation['manage_stock'] ); - - // Set stock to false but parent is managing stock. - $product->set_manage_stock( true ); - $product->save(); - $request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id ); - $request->set_body_params( - array( - 'manage_stock' => false, - ) - ); - - $response = $this->server->dispatch( $request ); - $variation = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 'parent', $variation['manage_stock'] ); - } -} diff --git a/tests/unit-tests/api/products.php b/tests/unit-tests/api/products.php deleted file mode 100644 index 6b8cff59db2..00000000000 --- a/tests/unit-tests/api/products.php +++ /dev/null @@ -1,807 +0,0 @@ -endpoint = new WC_REST_Products_Controller(); - $this->user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * - * @since 3.5.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v3/products', $routes ); - $this->assertArrayHasKey( '/wc/v3/products/(?P[\d]+)', $routes ); - $this->assertArrayHasKey( '/wc/v3/products/batch', $routes ); - } - - /** - * Test getting products. - * - * @since 3.5.0 - */ - public function test_get_products() { - wp_set_current_user( $this->user ); - WC_Helper_Product::create_external_product(); - sleep( 1 ); // So both products have different timestamps. - WC_Helper_Product::create_simple_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( 2, count( $products ) ); - $this->assertEquals( 'Dummy Product', $products[0]['name'] ); - $this->assertEquals( 'DUMMY SKU', $products[0]['sku'] ); - $this->assertEquals( 'Dummy External Product', $products[1]['name'] ); - $this->assertEquals( 'DUMMY EXTERNAL SKU', $products[1]['sku'] ); - } - - /** - * Test getting products without permission. - * - * @since 3.5.0 - */ - public function test_get_products_without_permission() { - wp_set_current_user( 0 ); - WC_Helper_Product::create_simple_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test getting a single product. - * - * @since 3.5.0 - */ - public function test_get_product() { - wp_set_current_user( $this->user ); - $simple = WC_Helper_Product::create_external_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $simple->get_id() ) ); - $product = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertContains( - array( - 'id' => $simple->get_id(), - 'name' => 'Dummy External Product', - 'type' => 'simple', - 'status' => 'publish', - 'sku' => 'DUMMY EXTERNAL SKU', - 'regular_price' => 10, - ), - $product - ); - } - - /** - * Test getting single product without permission. - * - * @since 3.5.0 - */ - public function test_get_product_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_simple_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test deleting a single product. - * - * @since 3.5.0 - */ - public function test_delete_product() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - - $request = new WP_REST_Request( 'DELETE', '/wc/v3/products/' . $product->get_id() ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products' ) ); - $variations = $response->get_data(); - $this->assertEquals( 0, count( $variations ) ); - } - - /** - * Test deleting a single product without permission. - * - * @since 3.5.0 - */ - public function test_delete_product_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'DELETE', '/wc/v3/products/' . $product->get_id() ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test deleting a single product with an invalid ID. - * - * @since 3.5.0 - */ - public function test_delete_product_with_invalid_id() { - wp_set_current_user( 0 ); - $request = new WP_REST_Request( 'DELETE', '/wc/v3/products/0' ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test editing a single product. Tests multiple product types. - * - * @since 3.5.0 - */ - public function test_update_product() { - wp_set_current_user( $this->user ); - - // test simple products. - $product = WC_Helper_Product::create_simple_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() ) ); - $data = $response->get_data(); - $date_created = date( 'Y-m-d\TH:i:s', current_time( 'timestamp' ) ); - - $this->assertEquals( 'DUMMY SKU', $data['sku'] ); - $this->assertEquals( 10, $data['regular_price'] ); - $this->assertEmpty( $data['sale_price'] ); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() ); - $request->set_body_params( - array( - 'sku' => 'FIXED-SKU', - 'sale_price' => '8', - 'description' => 'Testing', - 'date_created' => $date_created, - 'images' => array( - array( - 'position' => 0, - 'src' => 'http://cldup.com/Dr1Bczxq4q.png', - 'alt' => 'test upload image', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertContains( 'Testing', $data['description'] ); - $this->assertEquals( '8', $data['price'] ); - $this->assertEquals( '8', $data['sale_price'] ); - $this->assertEquals( '10', $data['regular_price'] ); - $this->assertEquals( 'FIXED-SKU', $data['sku'] ); - $this->assertEquals( $date_created, $data['date_created'] ); - $this->assertContains( 'Dr1Bczxq4q', $data['images'][0]['src'] ); - $this->assertContains( 'test upload image', $data['images'][0]['alt'] ); - $product->delete( true ); - - // test variable product (variations are tested in product-variations.php). - $product = WC_Helper_Product::create_variation_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() ) ); - $data = $response->get_data(); - - foreach ( array( 'small', 'large' ) as $term_name ) { - $this->assertContains( $term_name, $data['attributes'][0]['options'] ); - } - - $request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() ); - $request->set_body_params( - array( - 'attributes' => array( - array( - 'id' => 0, - 'name' => 'pa_color', - 'options' => array( - 'red', - 'yellow', - ), - 'visible' => false, - 'variation' => 1, - ), - array( - 'id' => 0, - 'name' => 'pa_size', - 'options' => array( - 'small', - ), - 'visible' => false, - 'variation' => 1, - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( array( 'small' ), $data['attributes'][0]['options'] ); - - foreach ( array( 'red', 'yellow' ) as $term_name ) { - $this->assertContains( $term_name, $data['attributes'][1]['options'] ); - } - - $product->delete( true ); - - // test external product. - $product = WC_Helper_Product::create_external_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() ) ); - $data = $response->get_data(); - - $this->assertEquals( 'Buy external product', $data['button_text'] ); - $this->assertEquals( 'http://woocommerce.com', $data['external_url'] ); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() ); - $request->set_body_params( - array( - 'button_text' => 'Test API Update', - 'external_url' => 'http://automattic.com', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'Test API Update', $data['button_text'] ); - $this->assertEquals( 'http://automattic.com', $data['external_url'] ); - } - - /** - * Test updating a single product without permission. - * - * @since 3.5.0 - */ - public function test_update_product_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() ); - $request->set_body_params( - array( - 'sku' => 'FIXED-SKU-NO-PERMISSION', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test updating a single product with an invalid ID. - * - * @since 3.5.0 - */ - public function test_update_product_with_invalid_id() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'PUT', '/wc/v2/products/0' ); - $request->set_body_params( - array( - 'sku' => 'FIXED-SKU-INVALID-ID', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - } - - /** - * Test creating a single product. - * - * @since 3.5.0 - */ - public function test_create_product() { - wp_set_current_user( $this->user ); - - $request = new WP_REST_Request( 'POST', '/wc/v3/products/shipping_classes' ); - $request->set_body_params( - array( - 'name' => 'Test', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $shipping_class_id = $data['id']; - - // Create simple. - $request = new WP_REST_Request( 'POST', '/wc/v3/products' ); - $request->set_body_params( - array( - 'type' => 'simple', - 'name' => 'Test Simple Product', - 'sku' => 'DUMMY SKU SIMPLE API', - 'regular_price' => '10', - 'shipping_class' => 'test', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( '10', $data['price'] ); - $this->assertEquals( '10', $data['regular_price'] ); - $this->assertTrue( $data['purchasable'] ); - $this->assertEquals( 'DUMMY SKU SIMPLE API', $data['sku'] ); - $this->assertEquals( 'Test Simple Product', $data['name'] ); - $this->assertEquals( 'simple', $data['type'] ); - $this->assertEquals( $shipping_class_id, $data['shipping_class_id'] ); - - // Create external. - $request = new WP_REST_Request( 'POST', '/wc/v3/products' ); - $request->set_body_params( - array( - 'type' => 'external', - 'name' => 'Test External Product', - 'sku' => 'DUMMY SKU EXTERNAL API', - 'regular_price' => '10', - 'button_text' => 'Test Button', - 'external_url' => 'https://wordpress.org', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( '10', $data['price'] ); - $this->assertEquals( '10', $data['regular_price'] ); - $this->assertFalse( $data['purchasable'] ); - $this->assertEquals( 'DUMMY SKU EXTERNAL API', $data['sku'] ); - $this->assertEquals( 'Test External Product', $data['name'] ); - $this->assertEquals( 'external', $data['type'] ); - $this->assertEquals( 'Test Button', $data['button_text'] ); - $this->assertEquals( 'https://wordpress.org', $data['external_url'] ); - - // Create variable. - $request = new WP_REST_Request( 'POST', '/wc/v3/products' ); - $request->set_body_params( - array( - 'type' => 'variable', - 'name' => 'Test Variable Product', - 'sku' => 'DUMMY SKU VARIABLE API', - 'attributes' => array( - array( - 'id' => 0, - 'name' => 'pa_size', - 'options' => array( - 'small', - 'medium', - ), - 'visible' => false, - 'variation' => 1, - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'DUMMY SKU VARIABLE API', $data['sku'] ); - $this->assertEquals( 'Test Variable Product', $data['name'] ); - $this->assertEquals( 'variable', $data['type'] ); - $this->assertEquals( array( 'small', 'medium' ), $data['attributes'][0]['options'] ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products' ) ); - $products = $response->get_data(); - $this->assertEquals( 3, count( $products ) ); - } - - /** - * Test creating a single product without permission. - * - * @since 3.5.0 - */ - public function test_create_product_without_permission() { - wp_set_current_user( 0 ); - - $request = new WP_REST_Request( 'POST', '/wc/v3/products' ); - $request->set_body_params( - array( - 'name' => 'Test Product', - 'regular_price' => '12', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test batch managing products. - * - * @since 3.5.0 - */ - public function test_products_batch() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $product_2 = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'POST', '/wc/v3/products/batch' ); - $request->set_body_params( - array( - 'update' => array( - array( - 'id' => $product->get_id(), - 'description' => 'Updated description.', - ), - ), - 'delete' => array( - $product_2->get_id(), - ), - 'create' => array( - array( - 'sku' => 'DUMMY SKU BATCH TEST 1', - 'regular_price' => '10', - 'name' => 'Test Batch Create 1', - 'type' => 'external', - 'button_text' => 'Test Button', - ), - array( - 'sku' => 'DUMMY SKU BATCH TEST 2', - 'regular_price' => '20', - 'name' => 'Test Batch Create 2', - 'type' => 'simple', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertContains( 'Updated description.', $data['update'][0]['description'] ); - $this->assertEquals( 'DUMMY SKU BATCH TEST 1', $data['create'][0]['sku'] ); - $this->assertEquals( 'DUMMY SKU BATCH TEST 2', $data['create'][1]['sku'] ); - $this->assertEquals( 'Test Button', $data['create'][0]['button_text'] ); - $this->assertEquals( 'external', $data['create'][0]['type'] ); - $this->assertEquals( 'simple', $data['create'][1]['type'] ); - $this->assertEquals( $product_2->get_id(), $data['delete'][0]['id'] ); - - $request = new WP_REST_Request( 'GET', '/wc/v3/products' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 3, count( $data ) ); - } - - /** - * Tests to make sure you can filter products post statuses by both - * the status query arg and WP_Query. - * - * @since 3.5.0 - */ - public function test_products_filter_post_status() { - wp_set_current_user( $this->user ); - for ( $i = 0; $i < 8; $i++ ) { - $product = WC_Helper_Product::create_simple_product(); - if ( 0 === $i % 2 ) { - wp_update_post( - array( - 'ID' => $product->get_id(), - 'post_status' => 'draft', - ) - ); - } - } - - // Test filtering with status=publish. - $request = new WP_REST_Request( 'GET', '/wc/v3/products' ); - $request->set_param( 'status', 'publish' ); - $response = $this->server->dispatch( $request ); - $products = $response->get_data(); - - $this->assertEquals( 4, count( $products ) ); - foreach ( $products as $product ) { - $this->assertEquals( 'publish', $product['status'] ); - } - - // Test filtering with status=draft. - $request = new WP_REST_Request( 'GET', '/wc/v3/products' ); - $request->set_param( 'status', 'draft' ); - $response = $this->server->dispatch( $request ); - $products = $response->get_data(); - - $this->assertEquals( 4, count( $products ) ); - foreach ( $products as $product ) { - $this->assertEquals( 'draft', $product['status'] ); - } - - // Test filtering with no filters - which should return 'any' (all 8). - $request = new WP_REST_Request( 'GET', '/wc/v3/products' ); - $response = $this->server->dispatch( $request ); - $products = $response->get_data(); - - $this->assertEquals( 8, count( $products ) ); - } - - /** - * Test product schema. - * - * @since 3.5.0 - */ - public function test_product_schema() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/products/' . $product->get_id() ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - $this->assertEquals( 65, count( $properties ) ); - } - - /** - * Test product category. - * - * @since 3.5.0 - */ - public function test_get_products_by_category() { - wp_set_current_user( $this->user ); - - // Create one product with a category. - $category = wp_insert_term( 'Some Category', 'product_cat' ); - - $product = new WC_Product_Simple(); - $product->set_category_ids( array( $category['term_id'] ) ); - $product->save(); - - // Create one product without category, i.e. Uncategorized. - $product_2 = new WC_Product_Simple(); - $product_2->save(); - - // Test product assigned to a single category. - $query_params = array( - 'category' => (string) $category['term_id'], - ); - $request = new WP_REST_Request( 'GET', '/wc/v2/products' ); - $request->set_query_params( $query_params ); - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - foreach ( $response_products as $response_product ) { - $this->assertEquals( $product->get_id(), $response_product['id'] ); - $this->assertEquals( $product->get_category_ids(), wp_list_pluck( $response_product['categories'], 'id' ) ); - } - - // Test product without categories. - $request = new WP_REST_Request( 'GET', '/wc/v2/products/' . $product_2->get_id() ); - $response = $this->server->dispatch( $request ); - $response_product = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertCount( 1, $response_product['categories'], print_r( $response_product, true ) ); - $this->assertEquals( 'uncategorized', $response_product['categories'][0]['slug'] ); - - } - - /** - * Test getting products by product type. - * - * @since 3.5.0 - */ - public function test_get_products_by_type() { - wp_set_current_user( $this->user ); - - $simple = WC_Helper_Product::create_simple_product(); - $external = WC_Helper_Product::create_external_product(); - $grouped = WC_Helper_Product::create_grouped_product(); - $variable = WC_Helper_Product::create_variation_product(); - - $product_ids_for_type = array( - 'simple' => array( $simple->get_id() ), - 'external' => array( $external->get_id() ), - 'grouped' => array( $grouped->get_id() ), - 'variable' => array( $variable->get_id() ), - ); - - foreach ( $grouped->get_children() as $additional_product ) { - $product_ids_for_type['simple'][] = $additional_product; - } - - foreach ( $product_ids_for_type as $product_type => $product_ids ) { - $query_params = array( - 'type' => $product_type, - ); - $request = new WP_REST_Request( 'GET', '/wc/v2/products' ); - $request->set_query_params( $query_params ); - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( count( $product_ids ), count( $response_products ) ); - foreach ( $response_products as $response_product ) { - $this->assertContains( $response_product['id'], $product_ids_for_type[ $product_type ], 'REST API: ' . $product_type . ' not found correctly' ); - } - } - } - - /** - * Test getting products by featured property. - * - * @since 3.5.0 - */ - public function test_get_featured_products() { - wp_set_current_user( $this->user ); - - // Create a featured product. - $feat_product = WC_Helper_Product::create_simple_product(); - $feat_product->set_featured( true ); - $feat_product->save(); - - // Create a non-featured product. - $nonfeat_product = WC_Helper_Product::create_simple_product(); - $nonfeat_product->save(); - - $query_params = array( - 'featured' => 'true', - ); - $request = new WP_REST_Request( 'GET', '/wc/v2/products' ); - $request->set_query_params( $query_params ); - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - foreach ( $response_products as $response_product ) { - $this->assertEquals( $feat_product->get_id(), $response_product['id'], 'REST API: Featured product not found correctly' ); - } - - $query_params = array( - 'featured' => 'false', - ); - $request = new WP_REST_Request( 'GET', '/wc/v2/products' ); - $request->set_query_params( $query_params ); - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - foreach ( $response_products as $response_product ) { - $this->assertEquals( $nonfeat_product->get_id(), $response_product['id'], 'REST API: Featured product not found correctly' ); - } - } - - /** - * Test getting products by shipping class property. - * - * @since 3.5.0 - */ - public function test_get_products_by_shipping_class() { - wp_set_current_user( $this->user ); - - $shipping_class_1 = wp_insert_term( 'Bulky', 'product_shipping_class' ); - - $product_1 = new WC_Product_Simple(); - $product_1->set_shipping_class_id( $shipping_class_1['term_id'] ); - $product_1->save(); - - $query_params = array( - 'shipping_class' => (string) $shipping_class_1['term_id'], - ); - $request = new WP_REST_Request( 'GET', '/wc/v2/products' ); - $request->set_query_params( $query_params ); - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - foreach ( $response_products as $response_product ) { - $this->assertEquals( $product_1->get_id(), $response_product['id'] ); - } - } - - /** - * Test getting products by tag. - * - * @since 3.5.0 - */ - public function test_get_products_by_tag() { - wp_set_current_user( $this->user ); - - $test_tag_1 = wp_insert_term( 'Tag 1', 'product_tag' ); - - // Product with a tag. - $product = WC_Helper_Product::create_simple_product(); - $product->set_tag_ids( array( $test_tag_1['term_id'] ) ); - $product->save(); - - // Product without a tag. - $product_2 = WC_Helper_Product::create_simple_product(); - - $query_params = array( - 'tag' => (string) $test_tag_1['term_id'], - ); - $request = new WP_REST_Request( 'GET', '/wc/v2/products' ); - $request->set_query_params( $query_params ); - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - foreach ( $response_products as $response_product ) { - $this->assertEquals( $product->get_id(), $response_product['id'] ); - } - } - - /** - * Test getting products by global attribute. - * - * @since 3.5.0 - */ - public function test_get_products_by_attribute() { - global $wpdb; - wp_set_current_user( $this->user ); - - // Variable product with 2 different variations. - $variable_product = WC_Helper_Product::create_variation_product(); - - // Terms created by variable product. - $term_large = get_term_by( 'slug', 'large', 'pa_size' ); - $term_small = get_term_by( 'slug', 'small', 'pa_size' ); - - // Simple product without attribute. - $product_1 = WC_Helper_Product::create_simple_product(); - - // Simple product with attribute size = large. - $product_2 = WC_Helper_Product::create_simple_product(); - $product_2->set_attributes( array( 'pa_size' => 'large' ) ); - $product_2->save(); - - // Link the product to the term. - $wpdb->insert( - $wpdb->prefix . 'term_relationships', - array( - 'object_id' => $product_2->get_id(), - 'term_taxonomy_id' => $term_large->term_id, - 'term_order' => 0, - ) - ); - - // Products with attribute size == large. - $expected_product_ids = array( - $variable_product->get_id(), - $product_2->get_id(), - ); - $query_params = array( - 'attribute' => 'pa_size', - 'attribute_term' => (string) $term_large->term_id, - ); - $request = new WP_REST_Request( 'GET', '/wc/v2/products' ); - $request->set_query_params( $query_params ); - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( count( $expected_product_ids ), count( $response_products ) ); - foreach ( $response_products as $response_product ) { - $this->assertContains( $response_product['id'], $expected_product_ids ); - } - - // Products with attribute size == small. - $expected_product_ids = array( - $variable_product->get_id(), - ); - $query_params = array( - 'attribute' => 'pa_size', - 'attribute_term' => (string) $term_small->term_id, - ); - $request = new WP_REST_Request( 'GET', '/wc/v2/products' ); - $request->set_query_params( $query_params ); - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( count( $expected_product_ids ), count( $response_products ) ); - foreach ( $response_products as $response_product ) { - $this->assertContains( $response_product['id'], $expected_product_ids ); - } - } -} diff --git a/tests/unit-tests/api/reports-coupons-totals.php b/tests/unit-tests/api/reports-coupons-totals.php deleted file mode 100644 index cc4e1ef2de0..00000000000 --- a/tests/unit-tests/api/reports-coupons-totals.php +++ /dev/null @@ -1,103 +0,0 @@ -user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * - * @since 3.5.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v3/reports/coupons/totals', $routes ); - } - - /** - * Test getting all product reviews. - * - * @since 3.5.0 - */ - public function test_get_reports() { - global $wpdb; - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/coupons/totals' ) ); - $report = $response->get_data(); - $types = wc_get_coupon_types(); - $data = array(); - - foreach ( $types as $slug => $name ) { - $results = $wpdb->get_results( - $wpdb->prepare( - " - SELECT count(meta_id) AS total - FROM $wpdb->postmeta - WHERE meta_key = 'discount_type' - AND meta_value = %s - ", - $slug - ) - ); - - $total = isset( $results[0] ) ? (int) $results[0]->total : 0; - - $data[] = array( - 'slug' => $slug, - 'name' => $name, - 'total' => $total, - ); - } - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( count( $types ), count( $report ) ); - $this->assertEquals( $data, $report ); - } - - /** - * Tests to make sure product reviews cannot be viewed without valid permissions. - * - * @since 3.5.0 - */ - public function test_get_reports_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/coupons/totals' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test the product review schema. - * - * @since 3.5.0 - */ - public function test_product_review_schema() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/reports/coupons/totals' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 3, count( $properties ) ); - $this->assertArrayHasKey( 'slug', $properties ); - $this->assertArrayHasKey( 'name', $properties ); - $this->assertArrayHasKey( 'total', $properties ); - } -} diff --git a/tests/unit-tests/api/reports-customers-totals.php b/tests/unit-tests/api/reports-customers-totals.php deleted file mode 100644 index 5b58d995d57..00000000000 --- a/tests/unit-tests/api/reports-customers-totals.php +++ /dev/null @@ -1,119 +0,0 @@ -user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * - * @since 3.5.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v3/reports/customers/totals', $routes ); - } - - /** - * Test getting all product reviews. - * - * @since 3.5.0 - */ - public function test_get_reports() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/customers/totals' ) ); - $report = $response->get_data(); - $users_count = count_users(); - $total_customers = 0; - - foreach ( $users_count['avail_roles'] as $role => $total ) { - if ( in_array( $role, array( 'administrator', 'shop_manager' ), true ) ) { - continue; - } - - $total_customers += (int) $total; - } - - $customers_query = new WP_User_Query( - array( - 'role__not_in' => array( 'administrator', 'shop_manager' ), - 'number' => 0, - 'fields' => 'ID', - 'count_total' => true, - 'meta_query' => array( // WPCS: slow query ok. - array( - 'key' => 'paying_customer', - 'value' => 1, - 'compare' => '=', - ), - ), - ) - ); - - $total_paying = (int) $customers_query->get_total(); - - $data = array( - array( - 'slug' => 'paying', - 'name' => __( 'Paying customer', 'woocommerce' ), - 'total' => $total_paying, - ), - array( - 'slug' => 'non_paying', - 'name' => __( 'Non-paying customer', 'woocommerce' ), - 'total' => $total_customers - $total_paying, - ), - ); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 2, count( $report ) ); - $this->assertEquals( $data, $report ); - } - - /** - * Tests to make sure product reviews cannot be viewed without valid permissions. - * - * @since 3.5.0 - */ - public function test_get_reports_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/customers/totals' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test the product review schema. - * - * @since 3.5.0 - */ - public function test_product_review_schema() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/reports/customers/totals' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 3, count( $properties ) ); - $this->assertArrayHasKey( 'slug', $properties ); - $this->assertArrayHasKey( 'name', $properties ); - $this->assertArrayHasKey( 'total', $properties ); - } -} diff --git a/tests/unit-tests/api/reports-orders-totals.php b/tests/unit-tests/api/reports-orders-totals.php deleted file mode 100644 index 51508045e54..00000000000 --- a/tests/unit-tests/api/reports-orders-totals.php +++ /dev/null @@ -1,92 +0,0 @@ -user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * - * @since 3.5.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v3/reports/orders/totals', $routes ); - } - - /** - * Test getting all product reviews. - * - * @since 3.5.0 - */ - public function test_get_reports() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/orders/totals' ) ); - $report = $response->get_data(); - $totals = wp_count_posts( 'shop_order' ); - $data = array(); - - foreach ( wc_get_order_statuses() as $slug => $name ) { - if ( ! isset( $totals->$slug ) ) { - continue; - } - - $data[] = array( - 'slug' => str_replace( 'wc-', '', $slug ), - 'name' => $name, - 'total' => (int) $totals->$slug, - ); - } - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( count( wc_get_order_statuses() ), count( $report ) ); - $this->assertEquals( $data, $report ); - } - - /** - * Tests to make sure product reviews cannot be viewed without valid permissions. - * - * @since 3.5.0 - */ - public function test_get_reports_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/orders/totals' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test the product review schema. - * - * @since 3.5.0 - */ - public function test_product_review_schema() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/reports/orders/totals' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 3, count( $properties ) ); - $this->assertArrayHasKey( 'slug', $properties ); - $this->assertArrayHasKey( 'name', $properties ); - $this->assertArrayHasKey( 'total', $properties ); - } -} diff --git a/tests/unit-tests/api/reports-products-totals.php b/tests/unit-tests/api/reports-products-totals.php deleted file mode 100644 index 30e6c34c865..00000000000 --- a/tests/unit-tests/api/reports-products-totals.php +++ /dev/null @@ -1,99 +0,0 @@ -user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * - * @since 3.5.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v3/reports/products/totals', $routes ); - } - - /** - * Test getting all product reviews. - * - * @since 3.5.0 - */ - public function test_get_reports() { - wp_set_current_user( $this->user ); - WC_Install::create_terms(); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/products/totals' ) ); - $report = $response->get_data(); - $types = wc_get_product_types(); - $terms = get_terms( - array( - 'taxonomy' => 'product_type', - 'hide_empty' => false, - ) - ); - $data = array(); - - foreach ( $terms as $product_type ) { - if ( ! isset( $types[ $product_type->name ] ) ) { - continue; - } - - $data[] = array( - 'slug' => $product_type->name, - 'name' => $types[ $product_type->name ], - 'total' => (int) $product_type->count, - ); - } - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( count( $types ), count( $report ) ); - $this->assertEquals( $data, $report ); - } - - /** - * Tests to make sure product reviews cannot be viewed without valid permissions. - * - * @since 3.5.0 - */ - public function test_get_reports_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/products/totals' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test the product review schema. - * - * @since 3.5.0 - */ - public function test_product_review_schema() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/reports/products/totals' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 3, count( $properties ) ); - $this->assertArrayHasKey( 'slug', $properties ); - $this->assertArrayHasKey( 'name', $properties ); - $this->assertArrayHasKey( 'total', $properties ); - } -} diff --git a/tests/unit-tests/api/reports-reviews-totals.php b/tests/unit-tests/api/reports-reviews-totals.php deleted file mode 100644 index ab5f7a4bd33..00000000000 --- a/tests/unit-tests/api/reports-reviews-totals.php +++ /dev/null @@ -1,98 +0,0 @@ -user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * - * @since 3.5.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v3/reports/reviews/totals', $routes ); - } - - /** - * Test getting all product reviews. - * - * @since 3.5.0 - */ - public function test_get_reports() { - global $wpdb; - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/reviews/totals' ) ); - $report = $response->get_data(); - $data = array(); - - $query_data = array( - 'count' => true, - 'post_type' => 'product', - 'meta_key' => 'rating', // WPCS: slow query ok. - 'meta_value' => '', // WPCS: slow query ok. - ); - - for ( $i = 1; $i <= 5; $i++ ) { - $query_data['meta_value'] = $i; - - $data[] = array( - 'slug' => 'rated_' . $i . '_out_of_5', - /* translators: %s: average rating */ - 'name' => sprintf( __( 'Rated %s out of 5', 'woocommerce' ), $i ), - 'total' => (int) get_comments( $query_data ), - ); - } - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( count( $data ), count( $report ) ); - $this->assertEquals( $data, $report ); - } - - /** - * Tests to make sure product reviews cannot be viewed without valid permissions. - * - * @since 3.5.0 - */ - public function test_get_reports_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/reviews/totals' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test the product review schema. - * - * @since 3.5.0 - */ - public function test_product_review_schema() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/reports/reviews/totals' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 3, count( $properties ) ); - $this->assertArrayHasKey( 'slug', $properties ); - $this->assertArrayHasKey( 'name', $properties ); - $this->assertArrayHasKey( 'total', $properties ); - } -} diff --git a/tests/unit-tests/api/settings.php b/tests/unit-tests/api/settings.php deleted file mode 100644 index aa4f67cd02e..00000000000 --- a/tests/unit-tests/api/settings.php +++ /dev/null @@ -1,895 +0,0 @@ -endpoint = new WC_REST_Setting_Options_Controller(); - WC_Helper_Settings::register(); - $this->user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * - * @since 3.5.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v3/settings', $routes ); - $this->assertArrayHasKey( '/wc/v3/settings/(?P[\w-]+)', $routes ); - $this->assertArrayHasKey( '/wc/v3/settings/(?P[\w-]+)/(?P[\w-]+)', $routes ); - } - - /** - * Test getting all groups. - * - * @since 3.5.0 - */ - public function test_get_groups() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings' ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - - $this->assertContains( - array( - 'id' => 'test', - 'label' => 'Test extension', - 'parent_id' => '', - 'description' => 'My awesome test settings.', - 'sub_groups' => array( 'sub-test' ), - '_links' => array( - 'options' => array( - array( - 'href' => rest_url( '/wc/v3/settings/test' ), - ), - ), - ), - ), - $data - ); - - $this->assertContains( - array( - 'id' => 'sub-test', - 'label' => 'Sub test', - 'parent_id' => 'test', - 'description' => '', - 'sub_groups' => array(), - '_links' => array( - 'options' => array( - array( - 'href' => rest_url( '/wc/v3/settings/sub-test' ), - ), - ), - ), - ), - $data - ); - } - - /** - * Test /settings without valid permissions/creds. - * - * @since 3.5.0 - */ - public function test_get_groups_without_permission() { - wp_set_current_user( 0 ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test /settings without valid permissions/creds. - * - * @since 3.5.0 - * @covers WC_Rest_Settings_Controller::get_items - */ - public function test_get_groups_none_registered() { - wp_set_current_user( $this->user ); - - remove_all_filters( 'woocommerce_settings_groups' ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings' ) ); - $this->assertEquals( 500, $response->get_status() ); - - WC_Helper_Settings::register(); - } - - /** - * Test groups schema. - * - * @since 3.5.0 - */ - public function test_get_group_schema() { - $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/settings' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - $this->assertEquals( 5, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'parent_id', $properties ); - $this->assertArrayHasKey( 'label', $properties ); - $this->assertArrayHasKey( 'description', $properties ); - $this->assertArrayHasKey( 'sub_groups', $properties ); - } - - /** - * Test settings schema. - * - * @since 3.5.0 - */ - public function test_get_setting_schema() { - $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/settings/test/woocommerce_shop_page_display' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - $this->assertEquals( 10, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'label', $properties ); - $this->assertArrayHasKey( 'description', $properties ); - $this->assertArrayHasKey( 'value', $properties ); - $this->assertArrayHasKey( 'default', $properties ); - $this->assertArrayHasKey( 'tip', $properties ); - $this->assertArrayHasKey( 'placeholder', $properties ); - $this->assertArrayHasKey( 'type', $properties ); - $this->assertArrayHasKey( 'options', $properties ); - $this->assertArrayHasKey( 'group_id', $properties ); - } - - /** - * Test getting a single group. - * - * @since 3.5.0 - */ - public function test_get_group() { - wp_set_current_user( $this->user ); - - // test route callback receiving an empty group id - $result = $this->endpoint->get_group_settings( '' ); - $this->assertIsWPError( $result ); - - // test getting a group that does not exist - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/not-real' ) ); - $this->assertEquals( 404, $response->get_status() ); - - // test getting the 'invalid' group - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/invalid' ) ); - $this->assertEquals( 404, $response->get_status() ); - - // test getting a valid group with settings attached to it - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test' ) ); - $data = $response->get_data(); - $this->assertEquals( 1, count( $data ) ); - $this->assertEquals( 'woocommerce_shop_page_display', $data[0]['id'] ); - $this->assertEmpty( $data[0]['value'] ); - } - - /** - * Test getting a single group without permission. - * - * @since 3.5.0 - */ - public function test_get_group_without_permission() { - wp_set_current_user( 0 ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/coupon-data' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test updating a single setting. - * - * @since 3.5.0 - */ - public function test_update_setting() { - wp_set_current_user( $this->user ); - - // test defaults first - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test/woocommerce_shop_page_display' ) ); - $data = $response->get_data(); - $this->assertEquals( '', $data['value'] ); - - // test updating shop display setting - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) ); - $request->set_body_params( - array( - 'value' => 'both', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'both', $data['value'] ); - $this->assertEquals( 'both', get_option( 'woocommerce_shop_page_display' ) ); - - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) ); - $request->set_body_params( - array( - 'value' => 'subcategories', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'subcategories', $data['value'] ); - $this->assertEquals( 'subcategories', get_option( 'woocommerce_shop_page_display' ) ); - - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) ); - $request->set_body_params( - array( - 'value' => '', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( '', $data['value'] ); - $this->assertEquals( '', get_option( 'woocommerce_shop_page_display' ) ); - } - - /** - * Test updating multiple settings at once. - * - * @since 3.5.0 - */ - public function test_update_settings() { - wp_set_current_user( $this->user ); - - // test defaults first - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test' ) ); - $data = $response->get_data(); - $this->assertEquals( '', $data[0]['value'] ); - - // test setting both at once - $request = new WP_REST_Request( 'POST', '/wc/v3/settings/test/batch' ); - $request->set_body_params( - array( - 'update' => array( - array( - 'id' => 'woocommerce_shop_page_display', - 'value' => 'both', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'both', $data['update'][0]['value'] ); - $this->assertEquals( 'both', get_option( 'woocommerce_shop_page_display' ) ); - - // test updating one, but making sure the other value stays the same - $request = new WP_REST_Request( 'POST', '/wc/v3/settings/test/batch' ); - $request->set_body_params( - array( - 'update' => array( - array( - 'id' => 'woocommerce_shop_page_display', - 'value' => 'subcategories', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( 'subcategories', $data['update'][0]['value'] ); - $this->assertEquals( 'subcategories', get_option( 'woocommerce_shop_page_display' ) ); - } - - /** - * Test getting a single setting. - * - * @since 3.5.0 - */ - public function test_get_setting() { - wp_set_current_user( $this->user ); - - // test getting an invalid setting from a group that does not exist - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/not-real/woocommerce_shop_page_display' ) ); - $data = $response->get_data(); - $this->assertEquals( 404, $response->get_status() ); - - // test getting an invalid setting from a group that does exist - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/invalid/invalid' ) ); - $data = $response->get_data(); - $this->assertEquals( 404, $response->get_status() ); - - // test getting a valid setting - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test/woocommerce_shop_page_display' ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - - $this->assertEquals( 'woocommerce_shop_page_display', $data['id'] ); - $this->assertEquals( 'Shop page display', $data['label'] ); - $this->assertEquals( '', $data['default'] ); - $this->assertEquals( 'select', $data['type'] ); - $this->assertEquals( '', $data['value'] ); - } - - /** - * Test getting a single setting without valid user permissions. - * - * @since 3.5.0 - */ - public function test_get_setting_without_permission() { - wp_set_current_user( 0 ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test/woocommerce_shop_page_display' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Tests the GET single setting route handler receiving an empty setting ID. - * - * @since 3.5.0 - */ - public function test_get_setting_empty_setting_id() { - $result = $this->endpoint->get_setting( 'test', '' ); - - $this->assertIsWPError( $result ); - } - - /** - * Tests the GET single setting route handler receiving an invalid setting ID. - * - * @since 3.5.0 - */ - public function test_get_setting_invalid_setting_id() { - $result = $this->endpoint->get_setting( 'test', 'invalid' ); - - $this->assertIsWPError( $result ); - } - - /** - * Tests the GET single setting route handler encountering an invalid setting type. - * - * @since 3.5.0 - */ - public function test_get_setting_invalid_setting_type() { - // $controller = $this->getMock( 'WC_Rest_Setting_Options_Controller', array( 'get_group_settings', 'is_setting_type_valid' ) ); - $controller = $this->getMockBuilder( 'WC_Rest_Setting_Options_Controller' )->setMethods( array( 'get_group_settings', 'is_setting_type_valid' ) )->getMock(); - - $controller - ->expects( $this->any() ) - ->method( 'get_group_settings' ) - ->will( $this->returnValue( WC_Helper_Settings::register_test_settings( array() ) ) ); - - $controller - ->expects( $this->any() ) - ->method( 'is_setting_type_valid' ) - ->will( $this->returnValue( false ) ); - - $result = $controller->get_setting( 'test', 'woocommerce_shop_page_display' ); - - $this->assertIsWPError( $result ); - } - - /** - * Test updating a single setting without valid user permissions. - * - * @since 3.5.0 - */ - public function test_update_setting_without_permission() { - wp_set_current_user( 0 ); - - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) ); - $request->set_body_params( - array( - 'value' => 'subcategories', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - - /** - * Test updating multiple settings without valid user permissions. - * - * @since 3.5.0 - */ - public function test_update_settings_without_permission() { - wp_set_current_user( 0 ); - - $request = new WP_REST_Request( 'POST', '/wc/v3/settings/test/batch' ); - $request->set_body_params( - array( - 'update' => array( - array( - 'id' => 'woocommerce_shop_page_display', - 'value' => 'subcategories', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test updating a bad setting ID. - * - * @since 3.5.0 - * @covers WC_Rest_Setting_Options_Controller::update_item - */ - public function test_update_setting_bad_setting_id() { - wp_set_current_user( $this->user ); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/settings/test/invalid' ); - $request->set_body_params( - array( - 'value' => 'test', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Tests our classic setting registration to make sure settings added for WP-Admin are available over the API. - * - * @since 3.5.0 - */ - public function test_classic_settings() { - wp_set_current_user( $this->user ); - - // Make sure the group is properly registered - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/products' ) ); - $data = $response->get_data(); - $this->assertTrue( is_array( $data ) ); - $this->assertContains( - array( - 'id' => 'woocommerce_downloads_require_login', - 'label' => 'Access restriction', - 'description' => 'Downloads require login', - 'type' => 'checkbox', - 'default' => 'no', - 'tip' => 'This setting does not apply to guest purchases.', - 'value' => 'no', - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v3/settings/products/woocommerce_downloads_require_login' ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v3/settings/products' ), - ), - ), - ), - ), - $data - ); - - // test get single - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/products/woocommerce_dimension_unit' ) ); - $data = $response->get_data(); - - $this->assertEquals( 'cm', $data['default'] ); - - // test update - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'products', 'woocommerce_dimension_unit' ) ); - $request->set_body_params( - array( - 'value' => 'yd', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'yd', $data['value'] ); - $this->assertEquals( 'yd', get_option( 'woocommerce_dimension_unit' ) ); - } - - /** - * Tests our email etting registration to make sure settings added for WP-Admin are available over the API. - * - * @since 3.5.0 - */ - public function test_email_settings() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/email_new_order' ) ); - $settings = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - - $this->assertContains( - array( - 'id' => 'recipient', - 'label' => 'Recipient(s)', - 'description' => 'Enter recipients (comma separated) for this email. Defaults to admin@example.org.', - 'type' => 'text', - 'default' => '', - 'tip' => 'Enter recipients (comma separated) for this email. Defaults to admin@example.org.', - 'value' => '', - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v3/settings/email_new_order/recipient' ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v3/settings/email_new_order' ), - ), - ), - ), - ), - $settings - ); - - // test get single - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/email_new_order/subject' ) ); - $setting = $response->get_data(); - - $this->assertEquals( - array( - 'id' => 'subject', - 'label' => 'Subject', - 'description' => 'Available placeholders: {site_title}, {order_date}, {order_number}', - 'type' => 'text', - 'default' => '', - 'tip' => 'Available placeholders: {site_title}, {order_date}, {order_number}', - 'value' => '', - 'group_id' => 'email_new_order', - ), - $setting - ); - - // test update - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_new_order', 'subject' ) ); - $request->set_body_params( - array( - 'value' => 'This is my subject', - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - - $this->assertEquals( - array( - 'id' => 'subject', - 'label' => 'Subject', - 'description' => 'Available placeholders: {site_title}, {order_date}, {order_number}', - 'type' => 'text', - 'default' => '', - 'tip' => 'Available placeholders: {site_title}, {order_date}, {order_number}', - 'value' => 'This is my subject', - 'group_id' => 'email_new_order', - ), - $setting - ); - - // test updating another subject and making sure it works with a "similar" id - $request = new WP_REST_Request( 'GET', sprintf( '/wc/v3/settings/%s/%s', 'email_customer_new_account', 'subject' ) ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - - $this->assertEmpty( $setting['value'] ); - - // test update - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_customer_new_account', 'subject' ) ); - $request->set_body_params( - array( - 'value' => 'This is my new subject', - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - - $this->assertEquals( 'This is my new subject', $setting['value'] ); - - // make sure the other is what we left it - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/email_new_order/subject' ) ); - $setting = $response->get_data(); - - $this->assertEquals( 'This is my subject', $setting['value'] ); - } - - /** - * Test validation of checkbox settings. - * - * @since 3.5.0 - */ - public function test_validation_checkbox() { - wp_set_current_user( $this->user ); - - // test bogus value - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_cancelled_order', 'enabled' ) ); - $request->set_body_params( - array( - 'value' => 'not_yes_or_no', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - - // test yes - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_cancelled_order', 'enabled' ) ); - $request->set_body_params( - array( - 'value' => 'yes', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - - // test no - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_cancelled_order', 'enabled' ) ); - $request->set_body_params( - array( - 'value' => 'no', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - } - - /** - * Test validation of radio settings. - * - * @since 3.5.0 - */ - public function test_validation_radio() { - wp_set_current_user( $this->user ); - - // not a valid option - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) ); - $request->set_body_params( - array( - 'value' => 'billing2', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - - // valid - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) ); - $request->set_body_params( - array( - 'value' => 'billing', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - } - - /** - * Test validation of multiselect. - * - * @since 3.5.0 - */ - public function test_validation_multiselect() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v3/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) ) ); - $setting = $response->get_data(); - $this->assertEmpty( $setting['value'] ); - - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) ); - $request->set_body_params( - array( - 'value' => array( 'AX', 'DZ', 'MMM' ), - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( array( 'AX', 'DZ' ), $setting['value'] ); - } - - /** - * Test validation of select. - * - * @since 3.5.0 - */ - public function test_validation_select() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v3/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) ) ); - $setting = $response->get_data(); - $this->assertEquals( 'kg', $setting['value'] ); - - // invalid - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) ); - $request->set_body_params( - array( - 'value' => 'pounds', // invalid, should be lbs - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - - // valid - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) ); - $request->set_body_params( - array( - 'value' => 'lbs', // invalid, should be lbs - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( 'lbs', $setting['value'] ); - } - - /** - * Test to make sure the 'base location' setting is present in the response. - * That it is returned as 'select' and not 'single_select_country', - * and that both state and country options are returned. - * - * @since 3.5.0 - */ - public function test_woocommerce_default_country() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_default_country' ) ); - $setting = $response->get_data(); - - $this->assertEquals( 'select', $setting['type'] ); - $this->assertArrayHasKey( 'GB', $setting['options'] ); - $this->assertArrayHasKey( 'US:OR', $setting['options'] ); - } - - /** - * Test to make sure the store address setting can be fetched and updated. - * - * @since 3.5.0 - */ - public function test_woocommerce_store_address() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_store_address' ) ); - $setting = $response->get_data(); - $this->assertEquals( 'text', $setting['type'] ); - - // Repalce the old value with something uniquely new - $old_value = $setting['value']; - $new_value = $old_value . ' ' . rand( 1000, 9999 ); - $request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_address' ); - $request->set_body_params( - array( - 'value' => $new_value, - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( $new_value, $setting['value'] ); - - // Put the original value back - $request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_address' ); - $request->set_body_params( - array( - 'value' => $old_value, - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( $old_value, $setting['value'] ); - } - - /** - * Test to make sure the store address 2 (line 2) setting can be fetched and updated. - * - * @since 3.5.0 - */ - public function test_woocommerce_store_address_2() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_store_address_2' ) ); - $setting = $response->get_data(); - $this->assertEquals( 'text', $setting['type'] ); - - // Repalce the old value with something uniquely new - $old_value = $setting['value']; - $new_value = $old_value . ' ' . rand( 1000, 9999 ); - $request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_address_2' ); - $request->set_body_params( - array( - 'value' => $new_value, - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( $new_value, $setting['value'] ); - - // Put the original value back - $request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_address_2' ); - $request->set_body_params( - array( - 'value' => $old_value, - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( $old_value, $setting['value'] ); - } - - /** - * Test to make sure the store city setting can be fetched and updated. - * - * @since 3.5.0 - */ - public function test_woocommerce_store_city() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_store_city' ) ); - $setting = $response->get_data(); - $this->assertEquals( 'text', $setting['type'] ); - - // Repalce the old value with something uniquely new - $old_value = $setting['value']; - $new_value = $old_value . ' ' . rand( 1000, 9999 ); - $request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_city' ); - $request->set_body_params( - array( - 'value' => $new_value, - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( $new_value, $setting['value'] ); - - // Put the original value back - $request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_city' ); - $request->set_body_params( - array( - 'value' => $old_value, - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( $old_value, $setting['value'] ); - } - - /** - * Test to make sure the store postcode setting can be fetched and updated. - * - * @since 3.5.0 - */ - public function test_woocommerce_store_postcode() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_store_postcode' ) ); - $setting = $response->get_data(); - $this->assertEquals( 'text', $setting['type'] ); - - // Repalce the old value with something uniquely new - $old_value = $setting['value']; - $new_value = $old_value . ' ' . rand( 1000, 9999 ); - $request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_postcode' ); - $request->set_body_params( - array( - 'value' => $new_value, - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( $new_value, $setting['value'] ); - - // Put the original value back - $request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_postcode' ); - $request->set_body_params( - array( - 'value' => $old_value, - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( $old_value, $setting['value'] ); - } -} diff --git a/tests/unit-tests/api/shipping-methods.php b/tests/unit-tests/api/shipping-methods.php deleted file mode 100644 index ad139c427af..00000000000 --- a/tests/unit-tests/api/shipping-methods.php +++ /dev/null @@ -1,143 +0,0 @@ -endpoint = new WC_REST_Shipping_Methods_Controller(); - $this->user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * - * @since 3.5.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v3/shipping_methods', $routes ); - $this->assertArrayHasKey( '/wc/v3/shipping_methods/(?P[\w-]+)', $routes ); - } - - /** - * Test getting all shipping methods. - * - * @since 3.5.0 - */ - public function test_get_shipping_methods() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods' ) ); - $methods = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertContains( - array( - 'id' => 'free_shipping', - 'title' => 'Free shipping', - 'description' => 'Free shipping is a special method which can be triggered with coupons and minimum spends.', - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v3/shipping_methods/free_shipping' ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v3/shipping_methods' ), - ), - ), - ), - ), - $methods - ); - } - - /** - * Tests to make sure shipping methods cannot viewed without valid permissions. - * - * @since 3.5.0 - */ - public function test_get_shipping_methods_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Tests getting a single shipping method. - * - * @since 3.5.0 - */ - public function test_get_shipping_method() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods/local_pickup' ) ); - $method = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( - array( - 'id' => 'local_pickup', - 'title' => 'Local pickup', - 'description' => 'Allow customers to pick up orders themselves. By default, when using local pickup store base taxes will apply regardless of customer address.', - ), - $method - ); - } - - /** - * Tests getting a single shipping method without the correct permissions. - * - * @since 3.5.0 - */ - public function test_get_shipping_method_without_permission() { - wp_set_current_user( 0 ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods/local_pickup' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Tests getting a shipping method with an invalid ID. - * - * @since 3.5.0 - */ - public function test_get_shipping_method_invalid_id() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods/fake_method' ) ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test the shipping method schema. - * - * @since 3.5.0 - */ - public function test_shipping_method_schema() { - wp_set_current_user( $this->user ); - - $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/shipping_methods' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 3, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'title', $properties ); - $this->assertArrayHasKey( 'description', $properties ); - } -} diff --git a/tests/unit-tests/api/shipping-zones.php b/tests/unit-tests/api/shipping-zones.php deleted file mode 100644 index 580391d1b33..00000000000 --- a/tests/unit-tests/api/shipping-zones.php +++ /dev/null @@ -1,825 +0,0 @@ -endpoint = new WC_REST_Shipping_Zones_Controller(); - $this->user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - $this->zones = array(); - } - - /** - * Helper method to create a Shipping Zone. - * - * @param string $name Zone name. - * @param int $order Optional. Zone sort order. - * @return WC_Shipping_Zone - */ - protected function create_shipping_zone( $name, $order = 0, $locations = array() ) { - $zone = new WC_Shipping_Zone( null ); - $zone->set_zone_name( $name ); - $zone->set_zone_order( $order ); - $zone->set_locations( $locations ); - $zone->save(); - - $this->zones[] = $zone; - - return $zone; - } - - /** - * Test route registration. - * - * @since 3.5.0 - */ - 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]+)/locations', $routes ); - $this->assertArrayHasKey( '/wc/v3/shipping/zones/(?P[\d]+)/methods', $routes ); - $this->assertArrayHasKey( '/wc/v3/shipping/zones/(?P[\d]+)/methods/(?P[\d]+)', $routes ); - } - - /** - * Test getting all Shipping Zones. - * - * @since 3.5.0 - */ - public function test_get_zones() { - wp_set_current_user( $this->user ); - - // "Rest of the World" zone exists by default - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones' ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( count( $data ), 1 ); - $this->assertContains( - array( - 'id' => $data[0]['id'], - 'name' => 'Locations not covered by your other zones', - 'order' => 0, - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $data[0]['id'] ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones' ), - ), - ), - 'describedby' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $data[0]['id'] . '/locations' ), - ), - ), - ), - ), - $data - ); - - // Create a zone and make sure it's in the response - $this->create_shipping_zone( 'Zone 1' ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones' ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( count( $data ), 2 ); - $this->assertContains( - array( - 'id' => $data[1]['id'], - 'name' => 'Zone 1', - 'order' => 0, - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $data[1]['id'] ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones' ), - ), - ), - 'describedby' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $data[1]['id'] . '/locations' ), - ), - ), - ), - ), - $data - ); - } - - /** - * Test /shipping/zones without valid permissions/creds. - * - * @since 3.5.0 - */ - public function test_get_shipping_zones_without_permission() { - wp_set_current_user( 0 ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test /shipping/zones while Shipping is disabled in WooCommerce. - * - * @since 3.5.0 - */ - public function test_get_shipping_zones_disabled_shipping() { - wp_set_current_user( $this->user ); - - add_filter( 'wc_shipping_enabled', '__return_false' ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones' ) ); - $this->assertEquals( 404, $response->get_status() ); - - remove_filter( 'wc_shipping_enabled', '__return_false' ); - } - - /** - * Test Shipping Zone schema. - * - * @since 3.5.0 - */ - public function test_get_shipping_zone_schema() { - $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/shipping/zones' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - $this->assertEquals( 3, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertTrue( $properties['id']['readonly'] ); - $this->assertArrayHasKey( 'name', $properties ); - $this->assertArrayHasKey( 'order', $properties ); - } - - /** - * Test Shipping Zone create endpoint. - * - * @since 3.5.0 - */ - public function test_create_shipping_zone() { - wp_set_current_user( $this->user ); - - $request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones' ); - $request->set_body_params( - array( - 'name' => 'Test Zone', - 'order' => 1, - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 201, $response->get_status() ); - $this->assertEquals( - array( - 'id' => $data['id'], - 'name' => 'Test Zone', - 'order' => 1, - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $data['id'] ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones' ), - ), - ), - 'describedby' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $data['id'] . '/locations' ), - ), - ), - ), - ), - $data - ); - } - - /** - * Test Shipping Zone create endpoint. - * - * @since 3.5.0 - */ - public function test_create_shipping_zone_without_permission() { - wp_set_current_user( 0 ); - - $request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones' ); - $request->set_body_params( - array( - 'name' => 'Test Zone', - 'order' => 1, - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test Shipping Zone update endpoint. - * - * @since 3.5.0 - */ - public function test_update_shipping_zone() { - wp_set_current_user( $this->user ); - - $zone = $this->create_shipping_zone( 'Test Zone' ); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/shipping/zones/' . $zone->get_id() ); - $request->set_body_params( - array( - 'name' => 'Zone Test', - 'order' => 2, - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( - array( - 'id' => $zone->get_id(), - 'name' => 'Zone Test', - 'order' => 2, - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones' ), - ), - ), - 'describedby' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ), - ), - ), - ), - ), - $data - ); - } - - /** - * Test Shipping Zone update endpoint with a bad zone ID. - * - * @since 3.5.0 - */ - public function test_update_shipping_zone_invalid_id() { - wp_set_current_user( $this->user ); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/shipping/zones/555555' ); - $request->set_body_params( - array( - 'name' => 'Zone Test', - 'order' => 2, - ) - ); - $response = $this->server->dispatch( $request ); - - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test Shipping Zone delete endpoint. - * - * @since 3.5.0 - */ - public function test_delete_shipping_zone() { - wp_set_current_user( $this->user ); - $zone = $this->create_shipping_zone( 'Zone 1' ); - - $request = new WP_REST_Request( 'DELETE', '/wc/v3/shipping/zones/' . $zone->get_id() ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - } - - /** - * Test Shipping Zone delete endpoint without permissions. - * - * @since 3.5.0 - */ - public function test_delete_shipping_zone_without_permission() { - wp_set_current_user( 0 ); - $zone = $this->create_shipping_zone( 'Zone 1' ); - - $request = new WP_REST_Request( 'DELETE', '/wc/v3/shipping/zones/' . $zone->get_id() ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test Shipping Zone delete endpoint with a bad zone ID. - * - * @since 3.5.0 - */ - public function test_delete_shipping_zone_invalid_id() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'DELETE', '/wc/v3/shipping/zones/555555' ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test getting a single Shipping Zone. - * - * @since 3.5.0 - */ - public function test_get_single_shipping_zone() { - wp_set_current_user( $this->user ); - - $zone = $this->create_shipping_zone( 'Test Zone' ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( - array( - 'id' => $zone->get_id(), - 'name' => 'Test Zone', - 'order' => 0, - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones' ), - ), - ), - 'describedby' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ), - ), - ), - ), - ), - $data - ); - } - - /** - * Test getting a single Shipping Zone with a bad zone ID. - * - * @since 3.5.0 - */ - public function test_get_single_shipping_zone_invalid_id() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/1' ) ); - - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test getting Shipping Zone Locations. - * - * @since 3.5.0 - */ - public function test_get_locations() { - wp_set_current_user( $this->user ); - - // Create a zone - $zone = $this->create_shipping_zone( - 'Zone 1', - 0, - array( - array( - 'code' => 'US', - 'type' => 'country', - ), - ) - ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( count( $data ), 1 ); - $this->assertEquals( - array( - array( - 'code' => 'US', - 'type' => 'country', - '_links' => array( - 'collection' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ), - ), - ), - 'describes' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ), - ), - ), - ), - ), - ), - $data - ); - } - - /** - * Test getting Shipping Zone Locations with a bad zone ID. - * - * @since 3.5.0 - */ - public function test_get_locations_invalid_id() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/1/locations' ) ); - - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test Shipping Zone Locations update endpoint. - * - * @since 3.5.0 - */ - public function test_update_locations() { - wp_set_current_user( $this->user ); - - $zone = $this->create_shipping_zone( 'Test Zone' ); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ); - $request->add_header( 'Content-Type', 'application/json' ); - $request->set_body( - json_encode( - array( - array( - 'code' => 'UK', - 'type' => 'country', - ), - array( - 'code' => 'US', // test that locations missing "type" treated as country. - ), - array( - 'code' => 'SW1A0AA', - 'type' => 'postcode', - ), - array( - 'type' => 'continent', // test that locations missing "code" aren't saved - ), - ) - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 3, count( $data ) ); - $this->assertEquals( - array( - array( - 'code' => 'UK', - 'type' => 'country', - '_links' => array( - 'collection' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ), - ), - ), - 'describes' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ), - ), - ), - ), - ), - array( - 'code' => 'US', - 'type' => 'country', - '_links' => array( - 'collection' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ), - ), - ), - 'describes' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ), - ), - ), - ), - ), - array( - 'code' => 'SW1A0AA', - 'type' => 'postcode', - '_links' => array( - 'collection' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ), - ), - ), - 'describes' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ), - ), - ), - ), - ), - ), - $data - ); - } - - /** - * Test updating Shipping Zone Locations with a bad zone ID. - * - * @since 3.5.0 - */ - public function test_update_locations_invalid_id() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'PUT', '/wc/v3/shipping/zones/1/locations' ) ); - - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test getting all Shipping Zone Methods and getting a single Shipping Zone Method. - * - * @since 3.5.0 - */ - public function test_get_methods() { - wp_set_current_user( $this->user ); - - // Create a shipping method and make sure it's in the response - $zone = $this->create_shipping_zone( 'Zone 1' ); - $instance_id = $zone->add_shipping_method( 'flat_rate' ); - $methods = $zone->get_shipping_methods(); - $method = $methods[ $instance_id ]; - - $settings = array(); - $method->init_instance_settings(); - foreach ( $method->get_instance_form_fields() as $id => $field ) { - $data = array( - 'id' => $id, - 'label' => $field['title'], - 'description' => ( empty( $field['description'] ) ? '' : $field['description'] ), - 'type' => $field['type'], - 'value' => $method->instance_settings[ $id ], - 'default' => ( empty( $field['default'] ) ? '' : $field['default'] ), - 'tip' => ( empty( $field['description'] ) ? '' : $field['description'] ), - 'placeholder' => ( empty( $field['placeholder'] ) ? '' : $field['placeholder'] ), - ); - if ( ! empty( $field['options'] ) ) { - $data['options'] = $field['options']; - } - $settings[ $id ] = $data; - } - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods' ) ); - $data = $response->get_data(); - $expected = array( - 'id' => $instance_id, - 'instance_id' => $instance_id, - 'title' => $method->instance_settings['title'], - 'order' => $method->method_order, - 'enabled' => ( 'yes' === $method->enabled ), - 'method_id' => $method->id, - 'method_title' => $method->method_title, - 'method_description' => $method->method_description, - 'settings' => $settings, - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods' ), - ), - ), - 'describes' => array( - array( - 'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ), - ), - ), - ), - ); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( count( $data ), 1 ); - $this->assertContains( $expected, $data ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( $expected, $data ); - } - - /** - * Test getting all Shipping Zone Methods with a bad zone ID. - * - * @since 3.5.0 - */ - public function test_get_methods_invalid_zone_id() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/1/methods' ) ); - - $this->assertEquals( 404, $response->get_status() ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/1/methods/1' ) ); - - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test getting a single Shipping Zone Method with a bad ID. - * - * @since 3.5.0 - */ - public function test_get_methods_invalid_method_id() { - wp_set_current_user( $this->user ); - - $zone = $this->create_shipping_zone( 'Zone 1' ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/1' ) ); - - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test updating a Shipping Zone Method. - * - * @since 3.5.0 - */ - public function test_update_methods() { - wp_set_current_user( $this->user ); - - $zone = $this->create_shipping_zone( 'Zone 1' ); - $instance_id = $zone->add_shipping_method( 'flat_rate' ); - $methods = $zone->get_shipping_methods(); - $method = $methods[ $instance_id ]; - - // Test defaults - $request = new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertArrayHasKey( 'title', $data['settings'] ); - $this->assertEquals( 'Flat rate', $data['settings']['title']['value'] ); - $this->assertArrayHasKey( 'tax_status', $data['settings'] ); - $this->assertEquals( 'taxable', $data['settings']['tax_status']['value'] ); - $this->assertArrayHasKey( 'cost', $data['settings'] ); - $this->assertEquals( '0', $data['settings']['cost']['value'] ); - - // Update a single value - $request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ); - $request->set_body_params( - array( - 'settings' => array( - 'cost' => 5, - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertArrayHasKey( 'title', $data['settings'] ); - $this->assertEquals( 'Flat rate', $data['settings']['title']['value'] ); - $this->assertArrayHasKey( 'tax_status', $data['settings'] ); - $this->assertEquals( 'taxable', $data['settings']['tax_status']['value'] ); - $this->assertArrayHasKey( 'cost', $data['settings'] ); - $this->assertEquals( '5', $data['settings']['cost']['value'] ); - - // Test multiple settings - $request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ); - $request->set_body_params( - array( - 'settings' => array( - 'cost' => 10, - 'tax_status' => 'none', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertArrayHasKey( 'title', $data['settings'] ); - $this->assertEquals( 'Flat rate', $data['settings']['title']['value'] ); - $this->assertArrayHasKey( 'tax_status', $data['settings'] ); - $this->assertEquals( 'none', $data['settings']['tax_status']['value'] ); - $this->assertArrayHasKey( 'cost', $data['settings'] ); - $this->assertEquals( '10', $data['settings']['cost']['value'] ); - - // Test bogus - $request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ); - $request->set_body_params( - array( - 'settings' => array( - 'cost' => 10, - 'tax_status' => 'this_is_not_a_valid_option', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - - // Test other parameters - $this->assertTrue( $data['enabled'] ); - $this->assertEquals( 1, $data['order'] ); - - $request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ); - $request->set_body_params( - array( - 'enabled' => false, - 'order' => 2, - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertFalse( $data['enabled'] ); - $this->assertEquals( 2, $data['order'] ); - $this->assertArrayHasKey( 'cost', $data['settings'] ); - $this->assertEquals( '10', $data['settings']['cost']['value'] ); - } - - /** - * Test creating a Shipping Zone Method. - * - * @since 3.5.0 - */ - public function test_create_method() { - wp_set_current_user( $this->user ); - $zone = $this->create_shipping_zone( 'Zone 1' ); - $request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods' ); - $request->set_body_params( - array( - 'method_id' => 'flat_rate', - 'enabled' => false, - 'order' => 2, - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertFalse( $data['enabled'] ); - $this->assertEquals( 2, $data['order'] ); - $this->assertArrayHasKey( 'cost', $data['settings'] ); - $this->assertEquals( '0', $data['settings']['cost']['value'] ); - } - - /** - * Test deleting a Shipping Zone Method. - * - * @since 3.5.0 - */ - public function test_delete_method() { - wp_set_current_user( $this->user ); - $zone = $this->create_shipping_zone( 'Zone 1' ); - $instance_id = $zone->add_shipping_method( 'flat_rate' ); - $methods = $zone->get_shipping_methods(); - $method = $methods[ $instance_id ]; - $request = new WP_REST_Request( 'DELETE', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - } -} diff --git a/tests/unit-tests/api/system-status.php b/tests/unit-tests/api/system-status.php deleted file mode 100644 index 51f3016bcab..00000000000 --- a/tests/unit-tests/api/system-status.php +++ /dev/null @@ -1,467 +0,0 @@ -endpoint = new WC_REST_System_Status_Controller(); - $this->user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - - // 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' ); - } - - /** - * Test route registration. - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v3/system_status', $routes ); - $this->assertArrayHasKey( '/wc/v3/system_status/tools', $routes ); - $this->assertArrayHasKey( '/wc/v3/system_status/tools/(?P[\w-]+)', $routes ); - } - - /** - * Test to make sure system status cannot be accessed without valid creds - * - * @since 3.5.0 - */ - public function test_get_system_status_info_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test to make sure root properties are present. - * (environment, theme, database, etc). - * - * @since 3.5.0 - */ - public function test_get_system_status_info_returns_root_properties() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) ); - $data = $response->get_data(); - - $this->assertArrayHasKey( 'environment', $data ); - $this->assertArrayHasKey( 'database', $data ); - $this->assertArrayHasKey( 'active_plugins', $data ); - $this->assertArrayHasKey( 'theme', $data ); - $this->assertArrayHasKey( 'settings', $data ); - $this->assertArrayHasKey( 'security', $data ); - $this->assertArrayHasKey( 'pages', $data ); - } - - /** - * Test to make sure environment response is correct. - * - * @since 3.5.0 - */ - public function test_get_system_status_info_environment() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) ); - $data = $response->get_data(); - $environment = (array) $data['environment']; - - // Make sure all expected data is present. - $this->assertEquals( 32, count( $environment ) ); - - // Test some responses to make sure they match up. - $this->assertEquals( get_option( 'home' ), $environment['home_url'] ); - $this->assertEquals( get_option( 'siteurl' ), $environment['site_url'] ); - $this->assertEquals( WC()->version, $environment['version'] ); - } - - /** - * Test to make sure database response is correct. - * - * @since 3.5.0 - */ - public function test_get_system_status_info_database() { - global $wpdb; - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) ); - $data = $response->get_data(); - $database = (array) $data['database']; - - $this->assertEquals( get_option( 'woocommerce_db_version' ), $database['wc_database_version'] ); - $this->assertEquals( $wpdb->prefix, $database['database_prefix'] ); - $this->assertEquals( WC_Geolocation::get_local_database_path(), $database['maxmind_geoip_database'] ); - $this->assertArrayHasKey( 'woocommerce', $database['database_tables'], wc_print_r( $database, true ) ); - $this->assertArrayHasKey( $wpdb->prefix . 'woocommerce_payment_tokens', $database['database_tables']['woocommerce'], wc_print_r( $database, true ) ); - } - - /** - * Test to make sure active plugins response is correct. - * - * @since 3.5.0 - */ - public function test_get_system_status_info_active_plugins() { - wp_set_current_user( $this->user ); - - $actual_plugins = array( 'hello.php' ); - update_option( 'active_plugins', $actual_plugins ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) ); - update_option( 'active_plugins', array() ); - - $data = $response->get_data(); - $plugins = (array) $data['active_plugins']; - - $this->assertEquals( 1, count( $plugins ) ); - $this->assertEquals( 'Hello Dolly', $plugins[0]['name'] ); - } - - /** - * Test to make sure theme response is correct. - * - * @since 3.5.0 - */ - public function test_get_system_status_info_theme() { - wp_set_current_user( $this->user ); - $active_theme = wp_get_theme(); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) ); - $data = $response->get_data(); - $theme = (array) $data['theme']; - - $this->assertEquals( 13, count( $theme ) ); - $this->assertEquals( $active_theme->Name, $theme['name'] ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar - } - - /** - * Test to make sure settings response is correct. - * - * @since 3.5.0 - */ - public function test_get_system_status_info_settings() { - wp_set_current_user( $this->user ); - - $term_response = array(); - $terms = get_terms( 'product_type', array( 'hide_empty' => 0 ) ); - foreach ( $terms as $term ) { - $term_response[ $term->slug ] = strtolower( $term->name ); - } - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) ); - $data = $response->get_data(); - $settings = (array) $data['settings']; - - $this->assertEquals( 12, count( $settings ) ); - $this->assertEquals( ( 'yes' === get_option( 'woocommerce_api_enabled' ) ), $settings['api_enabled'] ); - $this->assertEquals( get_woocommerce_currency(), $settings['currency'] ); - $this->assertEquals( $term_response, $settings['taxonomies'] ); - } - - /** - * Test to make sure security response is correct. - * - * @since 3.5.0 - */ - public function test_get_system_status_info_security() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) ); - $data = $response->get_data(); - $settings = (array) $data['security']; - - $this->assertEquals( 2, count( $settings ) ); - $this->assertEquals( 'https' === substr( wc_get_page_permalink( 'shop' ), 0, 5 ), $settings['secure_connection'] ); - $this->assertEquals( ! ( defined( 'WP_DEBUG' ) && defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG && WP_DEBUG_DISPLAY ) || 0 === intval( ini_get( 'display_errors' ) ), $settings['hide_errors'] ); - } - - /** - * Test to make sure pages response is correct. - * - * @since 3.5.0 - */ - public function test_get_system_status_info_pages() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) ); - $data = $response->get_data(); - $pages = $data['pages']; - $this->assertEquals( 5, count( $pages ) ); - } - - /** - * Test system status schema. - * - * @since 3.5.0 - */ - public function test_system_status_schema() { - $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/system_status' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - $this->assertEquals( 9, count( $properties ) ); - $this->assertArrayHasKey( 'environment', $properties ); - $this->assertArrayHasKey( 'database', $properties ); - $this->assertArrayHasKey( 'active_plugins', $properties ); - $this->assertArrayHasKey( 'theme', $properties ); - $this->assertArrayHasKey( 'settings', $properties ); - $this->assertArrayHasKey( 'security', $properties ); - $this->assertArrayHasKey( 'pages', $properties ); - } - - /** - * Test to make sure get_items (all tools) response is correct. - * - * @since 3.5.0 - */ - public function test_get_system_tools() { - wp_set_current_user( $this->user ); - - $tools_controller = new WC_REST_System_Status_Tools_Controller(); - $raw_tools = $tools_controller->get_tools(); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status/tools' ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( count( $raw_tools ), count( $data ) ); - $this->assertContains( - array( - 'id' => 'regenerate_thumbnails', - 'name' => 'Regenerate shop thumbnails', - 'action' => 'Regenerate', - 'description' => 'This will regenerate all shop thumbnails to match your theme and/or image settings.', - '_links' => array( - 'item' => array( - array( - 'href' => rest_url( '/wc/v3/system_status/tools/regenerate_thumbnails' ), - 'embeddable' => 1, - ), - ), - ), - ), - $data - ); - - $query_params = array( - '_fields' => 'id,name,nonexisting', - ); - $request = new WP_REST_Request( 'GET', '/wc/v3/system_status/tools' ); - $request->set_query_params( $query_params ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( count( $raw_tools ), count( $data ) ); - $this->assertContains( - array( - 'id' => 'regenerate_thumbnails', - 'name' => 'Regenerate shop thumbnails', - ), - $data - ); - foreach ( $data as $item ) { - // Fields that are not requested are not returned in response. - $this->assertArrayNotHasKey( 'action', $item ); - $this->assertArrayNotHasKey( 'description', $item ); - // Links are part of data in collections, so excluded if not explicitly requested. - $this->assertArrayNotHasKey( '_links', $item ); - // Non existing field is ignored. - $this->assertArrayNotHasKey( 'nonexisting', $item ); - } - - // Links are part of data, not links in collections. - $links = $response->get_links(); - $this->assertEquals( 0, count( $links ) ); - } - - /** - * Test to make sure system status tools cannot be accessed without valid creds - * - * @since 3.5.0 - */ - public function test_get_system_status_tools_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status/tools' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test to make sure we can load a single tool correctly. - * - * @since 3.5.0 - */ - public function test_get_system_tool() { - wp_set_current_user( $this->user ); - - $tools_controller = new WC_REST_System_Status_Tools_Controller(); - $raw_tools = $tools_controller->get_tools(); - $raw_tool = $raw_tools['recount_terms']; - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status/tools/recount_terms' ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - - $this->assertEquals( 'recount_terms', $data['id'] ); - $this->assertEquals( 'Term counts', $data['name'] ); - $this->assertEquals( 'Recount terms', $data['action'] ); - $this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] ); - - // Test for _fields query parameter. - $query_params = array( - '_fields' => 'id,name,nonexisting', - ); - $request = new WP_REST_Request( 'GET', '/wc/v3/system_status/tools/recount_terms' ); - $request->set_query_params( $query_params ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - - $this->assertEquals( 'recount_terms', $data['id'] ); - $this->assertEquals( 'Term counts', $data['name'] ); - $this->assertArrayNotHasKey( 'action', $data ); - $this->assertArrayNotHasKey( 'description', $data ); - // Links are part of links, not data in single items. - $this->assertArrayNotHasKey( '_links', $data ); - - // Links are part of links, not data in single item response. - $links = $response->get_links(); - $this->assertEquals( 1, count( $links ) ); - } - - /** - * Test to make sure a single system status toolscannot be accessed without valid creds. - * - * @since 3.5.0 - */ - public function test_get_system_status_tool_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status/tools/recount_terms' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test to make sure we can RUN a tool correctly. - * - * @since 3.5.0 - */ - public function test_execute_system_tool() { - wp_set_current_user( $this->user ); - - $tools_controller = new WC_REST_System_Status_Tools_Controller(); - $raw_tools = $tools_controller->get_tools(); - $raw_tool = $raw_tools['recount_terms']; - - $response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v3/system_status/tools/recount_terms' ) ); - $data = $response->get_data(); - - $this->assertEquals( 'recount_terms', $data['id'] ); - $this->assertEquals( 'Term counts', $data['name'] ); - $this->assertEquals( 'Recount terms', $data['action'] ); - $this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] ); - $this->assertTrue( $data['success'] ); - $this->assertEquals( 1, did_action( 'woocommerce_rest_insert_system_status_tool' ) ); - - $response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v3/system_status/tools/not_a_real_tool' ) ); - $this->assertEquals( 404, $response->get_status() ); - - // Test _fields for execute system tool request. - $query_params = array( - '_fields' => 'id,success,nonexisting', - ); - $request = new WP_REST_Request( 'PUT', '/wc/v3/system_status/tools/recount_terms' ); - $request->set_query_params( $query_params ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 'recount_terms', $data['id'] ); - $this->assertTrue( $data['success'] ); - - // Fields that are not requested are not returned in response. - $this->assertArrayNotHasKey( 'action', $data ); - $this->assertArrayNotHasKey( 'name', $data ); - $this->assertArrayNotHasKey( 'description', $data ); - // Links are part of links, not data in single item response. - $this->assertArrayNotHasKey( '_links', $data ); - // Non existing field is ignored. - $this->assertArrayNotHasKey( 'nonexisting', $data ); - - // Links are part of links, not data in single item response. - $links = $response->get_links(); - $this->assertEquals( 1, count( $links ) ); - } - - /** - * Test to make sure a tool cannot be run without valid creds. - * - * @since 3.5.0 - */ - public function test_execute_system_status_tool_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v3/system_status/tools/recount_terms' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test system status schema. - * - * @since 3.5.0 - */ - public function test_system_status_tool_schema() { - $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/system_status/tools' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 6, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'name', $properties ); - $this->assertArrayHasKey( 'action', $properties ); - $this->assertArrayHasKey( 'description', $properties ); - $this->assertArrayHasKey( 'success', $properties ); - $this->assertArrayHasKey( 'message', $properties ); - } - - /** - * Provides a mocked response for external requests performed by WC_REST_System_Status_Controller. - * This way 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 ( in_array( $url, array( 'https://www.paypal.com/cgi-bin/webscr', 'https://woocommerce.com/wc-api/product-key-api?request=ping&network=0' ), true ) ) { - $mocked_response = array( - 'response' => array( 'code' => 200 ), - ); - } elseif ( 'https://api.wordpress.org/themes/info/1.0/' === $url ) { - $mocked_response = array( - 'body' => 'O:8:"stdClass":12:{s:4:"name";s:7:"Default";s:4:"slug";s:7:"default";s:7:"version";s:5:"1.7.2";s:11:"preview_url";s:29:"https://wp-themes.com/default";s:6:"author";s:15:"wordpressdotorg";s:14:"screenshot_url";s:61:"//ts.w.org/wp-content/themes/default/screenshot.png?ver=1.7.2";s:6:"rating";d:100;s:11:"num_ratings";s:1:"3";s:10:"downloaded";i:296618;s:12:"last_updated";s:10:"2010-06-14";s:8:"homepage";s:37:"https://wordpress.org/themes/default/";s:13:"download_link";s:55:"https://downloads.wordpress.org/theme/default.1.7.2.zip";}', - 'response' => array( 'code' => 200 ), - ); - } - - return $mocked_response; - } -} diff --git a/tests/unit-tests/api/v2/coupons.php b/tests/unit-tests/api/v2/coupons.php deleted file mode 100644 index 592357327f7..00000000000 --- a/tests/unit-tests/api/v2/coupons.php +++ /dev/null @@ -1,471 +0,0 @@ -endpoint = new WC_REST_Coupons_Controller(); - $this->user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * @since 3.0.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v2/coupons', $routes ); - $this->assertArrayHasKey( '/wc/v2/coupons/(?P[\d]+)', $routes ); - $this->assertArrayHasKey( '/wc/v2/coupons/batch', $routes ); - } - - /** - * Test getting coupons. - * @since 3.0.0 - */ - public function test_get_coupons() { - wp_set_current_user( $this->user ); - - $coupon_1 = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' ); - $post_1 = get_post( $coupon_1->get_id() ); - $coupon_2 = WC_Helper_Coupon::create_coupon( 'dummycoupon-2' ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons' ) ); - $coupons = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 2, count( $coupons ) ); - $this->assertContains( - array( - 'id' => $coupon_1->get_id(), - 'code' => 'dummycoupon-1', - 'amount' => '1.00', - 'date_created' => wc_rest_prepare_date_response( $post_1->post_date_gmt, false ), - 'date_created_gmt' => wc_rest_prepare_date_response( $post_1->post_date_gmt ), - 'date_modified' => wc_rest_prepare_date_response( $post_1->post_modified_gmt, false ), - 'date_modified_gmt' => wc_rest_prepare_date_response( $post_1->post_modified_gmt ), - 'discount_type' => 'fixed_cart', - 'description' => 'This is a dummy coupon', - 'date_expires' => '', - 'date_expires_gmt' => '', - 'usage_count' => 0, - 'individual_use' => false, - 'product_ids' => array(), - 'excluded_product_ids' => array(), - 'usage_limit' => '', - 'usage_limit_per_user' => '', - 'limit_usage_to_x_items' => null, - 'free_shipping' => false, - 'product_categories' => array(), - 'excluded_product_categories' => array(), - 'exclude_sale_items' => false, - 'minimum_amount' => '0.00', - 'maximum_amount' => '0.00', - 'email_restrictions' => array(), - 'used_by' => array(), - 'meta_data' => array(), - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v2/coupons/' . $coupon_1->get_id() ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v2/coupons' ), - ), - ), - ), - ), - $coupons - ); - } - - /** - * Test getting coupons without valid permissions. - * @since 3.0.0 - */ - public function test_get_coupons_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test getting a single coupon. - * @since 3.0.0 - */ - public function test_get_coupon() { - wp_set_current_user( $this->user ); - $coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' ); - $post = get_post( $coupon->get_id() ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/' . $coupon->get_id() ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( - array( - 'id' => $coupon->get_id(), - 'code' => 'dummycoupon-1', - 'amount' => '1.00', - 'date_created' => wc_rest_prepare_date_response( $post->post_date_gmt, false ), - 'date_created_gmt' => wc_rest_prepare_date_response( $post->post_date_gmt ), - 'date_modified' => wc_rest_prepare_date_response( $post->post_modified_gmt, false ), - 'date_modified_gmt' => wc_rest_prepare_date_response( $post->post_modified_gmt ), - 'discount_type' => 'fixed_cart', - 'description' => 'This is a dummy coupon', - 'date_expires' => null, - 'date_expires_gmt' => null, - 'usage_count' => 0, - 'individual_use' => false, - 'product_ids' => array(), - 'excluded_product_ids' => array(), - 'usage_limit' => null, - 'usage_limit_per_user' => null, - 'limit_usage_to_x_items' => null, - 'free_shipping' => false, - 'product_categories' => array(), - 'excluded_product_categories' => array(), - 'exclude_sale_items' => false, - 'minimum_amount' => '0.00', - 'maximum_amount' => '0.00', - 'email_restrictions' => array(), - 'used_by' => array(), - 'meta_data' => array(), - ), - $data - ); - } - - /** - * Test getting a single coupon with an invalid ID. - * @since 3.0.0 - */ - public function test_get_coupon_invalid_id() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/0' ) ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test getting a single coupon without valid permissions. - * @since 3.0.0 - */ - public function test_get_coupon_without_permission() { - wp_set_current_user( 0 ); - $coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/' . $coupon->get_id() ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test creating a single coupon. - * @since 3.0.0 - */ - public function test_create_coupon() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'POST', '/wc/v2/coupons' ); - $request->set_body_params( - array( - 'code' => 'test', - 'amount' => '5.00', - 'discount_type' => 'fixed_product', - 'description' => 'Test', - 'usage_limit' => 10, - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 201, $response->get_status() ); - $this->assertEquals( - array( - 'id' => $data['id'], - 'code' => 'test', - 'amount' => '5.00', - 'date_created' => $data['date_created'], - 'date_created_gmt' => $data['date_created_gmt'], - 'date_modified' => $data['date_modified'], - 'date_modified_gmt' => $data['date_modified_gmt'], - 'discount_type' => 'fixed_product', - 'description' => 'Test', - 'date_expires' => null, - 'date_expires_gmt' => null, - 'usage_count' => 0, - 'individual_use' => false, - 'product_ids' => array(), - 'excluded_product_ids' => array(), - 'usage_limit' => 10, - 'usage_limit_per_user' => null, - 'limit_usage_to_x_items' => null, - 'free_shipping' => false, - 'product_categories' => array(), - 'excluded_product_categories' => array(), - 'exclude_sale_items' => false, - 'minimum_amount' => '0.00', - 'maximum_amount' => '0.00', - 'email_restrictions' => array(), - 'used_by' => array(), - 'meta_data' => array(), - ), - $data - ); - } - - /** - * Test creating a single coupon with invalid fields. - * @since 3.0.0 - */ - public function test_create_coupon_invalid_fields() { - wp_set_current_user( $this->user ); - - // test no code... - $request = new WP_REST_Request( 'POST', '/wc/v2/coupons' ); - $request->set_body_params( - array( - 'amount' => '5.00', - 'discount_type' => 'fixed_product', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 400, $response->get_status() ); - } - - /** - * Test creating a single coupon without valid permissions. - * @since 3.0.0 - */ - public function test_create_coupon_without_permission() { - wp_set_current_user( 0 ); - - // test no code... - $request = new WP_REST_Request( 'POST', '/wc/v2/coupons' ); - $request->set_body_params( - array( - 'code' => 'fail', - 'amount' => '5.00', - 'discount_type' => 'fixed_product', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test updating a single coupon. - * @since 3.0.0 - */ - public function test_update_coupon() { - wp_set_current_user( $this->user ); - $coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' ); - $post = get_post( $coupon->get_id() ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/' . $coupon->get_id() ) ); - $data = $response->get_data(); - $this->assertEquals( 'This is a dummy coupon', $data['description'] ); - $this->assertEquals( 'fixed_cart', $data['discount_type'] ); - $this->assertEquals( '1.00', $data['amount'] ); - - $request = new WP_REST_Request( 'PUT', '/wc/v2/coupons/' . $coupon->get_id() ); - $request->set_body_params( - array( - 'amount' => '10.00', - 'description' => 'New description', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( '10.00', $data['amount'] ); - $this->assertEquals( 'New description', $data['description'] ); - $this->assertEquals( 'fixed_cart', $data['discount_type'] ); - } - - /** - * Test updating a single coupon with an invalid ID. - * @since 3.0.0 - */ - public function test_update_coupon_invalid_id() { - wp_set_current_user( $this->user ); - - $request = new WP_REST_Request( 'PUT', '/wc/v2/coupons/0' ); - $request->set_body_params( - array( - 'code' => 'tester', - 'amount' => '10.00', - 'description' => 'New description', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 400, $response->get_status() ); - } - - /** - * Test updating a single coupon without valid permissions. - * @since 3.0.0 - */ - public function test_update_coupon_without_permission() { - wp_set_current_user( 0 ); - $coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' ); - $post = get_post( $coupon->get_id() ); - - $request = new WP_REST_Request( 'PUT', '/wc/v2/coupons/' . $coupon->get_id() ); - $request->set_body_params( - array( - 'amount' => '10.00', - 'description' => 'New description', - ) - ); - $response = $this->server->dispatch( $request ); - - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test deleting a single coupon. - * @since 3.0.0 - */ - public function test_delete_coupon() { - wp_set_current_user( $this->user ); - $coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' ); - $request = new WP_REST_Request( 'DELETE', '/wc/v2/coupons/' . $coupon->get_id() ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - } - - /** - * Test deleting a single coupon with an invalid ID. - * @since 3.0.0 - */ - public function test_delete_coupon_invalid_id() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'DELETE', '/wc/v2/coupons/0' ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test deleting a single coupon without valid permissions. - * @since 3.0.0 - */ - public function test_delete_coupon_without_permission() { - wp_set_current_user( 0 ); - $coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' ); - $request = new WP_REST_Request( 'DELETE', '/wc/v2/coupons/' . $coupon->get_id() ); - $response = $this->server->dispatch( $request ); - - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test batch operations on coupons. - * @since 3.0.0 - */ - public function test_batch_coupon() { - wp_set_current_user( $this->user ); - - $coupon_1 = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' ); - $coupon_2 = WC_Helper_Coupon::create_coupon( 'dummycoupon-2' ); - $coupon_3 = WC_Helper_Coupon::create_coupon( 'dummycoupon-3' ); - $coupon_4 = WC_Helper_Coupon::create_coupon( 'dummycoupon-4' ); - - $request = new WP_REST_Request( 'POST', '/wc/v2/coupons/batch' ); - $request->set_body_params( - array( - 'update' => array( - array( - 'id' => $coupon_1->get_id(), - 'amount' => '5.15', - ), - ), - 'delete' => array( - $coupon_2->get_id(), - $coupon_3->get_id(), - ), - 'create' => array( - array( - 'code' => 'new-coupon', - 'amount' => '11.00', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( '5.15', $data['update'][0]['amount'] ); - $this->assertEquals( '11.00', $data['create'][0]['amount'] ); - $this->assertEquals( 'new-coupon', $data['create'][0]['code'] ); - $this->assertEquals( $coupon_2->get_id(), $data['delete'][0]['id'] ); - $this->assertEquals( $coupon_3->get_id(), $data['delete'][1]['id'] ); - - $request = new WP_REST_Request( 'GET', '/wc/v2/coupons' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 3, count( $data ) ); - } - - /** - * Test coupon schema. - * @since 3.0.0 - */ - public function test_coupon_schema() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'OPTIONS', '/wc/v2/coupons' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 27, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'code', $properties ); - $this->assertArrayHasKey( 'date_created', $properties ); - $this->assertArrayHasKey( 'date_created_gmt', $properties ); - $this->assertArrayHasKey( 'date_modified', $properties ); - $this->assertArrayHasKey( 'date_modified_gmt', $properties ); - $this->assertArrayHasKey( 'description', $properties ); - $this->assertArrayHasKey( 'discount_type', $properties ); - $this->assertArrayHasKey( 'amount', $properties ); - $this->assertArrayHasKey( 'date_expires', $properties ); - $this->assertArrayHasKey( 'date_expires_gmt', $properties ); - $this->assertArrayHasKey( 'usage_count', $properties ); - $this->assertArrayHasKey( 'individual_use', $properties ); - $this->assertArrayHasKey( 'product_ids', $properties ); - $this->assertArrayHasKey( 'excluded_product_ids', $properties ); - $this->assertArrayHasKey( 'usage_limit', $properties ); - $this->assertArrayHasKey( 'usage_limit_per_user', $properties ); - $this->assertArrayHasKey( 'limit_usage_to_x_items', $properties ); - $this->assertArrayHasKey( 'free_shipping', $properties ); - $this->assertArrayHasKey( 'product_categories', $properties ); - $this->assertArrayHasKey( 'excluded_product_categories', $properties ); - $this->assertArrayHasKey( 'exclude_sale_items', $properties ); - $this->assertArrayHasKey( 'minimum_amount', $properties ); - $this->assertArrayHasKey( 'maximum_amount', $properties ); - $this->assertArrayHasKey( 'email_restrictions', $properties ); - $this->assertArrayHasKey( 'used_by', $properties ); - } -} diff --git a/tests/unit-tests/api/v2/customers.php b/tests/unit-tests/api/v2/customers.php deleted file mode 100644 index d6519cd246c..00000000000 --- a/tests/unit-tests/api/v2/customers.php +++ /dev/null @@ -1,566 +0,0 @@ -endpoint = new WC_REST_Customers_Controller(); - } - - /** - * Test route registration. - * - * @since 3.0.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - - $this->assertArrayHasKey( '/wc/v2/customers', $routes ); - $this->assertArrayHasKey( '/wc/v2/customers/(?P[\d]+)', $routes ); - $this->assertArrayHasKey( '/wc/v2/customers/batch', $routes ); - } - - /** - * Test getting customers. - * - * @since 3.0.0 - */ - public function test_get_customers() { - wp_set_current_user( 1 ); - - $customer_1 = WC_Helper_Customer::create_customer(); - WC_Helper_Customer::create_customer( 'test2', 'test2', 'test2@woo.local' ); - - $request = new WP_REST_Request( 'GET', '/wc/v2/customers' ); - $request->set_query_params( - array( - 'orderby' => 'id', - ) - ); - $response = $this->server->dispatch( $request ); - $customers = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 2, count( $customers ) ); - - $this->assertContains( - array( - 'id' => $customer_1->get_id(), - 'date_created' => wc_rest_prepare_date_response( $customer_1->get_date_created(), false ), - 'date_created_gmt' => wc_rest_prepare_date_response( $customer_1->get_date_created() ), - 'date_modified' => wc_rest_prepare_date_response( $customer_1->get_date_modified(), false ), - 'date_modified_gmt' => wc_rest_prepare_date_response( $customer_1->get_date_modified() ), - 'email' => 'test@woo.local', - 'first_name' => 'Justin', - 'last_name' => '', - 'role' => 'customer', - 'username' => 'testcustomer', - 'billing' => array( - 'first_name' => '', - 'last_name' => '', - 'company' => '', - 'address_1' => '123 South Street', - 'address_2' => 'Apt 1', - 'city' => 'Philadelphia', - 'state' => 'PA', - 'postcode' => '19123', - 'country' => 'US', - 'email' => '', - 'phone' => '', - ), - 'shipping' => array( - 'first_name' => '', - 'last_name' => '', - 'company' => '', - 'address_1' => '123 South Street', - 'address_2' => 'Apt 1', - 'city' => 'Philadelphia', - 'state' => 'PA', - 'postcode' => '19123', - 'country' => 'US', - ), - 'is_paying_customer' => false, - 'orders_count' => 0, - 'total_spent' => '0.00', - 'avatar_url' => $customer_1->get_avatar_url(), - 'meta_data' => array(), - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v2/customers/' . $customer_1->get_id() . '' ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v2/customers' ), - ), - ), - ), - ), - $customers - ); - } - - /** - * Test getting customers without valid permissions. - * - * @since 3.0.0 - */ - public function test_get_customers_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test creating a new customer. - * - * @since 3.0.0 - */ - public function test_create_customer() { - wp_set_current_user( 1 ); - - // Test just the basics first.. - $request = new WP_REST_Request( 'POST', '/wc/v2/customers' ); - $request->set_body_params( - array( - 'username' => 'create_customer_test', - 'password' => 'test123', - 'email' => 'create_customer_test@woo.local', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 201, $response->get_status() ); - $this->assertEquals( - array( - 'id' => $data['id'], - 'date_created' => $data['date_created'], - 'date_created_gmt' => $data['date_created_gmt'], - 'date_modified' => $data['date_modified'], - 'date_modified_gmt' => $data['date_modified_gmt'], - 'email' => 'create_customer_test@woo.local', - 'first_name' => '', - 'last_name' => '', - 'role' => 'customer', - 'username' => 'create_customer_test', - 'billing' => array( - 'first_name' => '', - 'last_name' => '', - 'company' => '', - 'address_1' => '', - 'address_2' => '', - 'city' => '', - 'state' => '', - 'postcode' => '', - 'country' => '', - 'email' => '', - 'phone' => '', - ), - 'shipping' => array( - 'first_name' => '', - 'last_name' => '', - 'company' => '', - 'address_1' => '', - 'address_2' => '', - 'city' => '', - 'state' => '', - 'postcode' => '', - 'country' => '', - ), - 'is_paying_customer' => false, - 'meta_data' => array(), - 'orders_count' => 0, - 'total_spent' => '0.00', - 'avatar_url' => $data['avatar_url'], - ), - $data - ); - - // Test extra data - $request = new WP_REST_Request( 'POST', '/wc/v2/customers' ); - $request->set_body_params( - array( - 'username' => 'create_customer_test2', - 'password' => 'test123', - 'email' => 'create_customer_test2@woo.local', - 'first_name' => 'Test', - 'last_name' => 'McTestFace', - 'billing' => array( - 'country' => 'US', - 'state' => 'WA', - ), - 'shipping' => array( - 'state' => 'CA', - 'country' => 'US', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 201, $response->get_status() ); - $this->assertEquals( - array( - 'id' => $data['id'], - 'date_created' => $data['date_created'], - 'date_created_gmt' => $data['date_created_gmt'], - 'date_modified' => $data['date_modified'], - 'date_modified_gmt' => $data['date_modified_gmt'], - 'email' => 'create_customer_test2@woo.local', - 'first_name' => 'Test', - 'last_name' => 'McTestFace', - 'role' => 'customer', - 'username' => 'create_customer_test2', - 'billing' => array( - 'first_name' => '', - 'last_name' => '', - 'company' => '', - 'address_1' => '', - 'address_2' => '', - 'city' => '', - 'state' => 'WA', - 'postcode' => '', - 'country' => 'US', - 'email' => '', - 'phone' => '', - ), - 'shipping' => array( - 'first_name' => '', - 'last_name' => '', - 'company' => '', - 'address_1' => '', - 'address_2' => '', - 'city' => '', - 'state' => 'CA', - 'postcode' => '', - 'country' => 'US', - ), - 'is_paying_customer' => false, - 'meta_data' => array(), - 'orders_count' => 0, - 'total_spent' => '0.00', - 'avatar_url' => $data['avatar_url'], - ), - $data - ); - - // Test without required field - $request = new WP_REST_Request( 'POST', '/wc/v2/customers' ); - $request->set_body_params( - array( - 'username' => 'create_customer_test3', - 'first_name' => 'Test', - 'last_name' => 'McTestFace', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 400, $response->get_status() ); - } - - /** - * Test creating customers without valid permissions. - * - * @since 3.0.0 - */ - public function test_create_customer_without_permission() { - wp_set_current_user( 0 ); - $request = new WP_REST_Request( 'POST', '/wc/v2/customers' ); - $request->set_body_params( - array( - 'username' => 'create_customer_test_without_permission', - 'password' => 'test123', - 'email' => 'create_customer_test_without_permission@woo.local', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test getting a single customer. - * - * @since 3.0.0 - */ - public function test_get_customer() { - wp_set_current_user( 1 ); - $customer = WC_Helper_Customer::create_customer( 'get_customer_test', 'test123', 'get_customer_test@woo.local' ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) ); - $data = $response->get_data(); - - $this->assertEquals( - array( - 'id' => $data['id'], - 'date_created' => $data['date_created'], - 'date_created_gmt' => $data['date_created_gmt'], - 'date_modified' => $data['date_modified'], - 'date_modified_gmt' => $data['date_modified_gmt'], - 'email' => 'get_customer_test@woo.local', - 'first_name' => 'Justin', - 'billing' => array( - 'first_name' => '', - 'last_name' => '', - 'company' => '', - 'address_1' => '123 South Street', - 'address_2' => 'Apt 1', - 'city' => 'Philadelphia', - 'state' => 'PA', - 'postcode' => '19123', - 'country' => 'US', - 'email' => '', - 'phone' => '', - ), - 'shipping' => array( - 'first_name' => '', - 'last_name' => '', - 'company' => '', - 'address_1' => '123 South Street', - 'address_2' => 'Apt 1', - 'city' => 'Philadelphia', - 'state' => 'PA', - 'postcode' => '19123', - 'country' => 'US', - ), - 'is_paying_customer' => false, - 'meta_data' => array(), - 'last_name' => '', - 'role' => 'customer', - 'username' => 'get_customer_test', - 'orders_count' => 0, - 'total_spent' => '0.00', - 'avatar_url' => $data['avatar_url'], - ), - $data - ); - } - - /** - * Test getting a single customer without valid permissions. - * - * @since 3.0.0 - */ - public function test_get_customer_without_permission() { - wp_set_current_user( 0 ); - $customer = WC_Helper_Customer::create_customer( 'get_customer_test_without_permission', 'test123', 'get_customer_test_without_permission@woo.local' ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test getting a single customer with an invalid ID. - * - * @since 3.0.0 - */ - public function test_get_customer_invalid_id() { - wp_set_current_user( 1 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/0' ) ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test updating a customer. - * - * @since 3.0.0 - */ - public function test_update_customer() { - wp_set_current_user( 1 ); - $customer = WC_Helper_Customer::create_customer( 'update_customer_test', 'test123', 'update_customer_test@woo.local' ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) ); - $data = $response->get_data(); - $this->assertEquals( 'update_customer_test', $data['username'] ); - $this->assertEquals( 'update_customer_test@woo.local', $data['email'] ); - - $request = new WP_REST_Request( 'PUT', '/wc/v2/customers/' . $customer->get_id() ); - $request->set_body_params( - array( - 'email' => 'updated_email@woo.local', - 'first_name' => 'UpdatedTest', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'updated_email@woo.local', $data['email'] ); - $this->assertEquals( 'UpdatedTest', $data['first_name'] ); - } - - /** - * Test updating a customer without valid permissions. - * - * @since 3.0.0 - */ - public function test_update_customer_without_permission() { - wp_set_current_user( 0 ); - $customer = WC_Helper_Customer::create_customer( 'update_customer_test_without_permission', 'test123', 'update_customer_test_without_permission@woo.local' ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test updating a customer with an invalid ID. - * - * @since 3.0.0 - */ - public function test_update_customer_invalid_id() { - wp_set_current_user( 1 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/0' ) ); - $this->assertEquals( 404, $response->get_status() ); - } - - - /** - * Test deleting a customer. - * - * @since 3.0.0 - */ - public function test_delete_customer() { - wp_set_current_user( 1 ); - $customer = WC_Helper_Customer::create_customer( 'delete_customer_test', 'test123', 'delete_customer_test@woo.local' ); - $request = new WP_REST_Request( 'DELETE', '/wc/v2/customers/' . $customer->get_id() ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - } - - /** - * Test deleting a customer with an invalid ID. - * - * @since 3.0.0 - */ - public function test_delete_customer_invalid_id() { - wp_set_current_user( 1 ); - $request = new WP_REST_Request( 'DELETE', '/wc/v2/customers/0' ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - } - - /** - * Test deleting a customer without valid permissions. - * - * @since 3.0.0 - */ - public function test_delete_customer_without_permission() { - wp_set_current_user( 0 ); - $customer = WC_Helper_Customer::create_customer( 'delete_customer_test_without_permission', 'test123', 'delete_customer_test_without_permission@woo.local' ); - $request = new WP_REST_Request( 'DELETE', '/wc/v2/customers/' . $customer->get_id() ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test customer batch endpoint. - * - * @since 3.0.0 - */ - public function test_batch_customer() { - wp_set_current_user( 1 ); - - $customer_1 = WC_Helper_Customer::create_customer( 'test_batch_customer', 'test123', 'test_batch_customer@woo.local' ); - $customer_2 = WC_Helper_Customer::create_customer( 'test_batch_customer2', 'test123', 'test_batch_customer2@woo.local' ); - $customer_3 = WC_Helper_Customer::create_customer( 'test_batch_customer3', 'test123', 'test_batch_customer3@woo.local' ); - $customer_4 = WC_Helper_Customer::create_customer( 'test_batch_customer4', 'test123', 'test_batch_customer4@woo.local' ); - - $request = new WP_REST_Request( 'POST', '/wc/v2/customers/batch' ); - $request->set_body_params( - array( - 'update' => array( - array( - 'id' => $customer_1->get_id(), - 'last_name' => 'McTest', - ), - ), - 'delete' => array( - $customer_2->get_id(), - $customer_3->get_id(), - ), - 'create' => array( - array( - 'username' => 'newuser', - 'password' => 'test123', - 'email' => 'newuser@woo.local', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'McTest', $data['update'][0]['last_name'] ); - $this->assertEquals( 'newuser', $data['create'][0]['username'] ); - $this->assertEmpty( $data['create'][0]['last_name'] ); - $this->assertEquals( $customer_2->get_id(), $data['delete'][0]['id'] ); - $this->assertEquals( $customer_3->get_id(), $data['delete'][1]['id'] ); - - $request = new WP_REST_Request( 'GET', '/wc/v2/customers' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 3, count( $data ) ); - } - - /** - * Test customer schema. - * - * @since 3.0.0 - */ - public function test_customer_schema() { - wp_set_current_user( 1 ); - $request = new WP_REST_Request( 'OPTIONS', '/wc/v2/customers' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 18, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'date_created', $properties ); - $this->assertArrayHasKey( 'date_created_gmt', $properties ); - $this->assertArrayHasKey( 'date_modified', $properties ); - $this->assertArrayHasKey( 'date_modified_gmt', $properties ); - $this->assertArrayHasKey( 'email', $properties ); - $this->assertArrayHasKey( 'first_name', $properties ); - $this->assertArrayHasKey( 'last_name', $properties ); - $this->assertArrayHasKey( 'role', $properties ); - $this->assertArrayHasKey( 'username', $properties ); - $this->assertArrayHasKey( 'password', $properties ); - $this->assertArrayHasKey( 'orders_count', $properties ); - $this->assertArrayHasKey( 'total_spent', $properties ); - $this->assertArrayHasKey( 'avatar_url', $properties ); - $this->assertArrayHasKey( 'billing', $properties ); - $this->assertArrayHasKey( 'first_name', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'last_name', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'company', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'address_1', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'address_2', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'city', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'state', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'postcode', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'country', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'email', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'phone', $properties['billing']['properties'] ); - $this->assertArrayHasKey( 'shipping', $properties ); - $this->assertArrayHasKey( 'first_name', $properties['shipping']['properties'] ); - $this->assertArrayHasKey( 'last_name', $properties['shipping']['properties'] ); - $this->assertArrayHasKey( 'company', $properties['shipping']['properties'] ); - $this->assertArrayHasKey( 'address_1', $properties['shipping']['properties'] ); - $this->assertArrayHasKey( 'address_2', $properties['shipping']['properties'] ); - $this->assertArrayHasKey( 'city', $properties['shipping']['properties'] ); - $this->assertArrayHasKey( 'state', $properties['shipping']['properties'] ); - $this->assertArrayHasKey( 'postcode', $properties['shipping']['properties'] ); - $this->assertArrayHasKey( 'country', $properties['shipping']['properties'] ); - } -} diff --git a/tests/unit-tests/api/v2/orders.php b/tests/unit-tests/api/v2/orders.php deleted file mode 100644 index 49df3ce98f7..00000000000 --- a/tests/unit-tests/api/v2/orders.php +++ /dev/null @@ -1,635 +0,0 @@ -endpoint = new WC_REST_Orders_Controller(); - $this->user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * @since 3.0.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v2/orders', $routes ); - $this->assertArrayHasKey( '/wc/v2/orders/batch', $routes ); - $this->assertArrayHasKey( '/wc/v2/orders/(?P[\d]+)', $routes ); - } - - /** - * Test getting all orders. - * @since 3.0.0 - */ - public function test_get_items() { - wp_set_current_user( $this->user ); - - // Create 10 orders. - for ( $i = 0; $i < 10; $i++ ) { - $this->orders[] = WC_Helper_Order::create_order( $this->user ); - } - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders' ) ); - $orders = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 10, count( $orders ) ); - } - - /** - * Tests to make sure orders cannot be viewed without valid permissions. - * - * @since 3.0.0 - */ - public function test_get_items_without_permission() { - wp_set_current_user( 0 ); - $this->orders[] = WC_Helper_Order::create_order(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Tests getting a single order. - * @since 3.0.0 - */ - public function test_get_item() { - wp_set_current_user( $this->user ); - $order = WC_Helper_Order::create_order(); - $order->add_meta_data( 'key', 'value' ); - $order->add_meta_data( 'key2', 'value2' ); - $order->save(); - $this->orders[] = $order; - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $order->get_id() ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( $order->get_id(), $data['id'] ); - - // Test meta data is set. - $this->assertEquals( 'key', $data['meta_data'][0]->key ); - $this->assertEquals( 'value', $data['meta_data'][0]->value ); - $this->assertEquals( 'key2', $data['meta_data'][1]->key ); - $this->assertEquals( 'value2', $data['meta_data'][1]->value ); - } - - /** - * Tests getting a single order without the correct permissions. - * @since 3.0.0 - */ - public function test_get_item_without_permission() { - wp_set_current_user( 0 ); - $order = WC_Helper_Order::create_order(); - $this->orders[] = $order; - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $order->get_id() ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Tests getting an order with an invalid ID. - * @since 3.0.0 - */ - public function test_get_item_invalid_id() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/99999999' ) ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Tests getting an order with an invalid ID. - * @since 3.5.0 - */ - public function test_get_item_refund_id() { - wp_set_current_user( $this->user ); - $order = WC_Helper_Order::create_order(); - $refund = wc_create_refund( - array( - 'order_id' => $order->get_id(), - ) - ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $refund->get_id() ) ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Tests creating an order. - * @since 3.0.0 - */ - public function test_create_order() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'POST', '/wc/v2/orders' ); - $request->set_body_params( - array( - 'payment_method' => 'bacs', - 'payment_method_title' => 'Direct Bank Transfer', - 'set_paid' => true, - 'billing' => array( - 'first_name' => 'John', - 'last_name' => 'Doe', - 'address_1' => '969 Market', - 'address_2' => '', - 'city' => 'San Francisco', - 'state' => 'CA', - 'postcode' => '94103', - 'country' => 'US', - 'email' => 'john.doe@example.com', - 'phone' => '(555) 555-5555', - ), - 'shipping' => array( - 'first_name' => 'John', - 'last_name' => 'Doe', - 'address_1' => '969 Market', - 'address_2' => '', - 'city' => 'San Francisco', - 'state' => 'CA', - 'postcode' => '94103', - 'country' => 'US', - ), - 'line_items' => array( - array( - 'product_id' => $product->get_id(), - 'quantity' => 2, - ), - ), - 'shipping_lines' => array( - array( - 'method_id' => 'flat_rate', - 'method_title' => 'Flat rate', - 'total' => '10', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $order = wc_get_order( $data['id'] ); - $this->assertEquals( 201, $response->get_status() ); - $this->assertEquals( $order->get_payment_method(), $data['payment_method'] ); - $this->assertEquals( $order->get_payment_method_title(), $data['payment_method_title'] ); - $this->assertEquals( $order->get_billing_first_name(), $data['billing']['first_name'] ); - $this->assertEquals( $order->get_billing_last_name(), $data['billing']['last_name'] ); - $this->assertEquals( '', $data['billing']['company'] ); - $this->assertEquals( $order->get_billing_address_1(), $data['billing']['address_1'] ); - $this->assertEquals( $order->get_billing_address_2(), $data['billing']['address_2'] ); - $this->assertEquals( $order->get_billing_city(), $data['billing']['city'] ); - $this->assertEquals( $order->get_billing_state(), $data['billing']['state'] ); - $this->assertEquals( $order->get_billing_postcode(), $data['billing']['postcode'] ); - $this->assertEquals( $order->get_billing_country(), $data['billing']['country'] ); - $this->assertEquals( $order->get_billing_email(), $data['billing']['email'] ); - $this->assertEquals( $order->get_billing_phone(), $data['billing']['phone'] ); - $this->assertEquals( $order->get_shipping_first_name(), $data['shipping']['first_name'] ); - $this->assertEquals( $order->get_shipping_last_name(), $data['shipping']['last_name'] ); - $this->assertEquals( '', $data['shipping']['company'] ); - $this->assertEquals( $order->get_shipping_address_1(), $data['shipping']['address_1'] ); - $this->assertEquals( $order->get_shipping_address_2(), $data['shipping']['address_2'] ); - $this->assertEquals( $order->get_shipping_city(), $data['shipping']['city'] ); - $this->assertEquals( $order->get_shipping_state(), $data['shipping']['state'] ); - $this->assertEquals( $order->get_shipping_postcode(), $data['shipping']['postcode'] ); - $this->assertEquals( $order->get_shipping_country(), $data['shipping']['country'] ); - $this->assertEquals( 1, count( $data['line_items'] ) ); - $this->assertEquals( 1, count( $data['shipping_lines'] ) ); - } - - /** - * Test the sanitization of the payment_method_title field through the API. - * - * @since 3.5.2 - */ - public function test_create_update_order_payment_method_title_sanitize() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - - // Test when creating order. - $request = new WP_REST_Request( 'POST', '/wc/v3/orders' ); - $request->set_body_params( - array( - 'payment_method' => 'bacs', - 'payment_method_title' => '

Sanitize this

', - 'set_paid' => true, - 'billing' => array( - 'first_name' => 'John', - 'last_name' => 'Doe', - 'address_1' => '969 Market', - 'address_2' => '', - 'city' => 'San Francisco', - 'state' => 'CA', - 'postcode' => '94103', - 'country' => 'US', - 'email' => 'john.doe@example.com', - 'phone' => '(555) 555-5555', - ), - 'shipping' => array( - 'first_name' => 'John', - 'last_name' => 'Doe', - 'address_1' => '969 Market', - 'address_2' => '', - 'city' => 'San Francisco', - 'state' => 'CA', - 'postcode' => '94103', - 'country' => 'US', - ), - 'line_items' => array( - array( - 'product_id' => $product->get_id(), - 'quantity' => 2, - ), - ), - 'shipping_lines' => array( - array( - 'method_id' => 'flat_rate', - 'method_title' => 'Flat rate', - 'total' => '10', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $order = wc_get_order( $data['id'] ); - $this->assertEquals( 201, $response->get_status() ); - $this->assertEquals( $order->get_payment_method(), $data['payment_method'] ); - $this->assertEquals( $order->get_payment_method_title(), 'Sanitize this' ); - - // Test when updating order. - $request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $data['id'] ); - $request->set_body_params( - array( - 'payment_method' => 'bacs', - 'payment_method_title' => '

Sanitize this too

', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $order = wc_get_order( $data['id'] ); - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( $order->get_payment_method(), $data['payment_method'] ); - $this->assertEquals( $order->get_payment_method_title(), 'Sanitize this too' ); - } - - /** - * Tests creating an order without required fields. - * @since 3.0.0 - */ - public function test_create_order_invalid_fields() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - - // non-existent customer - $request = new WP_REST_Request( 'POST', '/wc/v2/orders' ); - $request->set_body_params( - array( - 'payment_method' => 'bacs', - 'payment_method_title' => 'Direct Bank Transfer', - 'set_paid' => true, - 'customer_id' => 99999, - 'billing' => array( - 'first_name' => 'John', - 'last_name' => 'Doe', - 'address_1' => '969 Market', - 'address_2' => '', - 'city' => 'San Francisco', - 'state' => 'CA', - 'postcode' => '94103', - 'country' => 'US', - 'email' => 'john.doe@example.com', - 'phone' => '(555) 555-5555', - ), - 'shipping' => array( - 'first_name' => 'John', - 'last_name' => 'Doe', - 'address_1' => '969 Market', - 'address_2' => '', - 'city' => 'San Francisco', - 'state' => 'CA', - 'postcode' => '94103', - 'country' => 'US', - ), - 'line_items' => array( - array( - 'product_id' => $product->get_id(), - 'quantity' => 2, - ), - ), - 'shipping_lines' => array( - array( - 'method_id' => 'flat_rate', - 'method_title' => 'Flat rate', - 'total' => 10, - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( 400, $response->get_status() ); - } - - /** - * Tests updating an order. - * - * @since 3.0.0 - */ - public function test_update_order() { - wp_set_current_user( $this->user ); - $order = WC_Helper_Order::create_order(); - $request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() ); - $request->set_body_params( - array( - 'payment_method' => 'test-update', - 'billing' => array( - 'first_name' => 'Fish', - 'last_name' => 'Face', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 'test-update', $data['payment_method'] ); - $this->assertEquals( 'Fish', $data['billing']['first_name'] ); - $this->assertEquals( 'Face', $data['billing']['last_name'] ); - } - - /** - * Tests updating an order and removing items. - * - * @since 3.0.0 - */ - public function test_update_order_remove_items() { - wp_set_current_user( $this->user ); - $order = WC_Helper_Order::create_order(); - $fee = new WC_Order_Item_Fee(); - $fee->set_props( - array( - 'name' => 'Some Fee', - 'tax_status' => 'taxable', - 'total' => '100', - 'tax_class' => '', - ) - ); - $order->add_item( $fee ); - $order->save(); - - $request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() ); - $fee_data = current( $order->get_items( 'fee' ) ); - - $request->set_body_params( - array( - 'fee_lines' => array( - array( - 'id' => $fee_data->get_id(), - 'name' => null, - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertTrue( empty( $data['fee_lines'] ) ); - } - - /** - * Tests updating an order and adding a coupon. - * - * @since 3.3.0 - */ - public function test_update_order_add_coupons() { - wp_set_current_user( $this->user ); - $order = WC_Helper_Order::create_order(); - $order_item = current( $order->get_items() ); - $coupon = WC_Helper_Coupon::create_coupon( 'fake-coupon' ); - $coupon->set_amount( 5 ); - $coupon->save(); - $request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() ); - $request->set_body_params( - array( - 'coupon_lines' => array( - array( - 'code' => 'fake-coupon', - 'discount_total' => '5', - 'discount_tax' => '0', - ), - ), - 'line_items' => array( - array( - 'id' => $order_item->get_id(), - 'product_id' => $order_item->get_product_id(), - 'total' => '35.00', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertCount( 1, $data['coupon_lines'] ); - $this->assertEquals( '45.00', $data['total'] ); - } - - /** - * Tests updating an order and removing a coupon. - * - * @since 3.3.0 - */ - public function test_update_order_remove_coupons() { - wp_set_current_user( $this->user ); - $order = WC_Helper_Order::create_order(); - $order_item = current( $order->get_items() ); - $coupon = WC_Helper_Coupon::create_coupon( 'fake-coupon' ); - $coupon->set_amount( 5 ); - $coupon->save(); - - $order->apply_coupon( $coupon ); - $order->save(); - - // Check that the coupon is applied. - $this->assertEquals( '45.00', $order->get_total() ); - - $request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() ); - $coupon_data = current( $order->get_items( 'coupon' ) ); - - $request->set_body_params( - array( - 'coupon_lines' => array( - array( - 'id' => $coupon_data->get_id(), - 'code' => null, - ), - ), - 'line_items' => array( - array( - 'id' => $order_item->get_id(), - 'product_id' => $order_item->get_product_id(), - 'total' => '40.00', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertTrue( empty( $data['coupon_lines'] ) ); - $this->assertEquals( '50.00', $data['total'] ); - } - - /** - * Tests updating an order without the correct permissions. - * - * @since 3.0.0 - */ - public function test_update_order_without_permission() { - wp_set_current_user( 0 ); - $order = WC_Helper_Order::create_order(); - $request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() ); - $request->set_body_params( - array( - 'payment_method' => 'test-update', - 'billing' => array( - 'first_name' => 'Fish', - 'last_name' => 'Face', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Tests that updating an order with an invalid id fails. - * @since 3.0.0 - */ - public function test_update_order_invalid_id() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'POST', '/wc/v2/orders/999999' ); - $request->set_body_params( - array( - 'payment_method' => 'test-update', - 'billing' => array( - 'first_name' => 'Fish', - 'last_name' => 'Face', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - } - - /** - * Test deleting an order. - * @since 3.0.0 - */ - public function test_delete_order() { - wp_set_current_user( $this->user ); - $order = WC_Helper_Order::create_order(); - $request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/' . $order->get_id() ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( null, get_post( $order->get_id() ) ); - } - - /** - * Test deleting an order without permission/creds. - * @since 3.0.0 - */ - public function test_delete_order_without_permission() { - wp_set_current_user( 0 ); - $order = WC_Helper_Order::create_order(); - $request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/' . $order->get_id() ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test deleting an order with an invalid id. - * - * @since 3.0.0 - */ - public function test_delete_order_invalid_id() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/9999999' ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test batch managing product reviews. - */ - public function test_orders_batch() { - wp_set_current_user( $this->user ); - - $order1 = WC_Helper_Order::create_order(); - $order2 = WC_Helper_Order::create_order(); - $order3 = WC_Helper_Order::create_order(); - - $request = new WP_REST_Request( 'POST', '/wc/v2/orders/batch' ); - $request->set_body_params( - array( - 'update' => array( - array( - 'id' => $order1->get_id(), - 'payment_method' => 'updated', - ), - ), - 'delete' => array( - $order2->get_id(), - $order3->get_id(), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'updated', $data['update'][0]['payment_method'] ); - $this->assertEquals( $order2->get_id(), $data['delete'][0]['id'] ); - $this->assertEquals( $order3->get_id(), $data['delete'][1]['id'] ); - - $request = new WP_REST_Request( 'GET', '/wc/v2/orders' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( 1, count( $data ) ); - } - - /** - * Test the order schema. - * @since 3.0.0 - */ - public function test_order_schema() { - wp_set_current_user( $this->user ); - $order = WC_Helper_Order::create_order(); - $request = new WP_REST_Request( 'OPTIONS', '/wc/v2/orders/' . $order->get_id() ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 42, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - } -} diff --git a/tests/unit-tests/api/v2/payment-gateways.php b/tests/unit-tests/api/v2/payment-gateways.php deleted file mode 100644 index d8757c5d31d..00000000000 --- a/tests/unit-tests/api/v2/payment-gateways.php +++ /dev/null @@ -1,337 +0,0 @@ -endpoint = new WC_REST_Payment_Gateways_Controller(); - $this->user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * - * @since 3.0.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v2/payment_gateways', $routes ); - $this->assertArrayHasKey( '/wc/v2/payment_gateways/(?P[\w-]+)', $routes ); - } - - /** - * Test getting all payment gateways. - * - * @since 3.0.0 - */ - public function test_get_payment_gateways() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways' ) ); - $gateways = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertContains( - array( - 'id' => 'cheque', - 'title' => 'Check payments', - 'description' => 'Please send a check to Store Name, Store Street, Store Town, Store State / County, Store Postcode.', - 'order' => '', - 'enabled' => false, - 'method_title' => 'Check payments', - 'method_description' => 'Take payments in person via checks. This offline gateway can also be useful to test purchases.', - 'settings' => array_diff_key( - $this->get_settings( 'WC_Gateway_Cheque' ), - array( - 'enabled' => false, - 'description' => false, - ) - ), - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v2/payment_gateways/cheque' ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v2/payment_gateways' ), - ), - ), - ), - ), - $gateways - ); - } - - /** - * Tests to make sure payment gateways cannot viewed without valid permissions. - * - * @since 3.0.0 - */ - public function test_get_payment_gateways_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test getting a single payment gateway. - * - * @since 3.0.0 - */ - public function test_get_payment_gateway() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) ); - $paypal = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( - array( - 'id' => 'paypal', - 'title' => 'PayPal', - 'description' => "Pay via PayPal; you can pay with your credit card if you don't have a PayPal account.", - 'order' => '', - 'enabled' => false, - 'method_title' => 'PayPal', - 'method_description' => 'PayPal Standard redirects customers to PayPal to enter their payment information.', - 'settings' => array_diff_key( - $this->get_settings( 'WC_Gateway_Paypal' ), - array( - 'enabled' => false, - 'description' => false, - ) - ), - ), - $paypal - ); - } - - /** - * Test getting a payment gateway without valid permissions. - * - * @since 3.0.0 - */ - public function test_get_payment_gateway_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test getting a payment gateway with an invalid id. - * - * @since 3.0.0 - */ - public function test_get_payment_gateway_invalid_id() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/totally_fake_method' ) ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test updating a single payment gateway. - * - * @since 3.0.0 - */ - public function test_update_payment_gateway() { - wp_set_current_user( $this->user ); - - // Test defaults - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) ); - $paypal = $response->get_data(); - - $this->assertEquals( 'PayPal', $paypal['settings']['title']['value'] ); - $this->assertEquals( 'admin@example.org', $paypal['settings']['email']['value'] ); - $this->assertEquals( 'no', $paypal['settings']['testmode']['value'] ); - - // test updating single setting - $request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' ); - $request->set_body_params( - array( - 'settings' => array( - 'email' => 'woo@woo.local', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $paypal = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 'PayPal', $paypal['settings']['title']['value'] ); - $this->assertEquals( 'woo@woo.local', $paypal['settings']['email']['value'] ); - $this->assertEquals( 'no', $paypal['settings']['testmode']['value'] ); - - // test updating multiple settings - $request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' ); - $request->set_body_params( - array( - 'settings' => array( - 'testmode' => 'yes', - 'title' => 'PayPal - New Title', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $paypal = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 'PayPal - New Title', $paypal['settings']['title']['value'] ); - $this->assertEquals( 'woo@woo.local', $paypal['settings']['email']['value'] ); - $this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] ); - - // Test other parameters, and recheck settings - $request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' ); - $request->set_body_params( - array( - 'enabled' => false, - 'order' => 2, - ) - ); - $response = $this->server->dispatch( $request ); - $paypal = $response->get_data(); - - $this->assertFalse( $paypal['enabled'] ); - $this->assertEquals( 2, $paypal['order'] ); - $this->assertEquals( 'PayPal - New Title', $paypal['settings']['title']['value'] ); - $this->assertEquals( 'woo@woo.local', $paypal['settings']['email']['value'] ); - $this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] ); - - // test bogus - $request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' ); - $request->set_body_params( - array( - 'settings' => array( - 'paymentaction' => 'afasfasf', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - - $request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' ); - $request->set_body_params( - array( - 'settings' => array( - 'paymentaction' => 'authorization', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $paypal = $response->get_data(); - $this->assertEquals( 'authorization', $paypal['settings']['paymentaction']['value'] ); - } - - /** - * Test updating a payment gateway without valid permissions. - * - * @since 3.0.0 - */ - public function test_update_payment_gateway_without_permission() { - wp_set_current_user( 0 ); - $request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' ); - $request->set_body_params( - array( - 'settings' => array( - 'testmode' => 'yes', - 'title' => 'PayPal - New Title', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test updating a payment gateway with an invalid id. - * - * @since 3.0.0 - */ - public function test_update_payment_gateway_invalid_id() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/totally_fake_method' ); - $request->set_body_params( - array( - 'enabled' => true, - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test the payment gateway schema. - * - * @since 3.0.0 - */ - public function test_payment_gateway_schema() { - wp_set_current_user( $this->user ); - - $request = new WP_REST_Request( 'OPTIONS', '/wc/v2/payment_gateways' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 8, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'title', $properties ); - $this->assertArrayHasKey( 'description', $properties ); - $this->assertArrayHasKey( 'order', $properties ); - $this->assertArrayHasKey( 'enabled', $properties ); - $this->assertArrayHasKey( 'method_title', $properties ); - $this->assertArrayHasKey( 'method_description', $properties ); - $this->assertArrayHasKey( 'settings', $properties ); - } - - /** - * Loads a particular gateway's settings so we can correctly test API output. - * - * @since 3.0.0 - * @param string $gateway_class Name of WC_Payment_Gateway class. - */ - private function get_settings( $gateway_class ) { - $gateway = new $gateway_class(); - $settings = array(); - $gateway->init_form_fields(); - foreach ( $gateway->form_fields as $id => $field ) { - // Make sure we at least have a title and type - if ( empty( $field['title'] ) || empty( $field['type'] ) ) { - continue; - } - // Ignore 'title' settings/fields -- they are UI only - if ( 'title' === $field['type'] ) { - continue; - } - $data = array( - 'id' => $id, - 'label' => empty( $field['label'] ) ? $field['title'] : $field['label'], - 'description' => empty( $field['description'] ) ? '' : $field['description'], - 'type' => $field['type'], - 'value' => $gateway->settings[ $id ], - 'default' => empty( $field['default'] ) ? '' : $field['default'], - 'tip' => empty( $field['description'] ) ? '' : $field['description'], - 'placeholder' => empty( $field['placeholder'] ) ? '' : $field['placeholder'], - ); - if ( ! empty( $field['options'] ) ) { - $data['options'] = $field['options']; - } - $settings[ $id ] = $data; - } - return $settings; - } - -} diff --git a/tests/unit-tests/api/v2/product-reviews.php b/tests/unit-tests/api/v2/product-reviews.php deleted file mode 100644 index bf30b5a41f2..00000000000 --- a/tests/unit-tests/api/v2/product-reviews.php +++ /dev/null @@ -1,468 +0,0 @@ -user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * - * @since 3.0.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v3/products/reviews', $routes ); - $this->assertArrayHasKey( '/wc/v3/products/reviews/(?P[\d]+)', $routes ); - $this->assertArrayHasKey( '/wc/v3/products/reviews/batch', $routes ); - } - - /** - * Test getting all product reviews. - * - * @since 3.0.0 - */ - public function test_get_product_reviews() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - // Create 10 products reviews for the product - for ( $i = 0; $i < 10; $i++ ) { - $review_id = WC_Helper_Product::create_product_review( $product->get_id() ); - } - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews' ) ); - $product_reviews = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 10, count( $product_reviews ) ); - $this->assertContains( - array( - 'id' => $review_id, - 'date_created' => $product_reviews[0]['date_created'], - 'date_created_gmt' => $product_reviews[0]['date_created_gmt'], - 'product_id' => $product->get_id(), - 'status' => 'approved', - 'reviewer' => 'admin', - 'reviewer_email' => 'woo@woo.local', - 'review' => "

Review content here

\n", - 'rating' => 0, - 'verified' => false, - 'reviewer_avatar_urls' => $product_reviews[0]['reviewer_avatar_urls'], - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v3/products/reviews/' . $review_id ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v3/products/reviews' ), - ), - ), - 'up' => array( - array( - 'href' => rest_url( '/wc/v3/products/' . $product->get_id() ), - ), - ), - ), - ), - $product_reviews - ); - } - - /** - * Tests to make sure product reviews cannot be viewed without valid permissions. - * - * @since 3.0.0 - */ - public function test_get_product_reviews_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_simple_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Tests to make sure an error is returned when an invalid product is loaded. - * - * @since 3.0.0 - */ - public function test_get_product_reviews_invalid_product() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/0/reviews' ) ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Tests getting a single product review. - * - * @since 3.0.0 - */ - public function test_get_product_review() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $product_review_id = WC_Helper_Product::create_product_review( $product->get_id() ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/' . $product_review_id ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( - array( - 'id' => $product_review_id, - 'date_created' => $data['date_created'], - 'date_created_gmt' => $data['date_created_gmt'], - 'product_id' => $product->get_id(), - 'status' => 'approved', - 'reviewer' => 'admin', - 'reviewer_email' => 'woo@woo.local', - 'review' => "

Review content here

\n", - 'rating' => 0, - 'verified' => false, - 'reviewer_avatar_urls' => $data['reviewer_avatar_urls'], - ), - $data - ); - } - - /** - * Tests getting a single product review without the correct permissions. - * - * @since 3.0.0 - */ - public function test_get_product_review_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_simple_product(); - $product_review_id = WC_Helper_Product::create_product_review( $product->get_id() ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/' . $product_review_id ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Tests getting a product review with an invalid ID. - * - * @since 3.0.0 - */ - public function test_get_product_review_invalid_id() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/0' ) ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Tests creating a product review. - * - * @since 3.0.0 - */ - public function test_create_product_review() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' ); - $request->set_body_params( - array( - 'review' => 'Hello world.', - 'reviewer' => 'Admin', - 'reviewer_email' => 'woo@woo.local', - 'rating' => '5', - 'product_id' => $product->get_id(), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 201, $response->get_status() ); - $this->assertEquals( - array( - 'id' => $data['id'], - 'date_created' => $data['date_created'], - 'date_created_gmt' => $data['date_created_gmt'], - 'product_id' => $product->get_id(), - 'status' => 'approved', - 'reviewer' => 'Admin', - 'reviewer_email' => 'woo@woo.local', - 'review' => 'Hello world.', - 'rating' => 5, - 'verified' => false, - 'reviewer_avatar_urls' => $data['reviewer_avatar_urls'], - ), - $data - ); - } - - /** - * Tests creating a product review without required fields. - * - * @since 3.0.0 - */ - public function test_create_product_review_invalid_fields() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - - // missing review - $request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' ); - $request->set_body_params( - array( - 'reviewer' => 'Admin', - 'reviewer_email' => 'woo@woo.local', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 400, $response->get_status() ); - - // Missing reviewer. - $request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' ); - $request->set_body_params( - array( - 'review' => 'Hello world.', - 'reviewer_email' => 'woo@woo.local', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 400, $response->get_status() ); - - // missing reviewer_email - $request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' ); - $request->set_body_params( - array( - 'review' => 'Hello world.', - 'reviewer' => 'Admin', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 400, $response->get_status() ); - } - - /** - * Tests updating a product review. - * - * @since 3.0.0 - */ - public function test_update_product_review() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $product_review_id = WC_Helper_Product::create_product_review( $product->get_id() ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/' . $product_review_id ) ); - $data = $response->get_data(); - $this->assertEquals( "

Review content here

\n", $data['review'] ); - $this->assertEquals( 'admin', $data['reviewer'] ); - $this->assertEquals( 'woo@woo.local', $data['reviewer_email'] ); - $this->assertEquals( 0, $data['rating'] ); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/products/reviews/' . $product_review_id ); - $request->set_body_params( - array( - 'review' => 'Hello world - updated.', - 'reviewer' => 'Justin', - 'reviewer_email' => 'woo2@woo.local', - 'rating' => 3, - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( 'Hello world - updated.', $data['review'] ); - $this->assertEquals( 'Justin', $data['reviewer'] ); - $this->assertEquals( 'woo2@woo.local', $data['reviewer_email'] ); - $this->assertEquals( 3, $data['rating'] ); - } - - /** - * Tests updating a product review without the correct permissions. - * - * @since 3.0.0 - */ - public function test_update_product_review_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_simple_product(); - $product_review_id = WC_Helper_Product::create_product_review( $product->get_id() ); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/products/reviews/' . $product_review_id ); - $request->set_body_params( - array( - 'review' => 'Hello world.', - 'reviewer' => 'Admin', - 'reviewer_email' => 'woo@woo.dev', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Tests that updating a product review with an invalid id fails. - * - * @since 3.0.0 - */ - public function test_update_product_review_invalid_id() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - - $request = new WP_REST_Request( 'PUT', '/wc/v3/products/reviews/0' ); - $request->set_body_params( - array( - 'review' => 'Hello world.', - 'reviewer' => 'Admin', - 'reviewer_email' => 'woo@woo.dev', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test deleting a product review. - * - * @since 3.0.0 - */ - public function test_delete_product_review() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $product_review_id = WC_Helper_Product::create_product_review( $product->get_id() ); - - $request = new WP_REST_Request( 'DELETE', '/wc/v3/products/reviews/' . $product_review_id ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - } - - /** - * Test deleting a product review without permission/creds. - * - * @since 3.0.0 - */ - public function test_delete_product_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_simple_product(); - $product_review_id = WC_Helper_Product::create_product_review( $product->get_id() ); - - $request = new WP_REST_Request( 'DELETE', '/wc/v3/products/reviews/' . $product_review_id ); - $response = $this->server->dispatch( $request ); - - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test deleting a product review with an invalid id. - * - * @since 3.0.0 - */ - public function test_delete_product_review_invalid_id() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $product_review_id = WC_Helper_Product::create_product_review( $product->get_id() ); - - $request = new WP_REST_Request( 'DELETE', '/wc/v3/products/reviews/0' ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test batch managing product reviews. - */ - public function test_product_reviews_batch() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - - $review_1_id = WC_Helper_Product::create_product_review( $product->get_id() ); - $review_2_id = WC_Helper_Product::create_product_review( $product->get_id() ); - $review_3_id = WC_Helper_Product::create_product_review( $product->get_id() ); - $review_4_id = WC_Helper_Product::create_product_review( $product->get_id() ); - - $request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews/batch' ); - $request->set_body_params( - array( - 'update' => array( - array( - 'id' => $review_1_id, - 'review' => 'Updated review.', - ), - ), - 'delete' => array( - $review_2_id, - $review_3_id, - ), - 'create' => array( - array( - 'review' => 'New review.', - 'reviewer' => 'Justin', - 'reviewer_email' => 'woo3@woo.local', - 'product_id' => $product->get_id(), - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'Updated review.', $data['update'][0]['review'] ); - $this->assertEquals( 'New review.', $data['create'][0]['review'] ); - $this->assertEquals( $review_2_id, $data['delete'][0]['previous']['id'] ); - $this->assertEquals( $review_3_id, $data['delete'][1]['previous']['id'] ); - - $request = new WP_REST_Request( 'GET', '/wc/v3/products/reviews' ); - $request->set_param( 'product', $product->get_id() ); - - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 3, count( $data ) ); - } - - /** - * Test the product review schema. - * - * @since 3.0.0 - */ - public function test_product_review_schema() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/products/reviews' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 11, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'date_created', $properties ); - $this->assertArrayHasKey( 'date_created_gmt', $properties ); - $this->assertArrayHasKey( 'product_id', $properties ); - $this->assertArrayHasKey( 'status', $properties ); - $this->assertArrayHasKey( 'reviewer', $properties ); - $this->assertArrayHasKey( 'reviewer_email', $properties ); - $this->assertArrayHasKey( 'review', $properties ); - $this->assertArrayHasKey( 'rating', $properties ); - $this->assertArrayHasKey( 'verified', $properties ); - - if ( get_option( 'show_avatars' ) ) { - $this->assertArrayHasKey( 'reviewer_avatar_urls', $properties ); - } - } -} diff --git a/tests/unit-tests/api/v2/product-variations.php b/tests/unit-tests/api/v2/product-variations.php deleted file mode 100644 index 7e268a7c657..00000000000 --- a/tests/unit-tests/api/v2/product-variations.php +++ /dev/null @@ -1,473 +0,0 @@ -endpoint = new WC_REST_Product_Variations_Controller(); - $this->user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * - * @since 3.0.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v2/products/(?P[\d]+)/variations', $routes ); - $this->assertArrayHasKey( '/wc/v2/products/(?P[\d]+)/variations/(?P[\d]+)', $routes ); - $this->assertArrayHasKey( '/wc/v2/products/(?P[\d]+)/variations/batch', $routes ); - } - - /** - * Test getting variations. - * - * @since 3.0.0 - */ - public function test_get_variations() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_variation_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) ); - $variations = $response->get_data(); - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 2, count( $variations ) ); - $this->assertEquals( 'DUMMY SKU VARIABLE LARGE', $variations[0]['sku'] ); - $this->assertEquals( 'size', $variations[0]['attributes'][0]['name'] ); - } - - /** - * Test getting variations without permission. - * - * @since 3.0.0 - */ - public function test_get_variations_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_variation_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test getting a single variation. - * - * @since 3.0.0 - */ - public function test_get_variation() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_variation_product(); - $children = $product->get_children(); - $variation_id = $children[0]; - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ) ); - $variation = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( $variation_id, $variation['id'] ); - $this->assertEquals( 'size', $variation['attributes'][0]['name'] ); - } - - /** - * Test getting single variation without permission. - * - * @since 3.0.0 - */ - public function test_get_variation_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_variation_product(); - $children = $product->get_children(); - $variation_id = $children[0]; - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test deleting a single variation. - * - * @since 3.0.0 - */ - public function test_delete_variation() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_variation_product(); - $children = $product->get_children(); - $variation_id = $children[0]; - - $request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) ); - $variations = $response->get_data(); - $this->assertEquals( 1, count( $variations ) ); - } - - /** - * Test deleting a single variation without permission. - * - * @since 3.0.0 - */ - public function test_delete_variation_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_variation_product(); - $children = $product->get_children(); - $variation_id = $children[0]; - - $request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test deleting a single variation with an invalid ID. - * - * @since 3.0.0 - */ - public function test_delete_variation_with_invalid_id() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_variation_product(); - $request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/variations/0' ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test editing a single variation. - * - * @since 3.0.0 - */ - public function test_update_variation() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_variation_product(); - $children = $product->get_children(); - $variation_id = $children[0]; - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ) ); - $variation = $response->get_data(); - - $this->assertEquals( 'DUMMY SKU VARIABLE SMALL', $variation['sku'] ); - $this->assertEquals( 10, $variation['regular_price'] ); - $this->assertEmpty( $variation['sale_price'] ); - $this->assertEquals( 'small', $variation['attributes'][0]['option'] ); - - $request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ); - $request->set_body_params( - array( - 'sku' => 'FIXED-SKU', - 'sale_price' => '8', - 'description' => 'O_O', - 'image' => array( - 'position' => 0, - 'src' => 'http://cldup.com/Dr1Bczxq4q.png', - 'alt' => 'test upload image', - ), - 'attributes' => array( - array( - 'name' => 'pa_size', - 'option' => 'medium', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $variation = $response->get_data(); - - $this->assertTrue( isset( $variation['description'] ), print_r( $variation, true ) ); - $this->assertContains( 'O_O', $variation['description'], print_r( $variation, true ) ); - $this->assertEquals( '8', $variation['price'], print_r( $variation, true ) ); - $this->assertEquals( '8', $variation['sale_price'], print_r( $variation, true ) ); - $this->assertEquals( '10', $variation['regular_price'], print_r( $variation, true ) ); - $this->assertEquals( 'FIXED-SKU', $variation['sku'], print_r( $variation, true ) ); - $this->assertEquals( 'medium', $variation['attributes'][0]['option'], print_r( $variation, true ) ); - $this->assertContains( 'Dr1Bczxq4q', $variation['image']['src'], print_r( $variation, true ) ); - $this->assertContains( 'test upload image', $variation['image']['alt'], print_r( $variation, true ) ); - } - - /** - * Test updating a single variation without permission. - * - * @since 3.0.0 - */ - public function test_update_variation_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_variation_product(); - $children = $product->get_children(); - $variation_id = $children[0]; - - $request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ); - $request->set_body_params( - array( - 'sku' => 'FIXED-SKU-NO-PERMISSION', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test updating a single variation with an invalid ID. - * - * @since 3.0.0 - */ - public function test_update_variation_with_invalid_id() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_variation_product(); - $request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/0' ); - $request->set_body_params( - array( - 'sku' => 'FIXED-SKU-NO-PERMISSION', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - } - - /** - * Test creating a single variation. - * - * @since 3.0.0 - */ - public function test_create_variation() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_variation_product(); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) ); - $variations = $response->get_data(); - $this->assertEquals( 2, count( $variations ) ); - - $request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/variations' ); - $request->set_body_params( - array( - 'sku' => 'DUMMY SKU VARIABLE MEDIUM', - 'regular_price' => '12', - 'description' => 'A medium size.', - 'attributes' => array( - array( - 'name' => 'pa_size', - 'option' => 'medium', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $variation = $response->get_data(); - - $this->assertContains( 'A medium size.', $variation['description'] ); - $this->assertEquals( '12', $variation['price'] ); - $this->assertEquals( '12', $variation['regular_price'] ); - $this->assertTrue( $variation['purchasable'] ); - $this->assertEquals( 'DUMMY SKU VARIABLE MEDIUM', $variation['sku'] ); - $this->assertEquals( 'medium', $variation['attributes'][0]['option'] ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) ); - $variations = $response->get_data(); - $this->assertEquals( 3, count( $variations ) ); - } - - /** - * Test creating a single variation without permission. - * - * @since 3.0.0 - */ - public function test_create_variation_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_variation_product(); - - $request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/variations' ); - $request->set_body_params( - array( - 'sku' => 'DUMMY SKU VARIABLE MEDIUM', - 'regular_price' => '12', - 'description' => 'A medium size.', - 'attributes' => array( - array( - 'name' => 'pa_size', - 'option' => 'medium', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test batch managing product variations. - */ - public function test_product_variations_batch() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_variation_product(); - $children = $product->get_children(); - $request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/variations/batch' ); - $request->set_body_params( - array( - 'update' => array( - array( - 'id' => $children[0], - 'description' => 'Updated description.', - 'image' => array( - 'position' => 0, - 'src' => 'http://cldup.com/Dr1Bczxq4q.png', - 'alt' => 'test upload image', - ), - ), - ), - 'delete' => array( - $children[1], - ), - 'create' => array( - array( - 'sku' => 'DUMMY SKU VARIABLE MEDIUM', - 'regular_price' => '12', - 'description' => 'A medium size.', - 'attributes' => array( - array( - 'name' => 'pa_size', - 'option' => 'medium', - ), - ), - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertContains( 'Updated description.', $data['update'][0]['description'] ); - $this->assertEquals( 'DUMMY SKU VARIABLE MEDIUM', $data['create'][0]['sku'] ); - $this->assertEquals( 'medium', $data['create'][0]['attributes'][0]['option'] ); - $this->assertEquals( $children[1], $data['delete'][0]['id'] ); - - $request = new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 2, count( $data ) ); - } - - /** - * Test variation schema. - * - * @since 3.0.0 - */ - public function test_variation_schema() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'OPTIONS', '/wc/v2/products/' . $product->get_id() . '/variations' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 37, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'date_created', $properties ); - $this->assertArrayHasKey( 'date_modified', $properties ); - $this->assertArrayHasKey( 'description', $properties ); - $this->assertArrayHasKey( 'permalink', $properties ); - $this->assertArrayHasKey( 'sku', $properties ); - $this->assertArrayHasKey( 'price', $properties ); - $this->assertArrayHasKey( 'regular_price', $properties ); - $this->assertArrayHasKey( 'sale_price', $properties ); - $this->assertArrayHasKey( 'date_on_sale_from', $properties ); - $this->assertArrayHasKey( 'date_on_sale_to', $properties ); - $this->assertArrayHasKey( 'on_sale', $properties ); - $this->assertArrayHasKey( 'visible', $properties ); - $this->assertArrayHasKey( 'purchasable', $properties ); - $this->assertArrayHasKey( 'virtual', $properties ); - $this->assertArrayHasKey( 'downloadable', $properties ); - $this->assertArrayHasKey( 'downloads', $properties ); - $this->assertArrayHasKey( 'download_limit', $properties ); - $this->assertArrayHasKey( 'download_expiry', $properties ); - $this->assertArrayHasKey( 'tax_status', $properties ); - $this->assertArrayHasKey( 'tax_class', $properties ); - $this->assertArrayHasKey( 'manage_stock', $properties ); - $this->assertArrayHasKey( 'stock_quantity', $properties ); - $this->assertArrayHasKey( 'in_stock', $properties ); - $this->assertArrayHasKey( 'backorders', $properties ); - $this->assertArrayHasKey( 'backorders_allowed', $properties ); - $this->assertArrayHasKey( 'backordered', $properties ); - $this->assertArrayHasKey( 'weight', $properties ); - $this->assertArrayHasKey( 'dimensions', $properties ); - $this->assertArrayHasKey( 'shipping_class', $properties ); - $this->assertArrayHasKey( 'shipping_class_id', $properties ); - $this->assertArrayHasKey( 'image', $properties ); - $this->assertArrayHasKey( 'attributes', $properties ); - $this->assertArrayHasKey( 'menu_order', $properties ); - $this->assertArrayHasKey( 'meta_data', $properties ); - } - - /** - * Test updating a variation stock. - * - * @since 3.0.0 - */ - public function test_update_variation_manage_stock() { - wp_set_current_user( $this->user ); - - $product = WC_Helper_Product::create_variation_product(); - $product->set_manage_stock( false ); - $product->save(); - - $children = $product->get_children(); - $variation_id = $children[0]; - - // Set stock to true. - $request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ); - $request->set_body_params( - array( - 'manage_stock' => true, - ) - ); - - $response = $this->server->dispatch( $request ); - $variation = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( true, $variation['manage_stock'] ); - - // Set stock to false. - $request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ); - $request->set_body_params( - array( - 'manage_stock' => false, - ) - ); - - $response = $this->server->dispatch( $request ); - $variation = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( false, $variation['manage_stock'] ); - - // Set stock to false but parent is managing stock. - $product->set_manage_stock( true ); - $product->save(); - $request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ); - $request->set_body_params( - array( - 'manage_stock' => false, - ) - ); - - $response = $this->server->dispatch( $request ); - $variation = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 'parent', $variation['manage_stock'] ); - } -} diff --git a/tests/unit-tests/api/v2/products.php b/tests/unit-tests/api/v2/products.php deleted file mode 100644 index e45f51d7f4b..00000000000 --- a/tests/unit-tests/api/v2/products.php +++ /dev/null @@ -1,535 +0,0 @@ -endpoint = new WC_REST_Products_Controller(); - $this->user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * - * @since 3.0.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v2/products', $routes ); - $this->assertArrayHasKey( '/wc/v2/products/(?P[\d]+)', $routes ); - $this->assertArrayHasKey( '/wc/v2/products/batch', $routes ); - } - - /** - * Test getting products. - * - * @since 3.0.0 - */ - public function test_get_products() { - wp_set_current_user( $this->user ); - WC_Helper_Product::create_external_product(); - sleep( 1 ); // So both products have different timestamps. - WC_Helper_Product::create_simple_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) ); - $products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - - $this->assertEquals( 2, count( $products ) ); - $this->assertEquals( 'Dummy Product', $products[0]['name'] ); - $this->assertEquals( 'DUMMY SKU', $products[0]['sku'] ); - $this->assertEquals( 'Dummy External Product', $products[1]['name'] ); - $this->assertEquals( 'DUMMY EXTERNAL SKU', $products[1]['sku'] ); - } - - /** - * Test getting products without permission. - * - * @since 3.0.0 - */ - public function test_get_products_without_permission() { - wp_set_current_user( 0 ); - WC_Helper_Product::create_simple_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test getting a single product. - * - * @since 3.0.0 - */ - public function test_get_product() { - wp_set_current_user( $this->user ); - $simple = WC_Helper_Product::create_external_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $simple->get_id() ) ); - $product = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertContains( - array( - 'id' => $simple->get_id(), - 'name' => 'Dummy External Product', - 'type' => 'simple', - 'status' => 'publish', - 'sku' => 'DUMMY EXTERNAL SKU', - 'regular_price' => 10, - ), - $product - ); - } - - /** - * Test getting single product without permission. - * - * @since 3.0.0 - */ - public function test_get_product_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_simple_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test deleting a single product. - * - * @since 3.0.0 - */ - public function test_delete_product() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - - $request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) ); - $variations = $response->get_data(); - $this->assertEquals( 0, count( $variations ) ); - } - - /** - * Test deleting a single product without permission. - * - * @since 3.0.0 - */ - public function test_delete_product_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test deleting a single product with an invalid ID. - * - * @since 3.0.0 - */ - public function test_delete_product_with_invalid_id() { - wp_set_current_user( 0 ); - $request = new WP_REST_Request( 'DELETE', '/wc/v2/products/0' ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test editing a single product. Tests multiple product types. - * - * @since 3.0.0 - */ - public function test_update_product() { - wp_set_current_user( $this->user ); - - // test simple products. - $product = WC_Helper_Product::create_simple_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) ); - $data = $response->get_data(); - - $this->assertEquals( 'DUMMY SKU', $data['sku'] ); - $this->assertEquals( 10, $data['regular_price'] ); - $this->assertEmpty( $data['sale_price'] ); - - $request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() ); - $request->set_body_params( - array( - 'sku' => 'FIXED-SKU', - 'sale_price' => '8', - 'description' => 'Testing', - 'images' => array( - array( - 'position' => 0, - 'src' => 'http://cldup.com/Dr1Bczxq4q.png', - 'alt' => 'test upload image', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertContains( 'Testing', $data['description'] ); - $this->assertEquals( '8', $data['price'] ); - $this->assertEquals( '8', $data['sale_price'] ); - $this->assertEquals( '10', $data['regular_price'] ); - $this->assertEquals( 'FIXED-SKU', $data['sku'] ); - $this->assertContains( 'Dr1Bczxq4q', $data['images'][0]['src'] ); - $this->assertContains( 'test upload image', $data['images'][0]['alt'] ); - $product->delete( true ); - - // test variable product (variations are tested in product-variations.php). - $product = WC_Helper_Product::create_variation_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) ); - $data = $response->get_data(); - - foreach ( array( 'small', 'large' ) as $term_name ) { - $this->assertContains( $term_name, $data['attributes'][0]['options'] ); - } - - $request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() ); - $request->set_body_params( - array( - 'attributes' => array( - array( - 'id' => 0, - 'name' => 'pa_color', - 'options' => array( - 'red', - 'yellow', - ), - 'visible' => false, - 'variation' => 1, - ), - array( - 'id' => 0, - 'name' => 'pa_size', - 'options' => array( - 'small', - ), - 'visible' => false, - 'variation' => 1, - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( array( 'small' ), $data['attributes'][0]['options'] ); - $this->assertEquals( array( 'red', 'yellow' ), $data['attributes'][1]['options'] ); - $product->delete( true ); - - // test external product. - $product = WC_Helper_Product::create_external_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) ); - $data = $response->get_data(); - - $this->assertEquals( 'Buy external product', $data['button_text'] ); - $this->assertEquals( 'http://woocommerce.com', $data['external_url'] ); - - $request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() ); - $request->set_body_params( - array( - 'button_text' => 'Test API Update', - 'external_url' => 'http://automattic.com', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'Test API Update', $data['button_text'] ); - $this->assertEquals( 'http://automattic.com', $data['external_url'] ); - } - - /** - * Test updating a single product without permission. - * - * @since 3.0.0 - */ - public function test_update_product_without_permission() { - wp_set_current_user( 0 ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() ); - $request->set_body_params( - array( - 'sku' => 'FIXED-SKU-NO-PERMISSION', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test updating a single product with an invalid ID. - * - * @since 3.0.0 - */ - public function test_update_product_with_invalid_id() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'PUT', '/wc/v2/products/0' ); - $request->set_body_params( - array( - 'sku' => 'FIXED-SKU-INVALID-ID', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - } - - /** - * Test creating a single product. - * - * @since 3.0.0 - */ - public function test_create_product() { - wp_set_current_user( $this->user ); - - $request = new WP_REST_Request( 'POST', '/wc/v2/products/shipping_classes' ); - $request->set_body_params( - array( - 'name' => 'Test', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $shipping_class_id = $data['id']; - - // Create simple. - $request = new WP_REST_Request( 'POST', '/wc/v2/products' ); - $request->set_body_params( - array( - 'type' => 'simple', - 'name' => 'Test Simple Product', - 'sku' => 'DUMMY SKU SIMPLE API', - 'regular_price' => '10', - 'shipping_class' => 'test', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( '10', $data['price'] ); - $this->assertEquals( '10', $data['regular_price'] ); - $this->assertTrue( $data['purchasable'] ); - $this->assertEquals( 'DUMMY SKU SIMPLE API', $data['sku'] ); - $this->assertEquals( 'Test Simple Product', $data['name'] ); - $this->assertEquals( 'simple', $data['type'] ); - $this->assertEquals( $shipping_class_id, $data['shipping_class_id'] ); - - // Create external. - $request = new WP_REST_Request( 'POST', '/wc/v2/products' ); - $request->set_body_params( - array( - 'type' => 'external', - 'name' => 'Test External Product', - 'sku' => 'DUMMY SKU EXTERNAL API', - 'regular_price' => '10', - 'button_text' => 'Test Button', - 'external_url' => 'https://wordpress.org', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( '10', $data['price'] ); - $this->assertEquals( '10', $data['regular_price'] ); - $this->assertFalse( $data['purchasable'] ); - $this->assertEquals( 'DUMMY SKU EXTERNAL API', $data['sku'] ); - $this->assertEquals( 'Test External Product', $data['name'] ); - $this->assertEquals( 'external', $data['type'] ); - $this->assertEquals( 'Test Button', $data['button_text'] ); - $this->assertEquals( 'https://wordpress.org', $data['external_url'] ); - - // Create variable. - $request = new WP_REST_Request( 'POST', '/wc/v2/products' ); - $request->set_body_params( - array( - 'type' => 'variable', - 'name' => 'Test Variable Product', - 'sku' => 'DUMMY SKU VARIABLE API', - 'attributes' => array( - array( - 'id' => 0, - 'name' => 'pa_size', - 'options' => array( - 'small', - 'medium', - ), - 'visible' => false, - 'variation' => 1, - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'DUMMY SKU VARIABLE API', $data['sku'] ); - $this->assertEquals( 'Test Variable Product', $data['name'] ); - $this->assertEquals( 'variable', $data['type'] ); - $this->assertEquals( array( 'small', 'medium' ), $data['attributes'][0]['options'] ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) ); - $products = $response->get_data(); - $this->assertEquals( 3, count( $products ) ); - } - - /** - * Test creating a single product without permission. - * - * @since 3.0.0 - */ - public function test_create_product_without_permission() { - wp_set_current_user( 0 ); - - $request = new WP_REST_Request( 'POST', '/wc/v2/products' ); - $request->set_body_params( - array( - 'name' => 'Test Product', - 'regular_price' => '12', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test batch managing products. - */ - public function test_products_batch() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $product_2 = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'POST', '/wc/v2/products/batch' ); - $request->set_body_params( - array( - 'update' => array( - array( - 'id' => $product->get_id(), - 'description' => 'Updated description.', - ), - ), - 'delete' => array( - $product_2->get_id(), - ), - 'create' => array( - array( - 'sku' => 'DUMMY SKU BATCH TEST 1', - 'regular_price' => '10', - 'name' => 'Test Batch Create 1', - 'type' => 'external', - 'button_text' => 'Test Button', - ), - array( - 'sku' => 'DUMMY SKU BATCH TEST 2', - 'regular_price' => '20', - 'name' => 'Test Batch Create 2', - 'type' => 'simple', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertContains( 'Updated description.', $data['update'][0]['description'] ); - $this->assertEquals( 'DUMMY SKU BATCH TEST 1', $data['create'][0]['sku'] ); - $this->assertEquals( 'DUMMY SKU BATCH TEST 2', $data['create'][1]['sku'] ); - $this->assertEquals( 'Test Button', $data['create'][0]['button_text'] ); - $this->assertEquals( 'external', $data['create'][0]['type'] ); - $this->assertEquals( 'simple', $data['create'][1]['type'] ); - $this->assertEquals( $product_2->get_id(), $data['delete'][0]['id'] ); - - $request = new WP_REST_Request( 'GET', '/wc/v2/products' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 3, count( $data ) ); - } - - /** - * Tests to make sure you can filter products post statuses by both - * the status query arg and WP_Query. - * - * @since 3.0.0 - */ - public function test_products_filter_post_status() { - wp_set_current_user( $this->user ); - for ( $i = 0; $i < 8; $i++ ) { - $product = WC_Helper_Product::create_simple_product(); - if ( 0 === $i % 2 ) { - wp_update_post( - array( - 'ID' => $product->get_id(), - 'post_status' => 'draft', - ) - ); - } - } - - // Test filtering with status=publish. - $request = new WP_REST_Request( 'GET', '/wc/v2/products' ); - $request->set_param( 'status', 'publish' ); - $response = $this->server->dispatch( $request ); - $products = $response->get_data(); - - $this->assertEquals( 4, count( $products ) ); - foreach ( $products as $product ) { - $this->assertEquals( 'publish', $product['status'] ); - } - - // Test filtering with status=draft. - $request = new WP_REST_Request( 'GET', '/wc/v2/products' ); - $request->set_param( 'status', 'draft' ); - $response = $this->server->dispatch( $request ); - $products = $response->get_data(); - - $this->assertEquals( 4, count( $products ) ); - foreach ( $products as $product ) { - $this->assertEquals( 'draft', $product['status'] ); - } - - // Test filtering with no filters - which should return 'any' (all 8). - $request = new WP_REST_Request( 'GET', '/wc/v2/products' ); - $response = $this->server->dispatch( $request ); - $products = $response->get_data(); - - $this->assertEquals( 8, count( $products ) ); - } - - /** - * Test product schema. - * - * @since 3.0.0 - */ - public function test_product_schema() { - wp_set_current_user( $this->user ); - $product = WC_Helper_Product::create_simple_product(); - $request = new WP_REST_Request( 'OPTIONS', '/wc/v2/products/' . $product->get_id() ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - $this->assertEquals( 65, count( $properties ) ); - } -} diff --git a/tests/unit-tests/api/v2/settings.php b/tests/unit-tests/api/v2/settings.php deleted file mode 100644 index f88abaa1588..00000000000 --- a/tests/unit-tests/api/v2/settings.php +++ /dev/null @@ -1,892 +0,0 @@ -endpoint = new WC_REST_Setting_Options_Controller(); - WC_Helper_Settings::register(); - $this->user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * - * @since 3.0.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v2/settings', $routes ); - $this->assertArrayHasKey( '/wc/v2/settings/(?P[\w-]+)', $routes ); - $this->assertArrayHasKey( '/wc/v2/settings/(?P[\w-]+)/(?P[\w-]+)', $routes ); - } - - /** - * Test getting all groups. - * - * @since 3.0.0 - */ - public function test_get_groups() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings' ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - - $this->assertContains( - array( - 'id' => 'test', - 'label' => 'Test extension', - 'parent_id' => '', - 'description' => 'My awesome test settings.', - 'sub_groups' => array( 'sub-test' ), - '_links' => array( - 'options' => array( - array( - 'href' => rest_url( '/wc/v2/settings/test' ), - ), - ), - ), - ), - $data - ); - - $this->assertContains( - array( - 'id' => 'sub-test', - 'label' => 'Sub test', - 'parent_id' => 'test', - 'description' => '', - 'sub_groups' => array(), - '_links' => array( - 'options' => array( - array( - 'href' => rest_url( '/wc/v2/settings/sub-test' ), - ), - ), - ), - ), - $data - ); - } - - /** - * Test /settings without valid permissions/creds. - * - * @since 3.0.0 - */ - public function test_get_groups_without_permission() { - wp_set_current_user( 0 ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test /settings without valid permissions/creds. - * - * @since 3.0.0 - * @covers WC_Rest_Settings_Controller::get_items - */ - public function test_get_groups_none_registered() { - wp_set_current_user( $this->user ); - - remove_all_filters( 'woocommerce_settings_groups' ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings' ) ); - $this->assertEquals( 500, $response->get_status() ); - - WC_Helper_Settings::register(); - } - - /** - * Test groups schema. - * - * @since 3.0.0 - */ - public function test_get_group_schema() { - $request = new WP_REST_Request( 'OPTIONS', '/wc/v2/settings' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - $this->assertEquals( 5, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'parent_id', $properties ); - $this->assertArrayHasKey( 'label', $properties ); - $this->assertArrayHasKey( 'description', $properties ); - $this->assertArrayHasKey( 'sub_groups', $properties ); - } - - /** - * Test settings schema. - * - * @since 3.0.0 - */ - public function test_get_setting_schema() { - $request = new WP_REST_Request( 'OPTIONS', '/wc/v2/settings/test/woocommerce_shop_page_display' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - $this->assertEquals( 9, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'label', $properties ); - $this->assertArrayHasKey( 'description', $properties ); - $this->assertArrayHasKey( 'value', $properties ); - $this->assertArrayHasKey( 'default', $properties ); - $this->assertArrayHasKey( 'tip', $properties ); - $this->assertArrayHasKey( 'placeholder', $properties ); - $this->assertArrayHasKey( 'type', $properties ); - $this->assertArrayHasKey( 'options', $properties ); - } - - /** - * Test getting a single group. - * - * @since 3.0.0 - */ - public function test_get_group() { - wp_set_current_user( $this->user ); - - // test route callback receiving an empty group id - $result = $this->endpoint->get_group_settings( '' ); - $this->assertIsWPError( $result ); - - // test getting a group that does not exist - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/not-real' ) ); - $this->assertEquals( 404, $response->get_status() ); - - // test getting the 'invalid' group - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/invalid' ) ); - $this->assertEquals( 404, $response->get_status() ); - - // test getting a valid group with settings attached to it - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test' ) ); - $data = $response->get_data(); - $this->assertEquals( 1, count( $data ) ); - $this->assertEquals( 'woocommerce_shop_page_display', $data[0]['id'] ); - $this->assertEmpty( $data[0]['value'] ); - } - - /** - * Test getting a single group without permission. - * - * @since 3.0.0 - */ - public function test_get_group_without_permission() { - wp_set_current_user( 0 ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/coupon-data' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test updating a single setting. - * - * @since 3.0.0 - */ - public function test_update_setting() { - wp_set_current_user( $this->user ); - - // test defaults first - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test/woocommerce_shop_page_display' ) ); - $data = $response->get_data(); - $this->assertEquals( '', $data['value'] ); - - // test updating shop display setting - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) ); - $request->set_body_params( - array( - 'value' => 'both', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'both', $data['value'] ); - $this->assertEquals( 'both', get_option( 'woocommerce_shop_page_display' ) ); - - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) ); - $request->set_body_params( - array( - 'value' => 'subcategories', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'subcategories', $data['value'] ); - $this->assertEquals( 'subcategories', get_option( 'woocommerce_shop_page_display' ) ); - - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) ); - $request->set_body_params( - array( - 'value' => '', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( '', $data['value'] ); - $this->assertEquals( '', get_option( 'woocommerce_shop_page_display' ) ); - } - - /** - * Test updating multiple settings at once. - * - * @since 3.0.0 - */ - public function test_update_settings() { - wp_set_current_user( $this->user ); - - // test defaults first - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test' ) ); - $data = $response->get_data(); - $this->assertEquals( '', $data[0]['value'] ); - - // test setting both at once - $request = new WP_REST_Request( 'POST', '/wc/v2/settings/test/batch' ); - $request->set_body_params( - array( - 'update' => array( - array( - 'id' => 'woocommerce_shop_page_display', - 'value' => 'both', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'both', $data['update'][0]['value'] ); - $this->assertEquals( 'both', get_option( 'woocommerce_shop_page_display' ) ); - - // test updating one, but making sure the other value stays the same - $request = new WP_REST_Request( 'POST', '/wc/v2/settings/test/batch' ); - $request->set_body_params( - array( - 'update' => array( - array( - 'id' => 'woocommerce_shop_page_display', - 'value' => 'subcategories', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( 'subcategories', $data['update'][0]['value'] ); - $this->assertEquals( 'subcategories', get_option( 'woocommerce_shop_page_display' ) ); - } - - /** - * Test getting a single setting. - * - * @since 3.0.0 - */ - public function test_get_setting() { - wp_set_current_user( $this->user ); - - // test getting an invalid setting from a group that does not exist - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/not-real/woocommerce_shop_page_display' ) ); - $data = $response->get_data(); - $this->assertEquals( 404, $response->get_status() ); - - // test getting an invalid setting from a group that does exist - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/invalid/invalid' ) ); - $data = $response->get_data(); - $this->assertEquals( 404, $response->get_status() ); - - // test getting a valid setting - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test/woocommerce_shop_page_display' ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - - $this->assertEquals( 'woocommerce_shop_page_display', $data['id'] ); - $this->assertEquals( 'Shop page display', $data['label'] ); - $this->assertEquals( '', $data['default'] ); - $this->assertEquals( 'select', $data['type'] ); - $this->assertEquals( '', $data['value'] ); - } - - /** - * Test getting a single setting without valid user permissions. - * - * @since 3.0.0 - */ - public function test_get_setting_without_permission() { - wp_set_current_user( 0 ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test/woocommerce_shop_page_display' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Tests the GET single setting route handler receiving an empty setting ID. - * - * @since 3.0.0 - */ - public function test_get_setting_empty_setting_id() { - $result = $this->endpoint->get_setting( 'test', '' ); - - $this->assertIsWPError( $result ); - } - - /** - * Tests the GET single setting route handler receiving an invalid setting ID. - * - * @since 3.0.0 - */ - public function test_get_setting_invalid_setting_id() { - $result = $this->endpoint->get_setting( 'test', 'invalid' ); - - $this->assertIsWPError( $result ); - } - - /** - * Tests the GET single setting route handler encountering an invalid setting type. - * - * @since 3.0.0 - */ - public function test_get_setting_invalid_setting_type() { - // $controller = $this->getMock( 'WC_Rest_Setting_Options_Controller', array( 'get_group_settings', 'is_setting_type_valid' ) ); - $controller = $this->getMockBuilder( 'WC_Rest_Setting_Options_Controller' )->setMethods( array( 'get_group_settings', 'is_setting_type_valid' ) )->getMock(); - - $controller - ->expects( $this->any() ) - ->method( 'get_group_settings' ) - ->will( $this->returnValue( WC_Helper_Settings::register_test_settings( array() ) ) ); - - $controller - ->expects( $this->any() ) - ->method( 'is_setting_type_valid' ) - ->will( $this->returnValue( false ) ); - - $result = $controller->get_setting( 'test', 'woocommerce_shop_page_display' ); - - $this->assertIsWPError( $result ); - } - - /** - * Test updating a single setting without valid user permissions. - * - * @since 3.0.0 - */ - public function test_update_setting_without_permission() { - wp_set_current_user( 0 ); - - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) ); - $request->set_body_params( - array( - 'value' => 'subcategories', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - - /** - * Test updating multiple settings without valid user permissions. - * - * @since 3.0.0 - */ - public function test_update_settings_without_permission() { - wp_set_current_user( 0 ); - - $request = new WP_REST_Request( 'POST', '/wc/v2/settings/test/batch' ); - $request->set_body_params( - array( - 'update' => array( - array( - 'id' => 'woocommerce_shop_page_display', - 'value' => 'subcategories', - ), - ), - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test updating a bad setting ID. - * - * @since 3.0.0 - * @covers WC_Rest_Setting_Options_Controller::update_item - */ - public function test_update_setting_bad_setting_id() { - wp_set_current_user( $this->user ); - - $request = new WP_REST_Request( 'PUT', '/wc/v2/settings/test/invalid' ); - $request->set_body_params( - array( - 'value' => 'test', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Tests our classic setting registration to make sure settings added for WP-Admin are available over the API. - * - * @since 3.0.0 - */ - public function test_classic_settings() { - wp_set_current_user( $this->user ); - - // Make sure the group is properly registered - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/products' ) ); - $data = $response->get_data(); - $this->assertTrue( is_array( $data ) ); - $this->assertContains( - array( - 'id' => 'woocommerce_downloads_require_login', - 'label' => 'Access restriction', - 'description' => 'Downloads require login', - 'type' => 'checkbox', - 'default' => 'no', - 'tip' => 'This setting does not apply to guest purchases.', - 'value' => 'no', - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v2/settings/products/woocommerce_downloads_require_login' ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v2/settings/products' ), - ), - ), - ), - ), - $data - ); - - // test get single - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/products/woocommerce_dimension_unit' ) ); - $data = $response->get_data(); - - $this->assertEquals( 'cm', $data['default'] ); - - // test update - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_dimension_unit' ) ); - $request->set_body_params( - array( - 'value' => 'yd', - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'yd', $data['value'] ); - $this->assertEquals( 'yd', get_option( 'woocommerce_dimension_unit' ) ); - } - - /** - * Tests our email etting registration to make sure settings added for WP-Admin are available over the API. - * - * @since 3.0.0 - */ - public function test_email_settings() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/email_new_order' ) ); - $settings = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - - $this->assertContains( - array( - 'id' => 'recipient', - 'label' => 'Recipient(s)', - 'description' => 'Enter recipients (comma separated) for this email. Defaults to admin@example.org.', - 'type' => 'text', - 'default' => '', - 'tip' => 'Enter recipients (comma separated) for this email. Defaults to admin@example.org.', - 'value' => '', - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v2/settings/email_new_order/recipient' ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v2/settings/email_new_order' ), - ), - ), - ), - ), - $settings - ); - - // test get single - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/email_new_order/subject' ) ); - $setting = $response->get_data(); - - $this->assertEquals( - array( - 'id' => 'subject', - 'label' => 'Subject', - 'description' => 'Available placeholders: {site_title}, {order_date}, {order_number}', - 'type' => 'text', - 'default' => '', - 'tip' => 'Available placeholders: {site_title}, {order_date}, {order_number}', - 'value' => '', - ), - $setting - ); - - // test update - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_new_order', 'subject' ) ); - $request->set_body_params( - array( - 'value' => 'This is my subject', - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - - $this->assertEquals( - array( - 'id' => 'subject', - 'label' => 'Subject', - 'description' => 'Available placeholders: {site_title}, {order_date}, {order_number}', - 'type' => 'text', - 'default' => '', - 'tip' => 'Available placeholders: {site_title}, {order_date}, {order_number}', - 'value' => 'This is my subject', - ), - $setting - ); - - // test updating another subject and making sure it works with a "similar" id - $request = new WP_REST_Request( 'GET', sprintf( '/wc/v2/settings/%s/%s', 'email_customer_new_account', 'subject' ) ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - - $this->assertEmpty( $setting['value'] ); - - // test update - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_customer_new_account', 'subject' ) ); - $request->set_body_params( - array( - 'value' => 'This is my new subject', - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - - $this->assertEquals( 'This is my new subject', $setting['value'] ); - - // make sure the other is what we left it - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/email_new_order/subject' ) ); - $setting = $response->get_data(); - - $this->assertEquals( 'This is my subject', $setting['value'] ); - } - - /** - * Test validation of checkbox settings. - * - * @since 3.0.0 - */ - public function test_validation_checkbox() { - wp_set_current_user( $this->user ); - - // test bogus value - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_cancelled_order', 'enabled' ) ); - $request->set_body_params( - array( - 'value' => 'not_yes_or_no', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - - // test yes - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_cancelled_order', 'enabled' ) ); - $request->set_body_params( - array( - 'value' => 'yes', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - - // test no - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_cancelled_order', 'enabled' ) ); - $request->set_body_params( - array( - 'value' => 'no', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - } - - /** - * Test validation of radio settings. - * - * @since 3.0.0 - */ - public function test_validation_radio() { - wp_set_current_user( $this->user ); - - // not a valid option - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) ); - $request->set_body_params( - array( - 'value' => 'billing2', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - - // valid - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) ); - $request->set_body_params( - array( - 'value' => 'billing', - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - } - - /** - * Test validation of multiselect. - * - * @since 3.0.0 - */ - public function test_validation_multiselect() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v2/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) ) ); - $setting = $response->get_data(); - $this->assertEmpty( $setting['value'] ); - - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) ); - $request->set_body_params( - array( - 'value' => array( 'AX', 'DZ', 'MMM' ), - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( array( 'AX', 'DZ' ), $setting['value'] ); - } - - /** - * Test validation of select. - * - * @since 3.0.0 - */ - public function test_validation_select() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) ) ); - $setting = $response->get_data(); - $this->assertEquals( 'kg', $setting['value'] ); - - // invalid - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) ); - $request->set_body_params( - array( - 'value' => 'pounds', // invalid, should be lbs - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - - // valid - $request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) ); - $request->set_body_params( - array( - 'value' => 'lbs', // invalid, should be lbs - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( 'lbs', $setting['value'] ); - } - - /** - * Test to make sure the 'base location' setting is present in the response. - * That it is returned as 'select' and not 'single_select_country', - * and that both state and country options are returned. - * - * @since 3.0.7 - */ - public function test_woocommerce_default_country() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_default_country' ) ); - $setting = $response->get_data(); - - $this->assertEquals( 'select', $setting['type'] ); - $this->assertArrayHasKey( 'GB', $setting['options'] ); - $this->assertArrayHasKey( 'US:OR', $setting['options'] ); - } - - /** - * Test to make sure the store address setting can be fetched and updated. - * - * @since 3.1.1 - */ - public function test_woocommerce_store_address() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_store_address' ) ); - $setting = $response->get_data(); - $this->assertEquals( 'text', $setting['type'] ); - - // Repalce the old value with something uniquely new - $old_value = $setting['value']; - $new_value = $old_value . ' ' . rand( 1000, 9999 ); - $request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_address' ); - $request->set_body_params( - array( - 'value' => $new_value, - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( $new_value, $setting['value'] ); - - // Put the original value back - $request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_address' ); - $request->set_body_params( - array( - 'value' => $old_value, - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( $old_value, $setting['value'] ); - } - - /** - * Test to make sure the store address 2 (line 2) setting can be fetched and updated. - * - * @since 3.1.1 - */ - public function test_woocommerce_store_address_2() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_store_address_2' ) ); - $setting = $response->get_data(); - $this->assertEquals( 'text', $setting['type'] ); - - // Repalce the old value with something uniquely new - $old_value = $setting['value']; - $new_value = $old_value . ' ' . rand( 1000, 9999 ); - $request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_address_2' ); - $request->set_body_params( - array( - 'value' => $new_value, - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( $new_value, $setting['value'] ); - - // Put the original value back - $request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_address_2' ); - $request->set_body_params( - array( - 'value' => $old_value, - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( $old_value, $setting['value'] ); - } - - /** - * Test to make sure the store city setting can be fetched and updated. - * - * @since 3.1.1 - */ - public function test_woocommerce_store_city() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_store_city' ) ); - $setting = $response->get_data(); - $this->assertEquals( 'text', $setting['type'] ); - - // Repalce the old value with something uniquely new - $old_value = $setting['value']; - $new_value = $old_value . ' ' . rand( 1000, 9999 ); - $request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_city' ); - $request->set_body_params( - array( - 'value' => $new_value, - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( $new_value, $setting['value'] ); - - // Put the original value back - $request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_city' ); - $request->set_body_params( - array( - 'value' => $old_value, - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( $old_value, $setting['value'] ); - } - - /** - * Test to make sure the store postcode setting can be fetched and updated. - * - * @since 3.1.1 - */ - public function test_woocommerce_store_postcode() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_store_postcode' ) ); - $setting = $response->get_data(); - $this->assertEquals( 'text', $setting['type'] ); - - // Repalce the old value with something uniquely new - $old_value = $setting['value']; - $new_value = $old_value . ' ' . rand( 1000, 9999 ); - $request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_postcode' ); - $request->set_body_params( - array( - 'value' => $new_value, - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( $new_value, $setting['value'] ); - - // Put the original value back - $request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_postcode' ); - $request->set_body_params( - array( - 'value' => $old_value, - ) - ); - $response = $this->server->dispatch( $request ); - $setting = $response->get_data(); - $this->assertEquals( $old_value, $setting['value'] ); - } -} diff --git a/tests/unit-tests/api/v2/shipping-methods.php b/tests/unit-tests/api/v2/shipping-methods.php deleted file mode 100644 index 65c153e8f31..00000000000 --- a/tests/unit-tests/api/v2/shipping-methods.php +++ /dev/null @@ -1,143 +0,0 @@ -endpoint = new WC_REST_Shipping_Methods_Controller(); - $this->user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - * - * @since 3.0.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v2/shipping_methods', $routes ); - $this->assertArrayHasKey( '/wc/v2/shipping_methods/(?P[\w-]+)', $routes ); - } - - /** - * Test getting all shipping methods. - * - * @since 3.0.0 - */ - public function test_get_shipping_methods() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods' ) ); - $methods = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertContains( - array( - 'id' => 'free_shipping', - 'title' => 'Free shipping', - 'description' => 'Free shipping is a special method which can be triggered with coupons and minimum spends.', - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v2/shipping_methods/free_shipping' ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v2/shipping_methods' ), - ), - ), - ), - ), - $methods - ); - } - - /** - * Tests to make sure shipping methods cannot viewed without valid permissions. - * - * @since 3.0.0 - */ - public function test_get_shipping_methods_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Tests getting a single shipping method. - * - * @since 3.0.0 - */ - public function test_get_shipping_method() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods/local_pickup' ) ); - $method = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( - array( - 'id' => 'local_pickup', - 'title' => 'Local pickup', - 'description' => 'Allow customers to pick up orders themselves. By default, when using local pickup store base taxes will apply regardless of customer address.', - ), - $method - ); - } - - /** - * Tests getting a single shipping method without the correct permissions. - * - * @since 3.0.0 - */ - public function test_get_shipping_method_without_permission() { - wp_set_current_user( 0 ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods/local_pickup' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Tests getting a shipping method with an invalid ID. - * - * @since 3.0.0 - */ - public function test_get_shipping_method_invalid_id() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods/fake_method' ) ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test the shipping method schema. - * - * @since 3.0.0 - */ - public function test_shipping_method_schema() { - wp_set_current_user( $this->user ); - - $request = new WP_REST_Request( 'OPTIONS', '/wc/v2/shipping_methods' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 3, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'title', $properties ); - $this->assertArrayHasKey( 'description', $properties ); - } -} diff --git a/tests/unit-tests/api/v2/shipping-zones.php b/tests/unit-tests/api/v2/shipping-zones.php deleted file mode 100644 index c6e2c4a102e..00000000000 --- a/tests/unit-tests/api/v2/shipping-zones.php +++ /dev/null @@ -1,800 +0,0 @@ -endpoint = new WC_REST_Shipping_Zones_Controller(); - $this->user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - $this->zones = array(); - } - - /** - * Helper method to create a Shipping Zone. - * - * @param string $name Zone name. - * @param int $order Optional. Zone sort order. - * @return WC_Shipping_Zone - */ - protected function create_shipping_zone( $name, $order = 0, $locations = array() ) { - $zone = new WC_Shipping_Zone( null ); - $zone->set_zone_name( $name ); - $zone->set_zone_order( $order ); - $zone->set_locations( $locations ); - $zone->save(); - - $this->zones[] = $zone; - - return $zone; - } - - /** - * Test route registration. - * @since 3.0.0 - */ - 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]+)/locations', $routes ); - $this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P[\d]+)/methods', $routes ); - $this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P[\d]+)/methods/(?P[\d]+)', $routes ); - } - - /** - * Test getting all Shipping Zones. - * @since 3.0.0 - */ - public function test_get_zones() { - wp_set_current_user( $this->user ); - - // "Rest of the World" zone exists by default - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( count( $data ), 1 ); - $this->assertContains( - array( - 'id' => $data[0]['id'], - 'name' => 'Locations not covered by your other zones', - 'order' => 0, - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $data[0]['id'] ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones' ), - ), - ), - 'describedby' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $data[0]['id'] . '/locations' ), - ), - ), - ), - ), - $data - ); - - // Create a zone and make sure it's in the response - $this->create_shipping_zone( 'Zone 1' ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( count( $data ), 2 ); - $this->assertContains( - array( - 'id' => $data[1]['id'], - 'name' => 'Zone 1', - 'order' => 0, - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $data[1]['id'] ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones' ), - ), - ), - 'describedby' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $data[1]['id'] . '/locations' ), - ), - ), - ), - ), - $data - ); - } - - /** - * Test /shipping/zones without valid permissions/creds. - * @since 3.0.0 - */ - public function test_get_shipping_zones_without_permission() { - wp_set_current_user( 0 ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test /shipping/zones while Shipping is disabled in WooCommerce. - * @since 3.0.0 - */ - public function test_get_shipping_zones_disabled_shipping() { - wp_set_current_user( $this->user ); - - add_filter( 'wc_shipping_enabled', '__return_false' ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) ); - $this->assertEquals( 404, $response->get_status() ); - - remove_filter( 'wc_shipping_enabled', '__return_false' ); - } - - /** - * Test Shipping Zone schema. - * @since 3.0.0 - */ - public function test_get_shipping_zone_schema() { - $request = new WP_REST_Request( 'OPTIONS', '/wc/v2/shipping/zones' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - $this->assertEquals( 3, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertTrue( $properties['id']['readonly'] ); - $this->assertArrayHasKey( 'name', $properties ); - $this->assertArrayHasKey( 'order', $properties ); - } - - /** - * Test Shipping Zone create endpoint. - * @since 3.0.0 - */ - public function test_create_shipping_zone() { - wp_set_current_user( $this->user ); - - $request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones' ); - $request->set_body_params( - array( - 'name' => 'Test Zone', - 'order' => 1, - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 201, $response->get_status() ); - $this->assertEquals( - array( - 'id' => $data['id'], - 'name' => 'Test Zone', - 'order' => 1, - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $data['id'] ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones' ), - ), - ), - 'describedby' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $data['id'] . '/locations' ), - ), - ), - ), - ), - $data - ); - } - - /** - * Test Shipping Zone create endpoint. - * @since 3.0.0 - */ - public function test_create_shipping_zone_without_permission() { - wp_set_current_user( 0 ); - - $request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones' ); - $request->set_body_params( - array( - 'name' => 'Test Zone', - 'order' => 1, - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test Shipping Zone update endpoint. - * @since 3.0.0 - */ - public function test_update_shipping_zone() { - wp_set_current_user( $this->user ); - - $zone = $this->create_shipping_zone( 'Test Zone' ); - - $request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/' . $zone->get_id() ); - $request->set_body_params( - array( - 'name' => 'Zone Test', - 'order' => 2, - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( - array( - 'id' => $zone->get_id(), - 'name' => 'Zone Test', - 'order' => 2, - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones' ), - ), - ), - 'describedby' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ), - ), - ), - ), - ), - $data - ); - } - - /** - * Test Shipping Zone update endpoint with a bad zone ID. - * @since 3.0.0 - */ - public function test_update_shipping_zone_invalid_id() { - wp_set_current_user( $this->user ); - - $request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/555555' ); - $request->set_body_params( - array( - 'name' => 'Zone Test', - 'order' => 2, - ) - ); - $response = $this->server->dispatch( $request ); - - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test Shipping Zone delete endpoint. - * @since 3.0.0 - */ - public function test_delete_shipping_zone() { - wp_set_current_user( $this->user ); - $zone = $this->create_shipping_zone( 'Zone 1' ); - - $request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - } - - /** - * Test Shipping Zone delete endpoint without permissions. - * @since 3.0.0 - */ - public function test_delete_shipping_zone_without_permission() { - wp_set_current_user( 0 ); - $zone = $this->create_shipping_zone( 'Zone 1' ); - - $request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test Shipping Zone delete endpoint with a bad zone ID. - * @since 3.0.0 - */ - public function test_delete_shipping_zone_invalid_id() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/555555' ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test getting a single Shipping Zone. - * @since 3.0.0 - */ - public function test_get_single_shipping_zone() { - wp_set_current_user( $this->user ); - - $zone = $this->create_shipping_zone( 'Test Zone' ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( - array( - 'id' => $zone->get_id(), - 'name' => 'Test Zone', - 'order' => 0, - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones' ), - ), - ), - 'describedby' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ), - ), - ), - ), - ), - $data - ); - } - - /** - * Test getting a single Shipping Zone with a bad zone ID. - * @since 3.0.0 - */ - public function test_get_single_shipping_zone_invalid_id() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1' ) ); - - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test getting Shipping Zone Locations. - * @since 3.0.0 - */ - public function test_get_locations() { - wp_set_current_user( $this->user ); - - // Create a zone - $zone = $this->create_shipping_zone( - 'Zone 1', - 0, - array( - array( - 'code' => 'US', - 'type' => 'country', - ), - ) - ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( count( $data ), 1 ); - $this->assertEquals( - array( - array( - 'code' => 'US', - 'type' => 'country', - '_links' => array( - 'collection' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ), - ), - ), - 'describes' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ), - ), - ), - ), - ), - ), - $data - ); - } - - /** - * Test getting Shipping Zone Locations with a bad zone ID. - * @since 3.0.0 - */ - public function test_get_locations_invalid_id() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/locations' ) ); - - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test Shipping Zone Locations update endpoint. - * @since 3.0.0 - */ - public function test_update_locations() { - wp_set_current_user( $this->user ); - - $zone = $this->create_shipping_zone( 'Test Zone' ); - - $request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ); - $request->add_header( 'Content-Type', 'application/json' ); - $request->set_body( - json_encode( - array( - array( - 'code' => 'UK', - 'type' => 'country', - ), - array( - 'code' => 'US', // test that locations missing "type" treated as country. - ), - array( - 'code' => 'SW1A0AA', - 'type' => 'postcode', - ), - array( - 'type' => 'continent', // test that locations missing "code" aren't saved - ), - ) - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 3, count( $data ) ); - $this->assertEquals( - array( - array( - 'code' => 'UK', - 'type' => 'country', - '_links' => array( - 'collection' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ), - ), - ), - 'describes' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ), - ), - ), - ), - ), - array( - 'code' => 'US', - 'type' => 'country', - '_links' => array( - 'collection' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ), - ), - ), - 'describes' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ), - ), - ), - ), - ), - array( - 'code' => 'SW1A0AA', - 'type' => 'postcode', - '_links' => array( - 'collection' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ), - ), - ), - 'describes' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ), - ), - ), - ), - ), - ), - $data - ); - } - - /** - * Test updating Shipping Zone Locations with a bad zone ID. - * @since 3.0.0 - */ - public function test_update_locations_invalid_id() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/1/locations' ) ); - - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test getting all Shipping Zone Methods and getting a single Shipping Zone Method. - * @since 3.0.0 - */ - public function test_get_methods() { - wp_set_current_user( $this->user ); - - // Create a shipping method and make sure it's in the response - $zone = $this->create_shipping_zone( 'Zone 1' ); - $instance_id = $zone->add_shipping_method( 'flat_rate' ); - $methods = $zone->get_shipping_methods(); - $method = $methods[ $instance_id ]; - - $settings = array(); - $method->init_instance_settings(); - foreach ( $method->get_instance_form_fields() as $id => $field ) { - $data = array( - 'id' => $id, - 'label' => $field['title'], - 'description' => ( empty( $field['description'] ) ? '' : $field['description'] ), - 'type' => $field['type'], - 'value' => $method->instance_settings[ $id ], - 'default' => ( empty( $field['default'] ) ? '' : $field['default'] ), - 'tip' => ( empty( $field['description'] ) ? '' : $field['description'] ), - 'placeholder' => ( empty( $field['placeholder'] ) ? '' : $field['placeholder'] ), - ); - if ( ! empty( $field['options'] ) ) { - $data['options'] = $field['options']; - } - $settings[ $id ] = $data; - } - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' ) ); - $data = $response->get_data(); - $expected = array( - 'id' => $instance_id, - 'instance_id' => $instance_id, - 'title' => $method->instance_settings['title'], - 'order' => $method->method_order, - 'enabled' => ( 'yes' === $method->enabled ), - 'method_id' => $method->id, - 'method_title' => $method->method_title, - 'method_description' => $method->method_description, - 'settings' => $settings, - '_links' => array( - 'self' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ), - ), - ), - 'collection' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' ), - ), - ), - 'describes' => array( - array( - 'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ), - ), - ), - ), - ); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( count( $data ), 1 ); - $this->assertContains( $expected, $data ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( $expected, $data ); - } - - /** - * Test getting all Shipping Zone Methods with a bad zone ID. - * @since 3.0.0 - */ - public function test_get_methods_invalid_zone_id() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/methods' ) ); - - $this->assertEquals( 404, $response->get_status() ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/methods/1' ) ); - - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test getting a single Shipping Zone Method with a bad ID. - * @since 3.0.0 - */ - public function test_get_methods_invalid_method_id() { - wp_set_current_user( $this->user ); - - $zone = $this->create_shipping_zone( 'Zone 1' ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/1' ) ); - - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test updating a Shipping Zone Method. - * @since 3.0.0 - */ - public function test_update_methods() { - wp_set_current_user( $this->user ); - - $zone = $this->create_shipping_zone( 'Zone 1' ); - $instance_id = $zone->add_shipping_method( 'flat_rate' ); - $methods = $zone->get_shipping_methods(); - $method = $methods[ $instance_id ]; - - // Test defaults - $request = new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertArrayHasKey( 'title', $data['settings'] ); - $this->assertEquals( 'Flat rate', $data['settings']['title']['value'] ); - $this->assertArrayHasKey( 'tax_status', $data['settings'] ); - $this->assertEquals( 'taxable', $data['settings']['tax_status']['value'] ); - $this->assertArrayHasKey( 'cost', $data['settings'] ); - $this->assertEquals( '0', $data['settings']['cost']['value'] ); - - // Update a single value - $request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ); - $request->set_body_params( - array( - 'settings' => array( - 'cost' => 5, - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertArrayHasKey( 'title', $data['settings'] ); - $this->assertEquals( 'Flat rate', $data['settings']['title']['value'] ); - $this->assertArrayHasKey( 'tax_status', $data['settings'] ); - $this->assertEquals( 'taxable', $data['settings']['tax_status']['value'] ); - $this->assertArrayHasKey( 'cost', $data['settings'] ); - $this->assertEquals( '5', $data['settings']['cost']['value'] ); - - // Test multiple settings - $request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ); - $request->set_body_params( - array( - 'settings' => array( - 'cost' => 10, - 'tax_status' => 'none', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertArrayHasKey( 'title', $data['settings'] ); - $this->assertEquals( 'Flat rate', $data['settings']['title']['value'] ); - $this->assertArrayHasKey( 'tax_status', $data['settings'] ); - $this->assertEquals( 'none', $data['settings']['tax_status']['value'] ); - $this->assertArrayHasKey( 'cost', $data['settings'] ); - $this->assertEquals( '10', $data['settings']['cost']['value'] ); - - // Test bogus - $request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ); - $request->set_body_params( - array( - 'settings' => array( - 'cost' => 10, - 'tax_status' => 'this_is_not_a_valid_option', - ), - ) - ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - - // Test other parameters - $this->assertTrue( $data['enabled'] ); - $this->assertEquals( 1, $data['order'] ); - - $request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ); - $request->set_body_params( - array( - 'enabled' => false, - 'order' => 2, - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertFalse( $data['enabled'] ); - $this->assertEquals( 2, $data['order'] ); - $this->assertArrayHasKey( 'cost', $data['settings'] ); - $this->assertEquals( '10', $data['settings']['cost']['value'] ); - } - - /** - * Test creating a Shipping Zone Method. - * @since 3.0.0 - */ - public function test_create_method() { - wp_set_current_user( $this->user ); - $zone = $this->create_shipping_zone( 'Zone 1' ); - $request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' ); - $request->set_body_params( - array( - 'method_id' => 'flat_rate', - 'enabled' => false, - 'order' => 2, - ) - ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertFalse( $data['enabled'] ); - $this->assertEquals( 2, $data['order'] ); - $this->assertArrayHasKey( 'cost', $data['settings'] ); - $this->assertEquals( '0', $data['settings']['cost']['value'] ); - } - - /** - * Test deleting a Shipping Zone Method. - * @since 3.0.0 - */ - public function test_delete_method() { - wp_set_current_user( $this->user ); - $zone = $this->create_shipping_zone( 'Zone 1' ); - $instance_id = $zone->add_shipping_method( 'flat_rate' ); - $methods = $zone->get_shipping_methods(); - $method = $methods[ $instance_id ]; - $request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ); - $request->set_param( 'force', true ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); - } -} diff --git a/tests/unit-tests/api/v2/system-status.php b/tests/unit-tests/api/v2/system-status.php deleted file mode 100644 index f9b5e471a16..00000000000 --- a/tests/unit-tests/api/v2/system-status.php +++ /dev/null @@ -1,356 +0,0 @@ -endpoint = new WC_REST_System_Status_Controller(); - $this->user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test route registration. - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - $this->assertArrayHasKey( '/wc/v2/system_status', $routes ); - $this->assertArrayHasKey( '/wc/v2/system_status/tools', $routes ); - $this->assertArrayHasKey( '/wc/v2/system_status/tools/(?P[\w-]+)', $routes ); - } - - /** - * Test to make sure system status cannot be accessed without valid creds - * - * @since 3.0.0 - */ - public function test_get_system_status_info_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test to make sure root properties are present. - * (environment, theme, database, etc). - * - * @since 3.0.0 - */ - public function test_get_system_status_info_returns_root_properties() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) ); - $data = $response->get_data(); - - $this->assertArrayHasKey( 'environment', $data ); - $this->assertArrayHasKey( 'database', $data ); - $this->assertArrayHasKey( 'active_plugins', $data ); - $this->assertArrayHasKey( 'theme', $data ); - $this->assertArrayHasKey( 'settings', $data ); - $this->assertArrayHasKey( 'security', $data ); - $this->assertArrayHasKey( 'pages', $data ); - } - - /** - * Test to make sure environment response is correct. - * - * @since 3.0.0 - */ - public function test_get_system_status_info_environment() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) ); - $data = $response->get_data(); - $environment = (array) $data['environment']; - - // Make sure all expected data is present. - $this->assertEquals( 32, count( $environment ) ); - - // Test some responses to make sure they match up. - $this->assertEquals( get_option( 'home' ), $environment['home_url'] ); - $this->assertEquals( get_option( 'siteurl' ), $environment['site_url'] ); - $this->assertEquals( WC()->version, $environment['version'] ); - } - - /** - * Test to make sure database response is correct. - * - * @since 3.0.0 - */ - public function test_get_system_status_info_database() { - global $wpdb; - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) ); - $data = $response->get_data(); - $database = (array) $data['database']; - - $this->assertEquals( get_option( 'woocommerce_db_version' ), $database['wc_database_version'] ); - $this->assertEquals( $wpdb->prefix, $database['database_prefix'] ); - $this->assertEquals( WC_Geolocation::get_local_database_path(), $database['maxmind_geoip_database'] ); - $this->assertArrayHasKey( 'woocommerce', $database['database_tables'], print_r( $database, true ) ); - $this->assertArrayHasKey( $wpdb->prefix . 'woocommerce_payment_tokens', $database['database_tables']['woocommerce'], print_r( $database, true ) ); - } - - /** - * Test to make sure active plugins response is correct. - * - * @since 3.0.0 - */ - public function test_get_system_status_info_active_plugins() { - wp_set_current_user( $this->user ); - - $actual_plugins = array( 'hello.php' ); - update_option( 'active_plugins', $actual_plugins ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) ); - update_option( 'active_plugins', array() ); - - $data = $response->get_data(); - $plugins = (array) $data['active_plugins']; - - $this->assertEquals( 1, count( $plugins ) ); - $this->assertEquals( 'Hello Dolly', $plugins[0]['name'] ); - } - - /** - * Test to make sure theme response is correct. - * - * @since 3.0.0 - */ - public function test_get_system_status_info_theme() { - wp_set_current_user( $this->user ); - $active_theme = wp_get_theme(); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) ); - $data = $response->get_data(); - $theme = (array) $data['theme']; - - $this->assertEquals( 13, count( $theme ) ); - $this->assertEquals( $active_theme->Name, $theme['name'] ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar - } - - /** - * Test to make sure settings response is correct. - * - * @since 3.0.0 - */ - public function test_get_system_status_info_settings() { - wp_set_current_user( $this->user ); - - $term_response = array(); - $terms = get_terms( 'product_type', array( 'hide_empty' => 0 ) ); - foreach ( $terms as $term ) { - $term_response[ $term->slug ] = strtolower( $term->name ); - } - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) ); - $data = $response->get_data(); - $settings = (array) $data['settings']; - - $this->assertEquals( 12, count( $settings ) ); - $this->assertEquals( ( 'yes' === get_option( 'woocommerce_api_enabled' ) ), $settings['api_enabled'] ); - $this->assertEquals( get_woocommerce_currency(), $settings['currency'] ); - $this->assertEquals( $term_response, $settings['taxonomies'] ); - } - - /** - * Test to make sure security response is correct. - * - * @since 3.0.0 - */ - public function test_get_system_status_info_security() { - wp_set_current_user( $this->user ); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) ); - $data = $response->get_data(); - $settings = (array) $data['security']; - - $this->assertEquals( 2, count( $settings ) ); - $this->assertEquals( 'https' === substr( wc_get_page_permalink( 'shop' ), 0, 5 ), $settings['secure_connection'] ); - $this->assertEquals( ! ( defined( 'WP_DEBUG' ) && defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG && WP_DEBUG_DISPLAY ) || 0 === intval( ini_get( 'display_errors' ) ), $settings['hide_errors'] ); - } - - /** - * Test to make sure pages response is correct. - * - * @since 3.0.0 - */ - public function test_get_system_status_info_pages() { - wp_set_current_user( $this->user ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) ); - $data = $response->get_data(); - $pages = $data['pages']; - $this->assertEquals( 5, count( $pages ) ); - } - - /** - * Test system status schema. - * - * @since 3.0.0 - */ - public function test_system_status_schema() { - $request = new WP_REST_Request( 'OPTIONS', '/wc/v2/system_status' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - $this->assertEquals( 9, count( $properties ) ); - $this->assertArrayHasKey( 'environment', $properties ); - $this->assertArrayHasKey( 'database', $properties ); - $this->assertArrayHasKey( 'active_plugins', $properties ); - $this->assertArrayHasKey( 'theme', $properties ); - $this->assertArrayHasKey( 'settings', $properties ); - $this->assertArrayHasKey( 'security', $properties ); - $this->assertArrayHasKey( 'pages', $properties ); - } - - /** - * Test to make sure get_items (all tools) response is correct. - * - * @since 3.0.0 - */ - public function test_get_system_tools() { - wp_set_current_user( $this->user ); - - $tools_controller = new WC_REST_System_Status_Tools_Controller(); - $raw_tools = $tools_controller->get_tools(); - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools' ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( count( $raw_tools ), count( $data ) ); - $this->assertContains( - array( - 'id' => 'regenerate_thumbnails', - 'name' => 'Regenerate shop thumbnails', - 'action' => 'Regenerate', - 'description' => 'This will regenerate all shop thumbnails to match your theme and/or image settings.', - '_links' => array( - 'item' => array( - array( - 'href' => rest_url( '/wc/v2/system_status/tools/regenerate_thumbnails' ), - 'embeddable' => true, - ), - ), - ), - ), - $data - ); - } - - /** - * Test to make sure system status tools cannot be accessed without valid creds - * - * @since 3.0.0 - */ - public function test_get_system_status_tools_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test to make sure we can load a single tool correctly. - * - * @since 3.0.0 - */ - public function test_get_system_tool() { - wp_set_current_user( $this->user ); - - $tools_controller = new WC_REST_System_Status_Tools_Controller(); - $raw_tools = $tools_controller->get_tools(); - $raw_tool = $raw_tools['recount_terms']; - - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools/recount_terms' ) ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - - $this->assertEquals( 'recount_terms', $data['id'] ); - $this->assertEquals( 'Term counts', $data['name'] ); - $this->assertEquals( 'Recount terms', $data['action'] ); - $this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] ); - } - - /** - * Test to make sure a single system status toolscannot be accessed without valid creds. - * - * @since 3.0.0 - */ - public function test_get_system_status_tool_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools/recount_terms' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test to make sure we can RUN a tool correctly. - * - * @since 3.0.0 - */ - public function test_execute_system_tool() { - wp_set_current_user( $this->user ); - - $tools_controller = new WC_REST_System_Status_Tools_Controller(); - $raw_tools = $tools_controller->get_tools(); - $raw_tool = $raw_tools['recount_terms']; - - $response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/recount_terms' ) ); - $data = $response->get_data(); - - $this->assertEquals( 'recount_terms', $data['id'] ); - $this->assertEquals( 'Term counts', $data['name'] ); - $this->assertEquals( 'Recount terms', $data['action'] ); - $this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] ); - $this->assertTrue( $data['success'] ); - $this->assertEquals( 1, did_action( 'woocommerce_rest_insert_system_status_tool' ) ); - - $response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/not_a_real_tool' ) ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test to make sure a tool cannot be run without valid creds. - * - * @since 3.0.0 - */ - public function test_execute_system_status_tool_without_permission() { - wp_set_current_user( 0 ); - $response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/recount_terms' ) ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test system status schema. - * - * @since 3.0.0 - */ - public function test_system_status_tool_schema() { - $request = new WP_REST_Request( 'OPTIONS', '/wc/v2/system_status/tools' ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 6, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'name', $properties ); - $this->assertArrayHasKey( 'action', $properties ); - $this->assertArrayHasKey( 'description', $properties ); - $this->assertArrayHasKey( 'success', $properties ); - $this->assertArrayHasKey( 'message', $properties ); - } -} diff --git a/tests/unit-tests/api/wc-blocks/products-attributes-terms.php b/tests/unit-tests/api/wc-blocks/products-attributes-terms.php deleted file mode 100644 index 8a85261c08a..00000000000 --- a/tests/unit-tests/api/wc-blocks/products-attributes-terms.php +++ /dev/null @@ -1,105 +0,0 @@ -user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - - $this->contributor = $this->factory->user->create( - array( - 'role' => 'contributor', - ) - ); - - // Create 2 product attributes with terms. - $this->attr_color = WC_Helper_Product::create_attribute( 'color', array( 'red', 'yellow', 'blue' ) ); - $this->attr_size = WC_Helper_Product::create_attribute( 'size', array( 'small', 'medium', 'large', 'xlarge' ) ); - } - - /** - * Test getting attribute terms. - * - * @since 3.6.0 - */ - public function test_get_terms() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/' . $this->attr_color['attribute_id'] . '/terms' ); - - $response = $this->server->dispatch( $request ); - $response_terms = $response->get_data(); - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 3, count( $response_terms ) ); - $term = $response_terms[0]; - $this->assertArrayHasKey( 'attribute', $term ); - $attribute = $term['attribute']; - $this->assertArrayHasKey( 'id', $attribute ); - $this->assertArrayHasKey( 'name', $attribute ); - $this->assertArrayHasKey( 'slug', $attribute ); - } - - /** - * Test getting invalid attribute terms. - * - * @since 3.6.0 - */ - public function test_get_invalid_attribute_terms() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/99999/terms' ); - - $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test un-authorized getting attribute terms. - * - * @since 3.6.0 - */ - public function test_get_unauthed_attribute_terms() { - $request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/' . $this->attr_size['attribute_id'] . '/terms' ); - - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test getting attribute terms as contributor. - * - * @since 3.6.0 - */ - public function test_get_attribute_terms_contributor() { - wp_set_current_user( $this->contributor ); - $request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/' . $this->attr_size['attribute_id'] . '/terms' ); - - $response = $this->server->dispatch( $request ); - $response_terms = $response->get_data(); - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 4, count( $response_terms ) ); - } -} diff --git a/tests/unit-tests/api/wc-blocks/products-attributes.php b/tests/unit-tests/api/wc-blocks/products-attributes.php deleted file mode 100644 index 8a03a7b02e7..00000000000 --- a/tests/unit-tests/api/wc-blocks/products-attributes.php +++ /dev/null @@ -1,105 +0,0 @@ -user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - - $this->contributor = $this->factory->user->create( - array( - 'role' => 'contributor', - ) - ); - - // Create 2 product attributes with terms. - $this->attr_color = WC_Helper_Product::create_attribute( 'color', array( 'red', 'yellow', 'blue' ) ); - $this->attr_size = WC_Helper_Product::create_attribute( 'size', array( 'small', 'medium', 'large', 'xlarge' ) ); - } - - /** - * Test getting attributes. - * - * @since 3.6.0 - */ - public function test_get_attributes() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes' ); - - $response = $this->server->dispatch( $request ); - $response_attributes = $response->get_data(); - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 2, count( $response_attributes ) ); - $attribute = $response_attributes[0]; - $this->assertArrayHasKey( 'id', $attribute ); - $this->assertArrayHasKey( 'name', $attribute ); - $this->assertArrayHasKey( 'slug', $attribute ); - $this->assertArrayHasKey( 'count', $attribute ); - } - - /** - * Test getting invalid attribute. - * - * @since 3.6.0 - */ - public function test_get_invalid_attribute() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/11111' ); - - $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test un-authorized getting attribute. - * - * @since 3.6.0 - */ - public function test_get_unauthed_attribute() { - $request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/' . $this->attr_size['attribute_id'] ); - - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test getting attribute as contributor. - * - * @since 3.6.0 - */ - public function test_get_attribute_contributor() { - wp_set_current_user( $this->contributor ); - $request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/' . $this->attr_size['attribute_id'] ); - - $response = $this->server->dispatch( $request ); - $attribute = $response->get_data(); - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( $this->attr_size['attribute_id'], $attribute['id'] ); - $this->assertEquals( $this->attr_size['attribute_name'], $attribute['name'] ); - } -} diff --git a/tests/unit-tests/api/wc-blocks/products-categories.php b/tests/unit-tests/api/wc-blocks/products-categories.php deleted file mode 100644 index 455f6857bcd..00000000000 --- a/tests/unit-tests/api/wc-blocks/products-categories.php +++ /dev/null @@ -1,121 +0,0 @@ -user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - - $this->contributor = $this->factory->user->create( - array( - 'role' => 'contributor', - ) - ); - - // Create 3 product categories. - $parent = wp_insert_term( 'Parent Category', 'product_cat' ); - $child = wp_insert_term( - 'Child Category', - 'product_cat', - array( 'parent' => $parent['term_id'] ) - ); - $single = wp_insert_term( 'Standalone Category', 'product_cat' ); - $this->categories = array( - 'parent' => $parent, - 'child' => $child, - 'single' => $single, - ); - - // Create two products for the parent category. - $this->products = array(); - $this->products[0] = WC_Helper_Product::create_simple_product( false ); - $this->products[0]->set_category_ids( array( $parent['term_id'] ) ); - $this->products[0]->save(); - - $this->products[3] = WC_Helper_Product::create_simple_product( false ); - $this->products[3]->set_category_ids( array( $parent['term_id'], $single['term_id'] ) ); - $this->products[3]->save(); - } - - /** - * Test getting product categories. - * - * @since 3.6.0 - */ - public function test_get_product_categories() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'GET', $this->endpoint . '/products/categories' ); - - $response = $this->server->dispatch( $request ); - $categories = $response->get_data(); - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 4, count( $categories ) ); // Three created and `uncategorized`. - } - - /** - * Test getting invalid product category. - * - * @since 3.6.0 - */ - public function test_get_invalid_product_category() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'GET', $this->endpoint . '/products/categories/007' ); - - $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * Test un-authorized getting product category. - * - * @since 3.6.0 - */ - public function test_get_unauthed_product_category() { - $request = new WP_REST_Request( 'GET', $this->endpoint . '/products/categories/' . $this->categories['parent']['term_id'] ); - - $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); - } - - /** - * Test getting category as contributor. - * - * @since 3.6.0 - */ - public function test_get_attribute_terms_contributor() { - wp_set_current_user( $this->contributor ); - $request = new WP_REST_Request( 'GET', $this->endpoint . '/products/categories/' . $this->categories['parent']['term_id'] ); - - $response = $this->server->dispatch( $request ); - $category = $response->get_data(); - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( $category['name'], 'Parent Category' ); - $this->assertEquals( $category['parent'], 0 ); - $this->assertEquals( $category['count'], 2 ); - } -} diff --git a/tests/unit-tests/api/wc-blocks/products.php b/tests/unit-tests/api/wc-blocks/products.php deleted file mode 100644 index 1ce12330e1f..00000000000 --- a/tests/unit-tests/api/wc-blocks/products.php +++ /dev/null @@ -1,305 +0,0 @@ -user = $this->factory->user->create( - array( - 'role' => 'author', - ) - ); - $this->contributor = $this->factory->user->create( - array( - 'role' => 'contributor', - ) - ); - $this->subscriber = $this->factory->user->create( - array( - 'role' => 'subscriber', - ) - ); - - // Create 3 product categories. - $parent = wp_insert_term( 'Parent Category', 'product_cat' ); - $child = wp_insert_term( - 'Child Category', - 'product_cat', - array( 'parent' => $parent['term_id'] ) - ); - $single = wp_insert_term( 'Standalone Category', 'product_cat' ); - $this->categories = array( - 'parent' => $parent, - 'child' => $child, - 'single' => $single, - ); - - // Create two products, one with 'parent', and one with 'single'. - $this->products = array(); - $this->products[0] = WC_Helper_Product::create_simple_product( false ); - $this->products[0]->set_category_ids( array( $parent['term_id'] ) ); - $this->products[0]->save(); - - $this->products[1] = WC_Helper_Product::create_simple_product( false ); - $this->products[1]->set_category_ids( array( $single['term_id'] ) ); - $this->products[1]->save(); - - $this->products[2] = WC_Helper_Product::create_simple_product( false ); - $this->products[2]->set_category_ids( array( $child['term_id'], $single['term_id'] ) ); - $this->products[2]->save(); - - $this->products[3] = WC_Helper_Product::create_simple_product( false ); - $this->products[3]->set_category_ids( array( $parent['term_id'], $single['term_id'] ) ); - $this->products[3]->save(); - } - - /** - * Test route registration. - * - * @since 3.6.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - - $this->assertArrayHasKey( '/wc-blocks/v1/products', $routes ); - $this->assertArrayHasKey( '/wc-blocks/v1/products/(?P[\d]+)', $routes ); - } - - /** - * Test getting products. - * - * @since 3.6.0 - */ - public function test_get_products() { - wp_set_current_user( $this->user ); - WC_Helper_Product::create_external_product(); - sleep( 1 ); // So both products have different timestamps. - $product = WC_Helper_Product::create_simple_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ) ); - $products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - - $this->assertEquals( 6, count( $products ) ); - } - - /** - * Test getting products as an contributor. - * - * @since 3.6.0 - */ - public function test_get_products_as_contributor() { - wp_set_current_user( $this->contributor ); - WC_Helper_Product::create_simple_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ) ); - $this->assertEquals( 200, $response->get_status() ); - } - - /** - * Test getting products as an subscriber. - * - * @since 3.6.0 - */ - public function test_get_products_as_subscriber() { - wp_set_current_user( $this->subscriber ); - WC_Helper_Product::create_simple_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ) ); - $this->assertEquals( 403, $response->get_status() ); - } - - /** - * Test getting products with custom ordering. - * - * @since 3.6.0 - */ - public function test_get_products_order_by_price() { - wp_set_current_user( $this->user ); - WC_Helper_Product::create_external_product(); - sleep( 1 ); // So both products have different timestamps. - $product = WC_Helper_Product::create_simple_product( false ); // Prevent saving, since we save here. - // Customize the price, otherwise both are 10. - $product->set_props( - array( - 'regular_price' => 15, - 'price' => 15, - ) - ); - $product->save(); - - $request = new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ); - $request->set_param( 'orderby', 'price' ); - $request->set_param( 'order', 'asc' ); - $response = $this->server->dispatch( $request ); - $products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 6, count( $products ) ); - - $this->assertEquals( 'Dummy Product', $products[1]['name'] ); - $this->assertEquals( '10', $products[1]['price'] ); - } - - /** - * Test product_visibility queries. - * - * @since 3.6.0 - */ - public function test_product_visibility() { - wp_set_current_user( $this->user ); - $visible_product = WC_Helper_Product::create_simple_product(); - $visible_product->set_name( 'Visible Product' ); - $visible_product->set_catalog_visibility( 'visible' ); - $visible_product->save(); - - $catalog_product = WC_Helper_Product::create_simple_product(); - $catalog_product->set_name( 'Catalog Product' ); - $catalog_product->set_catalog_visibility( 'catalog' ); - $catalog_product->save(); - - $search_product = WC_Helper_Product::create_simple_product(); - $search_product->set_name( 'Search Product' ); - $search_product->set_catalog_visibility( 'search' ); - $search_product->save(); - - $hidden_product = WC_Helper_Product::create_simple_product(); - $hidden_product->set_name( 'Hidden Product' ); - $hidden_product->set_catalog_visibility( 'hidden' ); - $hidden_product->save(); - - $query_params = array( - 'catalog_visibility' => 'visible', - ); - $request = new WP_REST_REQUEST( 'GET', '/wc-blocks/v1/products' ); - $request->set_query_params( $query_params ); - $response = $this->server->dispatch( $request ); - $products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 5, count( $products ) ); - $this->assertEquals( 'Visible Product', $products[0]['name'] ); - - $query_params = array( - 'catalog_visibility' => 'catalog', - 'orderby' => 'id', - 'order' => 'asc', - ); - $request = new WP_REST_REQUEST( 'GET', '/wc-blocks/v1/products' ); - $request->set_query_params( $query_params ); - $response = $this->server->dispatch( $request ); - $products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 6, count( $products ) ); - $this->assertEquals( 'Dummy Product', $products[0]['name'] ); - - $query_params = array( - 'catalog_visibility' => 'search', - 'orderby' => 'id', - 'order' => 'asc', - ); - $request = new WP_REST_REQUEST( 'GET', '/wc-blocks/v1/products' ); - $request->set_query_params( $query_params ); - $response = $this->server->dispatch( $request ); - $products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 6, count( $products ) ); - $this->assertEquals( 'Dummy Product', $products[0]['name'] ); - - $query_params = array( - 'catalog_visibility' => 'hidden', - ); - $request = new WP_REST_REQUEST( 'GET', '/wc-blocks/v1/products' ); - $request->set_query_params( $query_params ); - $response = $this->server->dispatch( $request ); - $products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 1, count( $products ) ); - $this->assertEquals( 'Hidden Product', $products[0]['name'] ); - } - - /** - * Test product category intersection: Any product in either Single or Child (3). - * - * @since 3.6.0 - */ - public function test_get_products_in_any_categories_child() { - wp_set_current_user( $this->user ); - - $cats = $this->categories['child']['term_id'] . ',' . $this->categories['single']['term_id']; - - $request = new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ); - $request->set_param( 'category', $cats ); - $request->set_param( 'category_operator', 'in' ); - - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 3, count( $response_products ) ); - } - - /** - * Test product category intersection: Any product in both Single and Child (1). - * - * @since 3.6.0 - */ - public function test_get_products_in_all_categories_child() { - wp_set_current_user( $this->user ); - - $cats = $this->categories['child']['term_id'] . ',' . $this->categories['single']['term_id']; - - $request = new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ); - $request->set_param( 'category', $cats ); - $request->set_param( 'category_operator', 'and' ); - - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 1, count( $response_products ) ); - } - - /** - * Test product category intersection: Any product in both Single and Parent (1). - * - * @since 3.6.0 - */ - public function test_get_products_in_all_categories_parent() { - wp_set_current_user( $this->user ); - - $cats = $this->categories['parent']['term_id'] . ',' . $this->categories['single']['term_id']; - - $request = new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ); - $request->set_param( 'category', $cats ); - $request->set_param( 'category_operator', 'and' ); - - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 1, count( $response_products ) ); - } -} diff --git a/tests/unit-tests/packages/packages.php b/tests/unit-tests/packages/packages.php new file mode 100644 index 00000000000..a4b0a0ce45e --- /dev/null +++ b/tests/unit-tests/packages/packages.php @@ -0,0 +1,58 @@ +markTestSkipped( 'Packages are disabled unless running PHP 5.6+' ); + } + } + + /** + * Test packages exist - this requires composer install to have ran. + */ + public function test_packages_exist() { + $this->assertTrue( \Automattic\WooCommerce\Packages::package_exists( 'woocommerce-blocks' ) ); + $this->assertTrue( \Automattic\WooCommerce\Packages::package_exists( 'woocommerce-rest-api' ) ); + } + + /** + * Test packages autoload correctly. + */ + public function test_autoload_packages() { + $this->assertTrue( class_exists( '\Automattic\WooCommerce\Blocks\Package' ) ); + $this->assertTrue( class_exists( '\Automattic\WooCommerce\RestApi\Package' ) ); + } + + /** + * Check API package returns values. + * + * @return void + */ + public function test_api_package() { + $this->assertNotNull( wc()->api->get_rest_api_package_version() ); + $this->assertNotNull( wc()->api->get_rest_api_package_path() ); + } + + /** + * Test that the REST API package is working by hitting some endpoints. + */ + public function test_api_endpoints_exist() { + $response = wc()->api->get_endpoint_data( '/wc/v3' ); + $this->assertFalse( is_wp_error( $response ) ); + $this->assertEquals( 'wc/v3', $response['namespace'] ); // phpcs:ignore + } +} diff --git a/tests/unit-tests/api/functions.php b/tests/unit-tests/util/api-functions.php similarity index 100% rename from tests/unit-tests/api/functions.php rename to tests/unit-tests/util/api-functions.php diff --git a/woocommerce.php b/woocommerce.php index b4335390c8c..86bfe5faf38 100644 --- a/woocommerce.php +++ b/woocommerce.php @@ -19,47 +19,26 @@ if ( ! defined( 'WC_PLUGIN_FILE' ) ) { } /** - * Load all the packages. + * Load core packages and the autoloader. * - * We want to fail gracefully if `composer install` has not been executed yet, so we are checking for the autoloader. - * If the autoloader is not present, let's log the failure and display a nice admin notice. + * The new packages and autoloader require PHP 5.6+. If this dependency is not met, do not include them. Users will be warned + * that they are using an older version of PHP. WooCommerce will continue to load, but some functionality such as the REST API + * and Blocks will be missing. + * + * This requirement will be enforced in future versions of WooCommerce. */ -$autoloader = dirname( __FILE__ ) . '/vendor/autoload_packages.php'; -if ( is_readable( $autoloader ) ) { - require $autoloader; -} else { - if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { - error_log( // phpcs:ignore - esc_html__( 'Your installation of WooCommerce is incomplete. If you installed WooCommerce from GitHub, please refer to this document to set up your development environment: https://github.com/woocommerce/woocommerce/wiki/How-to-set-up-WooCommerce-development-environment', 'woocommerce' ) - ); +if ( version_compare( PHP_VERSION, '5.6.0', '>=' ) ) { + require __DIR__ . '/src/Autoloader.php'; + require __DIR__ . '/src/Packages.php'; + + if ( ! \Automattic\WooCommerce\Autoloader::init() ) { + return; } - /** - * Outputs an admin notice for folks running WooCommerce without having run composer install. - */ - add_action( - 'admin_notices', - function() { - ?> -
-

- ', - '' - ); - ?> -

-
-