|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Customer\Controller\Adminhtml\Wishlist\Product\Composite\Wishlist; |
| 9 | + |
| 10 | +use Magento\Backend\Model\Session; |
| 11 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 12 | +use Magento\Framework\App\Request\Http as HttpRequest; |
| 13 | +use Magento\Framework\Serialize\SerializerInterface; |
| 14 | +use Magento\TestFramework\TestCase\AbstractBackendController; |
| 15 | +use Magento\TestFramework\Wishlist\Model\GetWishlistByCustomerId; |
| 16 | +use Magento\Wishlist\Model\Item; |
| 17 | + |
| 18 | +/** |
| 19 | + * Tests for update wish list items. |
| 20 | + * |
| 21 | + * @magentoAppArea adminhtml |
| 22 | + */ |
| 23 | +class UpdateTest extends AbstractBackendController |
| 24 | +{ |
| 25 | + /** @var GetWishlistByCustomerId */ |
| 26 | + private $getWishlistByCustomerId; |
| 27 | + |
| 28 | + /** @var SerializerInterface */ |
| 29 | + private $json; |
| 30 | + |
| 31 | + /** @var Session */ |
| 32 | + private $session; |
| 33 | + |
| 34 | + /** |
| 35 | + * @inheritdoc |
| 36 | + */ |
| 37 | + protected function setUp(): void |
| 38 | + { |
| 39 | + parent::setUp(); |
| 40 | + |
| 41 | + $this->getWishlistByCustomerId = $this->_objectManager->get(GetWishlistByCustomerId::class); |
| 42 | + $this->json = $this->_objectManager->get(SerializerInterface::class); |
| 43 | + $this->session = $this->_objectManager->get(Session::class); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @magentoDataFixture Magento/Wishlist/_files/wishlist_with_simple_product.php |
| 48 | + * @magentoDbIsolation disabled |
| 49 | + * |
| 50 | + * @return void |
| 51 | + */ |
| 52 | + public function testUpdateItem(): void |
| 53 | + { |
| 54 | + $item = $this->getWishlistByCustomerId->getItemBySku(1, 'simple-1'); |
| 55 | + $this->assertNotNull($item); |
| 56 | + $params = ['id' => $item->getId(), 'qty' => 5]; |
| 57 | + $this->dispatchUpdateItemRequest($params); |
| 58 | + $this->assertEquals($params['qty'], $this->getWishlistByCustomerId->getItemBySku(1, 'simple-1')->getQty()); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @magentoDataFixture Magento/Wishlist/_files/wishlist_with_configurable_product.php |
| 63 | + * @magentoDbIsolation disabled |
| 64 | + * |
| 65 | + * @return void |
| 66 | + */ |
| 67 | + public function testUpdateItemOption(): void |
| 68 | + { |
| 69 | + $item = $this->getWishlistByCustomerId->getItemBySku(1, 'Configurable product'); |
| 70 | + $this->assertNotNull($item); |
| 71 | + $params = [ |
| 72 | + 'id' => $item->getId(), |
| 73 | + 'super_attribute' => $this->performConfigurableOption($item->getProduct()), |
| 74 | + 'qty' => 5, |
| 75 | + ]; |
| 76 | + $this->dispatchUpdateItemRequest($params); |
| 77 | + $this->assertUpdatedItem( |
| 78 | + $this->getWishlistByCustomerId->getItemBySku(1, 'Configurable product'), |
| 79 | + $params |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @return void |
| 85 | + */ |
| 86 | + public function testUpdateNotExistingItem(): void |
| 87 | + { |
| 88 | + $this->dispatchUpdateItemRequest(['id' => 989]); |
| 89 | + $this->assertTrue($this->session->getCompositeProductResult()->getError()); |
| 90 | + $this->assertEquals( |
| 91 | + (string)__('Please load Wish List item.'), |
| 92 | + $this->session->getCompositeProductResult()->getMessage() |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @return void |
| 98 | + */ |
| 99 | + public function testUpdateWithoutParams(): void |
| 100 | + { |
| 101 | + $this->dispatchUpdateItemRequest([]); |
| 102 | + $this->assertTrue($this->session->getCompositeProductResult()->getError()); |
| 103 | + $this->assertEquals( |
| 104 | + (string)__('Please define Wish List item ID.'), |
| 105 | + $this->session->getCompositeProductResult()->getMessage() |
| 106 | + ); |
| 107 | + } |
| 108 | + /** |
| 109 | + * Assert updated item in wish list. |
| 110 | + * |
| 111 | + * @param Item $item |
| 112 | + * @param array $expectedData |
| 113 | + * @return void |
| 114 | + */ |
| 115 | + private function assertUpdatedItem(Item $item, array $expectedData): void |
| 116 | + { |
| 117 | + $this->assertEquals($expectedData['qty'], $item->getQty()); |
| 118 | + $buyRequestOption = $this->json->unserialize($item->getOptionByCode('info_buyRequest')->getValue()); |
| 119 | + foreach ($expectedData as $key => $value) { |
| 120 | + $this->assertEquals($value, $buyRequestOption[$key]); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Perform configurable option to select. |
| 126 | + * |
| 127 | + * @param ProductInterface $product |
| 128 | + * @return array |
| 129 | + */ |
| 130 | + private function performConfigurableOption(ProductInterface $product): array |
| 131 | + { |
| 132 | + $configurableOptions = $product->getTypeInstance()->getConfigurableOptions($product); |
| 133 | + $attributeId = key($configurableOptions); |
| 134 | + $option = reset($configurableOptions[$attributeId]); |
| 135 | + |
| 136 | + return [$attributeId => $option['value_index']]; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Dispatch update wish list item request. |
| 141 | + * |
| 142 | + * @param array $params |
| 143 | + * @return void |
| 144 | + */ |
| 145 | + private function dispatchUpdateItemRequest(array $params): void |
| 146 | + { |
| 147 | + $this->getRequest()->setParams($params)->setMethod(HttpRequest::METHOD_POST); |
| 148 | + $this->dispatch('backend/customer/wishlist_product_composite_wishlist/update'); |
| 149 | + $this->assertRedirect($this->stringContains('backend/catalog/product/showUpdateResult/')); |
| 150 | + } |
| 151 | +} |
0 commit comments