Skip to content

Commit

Permalink
ContextStateManager can handle context current locale
Browse files Browse the repository at this point in the history
  • Loading branch information
jolelievre committed Nov 8, 2023
1 parent ecb4866 commit d200141
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Adapter/ContextStateManager.php
Expand Up @@ -34,6 +34,7 @@
use Currency;
use Customer;
use Language;
use PrestaShop\PrestaShop\Core\Localization\LocaleInterface;
use Shop;

/**
Expand All @@ -51,6 +52,7 @@ class ContextStateManager
'country',
'currency',
'language',
'currentLocale',
'customer',
'shop',
'shopContext',
Expand Down Expand Up @@ -145,6 +147,21 @@ public function setLanguage(?Language $language): self
return $this;
}

/**
* Sets context localization locale and saves previous value
*
* @param LocaleInterface|null $locale
*
* @return $this
*/
public function setCurrentLocale(?LocaleInterface $locale): self
{
$this->saveContextField('currentLocale');
$this->getContext()->currentLocale = $locale;

return $this;
}

/**
* Sets context customer and saves previous value
*
Expand Down
8 changes: 8 additions & 0 deletions tests/TestCase/ContextStateTestCase.php
Expand Up @@ -76,6 +76,14 @@ protected function createContextMock(array $contextFields): Context
if ($fieldName === 'language' && $contextValue instanceof Language) {
$contextMock->getTranslator()->setLocale('test' . $contextValue->id);
}
if ($fieldName === 'currentLocale') {
$contextMock
->method('getCurrentLocale')
->willReturnCallback(static function () use ($contextMock) {
return $contextMock->currentLocale;
})
;
}
}
LegacyContext::setInstanceForTesting($contextMock);

Expand Down
55 changes: 55 additions & 0 deletions tests/Unit/Adapter/ContextStateManagerTest.php
Expand Up @@ -31,8 +31,10 @@
use Currency;
use Customer;
use Language;
use PHPUnit\Framework\MockObject\MockObject;
use PrestaShop\PrestaShop\Adapter\ContextStateManager;
use PrestaShop\PrestaShop\Adapter\LegacyContext;
use PrestaShop\PrestaShop\Core\Localization\LocaleInterface;
use Tests\TestCase\ContextStateTestCase;

class ContextStateManagerTest extends ContextStateTestCase
Expand Down Expand Up @@ -174,6 +176,35 @@ public function testLanguageState()
$this->assertEquals('test42', $context->getTranslator()->getLocale());
}

public function testLocalizationLocaleState()
{
$context = $this->createContextMock([
'currentLocale' => $this->createLocalizationLocaleMock('fr-FR'),
]);
$this->assertEquals('fr-FR', $context->currentLocale->getCode());
$this->assertEquals('fr-FR', $context->getCurrentLocale()->getCode());

$contextStateManager = new ContextStateManager($this->legacyContext);
$this->assertNull($contextStateManager->getContextFieldsStack());

$contextStateManager->setCurrentLocale($this->createLocalizationLocaleMock('en-US'));
$this->assertEquals('en-US', $context->currentLocale->getCode());
$this->assertEquals('en-US', $context->getCurrentLocale()->getCode());
$this->assertIsArray($contextStateManager->getContextFieldsStack());
$this->assertCount(1, $contextStateManager->getContextFieldsStack());

$contextStateManager->setCurrentLocale($this->createLocalizationLocaleMock('en-GB'));
$this->assertEquals('en-GB', $context->currentLocale->getCode());
$this->assertEquals('en-GB', $context->getCurrentLocale()->getCode());
$this->assertIsArray($contextStateManager->getContextFieldsStack());
$this->assertCount(1, $contextStateManager->getContextFieldsStack());

$contextStateManager->restorePreviousContext();
$this->assertEquals('fr-FR', $context->currentLocale->getCode());
$this->assertEquals('fr-FR', $context->getCurrentLocale()->getCode());
$this->assertNull($contextStateManager->getContextFieldsStack());
}

public function testNullField()
{
$context = $this->createContextMock([
Expand Down Expand Up @@ -306,12 +337,15 @@ public function testMultipleSavedContextFields()
'currency' => $this->createContextFieldMock(Currency::class, 42),
'customer' => $this->createContextFieldMock(Customer::class, 42),
'language' => $this->createContextFieldMock(Language::class, 42),
'currentLocale' => $this->createLocalizationLocaleMock('fr-FR'),
]);
$this->assertEquals(42, $context->cart->id);
$this->assertEquals(42, $context->country->id);
$this->assertEquals(42, $context->currency->id);
$this->assertEquals(42, $context->customer->id);
$this->assertEquals(42, $context->language->id);
$this->assertEquals('fr-FR', $context->currentLocale->getCode());
$this->assertEquals('fr-FR', $context->getCurrentLocale()->getCode());

$contextStateManager = new ContextStateManager($this->legacyContext);
$this->assertNull($contextStateManager->getContextFieldsStack());
Expand All @@ -320,6 +354,7 @@ public function testMultipleSavedContextFields()
->setCart($this->createContextFieldMock(Cart::class, 51))
->setCurrency($this->createContextFieldMock(Currency::class, 51))
->setCustomer($this->createContextFieldMock(Customer::class, 51))
->setCurrentLocale($this->createLocalizationLocaleMock('en-US'))
;
$this->assertIsArray($contextStateManager->getContextFieldsStack());
$this->assertCount(1, $contextStateManager->getContextFieldsStack());
Expand All @@ -329,20 +364,25 @@ public function testMultipleSavedContextFields()
$this->assertEquals(51, $context->currency->id);
$this->assertEquals(51, $context->customer->id);
$this->assertEquals(42, $context->language->id);
$this->assertEquals('en-US', $context->currentLocale->getCode());
$this->assertEquals('en-US', $context->getCurrentLocale()->getCode());

$contextStateManager->saveCurrentContext();
$this->assertCount(2, $contextStateManager->getContextFieldsStack());

$contextStateManager
->setCart($this->createContextFieldMock(Cart::class, 69))
->setCurrency($this->createContextFieldMock(Currency::class, 69))
->setCurrentLocale($this->createLocalizationLocaleMock('en-GB'))
;

$this->assertEquals(69, $context->cart->id);
$this->assertEquals(42, $context->country->id);
$this->assertEquals(69, $context->currency->id);
$this->assertEquals(51, $context->customer->id);
$this->assertEquals(42, $context->language->id);
$this->assertEquals('en-GB', $context->currentLocale->getCode());
$this->assertEquals('en-GB', $context->getCurrentLocale()->getCode());
$this->assertCount(2, $contextStateManager->getContextFieldsStack());

$contextStateManager->restorePreviousContext();
Expand All @@ -352,6 +392,8 @@ public function testMultipleSavedContextFields()
$this->assertEquals(51, $context->currency->id);
$this->assertEquals(51, $context->customer->id);
$this->assertEquals(42, $context->language->id);
$this->assertEquals('en-US', $context->currentLocale->getCode());
$this->assertEquals('en-US', $context->getCurrentLocale()->getCode());
$this->assertIsArray($contextStateManager->getContextFieldsStack());
$this->assertCount(1, $contextStateManager->getContextFieldsStack());

Expand All @@ -362,6 +404,8 @@ public function testMultipleSavedContextFields()
$this->assertEquals(42, $context->currency->id);
$this->assertEquals(42, $context->customer->id);
$this->assertEquals(42, $context->language->id);
$this->assertEquals('fr-FR', $context->currentLocale->getCode());
$this->assertEquals('fr-FR', $context->getCurrentLocale()->getCode());
$this->assertNull($contextStateManager->getContextFieldsStack());
}

Expand Down Expand Up @@ -457,4 +501,15 @@ public function testSavedContextsFirst()
$this->assertEquals(42, $context->language->id);
$this->assertNull($contextStateManager->getContextFieldsStack());
}

private function createLocalizationLocaleMock(string $code): LocaleInterface|MockObject
{
$locale = $this->createMock(LocaleInterface::class);
$locale
->method('getCode')
->willReturn($code)
;

return $locale;
}
}

0 comments on commit d200141

Please sign in to comment.