Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/dev/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
CI bot committed Mar 1, 2023
2 parents 4fd8354 + b6a7851 commit 15999e2
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Doctrine\Persistence\ManagerRegistry;
use Gedmo\Translatable\TranslatableListener;
use Oro\Bundle\EmailBundle\Entity\EmailTemplate;
use Oro\Bundle\EmailBundle\Entity\EmailTemplateTranslation;
use Oro\Bundle\EmailBundle\Exception\EmailTemplateCompilationException;
Expand All @@ -14,35 +15,36 @@
use Oro\Bundle\LocaleBundle\Entity\Localization;
use Psr\Log\LoggerInterface;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Contracts\Translation\LocaleAwareInterface;
use Twig\Error\Error;

/**
* Provides compiled email template information ready to be sent via email.
*/
class EmailTemplateContentProvider
{
/** @var ManagerRegistry */
private $doctrine;

/** @var EmailRenderer */
private $emailRenderer;

/** @var PropertyAccessor */
private $propertyAccessor;

/** @var LoggerInterface */
private $logger;
private ManagerRegistry $doctrine;
private EmailRenderer $emailRenderer;
private PropertyAccessor $propertyAccessor;
private LoggerInterface $logger;
private TranslatableListener $translatableListener;
private LocaleAwareInterface $translator;
private ?string $previousLocale;

public function __construct(
ManagerRegistry $doctrine,
EmailRenderer $emailRenderer,
PropertyAccessor $propertyAccessor,
LoggerInterface $logger
LoggerInterface $logger,
TranslatableListener $translatableListener,
LocaleAwareInterface $translator,
) {
$this->doctrine = $doctrine;
$this->emailRenderer = $emailRenderer;
$this->propertyAccessor = $propertyAccessor;
$this->logger = $logger;
$this->translatableListener = $translatableListener;
$this->translator = $translator;
}

/**
Expand All @@ -68,6 +70,7 @@ public function getTemplateContent(
}

$emailTemplateModel = $this->getLocalizedModel($emailTemplateEntity, $localization);
$this->setLocalization($localization);

try {
[$subject, $content] = $this->emailRenderer->compileMessage($emailTemplateModel, $templateParams);
Expand All @@ -83,8 +86,9 @@ public function getTemplateContent(
),
['exception' => $exception]
);

throw new EmailTemplateCompilationException($criteria);
} finally {
$this->restoreLocalization();
}

return $emailTemplateModel;
Expand Down Expand Up @@ -173,4 +177,18 @@ private function findTemplate(array &$templateIndex, ?Localization $localization

return null;
}

private function setLocalization(Localization $localization): void
{
$this->previousLocale = $localization->getLanguageCode();
$this->translatableListener->setTranslatableLocale($this->previousLocale);
$this->translator->setLocale($this->previousLocale);
}

private function restoreLocalization(): void
{
$this->translatableListener->setTranslatableLocale($this->previousLocale);
$this->translator->setLocale($this->previousLocale);
$this->previousLocale = null;
}
}
2 changes: 2 additions & 0 deletions src/Oro/Bundle/EmailBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,8 @@ services:
- '@oro_email.email_renderer'
- '@property_accessor'
- '@logger'
- '@stof_doctrine_extensions.listener.translatable'
- '@translator'

oro_email.provider.url_provider:
class: 'Oro\Bundle\EmailBundle\Provider\UrlProvider'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Doctrine\Persistence\ManagerRegistry;
use Gedmo\Translatable\TranslatableListener;
use Oro\Bundle\EmailBundle\Entity\EmailTemplate;
use Oro\Bundle\EmailBundle\Entity\EmailTemplateTranslation;
use Oro\Bundle\EmailBundle\Entity\Repository\EmailTemplateRepository;
Expand All @@ -15,9 +16,11 @@
use Oro\Bundle\EmailBundle\Provider\EmailRenderer;
use Oro\Bundle\EmailBundle\Provider\EmailTemplateContentProvider;
use Oro\Bundle\LocaleBundle\Entity\Localization;
use Oro\Bundle\TranslationBundle\Entity\Language;
use Oro\Component\Testing\ReflectionUtil;
use Psr\Log\LoggerInterface;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Contracts\Translation\LocaleAwareInterface;
use Twig\Error\Error;

class EmailTemplateContentProviderTest extends \PHPUnit\Framework\TestCase
Expand All @@ -34,6 +37,9 @@ class EmailTemplateContentProviderTest extends \PHPUnit\Framework\TestCase
/** @var EmailTemplateContentProvider */
private $provider;

private ?TranslatableListener $translatableListener;
private ?LocaleAwareInterface $translator;

protected function setUp(): void
{
$this->repository = $this->createMock(EmailTemplateRepository::class);
Expand All @@ -46,19 +52,24 @@ protected function setUp(): void

$this->emailRenderer = $this->createMock(EmailRenderer::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->translatableListener = $this->createMock(TranslatableListener::class);
$this->translator = $this->createMock(LocaleAwareInterface::class);

$this->provider = new EmailTemplateContentProvider(
$doctrine,
$this->emailRenderer,
new PropertyAccessor(),
$this->logger
$this->logger,
$this->translatableListener,
$this->translator
);
}

private function getLocalization(int $id, Localization $parentLocalization = null): Localization
{
$localization = new Localization();
ReflectionUtil::setId($localization, $id);
$localization->setLanguage((new Language())->setCode('en'));
if (null !== $parentLocalization) {
$localization->setParentLocalization($parentLocalization);
}
Expand All @@ -73,6 +84,7 @@ public function testGetTemplateContentRepositoryException(\Throwable $exception)
{
$criteria = new EmailTemplateCriteria('test_template');
$localization = new Localization();
$localization->setFormattingCode('en');
$templateParams = ['any-key' => 'any-val'];

$this->repository->expects(self::once())
Expand Down Expand Up @@ -110,6 +122,7 @@ public function testGetTemplateContentRendererException(): void
{
$criteria = new EmailTemplateCriteria('test_template');
$localization = new Localization();
$localization->setLanguage((new Language())->setCode('en'));
$templateParams = ['any-key' => 'any-val'];

$emailTemplate = new EmailTemplate();
Expand Down Expand Up @@ -174,6 +187,14 @@ public function testGetTemplateContent(): void
'Compiled content',
]);

$this->translatableListener->expects(self::exactly(2))
->method('setTranslatableLocale')
->with($localizationChildrenB->getLanguageCode());

$this->translator->expects(self::exactly(2))
->method('setLocale')
->with($localizationChildrenB->getLanguageCode());

$model = $this->provider->getTemplateContent($criteria, $localizationChildrenB, $templateParams);
self::assertEquals(
(new EmailTemplateModel())
Expand Down
42 changes: 20 additions & 22 deletions src/Oro/Bundle/TagBundle/Grid/TagsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Oro\Bundle\DataGridBundle\Extension\InlineEditing\Configuration as InlineEditingConfiguration;
use Oro\Bundle\DataGridBundle\Extension\InlineEditing\InlineEditingConfigurator;
use Oro\Bundle\EntityBundle\ORM\EntityClassResolver;
use Oro\Bundle\FeatureToggleBundle\Checker\FeatureChecker;
use Oro\Bundle\TagBundle\Entity\Tag;
use Oro\Bundle\TagBundle\Entity\TagManager;
use Oro\Bundle\TagBundle\Helper\TaggableHelper;
Expand All @@ -21,37 +22,33 @@
*/
class TagsExtension extends AbstractTagsExtension
{
const TAGS_ROOT_PARAM = '_tags';
const DISABLED_PARAM = '_disabled';
public const TAGS_ROOT_PARAM = '_tags';
public const DISABLED_PARAM = '_disabled';

const COLUMN_NAME = 'tags';
private const COLUMN_NAME = 'tags';

/** @var TaggableHelper */
protected $taggableHelper;

/** @var AuthorizationCheckerInterface */
protected $authorizationChecker;

/** @var TokenStorageInterface */
protected $tokenStorage;

/** @var InlineEditingConfigurator */
private $inlineEditingConfigurator;
private TaggableHelper $taggableHelper;
private AuthorizationCheckerInterface $authorizationChecker;
private TokenStorageInterface $tokenStorage;
private InlineEditingConfigurator $inlineEditingConfigurator;
private FeatureChecker $featureChecker;

public function __construct(
TagManager $tagManager,
EntityClassResolver $entityClassResolver,
TaggableHelper $helper,
AuthorizationCheckerInterface $authorizationChecker,
TokenStorageInterface $tokenStorage,
InlineEditingConfigurator $inlineEditingConfigurator
InlineEditingConfigurator $inlineEditingConfigurator,
FeatureChecker $featureChecker
) {
parent::__construct($tagManager, $entityClassResolver);

$this->taggableHelper = $helper;
$this->authorizationChecker = $authorizationChecker;
$this->tokenStorage = $tokenStorage;
$this->inlineEditingConfigurator = $inlineEditingConfigurator;
$this->featureChecker = $featureChecker;
}

/**
Expand All @@ -68,13 +65,14 @@ public function getPriority()
public function isApplicable(DatagridConfiguration $config)
{
return
parent::isApplicable($config) &&
!$this->isDisabled() &&
!$this->isUnsupportedGridPrefix($config) &&
$this->isGridRootEntityTaggable($config) &&
null !== $config->offsetGetByPath(self::PROPERTY_ID_PATH) &&
null !== $this->tokenStorage->getToken() &&
$this->authorizationChecker->isGranted('oro_tag_view');
parent::isApplicable($config)
&& $this->featureChecker->isFeatureEnabled('manage_tags')
&& !$this->isDisabled()
&& !$this->isUnsupportedGridPrefix($config)
&& $this->isGridRootEntityTaggable($config)
&& null !== $config->offsetGetByPath(self::PROPERTY_ID_PATH)
&& null !== $this->tokenStorage->getToken()
&& $this->authorizationChecker->isGranted('oro_tag_view');
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Oro/Bundle/TagBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ services:
- '@security.authorization_checker'
- '@security.token_storage'
- '@oro_datagrid.inline_editing_configurator'
- '@oro_featuretoggle.checker.feature_checker'
calls:
- [addUnsupportedGridPrefix, ['oro_report']]
- [addUnsupportedGridPrefix, ['oro_segment']]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% import '@OroUI/macros.html.twig' as UI %}
{% import '@OroTag/macros.html.twig' as Tag %}
{% if is_granted('oro_tag_view') %}
{% if is_granted('oro_tag_view') and feature_enabled('manage_tags') %}
<div class="row-fluid">
<div class="responsive-block">
{{ UI.renderHtmlProperty('oro.tag.entity_plural_label'|trans, Tag.renderView(entity)) }}
Expand Down
Loading

0 comments on commit 15999e2

Please sign in to comment.