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,102 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\InstantPurchase\Model;
use Magento\Framework\App\Backpressure\ContextInterface;
use Magento\Framework\App\Backpressure\IdentityProviderInterface;
use Magento\Framework\App\Backpressure\SlidingWindow\LimitConfigManagerInterface;
use Magento\Framework\App\Request\Backpressure\ContextFactory;
use Magento\Quote\Model\Backpressure\OrderLimitConfigManager;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Magento\InstantPurchase\Controller\Button\PlaceOrder;
class BackpressureTest extends TestCase
{
/**
* @var ContextFactory
*/
private $contextFactory;
/**
* @var LimitConfigManagerInterface
*/
private $limitConfigManager;
/**
* @var IdentityProviderInterface|MockObject
*/
private $identityProvider;
/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();
$this->identityProvider = $this->createMock(IdentityProviderInterface::class);
$this->contextFactory = Bootstrap::getObjectManager()->create(
ContextFactory::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',
50
],
'customer' => [
ContextInterface::IDENTITY_TYPE_CUSTOMER,
'42',
100
]
];
}
/**
* Verify that backpressure is configured for guests.
*
* @param int $identityType
* @param string $identity
* @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,
int $expectedLimit
): void {
$this->identityProvider->method('fetchIdentityType')->willReturn($identityType);
$this->identityProvider->method('fetchIdentity')->willReturn($identity);
$context = $this->contextFactory->create($this->createMock(PlaceOrder::class));
$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,170 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\InstantPurchase\Model;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Address;
use Magento\Customer\Model\Customer;
use Magento\Framework\ObjectManagerInterface;
use Magento\Quote\Api\Data\ShippingMethodInterface;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Store\Model\Store;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Vault\Api\Data\PaymentTokenInterface;
use PHPUnit\Framework\TestCase;
/**
* @magentoAppIsolation enabled
*/
class InstantPurchaseTest extends TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_address.php
* @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
*/
public function testAvailableWhenEverythingSetUp()
{
$option = $this->invokeTestInstantPurchaseOptionCalculation();
$this->assertTrue($option->isAvailable());
$this->assertInstanceOf(PaymentTokenInterface::class, $option->getPaymentToken());
$this->assertInstanceOf(Address::class, $option->getShippingAddress());
$this->assertInstanceOf(Address::class, $option->getBillingAddress());
$this->assertInstanceOf(ShippingMethodInterface::class, $option->getShippingMethod());
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_address.php
*/
public function testNotAvailableWithoutPaymentToken()
{
$option = $this->invokeTestInstantPurchaseOptionCalculation();
$this->assertFalse($option->isAvailable());
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_address.php
* @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
* @magentoConfigFixture current_store payment/fake_vault/active 0
*/
public function testNotAvailableWhenVaultNotActive()
{
$option = $this->invokeTestInstantPurchaseOptionCalculation();
$this->assertFalse($option->isAvailable());
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_address.php
* @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
* @magentoConfigFixture current_store payment/fake/active 0
*/
public function testNotAvailableWhenVaultProviderNotActive()
{
$option = $this->invokeTestInstantPurchaseOptionCalculation();
$this->assertFalse($option->isAvailable());
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
*/
public function testNotAvailableWithoutAddresses()
{
$option = $this->invokeTestInstantPurchaseOptionCalculation();
$this->assertFalse($option->isAvailable());
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_address.php
* @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
* @magentoConfigFixture current_store carriers/flatrate/active 0
*/
public function testNotAvailableWhenShippingMethodsDisabled()
{
$option = $this->invokeTestInstantPurchaseOptionCalculation();
$this->assertFalse($option->isAvailable());
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_address.php
* @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
* @magentoConfigFixture current_store sales/instant_purchase/active 0
*/
public function testNotAvailableWhenDisabledInConfig()
{
$option = $this->invokeTestInstantPurchaseOptionCalculation();
$this->assertFalse($option->isAvailable());
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_address.php
* @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
* @magentoConfigFixture current_store payment/fake_vault/instant_purchase/supported 0
*/
public function testNotAvailableWhenSupportSwitchedOffForVault()
{
$option = $this->invokeTestInstantPurchaseOptionCalculation();
$this->assertFalse($option->isAvailable());
}
/**
* Run system under test
*
* @return InstantPurchaseOption
*/
private function invokeTestInstantPurchaseOptionCalculation(): InstantPurchaseOption
{
/** @var InstantPurchaseInterface $instantPurchase */
$instantPurchase = $this->objectManager->create(InstantPurchaseInterface::class);
$store = $this->getFixtureStore();
$customer = $this->getFixtureCustomer();
$option = $instantPurchase->getOption($store, $customer);
return $option;
}
/**
* Returns Store created by fixture.
*
* @return Store
*/
private function getFixtureStore(): Store
{
$repository = $this->objectManager->create(StoreRepositoryInterface::class);
$store = $repository->get('default');
return $store;
}
/**
* Returns Customer created by fixture.
*
* @return Customer
*/
private function getFixtureCustomer(): Customer
{
$repository = $this->objectManager->create(CustomerRepositoryInterface::class);
$customerData = $repository->getById(1);
$customer = $this->objectManager->create(Customer::class);
$customer->updateData($customerData);
return $customer;
}
}
@@ -0,0 +1,266 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\InstantPurchase\Model;
use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Customer;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Store\Model\Store;
use PHPUnit\Framework\TestCase;
use Magento\TestFramework\Helper\Bootstrap;
/**
* @magentoAppIsolation enabled
* @magentoDbIsolation enabled
*/
class PlaceOrderTest extends TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_address.php
* @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testPlaceOrderForSimpleProduct()
{
$this->invokeTestProductPlacement(
'simple',
[],
'Simple Product Ordered.'
);
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_address.php
* @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
* @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
*/
public function testPlaceOrderForConfigurableProduct()
{
$configurable = $this->getFixtureProduct('configurable');
$simple = $this->getFixtureProduct('simple_10');
$configurableAttributes = $configurable->getTypeInstance()->getConfigurableAttributes($configurable);
$selectedOptions = [];
foreach ($configurableAttributes as $configurableAttribute) {
$configurableAttributeId = $configurableAttribute->getData('attribute_id');
$configurableAttributeCode = $configurableAttribute->getProductAttribute()->getData('attribute_code');
$configurableAttributeValueInSimple = $simple->getCustomAttribute($configurableAttributeCode)->getValue();
$selectedOptions[$configurableAttributeId] = $configurableAttributeValueInSimple;
}
$this->invokeTestProductPlacement(
$configurable->getSku(),
[
'super_attribute' => $selectedOptions,
],
'Configurable Product Ordered.'
);
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_address.php
* @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
* @magentoDataFixture Magento/GroupedProduct/_files/product_grouped.php
*/
public function testPlaceOrderForGroupedProduct()
{
$grouped = $this->getFixtureProduct('grouped-product');
$selectedQuantities = [];
foreach ($grouped->getTypeInstance()->getAssociatedProductIds($grouped) as $associatedProductId) {
$selectedQuantities[$associatedProductId] = rand(1, 3);
}
$this->invokeTestProductPlacement(
$grouped->getSku(),
[
'super_group' => $selectedQuantities,
],
'Grouped Product Ordered.'
);
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_address.php
* @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
* @magentoDataFixture Magento/Downloadable/_files/product_downloadable.php
*/
public function testPlaceOrderForDownloadableProduct()
{
$downloadable = $this->getFixtureProduct('downloadable-product');
$selectedLinks = [];
foreach ($downloadable->getTypeInstance()->getLinks($downloadable) as $link) {
$selectedLinks[] = $link->getId();
}
$this->invokeTestProductPlacement(
$downloadable->getSku(),
[
'links' => $selectedLinks
],
'Downloadable Product Ordered.'
);
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_address.php
* @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
* @magentoDataFixture Magento/Catalog/_files/product_virtual.php
*/
public function testPlaceOrderForVirtualProduct()
{
$this->invokeTestProductPlacement(
'virtual-product',
[],
'Virtual Product Ordered.'
);
}
/**
* Run system under test.
*
* @param $productSku
* @param array $productRequest
* @param string $expectedResult
* @return int order identifier
*/
private function invokeTestProductPlacement($productSku, array $productRequest, string $expectedResult): int
{
/** @var PlaceOrder $model */
$model = $this->objectManager->create(PlaceOrder::class);
$store = $this->getFixtureStore();
$customer = $this->getFixtureCustomer();
$instantPurchaseOption = $this->createInstantPurchaseOptionFromFixture();
$product = $this->getFixtureProduct($productSku);
$orderId = $model->placeOrder(
$store,
$customer,
$instantPurchaseOption,
$product,
array_merge(
[
'qty' => '1',
'options' => $this->createProductOptionsRequest($product)
],
$productRequest
)
);
$this->assertNotEmpty($orderId, $expectedResult);
return $orderId;
}
/**
* Returns Store created by fixture.
*
* @return Store
*/
private function getFixtureStore(): Store
{
$repository = $this->objectManager->create(StoreRepositoryInterface::class);
$store = $repository->get('default');
return $store;
}
/**
* Returns Customer created by fixture.
*
* @return Customer
*/
private function getFixtureCustomer(): Customer
{
$repository = $this->objectManager->create(CustomerRepositoryInterface::class);
$customerData = $repository->getById(1);
$customer = $this->objectManager->create(Customer::class);
$customer->updateData($customerData);
return $customer;
}
/**
* Returns Product created by fixture.
*
* @param string $sku
* @return Product
*/
private function getFixtureProduct(string $sku): Product
{
$repository = $this->objectManager->create(ProductRepositoryInterface::class);
$product = $repository->get($sku, false, $this->getFixtureStore()->getId());
$product->setData('salable', true);
return $product;
}
/**
* Creates instant purchase option based on data from fixture.
*
* @return InstantPurchaseOption
*/
private function createInstantPurchaseOptionFromFixture(): InstantPurchaseOption
{
$factory = $this->objectManager->get(InstantPurchaseOptionLoadingFactory::class);
$fixtureCustomer = $this->getFixtureCustomer();
$option = $factory->create(
$fixtureCustomer->getId(),
'fakePublicHash', // @see Magento/InstantPurchase/_files/fake_payment_token.php
$fixtureCustomer->getDefaultShippingAddress()->getId(),
$fixtureCustomer->getDefaultBillingAddress()->getId(),
'instant-purchase',
'cheapest'
);
return $option;
}
/**
* Creates custom options selection product request data.
*
* @param Product $product
* @return array
*/
private function createProductOptionsRequest(Product $product): array
{
$options = [];
/** @var Product\Option $option */
foreach ($product->getOptions() as $option) {
switch ($option->getGroupByType()) {
case ProductCustomOptionInterface::OPTION_GROUP_DATE:
$value = [
'year' => date('Y'),
'month' => date('n'),
'day' => date('j'),
'hour' => date('G'),
'minute' => date('i')
];
break;
case ProductCustomOptionInterface::OPTION_GROUP_SELECT:
$value = key($option->getValues());
break;
default:
$value = 'test';
break;
}
$options[$option->getId()] = $value;
}
return $options;
}
}