diff --git a/src/class-actions.php b/src/class-actions.php index 0b72aa23..3f79e961 100644 --- a/src/class-actions.php +++ b/src/class-actions.php @@ -61,7 +61,7 @@ use WPGraphQL\Extensions\WooCommerce\Mutation\Customer_Update; use WPGraphQL\Extensions\WooCommerce\Mutation\Cart_Add_Item; use WPGraphQL\Extensions\WooCommerce\Mutation\Cart_Update_Item_Quantity; use WPGraphQL\Extensions\WooCommerce\Mutation\Cart_Remove_Items; -use WPGraphQL\Extensions\WooCommerce\Mutation\Cart_Restore_Item; +use WPGraphQL\Extensions\WooCommerce\Mutation\Cart_Restore_Items; use WPGraphQL\Extensions\WooCommerce\Mutation\Cart_Empty; use WPGraphQL\Extensions\WooCommerce\Mutation\Cart_Apply_Coupon; use WPGraphQL\Extensions\WooCommerce\Mutation\Cart_Remove_Coupon; @@ -148,7 +148,7 @@ class Actions { Cart_Add_Item::register_mutation(); Cart_Update_Item_Quantity::register_mutation(); Cart_Remove_Items::register_mutation(); - Cart_Restore_Item::register_mutation(); + Cart_Restore_Items::register_mutation(); Cart_Empty::register_mutation(); Cart_Apply_Coupon::register_mutation(); Cart_Remove_Coupon::register_mutation(); diff --git a/src/mutation/class-cart-restore-item.php b/src/mutation/class-cart-restore-items.php similarity index 55% rename from src/mutation/class-cart-restore-item.php rename to src/mutation/class-cart-restore-items.php index a798e927..4006f7e7 100644 --- a/src/mutation/class-cart-restore-item.php +++ b/src/mutation/class-cart-restore-items.php @@ -1,8 +1,8 @@ self::get_input_fields(), 'outputFields' => self::get_output_fields(), @@ -39,8 +40,8 @@ class Cart_Restore_Item { */ public static function get_input_fields() { $input_fields = array( - 'key' => array( - 'type' => array( 'non_null' => 'ID' ), + 'keys' => array( + 'type' => array( 'list_of' => 'ID' ), 'description' => __( 'Cart item key of the item being removed', 'wp-graphql-woocommerce' ), ), ); @@ -54,14 +55,7 @@ class Cart_Restore_Item { * @return array */ public static function get_output_fields() { - return array( - 'cartItem' => array( - 'type' => 'CartItem', - 'resolve' => function ( $payload ) { - return WC()->cart->get_cart_item( $payload['id'] ); - }, - ), - ); + return Cart_Remove_Items::get_output_fields(); } /** @@ -71,23 +65,22 @@ class Cart_Restore_Item { */ public static function mutate_and_get_payload() { return function( $input, AppContext $context, ResolveInfo $info ) { - // Retrieve product database ID if relay ID provided. - if ( empty( $input['key'] ) ) { - throw new UserError( __( 'No cart item key provided', 'wp-graphql-woocommerce' ) ); + if ( empty( $input['keys'] ) ) { + throw new UserError( __( 'No cart item keys provided', 'wp-graphql-woocommerce' ) ); } - // Get WC_Cart instance. - $cart = WC()->cart; - - // Restore cart item. - $success = $cart->restore_cart_item( $input['key'] ); - if ( false === $success ) { - /* translators: Cart item not found message */ - throw new UserError( sprintf( __( 'Failed to restore cart item with the key: %s', 'wp-graphql-woocommerce' ), $input['key'] ) ); + // Restore cart items. + foreach ( $input['keys'] as $key ) { + $success = \WC()->cart->restore_cart_item( $key ); + if ( false === $success ) { + /* translators: Cart item not found message */ + throw new UserError( sprintf( __( 'Failed to restore cart item with the key: %s', 'wp-graphql-woocommerce' ), $key ) ); + } } + $cart_items = Cart_Mutation::retrieve_cart_items( $input, $context, $info, 'restore' ); // Return payload. - return array( 'id' => $input['key'] ); + return array( 'items' => $cart_items ); }; } } diff --git a/tests/wpunit/CartMutationsTest.php b/tests/wpunit/CartMutationsTest.php index 9573d7cf..2f24d935 100644 --- a/tests/wpunit/CartMutationsTest.php +++ b/tests/wpunit/CartMutationsTest.php @@ -91,6 +91,40 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase { return $actual; } + private function restoreItems( $input ) { + $mutation = ' + mutation restoreCartItems( $input: RestoreCartItemsInput! ) { + restoreCartItems( input: $input ) { + clientMutationId + cartItems { + key + product { + id + } + variation { + id + } + quantity + subtotal + subtotalTax + total + tax + } + } + } + '; + + $actual = graphql( + array( + 'query' => $mutation, + 'operation_name' => 'restoreCartItems', + 'variables' => array( 'input' => $input ), + ) + ); + + return $actual; + } + // tests public function testAddToCartMutationWithProduct() { $product_id = $this->product->create_simple(); @@ -411,53 +445,38 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase { $this->assertTrue( \WC()->cart->is_empty() ); } - public function testRestoreCartItemMutation() { - $cart = WC()->cart; - - // Create products. + public function testRestoreCartItemsMutation() { + // Create products $ids = $this->variation->create( $this->product->create_variable() ); - // Create cart item. - $cart_item = $cart->get_cart_item( - $cart->add_to_cart( $ids['product'], 2, $ids['variations'][0] ) - ); - - // Remove cart item. - $cart->remove_cart_item( $cart_item['key'] ); - - $mutation = ' - mutation restoreCartItem( $input: RestoreCartItemInput! ) { - restoreCartItem( input: $input ) { - clientMutationId - cartItem { - key - product { - id - } - variation { - id - } - quantity - subtotal - subtotalTax - total - tax - } - } - } - '; - - $variables = array( - 'input' => array( - 'clientMutationId' => 'someId', - 'key' => $cart_item['key'], - ), - ); - $actual = graphql( + // Add item. + $addToCart = $this->addToCart( array( - 'query' => $mutation, - 'operation_name' => 'restoreCartItem', - 'variables' => $variables, + 'clientMutationId' => 'someId', + 'productId' => $ids['product'], + 'quantity' => 2, + 'variationId' => $ids['variations'][0], + ) + ); + + $this->assertArrayHasKey('data', $addToCart ); + $this->assertArrayHasKey('addToCart', $addToCart['data'] ); + $this->assertArrayHasKey('cartItem', $addToCart['data']['addToCart'] ); + $cartItem = $addToCart['data']['addToCart']['cartItem']; + $key = $cartItem['key']; + + // Remove item. + $this->removeItemsFromCart( + array( + 'clientMutationId' => 'someId', + 'all' => true + ) + ); + + $actual = $this->restoreItems( + array( + 'clientMutationId' => 'someId', + 'keys' => array( $key ), ) ); @@ -466,28 +485,83 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase { $expected = array( 'data' => array( - 'restoreCartItem' => array( + 'restoreCartItems' => array( 'clientMutationId' => 'someId', - 'cartItem' => array( - 'key' => $cart_item['key'], - 'product' => array( - 'id' => $this->product->to_relay_id( $cart_item['product_id'] ), - ), - 'variation' => array( - 'id' => $this->variation->to_relay_id( $cart_item['variation_id'] ), - ), - 'quantity' => $cart_item['quantity'], - 'subtotal' => floatval( $cart_item['line_subtotal'] ), - 'subtotalTax' => floatval( $cart_item['line_subtotal_tax'] ), - 'total' => floatval( $cart_item['line_total'] ), - 'tax' => floatval( $cart_item['line_tax'] ), - ), + 'cartItems' => array( $cartItem ), ), ), ); $this->assertEqualSets( $expected, $actual ); - $this->assertNotEmpty( \WC()->cart->get_cart_item( $cart_item['key'] ) ); + $this->assertNotEmpty( \WC()->cart->get_cart_item( $key ) ); + } + + public function testRestoreCartItemsMutationWithMultipleItems() { + // Create products + $ids = $this->variation->create( $this->product->create_variable() ); + + // Add item 1. + $addToCart = $this->addToCart( + array( + 'clientMutationId' => 'someId', + 'productId' => $ids['product'], + 'quantity' => 2, + 'variationId' => $ids['variations'][0], + ) + ); + + $this->assertArrayHasKey('data', $addToCart ); + $this->assertArrayHasKey('addToCart', $addToCart['data'] ); + $this->assertArrayHasKey('cartItem', $addToCart['data']['addToCart'] ); + $cartItem1 = $addToCart['data']['addToCart']['cartItem']; + $key1 = $cartItem1['key']; + + // Add item 2. + $addToCart = $this->addToCart( + array( + 'clientMutationId' => 'someId', + 'productId' => $ids['product'], + 'quantity' => 1, + 'variationId' => $ids['variations'][1], + ) + ); + + $this->assertArrayHasKey('data', $addToCart ); + $this->assertArrayHasKey('addToCart', $addToCart['data'] ); + $this->assertArrayHasKey('cartItem', $addToCart['data']['addToCart'] ); + $cartItem2 = $addToCart['data']['addToCart']['cartItem']; + $key2 = $cartItem2['key']; + + // Remove items. + $this->removeItemsFromCart( + array( + 'clientMutationId' => 'someId', + 'all' => true + ) + ); + + $actual = $this->restoreItems( + array( + 'clientMutationId' => 'someId', + 'keys' => array( $key1, $key2 ), + ) + ); + + // use --debug flag to view. + codecept_debug( $actual ); + + $expected = array( + 'data' => array( + 'restoreCartItems' => array( + 'clientMutationId' => 'someId', + 'cartItems' => array( $cartItem1, $cartItem2 ), + ), + ), + ); + + $this->assertEqualSets( $expected, $actual ); + $this->assertNotEmpty( \WC()->cart->get_cart_item( $key1 ) ); + $this->assertNotEmpty( \WC()->cart->get_cart_item( $key2 ) ); } public function testEmptyCartMutation() { diff --git a/vendor/autoload.php b/vendor/autoload.php index 52321b87..7e27af39 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -4,4 +4,4 @@ require_once __DIR__ . '/composer/autoload_real.php'; -return ComposerAutoloaderInitc0b9c25b0e0e6597e0a2526e78def682::getLoader(); +return ComposerAutoloaderInit000da6d626ad7439f4c7712f5e605082::getLoader(); diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index fad6160a..a726fd48 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -58,7 +58,7 @@ return array( 'WPGraphQL\\Extensions\\WooCommerce\\Mutation\\Cart_Empty' => $baseDir . '/src/mutation/class-cart-empty.php', 'WPGraphQL\\Extensions\\WooCommerce\\Mutation\\Cart_Remove_Coupon' => $baseDir . '/src/mutation/class-cart-remove-coupon.php', 'WPGraphQL\\Extensions\\WooCommerce\\Mutation\\Cart_Remove_Items' => $baseDir . '/src/mutation/class-cart-remove-items.php', - 'WPGraphQL\\Extensions\\WooCommerce\\Mutation\\Cart_Restore_Item' => $baseDir . '/src/mutation/class-cart-restore-item.php', + 'WPGraphQL\\Extensions\\WooCommerce\\Mutation\\Cart_Restore_Items' => $baseDir . '/src/mutation/class-cart-restore-items.php', 'WPGraphQL\\Extensions\\WooCommerce\\Mutation\\Cart_Update_Item_Quantity' => $baseDir . '/src/mutation/class-cart-update-item-quantity.php', 'WPGraphQL\\Extensions\\WooCommerce\\Mutation\\Customer_Register' => $baseDir . '/src/mutation/class-customer-register.php', 'WPGraphQL\\Extensions\\WooCommerce\\Mutation\\Customer_Update' => $baseDir . '/src/mutation/class-customer-update.php', diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index 1f7bfce5..e9de87bf 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -2,7 +2,7 @@ // autoload_real.php @generated by Composer -class ComposerAutoloaderInitc0b9c25b0e0e6597e0a2526e78def682 +class ComposerAutoloaderInit000da6d626ad7439f4c7712f5e605082 { private static $loader; @@ -19,15 +19,15 @@ class ComposerAutoloaderInitc0b9c25b0e0e6597e0a2526e78def682 return self::$loader; } - spl_autoload_register(array('ComposerAutoloaderInitc0b9c25b0e0e6597e0a2526e78def682', 'loadClassLoader'), true, true); + spl_autoload_register(array('ComposerAutoloaderInit000da6d626ad7439f4c7712f5e605082', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(); - spl_autoload_unregister(array('ComposerAutoloaderInitc0b9c25b0e0e6597e0a2526e78def682', 'loadClassLoader')); + spl_autoload_unregister(array('ComposerAutoloaderInit000da6d626ad7439f4c7712f5e605082', 'loadClassLoader')); $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded()); if ($useStaticLoader) { require_once __DIR__ . '/autoload_static.php'; - call_user_func(\Composer\Autoload\ComposerStaticInitc0b9c25b0e0e6597e0a2526e78def682::getInitializer($loader)); + call_user_func(\Composer\Autoload\ComposerStaticInit000da6d626ad7439f4c7712f5e605082::getInitializer($loader)); } else { $map = require __DIR__ . '/autoload_namespaces.php'; foreach ($map as $namespace => $path) { diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index a1ee1497..3ea7dbed 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -4,7 +4,7 @@ namespace Composer\Autoload; -class ComposerStaticInitc0b9c25b0e0e6597e0a2526e78def682 +class ComposerStaticInit000da6d626ad7439f4c7712f5e605082 { public static $prefixLengthsPsr4 = array ( 'W' => @@ -73,7 +73,7 @@ class ComposerStaticInitc0b9c25b0e0e6597e0a2526e78def682 'WPGraphQL\\Extensions\\WooCommerce\\Mutation\\Cart_Empty' => __DIR__ . '/../..' . '/src/mutation/class-cart-empty.php', 'WPGraphQL\\Extensions\\WooCommerce\\Mutation\\Cart_Remove_Coupon' => __DIR__ . '/../..' . '/src/mutation/class-cart-remove-coupon.php', 'WPGraphQL\\Extensions\\WooCommerce\\Mutation\\Cart_Remove_Items' => __DIR__ . '/../..' . '/src/mutation/class-cart-remove-items.php', - 'WPGraphQL\\Extensions\\WooCommerce\\Mutation\\Cart_Restore_Item' => __DIR__ . '/../..' . '/src/mutation/class-cart-restore-item.php', + 'WPGraphQL\\Extensions\\WooCommerce\\Mutation\\Cart_Restore_Items' => __DIR__ . '/../..' . '/src/mutation/class-cart-restore-items.php', 'WPGraphQL\\Extensions\\WooCommerce\\Mutation\\Cart_Update_Item_Quantity' => __DIR__ . '/../..' . '/src/mutation/class-cart-update-item-quantity.php', 'WPGraphQL\\Extensions\\WooCommerce\\Mutation\\Customer_Register' => __DIR__ . '/../..' . '/src/mutation/class-customer-register.php', 'WPGraphQL\\Extensions\\WooCommerce\\Mutation\\Customer_Update' => __DIR__ . '/../..' . '/src/mutation/class-customer-update.php', @@ -115,9 +115,9 @@ class ComposerStaticInitc0b9c25b0e0e6597e0a2526e78def682 public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { - $loader->prefixLengthsPsr4 = ComposerStaticInitc0b9c25b0e0e6597e0a2526e78def682::$prefixLengthsPsr4; - $loader->prefixDirsPsr4 = ComposerStaticInitc0b9c25b0e0e6597e0a2526e78def682::$prefixDirsPsr4; - $loader->classMap = ComposerStaticInitc0b9c25b0e0e6597e0a2526e78def682::$classMap; + $loader->prefixLengthsPsr4 = ComposerStaticInit000da6d626ad7439f4c7712f5e605082::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInit000da6d626ad7439f4c7712f5e605082::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInit000da6d626ad7439f4c7712f5e605082::$classMap; }, null, ClassLoader::class); }