init
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Checkout\Model;
|
||||
|
||||
use Magento\Checkout\Api\GuestPaymentInformationManagementInterface;
|
||||
use Magento\Checkout\Api\PaymentInformationManagementInterface;
|
||||
use Magento\Framework\App\Backpressure\ContextInterface;
|
||||
use Magento\Framework\App\Backpressure\IdentityProviderInterface;
|
||||
use Magento\Framework\App\Backpressure\SlidingWindow\LimitConfigManagerInterface;
|
||||
use Magento\Framework\Webapi\Backpressure\BackpressureContextFactory;
|
||||
use Magento\Quote\Model\Backpressure\OrderLimitConfigManager;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class BackpressureTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var BackpressureContextFactory
|
||||
*/
|
||||
private $webapiContextFactory;
|
||||
|
||||
/**
|
||||
* @var LimitConfigManagerInterface
|
||||
*/
|
||||
private $limitConfigManager;
|
||||
|
||||
/**
|
||||
* @var IdentityProviderInterface|MockObject
|
||||
*/
|
||||
private $identityProvider;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->identityProvider = $this->createMock(IdentityProviderInterface::class);
|
||||
$this->webapiContextFactory = Bootstrap::getObjectManager()->create(
|
||||
BackpressureContextFactory::class,
|
||||
['identityProvider' => $this->identityProvider]
|
||||
);
|
||||
$this->limitConfigManager = Bootstrap::getObjectManager()->get(LimitConfigManagerInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configured cases.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getConfiguredCases(): array
|
||||
{
|
||||
return [
|
||||
'guest' => [
|
||||
ContextInterface::IDENTITY_TYPE_IP,
|
||||
'127.0.0.1',
|
||||
GuestPaymentInformationManagementInterface::class,
|
||||
'savePaymentInformationAndPlaceOrder',
|
||||
'/V1/guest-carts/:cartId/payment-information',
|
||||
50
|
||||
],
|
||||
'customer' => [
|
||||
ContextInterface::IDENTITY_TYPE_CUSTOMER,
|
||||
'42',
|
||||
PaymentInformationManagementInterface::class,
|
||||
'savePaymentInformationAndPlaceOrder',
|
||||
'/V1/carts/mine/payment-information',
|
||||
100
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that backpressure is configured for guests.
|
||||
*
|
||||
* @param int $identityType
|
||||
* @param string $identity
|
||||
* @param string $service
|
||||
* @param string $method
|
||||
* @param string $endpoint
|
||||
* @param int $expectedLimit
|
||||
* @return void
|
||||
* @dataProvider getConfiguredCases
|
||||
* @magentoConfigFixture current_store sales/backpressure/enabled 1
|
||||
* @magentoConfigFixture current_store sales/backpressure/limit 100
|
||||
* @magentoConfigFixture current_store sales/backpressure/guest_limit 50
|
||||
* @magentoConfigFixture current_store sales/backpressure/period 60
|
||||
*/
|
||||
public function testConfigured(
|
||||
int $identityType,
|
||||
string $identity,
|
||||
string $service,
|
||||
string $method,
|
||||
string $endpoint,
|
||||
int $expectedLimit
|
||||
): void {
|
||||
$this->identityProvider->method('fetchIdentityType')->willReturn($identityType);
|
||||
$this->identityProvider->method('fetchIdentity')->willReturn($identity);
|
||||
|
||||
$context = $this->webapiContextFactory->create(
|
||||
$service,
|
||||
$method,
|
||||
$endpoint
|
||||
);
|
||||
$this->assertEquals(OrderLimitConfigManager::REQUEST_TYPE_ID, $context->getTypeId());
|
||||
|
||||
$limits = $this->limitConfigManager->readLimit($context);
|
||||
$this->assertEquals($expectedLimit, $limits->getLimit());
|
||||
$this->assertEquals(60, $limits->getPeriod());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Checkout\Model;
|
||||
|
||||
use Magento\Captcha\Model\DefaultModel;
|
||||
use Magento\Customer\Api\CustomerRepositoryInterface;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Magento\Captcha\Helper\Data as CaptchaHelper;
|
||||
use Magento\Framework\App\Request\Http as HttpRequest;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use Magento\Customer\Model\Session as CustomerSession;
|
||||
|
||||
/**
|
||||
* Test CAPTCHA-based rate limiter.
|
||||
*
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoAppArea frontend
|
||||
*/
|
||||
class CaptchaRateLimiterTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var CaptchaRateLimiter
|
||||
*/
|
||||
private $model;
|
||||
|
||||
/**
|
||||
* @var CaptchaHelper
|
||||
*/
|
||||
private $captchaHelper;
|
||||
|
||||
/**
|
||||
* @var HttpRequest;
|
||||
*/
|
||||
private $request;
|
||||
|
||||
/**
|
||||
* @var CustomerSession
|
||||
*/
|
||||
private $customerSession;
|
||||
|
||||
/**
|
||||
* @var CustomerRepositoryInterface
|
||||
*/
|
||||
private $customerRepo;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
/** @var ObjectManager $objectManager */
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$this->request = $objectManager->get(RequestInterface::class);
|
||||
$this->request->getServer()->set('REMOTE_ADDR', '127.0.0.1');
|
||||
$objectManager->removeSharedInstance(RemoteAddress::class);
|
||||
$this->captchaHelper = $objectManager->get(CaptchaHelper::class);
|
||||
$this->customerSession = $objectManager->get(CustomerSession::class);
|
||||
$this->customerRepo = $objectManager->get(CustomerRepositoryInterface::class);
|
||||
$this->model = $objectManager->create(
|
||||
CaptchaRateLimiter::class,
|
||||
['captchaId' => 'payment_processing_request']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that limits work for logged-in customers.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
*
|
||||
* @magentoConfigFixture default_store customer/captcha/enable 1
|
||||
* @magentoConfigFixture default_store customer/captcha/forms payment_processing_request
|
||||
* @magentoConfigFixture default_store customer/captcha/failed_attempts_login 2
|
||||
* @magentoConfigFixture default_store customer/captcha/failed_attempts_ip 10
|
||||
*/
|
||||
public function testLoggedInLimits(): void
|
||||
{
|
||||
//Logging in
|
||||
$customer = $this->customerRepo->get('customer@example.com');
|
||||
$this->customerSession->loginById($customer->getId());
|
||||
|
||||
$this->model->limit();
|
||||
$this->model->limit();
|
||||
try {
|
||||
$this->model->limit();
|
||||
$limited = false;
|
||||
} catch (LocalizedException $exception) {
|
||||
$limited = true;
|
||||
}
|
||||
$this->assertTrue($limited);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that limits work for guest.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @magentoConfigFixture default_store customer/captcha/enable 1
|
||||
* @magentoConfigFixture default_store customer/captcha/forms payment_processing_request
|
||||
* @magentoConfigFixture default_store customer/captcha/failed_attempts_login 10
|
||||
* @magentoConfigFixture default_store customer/captcha/failed_attempts_ip 2
|
||||
*/
|
||||
public function testGuestLimits(): void
|
||||
{
|
||||
$this->model->limit();
|
||||
$this->model->limit();
|
||||
try {
|
||||
$this->model->limit();
|
||||
$limited = false;
|
||||
} catch (LocalizedException $exception) {
|
||||
$limited = true;
|
||||
}
|
||||
$this->assertTrue($limited);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that CAPTCHA is validated.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @magentoConfigFixture default_store customer/captcha/enable 1
|
||||
* @magentoConfigFixture default_store customer/captcha/forms payment_processing_request
|
||||
* @magentoConfigFixture default_store customer/captcha/failed_attempts_login 10
|
||||
* @magentoConfigFixture default_store customer/captcha/failed_attempts_ip 2
|
||||
*/
|
||||
public function testCaptchaValidation(): void
|
||||
{
|
||||
$this->model->limit();
|
||||
$this->model->limit();
|
||||
try {
|
||||
$this->model->limit();
|
||||
$limited = false;
|
||||
} catch (LocalizedException $exception) {
|
||||
$limited = true;
|
||||
}
|
||||
//CAPTCHA is required
|
||||
$this->assertTrue($limited);
|
||||
|
||||
//Providing CAPTCHA value
|
||||
/** @var DefaultModel $captcha */
|
||||
$captcha = $this->captchaHelper->getCaptcha(CaptchaPaymentProcessingRateLimiter::CAPTCHA_FORM);
|
||||
$captcha->generate();
|
||||
$this->request->setPostValue(
|
||||
'captcha',
|
||||
[CaptchaPaymentProcessingRateLimiter::CAPTCHA_FORM => $captcha->getWord()]
|
||||
);
|
||||
$this->model->limit();
|
||||
//Providing CAPTCHA value in a header
|
||||
/** @var DefaultModel $captcha */
|
||||
$captcha = $this->captchaHelper->getCaptcha(CaptchaPaymentProcessingRateLimiter::CAPTCHA_FORM);
|
||||
$captcha->generate();
|
||||
$this->request->setPostValue(
|
||||
'captcha',
|
||||
[CaptchaPaymentProcessingRateLimiter::CAPTCHA_FORM => '']
|
||||
);
|
||||
$this->request->getHeaders()->addHeaderLine('X-Captcha', $captcha->getWord());
|
||||
$this->model->limit();
|
||||
|
||||
//Providing invalid CAPTCHA value.
|
||||
$this->request->setPostValue(
|
||||
'captcha',
|
||||
[CaptchaPaymentProcessingRateLimiter::CAPTCHA_FORM => 'invalid']
|
||||
);
|
||||
$this->request->getHeaders()->removeHeader($this->request->getHeaders()->get('X-Captcha'));
|
||||
try {
|
||||
$this->model->limit();
|
||||
$limited = false;
|
||||
} catch (LocalizedException $exception) {
|
||||
$limited = true;
|
||||
}
|
||||
//CAPTCHA was validated
|
||||
$this->assertTrue($limited);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Checkout\Model;
|
||||
|
||||
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Checkout\Model\Session as CheckoutSession;
|
||||
use Magento\Checkout\Model\SessionFactory as CheckoutSessionFactory;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\Quote\Api\CartRepositoryInterface;
|
||||
use Magento\Quote\Api\Data\CartInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Store\ExecuteInStoreContext;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test for checkout cart model.
|
||||
*
|
||||
* @see \Magento\Checkout\Model\Cart
|
||||
* @magentoDbIsolation enabled
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class CartTest extends TestCase
|
||||
{
|
||||
/** @var ObjectManagerInterface */
|
||||
private $objectManager;
|
||||
|
||||
/** @var CartFactory */
|
||||
private $cartFactory;
|
||||
|
||||
/** @var ProductInterfaceFactory */
|
||||
private $productFactory;
|
||||
|
||||
/** @var ProductRepositoryInterface */
|
||||
private $productRepository;
|
||||
|
||||
/** @var ExecuteInStoreContext */
|
||||
private $executeInStoreContext;
|
||||
|
||||
/** @var CheckoutSession */
|
||||
private $checkoutSession;
|
||||
|
||||
/** @var CartInterface */
|
||||
private $quote;
|
||||
|
||||
/** @var CartRepositoryInterface */
|
||||
private $quoteRepository;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->cartFactory = $this->objectManager->get(CartFactory::class);
|
||||
$this->productFactory = $this->objectManager->get(ProductInterfaceFactory::class);
|
||||
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
|
||||
$this->productRepository->cleanCache();
|
||||
$this->executeInStoreContext = $this->objectManager->get(ExecuteInStoreContext::class);
|
||||
$this->checkoutSession = $this->objectManager->get(CheckoutSessionFactory::class)->create();
|
||||
$this->quoteRepository = $this->objectManager->get(CartRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if ($this->quote instanceof CartInterface) {
|
||||
$this->quoteRepository->delete($this->quote);
|
||||
}
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Checkout/_files/set_product_min_in_cart.php
|
||||
* @magentoAppIsolation enabled
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAddProductWithLowerQty(): void
|
||||
{
|
||||
$cart = $this->cartFactory->create();
|
||||
$this->expectException(LocalizedException::class);
|
||||
$this->expectExceptionMessage((string)__('The fewest you may purchase is %1', 3));
|
||||
$product = $this->productRepository->get('simple');
|
||||
$cart->addProduct($product->getId(), ['qty' => 1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Checkout/_files/set_product_min_in_cart.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAddProductWithNoQty(): void
|
||||
{
|
||||
$cart = $this->cartFactory->create();
|
||||
$product = $this->productRepository->get('simple');
|
||||
$cart->addProduct($product->getId(), [])->save();
|
||||
$this->quote = $cart->getQuote();
|
||||
$this->assertCount(1, $cart->getItems());
|
||||
$this->assertEquals($product->getId(), $this->checkoutSession->getLastAddedProductId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddNotExistingProduct(): void
|
||||
{
|
||||
$product = $this->productFactory->create();
|
||||
$this->expectExceptionObject(
|
||||
new LocalizedException(__('The product wasn\'t found. Verify the product and try again.'))
|
||||
);
|
||||
$this->cartFactory->create()->addProduct($product);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddNotExistingProductId(): void
|
||||
{
|
||||
$this->expectExceptionObject(
|
||||
new LocalizedException(__('The product wasn\'t found. Verify the product and try again.'))
|
||||
);
|
||||
$this->cartFactory->create()->addProduct(989);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
|
||||
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAddProductFromUnavailableWebsite(): void
|
||||
{
|
||||
$product = $this->productRepository->get('simple');
|
||||
$this->expectExceptionObject(
|
||||
new LocalizedException(__('The product wasn\'t found. Verify the product and try again.'))
|
||||
);
|
||||
$this->executeInStoreContext
|
||||
->execute('fixture_second_store', [$this->cartFactory->create(), 'addProduct'], $product->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAddProductWithInvalidRequest(): void
|
||||
{
|
||||
$product = $this->productRepository->get('simple');
|
||||
$message = __('We found an invalid request for adding product to quote.');
|
||||
$this->expectExceptionObject(new LocalizedException($message));
|
||||
$this->cartFactory->create()->addProduct($product->getId(), '');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,313 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Checkout\Model;
|
||||
|
||||
use Magento\Catalog\Api\Data\ProductTierPriceInterface;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Model\Product\Attribute\Source\Status;
|
||||
use Magento\Customer\Api\CustomerRepositoryInterface;
|
||||
use Magento\Customer\Model\Session as CustomerSession;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\Quote\Api\CartRepositoryInterface;
|
||||
use Magento\Quote\Api\Data\CartInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Quote\Model\GetQuoteByReservedOrderId;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Checkout Session model test.
|
||||
*
|
||||
* @see \Magento\Checkout\Model\Session
|
||||
* @magentoDbIsolation enabled
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class SessionTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var CustomerRepositoryInterface
|
||||
*/
|
||||
private $customerRepository;
|
||||
|
||||
/**
|
||||
* @var CustomerSession
|
||||
*/
|
||||
private $customerSession;
|
||||
|
||||
/**
|
||||
* @var Session
|
||||
*/
|
||||
private $checkoutSession;
|
||||
|
||||
/**
|
||||
* @var GetQuoteByReservedOrderId
|
||||
*/
|
||||
private $getQuoteByReservedOrderId;
|
||||
|
||||
/**
|
||||
* @var ProductRepositoryInterface
|
||||
*/
|
||||
private $productRepository;
|
||||
|
||||
/**
|
||||
* @var CartRepositoryInterface
|
||||
*/
|
||||
private $quoteRepository;
|
||||
|
||||
/**
|
||||
* @var CartInterface
|
||||
*/
|
||||
private $quote;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->customerRepository = $this->objectManager->create(CustomerRepositoryInterface::class);
|
||||
$this->customerSession = $this->objectManager->get(CustomerSession::class);
|
||||
$this->checkoutSession = $this->objectManager->get(Session::class);
|
||||
$this->getQuoteByReservedOrderId = $this->objectManager->get(GetQuoteByReservedOrderId::class);
|
||||
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
|
||||
$this->productRepository->cleanCache();
|
||||
$this->quoteRepository = $this->objectManager->get(CartRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if ($this->quote instanceof CartInterface) {
|
||||
$this->quoteRepository->delete($this->quote);
|
||||
}
|
||||
$this->customerSession->setCustomerId(null);
|
||||
$this->checkoutSession->clearQuote();
|
||||
$this->checkoutSession->setCustomerData(null);
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that quote items and totals are correct when product becomes unavailable.
|
||||
*
|
||||
* @magentoDataFixture Magento/Sales/_files/quote.php
|
||||
* @magentoAppIsolation enabled
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetQuoteWithUnavailableProduct(): void
|
||||
{
|
||||
$reservedOrderId = 'test01';
|
||||
$quoteGrandTotal = 10;
|
||||
$quote = $this->getQuoteByReservedOrderId->execute($reservedOrderId);
|
||||
$this->assertEquals(1, $quote->getItemsCount());
|
||||
$this->assertCount(1, $quote->getItems());
|
||||
$this->assertEquals($quoteGrandTotal, $quote->getShippingAddress()->getBaseGrandTotal());
|
||||
$product = $this->productRepository->get('simple');
|
||||
$product->setStatus(Status::STATUS_DISABLED);
|
||||
$this->productRepository->save($product);
|
||||
$this->checkoutSession->setQuoteId($quote->getId());
|
||||
$quote = $this->checkoutSession->getQuote();
|
||||
$this->assertEquals(0, $quote->getItemsCount());
|
||||
$this->assertEmpty($quote->getItems());
|
||||
$this->assertEquals(0, $quote->getShippingAddress()->getBaseGrandTotal());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test covers case when quote is not yet initialized and customer data is set to checkout session model.
|
||||
*
|
||||
* Expected result - quote object should be loaded and customer data should be set to it.
|
||||
*
|
||||
* @magentoDataFixture Magento/Sales/_files/quote_with_customer.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetQuoteNotInitializedCustomerSet(): void
|
||||
{
|
||||
$customer = $this->customerRepository->getById(1);
|
||||
$this->checkoutSession->setCustomerData($customer);
|
||||
$quote = $this->checkoutSession->getQuote();
|
||||
$this->validateCustomerDataInQuote($quote);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test covers case when quote is not yet initialized and customer data is set to customer session model.
|
||||
*
|
||||
* Expected result - quote object should be loaded and customer data should be set to it.
|
||||
*
|
||||
* @magentoDataFixture Magento/Sales/_files/quote_with_customer.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetQuoteNotInitializedCustomerLoggedIn(): void
|
||||
{
|
||||
$customer = $this->customerRepository->getById(1);
|
||||
$this->customerSession->setCustomerDataObject($customer);
|
||||
$quote = $this->checkoutSession->getQuote();
|
||||
$this->validateCustomerDataInQuote($quote);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Sales/_files/quote_with_customer.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetQuoteWithMismatchingSession(): void
|
||||
{
|
||||
$quote = $this->getQuoteByReservedOrderId->execute('test01');
|
||||
$this->checkoutSession->setQuoteId($quote->getId());
|
||||
$this->quote = $this->checkoutSession->getQuote();
|
||||
$this->assertEmpty($this->quote->getCustomerId());
|
||||
$this->assertNotEquals($quote->getId(), $this->quote->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tes merging of customer data into initialized quote object.
|
||||
*
|
||||
* Conditions:
|
||||
* 1. Quote without customer data is set to checkout session
|
||||
* 2. Customer without associated quote is set to checkout session
|
||||
*
|
||||
* Expected result:
|
||||
* Quote which is set to checkout session should contain customer data
|
||||
*
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testLoadCustomerQuoteCustomerWithoutQuote(): void
|
||||
{
|
||||
$this->quote = $this->checkoutSession->getQuote();
|
||||
$this->assertEmpty(
|
||||
$this->quote->getCustomerId(),
|
||||
'Precondition failed: Customer data must not be set to quote'
|
||||
);
|
||||
$this->assertEmpty(
|
||||
$this->quote->getCustomerEmail(),
|
||||
'Precondition failed: Customer data must not be set to quote'
|
||||
);
|
||||
self::assertEquals(
|
||||
'1',
|
||||
$this->quote->getCustomerIsGuest(),
|
||||
'Precondition failed: Customer must be as guest in quote'
|
||||
);
|
||||
$customer = $this->customerRepository->getById(1);
|
||||
$this->customerSession->setCustomerDataObject($customer);
|
||||
$this->quote = $this->checkoutSession->getQuote();
|
||||
$this->assertEmpty(
|
||||
$this->quote->getCustomerEmail(),
|
||||
'Precondition failed: Customer data must not be set to quote'
|
||||
);
|
||||
$this->checkoutSession->loadCustomerQuote();
|
||||
$this->quote = $this->checkoutSession->getQuote();
|
||||
$this->validateCustomerDataInQuote($this->quote);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoDataFixture Magento/Sales/_files/quote.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetQuoteWithProductWithTierPrice(): void
|
||||
{
|
||||
$reservedOrderId = 'test01';
|
||||
$customerGroupId = 1;
|
||||
$tierPriceQty = 1;
|
||||
$tierPriceValue = 9;
|
||||
$product = $this->productRepository->get('simple');
|
||||
$tierPrice = $this->objectManager->get(ProductTierPriceInterface::class)
|
||||
->setCustomerGroupId($customerGroupId)
|
||||
->setQty($tierPriceQty)
|
||||
->setValue($tierPriceValue);
|
||||
$product->setTierPrices([$tierPrice]);
|
||||
$this->productRepository->save($product);
|
||||
$quote = $this->getQuoteByReservedOrderId->execute($reservedOrderId);
|
||||
$this->checkoutSession->setQuoteId($quote->getId());
|
||||
$quote = $this->checkoutSession->getQuote();
|
||||
$item = $quote->getItems()[0];
|
||||
$quoteProduct = $item->getProduct();
|
||||
$this->assertEquals(10, $quoteProduct->getTierPrice($tierPriceQty));
|
||||
$customer = $this->customerRepository->getById(1);
|
||||
$this->customerSession->setCustomerDataAsLoggedIn($customer);
|
||||
$quote = $this->checkoutSession->getQuote();
|
||||
$item = $quote->getItems()[0];
|
||||
$quoteProduct = $item->getProduct();
|
||||
$this->assertEquals($tierPriceValue, $quoteProduct->getTierPrice(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test covers case when quote is not yet initialized and customer is guest
|
||||
*
|
||||
* Expected result - quote object should be loaded with customer as guest
|
||||
*/
|
||||
public function testGetQuoteNotInitializedGuest()
|
||||
{
|
||||
$quote = $this->checkoutSession->getQuote();
|
||||
self::assertEquals('1', $quote->getCustomerIsGuest());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
|
||||
* @magentoDataFixture Magento/Checkout/_files/quote_with_customer_without_address.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMergeGuestQuoteWithCustomerQuote(): void
|
||||
{
|
||||
$guestQuote = $this->getQuoteByReservedOrderId->execute('test_order_with_simple_product_without_address');
|
||||
$customerQuote = $this->getQuoteByReservedOrderId->execute('test_order_with_customer_without_address');
|
||||
$this->checkoutSession->setQuoteId($guestQuote->getId());
|
||||
$this->customerSession->setCustomerId(1);
|
||||
$updatedQuote = $this->checkoutSession->loadCustomerQuote()->getQuote();
|
||||
$this->assertNull($this->getQuoteByReservedOrderId->execute('test_order_with_simple_product_without_address'));
|
||||
$this->assertEquals($customerQuote->getId(), $updatedQuote->getId());
|
||||
$this->assertCount(2, $updatedQuote->getItems());
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that quote has customer data specified in customer fixture.
|
||||
*
|
||||
* @param CartInterface $quote
|
||||
* @return void
|
||||
*/
|
||||
private function validateCustomerDataInQuote(CartInterface $quote): void
|
||||
{
|
||||
$customerIdFromFixture = 1;
|
||||
$customerEmailFromFixture = 'customer@example.com';
|
||||
$customerFirstNameFromFixture = 'John';
|
||||
$this->assertEquals(
|
||||
$customerEmailFromFixture,
|
||||
$quote->getCustomerEmail(),
|
||||
'Customer email was not set to Quote correctly.'
|
||||
);
|
||||
$this->assertEquals(
|
||||
$customerIdFromFixture,
|
||||
$quote->getCustomerId(),
|
||||
'Customer ID was not set to Quote correctly.'
|
||||
);
|
||||
$this->assertEquals(
|
||||
$customerFirstNameFromFixture,
|
||||
$quote->getCustomerFirstname(),
|
||||
'Customer first name was not set to Quote correctly.'
|
||||
);
|
||||
self::assertEquals(
|
||||
'0',
|
||||
$quote->getCustomerIsGuest(),
|
||||
'Customer should not be as guest in Quote.'
|
||||
);
|
||||
}
|
||||
}
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Checkout\Model;
|
||||
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\Checkout\Api\Data\ShippingInformationInterface;
|
||||
use Magento\Checkout\Api\PaymentInformationManagementInterface;
|
||||
use Magento\Checkout\Api\ShippingInformationManagementInterface;
|
||||
use Magento\Customer\Api\CustomerRepositoryInterface;
|
||||
use Magento\Quote\Api\CartItemRepositoryInterface;
|
||||
use Magento\Quote\Api\CartManagementInterface;
|
||||
use Magento\Quote\Api\Data\AddressInterfaceFactory;
|
||||
use Magento\Quote\Api\Data\CartItemInterface;
|
||||
use Magento\Quote\Api\Data\PaymentInterface;
|
||||
use Magento\Quote\Api\ShipmentEstimationInterface;
|
||||
use Magento\Sales\Api\InvoiceOrderInterface;
|
||||
|
||||
/**
|
||||
* Shipping information managment test.
|
||||
*/
|
||||
class ShippingInformationManagementTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/** @var CartManagementInterface */
|
||||
private $cartManagement;
|
||||
|
||||
/** @var CartItemRepositoryInterface */
|
||||
private $cartItemRepository;
|
||||
|
||||
/** @var CartItemInterface */
|
||||
private $cartItem;
|
||||
|
||||
/** @var ShippingInformationManagementInterface */
|
||||
private $shippingInformationManagement;
|
||||
|
||||
/** @var ShippingInformationInterface */
|
||||
private $shippingInformation;
|
||||
|
||||
/** @var CustomerRepositoryInterface */
|
||||
private $customerRepository;
|
||||
|
||||
/** @var AddressInterfaceFactory */
|
||||
private $apiAddressFactory;
|
||||
|
||||
/** @var ShipmentEstimationInterface */
|
||||
private $shipmentEstimation;
|
||||
|
||||
/** @var PaymentInformationManagementInterface */
|
||||
private $paymentInformationManagement;
|
||||
|
||||
/** @var PaymentInterface */
|
||||
private $payment;
|
||||
|
||||
/** @var InvoiceOrderInterface */
|
||||
private $invoiceOrder;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
|
||||
$this->cartManagement = $objectManager->create(CartManagementInterface::class);
|
||||
$this->cartItemRepository = $objectManager->create(CartItemRepositoryInterface::class);
|
||||
$this->cartItem = $objectManager->create(CartItemInterface::class);
|
||||
$this->shippingInformationManagement = $objectManager->create(ShippingInformationManagementInterface::class);
|
||||
$this->shippingInformation = $objectManager->create(ShippingInformationInterface::class);
|
||||
$this->customerRepository = $objectManager->create(CustomerRepositoryInterface::class);
|
||||
$this->apiAddressFactory = $objectManager->create(AddressInterfaceFactory::class);
|
||||
$this->shipmentEstimation = $objectManager->create(ShipmentEstimationInterface::class);
|
||||
$this->paymentInformationManagement = $objectManager->create(PaymentInformationManagementInterface::class);
|
||||
$this->payment = $objectManager->create(PaymentInterface::class);
|
||||
$this->invoiceOrder = $objectManager->create(InvoiceOrderInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoDataFixture Magento/Customer/_files/customer_address.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/product_virtual_in_stock.php
|
||||
*/
|
||||
public function testQuoteApiWithOnlyVirtualProducts()
|
||||
{
|
||||
$customer = $this->customerRepository->getById(1);
|
||||
|
||||
// Create empty quote
|
||||
$quoteId = $this->cartManagement->createEmptyCartForCustomer($customer->getId());
|
||||
|
||||
$cartItem = $this->cartItem
|
||||
->setSku('virtual-product')
|
||||
->setQty(1)
|
||||
->setQuoteId($quoteId);
|
||||
|
||||
// Add item to cart
|
||||
$this->cartItemRepository->save($cartItem);
|
||||
|
||||
$billingAddress = $shippingAddress = null;
|
||||
foreach ($customer->getAddresses() as $address) {
|
||||
$billingAddress = $address;
|
||||
$shippingAddress = $address;
|
||||
break;
|
||||
}
|
||||
|
||||
/** @var \Magento\Quote\Model\Quote\Address $apiBillingAddress */
|
||||
$apiBillingAddress = $this->apiAddressFactory->create();
|
||||
$apiBillingAddress->setRegion($billingAddress->getRegion())
|
||||
->setRegionId($billingAddress->getRegionId())
|
||||
->setCountryId($billingAddress->getCountryId())
|
||||
->setStreet($billingAddress->getStreet())
|
||||
->setPostcode($billingAddress->getPostcode())
|
||||
->setCity($billingAddress->getCity())
|
||||
->setFirstname($billingAddress->getFirstname())
|
||||
->setLastname($billingAddress->getLastname())
|
||||
->setEmail($customer->getEmail())
|
||||
->setTelephone($billingAddress->getTelephone());
|
||||
|
||||
/** @var \Magento\Quote\Model\Quote\Address $apiShippingAddress */
|
||||
$apiShippingAddress = $this->apiAddressFactory->create();
|
||||
$apiShippingAddress->setRegion($shippingAddress->getRegion())
|
||||
->setRegionId($shippingAddress->getRegionId())
|
||||
->setCountryId($shippingAddress->getCountryId())
|
||||
->setStreet($shippingAddress->getStreet())
|
||||
->setPostcode($shippingAddress->getPostcode())
|
||||
->setCity($shippingAddress->getCity())
|
||||
->setFirstname($shippingAddress->getFirstname())
|
||||
->setLastname($shippingAddress->getLastname())
|
||||
->setEmail($customer->getEmail())
|
||||
->setTelephone($shippingAddress->getTelephone());
|
||||
|
||||
// Estimate shipping
|
||||
$this->shipmentEstimation->estimateByExtendedAddress($quoteId, $apiShippingAddress);
|
||||
|
||||
$addressInformation = $this->shippingInformation
|
||||
->setBillingAddress($apiBillingAddress)
|
||||
->setShippingAddress($apiShippingAddress)
|
||||
->setShippingCarrierCode('flatrate')
|
||||
->setShippingMethodCode('flatrate');
|
||||
|
||||
// Set address information on quote
|
||||
$this->shippingInformationManagement->saveAddressInformation($quoteId, $addressInformation);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user