This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,185 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Wishlist\Model;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product\Exception as ProductException;
use Magento\Checkout\Model\CartFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObjectFactory;
use Magento\TestFramework\Wishlist\Model\GetWishlistByCustomerId;
use Magento\Wishlist\Model\Item\OptionFactory;
use PHPUnit\Framework\TestCase;
/**
* Tests for wish list item model.
*
* @magentoDbIsolation enabled
* @magentoAppIsolation disabled
*/
class ItemTest extends TestCase
{
/** @var ObjectManager */
private $objectManager;
/** @var Item */
private $model;
/** @var DataObjectFactory */
private $dataObjectFactory;
/** @var OptionFactory */
private $optionFactory;
/** @var GetWishlistByCustomerId */
private $getWishlistByCustomerId;
/** @var CartFactory */
private $cartFactory;
/** @var ProductRepositoryInterface */
private $productRepository;
/** @var ItemFactory */
private $itemFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = ObjectManager::getInstance();
$this->dataObjectFactory = $this->objectManager->get(DataObjectFactory::class);
$this->model = $this->objectManager->get(Item::class);
$this->itemFactory = $this->objectManager->get(ItemFactory::class);
$this->optionFactory = $this->objectManager->get(OptionFactory::class);
$this->getWishlistByCustomerId = $this->objectManager->get(GetWishlistByCustomerId::class);
$this->cartFactory = $this->objectManager->get(CartFactory::class);
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$this->productRepository->cleanCache();
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->cartFactory->create()->truncate();
parent::tearDown();
}
/**
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
*
* @return void
*/
public function testBuyRequest(): void
{
$product = $this->productRepository->get('simple');
$option = $this->optionFactory->create(
['data' => ['code' => 'info_buyRequest', 'value' => '{"qty":23}']]
);
$option->setProduct($product);
$this->model->addOption($option);
$buyRequest = $this->model->getBuyRequest();
$this->assertEquals($buyRequest->getOriginalQty(), 23);
$this->model->mergeBuyRequest(['qty' => 11, 'additional_data' => 'some value']);
$buyRequest = $this->model->getBuyRequest();
$this->assertEquals(
['additional_data' => 'some value', 'qty' => 0, 'original_qty' => 11],
$buyRequest->getData()
);
}
/**
* @return void
*/
public function testSetBuyRequest(): void
{
$buyRequest = $this->dataObjectFactory->create(
['data' => ['field_1' => 'some data', 'field_2' => 234]]
);
$this->model->setBuyRequest($buyRequest);
$this->assertJsonStringEqualsJsonString(
'{"field_1":"some data","field_2":234,"id":null}',
$this->model->getData('buy_request')
);
}
/**
* @magentoDataFixture Magento/Wishlist/_files/wishlist_with_simple_product.php
* @magentoDbIsolation disabled
*
* @return void
*/
public function testAddItemToCart(): void
{
$item = $this->getWishlistByCustomerId->getItemBySku(1, 'simple-1');
$this->assertNotNull($item);
$cart = $this->cartFactory->create();
$this->assertTrue($item->addToCart($cart));
$this->assertCount(1, $cart->getQuote()->getItemsCollection());
$this->assertCount(1, $this->getWishlistByCustomerId->execute(1)->getItemCollection());
}
/**
* @magentoDataFixture Magento/Wishlist/_files/wishlist_with_simple_product.php
* @magentoDbIsolation disabled
*
* @return void
*/
public function testAddItemToCartAndDeleteFromWishList(): void
{
$item = $this->getWishlistByCustomerId->getItemBySku(1, 'simple-1');
$this->assertNotNull($item);
$cart = $this->cartFactory->create();
$item->addToCart($cart, true);
$this->assertCount(1, $cart->getQuote()->getItemsCollection());
$this->assertCount(0, $this->getWishlistByCustomerId->execute(1)->getItemCollection());
}
/**
* @magentoDataFixture Magento/Catalog/_files/product_simple_out_of_stock.php
*
* @return void
*/
public function testAddOutOfStockItemToCart(): void
{
$product = $this->productRepository->get('simple-out-of-stock');
$item = $this->itemFactory->create()->setProduct($product);
$this->expectExceptionObject(new ProductException(__('Product is not salable.')));
$item->addToCart($this->cartFactory->create());
}
/**
* @magentoDataFixture Magento/Catalog/_files/multiple_products.php
*
* @return void
*/
public function testAddDisabledItemToCart(): void
{
$product = $this->productRepository->get('simple3');
$item = $this->itemFactory->create()->setProduct($product);
$this->assertFalse($item->addToCart($this->cartFactory->create()));
}
/**
* @magentoDataFixture Magento/Catalog/_files/simple_products_not_visible_individually.php
*
* @return void
*/
public function testAddNotVisibleItemToCart(): void
{
$product = $this->productRepository->get('simple_not_visible_1');
$item = $this->itemFactory->create()->setProduct($product)->setStoreId($product->getStoreId());
$this->assertFalse($item->addToCart($this->cartFactory->create()));
}
}
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Wishlist\Model\ResourceModel\Item\Collection;
use Magento\Customer\Controller\RegistryConstants;
use Magento\Customer\Model\Customer;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Registry;
use Magento\Store\Model\Website;
use PHPUnit\Framework\TestCase;
/**
* Class to test wishlist collection by customer functionality
*
* @magentoAppArea adminhtml
*/
class GridTest extends TestCase
{
/**
* @var ObjectManager
*/
private $objectManager;
/**
* @var Registry
*/
private $registryManager;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = ObjectManager::getInstance();
$this->registryManager = $this->objectManager->get(Registry::class);
}
/**
* Test to load wishlist collection by customer on second website
*
* @magentoDbIsolation disabled
* @magentoDataFixture Magento/Wishlist/_files/wishlist_on_second_website.php
*/
public function testLoadOnSecondWebsite()
{
$customer = $this->loadCustomer();
$this->registryManager->register(RegistryConstants::CURRENT_CUSTOMER_ID, $customer->getId());
$gridCollection = $this->objectManager->get(Grid::class);
$this->assertNotEmpty($gridCollection->getItems());
}
/**
* Load customer in second website
*
* @return Customer
*/
private function loadCustomer(): Customer
{
/** @var $website Website */
$website = $this->objectManager->get(Website::class);
$website->load('newwebsite', 'code');
/** @var Customer $customer */
$customer = $this->objectManager->get(Customer::class);
$customer->setWebsiteId($website->getId());
$customer->loadByEmail('customer2@example.com');
return $customer;
}
}
@@ -0,0 +1,91 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Wishlist\Model\ResourceModel\Item;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\ObjectManager;
use Magento\Wishlist\Model\Wishlist;
use Magento\Catalog\Model\Attribute\Config;
class CollectionTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ObjectManager
*/
private $objectManager;
/**
* @var Collection
*/
private $itemCollection;
/**
* @var Wishlist
*/
private $wishlist;
/**
* @var Config\Data
*/
private $attributeConfig;
protected function setUp(): void
{
$this->objectManager = ObjectManager::getInstance();
$this->wishlist = $this->objectManager->create(Wishlist::class);
$this->itemCollection = $this->objectManager->get(Collection::class);
$this->attributeConfig = $this->objectManager->get(Config\Data::class);
}
/**
* Verify that Wishlist Item Collection uses Catalog Attributes defined in the configuration.
*
* @magentoDataFixture Magento/Wishlist/_files/wishlist_shared.php
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
public function testLoadedProductAttributes()
{
$this->addAttributesToWishlistConfig([
'short_description',
]);
$this->wishlist->loadByCode('fixture_unique_code');
$this->itemCollection->addWishlistFilter($this->wishlist);
/** @var Product $productOnWishlist */
$productOnWishlist = $this->itemCollection->getFirstItem()->getProduct();
$this->assertEquals('Simple Product', $productOnWishlist->getName());
$this->assertEquals('Short description', $productOnWishlist->getData('short_description'));
}
/**
* Tests collection load.
* Tests collection load method when product salable filter flag is setted to true
* and few products are present.
*
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
* @magentoDataFixture Magento/Wishlist/_files/wishlist.php
* @magentoDbIsolation disabled
*/
public function testLoadWhenFewProductsPresent()
{
$this->itemCollection->setSalableFilter(true);
$this->itemCollection->addCustomerIdFilter(1);
$this->itemCollection->load();
$this->assertCount(1, $this->itemCollection->getItems());
}
/**
* @param array $attributes
*/
private function addAttributesToWishlistConfig($attributes)
{
$this->attributeConfig->merge([
'wishlist_item' => $attributes
]);
}
}
@@ -0,0 +1,307 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Wishlist\Model;
use Magento\Bundle\Model\Product\OptionList;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product\Attribute\Source\Status as ProductStatus;
use Magento\CatalogInventory\Model\StockStateException;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObjectFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\TestFramework\Wishlist\Model\GetWishlistByCustomerId;
use PHPUnit\Framework\TestCase;
/**
* Tests for wish list model.
*
* @magentoDbIsolation disabled
* @magentoAppIsolation disabled
*/
class WishlistTest extends TestCase
{
/** @var ObjectManager */
private $objectManager;
/** @var WishlistFactory */
private $wishlistFactory;
/** @var GetWishlistByCustomerId */
private $getWishlistByCustomerId;
/** @var ProductRepositoryInterface */
private $productRepository;
/** @var DataObjectFactory */
private $dataObjectFactory;
/** @var SerializerInterface */
private $json;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = ObjectManager::getInstance();
$this->wishlistFactory = $this->objectManager->get(WishlistFactory::class);
$this->getWishlistByCustomerId = $this->objectManager->get(GetWishlistByCustomerId::class);
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$this->productRepository->cleanCache();
$this->dataObjectFactory = $this->objectManager->get(DataObjectFactory::class);
$this->json = $this->objectManager->get(SerializerInterface::class);
}
/**
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoDataFixture Magento/Customer/_files/customer.php
*
* @return void
*/
public function testAddNewItem(): void
{
$productSku = 'simple';
$customerId = 1;
$product = $this->productRepository->get($productSku);
$wishlist = $this->getWishlistByCustomerId->execute($customerId);
$wishlist->addNewItem(
$product,
'{"qty":2}'
);
$wishlist->addNewItem(
$product,
['qty' => 3]
);
$wishlist->addNewItem(
$product,
$this->dataObjectFactory->create(['data' => ['qty' => 4]])
);
$wishlist->addNewItem($product);
$wishlistItem = $this->getWishlistByCustomerId->getItemBySku(1, $productSku);
$this->assertInstanceOf(Item::class, $wishlistItem);
$this->assertEquals($wishlistItem->getQty(), 10);
}
/**
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoDataFixture Magento/Customer/_files/customer.php
*
* @return void
*/
public function testAddNewItemInvalidWishlistItemConfiguration(): void
{
$productSku = 'simple';
$customerId = 1;
$product = $this->productRepository->get($productSku);
$wishlist = $this->getWishlistByCustomerId->execute($customerId);
$this->expectExceptionObject(new \InvalidArgumentException('Invalid wishlist item configuration.'));
$wishlist->addNewItem($product, '{"qty":2');
}
/**
* @magentoDataFixture Magento/Wishlist/_files/wishlist.php
*
* @return void
*/
public function testGetItemCollection(): void
{
$productSku = 'simple';
$item = $this->getWishlistByCustomerId->getItemBySku(1, $productSku);
$this->assertNotNull($item);
}
/**
* @magentoDataFixture Magento/Wishlist/_files/wishlist_with_disabled_simple_product.php
*
* @return void
*/
public function testGetItemCollectionWithDisabledProduct(): void
{
$customerId = 1;
$this->assertEmpty($this->getWishlistByCustomerId->execute($customerId)->getItemCollection()->getItems());
}
/**
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_two_child_products.php
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDbIsolation disabled
*
* @return void
*/
public function testAddConfigurableProductToWishList(): void
{
$configurableProduct = $this->productRepository->get('Configurable product');
$configurableOptions = $configurableProduct->getTypeInstance()->getConfigurableOptions($configurableProduct);
$attributeId = key($configurableOptions);
$option = reset($configurableOptions[$attributeId]);
$buyRequest = [
'super_attribute' => [
$attributeId => $option['value_index']
],
'action' => 'add',
];
$wishlist = $this->getWishlistByCustomerId->execute(1);
$wishlist->addNewItem($configurableProduct, $buyRequest);
$item = $this->getWishlistByCustomerId->getItemBySku(1, 'Configurable product');
$this->assertNotNull($item);
$this->assertWishListItem($item, $option['sku'], $buyRequest);
}
/**
* @magentoDataFixture Magento/Bundle/_files/fixed_bundle_product_without_discounts.php
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDbIsolation disabled
*
* @return void
*/
public function testAddBundleProductToWishList(): void
{
$bundleProduct = $this->productRepository->get('fixed_bundle_product_without_discounts');
$bundleOptionList = $this->objectManager->create(OptionList::class);
$bundleOptions = $bundleOptionList->getItems($bundleProduct);
$option = reset($bundleOptions);
$productLinks = $option->getProductLinks();
$this->assertNotNull($productLinks[0]);
$buyRequest = [
'bundle_option' => [
$option->getOptionId() => $productLinks[0]->getId()
],
'action' => 'add',
];
$skuWithChosenOption = implode('-', [$bundleProduct->getSku(), $productLinks[0]->getSku()]);
$wishlist = $this->getWishlistByCustomerId->execute(1);
$wishlist->addNewItem($bundleProduct, $buyRequest);
$item = $this->getWishlistByCustomerId->getItemBySku(1, 'fixed_bundle_product_without_discounts');
$this->assertNotNull($item);
$this->assertWishListItem($item, $skuWithChosenOption, $buyRequest);
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
*
* @return void
*/
public function testAddNotExistingItemToWishList(): void
{
$wishlist = $this->getWishlistByCustomerId->execute(1);
$this->expectExceptionObject(new LocalizedException(__('Cannot specify product.')));
$wishlist->addNewItem(989);
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Catalog/_files/product_out_of_stock_with_multiselect_attribute.php
*
* @return void
*/
public function testAddOutOfStockItemToWishList(): void
{
$product = $this->productRepository->get('simple_ms_out_of_stock');
$wishlist = $this->getWishlistByCustomerId->execute(1);
$this->expectExceptionObject(new StockStateException(__('Cannot add product without stock to wishlist.')));
$wishlist->addNewItem($product);
}
/**
* @magentoDataFixture Magento/Wishlist/_files/wishlist.php
*
* @return void
*/
public function testUpdateItemQtyInWishList(): void
{
$wishlist = $this->getWishlistByCustomerId->execute(1);
$item = $this->getWishlistByCustomerId->getItemBySku(1, 'simple');
$this->assertNotNull($item);
$buyRequest = $this->dataObjectFactory->create(['data' => ['qty' => 55]]);
$wishlist->updateItem($item->getId(), $buyRequest);
$updatedItem = $this->getWishlistByCustomerId->getItemBySku(1, 'simple');
$this->assertEquals(55, $updatedItem->getQty());
}
/**
* Update description of wishlist item
*
* @magentoDataFixture Magento/Wishlist/_files/wishlist.php
*
* @return void
*/
public function testUpdateItemDescriptionInWishList(): void
{
$itemDescription = 'Test Description';
$wishlist = $this->getWishlistByCustomerId->execute(1);
$item = $this->getWishlistByCustomerId->getItemBySku(1, 'simple');
$item->setDescription($itemDescription);
$this->assertNotNull($item);
$buyRequest = $this->dataObjectFactory->create(['data' => ['qty' => 55]]);
$wishlist->updateItem($item, $buyRequest);
$updatedItem = $this->getWishlistByCustomerId->getItemBySku(1, 'simple');
$this->assertEquals(55, $updatedItem->getQty());
$this->assertEquals($itemDescription, $updatedItem->getDescription());
}
/**
* @return void
*/
public function testUpdateNotExistingItemInWishList(): void
{
$this->expectExceptionObject(new LocalizedException(__('We can\'t specify a wish list item.')));
$this->wishlistFactory->create()->updateItem(989, []);
}
/**
* @magentoDataFixture Magento/Wishlist/_files/wishlist.php
*
* @return void
*/
public function testUpdateNotExistingProductInWishList(): void
{
$wishlist = $this->getWishlistByCustomerId->execute(1);
$item = $this->getWishlistByCustomerId->getItemBySku(1, 'simple');
$this->assertNotNull($item);
$item->getProduct()->setId(null);
$this->expectExceptionObject(new LocalizedException(__('The product does not exist.')));
$wishlist->updateItem($item, []);
}
/**
* Test that admin user should be able to update wishlist on second website
*
* @magentoAppArea adminhtml
* @magentoDbIsolation disabled
* @magentoDataFixture Magento/Wishlist/_files/wishlist_on_second_website.php
*
* @return void
*/
public function testUpdateWishListItemOnSecondWebsite(): void
{
$wishlist = $this->getWishlistByCustomerId->execute(1);
$item = $this->getWishlistByCustomerId->getItemBySku(1, 'simple-2');
$this->assertNotNull($item);
$this->assertEquals(1, $item->getQty());
$buyRequest = $this->dataObjectFactory->create(['data' => ['qty' => 2]]);
$wishlist->updateItem($item->getId(), $buyRequest);
$updatedItem = $this->getWishlistByCustomerId->getItemBySku(1, 'simple-2');
$this->assertEquals(2, $updatedItem->getQty());
}
/**
* Assert item in wish list.
*
* @param Item $item
* @param string $itemSku
* @param array $buyRequest
* @return void
*/
private function assertWishListItem(Item $item, string $itemSku, array $buyRequest): void
{
$this->assertEquals($itemSku, $item->getProduct()->getSku());
$buyRequestOption = $item->getOptionByCode('info_buyRequest');
$this->assertEquals($buyRequest, $this->json->unserialize($buyRequestOption->getValue()));
}
}