From 5de8d9b20fc44d80b2ea1eced24a9e68e9439010 Mon Sep 17 00:00:00 2001 From: eduard13 Date: Mon, 11 May 2020 14:15:01 +0300 Subject: [PATCH 1/9] Adding Wishlist GraphQl support --- .../Model/Wishlist/AddProductsToWishlist.php | 162 ++++++++++++++++++ .../BuyRequest/BundleDataProvider.php | 55 ++++++ .../Wishlist/BuyRequest/BuyRequestBuilder.php | 63 +++++++ .../BuyRequestDataProviderInterface.php | 26 +++ .../CustomizableOptionDataProvider.php | 92 ++++++++++ .../DownloadableLinkDataProvider.php | 54 ++++++ .../BuyRequest/SuperAttributeDataProvider.php | 64 +++++++ .../BuyRequest/SuperGroupDataProvider.php | 64 +++++++ .../Wishlist/Model/Wishlist/Config.php | 46 +++++ .../Model/Wishlist/Data/EnteredOption.php | 54 ++++++ .../Wishlist/Model/Wishlist/Data/Error.php | 54 ++++++ .../Model/Wishlist/Data/SelectedOption.php | 37 ++++ .../Model/Wishlist/Data/WishlistItem.php | 146 ++++++++++++++++ .../Wishlist/Data/WishlistItemFactory.php | 75 ++++++++ .../Model/Wishlist/Data/WishlistOutput.php | 56 ++++++ .../Wishlist/RemoveProductsFromWishlist.php | 131 ++++++++++++++ .../Wishlist/UpdateProductsInWishlist.php | 136 +++++++++++++++ app/code/Magento/Wishlist/etc/graphql/di.xml | 20 +++ .../Mapper/WishlistDataMapper.php | 35 ++++ .../AddProductsToWishlistResolver.php | 129 ++++++++++++++ .../Resolver/CustomerWishlistResolver.php | 6 +- .../RemoveProductsFromWishlistResolver.php | 127 ++++++++++++++ .../UpdateProductsInWishlistResolver.php | 137 +++++++++++++++ .../Model/Resolver/WishlistResolver.php | 7 +- .../WishlistGraphQl/etc/schema.graphqls | 42 +++++ 25 files changed, 1814 insertions(+), 4 deletions(-) create mode 100644 app/code/Magento/Wishlist/Model/Wishlist/AddProductsToWishlist.php create mode 100644 app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/BundleDataProvider.php create mode 100644 app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/BuyRequestBuilder.php create mode 100644 app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/BuyRequestDataProviderInterface.php create mode 100644 app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/CustomizableOptionDataProvider.php create mode 100644 app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/DownloadableLinkDataProvider.php create mode 100644 app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/SuperAttributeDataProvider.php create mode 100644 app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/SuperGroupDataProvider.php create mode 100644 app/code/Magento/Wishlist/Model/Wishlist/Config.php create mode 100644 app/code/Magento/Wishlist/Model/Wishlist/Data/EnteredOption.php create mode 100644 app/code/Magento/Wishlist/Model/Wishlist/Data/Error.php create mode 100644 app/code/Magento/Wishlist/Model/Wishlist/Data/SelectedOption.php create mode 100644 app/code/Magento/Wishlist/Model/Wishlist/Data/WishlistItem.php create mode 100644 app/code/Magento/Wishlist/Model/Wishlist/Data/WishlistItemFactory.php create mode 100644 app/code/Magento/Wishlist/Model/Wishlist/Data/WishlistOutput.php create mode 100644 app/code/Magento/Wishlist/Model/Wishlist/RemoveProductsFromWishlist.php create mode 100644 app/code/Magento/Wishlist/Model/Wishlist/UpdateProductsInWishlist.php create mode 100644 app/code/Magento/Wishlist/etc/graphql/di.xml create mode 100644 app/code/Magento/WishlistGraphQl/Mapper/WishlistDataMapper.php create mode 100644 app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlistResolver.php create mode 100644 app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlistResolver.php create mode 100644 app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlistResolver.php diff --git a/app/code/Magento/Wishlist/Model/Wishlist/AddProductsToWishlist.php b/app/code/Magento/Wishlist/Model/Wishlist/AddProductsToWishlist.php new file mode 100644 index 0000000000000..6700f1585acb4 --- /dev/null +++ b/app/code/Magento/Wishlist/Model/Wishlist/AddProductsToWishlist.php @@ -0,0 +1,162 @@ +productRepository = $productRepository; + $this->buyRequestBuilder = $buyRequestBuilder; + $this->wishlistResource = $wishlistResource; + } + + /** + * Adding products to wishlist + * + * @param Wishlist $wishlist + * @param array $wishlistItems + * + * @return WishlistOutput + * + * @throws AlreadyExistsException + */ + public function execute(Wishlist $wishlist, array $wishlistItems): WishlistOutput + { + foreach ($wishlistItems as $wishlistItem) { + $this->addItemToWishlist($wishlist, $wishlistItem); + } + + $wishlistOutput = $this->prepareOutput($wishlist); + + if ($wishlist->isObjectNew() || count($wishlistOutput->getErrors()) !== count($wishlistItems)) { + $this->wishlistResource->save($wishlist); + } + + return $wishlistOutput; + } + + /** + * Add product item to wishlist + * + * @param Wishlist $wishlist + * @param WishlistItem $wishlistItem + */ + private function addItemToWishlist(Wishlist $wishlist, WishlistItem $wishlistItem) + { + $sku = $wishlistItem->getParentSku() ?? $wishlistItem->getSku(); + + try { + $product = $this->productRepository->get($sku, false, null, true); + } catch (NoSuchEntityException $e) { + $this->addError( + __('Could not find a product with SKU "%sku"', ['sku' => $sku])->render(), + self::ERROR_PRODUCT_NOT_FOUND + ); + + return; + } + + try { + $options = $this->buyRequestBuilder->build($wishlistItem, (int) $product->getId()); + $result = $wishlist->addNewItem($product, $options); + + if (is_string($result)) { + $this->addError($result); + } + } catch (LocalizedException $exception) { + $this->addError($exception->getMessage()); + } catch (\Throwable $e) { + $this->addError( + __( + 'Could not add the product with SKU "%sku" to the wishlist:: %message', + ['sku' => $sku, 'message' => $e->getMessage()] + )->render() + ); + } + } + + /** + * Add wishlist line item error + * + * @param string $message + * @param string|null $code + * + * @return void + */ + private function addError(string $message, string $code = null): void + { + $this->errors[] = new Data\Error( + $message, + $code ?? self::ERROR_UNDEFINED + ); + } + + /** + * Prepare output + * + * @param Wishlist $wishlist + * + * @return WishlistOutput + */ + private function prepareOutput(Wishlist $wishlist): WishlistOutput + { + $output = new WishlistOutput($wishlist, $this->errors); + $this->errors = []; + + return $output; + } +} diff --git a/app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/BundleDataProvider.php b/app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/BundleDataProvider.php new file mode 100644 index 0000000000000..1cfa316c3cd01 --- /dev/null +++ b/app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/BundleDataProvider.php @@ -0,0 +1,55 @@ +getSelectedOptions() as $optionData) { + $optionData = \explode('/', base64_decode($optionData->getId())); + + if ($this->isProviderApplicable($optionData) === false) { + continue; + } + + [, $optionId, $optionValueId, $optionQuantity] = $optionData; + + $bundleOptionsData['bundle_option'][$optionId] = $optionValueId; + $bundleOptionsData['bundle_option_qty'][$optionId] = $optionQuantity; + } + + return $bundleOptionsData; + } + + /** + * Checks whether this provider is applicable for the current option + * + * @param array $optionData + * + * @return bool + */ + private function isProviderApplicable(array $optionData): bool + { + return $optionData[0] === self::PROVIDER_OPTION_TYPE; + } +} diff --git a/app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/BuyRequestBuilder.php b/app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/BuyRequestBuilder.php new file mode 100644 index 0000000000000..1f7ddce345b1c --- /dev/null +++ b/app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/BuyRequestBuilder.php @@ -0,0 +1,63 @@ +dataObjectFactory = $dataObjectFactory; + $this->providers = $providers; + } + + /** + * Build product buy request for adding to wishlist + * + * @param WishlistItem $wishlistItemData + * @param int|null $productId + * + * @return DataObject + */ + public function build(WishlistItem $wishlistItemData, ?int $productId = null): DataObject + { + $requestData = [ + [ + 'qty' => $wishlistItemData->getQuantity(), + ] + ]; + + foreach ($this->providers as $provider) { + $requestData[] = $provider->execute($wishlistItemData, $productId); + } + + return $this->dataObjectFactory->create(['data' => array_merge(...$requestData)]); + } +} diff --git a/app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/BuyRequestDataProviderInterface.php b/app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/BuyRequestDataProviderInterface.php new file mode 100644 index 0000000000000..fac45d7f86c7c --- /dev/null +++ b/app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/BuyRequestDataProviderInterface.php @@ -0,0 +1,26 @@ +getSelectedOptions() as $optionData) { + $optionData = \explode('/', base64_decode($optionData->getId())); + + if ($this->isProviderApplicable($optionData) === false) { + continue; + } + + [, $optionId, $optionValue] = $optionData; + + $customizableOptionsData[$optionId][] = $optionValue; + } + + foreach ($wishlistItemData->getEnteredOptions() as $option) { + $optionData = \explode('/', base64_decode($option->getId())); + + if ($this->isProviderApplicable($optionData) === false) { + continue; + } + + [, $optionId] = $optionData; + + $customizableOptionsData[$optionId][] = $option->getValue(); + } + + if (empty($customizableOptionsData)) { + return $customizableOptionsData; + } + + $result = ['options' => $this->flattenOptionValues($customizableOptionsData)]; + + if ($productId) { + $result += ['product' => $productId]; + } + + return $result; + } + + /** + * Flatten option values for non-multiselect customizable options + * + * @param array $customizableOptionsData + * + * @return array + */ + private function flattenOptionValues(array $customizableOptionsData): array + { + foreach ($customizableOptionsData as $optionId => $optionValue) { + if (count($optionValue) === 1) { + $customizableOptionsData[$optionId] = $optionValue[0]; + } + } + + return $customizableOptionsData; + } + + /** + * Checks whether this provider is applicable for the current option + * + * @param array $optionData + * @return bool + */ + private function isProviderApplicable(array $optionData): bool + { + return $optionData[0] === self::PROVIDER_OPTION_TYPE; + } +} diff --git a/app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/DownloadableLinkDataProvider.php b/app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/DownloadableLinkDataProvider.php new file mode 100644 index 0000000000000..1ad1a0b64706a --- /dev/null +++ b/app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/DownloadableLinkDataProvider.php @@ -0,0 +1,54 @@ +getSelectedOptions() as $optionData) { + $optionData = \explode('/', base64_decode($optionData->getId())); + + if ($this->isProviderApplicable($optionData) === false) { + continue; + } + + [, $linkId] = $optionData; + + $linksData[] = $linkId; + } + + return $linksData ? ['links' => $linksData] : []; + } + + /** + * Checks whether this provider is applicable for the current option + * + * @param array $optionData + * + * @return bool + */ + private function isProviderApplicable(array $optionData): bool + { + return $optionData[0] === self::PROVIDER_OPTION_TYPE; + } +} diff --git a/app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/SuperAttributeDataProvider.php b/app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/SuperAttributeDataProvider.php new file mode 100644 index 0000000000000..01e29bcf80c0b --- /dev/null +++ b/app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/SuperAttributeDataProvider.php @@ -0,0 +1,64 @@ +getSelectedOptions() as $optionData) { + $optionData = \explode('/', base64_decode($optionData->getId())); + + if ($this->isProviderApplicable($optionData) === false) { + continue; + } + + [, $attributeId, $valueIndex] = $optionData; + + $configurableData[$attributeId] = $valueIndex; + } + + if (empty($configurableData)) { + return $configurableData; + } + + $result = ['super_attribute' => $configurableData]; + + if ($productId) { + $result += ['product' => $productId]; + } + + return $result; + } + + /** + * Checks whether this provider is applicable for the current option + * + * @param array $optionData + * + * @return bool + */ + private function isProviderApplicable(array $optionData): bool + { + return $optionData[0] === self::PROVIDER_OPTION_TYPE; + } +} diff --git a/app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/SuperGroupDataProvider.php b/app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/SuperGroupDataProvider.php new file mode 100644 index 0000000000000..a11f631f1f5aa --- /dev/null +++ b/app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/SuperGroupDataProvider.php @@ -0,0 +1,64 @@ +getSelectedOptions() as $optionData) { + $optionData = \explode('/', base64_decode($optionData->getId())); + + if ($this->isProviderApplicable($optionData) === false) { + continue; + } + + [, $simpleProductId, $quantity] = $optionData; + + $groupedData[$simpleProductId] = $quantity; + } + + if (empty($groupedData)) { + return $groupedData; + } + + $result = ['super_group' => $groupedData]; + + if ($productId) { + $result += ['product' => $productId]; + } + + return $result; + } + + /** + * Checks whether this provider is applicable for the current option + * + * @param array $optionData + * + * @return bool + */ + private function isProviderApplicable(array $optionData): bool + { + return $optionData[0] === self::PROVIDER_OPTION_TYPE; + } +} diff --git a/app/code/Magento/Wishlist/Model/Wishlist/Config.php b/app/code/Magento/Wishlist/Model/Wishlist/Config.php new file mode 100644 index 0000000000000..041e9f1ca0a21 --- /dev/null +++ b/app/code/Magento/Wishlist/Model/Wishlist/Config.php @@ -0,0 +1,46 @@ +scopeConfig = $scopeConfig; + } + + /** + * Check whether the wishlist is enabled or not + * + * @return bool + */ + public function isEnabled(): bool + { + return $this->scopeConfig->isSetFlag( + self::XML_PATH_WISHLIST_ACTIVE, + ScopeInterface::SCOPE_STORES + ); + } +} diff --git a/app/code/Magento/Wishlist/Model/Wishlist/Data/EnteredOption.php b/app/code/Magento/Wishlist/Model/Wishlist/Data/EnteredOption.php new file mode 100644 index 0000000000000..0d6b2a2302540 --- /dev/null +++ b/app/code/Magento/Wishlist/Model/Wishlist/Data/EnteredOption.php @@ -0,0 +1,54 @@ +id = $id; + $this->value = $value; + } + + /** + * Get entered option id + * + * @return string + */ + public function getId(): string + { + return $this->id; + } + + /** + * Get entered option value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } +} diff --git a/app/code/Magento/Wishlist/Model/Wishlist/Data/Error.php b/app/code/Magento/Wishlist/Model/Wishlist/Data/Error.php new file mode 100644 index 0000000000000..cb8420169fa8a --- /dev/null +++ b/app/code/Magento/Wishlist/Model/Wishlist/Data/Error.php @@ -0,0 +1,54 @@ +message = $message; + $this->code = $code; + } + + /** + * Get error message + * + * @return string + */ + public function getMessage(): string + { + return $this->message; + } + + /** + * Get error code + * + * @return string + */ + public function getCode(): string + { + return $this->code; + } +} diff --git a/app/code/Magento/Wishlist/Model/Wishlist/Data/SelectedOption.php b/app/code/Magento/Wishlist/Model/Wishlist/Data/SelectedOption.php new file mode 100644 index 0000000000000..129a61c0a2a6c --- /dev/null +++ b/app/code/Magento/Wishlist/Model/Wishlist/Data/SelectedOption.php @@ -0,0 +1,37 @@ +id = $id; + } + + /** + * Get selected option id + * + * @return string + */ + public function getId(): string + { + return $this->id; + } +} diff --git a/app/code/Magento/Wishlist/Model/Wishlist/Data/WishlistItem.php b/app/code/Magento/Wishlist/Model/Wishlist/Data/WishlistItem.php new file mode 100644 index 0000000000000..236b7f1eee72d --- /dev/null +++ b/app/code/Magento/Wishlist/Model/Wishlist/Data/WishlistItem.php @@ -0,0 +1,146 @@ +quantity = $quantity; + $this->sku = $sku; + $this->parentSku = $parentSku; + $this->id = $id; + $this->description = $description; + $this->selectedOptions = $selectedOptions; + $this->enteredOptions = $enteredOptions; + } + + /** + * Get wishlist item id + * + * @return int|null + */ + public function getId(): ?int + { + return $this->id; + } + + /** + * Get wishlist item description + * + * @return string|null + */ + public function getDescription(): ?string + { + return $this->description; + } + + /** + * Get sku + * + * @return string|null + */ + public function getSku(): ?string + { + return $this->sku; + } + + /** + * Get quantity + * + * @return float + */ + public function getQuantity(): float + { + return $this->quantity; + } + + /** + * Get parent sku + * + * @return string|null + */ + public function getParentSku(): ?string + { + return $this->parentSku; + } + + /** + * Get selected options + * + * @return SelectedOption[]|null + */ + public function getSelectedOptions(): ?array + { + return $this->selectedOptions; + } + + /** + * Get entered options + * + * @return EnteredOption[]|null + */ + public function getEnteredOptions(): ?array + { + return $this->enteredOptions; + } +} diff --git a/app/code/Magento/Wishlist/Model/Wishlist/Data/WishlistItemFactory.php b/app/code/Magento/Wishlist/Model/Wishlist/Data/WishlistItemFactory.php new file mode 100644 index 0000000000000..153e8451bae31 --- /dev/null +++ b/app/code/Magento/Wishlist/Model/Wishlist/Data/WishlistItemFactory.php @@ -0,0 +1,75 @@ +createSelectedOptions($data['selected_options']) : [], + isset($data['entered_options']) ? $this->createEnteredOptions($data['entered_options']) : [] + ); + } + + /** + * Create array of Entered Options + * + * @param array $options + * + * @return EnteredOption[] + */ + private function createEnteredOptions(array $options): array + { + return \array_map( + function (array $option) { + if (!isset($option['id'], $option['value'])) { + throw new InputException( + __('Required fields are not present EnteredOption.id, EnteredOption.value') + ); + } + return new EnteredOption($option['id'], $option['value']); + }, + $options + ); + } + + /** + * Create array of Selected Options + * + * @param string[] $options + * + * @return SelectedOption[] + */ + private function createSelectedOptions(array $options): array + { + return \array_map( + function ($option) { + return new SelectedOption($option); + }, + $options + ); + } +} diff --git a/app/code/Magento/Wishlist/Model/Wishlist/Data/WishlistOutput.php b/app/code/Magento/Wishlist/Model/Wishlist/Data/WishlistOutput.php new file mode 100644 index 0000000000000..fc7db9ec910fb --- /dev/null +++ b/app/code/Magento/Wishlist/Model/Wishlist/Data/WishlistOutput.php @@ -0,0 +1,56 @@ +wishlist = $wishlist; + $this->errors = $errors; + } + + /** + * Get Wishlist + * + * @return Wishlist + */ + public function getWishlist(): Wishlist + { + return $this->wishlist; + } + + /** + * Get errors happened during adding products to wishlist + * + * @return Error[] + */ + public function getErrors(): array + { + return $this->errors; + } +} diff --git a/app/code/Magento/Wishlist/Model/Wishlist/RemoveProductsFromWishlist.php b/app/code/Magento/Wishlist/Model/Wishlist/RemoveProductsFromWishlist.php new file mode 100644 index 0000000000000..c213c048fd61a --- /dev/null +++ b/app/code/Magento/Wishlist/Model/Wishlist/RemoveProductsFromWishlist.php @@ -0,0 +1,131 @@ +wishlistItemFactory = $wishlistItemFactory; + $this->wishlistItemResource = $wishlistItemResource; + } + + /** + * Removing items from wishlist + * + * @param Wishlist $wishlist + * @param array $wishlistItemsIds + * + * @return WishlistOutput + */ + public function execute(Wishlist $wishlist, array $wishlistItemsIds): WishlistOutput + { + foreach ($wishlistItemsIds as $wishlistItemId) { + $this->removeItemFromWishlist((int) $wishlistItemId); + } + + return $this->prepareOutput($wishlist); + } + + /** + * Remove product item from wishlist + * + * @param int $wishlistItemId + */ + private function removeItemFromWishlist(int $wishlistItemId): void + { + try { + /** @var WishlistItem $wishlistItem */ + $wishlistItem = $this->wishlistItemFactory->create(); + $this->wishlistItemResource->load($wishlistItem, $wishlistItemId); + if (!$wishlistItem->getId()) { + $this->addError( + __('Could not find a wishlist item with ID "%id"', ['id' => $wishlistItemId])->render(), + self::ERROR_PRODUCT_NOT_FOUND + ); + } + + $this->wishlistItemResource->delete($wishlistItem); + } catch (\Exception $e) { + $this->addError( + __( + 'We can\'t delete the item with ID "%id" from the Wish List right now.', + ['id' => $wishlistItemId] + )->render() + ); + } + } + + /** + * Add wishlist line item error + * + * @param string $message + * @param string|null $code + * + * @return void + */ + private function addError(string $message, string $code = null): void + { + $this->errors[] = new Data\Error( + $message, + $code ?? self::ERROR_UNDEFINED + ); + } + + /** + * Prepare output + * + * @param Wishlist $wishlist + * + * @return WishlistOutput + */ + private function prepareOutput(Wishlist $wishlist): WishlistOutput + { + $output = new WishlistOutput($wishlist, $this->errors); + $this->errors = []; + + return $output; + } +} diff --git a/app/code/Magento/Wishlist/Model/Wishlist/UpdateProductsInWishlist.php b/app/code/Magento/Wishlist/Model/Wishlist/UpdateProductsInWishlist.php new file mode 100644 index 0000000000000..691e00090373a --- /dev/null +++ b/app/code/Magento/Wishlist/Model/Wishlist/UpdateProductsInWishlist.php @@ -0,0 +1,136 @@ +buyRequestBuilder = $buyRequestBuilder; + $this->wishlistItemFactory = $wishlistItemFactory; + $this->wishlistItemResource = $wishlistItemResource; + } + + /** + * Adding products to wishlist + * + * @param Wishlist $wishlist + * @param array $wishlistItems + * + * @return WishlistOutput + */ + public function execute(Wishlist $wishlist, array $wishlistItems): WishlistOutput + { + foreach ($wishlistItems as $wishlistItem) { + $this->updateItemInWishlist($wishlist, $wishlistItem); + } + + return $this->prepareOutput($wishlist); + } + + /** + * Update product item in wishlist + * + * @param Wishlist $wishlist + * @param WishlistItemData $wishlistItemData + */ + private function updateItemInWishlist(Wishlist $wishlist, WishlistItemData $wishlistItemData) + { + try { + $options = $this->buyRequestBuilder->build($wishlistItemData); + /** @var WishlistItem $wishlistItem */ + $wishlistItem = $this->wishlistItemFactory->create(); + $this->wishlistItemResource->load($wishlistItem, $wishlistItemData->getId()); + $wishlistItem->setDescription($wishlistItemData->getDescription()); + $resultItem = $wishlist->updateItem($wishlistItem, $options); + + if (is_string($resultItem)) { + $this->addError($resultItem); + } + } catch (LocalizedException $exception) { + $this->addError($exception->getMessage()); + } + } + + /** + * Add wishlist line item error + * + * @param string $message + * @param string|null $code + * + * @return void + */ + private function addError(string $message, string $code = null): void + { + $this->errors[] = new Data\Error( + $message, + $code ?? self::ERROR_UNDEFINED + ); + } + + /** + * Prepare output + * + * @param Wishlist $wishlist + * + * @return WishlistOutput + */ + private function prepareOutput(Wishlist $wishlist): WishlistOutput + { + $output = new WishlistOutput($wishlist, $this->errors); + $this->errors = []; + + return $output; + } +} diff --git a/app/code/Magento/Wishlist/etc/graphql/di.xml b/app/code/Magento/Wishlist/etc/graphql/di.xml new file mode 100644 index 0000000000000..9726376bf30be --- /dev/null +++ b/app/code/Magento/Wishlist/etc/graphql/di.xml @@ -0,0 +1,20 @@ + + + + + + + Magento\Wishlist\Model\Wishlist\BuyRequest\SuperAttributeDataProvider + Magento\Wishlist\Model\Wishlist\BuyRequest\CustomizableOptionDataProvider + Magento\Wishlist\Model\Wishlist\BuyRequest\BundleDataProvider + Magento\Wishlist\Model\Wishlist\BuyRequest\DownloadableLinkDataProvider + Magento\Wishlist\Model\Wishlist\BuyRequest\SuperGroupDataProvider + + + + diff --git a/app/code/Magento/WishlistGraphQl/Mapper/WishlistDataMapper.php b/app/code/Magento/WishlistGraphQl/Mapper/WishlistDataMapper.php new file mode 100644 index 0000000000000..9cc1404613e41 --- /dev/null +++ b/app/code/Magento/WishlistGraphQl/Mapper/WishlistDataMapper.php @@ -0,0 +1,35 @@ + $wishlist->getId(), + 'sharing_code' => $wishlist->getSharingCode(), + 'updated_at' => $wishlist->getUpdatedAt(), + 'items_count' => $wishlist->getItemsCount(), + 'name' => $wishlist->getName(), + 'model' => $wishlist, + ]; + } +} diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlistResolver.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlistResolver.php new file mode 100644 index 0000000000000..cdcffa8aa2adc --- /dev/null +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlistResolver.php @@ -0,0 +1,129 @@ +wishlistResource = $wishlistResource; + $this->wishlistFactory = $wishlistFactory; + $this->wishlistConfig = $wishlistConfig; + $this->addProductsToWishlist = $addProductsToWishlist; + $this->wishlistDataMapper = $wishlistDataMapper; + } + + /** + * @inheritdoc + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + if (!$this->wishlistConfig->isEnabled()) { + throw new GraphQlInputException(__('The wishlist is not currently available.')); + } + + $customerId = $context->getUserId(); + + /* Guest checking */ + if (!$customerId && 0 === $customerId) { + throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist')); + } + + $wishlistId = $args['wishlist_id'] ?: null; + $wishlistItemsData = $args['wishlist_items']; + $wishlist = $this->wishlistFactory->create(); + + if ($wishlistId) { + $this->wishlistResource->load($wishlist, $wishlistId); + } elseif ($customerId) { + $wishlist->loadByCustomerId($customerId, true); + } + + if (null === $wishlist->getId()) { + throw new GraphQlInputException(__('Something went wrong while creating the wishlist')); + } + + $wishlistItems = []; + foreach ($wishlistItemsData as $wishlistItemData) { + $wishlistItems[] = (new WishlistItemFactory())->create($wishlistItemData); + } + + $wishlistOutput = $this->addProductsToWishlist->execute($wishlist, $wishlistItems); + + return [ + 'wishlist' => $this->wishlistDataMapper->map($wishlistOutput->getWishlist()), + 'userInputErrors' => \array_map( + function (Error $error) { + return [ + 'code' => $error->getCode(), + 'message' => $error->getMessage(), + ]; + }, + $wishlistOutput->getErrors() + ) + ]; + } +} diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistResolver.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistResolver.php index a84ce0e965b6d..94d543d25aa7a 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistResolver.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistResolver.php @@ -27,8 +27,9 @@ class CustomerWishlistResolver implements ResolverInterface /** * @param WishlistFactory $wishlistFactory */ - public function __construct(WishlistFactory $wishlistFactory) - { + public function __construct( + WishlistFactory $wishlistFactory + ) { $this->wishlistFactory = $wishlistFactory; } @@ -42,6 +43,7 @@ public function resolve( array $value = null, array $args = null ) { + // Todo: Check if wishlist is enabled if (false === $context->getExtensionAttributes()->getIsCustomer()) { throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.')); } diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlistResolver.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlistResolver.php new file mode 100644 index 0000000000000..8c8d3aea54993 --- /dev/null +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlistResolver.php @@ -0,0 +1,127 @@ +wishlistResource = $wishlistResource; + $this->wishlistConfig = $wishlistConfig; + $this->wishlistFactory = $wishlistFactory; + $this->wishlistDataMapper = $wishlistDataMapper; + $this->removeProductsFromWishlist = $removeProductsFromWishlist; + } + + /** + * @inheritdoc + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + if (!$this->wishlistConfig->isEnabled()) { + throw new GraphQlInputException(__('The wishlist is not currently available.')); + } + + $customerId = $context->getUserId(); + + /* Guest checking */ + if (!$customerId && 0 === $customerId) { + throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist')); + } + + $wishlistId = $args['wishlist_id']; + + if (!$wishlistId) { + throw new GraphQlInputException(__('The wishlist id is missing.')); + } + + $wishlist = $this->wishlistFactory->create(); + $this->wishlistResource->load($wishlist, $wishlistId); + + if (!$wishlist) { + throw new GraphQlInputException(__('The wishlist was not found.')); + } + + $wishlistItemsIds = $args['wishlist_items_ids']; + $wishlistOutput = $this->removeProductsFromWishlist->execute($wishlist, $wishlistItemsIds); + + if (!empty($wishlistItemsIds)) { + $this->wishlistResource->save($wishlist); + } + + return [ + 'wishlist' => $this->wishlistDataMapper->map($wishlistOutput->getWishlist()), + 'userInputErrors' => \array_map( + function (Error $error) { + return [ + 'code' => $error->getCode(), + 'message' => $error->getMessage(), + ]; + }, + $wishlistOutput->getErrors() + ) + ]; + } +} diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlistResolver.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlistResolver.php new file mode 100644 index 0000000000000..6ce074b3e8dc2 --- /dev/null +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlistResolver.php @@ -0,0 +1,137 @@ +wishlistResource = $wishlistResource; + $this->wishlistFactory = $wishlistFactory; + $this->wishlistConfig = $wishlistConfig; + $this->updateProductsInWishlist = $updateProductsInWishlist; + $this->wishlistDataMapper = $wishlistDataMapper; + } + + /** + * @inheritdoc + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + if (!$this->wishlistConfig->isEnabled()) { + throw new GraphQlInputException(__('The wishlist is not currently available.')); + } + + $customerId = $context->getUserId(); + + /* Guest checking */ + if (!$customerId && 0 === $customerId) { + throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist')); + } + + $wishlistId = $args['wishlist_id']; + + if (!$wishlistId) { + throw new GraphQlInputException(__('The wishlist id is missing.')); + } + + $wishlist = $this->wishlistFactory->create(); + + if ($wishlistId) { + $this->wishlistResource->load($wishlist, $wishlistId); + } + + if (null === $wishlist->getId()) { + throw new GraphQlInputException(__('The wishlist was not found.')); + } + + $wishlistItems = []; + $wishlistItemsData = $args['wishlist_items']; + + foreach ($wishlistItemsData as $wishlistItemData) { + $wishlistItems[] = (new WishlistItemFactory())->create($wishlistItemData); + } + + $wishlistOutput = $this->updateProductsInWishlist->execute($wishlist, $wishlistItems); + + if (count($wishlistOutput->getErrors()) !== count($wishlistItems)) { + $this->wishlistResource->save($wishlist); + } + + return [ + 'wishlist' => $this->wishlistDataMapper->map($wishlistOutput->getWishlist()), + 'userInputErrors' => \array_map( + function (Error $error) { + return [ + 'code' => $error->getCode(), + 'message' => $error->getMessage(), + ]; + }, + $wishlistOutput->getErrors() + ) + ]; + } +} diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/WishlistResolver.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/WishlistResolver.php index 792928ab61aaf..d0d409abd1698 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/WishlistResolver.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/WishlistResolver.php @@ -34,8 +34,10 @@ class WishlistResolver implements ResolverInterface * @param WishlistResourceModel $wishlistResource * @param WishlistFactory $wishlistFactory */ - public function __construct(WishlistResourceModel $wishlistResource, WishlistFactory $wishlistFactory) - { + public function __construct( + WishlistResourceModel $wishlistResource, + WishlistFactory $wishlistFactory + ) { $this->wishlistResource = $wishlistResource; $this->wishlistFactory = $wishlistFactory; } @@ -50,6 +52,7 @@ public function resolve( array $value = null, array $args = null ) { + // Todo: Check if wishlist is enabled $customerId = $context->getUserId(); /* Guest checking */ diff --git a/app/code/Magento/WishlistGraphQl/etc/schema.graphqls b/app/code/Magento/WishlistGraphQl/etc/schema.graphqls index deaa66921ba7c..1fcfc3b9488af 100644 --- a/app/code/Magento/WishlistGraphQl/etc/schema.graphqls +++ b/app/code/Magento/WishlistGraphQl/etc/schema.graphqls @@ -32,3 +32,45 @@ type WishlistItem { added_at: String @doc(description: "The time when the customer added the item to the wish list"), product: ProductInterface @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\ProductResolver") } + +type Mutation { + addProductsToWishlist(wishlist_id: ID!, wishlist_items: [WishlistItemInput!]!): AddProductsToWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\AddProductsToWishlistResolver") + removeProductsFromWishlist(wishlist_id: ID!, wishlist_items_ids: [ID!]!): RemoveProductsFromWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\RemoveProductsFromWishlistResolver") + updateProductsInWishlist(wishlist_id: ID!, wishlist_items: [WishlistItemUpdateInput!]!): UpdateProductsInWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\UpdateProductsInWishlistResolver") +} + +input WishlistItemInput { + sku: String + quantity: Float + parent_sku: String, + selected_options: [String!] + entered_options: [EnteredOptionInput!] +} + +type AddProductsToWishlistOutput { + wishlist: Wishlist! + userInputErrors:[CheckoutUserInputError]! @doc(description: "An array of wishlist adding errors.") +} + +input EnteredOptionInput { + id: String! @doc(description: "base64 encoded id") + value: String! +} + +type RemoveProductsFromWishlistOutput { + wishlist: Wishlist! + userInputErrors:[CheckoutUserInputError]! @doc(description:"An array of wishlist removing errors.") +} + +input WishlistItemUpdateInput { + wishlist_item_id: ID + quantity: Float + description: String + selected_options: [String!] + entered_options: [EnteredOptionInput!] +} + +type UpdateProductsInWishlistOutput { + wishlist: Wishlist! + userInputErrors:[CheckoutUserInputError]! @doc(description:"An array of wishlist updating errors.") +} From 7488a36001628ec19b364ad08f7886f65b172da9 Mon Sep 17 00:00:00 2001 From: eduard13 Date: Fri, 22 May 2020 10:28:49 +0300 Subject: [PATCH 2/9] Test coverage for Wishlist GraphQl --- ...Resolver.php => AddProductsToWishlist.php} | 10 +- ...ver.php => RemoveProductsFromWishlist.php} | 10 +- ...olver.php => UpdateProductsInWishlist.php} | 10 +- .../WishlistGraphQl/etc/schema.graphqls | 6 +- .../AddBundleProductToWishlistTest.php | 176 ++++++++++++++ .../AddConfigurableProductToWishlistTest.php | 222 ++++++++++++++++++ .../AddDownloadableProductToWishlistTest.php | 216 +++++++++++++++++ .../DeleteProductsFromWishlistTest.php | 147 ++++++++++++ .../GetCustomOptionsWithIDV2ForQueryBySku.php | 97 ++++++++ .../UpdateProductsFromWishlistTest.php | 159 +++++++++++++ 10 files changed, 1035 insertions(+), 18 deletions(-) rename app/code/Magento/WishlistGraphQl/Model/Resolver/{AddProductsToWishlistResolver.php => AddProductsToWishlist.php} (92%) rename app/code/Magento/WishlistGraphQl/Model/Resolver/{RemoveProductsFromWishlistResolver.php => RemoveProductsFromWishlist.php} (91%) rename app/code/Magento/WishlistGraphQl/Model/Resolver/{UpdateProductsInWishlistResolver.php => UpdateProductsInWishlist.php} (92%) create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/AddBundleProductToWishlistTest.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/AddConfigurableProductToWishlistTest.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/AddDownloadableProductToWishlistTest.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/DeleteProductsFromWishlistTest.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/GetCustomOptionsWithIDV2ForQueryBySku.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/UpdateProductsFromWishlistTest.php diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlistResolver.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlist.php similarity index 92% rename from app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlistResolver.php rename to app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlist.php index cdcffa8aa2adc..7c2e7c304c53d 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlistResolver.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlist.php @@ -13,7 +13,7 @@ use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResourceModel; -use Magento\Wishlist\Model\Wishlist\AddProductsToWishlist; +use Magento\Wishlist\Model\Wishlist\AddProductsToWishlist as AddProductsToWishlistModel; use Magento\Wishlist\Model\Wishlist\Config as WishlistConfig; use Magento\Wishlist\Model\Wishlist\Data\Error; use Magento\Wishlist\Model\Wishlist\Data\WishlistItemFactory; @@ -23,10 +23,10 @@ /** * Adding products to wishlist resolver */ -class AddProductsToWishlistResolver implements ResolverInterface +class AddProductsToWishlist implements ResolverInterface { /** - * @var AddProductsToWishlist + * @var AddProductsToWishlistModel */ private $addProductsToWishlist; @@ -54,14 +54,14 @@ class AddProductsToWishlistResolver implements ResolverInterface * @param WishlistResourceModel $wishlistResource * @param WishlistFactory $wishlistFactory * @param WishlistConfig $wishlistConfig - * @param AddProductsToWishlist $addProductsToWishlist + * @param AddProductsToWishlistModel $addProductsToWishlist * @param WishlistDataMapper $wishlistDataMapper */ public function __construct( WishlistResourceModel $wishlistResource, WishlistFactory $wishlistFactory, WishlistConfig $wishlistConfig, - AddProductsToWishlist $addProductsToWishlist, + AddProductsToWishlistModel $addProductsToWishlist, WishlistDataMapper $wishlistDataMapper ) { $this->wishlistResource = $wishlistResource; diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlistResolver.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlist.php similarity index 91% rename from app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlistResolver.php rename to app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlist.php index 8c8d3aea54993..01e60c21190fe 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlistResolver.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlist.php @@ -15,14 +15,14 @@ use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResourceModel; use Magento\Wishlist\Model\Wishlist\Config as WishlistConfig; use Magento\Wishlist\Model\Wishlist\Data\Error; -use Magento\Wishlist\Model\Wishlist\RemoveProductsFromWishlist; +use Magento\Wishlist\Model\Wishlist\RemoveProductsFromWishlist as RemoveProductsFromWishlistModel; use Magento\Wishlist\Model\WishlistFactory; use Magento\WishlistGraphQl\Mapper\WishlistDataMapper; /** * Removing products from wishlist resolver */ -class RemoveProductsFromWishlistResolver implements ResolverInterface +class RemoveProductsFromWishlist implements ResolverInterface { /** * @var WishlistDataMapper @@ -30,7 +30,7 @@ class RemoveProductsFromWishlistResolver implements ResolverInterface private $wishlistDataMapper; /** - * @var RemoveProductsFromWishlist + * @var RemoveProductsFromWishlistModel */ private $removeProductsFromWishlist; @@ -54,14 +54,14 @@ class RemoveProductsFromWishlistResolver implements ResolverInterface * @param WishlistResourceModel $wishlistResource * @param WishlistConfig $wishlistConfig * @param WishlistDataMapper $wishlistDataMapper - * @param RemoveProductsFromWishlist $removeProductsFromWishlist + * @param RemoveProductsFromWishlistModel $removeProductsFromWishlist */ public function __construct( WishlistFactory $wishlistFactory, WishlistResourceModel $wishlistResource, WishlistConfig $wishlistConfig, WishlistDataMapper $wishlistDataMapper, - RemoveProductsFromWishlist $removeProductsFromWishlist + RemoveProductsFromWishlistModel $removeProductsFromWishlist ) { $this->wishlistResource = $wishlistResource; $this->wishlistConfig = $wishlistConfig; diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlistResolver.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlist.php similarity index 92% rename from app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlistResolver.php rename to app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlist.php index 6ce074b3e8dc2..e8483cf6391b6 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlistResolver.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlist.php @@ -16,17 +16,17 @@ use Magento\Wishlist\Model\Wishlist\Config as WishlistConfig; use Magento\Wishlist\Model\Wishlist\Data\Error; use Magento\Wishlist\Model\Wishlist\Data\WishlistItemFactory; -use Magento\Wishlist\Model\Wishlist\UpdateProductsInWishlist; +use Magento\Wishlist\Model\Wishlist\UpdateProductsInWishlist as UpdateProductsInWishlistModel; use Magento\Wishlist\Model\WishlistFactory; use Magento\WishlistGraphQl\Mapper\WishlistDataMapper; /** * Update wishlist items resolver */ -class UpdateProductsInWishlistResolver implements ResolverInterface +class UpdateProductsInWishlist implements ResolverInterface { /** - * @var UpdateProductsInWishlist + * @var UpdateProductsInWishlistModel */ private $updateProductsInWishlist; @@ -54,14 +54,14 @@ class UpdateProductsInWishlistResolver implements ResolverInterface * @param WishlistResourceModel $wishlistResource * @param WishlistFactory $wishlistFactory * @param WishlistConfig $wishlistConfig - * @param UpdateProductsInWishlist $updateProductsInWishlist + * @param UpdateProductsInWishlistModel $updateProductsInWishlist * @param WishlistDataMapper $wishlistDataMapper */ public function __construct( WishlistResourceModel $wishlistResource, WishlistFactory $wishlistFactory, WishlistConfig $wishlistConfig, - UpdateProductsInWishlist $updateProductsInWishlist, + UpdateProductsInWishlistModel $updateProductsInWishlist, WishlistDataMapper $wishlistDataMapper ) { $this->wishlistResource = $wishlistResource; diff --git a/app/code/Magento/WishlistGraphQl/etc/schema.graphqls b/app/code/Magento/WishlistGraphQl/etc/schema.graphqls index 1fcfc3b9488af..d7a22a52ee1d2 100644 --- a/app/code/Magento/WishlistGraphQl/etc/schema.graphqls +++ b/app/code/Magento/WishlistGraphQl/etc/schema.graphqls @@ -34,9 +34,9 @@ type WishlistItem { } type Mutation { - addProductsToWishlist(wishlist_id: ID!, wishlist_items: [WishlistItemInput!]!): AddProductsToWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\AddProductsToWishlistResolver") - removeProductsFromWishlist(wishlist_id: ID!, wishlist_items_ids: [ID!]!): RemoveProductsFromWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\RemoveProductsFromWishlistResolver") - updateProductsInWishlist(wishlist_id: ID!, wishlist_items: [WishlistItemUpdateInput!]!): UpdateProductsInWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\UpdateProductsInWishlistResolver") + addProductsToWishlist(wishlist_id: ID!, wishlist_items: [WishlistItemInput!]!): AddProductsToWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\AddProductsToWishlist") + removeProductsFromWishlist(wishlist_id: ID!, wishlist_items_ids: [ID!]!): RemoveProductsFromWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\RemoveProductsFromWishlist") + updateProductsInWishlist(wishlist_id: ID!, wishlist_items: [WishlistItemUpdateInput!]!): UpdateProductsInWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\UpdateProductsInWishlist") } input WishlistItemInput { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/AddBundleProductToWishlistTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/AddBundleProductToWishlistTest.php new file mode 100644 index 0000000000000..f0862feed42ca --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/AddBundleProductToWishlistTest.php @@ -0,0 +1,176 @@ +customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); + $this->wishlistFactory = $objectManager->get(WishlistFactory::class); + $this->productRepository = $objectManager->get(ProductRepositoryInterface::class); + } + + /** + * @magentoConfigFixture default_store wishlist/general/active 1 + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Bundle/_files/product_1.php + * + * @throws Exception + */ + public function testAddBundleProductWithOptions(): void + { + $sku = 'bundle-product'; + $product = $this->productRepository->get($sku); + $customerId = 1; + $qty = 2; + $optionQty = 1; + + /** @var Type $typeInstance */ + $typeInstance = $product->getTypeInstance(); + $typeInstance->setStoreFilter($product->getStoreId(), $product); + /** @var Option $option */ + $option = $typeInstance->getOptionsCollection($product)->getFirstItem(); + /** @var Product $selection */ + $selection = $typeInstance->getSelectionsCollection([$option->getId()], $product)->getFirstItem(); + $optionId = $option->getId(); + $selectionId = $selection->getSelectionId(); + $bundleOptions = $this->generateBundleOptionIdV2((int) $optionId, (int) $selectionId, $optionQty); + + $query = $this->getQuery($sku, $qty, $bundleOptions); + $response = $this->graphQlMutation($query, [], '', $this->getHeaderMap()); + $wishlist = $this->wishlistFactory->create()->loadByCustomerId($customerId, true); + /** @var Item $item */ + $item = $wishlist->getItemCollection()->getFirstItem(); + + $this->assertArrayHasKey('addProductsToWishlist', $response); + $this->assertArrayHasKey('wishlist', $response['addProductsToWishlist']); + $response = $response['addProductsToWishlist']['wishlist']; + $this->assertEquals($wishlist->getItemsCount(), $response['items_count']); + $this->assertEquals($wishlist->getSharingCode(), $response['sharing_code']); + $this->assertEquals($wishlist->getUpdatedAt(), $response['updated_at']); + $this->assertEquals($item->getData('qty'), $response['items'][0]['qty']); + $this->assertEquals($item->getDescription(), $response['items'][0]['description']); + $this->assertEquals($item->getAddedAt(), $response['items'][0]['added_at']); + } + + /** + * Authentication header map + * + * @param string $username + * @param string $password + * + * @return array + * + * @throws AuthenticationException + */ + private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array + { + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); + + return ['Authorization' => 'Bearer ' . $customerToken]; + } + + /** + * Returns GraphQl mutation string + * + * @param string $sku + * @param int $qty + * @param string $bundleOptions + * @param int $wishlistId + * + * @return string + */ + private function getQuery( + string $sku, + int $qty, + string $bundleOptions, + int $wishlistId = 0 + ): string { + return <<customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); + $this->wishlistFactory = $objectManager->get(WishlistFactory::class); + } + + /** + * @magentoConfigFixture default_store wishlist/general/active 1 + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php + * + * @throws Exception + */ + public function testAddDownloadableProductWithOptions(): void + { + $product = $this->getConfigurableProductInfo(); + $customerId = 1; + $qty = 2; + $attributeId = (int) $product['configurable_options'][0]['attribute_id']; + $valueIndex = $product['configurable_options'][0]['values'][0]['value_index']; + $childSku = $product['variants'][0]['product']['sku']; + $parentSku = $product['sku']; + $selectedConfigurableOptionsQuery = $this->generateSuperAttributesIdV2Query($attributeId, $valueIndex); + + $query = $this->getQuery($parentSku, $childSku, $qty, $selectedConfigurableOptionsQuery); + + $response = $this->graphQlMutation($query, [], '', $this->getHeadersMap()); + $wishlist = $this->wishlistFactory->create()->loadByCustomerId($customerId, true); + /** @var Item $wishlistItem */ + $wishlistItem = $wishlist->getItemCollection()->getFirstItem(); + + self::assertArrayHasKey('addProductsToWishlist', $response); + self::assertArrayHasKey('wishlist', $response['addProductsToWishlist']); + $wishlistResponse = $response['addProductsToWishlist']['wishlist']; + self::assertEquals($wishlist->getItemsCount(), $wishlistResponse['items_count']); + self::assertEquals($wishlist->getSharingCode(), $wishlistResponse['sharing_code']); + self::assertEquals($wishlist->getUpdatedAt(), $wishlistResponse['updated_at']); + self::assertEquals($wishlistItem->getId(), $wishlistResponse['items'][0]['id']); + self::assertEquals($wishlistItem->getData('qty'), $wishlistResponse['items'][0]['qty']); + self::assertEquals($wishlistItem->getDescription(), $wishlistResponse['items'][0]['description']); + self::assertEquals($wishlistItem->getAddedAt(), $wishlistResponse['items'][0]['added_at']); + } + + /** + * Authentication header map + * + * @param string $username + * @param string $password + * + * @return array + * + * @throws AuthenticationException + */ + private function getHeadersMap(string $username = 'customer@example.com', string $password = 'password'): array + { + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); + + return ['Authorization' => 'Bearer ' . $customerToken]; + } + + /** + * Returns GraphQl mutation string + * + * @param string $parentSku + * @param string $childSku + * @param int $qty + * @param string $customizableOptions + * @param int $wishlistId + * + * @return string + */ + private function getQuery( + string $parentSku, + string $childSku, + int $qty, + string $customizableOptions, + int $wishlistId = 0 + ): string { + return <<graphQlQuery($this->getFetchProductQuery('configurable')); + + return current($searchResponse['products']['items']); + } + + /** + * Returns GraphQl query for fetching configurable product information + * + * @param string $term + * + * @return string + */ + private function getFetchProductQuery(string $term): string + { + return <<objectManager = Bootstrap::getObjectManager(); + $this->customerTokenService = $this->objectManager->get(CustomerTokenServiceInterface::class); + $this->wishlistFactory = $this->objectManager->get(WishlistFactory::class); + $this->getCustomOptionsWithIDV2ForQueryBySku = + $this->objectManager->get(GetCustomOptionsWithIDV2ForQueryBySku::class); + } + + /** + * @magentoConfigFixture default_store wishlist/general/active 1 + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable_with_custom_options.php + */ + public function testAddDownloadableProductWithOptions(): void + { + $customerId = 1; + $sku = 'downloadable-product-with-purchased-separately-links'; + $qty = 2; + $links = $this->getProductsLinks($sku); + $linkId = key($links); + $itemOptions = $this->getCustomOptionsWithIDV2ForQueryBySku->execute($sku); + $itemOptions['selected_options'][] = $this->generateProductLinkSelectedOptions($linkId); + $productOptionsQuery = preg_replace( + '/"([^"]+)"\s*:\s*/', + '$1:', + json_encode($itemOptions) + ); + $query = $this->getQuery($qty, $sku, trim($productOptionsQuery, '{}')); + $response = $this->graphQlMutation($query, [], '', $this->getHeaderMap()); + $wishlist = $this->wishlistFactory->create(); + $wishlist->loadByCustomerId($customerId, true); + /** @var Item $wishlistItem */ + $wishlistItem = $wishlist->getItemCollection()->getFirstItem(); + + self::assertArrayHasKey('addProductsToWishlist', $response); + self::assertArrayHasKey('wishlist', $response['addProductsToWishlist']); + $wishlistResponse = $response['addProductsToWishlist']['wishlist']; + self::assertEquals($wishlist->getItemsCount(), $wishlistResponse['items_count']); + self::assertEquals($wishlist->getSharingCode(), $wishlistResponse['sharing_code']); + self::assertEquals($wishlist->getUpdatedAt(), $wishlistResponse['updated_at']); + self::assertEquals($wishlistItem->getId(), $wishlistResponse['items'][0]['id']); + self::assertEquals($wishlistItem->getData('qty'), $wishlistResponse['items'][0]['qty']); + self::assertEquals($wishlistItem->getDescription(), $wishlistResponse['items'][0]['description']); + self::assertEquals($wishlistItem->getAddedAt(), $wishlistResponse['items'][0]['added_at']); + } + + /** + * @magentoConfigFixture default_store wishlist/general/active 0 + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable_with_custom_options.php + */ + public function testAddDownloadableProductOnDisabledWishlist(): void + { + $qty = 2; + $sku = 'downloadable-product-with-purchased-separately-links'; + $links = $this->getProductsLinks($sku); + $linkId = key($links); + $itemOptions = $this->getCustomOptionsWithIDV2ForQueryBySku->execute($sku); + $itemOptions['selected_options'][] = $this->generateProductLinkSelectedOptions($linkId); + $productOptionsQuery = trim(preg_replace( + '/"([^"]+)"\s*:\s*/', + '$1:', + json_encode($itemOptions) + ), '{}'); + $query = $this->getQuery($qty, $sku, $productOptionsQuery); + $this->expectExceptionMessage('The wishlist is not currently available.'); + $this->graphQlMutation($query, [], '', $this->getHeaderMap()); + } + + /** + * Function returns array of all product's links + * + * @param string $sku + * + * @return array + */ + private function getProductsLinks(string $sku): array + { + $result = []; + /** @var ProductRepositoryInterface $productRepository */ + $productRepository = $this->objectManager->get(ProductRepositoryInterface::class); + $product = $productRepository->get($sku, false, null, true); + + foreach ($product->getDownloadableLinks() as $linkObject) { + $result[$linkObject->getLinkId()] = [ + 'title' => $linkObject->getTitle(), + 'price' => $linkObject->getPrice(), + ]; + } + + return $result; + } + + /** + * Authentication header map + * + * @param string $username + * @param string $password + * + * @return array + * + * @throws AuthenticationException + */ + private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array + { + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); + + return ['Authorization' => 'Bearer ' . $customerToken]; + } + + /** + * Returns GraphQl mutation string + * + * @param int $qty + * @param string $sku + * @param string $customizableOptions + * + * @return string + */ + private function getQuery( + int $qty, + string $sku, + string $customizableOptions + ): string { + return <<customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); + } + + /** + * @magentoConfigFixture default_store wishlist/general/active 1 + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Wishlist/_files/wishlist_with_simple_product.php + */ + public function testDeleteWishlistItemFromWishlist(): void + { + $wishlist = $this->getWishlist(); + $wishlistId = $wishlist['customer']['wishlist']['id']; + $wishlist = $wishlist['customer']['wishlist']; + $wishlistItems = $wishlist['items']; + self::assertEquals(1, $wishlist['items_count']); + + $query = $this->getQuery((int) $wishlistId, (int) $wishlistItems[0]['id']); + $response = $this->graphQlMutation($query, [], '', $this->getHeaderMap()); + + self::assertArrayHasKey('removeProductsFromWishlist', $response); + self::assertArrayHasKey('wishlist', $response['removeProductsFromWishlist']); + $wishlistResponse = $response['removeProductsFromWishlist']['wishlist']; + self::assertEquals(0, $wishlistResponse['items_count']); + self::assertEmpty($wishlistResponse['items']); + } + + /** + * Authentication header map + * + * @param string $username + * @param string $password + * + * @return array + * + * @throws AuthenticationException + */ + private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array + { + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); + + return ['Authorization' => 'Bearer ' . $customerToken]; + } + + /** + * Returns GraphQl mutation string + * + * @param int $wishlistId + * @param int $wishlistItemId + * + * @return string + */ + private function getQuery( + int $wishlistId, + int $wishlistItemId + ): string { + return <<graphQlQuery($this->getCustomerWishlistQuery(), [], '', $this->getHeaderMap()); + } + + /** + * Get customer wishlist query + * + * @return string + */ + private function getCustomerWishlistQuery(): string + { + return <<productCustomOptionRepository = $productCustomOptionRepository; + } + + /** + * Returns array of custom options for the product + * + * @param string $sku + * + * @return array + */ + public function execute(string $sku): array + { + $customOptions = $this->productCustomOptionRepository->getList($sku); + $selectedOptions = []; + $enteredOptions = []; + + foreach ($customOptions as $customOption) { + $optionType = $customOption->getType(); + + if ($optionType === 'field' || $optionType === 'area' || $optionType === 'date') { + $enteredOptions[] = [ + 'id' => $this->encodeEnteredOption((int)$customOption->getOptionId()), + 'value' => '2012-12-12' + ]; + } elseif ($optionType === 'drop_down') { + $optionSelectValues = $customOption->getValues(); + $selectedOptions[] = $this->encodeSelectedOption( + (int)$customOption->getOptionId(), + (int)reset($optionSelectValues)->getOptionTypeId() + ); + } elseif ($optionType === 'multiple') { + foreach ($customOption->getValues() as $optionValue) { + $selectedOptions[] = $this->encodeSelectedOption( + (int)$customOption->getOptionId(), + (int)$optionValue->getOptionTypeId() + ); + } + } + } + + return [ + 'selected_options' => $selectedOptions, + 'entered_options' => $enteredOptions + ]; + } + + /** + * Returns id_v2 of the selected custom option + * + * @param int $optionId + * @param int $optionValueId + * + * @return string + */ + private function encodeSelectedOption(int $optionId, int $optionValueId): string + { + return base64_encode("custom-option/$optionId/$optionValueId"); + } + + /** + * Returns id_v2 of the entered custom option + * + * @param int $optionId + * + * @return string + */ + private function encodeEnteredOption(int $optionId): string + { + return base64_encode("custom-option/$optionId"); + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/UpdateProductsFromWishlistTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/UpdateProductsFromWishlistTest.php new file mode 100644 index 0000000000000..2bd1f8bab4b1a --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/UpdateProductsFromWishlistTest.php @@ -0,0 +1,159 @@ +customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); + } + + /** + * @magentoConfigFixture default_store wishlist/general/active 1 + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Wishlist/_files/wishlist_with_simple_product.php + */ + public function testUpdateSimpleProductFromWishlist(): void + { + $wishlist = $this->getWishlist(); + $qty = 5; + $description = 'New Description'; + $wishlistId = $wishlist['customer']['wishlist']['id']; + $wishlistItem = $wishlist['customer']['wishlist']['items'][0]; + self::assertNotEquals($description, $wishlistItem['description']); + self::assertNotEquals($qty, $wishlistItem['qty']); + + $query = $this->getQuery((int) $wishlistId, (int) $wishlistItem['id'], $qty, $description); + $response = $this->graphQlMutation($query, [], '', $this->getHeaderMap()); + + self::assertArrayHasKey('updateProductsInWishlist', $response); + self::assertArrayHasKey('wishlist', $response['updateProductsInWishlist']); + $wishlistResponse = $response['updateProductsInWishlist']['wishlist']; + self::assertEquals($qty, $wishlistResponse['items'][0]['qty']); + self::assertEquals($description, $wishlistResponse['items'][0]['description']); + } + + /** + * Authentication header map + * + * @param string $username + * @param string $password + * + * @return array + * + * @throws AuthenticationException + */ + private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array + { + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); + + return ['Authorization' => 'Bearer ' . $customerToken]; + } + + /** + * Returns GraphQl mutation string + * + * @param int $wishlistId + * @param int $wishlistItemId + * @param int $qty + * @param string $description + * + * @return string + */ + private function getQuery( + int $wishlistId, + int $wishlistItemId, + int $qty, + string $description + ): string { + return <<graphQlQuery($this->getCustomerWishlistQuery(), [], '', $this->getHeaderMap()); + } + + /** + * Get customer wishlist query + * + * @return string + */ + private function getCustomerWishlistQuery(): string + { + return << Date: Mon, 25 May 2020 13:12:31 +0300 Subject: [PATCH 3/9] Implementing Todos and adjusting the tests --- .../Model/Wishlist/AddProductsToWishlist.php | 4 ++- .../Wishlist/RemoveProductsFromWishlist.php | 2 ++ .../Wishlist/UpdateProductsInWishlist.php | 4 ++- .../Resolver/CustomerWishlistResolver.php | 17 ++++++++-- .../Model/Resolver/WishlistResolver.php | 17 ++++++++-- .../GraphQl/Wishlist/CustomerWishlistTest.php | 32 +++++++++++++++++++ .../Magento/GraphQl/Wishlist/WishlistTest.php | 2 ++ 7 files changed, 72 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/Wishlist/Model/Wishlist/AddProductsToWishlist.php b/app/code/Magento/Wishlist/Model/Wishlist/AddProductsToWishlist.php index 6700f1585acb4..7acfb503a5ad0 100644 --- a/app/code/Magento/Wishlist/Model/Wishlist/AddProductsToWishlist.php +++ b/app/code/Magento/Wishlist/Model/Wishlist/AddProductsToWishlist.php @@ -94,8 +94,10 @@ public function execute(Wishlist $wishlist, array $wishlistItems): WishlistOutpu * * @param Wishlist $wishlist * @param WishlistItem $wishlistItem + * + * @return void */ - private function addItemToWishlist(Wishlist $wishlist, WishlistItem $wishlistItem) + private function addItemToWishlist(Wishlist $wishlist, WishlistItem $wishlistItem): void { $sku = $wishlistItem->getParentSku() ?? $wishlistItem->getSku(); diff --git a/app/code/Magento/Wishlist/Model/Wishlist/RemoveProductsFromWishlist.php b/app/code/Magento/Wishlist/Model/Wishlist/RemoveProductsFromWishlist.php index c213c048fd61a..d143830064752 100644 --- a/app/code/Magento/Wishlist/Model/Wishlist/RemoveProductsFromWishlist.php +++ b/app/code/Magento/Wishlist/Model/Wishlist/RemoveProductsFromWishlist.php @@ -73,6 +73,8 @@ public function execute(Wishlist $wishlist, array $wishlistItemsIds): WishlistOu * Remove product item from wishlist * * @param int $wishlistItemId + * + * @return void */ private function removeItemFromWishlist(int $wishlistItemId): void { diff --git a/app/code/Magento/Wishlist/Model/Wishlist/UpdateProductsInWishlist.php b/app/code/Magento/Wishlist/Model/Wishlist/UpdateProductsInWishlist.php index 691e00090373a..4abcada138362 100644 --- a/app/code/Magento/Wishlist/Model/Wishlist/UpdateProductsInWishlist.php +++ b/app/code/Magento/Wishlist/Model/Wishlist/UpdateProductsInWishlist.php @@ -84,8 +84,10 @@ public function execute(Wishlist $wishlist, array $wishlistItems): WishlistOutpu * * @param Wishlist $wishlist * @param WishlistItemData $wishlistItemData + * + * @return void */ - private function updateItemInWishlist(Wishlist $wishlist, WishlistItemData $wishlistItemData) + private function updateItemInWishlist(Wishlist $wishlist, WishlistItemData $wishlistItemData): void { try { $options = $this->buyRequestBuilder->build($wishlistItemData); diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistResolver.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistResolver.php index 94d543d25aa7a..cad574ef56ed2 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistResolver.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistResolver.php @@ -9,9 +9,11 @@ use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; +use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Wishlist\Model\Wishlist; +use Magento\Wishlist\Model\Wishlist\Config as WishlistConfig; use Magento\Wishlist\Model\WishlistFactory; /** @@ -24,13 +26,21 @@ class CustomerWishlistResolver implements ResolverInterface */ private $wishlistFactory; + /** + * @var WishlistConfig + */ + private $wishlistConfig; + /** * @param WishlistFactory $wishlistFactory + * @param WishlistConfig $wishlistConfig */ public function __construct( - WishlistFactory $wishlistFactory + WishlistFactory $wishlistFactory, + WishlistConfig $wishlistConfig ) { $this->wishlistFactory = $wishlistFactory; + $this->wishlistConfig = $wishlistConfig; } /** @@ -43,7 +53,10 @@ public function resolve( array $value = null, array $args = null ) { - // Todo: Check if wishlist is enabled + if (!$this->wishlistConfig->isEnabled()) { + throw new GraphQlInputException(__('The wishlist is not currently available.')); + } + if (false === $context->getExtensionAttributes()->getIsCustomer()) { throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.')); } diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/WishlistResolver.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/WishlistResolver.php index d0d409abd1698..09c0a8a935a6c 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/WishlistResolver.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/WishlistResolver.php @@ -8,10 +8,12 @@ namespace Magento\WishlistGraphQl\Model\Resolver; use Magento\Framework\GraphQl\Config\Element\Field; +use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResourceModel; use Magento\Wishlist\Model\Wishlist; +use Magento\Wishlist\Model\Wishlist\Config as WishlistConfig; use Magento\Wishlist\Model\WishlistFactory; use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; @@ -30,16 +32,24 @@ class WishlistResolver implements ResolverInterface */ private $wishlistFactory; + /** + * @var WishlistConfig + */ + private $wishlistConfig; + /** * @param WishlistResourceModel $wishlistResource * @param WishlistFactory $wishlistFactory + * @param WishlistConfig $wishlistConfig */ public function __construct( WishlistResourceModel $wishlistResource, - WishlistFactory $wishlistFactory + WishlistFactory $wishlistFactory, + WishlistConfig $wishlistConfig ) { $this->wishlistResource = $wishlistResource; $this->wishlistFactory = $wishlistFactory; + $this->wishlistConfig = $wishlistConfig; } /** @@ -52,7 +62,10 @@ public function resolve( array $value = null, array $args = null ) { - // Todo: Check if wishlist is enabled + if (!$this->wishlistConfig->isEnabled()) { + throw new GraphQlInputException(__('The wishlist is not currently available.')); + } + $customerId = $context->getUserId(); /* Guest checking */ diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistTest.php index 2208f904320d9..0a8e1757a2ce2 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistTest.php @@ -32,6 +32,7 @@ protected function setUp(): void } /** + * @magentoConfigFixture default_store wishlist/general/active 1 * @magentoApiDataFixture Magento/Wishlist/_files/wishlist.php */ public function testCustomerWishlist(): void @@ -74,6 +75,7 @@ public function testCustomerWishlist(): void } /** + * @magentoConfigFixture default_store wishlist/general/active 1 * @magentoApiDataFixture Magento/Customer/_files/customer.php */ public function testCustomerAlwaysHasWishlist(): void @@ -100,6 +102,7 @@ public function testCustomerAlwaysHasWishlist(): void } /** + * @magentoConfigFixture default_store wishlist/general/active 1 */ public function testGuestCannotGetWishlist() { @@ -121,6 +124,35 @@ public function testGuestCannotGetWishlist() $this->graphQlQuery($query); } + /** + * @magentoConfigFixture default_store wishlist/general/active 0 + * @magentoApiDataFixture Magento/Customer/_files/customer.php + */ + public function testCustomerCannotGetWishlistWhenDisabled() + { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('The wishlist is not currently available.'); + + $query = + <<graphQlQuery( + $query, + [], + '', + $this->getCustomerAuthHeaders('customer@example.com', 'password') + ); + } + /** * @param string $email * @param string $password diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/WishlistTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/WishlistTest.php index bb353938239bc..88c59d6dd8428 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/WishlistTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/WishlistTest.php @@ -39,6 +39,7 @@ protected function setUp(): void } /** + * @magentoConfigFixture default_store wishlist/general/active 1 * @magentoApiDataFixture Magento/Wishlist/_files/wishlist.php */ public function testGetCustomerWishlist(): void @@ -94,6 +95,7 @@ public function testGetCustomerWishlist(): void } /** + * @magentoConfigFixture default_store wishlist/general/active 1 */ public function testGetGuestWishlist() { From 58215aafb22108ecc30337a87ccf80fd1ecb0270 Mon Sep 17 00:00:00 2001 From: eduard13 Date: Tue, 26 May 2020 08:45:07 +0300 Subject: [PATCH 4/9] Fixing Unit Test --- .../Test/Unit/CustomerWishlistResolverTest.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/WishlistGraphQl/Test/Unit/CustomerWishlistResolverTest.php b/app/code/Magento/WishlistGraphQl/Test/Unit/CustomerWishlistResolverTest.php index 8385d3ca852a4..d8d4545661978 100644 --- a/app/code/Magento/WishlistGraphQl/Test/Unit/CustomerWishlistResolverTest.php +++ b/app/code/Magento/WishlistGraphQl/Test/Unit/CustomerWishlistResolverTest.php @@ -14,6 +14,7 @@ use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\GraphQl\Model\Query\ContextExtensionInterface; use Magento\Wishlist\Model\Wishlist; +use Magento\Wishlist\Model\Wishlist\Config; use Magento\Wishlist\Model\WishlistFactory; use Magento\WishlistGraphQl\Model\Resolver\CustomerWishlistResolver; use PHPUnit\Framework\MockObject\MockObject; @@ -48,6 +49,11 @@ class CustomerWishlistResolverTest extends TestCase */ private $resolver; + /** + * @var Config|MockObject + */ + private $wishlistConfigMock; + /** * Build the Testing Environment */ @@ -74,9 +80,14 @@ protected function setUp(): void ->setMethods(['loadByCustomerId', 'getId', 'getSharingCode', 'getUpdatedAt', 'getItemsCount']) ->getMock(); + $this->wishlistConfigMock = $this->createMock(Config::class); + + $this->wishlistConfigMock = $this->getMockClass(); + $objectManager = new ObjectManager($this); $this->resolver = $objectManager->getObject(CustomerWishlistResolver::class, [ - 'wishlistFactory' => $this->wishlistFactoryMock + 'wishlistFactory' => $this->wishlistFactoryMock, + 'wishlistConfig' => $this->wishlistConfigMock ]); } @@ -85,6 +96,8 @@ protected function setUp(): void */ public function testThrowExceptionWhenUserNotAuthorized(): void { + $this->wishlistConfigMock->method('isEnabled')->willReturn(true); + // Given $this->extensionAttributesMock->method('getIsCustomer') ->willReturn(false); @@ -107,6 +120,8 @@ public function testThrowExceptionWhenUserNotAuthorized(): void */ public function testFactoryCreatesWishlistByAuthorizedCustomerId(): void { + $this->wishlistConfigMock->method('isEnabled')->willReturn(true); + // Given $this->extensionAttributesMock->method('getIsCustomer') ->willReturn(true); From 73650e3c4259dfe1c2bb29daa29d55cbc6462682 Mon Sep 17 00:00:00 2001 From: eduard13 Date: Tue, 26 May 2020 09:30:01 +0300 Subject: [PATCH 5/9] Fixing Unit Test --- .../WishlistGraphQl/Test/Unit/CustomerWishlistResolverTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/code/Magento/WishlistGraphQl/Test/Unit/CustomerWishlistResolverTest.php b/app/code/Magento/WishlistGraphQl/Test/Unit/CustomerWishlistResolverTest.php index d8d4545661978..017462b4c94c6 100644 --- a/app/code/Magento/WishlistGraphQl/Test/Unit/CustomerWishlistResolverTest.php +++ b/app/code/Magento/WishlistGraphQl/Test/Unit/CustomerWishlistResolverTest.php @@ -82,8 +82,6 @@ protected function setUp(): void $this->wishlistConfigMock = $this->createMock(Config::class); - $this->wishlistConfigMock = $this->getMockClass(); - $objectManager = new ObjectManager($this); $this->resolver = $objectManager->getObject(CustomerWishlistResolver::class, [ 'wishlistFactory' => $this->wishlistFactoryMock, From a5cc7903b1eeda59df56bb3683d2823fc7c880ad Mon Sep 17 00:00:00 2001 From: eduard13 Date: Tue, 2 Jun 2020 10:26:53 +0300 Subject: [PATCH 6/9] Improving checking the customer wishlist --- .../Model/Resolver/AddProductsToWishlist.php | 6 +++--- .../Resolver/RemoveProductsFromWishlist.php | 18 +++++++++++------- .../Resolver/UpdateProductsInWishlist.php | 15 ++++++++------- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlist.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlist.php index 7c2e7c304c53d..5a44242bdcd24 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlist.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlist.php @@ -88,7 +88,7 @@ public function resolve( $customerId = $context->getUserId(); /* Guest checking */ - if (!$customerId && 0 === $customerId) { + if (null === $customerId || 0 === $customerId) { throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist')); } @@ -102,8 +102,8 @@ public function resolve( $wishlist->loadByCustomerId($customerId, true); } - if (null === $wishlist->getId()) { - throw new GraphQlInputException(__('Something went wrong while creating the wishlist')); + if (null === $wishlist->getId() || $customerId !== (int) $wishlist->getCustomerId()) { + throw new GraphQlInputException(__('The wishlist was not found.')); } $wishlistItems = []; diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlist.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlist.php index 01e60c21190fe..d493c08dfb671 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlist.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlist.php @@ -87,20 +87,24 @@ public function resolve( $customerId = $context->getUserId(); /* Guest checking */ - if (!$customerId && 0 === $customerId) { + if (null === $customerId || 0 === $customerId) { throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist')); } - $wishlistId = $args['wishlist_id']; + $wishlistId = $args['wishlist_id'] ?: null; + $wishlist = $this->wishlistFactory->create(); - if (!$wishlistId) { - throw new GraphQlInputException(__('The wishlist id is missing.')); + if ($wishlistId) { + $this->wishlistResource->load($wishlist, $wishlistId); + } elseif ($customerId) { + $wishlist->loadByCustomerId($customerId, true); } - $wishlist = $this->wishlistFactory->create(); - $this->wishlistResource->load($wishlist, $wishlistId); + if ($wishlistId) { + $this->wishlistResource->load($wishlist, $wishlistId); + } - if (!$wishlist) { + if (null === $wishlist->getId() || $customerId !== (int) $wishlist->getCustomerId()) { throw new GraphQlInputException(__('The wishlist was not found.')); } diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlist.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlist.php index e8483cf6391b6..8a0411e71642f 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlist.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlist.php @@ -88,23 +88,24 @@ public function resolve( $customerId = $context->getUserId(); /* Guest checking */ - if (!$customerId && 0 === $customerId) { + if (null === $customerId || 0 === $customerId) { throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist')); } - $wishlistId = $args['wishlist_id']; + $wishlistId = $args['wishlist_id'] ?: null; + $wishlist = $this->wishlistFactory->create(); - if (!$wishlistId) { - throw new GraphQlInputException(__('The wishlist id is missing.')); + if ($wishlistId) { + $this->wishlistResource->load($wishlist, $wishlistId); + } elseif ($customerId) { + $wishlist->loadByCustomerId($customerId, true); } - $wishlist = $this->wishlistFactory->create(); - if ($wishlistId) { $this->wishlistResource->load($wishlist, $wishlistId); } - if (null === $wishlist->getId()) { + if (null === $wishlist->getId() || $customerId !== (int) $wishlist->getCustomerId()) { throw new GraphQlInputException(__('The wishlist was not found.')); } From 08bd47efff266a548c4544062ed7544ca1ecde4b Mon Sep 17 00:00:00 2001 From: eduard13 Date: Tue, 2 Jun 2020 12:05:10 +0300 Subject: [PATCH 7/9] Fixing static tests --- .../Model/Resolver/AddProductsToWishlist.php | 59 ++++++++++++----- .../Resolver/RemoveProductsFromWishlist.php | 39 +++++++---- .../Resolver/UpdateProductsInWishlist.php | 65 +++++++++++++------ 3 files changed, 115 insertions(+), 48 deletions(-) diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlist.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlist.php index 5a44242bdcd24..bb36a3408f1f1 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlist.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlist.php @@ -13,6 +13,7 @@ use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResourceModel; +use Magento\Wishlist\Model\Wishlist; use Magento\Wishlist\Model\Wishlist\AddProductsToWishlist as AddProductsToWishlistModel; use Magento\Wishlist\Model\Wishlist\Config as WishlistConfig; use Magento\Wishlist\Model\Wishlist\Data\Error; @@ -92,30 +93,19 @@ public function resolve( throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist')); } - $wishlistId = $args['wishlist_id'] ?: null; - $wishlistItemsData = $args['wishlist_items']; - $wishlist = $this->wishlistFactory->create(); - - if ($wishlistId) { - $this->wishlistResource->load($wishlist, $wishlistId); - } elseif ($customerId) { - $wishlist->loadByCustomerId($customerId, true); - } + $wishlistId = ((int) $args['wishlist_id']) ?: null; + $wishlist = $this->getWishlist($wishlistId, $customerId); if (null === $wishlist->getId() || $customerId !== (int) $wishlist->getCustomerId()) { throw new GraphQlInputException(__('The wishlist was not found.')); } - $wishlistItems = []; - foreach ($wishlistItemsData as $wishlistItemData) { - $wishlistItems[] = (new WishlistItemFactory())->create($wishlistItemData); - } - + $wishlistItems = $this->getWishlistItems($args['wishlist_items']); $wishlistOutput = $this->addProductsToWishlist->execute($wishlist, $wishlistItems); return [ 'wishlist' => $this->wishlistDataMapper->map($wishlistOutput->getWishlist()), - 'userInputErrors' => \array_map( + 'userInputErrors' => array_map( function (Error $error) { return [ 'code' => $error->getCode(), @@ -126,4 +116,43 @@ function (Error $error) { ) ]; } + + /** + * Get wishlist items + * + * @param array $wishlistItemsData + * + * @return array + */ + private function getWishlistItems(array $wishlistItemsData): array + { + $wishlistItems = []; + + foreach ($wishlistItemsData as $wishlistItemData) { + $wishlistItems[] = (new WishlistItemFactory())->create($wishlistItemData); + } + + return $wishlistItems; + } + + /** + * Get customer wishlist + * + * @param int|null $wishlistId + * @param int|null $customerId + * + * @return Wishlist + */ + private function getWishlist(?int $wishlistId, ?int $customerId): Wishlist + { + $wishlist = $this->wishlistFactory->create(); + + if ($wishlistId !== null && $wishlistId > 0) { + $this->wishlistResource->load($wishlist, $wishlistId); + } elseif ($customerId !== null) { + $wishlist->loadByCustomerId($customerId, true); + } + + return $wishlist; + } } diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlist.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlist.php index d493c08dfb671..19c8b795b3d1c 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlist.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlist.php @@ -13,8 +13,10 @@ use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResourceModel; +use Magento\Wishlist\Model\Wishlist; use Magento\Wishlist\Model\Wishlist\Config as WishlistConfig; use Magento\Wishlist\Model\Wishlist\Data\Error; +use Magento\Wishlist\Model\Wishlist\Data\WishlistItemFactory; use Magento\Wishlist\Model\Wishlist\RemoveProductsFromWishlist as RemoveProductsFromWishlistModel; use Magento\Wishlist\Model\WishlistFactory; use Magento\WishlistGraphQl\Mapper\WishlistDataMapper; @@ -87,22 +89,12 @@ public function resolve( $customerId = $context->getUserId(); /* Guest checking */ - if (null === $customerId || 0 === $customerId) { + if ($customerId === null || 0 === $customerId) { throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist')); } - $wishlistId = $args['wishlist_id'] ?: null; - $wishlist = $this->wishlistFactory->create(); - - if ($wishlistId) { - $this->wishlistResource->load($wishlist, $wishlistId); - } elseif ($customerId) { - $wishlist->loadByCustomerId($customerId, true); - } - - if ($wishlistId) { - $this->wishlistResource->load($wishlist, $wishlistId); - } + $wishlistId = ((int) $args['wishlist_id']) ?: null; + $wishlist = $this->getWishlist($wishlistId, $customerId); if (null === $wishlist->getId() || $customerId !== (int) $wishlist->getCustomerId()) { throw new GraphQlInputException(__('The wishlist was not found.')); @@ -128,4 +120,25 @@ function (Error $error) { ) ]; } + + /** + * Get customer wishlist + * + * @param int|null $wishlistId + * @param int|null $customerId + * + * @return Wishlist + */ + private function getWishlist(?int $wishlistId, ?int $customerId): Wishlist + { + $wishlist = $this->wishlistFactory->create(); + + if ($wishlistId !== null && $wishlistId > 0) { + $this->wishlistResource->load($wishlist, $wishlistId); + } elseif ($customerId !== null) { + $wishlist->loadByCustomerId($customerId, true); + } + + return $wishlist; + } } diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlist.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlist.php index 8a0411e71642f..3f895fd87998c 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlist.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlist.php @@ -13,6 +13,7 @@ use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResourceModel; +use Magento\Wishlist\Model\Wishlist; use Magento\Wishlist\Model\Wishlist\Config as WishlistConfig; use Magento\Wishlist\Model\Wishlist\Data\Error; use Magento\Wishlist\Model\Wishlist\Data\WishlistItemFactory; @@ -88,34 +89,19 @@ public function resolve( $customerId = $context->getUserId(); /* Guest checking */ - if (null === $customerId || 0 === $customerId) { + if (null === $customerId || $customerId === 0) { throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist')); } - $wishlistId = $args['wishlist_id'] ?: null; - $wishlist = $this->wishlistFactory->create(); - - if ($wishlistId) { - $this->wishlistResource->load($wishlist, $wishlistId); - } elseif ($customerId) { - $wishlist->loadByCustomerId($customerId, true); - } - - if ($wishlistId) { - $this->wishlistResource->load($wishlist, $wishlistId); - } + $wishlistId = ((int) $args['wishlist_id']) ?: null; + $wishlist = $this->getWishlist($wishlistId, $customerId); if (null === $wishlist->getId() || $customerId !== (int) $wishlist->getCustomerId()) { throw new GraphQlInputException(__('The wishlist was not found.')); } - $wishlistItems = []; - $wishlistItemsData = $args['wishlist_items']; - - foreach ($wishlistItemsData as $wishlistItemData) { - $wishlistItems[] = (new WishlistItemFactory())->create($wishlistItemData); - } - + $wishlistItems = $args['wishlist_items']; + $wishlistItems = $this->getWishlistItems($wishlistItems); $wishlistOutput = $this->updateProductsInWishlist->execute($wishlist, $wishlistItems); if (count($wishlistOutput->getErrors()) !== count($wishlistItems)) { @@ -135,4 +121,43 @@ function (Error $error) { ) ]; } + + /** + * Get DTO wishlist items + * + * @param array $wishlistItemsData + * + * @return array + */ + private function getWishlistItems(array $wishlistItemsData): array + { + $wishlistItems = []; + + foreach ($wishlistItemsData as $wishlistItemData) { + $wishlistItems[] = (new WishlistItemFactory())->create($wishlistItemData); + } + + return $wishlistItems; + } + + /** + * Get customer wishlist + * + * @param int|null $wishlistId + * @param int|null $customerId + * + * @return Wishlist + */ + private function getWishlist(?int $wishlistId, ?int $customerId): Wishlist + { + $wishlist = $this->wishlistFactory->create(); + + if (null !== $wishlistId && 0 < $wishlistId) { + $this->wishlistResource->load($wishlist, $wishlistId); + } elseif ($customerId !== null) { + $wishlist->loadByCustomerId($customerId, true); + } + + return $wishlist; + } } From 476ea33acaaaff969ef41837ed68f1c4b2090d2f Mon Sep 17 00:00:00 2001 From: eduard13 Date: Thu, 11 Jun 2020 13:00:02 +0300 Subject: [PATCH 8/9] Converting the arguments in camelcase format --- .../Model/Resolver/AddProductsToWishlist.php | 4 ++-- .../Model/Resolver/RemoveProductsFromWishlist.php | 4 ++-- .../Model/Resolver/UpdateProductsInWishlist.php | 4 ++-- app/code/Magento/WishlistGraphQl/etc/schema.graphqls | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlist.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlist.php index bb36a3408f1f1..11c8446a72a9d 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlist.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlist.php @@ -93,14 +93,14 @@ public function resolve( throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist')); } - $wishlistId = ((int) $args['wishlist_id']) ?: null; + $wishlistId = ((int) $args['wishlistId']) ?: null; $wishlist = $this->getWishlist($wishlistId, $customerId); if (null === $wishlist->getId() || $customerId !== (int) $wishlist->getCustomerId()) { throw new GraphQlInputException(__('The wishlist was not found.')); } - $wishlistItems = $this->getWishlistItems($args['wishlist_items']); + $wishlistItems = $this->getWishlistItems($args['wishlistItems']); $wishlistOutput = $this->addProductsToWishlist->execute($wishlist, $wishlistItems); return [ diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlist.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlist.php index 19c8b795b3d1c..1c741361ea7b7 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlist.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlist.php @@ -93,14 +93,14 @@ public function resolve( throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist')); } - $wishlistId = ((int) $args['wishlist_id']) ?: null; + $wishlistId = ((int) $args['wishlistId']) ?: null; $wishlist = $this->getWishlist($wishlistId, $customerId); if (null === $wishlist->getId() || $customerId !== (int) $wishlist->getCustomerId()) { throw new GraphQlInputException(__('The wishlist was not found.')); } - $wishlistItemsIds = $args['wishlist_items_ids']; + $wishlistItemsIds = $args['wishlistItemsIds']; $wishlistOutput = $this->removeProductsFromWishlist->execute($wishlist, $wishlistItemsIds); if (!empty($wishlistItemsIds)) { diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlist.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlist.php index 3f895fd87998c..50a56863596c0 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlist.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlist.php @@ -93,14 +93,14 @@ public function resolve( throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist')); } - $wishlistId = ((int) $args['wishlist_id']) ?: null; + $wishlistId = ((int) $args['wishlistId']) ?: null; $wishlist = $this->getWishlist($wishlistId, $customerId); if (null === $wishlist->getId() || $customerId !== (int) $wishlist->getCustomerId()) { throw new GraphQlInputException(__('The wishlist was not found.')); } - $wishlistItems = $args['wishlist_items']; + $wishlistItems = $args['wishlistItems']; $wishlistItems = $this->getWishlistItems($wishlistItems); $wishlistOutput = $this->updateProductsInWishlist->execute($wishlist, $wishlistItems); diff --git a/app/code/Magento/WishlistGraphQl/etc/schema.graphqls b/app/code/Magento/WishlistGraphQl/etc/schema.graphqls index d7a22a52ee1d2..2bd101d1ce21c 100644 --- a/app/code/Magento/WishlistGraphQl/etc/schema.graphqls +++ b/app/code/Magento/WishlistGraphQl/etc/schema.graphqls @@ -34,9 +34,9 @@ type WishlistItem { } type Mutation { - addProductsToWishlist(wishlist_id: ID!, wishlist_items: [WishlistItemInput!]!): AddProductsToWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\AddProductsToWishlist") - removeProductsFromWishlist(wishlist_id: ID!, wishlist_items_ids: [ID!]!): RemoveProductsFromWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\RemoveProductsFromWishlist") - updateProductsInWishlist(wishlist_id: ID!, wishlist_items: [WishlistItemUpdateInput!]!): UpdateProductsInWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\UpdateProductsInWishlist") + addProductsToWishlist(wishlistId: ID!, wishlistItems: [WishlistItemInput!]!): AddProductsToWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\AddProductsToWishlist") + removeProductsFromWishlist(wishlistId: ID!, wishlistItemsIds: [ID!]!): RemoveProductsFromWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\RemoveProductsFromWishlist") + updateProductsInWishlist(wishlistId: ID!, wishlistItems: [WishlistItemUpdateInput!]!): UpdateProductsInWishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\UpdateProductsInWishlist") } input WishlistItemInput { From f1d13b7dc2177e4eeb5b776e438d76fcd7df9d32 Mon Sep 17 00:00:00 2001 From: eduard13 Date: Thu, 11 Jun 2020 13:33:17 +0300 Subject: [PATCH 9/9] Converting the arguments in camelcase format --- .../GraphQl/Wishlist/AddBundleProductToWishlistTest.php | 4 ++-- .../GraphQl/Wishlist/AddConfigurableProductToWishlistTest.php | 4 ++-- .../GraphQl/Wishlist/AddDownloadableProductToWishlistTest.php | 4 ++-- .../GraphQl/Wishlist/DeleteProductsFromWishlistTest.php | 4 ++-- .../GraphQl/Wishlist/UpdateProductsFromWishlistTest.php | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/AddBundleProductToWishlistTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/AddBundleProductToWishlistTest.php index f0862feed42ca..c0199e8908d0e 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/AddBundleProductToWishlistTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/AddBundleProductToWishlistTest.php @@ -129,8 +129,8 @@ private function getQuery( return <<