woocommerce/plugins/woocommerce-blocks/tests/php/StoreApi/Routes/CartItems.php

297 lines
8.1 KiB
PHP
Raw Normal View History

<?php
/**
* Controller Tests.
*/
namespace Automattic\WooCommerce\Blocks\Tests\StoreApi\Routes;
use Automattic\WooCommerce\Blocks\Tests\StoreApi\Routes\ControllerTestCase;
use Automattic\WooCommerce\Blocks\Tests\Helpers\FixtureData;
use Automattic\WooCommerce\Blocks\Tests\Helpers\ValidateSchema;
/**
* Cart Controller Tests.
*/
class CartItems extends ControllerTestCase {
/**
Add PHP8 Unit Testing (https://github.com/woocommerce/woocommerce-blocks/pull/7528) * fixed method sig * Updated to @wordpress/env@5.5.0 and set default PHP 7.4 for wp-env. * updated Coding Standards flow to use PHP 8.0 * Added comment to E2E flows explaining what PHP version is used * Revert "Updated to @wordpress/env@5.5.0 and set default PHP 7.4 for wp-env." This reverts commit 696cd7f42edc9d9726b777cf4f83a501a6d63936. * Added comment to Unit test flows explaining what PHP version is used. Specified PHP version on .wp-env.json * Fixed composer-lock.json version. * Updated tests to run on PHP Unit 9.2.6 * Updated tests to run on PHP 8 * Reverted test, mismatched results between local and pipeline * Removed Todo * Updated platform overrides * Update Migrationb tests with Mockery for PHP8 compat * try at PHP unit flow matrix * Fix blocks.ini invalid config * Temp disable E2E * Downgraded woocommerce/woocommerce-sniffs as it introduced new sniffs we should be handling on a different PR * re-enable E2E tests * blocks.ini fix * revert blocks.ini fix * Update @wordpress/env * remove .htaccess mapping * Fix permissions for tests * Debug permissions * Attempt at perm fix * Attempt at perm fix * Downgraded @wordpress/env * Another attempt at upgrade @wordpress/env * Attempt at cleaning wp-env before run * Attempt at destroying wp-env before run. Disabled E2E. * Attempt at destroying wp-env before run. * debug wp-env data * attempt at deleting wp-env data (destroy won't work due to prompt) * re-enable E2E * Fix deprecation warnings * Cleaned wp-env data for E2E * Fix perms for E2E * Updated RateLimitsTests * debug * Force 7.4 for wp-env * Run sh outside of npm * Reverted E2E flow * reverted wp-env-config.sh debug test * reverted .wp-env.json phpVersion force * Update tests/php/StoreApi/Utilities/ProductQueryFilters.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/CartExtensions.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/CartItems.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/Products.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/ProductCollectionData.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/Batch.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/Checkout.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/CartCoupons.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/ProductAttributes.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/Cart.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * downgraded @wordpress/env to v4 * Reverted back to reflection class for pivate attribs manipulation on tests * reverted JS unit testing job name * Update tests/php/StoreApi/Formatters/TestMoneyFormatter.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Typo fix Co-authored-by: Mike Jolley <mike.jolley@me.com>
2022-11-09 15:28:08 +00:00
* Setup test product data. Called before every test.
*/
Add PHP8 Unit Testing (https://github.com/woocommerce/woocommerce-blocks/pull/7528) * fixed method sig * Updated to @wordpress/env@5.5.0 and set default PHP 7.4 for wp-env. * updated Coding Standards flow to use PHP 8.0 * Added comment to E2E flows explaining what PHP version is used * Revert "Updated to @wordpress/env@5.5.0 and set default PHP 7.4 for wp-env." This reverts commit 696cd7f42edc9d9726b777cf4f83a501a6d63936. * Added comment to Unit test flows explaining what PHP version is used. Specified PHP version on .wp-env.json * Fixed composer-lock.json version. * Updated tests to run on PHP Unit 9.2.6 * Updated tests to run on PHP 8 * Reverted test, mismatched results between local and pipeline * Removed Todo * Updated platform overrides * Update Migrationb tests with Mockery for PHP8 compat * try at PHP unit flow matrix * Fix blocks.ini invalid config * Temp disable E2E * Downgraded woocommerce/woocommerce-sniffs as it introduced new sniffs we should be handling on a different PR * re-enable E2E tests * blocks.ini fix * revert blocks.ini fix * Update @wordpress/env * remove .htaccess mapping * Fix permissions for tests * Debug permissions * Attempt at perm fix * Attempt at perm fix * Downgraded @wordpress/env * Another attempt at upgrade @wordpress/env * Attempt at cleaning wp-env before run * Attempt at destroying wp-env before run. Disabled E2E. * Attempt at destroying wp-env before run. * debug wp-env data * attempt at deleting wp-env data (destroy won't work due to prompt) * re-enable E2E * Fix deprecation warnings * Cleaned wp-env data for E2E * Fix perms for E2E * Updated RateLimitsTests * debug * Force 7.4 for wp-env * Run sh outside of npm * Reverted E2E flow * reverted wp-env-config.sh debug test * reverted .wp-env.json phpVersion force * Update tests/php/StoreApi/Utilities/ProductQueryFilters.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/CartExtensions.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/CartItems.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/Products.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/ProductCollectionData.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/Batch.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/Checkout.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/CartCoupons.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/ProductAttributes.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/Cart.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * downgraded @wordpress/env to v4 * Reverted back to reflection class for pivate attribs manipulation on tests * reverted JS unit testing job name * Update tests/php/StoreApi/Formatters/TestMoneyFormatter.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Typo fix Co-authored-by: Mike Jolley <mike.jolley@me.com>
2022-11-09 15:28:08 +00:00
protected function setUp(): void {
parent::setUp();
$fixtures = new FixtureData();
$this->products = array(
$fixtures->get_simple_product(
array(
'name' => 'Test Product 1',
'stock_status' => 'instock',
'regular_price' => 10,
'weight' => 10,
'image_id' => $fixtures->sideload_image(),
)
),
);
$variable_product = $fixtures->get_variable_product(
array(
'name' => 'Test Product 2',
'stock_status' => 'instock',
'regular_price' => 10,
'weight' => 10,
'image_id' => $fixtures->sideload_image(),
),
array(
$fixtures->get_product_attribute( 'color', array( 'red', 'green', 'blue' ) ),
$fixtures->get_product_attribute( 'size', array( 'small', 'medium', 'large' ) ),
)
);
$variation = $fixtures->get_variation_product(
$variable_product->get_id(),
array(
'pa_color' => 'red',
'pa_size' => 'small',
)
);
$this->products[] = $variable_product;
wc_empty_cart();
$this->keys = array();
$this->keys[] = wc()->cart->add_to_cart( $this->products[0]->get_id(), 2 );
$this->keys[] = wc()->cart->add_to_cart(
$this->products[1]->get_id(),
1,
$variation->get_id(),
array(
'attribute_pa_color' => 'red',
'attribute_pa_size' => 'small',
)
);
}
/**
* Test getting cart.
*/
public function test_get_items() {
$this->assertAPIResponse(
'/wc/store/v1/cart/items',
200,
array(
0 => array(
'key' => $this->keys[0],
'id' => $this->products[0]->get_id(),
'name' => $this->products[0]->get_name(),
'sku' => $this->products[0]->get_sku(),
'permalink' => $this->products[0]->get_permalink(),
'quantity' => 2,
'totals' => array(
'line_subtotal' => '2000',
'line_total' => '2000',
),
),
1 => array(
'key' => $this->keys[1],
'quantity' => 1,
'totals' => array(
'line_subtotal' => '1000',
'line_total' => '1000',
),
),
)
);
}
/**
* Test getting cart item by key.
*/
public function test_get_item() {
$this->assertAPIResponse(
'/wc/store/v1/cart/items/' . $this->keys[0],
200,
array(
'key' => $this->keys[0],
'id' => $this->products[0]->get_id(),
'name' => $this->products[0]->get_name(),
'sku' => $this->products[0]->get_sku(),
'permalink' => $this->products[0]->get_permalink(),
'quantity' => 2,
'totals' => array(
'line_subtotal' => '2000',
'line_total' => '2000',
),
)
);
}
/**
* Test add to cart.
*/
public function test_create_item() {
wc_empty_cart();
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/items' );
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
$request->set_body_params(
array(
'id' => $this->products[0]->get_id(),
'quantity' => '10',
)
);
$this->assertAPIResponse(
$request,
201,
array(
'id' => $this->products[0]->get_id(),
'quantity' => 10,
)
);
$this->assertAPIResponse(
$request,
201,
array(
'id' => $this->products[0]->get_id(),
'quantity' => 20,
)
);
}
/**
* Test add to cart does not allow invalid items.
*/
public function test_invalid_create_item() {
wc_empty_cart();
$fixtures = new FixtureData();
$invalid_product = $fixtures->get_simple_product(
array(
'name' => 'Invalid Product',
'regular_price' => '',
)
);
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/items' );
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
$request->set_body_params(
array(
'id' => $invalid_product->get_id(),
'quantity' => '10',
)
);
$this->assertAPIResponse(
$request,
400
);
}
/**
* Test updating an item.
*/
public function test_update_item() {
$request = new \WP_REST_Request( 'PUT', '/wc/store/v1/cart/items/' . $this->keys[0] );
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
$request->set_body_params(
array(
'quantity' => '10',
)
);
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 10, $data['quantity'] );
}
/**
* Test delete item.
*/
public function test_delete_item() {
$request = new \WP_REST_Request( 'DELETE', '/wc/store/v1/cart/items/' . $this->keys[0] );
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
$this->assertAPIResponse(
$request,
204,
array()
);
$request = new \WP_REST_Request( 'DELETE', '/wc/store/v1/cart/items/' . $this->keys[0] );
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
$this->assertAPIResponse(
$request,
404
);
}
/**
* Test delete all items.
*/
public function test_delete_items() {
$request = new \WP_REST_Request( 'DELETE', '/wc/store/v1/cart/items' );
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( array(), $data );
$response = rest_get_server()->dispatch( new \WP_REST_Request( 'GET', '/wc/store/v1/cart/items' ) );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 0, count( $data ) );
}
/**
* Test conversion of cart item to rest response.
*/
public function test_prepare_item() {
$routes = new \Automattic\WooCommerce\StoreApi\RoutesController( new \Automattic\WooCommerce\StoreApi\SchemaController( $this->mock_extend ) );
$controller = $routes->get( 'cart-items', 'v1' );
$cart = wc()->cart->get_cart();
$response = $controller->prepare_item_for_response( current( $cart ), new \WP_REST_Request() );
$data = $response->get_data();
$this->assertArrayHasKey( 'key', $data );
$this->assertArrayHasKey( 'id', $data );
$this->assertArrayHasKey( 'quantity', $data );
$this->assertArrayHasKey( 'name', $data );
$this->assertArrayHasKey( 'sku', $data );
$this->assertArrayHasKey( 'permalink', $data );
$this->assertArrayHasKey( 'images', $data );
$this->assertArrayHasKey( 'totals', $data );
$this->assertArrayHasKey( 'variation', $data );
$this->assertArrayHasKey( 'item_data', $data );
$this->assertArrayHasKey( 'low_stock_remaining', $data );
$this->assertArrayHasKey( 'backorders_allowed', $data );
$this->assertArrayHasKey( 'show_backorder_badge', $data );
$this->assertArrayHasKey( 'short_description', $data );
Stop hidden products from being linked in cart and checkout blocks (https://github.com/woocommerce/woocommerce-blocks/pull/3415) * Add catalog_visibility to CartItemSchema.php This is used to get whether the product is visible in the catalogue, visible in the shop only, visible in search results only, or visible everywhere. We need to know this so we can pass it to the ProductImage and ProductName components. * Remove links from CartLineItemRow if not visible in catalogue Added catalog_visibility to lineItems prop, and when the product is not visible in the catalogue do not wrap product image in a link. Also pass down the hasLink prop to ProductName. * Add hasLink prop to ProductName When this prop is false we should not output the link around the product name. This is for when the product is hidden from the catalogue but we still want to show its name somewhere. * Add tests and storybook for ProductName * Add catalog_visibility check to OrderSummaryItem When the catalogue visibility of a product is set to hidden or search, then the product name in the checkout sidebar should not be hyperlinked. * Reverse logic for hiding link on product image & disabling link on name Following a point from @budzanowski we do not need the hasLink prop, making use of disabled is probably a better idea. * Remove tabindex from a in ProductName & output span if name is disabled This change removes the need to pass a tabindex to the a in ProductName. This is because a disabled ProductName will now never output an a tag. When the ProductName is disabled a span is output instead, which has no tabindex by default. This change also reverses the logic to decide whether the a or span should be output so as to make the code more readable and flow better. * Update storybook and tests/snapshots for ProductName
2020-11-23 13:03:08 +00:00
$this->assertArrayHasKey( 'catalog_visibility', $data );
}
/**
* Test schema matches responses.
*
* Tests schema of both products in cart to cover as much schema as possible.
*/
public function test_get_item_schema() {
$routes = new \Automattic\WooCommerce\StoreApi\RoutesController( new \Automattic\WooCommerce\StoreApi\SchemaController( $this->mock_extend ) );
$controller = $routes->get( 'cart-items', 'v1' );
$schema = $controller->get_item_schema();
$cart = wc()->cart->get_cart();
$validate = new ValidateSchema( $schema );
// Simple product.
$response = $controller->prepare_item_for_response( current( $cart ), new \WP_REST_Request() );
$diff = $validate->get_diff_from_object( $response->get_data() );
$this->assertEmpty( $diff, print_r( $diff, true ) );
// Variable product.
$response = $controller->prepare_item_for_response( end( $cart ), new \WP_REST_Request() );
$diff = $validate->get_diff_from_object( $response->get_data() );
$this->assertEmpty( $diff, print_r( $diff, true ) );
}
}