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

Issue 28656 inline translation rest api #28856

36 changes: 35 additions & 1 deletion lib/internal/Magento/Framework/Translate/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ class Inline implements \Magento\Framework\Translate\InlineInterface
*/
protected $state;

/**
* @var array
*/
private $allowedAreas = [
\Magento\Framework\App\Area::AREA_FRONTEND,
\Magento\Framework\App\Area::AREA_ADMINHTML
];

/**
* @var \Magento\Framework\App\State
*/
private $appState;

/**
* Initialize inline translation model
*
Expand All @@ -78,6 +91,7 @@ class Inline implements \Magento\Framework\Translate\InlineInterface
* @param Inline\ConfigInterface $config
* @param Inline\ParserInterface $parser
* @param Inline\StateInterface $state
* @param \Magento\Framework\App\State $appState
* @param string $templateFileName
* @param string $translatorRoute
* @param null $scope
Expand All @@ -89,6 +103,7 @@ public function __construct(
\Magento\Framework\Translate\Inline\ConfigInterface $config,
\Magento\Framework\Translate\Inline\ParserInterface $parser,
\Magento\Framework\Translate\Inline\StateInterface $state,
\Magento\Framework\App\State $appState,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
\Magento\Framework\App\State $appState,
\Magento\Framework\App\State $appState = null,

$templateFileName = '',
$translatorRoute = '',
$scope = null
Expand All @@ -99,6 +114,7 @@ public function __construct(
$this->config = $config;
$this->parser = $parser;
$this->state = $state;
$this->appState = $appState;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->appState = $appState;
$this->appState = $appState ?: \Magento\Framework\App\ObjectManager::getInstance()
->(\Magento\Framework\App\State::class);

$this->templateFileName = $templateFileName;
$this->translatorRoute = $translatorRoute;
$this->scope = $scope;
Expand All @@ -116,7 +132,8 @@ public function isAllowed()
$scope = $this->scopeResolver->getScope($this->scope);
}
$this->isAllowed = $this->config->isActive($scope)
&& $this->config->isDevAllowed($scope);
&& $this->config->isDevAllowed($scope)
&& $this->isAreaAllowed();
}
return $this->state->isEnabled() && $this->isAllowed;
}
Expand Down Expand Up @@ -249,4 +266,21 @@ protected function stripInlineTranslations(&$body)
}
return $this;
}

/**
* Indicates whether the current area is valid for inline translation
*
* @return bool
*/
private function isAreaAllowed()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private function isAreaAllowed()
private function isAreaAllowed(): bool

{
try {
return in_array(
$this->appState->getAreaCode(),
$this->allowedAreas
);
} catch (\Magento\Framework\Exception\LocalizedException $e) {
return false;
}
}
}
62 changes: 48 additions & 14 deletions lib/internal/Magento/Framework/Translate/Test/Unit/InlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

namespace Magento\Framework\Translate\Test\Unit;

use Magento\Framework\App\Area;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ScopeResolverInterface;
use Magento\Framework\App\State as AppState;
use Magento\Framework\Translate\Inline;
use Magento\Framework\Translate\Inline\ConfigInterface;
use Magento\Framework\Translate\Inline\ParserFactory;
Expand Down Expand Up @@ -51,6 +53,11 @@ class InlineTest extends TestCase
*/
protected $stateMock;

/**
* @var AppState|MockObject
*/
protected $appStateMock;

protected function setUp(): void
{
$this->scopeResolverMock =
Expand All @@ -60,26 +67,29 @@ protected function setUp(): void
$this->configMock = $this->getMockForAbstractClass(ConfigInterface::class);
$this->parserMock = $this->getMockForAbstractClass(ParserInterface::class);
$this->stateMock = $this->getMockForAbstractClass(StateInterface::class);
$this->appStateMock = $this->createMock(AppState::class);
}

/**
* @param bool $isEnabled
* @param bool $isActive
* @param bool $isDevAllowed
* @param string $area
* @param bool $result
* @dataProvider isAllowedDataProvider
*/
public function testIsAllowed($isEnabled, $isActive, $isDevAllowed, $result)
public function testIsAllowed($isEnabled, $isActive, $isDevAllowed, $area, $result)
{
$this->prepareIsAllowed($isEnabled, $isActive, $isDevAllowed);
$this->prepareIsAllowed($isEnabled, $isActive, $isDevAllowed, null, $area);

$model = new Inline(
$this->scopeResolverMock,
$this->urlMock,
$this->layoutMock,
$this->configMock,
$this->parserMock,
$this->stateMock
$this->stateMock,
$this->appStateMock
);

$this->assertEquals($result, $model->isAllowed());
Expand All @@ -92,14 +102,21 @@ public function testIsAllowed($isEnabled, $isActive, $isDevAllowed, $result)
public function isAllowedDataProvider()
{
return [
[true, true, true, true],
[true, false, true, false],
[true, true, false, false],
[true, false, false, false],
[false, true, true, false],
[false, false, true, false],
[false, true, false, false],
[false, false, false, false],
[true, true, true, Area::AREA_FRONTEND, true],
[true, false, true, Area::AREA_FRONTEND, false],
[true, true, false, Area::AREA_FRONTEND, false],
[true, false, false, Area::AREA_FRONTEND, false],
[false, true, true, Area::AREA_FRONTEND, false],
[false, false, true, Area::AREA_FRONTEND, false],
[false, true, false, Area::AREA_FRONTEND, false],
[false, false, false, Area::AREA_FRONTEND, false],
[true, true, true, Area::AREA_GLOBAL, false],
[true, true, true, Area::AREA_ADMINHTML, true],
[true, true, true, Area::AREA_DOC, false],
[true, true, true, Area::AREA_CRONTAB, false],
[true, true, true, Area::AREA_WEBAPI_REST, false],
[true, true, true, Area::AREA_WEBAPI_SOAP, false],
[true, true, true, Area::AREA_GRAPHQL, false]
];
}

Expand All @@ -111,7 +128,8 @@ public function testGetParser()
$this->layoutMock,
$this->configMock,
$this->parserMock,
$this->stateMock
$this->stateMock,
$this->appStateMock
);
$this->assertEquals($this->parserMock, $model->getParser());
}
Expand All @@ -133,6 +151,7 @@ public function testProcessResponseBodyStripInline($body, $expected)
$this->configMock,
$this->parserMock,
$this->stateMock,
$this->appStateMock,
'',
'',
$scope
Expand Down Expand Up @@ -201,6 +220,7 @@ public function testProcessResponseBody($scope, $body, $expected)
$this->configMock,
$this->parserMock,
$this->stateMock,
$this->appStateMock,
'',
'',
$scope
Expand Down Expand Up @@ -266,6 +286,7 @@ public function testProcessResponseBodyGetInlineScript($scope, $body, $expected)
$this->configMock,
$this->parserMock,
$this->stateMock,
$this->appStateMock,
'',
'',
$scope
Expand All @@ -292,8 +313,13 @@ public function processResponseBodyGetInlineScriptDataProvider()
* @param bool $isDevAllowed
* @param null|string $scope
*/
protected function prepareIsAllowed($isEnabled, $isActive, $isDevAllowed, $scope = null)
{
protected function prepareIsAllowed(
$isEnabled,
$isActive,
$isDevAllowed,
$scope = null,
$area = Area::AREA_FRONTEND
) {
$scopeMock = $this->getMockForAbstractClass(ScopeConfigInterface::class);
$this->stateMock->expects($this->any())->method('isEnabled')->willReturn($isEnabled);
$this->scopeResolverMock->expects(
Expand Down Expand Up @@ -323,5 +349,13 @@ protected function prepareIsAllowed($isEnabled, $isActive, $isDevAllowed, $scope
)->willReturn(
$isDevAllowed
);

$this->appStateMock->expects(
($isActive && $isDevAllowed) ? $this->once() : $this->never()
)->method(
'getAreaCode'
)->willReturn(
$area
);
}
}