Skip to content

Commit

Permalink
MAGETWO-28268: [GITHUB] Inline translate malfunctioning #658
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurii Torbyk committed Dec 24, 2014
1 parent 3bca5f4 commit 8b1ed6f
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 78 deletions.
3 changes: 2 additions & 1 deletion app/code/Magento/Backend/view/adminhtml/requirejs-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
var config = {
map: {
'*': {
editTrigger: 'mage/edit-trigger',
translateInline: 'mage/translate-inline',
form: 'mage/backend/form',
button: 'mage/backend/button',
Expand Down Expand Up @@ -36,4 +37,4 @@ var config = {
paths: {
"jquery/ui": "jquery/jquery-ui-1.9.2"
}
};
};
16 changes: 16 additions & 0 deletions app/code/Magento/PageCache/Controller/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@

class Block extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\Translate\InlineInterface
*/
protected $translateInline;

/**
* @param \Magento\Framework\App\Action\Context $context
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Translate\InlineInterface $translateInline
) {
parent::__construct($context);
$this->translateInline = $translateInline;
}

/**
* Get blocks from layout by handles
*
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/PageCache/Controller/Block/Esi.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function execute()
$response->setHeader('X-Magento-Tags', implode(',', $blockInstance->getIdentities()));
}
}
$this->translateInline->processResponseBody($html);
$response->appendBody($html);
$response->setPublicHeaders($ttl);
}
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/PageCache/Controller/Block/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function execute()
}

$this->getResponse()->setPrivateHeaders(\Magento\PageCache\Helper\Data::PRIVATE_MAX_AGE_CACHE);
$this->translateInline->processResponseBody($data);
$this->getResponse()->appendBody(json_encode($data));
}
}
6 changes: 3 additions & 3 deletions app/code/Magento/Translation/Block/Js.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public function __construct(
*/
public function getTranslatedJson()
{
$json = \Zend_Json::encode($this->dataProvider->getTranslateData());
$this->translateInline->processResponseBody($json, false);
return $json;
$data = $this->dataProvider->getTranslateData();
$this->translateInline->processResponseBody($data);
return \Zend_Json::encode($data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class LayoutTest extends \PHPUnit_Framework_TestCase
*/
protected $layout;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Translate\InlineInterface
*/
protected $translateInline;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Result\Layout
*/
Expand All @@ -36,14 +41,18 @@ protected function setUp()
$this->layout = $this->getMock('Magento\Framework\View\Layout', [], [], '', false);
$this->request = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false);
$this->eventManager = $this->getMock('Magento\Framework\Event\ManagerInterface', [], [], '', false);
$this->translateInline = $this->getMock('Magento\Framework\Translate\InlineInterface');

$context = $this->getMock('Magento\Framework\View\Element\Template\Context', [], [], '', false);
$context->expects($this->any())->method('getLayout')->will($this->returnValue($this->layout));
$context->expects($this->any())->method('getRequest')->will($this->returnValue($this->request));
$context->expects($this->any())->method('getEventManager')->will($this->returnValue($this->eventManager));

$this->resultLayout = (new \Magento\TestFramework\Helper\ObjectManager($this))
->getObject('Magento\Framework\View\Result\Layout', ['context' => $context]);
->getObject(
'Magento\Framework\View\Result\Layout',
['context' => $context, 'translateInline' => $this->translateInline]
);
}

/**
Expand Down Expand Up @@ -90,12 +99,14 @@ public function testAddUpdate()
* @param bool $replaceHeader
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $setHttpResponseCodeCount
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $setHeaderCount
* @dataProvider providerRenderResult
* @dataProvider dataProviderRenderResult
*/
public function testRenderResult(
$httpCode, $headerName, $headerValue, $replaceHeader, $setHttpResponseCodeCount, $setHeaderCount
) {
$this->layout->expects($this->once())->method('getOutput')->will($this->returnValue('output'));
$layoutOutput = 'output';

$this->layout->expects($this->once())->method('getOutput')->will($this->returnValue($layoutOutput));

$this->request->expects($this->once())->method('getFullActionName')
->will($this->returnValue('Module_Controller_Action'));
Expand All @@ -105,11 +116,16 @@ public function testRenderResult(
['layout_render_before_Module_Controller_Action']
);

$this->translateInline->expects($this->once())
->method('processResponseBody')
->with($layoutOutput)
->willReturnSelf();

/** @var \Magento\Framework\App\Response\Http|\PHPUnit_Framework_MockObject_MockObject $response */
$response = $this->getMock('Magento\Framework\App\Response\Http', [], [], '', false);
$response->expects($setHttpResponseCodeCount)->method('setHttpResponseCode')->with($httpCode);
$response->expects($setHeaderCount)->method('setHeader')->with($headerName, $headerValue, $replaceHeader);
$response->expects($this->once())->method('appendBody')->with('output');
$response->expects($this->once())->method('appendBody')->with($layoutOutput);

$this->resultLayout->setHttpResponseCode($httpCode);

Expand All @@ -123,7 +139,7 @@ public function testRenderResult(
/**
* @return array
*/
public function providerRenderResult()
public function dataProviderRenderResult()
{
return [
[200, 'content-type', 'text/html', true, $this->once(), $this->once()],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,39 @@ class EsiTest extends \PHPUnit_Framework_TestCase
*/
protected $layoutMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Translate\InlineInterface
*/
protected $translateInline;

/**
* Set up before test
*/
protected function setUp()
{
$this->layoutMock = $this->getMockBuilder(
'Magento\Framework\View\Layout'
)->disableOriginalConstructor()->getMock();
$this->layoutMock = $this->getMockBuilder('Magento\Framework\View\Layout')
->disableOriginalConstructor()->getMock();

$contextMock =
$this->getMockBuilder('Magento\Framework\App\Action\Context')->disableOriginalConstructor()->getMock();

$this->requestMock = $this->getMockBuilder(
'Magento\Framework\App\Request\Http'
)->disableOriginalConstructor()->getMock();
$this->responseMock = $this->getMockBuilder(
'Magento\Framework\App\Response\Http'
)->disableOriginalConstructor()->getMock();
$this->requestMock = $this->getMockBuilder('Magento\Framework\App\Request\Http')
->disableOriginalConstructor()->getMock();
$this->responseMock = $this->getMockBuilder('Magento\Framework\App\Response\Http')
->disableOriginalConstructor()->getMock();
$this->viewMock = $this->getMockBuilder('Magento\Framework\App\View')->disableOriginalConstructor()->getMock();

$contextMock->expects($this->any())->method('getRequest')->will($this->returnValue($this->requestMock));
$contextMock->expects($this->any())->method('getResponse')->will($this->returnValue($this->responseMock));
$contextMock->expects($this->any())->method('getView')->will($this->returnValue($this->viewMock));

$this->action = new \Magento\PageCache\Controller\Block\Esi($contextMock);
$this->translateInline = $this->getMock('Magento\Framework\Translate\InlineInterface');

$helperObjectManager = new \Magento\TestFramework\Helper\ObjectManager($this);
$this->action = $helperObjectManager->getObject(
'Magento\PageCache\Controller\Block\Esi',
['context' => $contextMock, 'translateInline' => $this->translateInline]
);
}

/**
Expand Down Expand Up @@ -88,15 +96,10 @@ public function testExecute($blockClass, $shouldSetHeaders)

$this->viewMock->expects($this->once())->method('getLayout')->will($this->returnValue($this->layoutMock));

$this->layoutMock->expects(
$this->once()
)->method(
'getBlock'
)->with(
$this->equalTo($block)
)->will(
$this->returnValue($blockInstance1)
);
$this->layoutMock->expects($this->once())
->method('getBlock')
->with($this->equalTo($block))
->will($this->returnValue($blockInstance1));

if ($shouldSetHeaders) {
$this->responseMock->expects($this->once())
Expand All @@ -107,6 +110,11 @@ public function testExecute($blockClass, $shouldSetHeaders)
->method('setHeader');
}

$this->translateInline->expects($this->once())
->method('processResponseBody')
->with($html)
->willReturnSelf();

$this->responseMock->expects($this->once())
->method('appendBody')
->with($this->equalTo($html));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class RenderTest extends \PHPUnit_Framework_TestCase
*/
protected $action;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Translate\InlineInterface
*/
protected $translateInline;

/**
* @var \Magento\Framework\View\Layout|\PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -57,7 +62,13 @@ protected function setUp()
$contextMock->expects($this->any())->method('getResponse')->will($this->returnValue($this->responseMock));
$contextMock->expects($this->any())->method('getView')->will($this->returnValue($this->viewMock));

$this->action = new \Magento\PageCache\Controller\Block\Render($contextMock);
$this->translateInline = $this->getMock('Magento\Framework\Translate\InlineInterface');

$helperObjectManager = new \Magento\TestFramework\Helper\ObjectManager($this);
$this->action = $helperObjectManager->getObject(
'Magento\PageCache\Controller\Block\Render',
['context' => $contextMock, 'translateInline' => $this->translateInline]
);
}

public function testExecuteNotAjax()
Expand Down Expand Up @@ -110,54 +121,33 @@ public function testExecute()
$blockInstance2->expects($this->once())->method('toHtml')->will($this->returnValue($expectedData['block2']));

$this->requestMock->expects($this->once())->method('isAjax')->will($this->returnValue(true));
$this->requestMock->expects(
$this->at(1)
)->method(
'getParam'
)->with(
$this->equalTo('blocks'),
$this->equalTo('')
)->will(
$this->returnValue(json_encode($blocks))
);
$this->requestMock->expects(
$this->at(2)
)->method(
'getParam'
)->with(
$this->equalTo('handles'),
$this->equalTo('')
)->will(
$this->returnValue(json_encode($handles))
);
$this->requestMock->expects($this->at(1))
->method('getParam')
->with($this->equalTo('blocks'), $this->equalTo(''))
->will($this->returnValue(json_encode($blocks)));
$this->requestMock->expects($this->at(2))
->method('getParam')
->with($this->equalTo('handles'), $this->equalTo(''))
->will($this->returnValue(json_encode($handles)));
$this->viewMock->expects($this->once())->method('loadLayout')->with($this->equalTo($handles));
$this->viewMock->expects($this->any())->method('getLayout')->will($this->returnValue($this->layoutMock));
$this->layoutMock->expects(
$this->at(0)
)->method(
'getBlock'
)->with(
$this->equalTo($blocks[0])
)->will(
$this->returnValue($blockInstance1)
);
$this->layoutMock->expects(
$this->at(1)
)->method(
'getBlock'
)->with(
$this->equalTo($blocks[1])
)->will(
$this->returnValue($blockInstance2)
);

$this->responseMock->expects(
$this->once()
)->method(
'appendBody'
)->with(
$this->equalTo(json_encode($expectedData))
);
$this->layoutMock->expects($this->at(0))
->method('getBlock')
->with($this->equalTo($blocks[0]))
->will($this->returnValue($blockInstance1));
$this->layoutMock->expects($this->at(1))
->method('getBlock')
->with($this->equalTo($blocks[1]))
->will($this->returnValue($blockInstance2));

$this->translateInline->expects($this->once())
->method('processResponseBody')
->with($expectedData)
->willReturnSelf();

$this->responseMock->expects($this->once())
->method('appendBody')
->with($this->equalTo(json_encode($expectedData)));

$this->action->execute();
}
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/Magento/Framework/View/Result/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ public function renderResult(ResponseInterface $response)
*/
protected function render(ResponseInterface $response)
{
$response->appendBody($this->layout->getOutput());
$output = $this->layout->getOutput();
$this->translateInline->processResponseBody($output);
$response->appendBody($output);
return $this;
}
}
5 changes: 3 additions & 2 deletions lib/internal/Magento/Framework/View/Result/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,10 @@ protected function render(ResponseInterface $response)
]);

$output = $this->getLayout()->getOutput();
$this->translateInline->processResponseBody($output);
$this->assign('layoutContent', $output);
$response->appendBody($this->renderPage());
$output = $this->renderPage();
$this->translateInline->processResponseBody($output);
$response->appendBody($output);
} else {
parent::render($response);
}
Expand Down

0 comments on commit 8b1ed6f

Please sign in to comment.