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,227 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\SalesRule\Model\Coupon;
use Magento\Framework\App\Request\Http;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magento\SalesRule\Api\Exception\CodeRequestLimitException;
use Magento\TestFramework\ObjectManager;
use PHPUnit\Framework\TestCase;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Customer\Model\Session as CustomerSession;
/**
* Test for captcha based implementation.
*
* @magentoAppArea frontend
*/
class CodeLimitManagerTest extends TestCase
{
/**
* @var CodeLimitManager
*/
private $manager;
/**
* @var CustomerSession
*/
private $customerSession;
/**
* @inheritDoc
*/
protected function setUp(): void
{
/** @var ObjectManager $objectManager */
$objectManager = Bootstrap::getObjectManager();
$this->manager = $objectManager->get(CodeLimitManager::class);
$this->customerSession = $objectManager->get(CustomerSession::class);
/** @var Http $request */
$request = $objectManager->get(RequestInterface::class);
$request->getServer()->set('REMOTE_ADDR', '127.0.0.1');
$objectManager->removeSharedInstance(RemoteAddress::class);
}
/**
* @inheritDoc
*/
protected function tearDown(): void
{
$this->customerSession->logout();
$this->customerSession->clearStorage();
}
/**
* Log in customer by ID.
*
* @param int $id
* @return void
*/
private function loginCustomer(int $id): void
{
if (!$this->customerSession->loginById($id)) {
throw new \RuntimeException('Failed to log in customer');
}
}
/**
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*
* @magentoDataFixture Magento/Customer/_files/customer.php
*
* @magentoConfigFixture default_store customer/captcha/enable 1
*/
public function testCounterDisabled()
{
$this->manager->checkRequest('fakeCode1');
$this->loginCustomer(1);
$this->manager->checkRequest('fakeCode2');
}
/**
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*
* @magentoConfigFixture default_store customer/captcha/enable 1
* @magentoConfigFixture default_store customer/captcha/forms sales_rule_coupon_request
* @magentoConfigFixture default_store customer/captcha/failed_attempts_login 3
* @magentoConfigFixture default_store customer/captcha/failed_attempts_ip 5
*
* @magentoDataFixture Magento/Customer/_files/customer.php
*/
public function testUnderLimit()
{
$this->manager->checkRequest('fakeCode3');
$this->manager->checkRequest('fakeCode4');
$this->loginCustomer(1);
$this->manager->checkRequest('fakeCode5');
$this->manager->checkRequest('fakeCode6');
}
/**
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*
* @magentoConfigFixture default_store customer/captcha/enable 1
* @magentoConfigFixture default_store customer/captcha/forms sales_rule_coupon_request
* @magentoConfigFixture default_store customer/captcha/failed_attempts_login 10
* @magentoConfigFixture default_store customer/captcha/failed_attempts_ip 2
*
*/
public function testAboveLimitNotLoggedIn()
{
$this->expectException(\Magento\SalesRule\Api\Exception\CodeRequestLimitException::class);
try {
$this->manager->checkRequest('fakeCode7');
$this->manager->checkRequest('fakeCode8');
} catch (CodeRequestLimitException $exception) {
$this->fail('Attempt denied before reaching the limit');
}
$this->manager->checkRequest('fakeCode9');
}
/**
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*
* @magentoConfigFixture default_store customer/captcha/enable 1
* @magentoConfigFixture default_store customer/captcha/forms sales_rule_coupon_request
* @magentoConfigFixture default_store customer/captcha/failed_attempts_login 2
* @magentoConfigFixture default_store customer/captcha/failed_attempts_ip 10
*
* @magentoDataFixture Magento/Customer/_files/customer.php
*
*/
public function testAboveLimitLoggedIn()
{
$this->expectException(\Magento\SalesRule\Api\Exception\CodeRequestLimitException::class);
try {
$this->loginCustomer(1);
$this->manager->checkRequest('fakeCode10');
$this->manager->checkRequest('fakeCode11');
} catch (CodeRequestLimitException $exception) {
$this->fail('Attempt denied before reaching the limit');
}
$this->manager->checkRequest('fakeCode12');
}
/**
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*
* @magentoConfigFixture default_store customer/captcha/enable 1
* @magentoConfigFixture default_store customer/captcha/forms sales_rule_coupon_request
* @magentoConfigFixture default_store customer/captcha/failed_attempts_login 10
* @magentoConfigFixture default_store customer/captcha/failed_attempts_ip 10
* @magentoConfigFixture default_store customer/captcha/mode always
*
* @magentoDataFixture Magento/Customer/_files/customer.php
*
*/
public function testCustomerNotAllowedWithoutCode()
{
$this->expectException(\Magento\SalesRule\Api\Exception\CodeRequestLimitException::class);
$this->loginCustomer(1);
$this->manager->checkRequest('fakeCode13');
}
/**
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*
* @magentoConfigFixture default_store customer/captcha/enable 1
* @magentoConfigFixture default_store customer/captcha/forms sales_rule_coupon_request
* @magentoConfigFixture default_store customer/captcha/failed_attempts_login 10
* @magentoConfigFixture default_store customer/captcha/failed_attempts_ip 10
* @magentoConfigFixture default_store customer/captcha/mode always
*
*/
public function testGuestNotAllowedWithoutCode()
{
$this->expectException(\Magento\SalesRule\Api\Exception\CodeRequestLimitException::class);
$this->manager->checkRequest('fakeCode14');
}
/**
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*
* @magentoConfigFixture default_store customer/captcha/enable 1
* @magentoConfigFixture default_store customer/captcha/forms sales_rule_coupon_request
* @magentoConfigFixture default_store customer/captcha/failed_attempts_login 2
* @magentoConfigFixture default_store customer/captcha/failed_attempts_ip 10
*
* @magentoDataFixture Magento/SalesRule/_files/rules.php
* @magentoDataFixture Magento/SalesRule/_files/coupons.php
* @magentoDataFixture Magento/Customer/_files/customer.php
*
*/
public function testLoggingOnlyInvalidCodes()
{
$this->expectException(\Magento\SalesRule\Api\Exception\CodeRequestLimitException::class);
try {
$this->loginCustomer(1);
$this->manager->checkRequest('coupon_code');
$this->manager->checkRequest('coupon_code');
$this->manager->checkRequest('fakeCode15');
$this->manager->checkRequest('fakeCode16');
} catch (CodeRequestLimitException $exception) {
$this->fail('Attempts are logged for existing codes');
}
$this->manager->checkRequest('fakeCode17');
}
}
@@ -0,0 +1,138 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\SalesRule\Model\Coupon;
use Magento\Captcha\Model\DefaultModel;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\Request\Http;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\SalesRule\Api\Exception\CodeRequestLimitException;
use PHPUnit\Framework\TestCase;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Captcha\Helper\Data as CaptchaHelper;
use Magento\TestFramework\ObjectManager;
/**
* Test cases related to coupons.
*/
class QuoteRepositoryTest extends TestCase
{
/**
* @var CartRepositoryInterface
*/
private $repo;
/**
* @var SearchCriteriaBuilder
*/
private $criteriaBuilder;
/**
* @var Http
*/
private $request;
/**
* @var CaptchaHelper
*/
private $captchaHelper;
/**
* @inheritDoc
*/
protected function setUp(): void
{
/** @var ObjectManager $objectManager */
$objectManager = Bootstrap::getObjectManager();
/** @var Http $request */
$request = $objectManager->get(RequestInterface::class);
$request->getServer()->set('REMOTE_ADDR', '127.0.0.1');
$this->request = $request;
$objectManager->removeSharedInstance(RemoteAddress::class);
$this->repo = $objectManager->get(CartRepositoryInterface::class);
$this->criteriaBuilder = $objectManager->get(SearchCriteriaBuilder::class);
$this->captchaHelper = $objectManager->get(CaptchaHelper::class);
}
/**
* Load cart from fixture.
*
* @return CartInterface
*/
private function getCart(): CartInterface
{
$carts = $this->repo->getList(
$this->criteriaBuilder->addFilter('reserved_order_id', 'test01')->create()
)->getItems();
if (!$carts) {
throw new \RuntimeException('Cart from fixture not found');
}
return array_shift($carts);
}
/**
* Case when coupon requests limit is reached.
*
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*
* @magentoConfigFixture default_store customer/captcha/enable 1
* @magentoConfigFixture default_store customer/captcha/forms sales_rule_coupon_request
* @magentoConfigFixture default_store customer/captcha/failed_attempts_login 10
* @magentoConfigFixture default_store customer/captcha/failed_attempts_ip 2
*
* @magentoDataFixture Magento/Sales/_files/quote.php
* @magentoDataFixture Magento/SalesRule/_files/coupon_cart_fixed_discount.php
*
*/
public function testAboveLimitFail()
{
$this->expectException(\Magento\SalesRule\Api\Exception\CodeRequestLimitException::class);
//Making number of requests above limit.
try {
$this->repo->save($this->getCart()->setCouponCode('fake20'));
$this->repo->save($this->getCart()->setCouponCode('fake21'));
} catch (CodeRequestLimitException $exception) {
$this->fail('Denied access before the limit is reached.');
}
$this->repo->save($this->getCart()->setCouponCode('fake22'));
}
/**
* Case when coupon requests limit reached but genuine request provided.
*
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*
* @magentoConfigFixture default_store customer/captcha/enable 1
* @magentoConfigFixture default_store customer/captcha/forms sales_rule_coupon_request
* @magentoConfigFixture default_store customer/captcha/failed_attempts_login 10
* @magentoConfigFixture default_store customer/captcha/failed_attempts_ip 2
*
* @magentoDataFixture Magento/Sales/_files/quote.php
* @magentoDataFixture Magento/SalesRule/_files/coupon_cart_fixed_discount.php
*/
public function testAboveLimitSuccess()
{
$this->repo->save($this->getCart()->setCouponCode('fake24'));
$this->repo->save($this->getCart()->setCouponCode('fake25'));
//Providing genuine proof.
/** @var DefaultModel $captcha */
$captcha = $this->captchaHelper->getCaptcha('sales_rule_coupon_request');
$captcha->generate();
$this->request->setPostValue('captcha', ['sales_rule_coupon_request' => $captcha->getWord()]);
$this->repo->save($this->getCart()->setCouponCode('fake26'));
}
}
@@ -0,0 +1,158 @@
<?php
/**
* Copyright 2023 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);
namespace Magento\SalesRule\Model\Coupon;
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
use Magento\Checkout\Test\Fixture\SetBillingAddress as SetBillingAddress;
use Magento\Checkout\Test\Fixture\SetDeliveryMethod;
use Magento\Checkout\Test\Fixture\SetPaymentMethod as SetPaymentMethod;
use Magento\Checkout\Test\Fixture\SetShippingAddress as SetShippingAddress;
use Magento\Customer\Test\Fixture\Customer;
use Magento\Framework\MessageQueue\ConsumerFactory;
use Magento\Quote\Api\CartManagementInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\CouponManagementInterface;
use Magento\Quote\Test\Fixture\AddProductToCart;
use Magento\Quote\Test\Fixture\CustomerCart;
use Magento\Quote\Test\Fixture\GuestCart;
use Magento\Sales\Api\OrderManagementInterface;
use Magento\SalesRule\Test\Fixture\Rule as SalesRuleFixture;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorage;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\MessageQueue\ClearQueueProcessor;
use PHPUnit\Framework\TestCase;
class UpdateCouponUsagesTest extends TestCase
{
/**
* @var DataFixtureStorage
*/
private $fixtures;
/**
* @var CouponManagementInterface
*/
private $couponManagement;
/**
* @var CartManagementInterface
*/
private $cartManagement;
/**
* @var CartRepositoryInterface
*/
private $cartRepository;
/**
* @var OrderManagementInterface
*/
private $orderManagement;
/**
* @var ConsumerFactory
*/
private $consumerFactory;
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$clearQueueProcessor = $objectManager->get(ClearQueueProcessor::class);
$clearQueueProcessor->execute('sales.rule.update.coupon.usage');
$this->fixtures = $objectManager->get(DataFixtureStorageManager::class)->getStorage();
$this->couponManagement = $objectManager->get(CouponManagementInterface::class);
$this->cartManagement = $objectManager->get(CartManagementInterface::class);
$this->cartRepository = $objectManager->get(CartRepositoryInterface::class);
$this->orderManagement = $objectManager->get(OrderManagementInterface::class);
$this->consumerFactory = $objectManager->get(ConsumerFactory::class);
}
#[
DataFixture(ProductFixture::class, as: 'p1'),
DataFixture(
SalesRuleFixture::class,
['coupon_code' => 'one_per_customer', 'uses_per_customer' => 1, 'discount_amount' => 10]
),
DataFixture(Customer::class, as: 'customer'),
DataFixture(CustomerCart::class, ['customer_id' => '$customer.id$'], 'cart1'),
DataFixture(AddProductToCart::class, ['cart_id' => '$cart1.id$', 'product_id' => '$p1.id$']),
DataFixture(SetBillingAddress::class, ['cart_id' => '$cart1.id$']),
DataFixture(SetShippingAddress::class, ['cart_id' => '$cart1.id$']),
DataFixture(SetDeliveryMethod::class, ['cart_id' => '$cart1.id$']),
DataFixture(SetPaymentMethod::class, ['cart_id' => '$cart1.id$']),
DataFixture(GuestCart::class, as: 'cart2'),
DataFixture(AddProductToCart::class, ['cart_id' => '$cart2.id$', 'product_id' => '$p1.id$']),
DataFixture(SetBillingAddress::class, ['cart_id' => '$cart2.id$']),
DataFixture(SetShippingAddress::class, ['cart_id' => '$cart2.id$']),
DataFixture(SetDeliveryMethod::class, ['cart_id' => '$cart2.id$']),
DataFixture(SetPaymentMethod::class, ['cart_id' => '$cart2.id$']),
]
public function testCancelOrderBeforeUsageConsumerExecution(): void
{
$cart = $this->fixtures->get('cart1');
$this->couponManagement->set($cart->getId(), 'one_per_customer');
$orderId = $this->cartManagement->placeOrder($cart->getId());
$this->orderManagement->cancel($orderId);
$consumer = $this->consumerFactory->get('sales.rule.update.coupon.usage');
$consumer->process(1);
$cart = $this->fixtures->get('cart2');
$customer = $this->fixtures->get('customer');
$this->cartManagement->assignCustomer($cart->getId(), $customer->getId(), 1);
$cart = $this->cartRepository->get($cart->getId());
$this->couponManagement->set($cart->getId(), 'one_per_customer');
$this->cartManagement->placeOrder($cart->getId());
$consumer->process(1);
}
#[
DataFixture(ProductFixture::class, as: 'p1'),
DataFixture(
SalesRuleFixture::class,
['coupon_code' => 'once', 'uses_per_coupon' => 1, 'discount_amount' => 10]
),
DataFixture(Customer::class, as: 'customer'),
DataFixture(CustomerCart::class, ['customer_id' => '$customer.id$'], 'cart1'),
DataFixture(AddProductToCart::class, ['cart_id' => '$cart1.id$', 'product_id' => '$p1.id$']),
DataFixture(SetBillingAddress::class, ['cart_id' => '$cart1.id$']),
DataFixture(SetShippingAddress::class, ['cart_id' => '$cart1.id$']),
DataFixture(SetDeliveryMethod::class, ['cart_id' => '$cart1.id$']),
DataFixture(SetPaymentMethod::class, ['cart_id' => '$cart1.id$']),
DataFixture(GuestCart::class, as: 'cart2'),
DataFixture(AddProductToCart::class, ['cart_id' => '$cart2.id$', 'product_id' => '$p1.id$']),
DataFixture(SetBillingAddress::class, ['cart_id' => '$cart2.id$']),
DataFixture(SetShippingAddress::class, ['cart_id' => '$cart2.id$']),
DataFixture(SetDeliveryMethod::class, ['cart_id' => '$cart2.id$']),
DataFixture(SetPaymentMethod::class, ['cart_id' => '$cart2.id$']),
]
public function testCancelOrderBeforeConsumerAndRuleTimesUsed(): void
{
$cart = $this->fixtures->get('cart1');
$this->couponManagement->set($cart->getId(), 'once');
$orderId = $this->cartManagement->placeOrder($cart->getId());
$this->orderManagement->cancel($orderId);
$consumer = $this->consumerFactory->get('sales.rule.update.coupon.usage');
$consumer->process(2);
$cart = $this->fixtures->get('cart2');
$customer = $this->fixtures->get('customer');
$this->cartManagement->assignCustomer($cart->getId(), $customer->getId(), 1);
$cart = $this->cartRepository->get($cart->getId());
$this->couponManagement->set($cart->getId(), 'once');
$this->cartManagement->placeOrder($cart->getId());
$consumer->process(1);
}
}