Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Locale] Current locale set on request #12458

Merged
merged 7 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@admin_locale
Feature: Browsing administration panel in a proper locale
In order to see panel in correct language
As an Admin
I want to browse admin panel in my locale

Background:
Given the store operates on a single channel
And that channel allows to shop using "English (United States)" and "Polish (Poland)" locales
And it uses the "Polish (Poland)" locale by default
And I am logged in as an administrator
And I am using "Polish (Poland)" locale for my panel

@ui
Scenario: Getting errors in my language
Given I am editing my details
When I change its email to "wrong-email"
And I save my changes
Then I should be notified that this email is not valid in "Polish (Poland)" locale

@ui
Scenario: Seeing menu in my language
When I open administration dashboard
Then I should see sidebar catalog section configuration in "Polish (Poland)" locale
34 changes: 25 additions & 9 deletions src/Sylius/Behat/Context/Ui/Admin/LocaleContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,30 @@
namespace Sylius\Behat\Context\Ui\Admin;

use Behat\Behat\Context\Context;
use Sylius\Behat\Page\Admin\Administrator\CreatePageInterface;
use Sylius\Behat\Page\Admin\DashboardPageInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Webmozart\Assert\Assert;

final class LocaleContext implements Context
{
/** @var CreatePageInterface */
private $createPage;

/** @var DashboardPageInterface */
private $dashboardPage;

/** @var TranslatorInterface */
private $translator;

public function __construct(DashboardPageInterface $dashboardPage, TranslatorInterface $translator)
{
public function __construct(
DashboardPageInterface $dashboardPage,
TranslatorInterface $translator,
CreatePageInterface $createPage
) {
$this->dashboardPage = $dashboardPage;
$this->translator = $translator;
$this->createPage = $createPage;
}

/**
Expand All @@ -45,15 +53,23 @@ public function iShouldBeViewingTheAdministrationPanelIn($localeCode)
}

/**
* @param string $text
* @param string $localeCode
*
* @return string
* @Then I should be notified that this email is not valid in :localeCode locale
*/
private function translate($text, $localeCode)
public function iShouldBeNotifiedThatThisEmailIsNotValidInLocale(string $localeCode): void
{
$this->translator->setLocale($localeCode);
Assert::same($this->createPage->getValidationMessage('email'), $this->translate('sylius.contact.email.invalid', $localeCode, 'validators'));
}

return $this->translator->trans($text);
/**
* @Then I should see sidebar catalog section configuration in :localeCode locale
*/
public function iShouldSeeSidebarSectionConfigurationInLocale(string $localeCode): void
{
Assert::true($this->dashboardPage->isSectionWithLabelVisible($this->translate('sylius.menu.admin.main.catalog.header', $localeCode)));
}

private function translate(string $text, string $localeCode, string $domain = null): string
{
return $this->translator->trans($text, [], $domain, $localeCode);
}
}
6 changes: 6 additions & 0 deletions src/Sylius/Behat/Page/Admin/DashboardPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public function getSubHeader(): string
return trim($this->getElement('sub_header')->getText());
}

public function isSectionWithLabelVisible(string $name): bool
{
return $this->getElement('admin_menu')->find('css', sprintf('div:contains(%s)', $name)) !== null;
}

public function logOut(): void
{
$this->getElement('logout')->click();
Expand Down Expand Up @@ -97,6 +102,7 @@ protected function getDefinedElements(): array
'total_sales' => '#total-sales',
'sub_header' => '.ui.header .content .sub.header',
'channel_choosing_link' => 'a:contains("%channelName%")',
'admin_menu' => '.sylius-admin-menu',
]);
}
}
2 changes: 2 additions & 0 deletions src/Sylius/Behat/Page/Admin/DashboardPageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public function getAverageOrderValue(): string;

public function getSubHeader(): string;

public function isSectionWithLabelVisible(string $name): bool;

public function logOut(): void;

public function chooseChannel(string $channelName): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<service id="sylius.behat.context.ui.admin.locale" class="Sylius\Behat\Context\Ui\Admin\LocaleContext">
<argument type="service" id="sylius.behat.page.admin.dashboard" />
<argument type="service" id="translator" />
<argument type="service" id="sylius.behat.page.admin.administrator.create" />
</service>

<service id="sylius.behat.context.ui.admin.login" class="Sylius\Behat\Context\Ui\Admin\LoginContext">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ sylius_grid:
update_product_variant_positions: "@SyliusAdmin/ProductVariant/Grid/Action/updatePositions.html.twig"
filter:
entities: '@SyliusUi/Grid/Filter/entities.html.twig'

liip_imagine:
filter_sets:
sylius_admin_product_original: ~
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\CoreBundle\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\EventListener\LocaleAwareListener as DecoratedLocaleListener;

class LocaleAwareListener implements EventSubscriberInterface
{
/** @var DecoratedLocaleListener */
private $decoratedListener;

public function __construct(DecoratedLocaleListener $decoratedListener)
{
$this->decoratedListener = $decoratedListener;
}

public function onKernelRequest(RequestEvent $event): void
{
$this->decoratedListener->onKernelRequest($event);
}

public function onKernelFinishRequest(FinishRequestEvent $event): void
{
$this->decoratedListener->onKernelFinishRequest($event);
}

public static function getSubscribedEvents(): array
{
return [
// must be registered after the Locale listener
KernelEvents::REQUEST => [['onKernelRequest', 4]],
KernelEvents::FINISH_REQUEST => [['onKernelFinishRequest', -15]],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
<tag name="kernel.event_listener" event="sylius.address.pre_create" method="preCreate" />
</service>

<service id="Sylius\Bundle\CoreBundle\EventListener\LocaleAwareListener" decorates="locale_aware_listener">
<argument type="service" id="Sylius\Bundle\CoreBundle\EventListener\LocaleAwareListener.inner" />
</service>

<service id="sylius.listener.taxon_deletion" class="Sylius\Bundle\CoreBundle\EventListener\TaxonDeletionListener">
<argument type="service" id="session" />
<argument type="service" id="sylius.repository.channel" />
Expand Down
4 changes: 2 additions & 2 deletions src/Sylius/Bundle/LocaleBundle/.gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/test/var/
vendor/
bin/

composer.phar
composer.lock

test/app/cache
test/app/logs
test/app/db.sql
/.phpunit.result.cache