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,346 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Wishlist\Controller\Index;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Escaper;
use Magento\Framework\Message\MessageInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\AbstractController;
use Magento\TestFramework\Wishlist\Model\GetWishlistByCustomerId;
use Laminas\Stdlib\Parameters;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
use Magento\Wishlist\Model\DataSerializer;
use Magento\Framework\UrlInterface;
/**
* Test for add product to wish list.
*
* @magentoDbIsolation disabled
* @magentoAppArea frontend
* @magentoDataFixture Magento/Customer/_files/customer.php
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AddTest extends AbstractController
{
/** @var Session */
private $customerSession;
/** @var GetWishlistByCustomerId */
private $getWishlistByCustomerId;
/** @var ProductRepositoryInterface */
private $productRepository;
/** @var Escaper */
private $escaper;
/** @var CustomerRepositoryInterface */
private $customerRepository;
/**
* @var TransportBuilderMock
*/
private $transportBuilder;
/**
* @var UrlInterface
*/
private $urlBuilder;
/**
* @var DataSerializer
*/
private $dataSerializer;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->customerSession = $this->_objectManager->get(Session::class);
$this->getWishlistByCustomerId = $this->_objectManager->get(GetWishlistByCustomerId::class);
$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
$this->productRepository->cleanCache();
$this->escaper = $this->_objectManager->get(Escaper::class);
$this->customerRepository = $this->_objectManager->get(CustomerRepositoryInterface::class);
$this->transportBuilder = $this->_objectManager->get(TransportBuilderMock::class);
$this->urlBuilder = $this->_objectManager->get(UrlInterface::class);
$this->dataSerializer = $this->_objectManager->get(DataSerializer::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->customerSession->setCustomerId(null);
parent::tearDown();
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Catalog/_files/product_simple_xss.php
*
* @return void
*/
public function testAddActionProductNameXss(): void
{
$this->prepareReferer();
$this->customerSession->setCustomerId(1);
$product = $this->productRepository->get('product-with-xss');
$escapedProductName = $this->escaper->escapeHtml($product->getName());
$this->performAddToWishListRequest(['product' => $product->getId()]);
$this->assertSuccess(1, 1, $escapedProductName);
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Wishlist/_files/wishlist_with_configurable_product.php
* @magentoDbIsolation disabled
*
* @return void
*/
public function testAddConfigurableProductToWishList(): void
{
$this->prepareReferer();
$this->customerSession->setCustomerId(1);
$product = $this->productRepository->get('Configurable product');
$this->performAddToWishListRequest(['product' => $product->getId()]);
$this->assertSuccess(1, 1, $product->getName());
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Catalog/_files/multiple_products.php
*
* @return void
*/
public function testAddDisabledProductToWishList(): void
{
$expectedMessage = $this->escaper->escapeHtml("We can't specify a product.");
$this->customerSession->setCustomerId(1);
$product = $this->productRepository->get('simple3');
$this->performAddToWishListRequest(['product' => $product->getId()]);
$this->assertSessionMessages($this->equalTo([(string)__($expectedMessage)]), MessageInterface::TYPE_ERROR);
$this->assertRedirect($this->stringContains('wishlist/'));
}
/**
* @return void
*/
public function testAddToWishListWithoutParams(): void
{
$this->customerSession->setCustomerId(1);
$this->performAddToWishListRequest([]);
$this->assertSessionMessages($this->isEmpty(), MessageInterface::TYPE_ERROR);
$this->assertSessionMessages($this->isEmpty(), MessageInterface::TYPE_SUCCESS);
$this->assertRedirect($this->stringContains('wishlist/'));
}
/**
* @return void
*/
public function testAddNotExistingProductToWishList(): void
{
$this->customerSession->setCustomerId(1);
$expectedMessage = $this->escaper->escapeHtml("We can't specify a product.");
$this->performAddToWishListRequest(['product' => 989]);
$this->assertSessionMessages($this->equalTo([(string)__($expectedMessage)]), MessageInterface::TYPE_ERROR);
$this->assertRedirect($this->stringContains('wishlist/'));
}
/**
* @return void
*/
public function testAddToNotExistingWishList(): void
{
$expectedMessage = $this->escaper->escapeHtml("The requested Wish List doesn't exist.");
$this->customerSession->setCustomerId(1);
$this->performAddToWishListRequest(['wishlist_id' => 989]);
$this->assertSessionMessages($this->equalTo([(string)__($expectedMessage)]), MessageInterface::TYPE_ERROR);
$this->assert404NotFound();
}
/**
* Add Product to Wishlist Before Login, Create Customer & Send Confirmation Email
*
* @magentoAppArea frontend
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoConfigFixture current_website customer/create_account/confirm 1
*
* @return void
*/
public function testCreateCustomerWithEmailConfirmationAfterAddToWishlist(): void
{
$product = $this->productRepository->get('simple');
$data = [];
$data['product'] = (int)$product->getId();
$this->customerSession->setBeforeWishlistRequest($data);
$this->customerSession->setBeforeAuthUrl($this->urlBuilder->getUrl('wishlist/index/add'));
$email = 'test_example_new@email.com';
$this->fillRequestWithCustomerData($email);
$this->dispatch('customer/account/createPost');
$this->assertRedirect($this->stringContains('customer/account/index'));
$message = 'You must confirm your account.'
. ' Please check your email for the confirmation link or <a href="%1">click here</a> for a new link.';
$url = $this->urlBuilder->getUrl('customer/account/confirmation', ['_query' => ['email' => $email]]);
$this->assertSessionMessages($this->containsEqual((string)__($message, $url)), MessageInterface::TYPE_SUCCESS);
/** @var CustomerInterface $customer */
$customer = $this->customerRepository->get($email);
$confirmation = $customer->getConfirmation();
$sendMessage = $this->transportBuilder->getSentMessage();
$this->assertNotNull($sendMessage);
$rawMessage = $sendMessage->getBody()->getParts()[0]->getRawContent();
$this->assertStringContainsString(
(string)__(
'You must confirm your %customer_email email before you can sign in (link is only valid once):',
['customer_email' => $email]
),
$rawMessage
);
$this->assertStringContainsString(
sprintf('token'),
$rawMessage
);
$this->assertStringContainsString(
sprintf('id=%s&amp;key=%s', $customer->getId(), $confirmation),
$rawMessage
);
}
/**
* Save Wishlist Product Data into Cache.
* Also Add Product To Wishlist based on the data retrieved from Cache Token
*
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoDataFixture Magento/Customer/_files/customer_confirmation_config_enable.php
* @magentoDataFixture Magento/Customer/_files/unconfirmed_customer.php
* @magentoDbIsolation disabled
*
* @return void
*/
public function testAddToWishlistOnCustomerConfirmation(): void
{
$this->prepareReferer();
$product = $this->productRepository->get('simple');
$data = [];
$data['product'] = (int)$product->getId();
$token = $this->dataSerializer->serialize($data);//Save into Cache
$customer = $this->customerRepository->get('unconfirmedcustomer@example.com');
$customer->setConfirmation(null);
$this->customerRepository->save($customer);
$this->assertEquals(null, $customer->getConfirmation());
$this->customerSession->setCustomerId((int)$customer->getId());
$this->getRequest()->setParams(['token' => $token])->setMethod(HttpRequest::METHOD_GET);
$this->dispatch('wishlist/index/add');
$this->assertSuccess((int)$customer->getId(), 1, $product->getName());
}
/**
* @magentoConfigFixture current_store customer/captcha/enable 0
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testAddToWishlistBeforeLogin(): void
{
$this->prepareReferer();
$product = $this->productRepository->get('simple');
$this->performAddToWishListRequest(['product' => $product->getId()]);
$this->assertSessionMessages(
$this->equalTo([(string)__('You must login or register to add items to your wishlist.')]),
MessageInterface::TYPE_ERROR
);
// re-initialize the application to make a second request
Bootstrap::getInstance()->getBootstrap()->getApplication()->reinitialize();
$this->_objectManager = Bootstrap::getObjectManager();
$this->customerSession = $this->_objectManager->get(Session::class);
$this->_request = null;
$this->_response = null;
// login
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->getRequest()->setPostValue([
'login' => [
'username' => 'customer@example.com',
'password' => 'password',
],
]);
$this->dispatch('customer/account/loginPost');
$this->assertTrue($this->customerSession->isLoggedIn());
$this->assertSuccess(1, 1, $product->getName());
}
/**
* Perform request add item to wish list.
*
* @param array $params
* @return void
*/
private function performAddToWishListRequest(array $params): void
{
$this->getRequest()->setParams($params)->setMethod(HttpRequest::METHOD_POST);
$this->dispatch('wishlist/index/add');
}
/**
* Assert success response and items count.
*
* @param int $customerId
* @param int $itemsCount
* @param string $productName
* @return void
*/
private function assertSuccess(int $customerId, int $itemsCount, string $productName): void
{
$expectedMessage = sprintf("\n%s has been added to your Wish List.", $productName)
. " Click <a href=\"http://localhost/test\">here</a> to continue shopping.";
$this->assertSessionMessages($this->equalTo([(string)__($expectedMessage)]), MessageInterface::TYPE_SUCCESS);
$wishlist = $this->getWishlistByCustomerId->execute($customerId);
$this->assertCount($itemsCount, $wishlist->getItemCollection());
$this->assertRedirect($this->stringContains('wishlist/index/index/wishlist_id/' . $wishlist->getId()));
}
/**
* Prepare referer to test.
*
* @return void
*/
private function prepareReferer(): void
{
$parameters = $this->_objectManager->create(Parameters::class);
$parameters->set('HTTP_REFERER', 'http://localhost/test');
$this->getRequest()->setServer($parameters);
}
/**
* Fills request with customer data.
*
* @param string $email
* @return void
*/
private function fillRequestWithCustomerData(string $email): void
{
$this->getRequest()
->setMethod(HttpRequest::METHOD_POST)
->setParam(CustomerInterface::FIRSTNAME, 'firstname1')
->setParam(CustomerInterface::LASTNAME, 'lastname1')
->setParam(CustomerInterface::EMAIL, $email)
->setParam('password', '_Password1')
->setParam('password_confirmation', '_Password1');
}
}
@@ -0,0 +1,110 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Wishlist\Controller\Index;
use Magento\Checkout\Model\CartFactory;
use Magento\Checkout\Model\Cart as CartModel;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\Escaper;
use Magento\Framework\Message\MessageInterface;
use Magento\TestFramework\TestCase\AbstractController;
use Magento\TestFramework\Wishlist\Model\GetWishlistByCustomerId;
/**
* Test for add all products to cart from wish list.
*
* @magentoDbIsolation disabled
* @magentoAppArea frontend
*/
class AllcartTest extends AbstractController
{
/** @var Session */
private $customerSession;
/** @var CartModel */
private $cart;
/** @var Escaper */
private $escaper;
/** @var GetWishlistByCustomerId */
private $getWishlistByCustomerId;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->customerSession = $this->_objectManager->get(Session::class);
$this->cart = $this->_objectManager->get(CartFactory::class)->create();
$this->escaper = $this->_objectManager->get(Escaper::class);
$this->getWishlistByCustomerId = $this->_objectManager->get(GetWishlistByCustomerId::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->customerSession->setCustomerId(null);
parent::tearDown();
}
/**
* @magentoDataFixture Magento/Wishlist/_files/wishlist_with_product_qty_increments.php
*
* @return void
*/
public function testAddProductQtyIncrementToCartFromWishList(): void
{
$this->customerSession->setCustomerId(1);
$this->performAddAllToCartRequest();
$wishlistCollection = $this->getWishlistByCustomerId->execute(1)->getItemCollection();
$this->assertCount(1, $wishlistCollection);
$this->assertCount(0, $this->cart->getQuote()->getItemsCollection());
$item = $this->getWishlistByCustomerId->getItemBySku(1, 'simple');
$this->assertNotNull($item);
$expectedMessage = $this->escaper->escapeHtml(
sprintf('You can buy this product only in quantities of 5 at a time for "%s".', $item->getName())
);
$this->assertSessionMessages($this->equalTo([(string)__($expectedMessage)]), MessageInterface::TYPE_ERROR);
}
/**
* @magentoDataFixture Magento/Wishlist/_files/wishlist_with_simple_product.php
*
* @return void
*/
public function testAddAllProductToCartFromWishList(): void
{
$this->customerSession->setCustomerId(1);
$this->performAddAllToCartRequest();
$quoteCollection = $this->cart->getQuote()->getItemsCollection();
$this->assertCount(1, $quoteCollection);
$item = $quoteCollection->getFirstItem();
$expectedMessage = $this->escaper->escapeHtml(
sprintf('1 product(s) have been added to shopping cart: "%s".', $item->getName())
);
$this->assertSessionMessages($this->equalTo([(string)__($expectedMessage)]), MessageInterface::TYPE_SUCCESS);
}
/**
* Perform add all products to cart from wish list request.
*
* @return void
*/
private function performAddAllToCartRequest(): void
{
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->dispatch('wishlist/index/allcart');
}
}
@@ -0,0 +1,170 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Wishlist\Controller\Index;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
use Magento\Checkout\Model\CartFactory;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\Escaper;
use Magento\Framework\Message\MessageInterface;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\TestCase\AbstractController;
use Magento\TestFramework\Wishlist\Model\GetWishlistByCustomerId;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
/**
* Test for add product to cart from wish list.
*
* @magentoDbIsolation disabled
* @magentoAppArea frontend
*/
class CartTest extends AbstractController
{
/** @var Session */
private $customerSession;
/** @var GetWishlistByCustomerId */
private $getWishlistByCustomerId;
/** @var CartFactory */
private $cartFactory;
/** @var Escaper */
private $escaper;
/** @var ProductRepositoryInterface */
private $productRepository;
/**
* @var \Magento\TestFramework\Fixture\DataFixtureStorage
*/
private $fixtures;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->customerSession = $this->_objectManager->get(Session::class);
$this->getWishlistByCustomerId = $this->_objectManager->get(GetWishlistByCustomerId::class);
$this->cartFactory = $this->_objectManager->get(CartFactory::class);
$this->escaper = $this->_objectManager->get(Escaper::class);
$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
$this->fixtures = $this->_objectManager->get(DataFixtureStorageManager::class)->getStorage();
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->customerSession->setCustomerId(null);
parent::tearDown();
}
/**
* @magentoDataFixture Magento/Wishlist/_files/wishlist_with_simple_product.php
*
* @return void
*/
public function testAddSimpleProductToCart(): void
{
$this->customerSession->setCustomerId(1);
$item = $this->getWishlistByCustomerId->getItemBySku(1, 'simple-1');
$this->assertNotNull($item);
$this->performAddToCartRequest(['item' => $item->getId(), 'qty' => 3]);
$message = sprintf("\n" . 'You added %s to your ' .
'<a href="http://localhost/index.php/checkout/cart/">shopping cart</a>.', $item->getName());
$this->assertSessionMessages($this->equalTo([(string)__($message)]), MessageInterface::TYPE_SUCCESS);
$this->assertCount(0, $this->getWishlistByCustomerId->execute(1)->getItemCollection());
$cart = $this->cartFactory->create();
$this->assertEquals(1, $cart->getItemsCount());
$this->assertEquals(3, $cart->getItemsQty());
}
/**
* @magentoDataFixture Magento/Wishlist/_files/wishlist_with_configurable_product.php
*
* @return void
*/
public function testAddItemWithNotChosenOptionToCart(): void
{
$this->customerSession->setCustomerId(1);
$item = $this->getWishlistByCustomerId->getItemBySku(1, 'Configurable product');
$this->assertNotNull($item);
$this->performAddToCartRequest(['item' => $item->getId(), 'qty' => 1]);
$redirectUrl = sprintf("wishlist/index/configure/id/%s/product_id/%s", $item->getId(), $item->getProductId());
$this->assertRedirect($this->stringContains($redirectUrl));
$message = 'You need to choose options for your item.';
$this->assertSessionMessages($this->equalTo([(string)__($message)]), MessageInterface::TYPE_NOTICE);
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
*
* @return void
*/
public function testAddNotExistingItemToCart(): void
{
$this->customerSession->setCustomerId(1);
$this->performAddToCartRequest(['item' => 989]);
$this->assertRedirect($this->stringContains('wishlist/index/'));
}
/**
* Add wishlist item with related Products to Cart.
*
* @return void
* @magentoDataFixture Magento/Wishlist/_files/wishlist_with_simple_product.php
*/
#[
DataFixture(ProductFixture::class, as: 'product1'),
DataFixture(ProductFixture::class, as: 'product2'),
]
public function testAddItemWithRelatedProducts(): void
{
$firstProductId = $this->fixtures->get('product1')->getId();
$secondProductID = $this->fixtures->get('product2')->getId();
$relatedIds = $expectedAddedIds = [$firstProductId, $secondProductID];
$this->customerSession->setCustomerId(1);
$item = $this->getWishlistByCustomerId->getItemBySku(1, 'simple-1');
$this->assertNotNull($item);
$this->performAddToCartRequest([
'item' => $item->getId(),
'qty' => 1,
'related_product' => implode(',', $relatedIds),
]);
$this->assertCount(0, $this->getWishlistByCustomerId->execute(1)->getItemCollection());
$cart = $this->cartFactory->create();
$this->assertEquals(3, $cart->getItemsCount());
$expectedAddedIds[] = $item->getProductId();
foreach ($expectedAddedIds as $addedId) {
$this->assertContains($addedId, $cart->getProductIds());
}
}
/**
* Perform request add to cart from wish list.
*
* @param array $params
* @return void
*/
private function performAddToCartRequest(array $params): void
{
$this->getRequest()->setParams($params)->setMethod(HttpRequest::METHOD_POST);
$this->dispatch('wishlist/index/cart');
}
}
@@ -0,0 +1,82 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Wishlist\Controller\Index;
use Magento\Customer\Model\Session;
use Magento\TestFramework\Helper\Xpath;
use Magento\TestFramework\TestCase\AbstractController;
/**
* Test wish list on customer account page.
*
* @magentoDbIsolation disabled
* @magentoAppArea frontend
*/
class IndexTest extends AbstractController
{
/** @var Session */
private $customerSession;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->customerSession = $this->_objectManager->get(Session::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->customerSession->setCustomerId(null);
parent::tearDown();
}
/**
* Verify wishlist view action
*
* The following is verified:
* - \Magento\Wishlist\Model\ResourceModel\Item\Collection
* - \Magento\Wishlist\Block\Customer\Wishlist
* - \Magento\Wishlist\Block\Customer\Wishlist\Items
* - \Magento\Wishlist\Block\Customer\Wishlist\Item\Column
* - \Magento\Wishlist\Block\Customer\Wishlist\Item\Column\Cart
* - \Magento\Wishlist\Block\Customer\Wishlist\Item\Column\Comment
* - \Magento\Wishlist\Block\Customer\Wishlist\Button
* - that \Magento\Wishlist\Block\Customer\Wishlist\Item\Options doesn't throw a fatal error
*
* @magentoDataFixture Magento/Wishlist/_files/wishlist.php
*
* @return void
*/
public function testItemColumnBlock(): void
{
$this->customerSession->setCustomerId(1);
$this->dispatch('wishlist/index/index');
$body = $this->getResponse()->getBody();
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
'//img[contains(@src, "small_image.jpg") and @alt = "Simple Product"]',
$body
)
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
'//textarea[contains(@name, "description")]',
$body
)
);
}
}
@@ -0,0 +1,95 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Wishlist\Controller\Index;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\Message\MessageInterface;
use Magento\TestFramework\TestCase\AbstractController;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Catalog\Api\ProductRepositoryInterface;
/**
* Test for wishlist plugin before dispatch
*
* @magentoDbIsolation enabled
* @magentoAppArea frontend
*/
class PluginTest extends AbstractController
{
/**
* @var CustomerSession
*/
private $customerSession;
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->customerSession = $this->_objectManager->get(CustomerSession::class);
$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->customerSession->logout();
$this->customerSession = null;
parent::tearDown();
}
/**
* Test for adding product to wishlist with invalidate credentials
*
* @magentoDataFixture Magento/Catalog/_files/product_simple_xss.php
* @magentoDataFixture Magento/Customer/_files/customer.php
*
* @return void
*/
public function testAddActionProductWithInvalidCredentials(): void
{
$product = $this->productRepository->get('product-with-xss');
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->getRequest()->setPostValue(
[
'login' => [
'username' => 'invalidCustomer@example.com',
'password' => 'invalidPassword',
],
]
);
$this->getRequest()->setParams(['product' => $product->getId(), 'nocookie' => 1]);
$this->dispatch('wishlist/index/add');
$this->assertArrayNotHasKey('login', $this->customerSession->getBeforeWishlistRequest());
$expectedMessage = 'You must login or register to add items to your wishlist.';
$this->assertSessionMessages($this->equalTo([(string)__($expectedMessage)]), MessageInterface::TYPE_ERROR);
}
/**
* @magentoConfigFixture current_store wishlist/general/active 0
* @magentoDataFixture Magento/Customer/_files/customer.php
*
* @return void
*/
public function testWithDisabledWishList(): void
{
$this->customerSession->setCustomerId(1);
$this->dispatch('wishlist/index/index');
$this->assert404NotFound();
}
}
@@ -0,0 +1,79 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Wishlist\Controller\Index;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\Message\MessageInterface;
use Magento\TestFramework\TestCase\AbstractController;
use Magento\TestFramework\Wishlist\Model\GetWishlistByCustomerId;
/**
* Test for remove product from wish list.
*
* @magentoDbIsolation disabled
* @magentoAppArea frontend
* @magentoDataFixture Magento/Wishlist/_files/wishlist.php
*/
class RemoveTest extends AbstractController
{
/** @var Session */
private $customerSession;
/** @var GetWishlistByCustomerId */
private $getWishlistByCustomerId;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->customerSession = $this->_objectManager->get(Session::class);
$this->getWishlistByCustomerId = $this->_objectManager->get(GetWishlistByCustomerId::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->customerSession->setCustomerId(null);
parent::tearDown();
}
/**
* @return void
*/
public function testRemoveProductFromWishList(): void
{
$customerId = 1;
$this->customerSession->setCustomerId($customerId);
$item = $this->getWishlistByCustomerId->getItemBySku($customerId, 'simple');
$this->assertNotNull($item);
$productName = $item->getProduct()->getName();
$this->getRequest()->setParam('item', $item->getId())->setMethod(HttpRequest::METHOD_POST);
$this->dispatch('wishlist/index/remove');
$message = sprintf("\n%s has been removed from your Wish List.\n", $productName);
$this->assertSessionMessages($this->equalTo([(string)__($message)]), MessageInterface::TYPE_SUCCESS);
$this->assertCount(0, $this->getWishlistByCustomerId->execute($customerId)->getItemCollection());
}
/**
* @return void
*/
public function testRemoveNotExistingItemFromWishList(): void
{
$this->customerSession->setCustomerId(1);
$this->getRequest()->setParams(['item' => 989])->setMethod(HttpRequest::METHOD_POST);
$this->dispatch('wishlist/index/remove');
$this->assert404NotFound();
}
}
@@ -0,0 +1,180 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Wishlist\Controller\Index;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Customer\Api\CustomerNameGenerationInterface;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\Message\MessageInterface;
use Magento\TestFramework\Helper\Xpath;
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
use Magento\TestFramework\TestCase\AbstractController;
/**
* Test sending wish list.
*
* @magentoDbIsolation disabled
* @magentoAppArea frontend
* @magentoDataFixture Magento/Wishlist/_files/wishlist.php
*/
class SendTest extends AbstractController
{
/** @var Session */
private $customerSession;
/** @var CustomerNameGenerationInterface */
private $customerNameGeneration;
/** @var ProductRepositoryInterface */
private $productRepository;
/** @var TransportBuilderMock */
private $transportBuilder;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->customerSession = $this->_objectManager->get(Session::class);
$this->customerNameGeneration = $this->_objectManager->get(CustomerNameGenerationInterface::class);
$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
$this->productRepository->cleanCache();
$this->transportBuilder = $this->_objectManager->get(TransportBuilderMock::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->customerSession->setCustomerId(null);
parent::tearDown();
}
/**
* @return void
*/
public function testSendWishList(): void
{
$product = $this->productRepository->get('simple');
$this->customerSession->setCustomerId(1);
$shareMessage = 'Here\'s what I want for my birthday.';
$postValues = ['emails' => 'test@example.com', 'message' => $shareMessage];
$this->dispatchSendWishListRequest($postValues);
$this->assertSessionMessages(
$this->equalTo([(string)__('Your wish list has been shared.')]),
MessageInterface::TYPE_SUCCESS
);
$this->assertNotNull($this->transportBuilder->getSentMessage());
$messageContent = $this->transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent();
$this->assertStringContainsString($shareMessage, $messageContent);
$this->assertStringContainsString(
sprintf(
'%s wants to share this Wish List',
$this->customerNameGeneration->getCustomerName($this->customerSession->getCustomerDataObject())
),
$messageContent
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//a[contains(@href, '%s')]/strong[contains(text(), '%s')]",
$product->getProductUrl(),
$product->getName()
),
$messageContent
)
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
"//a[contains(@href, 'wishlist/shared/index/code/fixture_unique_code/')"
. " and contains(text(), 'View all Wish List')]",
$messageContent
)
);
}
/**
* @magentoConfigFixture current_store wishlist/email/number_limit 2
*
* @return void
*/
public function testSendWishListWithEmailsLimit(): void
{
$this->customerSession->setCustomerId(1);
$postValues = ['emails' => 'test@example.com, test2@example.com, test3@example.com'];
$this->dispatchSendWishListRequest($postValues);
$this->assertResponseWithError('Maximum of 2 emails can be sent.');
}
/**
* @magentoConfigFixture current_store wishlist/email/text_limit 10
*
* @return void
*/
public function testSendWishListWithTextLimit(): void
{
$this->customerSession->setCustomerId(1);
$postValues = ['emails' => 'test@example.com', 'message' => 'Test message'];
$this->dispatchSendWishListRequest($postValues);
$this->assertResponseWithError('Message length must not exceed 10 symbols');
}
/**
* @return void
*/
public function testSendWishListWithoutEmails(): void
{
$this->customerSession->setCustomerId(1);
$postValues = ['emails' => ''];
$this->dispatchSendWishListRequest($postValues);
$this->assertResponseWithError('Please enter an email address.');
}
/**
* @return void
*/
public function testSendWishListWithInvalidEmail(): void
{
$this->customerSession->setCustomerId(1);
$postValues = ['emails' => 'test @example.com'];
$this->dispatchSendWishListRequest($postValues);
$this->assertResponseWithError('Please enter a valid email address.');
}
/**
* Dispatch send wish list request.
*
* @param array $postValues
* @return void
*/
private function dispatchSendWishListRequest(array $postValues): void
{
$this->getRequest()->setPostValue($postValues)->setMethod(HttpRequest::METHOD_POST);
$this->dispatch('wishlist/index/send');
}
/**
* Assert error message and redirect.
*
* @param string $message
* @return void
*/
private function assertResponseWithError(string $message): void
{
$this->assertSessionMessages($this->equalTo([__($message)]), MessageInterface::TYPE_ERROR);
$this->assertRedirect($this->stringContains('wishlist/index/share'));
}
}
@@ -0,0 +1,241 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Wishlist\Controller\Index;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\Escaper;
use Magento\Framework\Message\MessageInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\TestFramework\TestCase\AbstractController;
use Magento\TestFramework\Wishlist\Model\GetWishlistByCustomerId;
use Magento\Wishlist\Model\Item;
/**
* Test for update wish list item.
*
* @magentoDbIsolation enabled
* @magentoAppArea frontend
*/
class UpdateItemOptionsTest extends AbstractController
{
/** @var Session */
private $customerSession;
/** @var ProductRepositoryInterface */
private $productRepository;
/** @var Escaper */
private $escaper;
/** @var SerializerInterface */
private $json;
/** @var GetWishlistByCustomerId */
private $getWishlistByCustomerId;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->customerSession = $this->_objectManager->get(Session::class);
$this->getWishlistByCustomerId = $this->_objectManager->get(GetWishlistByCustomerId::class);
$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
$this->productRepository->cleanCache();
$this->escaper = $this->_objectManager->get(Escaper::class);
$this->json = $this->_objectManager->get(SerializerInterface::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->customerSession->setCustomerId(null);
parent::tearDown();
}
/**
* @magentoDataFixture Magento/Wishlist/_files/wishlist_with_configurable_product.php
* @magentoDbIsolation disabled
*
* @return void
*/
public function testUpdateItemOptions(): void
{
$this->customerSession->setCustomerId(1);
$item = $this->getWishlistByCustomerId->getItemBySku(1, 'Configurable product');
$this->assertNotNull($item);
$params = [
'id' => $item->getId(),
'product' => $item->getProductId(),
'super_attribute' => $this->performConfigurableOption($item->getProduct()),
'qty' => 5,
];
$this->performUpdateWishListItemRequest($params);
$message = sprintf("%s has been updated in your Wish List.", $item->getProduct()->getName());
$this->assertSessionMessages($this->equalTo([(string)__($message)]), MessageInterface::TYPE_SUCCESS);
$this->assertRedirect($this->stringContains('wishlist/index/index/wishlist_id/' . $item->getWishlistId()));
$this->assertUpdatedItem(
$this->getWishlistByCustomerId->getItemBySku(1, 'Configurable product'),
$params
);
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
*
* @return void
*/
public function testUpdateItemOptionsWithoutParams(): void
{
$this->customerSession->setCustomerId(1);
$this->performUpdateWishListItemRequest([]);
$this->assertSessionMessages($this->isEmpty(), MessageInterface::TYPE_ERROR);
$this->assertSessionMessages($this->isEmpty(), MessageInterface::TYPE_SUCCESS);
$this->assertRedirect($this->stringContains('wishlist/'));
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
*
* @return void
*/
public function testUpdateNotExistingItem(): void
{
$this->customerSession->setCustomerId(1);
$this->performUpdateWishListItemRequest(['product' => 989]);
$message = $this->escaper->escapeHtml("We can't specify a product.");
$this->assertSessionMessages($this->equalTo([(string)__($message)]), MessageInterface::TYPE_ERROR);
$this->assertRedirect($this->stringContains('wishlist/'));
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Catalog/_files/multiple_products.php
*
* @return void
*/
public function testUpdateOutOfStockItem(): void
{
$product = $this->productRepository->get('simple3');
$this->customerSession->setCustomerId(1);
$this->performUpdateWishListItemRequest(['product' => $product->getId()]);
$message = $this->escaper->escapeHtml("We can't specify a product.");
$this->assertSessionMessages($this->equalTo([(string)__($message)]), MessageInterface::TYPE_ERROR);
$this->assertRedirect($this->stringContains('wishlist/'));
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Catalog/_files/product_out_of_stock_with_multiselect_attribute.php
*
* @return void
*/
public function testUpdateItemNotSpecifyAsWishListItem(): void
{
$product = $this->productRepository->get('simple_ms_out_of_stock');
$this->customerSession->setCustomerId(1);
$this->performUpdateWishListItemRequest(['product' => $product->getId()]);
$message = $this->escaper->escapeHtml("We can't specify a wish list item.");
$this->assertSessionMessages($this->equalTo([(string)__($message)]), MessageInterface::TYPE_ERROR);
$this->assertRedirect($this->stringContains('wishlist/index/index/wishlist_id/'));
}
/**
* @magentoDataFixture Magento/Wishlist/_files/wishlist_with_grouped_product.php
* @magentoDbIsolation disabled
*
* @return void
*/
public function testUpdateItemOptionsForGroupedProduct(): void
{
$this->customerSession->setCustomerId(1);
$item = $this->getWishlistByCustomerId->getItemBySku(1, 'grouped');
$this->assertNotNull($item);
$params = [
'id' => $item->getId(),
'product' => $item->getProductId(),
'super_group' => $this->performGroupedOption(),
'qty' => 1,
];
$this->performUpdateWishListItemRequest($params);
$message = sprintf("%s has been updated in your Wish List.", $item->getProduct()->getName());
$this->assertSessionMessages($this->equalTo([(string)__($message)]), MessageInterface::TYPE_SUCCESS);
$this->assertRedirect($this->stringContains('wishlist/index/index/wishlist_id/' . $item->getWishlistId()));
$this->assertUpdatedItem(
$this->getWishlistByCustomerId->getItemBySku(1, 'grouped'),
$params
);
}
/**
* Perform request update wish list item.
*
* @param array $params
* @return void
*/
private function performUpdateWishListItemRequest(array $params): void
{
$this->getRequest()->setParams($params)->setMethod(HttpRequest::METHOD_POST);
$this->dispatch('wishlist/index/updateItemOptions');
}
/**
* Assert updated item in wish list.
*
* @param Item $item
* @param array $expectedData
* @return void
*/
private function assertUpdatedItem(Item $item, array $expectedData): void
{
$this->assertEquals($expectedData['qty'], $item->getQty());
$buyRequestOption = $this->json->unserialize($item->getOptionByCode('info_buyRequest')->getValue());
foreach ($expectedData as $key => $value) {
$this->assertEquals($value, $buyRequestOption[$key]);
}
}
/**
* Perform configurable option to select.
*
* @param ProductInterface $product
* @return array
*/
private function performConfigurableOption(ProductInterface $product): array
{
$configurableOptions = $product->getTypeInstance()->getConfigurableOptions($product);
$attributeId = key($configurableOptions);
$option = reset($configurableOptions[$attributeId]);
return [$attributeId => $option['value_index']];
}
/**
* Perform group option to select.
*
* @return array
*/
private function performGroupedOption(): array
{
$simple1 = $this->productRepository->get('simple_11');
$simple2 = $this->productRepository->get('simple_22');
return [
$simple1->getId() => '3',
$simple2->getId() => '3',
];
}
}
@@ -0,0 +1,98 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Wishlist\Controller\Index;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\Message\MessageInterface;
use Magento\TestFramework\TestCase\AbstractController;
use Magento\TestFramework\Wishlist\Model\GetWishlistByCustomerId;
/**
* Test for update wish list item.
*
* @magentoDbIsolation disabled
* @magentoAppArea frontend
* @magentoDataFixture Magento/Wishlist/_files/wishlist.php
*/
class UpdateTest extends AbstractController
{
/** @var Session */
private $customerSession;
/** @var GetWishlistByCustomerId */
private $getWishlistByCustomerId;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->customerSession = $this->_objectManager->get(Session::class);
$this->getWishlistByCustomerId = $this->_objectManager->get(GetWishlistByCustomerId::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->customerSession->setCustomerId(null);
parent::tearDown();
}
/**
* @return void
*/
public function testUpdateWishListItem(): void
{
$this->customerSession->setCustomerId(1);
$item = $this->getWishlistByCustomerId->getItemBySku(1, 'simple');
$this->assertNotNull($item);
$params = ['description' => [$item->getId() => 'Some description.'], 'qty' => [$item->getId() => 5]];
$this->performUpdateWishListItemRequest($params);
$message = sprintf("%s has been updated in your Wish List.", $item->getProduct()->getName());
$this->assertSessionMessages($this->equalTo([(string)__($message)]), MessageInterface::TYPE_SUCCESS);
$this->assertRedirect($this->stringContains('wishlist/index/index/wishlist_id/' . $item->getWishlistId()));
$updatedItem = $this->getWishlistByCustomerId->getItemBySku(1, 'simple');
$this->assertNotNull($updatedItem);
$this->assertEquals(5, $updatedItem->getQty());
$this->assertEquals('Some description.', $updatedItem->getDescription());
}
/**
* @return void
*/
public function testUpdateWishListItemZeroQty(): void
{
$this->customerSession->setCustomerId(1);
$item = $this->getWishlistByCustomerId->getItemBySku(1, 'simple');
$this->assertNotNull($item);
$params = ['description' => [$item->getId() => ''], 'qty' => [$item->getId() => 0]];
$this->performUpdateWishListItemRequest($params);
$message = sprintf("%s has been updated in your Wish List.", $item->getProduct()->getName());
$this->assertSessionMessages($this->equalTo([(string)__($message)]), MessageInterface::TYPE_SUCCESS);
$this->assertRedirect($this->stringContains('wishlist/index/index/wishlist_id/' . $item->getWishlistId()));
$this->assertCount(0, $this->getWishlistByCustomerId->execute(1)->getItemCollection());
}
/**
* Perform update wish list item request.
*
* @param array $params
* @return void
*/
private function performUpdateWishListItemRequest(array $params): void
{
$this->getRequest()->setPostValue($params)->setMethod(HttpRequest::METHOD_POST);
$this->dispatch('wishlist/index/update');
}
}
@@ -0,0 +1,92 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Wishlist\Controller;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Area;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\Message\MessageInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Request;
use Magento\TestFramework\TestCase\AbstractController;
/**
* @magentoAppIsolation enabled
*/
class ShareTest extends AbstractController
{
/**
* Test share wishlist with correct data
*
* @magentoDataFixture Magento/Wishlist/_files/wishlist.php
*/
public function testSuccessfullyShareWishlist()
{
$this->login(1);
$this->prepareRequestData();
$this->dispatch('wishlist/index/send/');
$this->assertSessionMessages(
$this->equalTo(['Your wish list has been shared.']),
MessageInterface::TYPE_SUCCESS
);
}
/**
* Test share wishlist with incorrect data
*
* @magentoDataFixture Magento/Wishlist/_files/wishlist.php
*/
public function testShareWishlistWithoutEmails()
{
$this->login(1);
$this->prepareRequestData(true);
$this->dispatch('wishlist/index/send/');
$this->assertSessionMessages(
$this->equalTo(['Please enter an email address.']),
MessageInterface::TYPE_ERROR
);
}
/**
* Login the user
*
* @param string $customerId Customer to mark as logged in for the session
* @return void
*/
protected function login($customerId)
{
/** @var Session $session */
$session = $this->_objectManager->get(Session::class);
$session->loginById($customerId);
}
/**
* Prepares the request with data
*
* @param bool $invalidData
* @return void
*/
private function prepareRequestData($invalidData = false)
{
Bootstrap::getInstance()->loadArea(Area::AREA_FRONTEND);
$emails = !$invalidData ? 'email-1@example.com,email-2@example.com' : '';
/** @var FormKey $formKey */
$formKey = $this->_objectManager->get(FormKey::class);
$post = [
'emails' => $emails,
'message' => '',
'form_key' => $formKey->getFormKey(),
];
$this->getRequest()->setMethod(Request::METHOD_POST);
$this->getRequest()->setPostValue($post);
}
}
@@ -0,0 +1,30 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Wishlist\Controller;
use Magento\Framework\App\Request\Http as HttpRequest;
class SharedTest extends \Magento\TestFramework\TestCase\AbstractController
{
/**
* @magentoDataFixture Magento/Wishlist/_files/wishlist_shared.php
* @magentoDbIsolation disabled
* @return void
*/
public function testAllcartAction()
{
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->getRequest()->setParam('code', 'fixture_unique_code');
$this->dispatch('wishlist/shared/allcart');
/** @var \Magento\Checkout\Model\Cart $cart */
$cart = $this->_objectManager->get(\Magento\Checkout\Model\Cart::class);
$quoteCount = $cart->getQuote()->getItemsCollection()->count();
$this->assertEquals(1, $quoteCount);
}
}