This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace Esun\Cart\Test\Unit\Model;
use Esun\Cart\Model\TestModeConfig;
use Magento\Customer\Model\Context as CustomerContext;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Http\Context as HttpContext;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class TestModeConfigTest extends TestCase
{
#[DataProvider('restrictionProvider')]
public function testRestrictionUsesModeAndCustomerGroup(
bool $enabled,
?string $allowedGroups,
int $currentGroupId,
bool $expected
): void {
$scopeConfig = $this->createMock(ScopeConfigInterface::class);
$scopeConfig->method('isSetFlag')->willReturn($enabled);
$scopeConfig->method('getValue')->willReturn($allowedGroups);
$httpContext = $this->createMock(HttpContext::class);
$httpContext->method('getValue')
->with(CustomerContext::CONTEXT_GROUP)
->willReturn($currentGroupId);
self::assertSame($expected, (new TestModeConfig($scopeConfig, $httpContext))->isRestricted());
}
public static function restrictionProvider(): array
{
return [
'mode disabled' => [false, null, 1, false],
'allowed group' => [true, '1,3', 3, false],
'blocked group' => [true, '1,3', 2, true],
'no groups configured' => [true, null, 1, true],
];
}
public function testPopupTextUsesConfiguredValues(): void
{
$config = $this->createConfig([
'esun_cart/test_mode/popup_title' => 'Private Preview',
'esun_cart/test_mode/popup_message' => 'Purchasing opens soon.',
]);
self::assertSame('Private Preview', $config->getPopupTitle());
self::assertSame('Purchasing opens soon.', $config->getPopupMessage());
}
public function testPopupTextFallsBackWhenValuesAreEmpty(): void
{
$config = $this->createConfig([
'esun_cart/test_mode/popup_title' => ' ',
'esun_cart/test_mode/popup_message' => null,
]);
self::assertSame('Test Mode', $config->getPopupTitle());
self::assertSame(
'Our website is currently in beta testing. Purchases are not available at this time.',
$config->getPopupMessage()
);
}
/** @param array<string, string|null> $values */
private function createConfig(array $values): TestModeConfig
{
$scopeConfig = $this->createMock(ScopeConfigInterface::class);
$scopeConfig->method('getValue')->willReturnCallback(
static fn (string $path): ?string => $values[$path] ?? null
);
return new TestModeConfig($scopeConfig, $this->createMock(HttpContext::class));
}
}
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Esun\Cart\Test\Unit\Plugin\TestMode;
use Esun\Cart\Model\TestModeConfig;
use Esun\Cart\Plugin\TestMode\RestrictAddToCartPlugin;
use Magento\Checkout\Model\Cart;
use Magento\Framework\Exception\LocalizedException;
use PHPUnit\Framework\TestCase;
class RestrictAddToCartPluginTest extends TestCase
{
public function testAllowsAddToCartWhenCustomerIsNotRestricted(): void
{
$config = $this->createMock(TestModeConfig::class);
$config->method('isRestricted')->willReturn(false);
$plugin = new RestrictAddToCartPlugin($config);
self::assertNull($plugin->beforeAddProduct($this->createMock(Cart::class), 10));
}
public function testRejectsAddToCartWhenCustomerIsRestricted(): void
{
$config = $this->createMock(TestModeConfig::class);
$config->method('isRestricted')->willReturn(true);
$plugin = new RestrictAddToCartPlugin($config);
$this->expectException(LocalizedException::class);
$this->expectExceptionMessage('Our website is currently in beta testing.');
$plugin->beforeAddProduct($this->createMock(Cart::class), 10);
}
}
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace Esun\Cart\Test\Unit\Plugin\TestMode;
use Esun\Cart\Model\TestModeConfig;
use Esun\Cart\Plugin\TestMode\RestrictCheckoutPlugin;
use Magento\Checkout\Controller\Index\Index as CheckoutIndex;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Framework\Controller\ResultInterface;
use PHPUnit\Framework\TestCase;
class RestrictCheckoutPluginTest extends TestCase
{
public function testContinuesToCheckoutWhenCustomerIsNotRestricted(): void
{
$config = $this->createMock(TestModeConfig::class);
$config->method('isRestricted')->willReturn(false);
$redirectFactory = $this->createMock(RedirectFactory::class);
$expectedResult = $this->createMock(ResultInterface::class);
$plugin = new RestrictCheckoutPlugin($config, $redirectFactory);
$result = $plugin->aroundExecute(
$this->createMock(CheckoutIndex::class),
static fn (): ResultInterface => $expectedResult
);
self::assertSame($expectedResult, $result);
}
public function testRedirectsRestrictedCustomerToCart(): void
{
$config = $this->createMock(TestModeConfig::class);
$config->method('isRestricted')->willReturn(true);
$redirect = $this->createMock(Redirect::class);
$redirect->expects(self::once())
->method('setPath')
->with('checkout/cart', ['_query' => ['purchase_test_mode' => 1]])
->willReturnSelf();
$redirectFactory = $this->createMock(RedirectFactory::class);
$redirectFactory->expects(self::once())->method('create')->willReturn($redirect);
$plugin = new RestrictCheckoutPlugin($config, $redirectFactory);
$result = $plugin->aroundExecute(
$this->createMock(CheckoutIndex::class),
static fn (): ResultInterface => throw new \LogicException('Checkout must not continue.')
);
self::assertSame($redirect, $result);
}
}