80 lines
2.7 KiB
PHP
80 lines
2.7 KiB
PHP
<?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));
|
|
}
|
|
}
|