/** * Render js translation * * @return string */ public function render() { $json = \Zend_Json::encode($this->dataProvider->getTranslateData()); $this->translateInline->processResponseBody($json, false); $script = '(function($) {$.mage.translate.add(' . $json . ')})(jQuery);'; return '<script type="text/javascript">//<![CDATA[' . "\n{$script}\n" . '//]]></script>'; }
/** * @magentoAdminConfigFixture dev/translate_inline/active_admin 1 * @covers \Magento\Framework\Translate\Inline::getAjaxUrl */ public function testAjaxUrl() { $body = '<html><body>some body</body></html>'; /** @var \Magento\Backend\Model\UrlInterface $url */ $url = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Framework\\UrlInterface'); $url->getUrl(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE . '/ajax/translate'); $this->_translateInline->processResponseBody($body, true); $this->assertContains($url->getUrl(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE . '/ajax/translate'), $body); }
public function testProcessResponseBodyStringProcessingAttributesCorrectly() { $testContent = file_get_contents(__DIR__ . '/_files/datatranslate_fixture.html'); $processedAttributes = ["data-translate=\"[{'shown':'* Required Fields','translated':'* Required Fields'," . "'original':'* Required Fields','location':'Tag attribute (ALT, TITLE, etc.)'}]\"", "data-translate=\"[{'shown':'Email','translated':'Email','original':'Email'," . "'location':'Tag attribute (ALT, TITLE, etc.)'}]\"", "data-translate=\"[{'shown':'Password','translated':'Password','original':'Password'," . "'location':'Tag attribute (ALT, TITLE, etc.)'}]\""]; $this->translateInlineMock->expects($this->any())->method('getAdditionalHtmlAttribute')->willReturn(null); $processedContent = $this->model->processResponseBodyString($testContent); foreach ($processedAttributes as $attribute) { $this->assertContains($attribute, $processedContent, "data-translate attribute not processed correctly"); } }
public function testExecute() { $blocks = ['block1', 'block2']; $handles = ['handle1', 'handle2']; $originalRequest = '{"route":"route","controller":"controller","action":"action","uri":"uri"}'; $expectedData = ['block1' => 'data1', 'block2' => 'data2']; $blockInstance1 = $this->getMock('Magento\\PageCache\\Test\\Unit\\Block\\Controller\\StubBlock', ['toHtml'], [], '', false); $blockInstance1->expects($this->once())->method('toHtml')->will($this->returnValue($expectedData['block1'])); $blockInstance2 = $this->getMock('Magento\\PageCache\\Test\\Unit\\Block\\Controller\\StubBlock', ['toHtml'], [], '', false); $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('getRouteName')->will($this->returnValue('magento_pagecache')); $this->requestMock->expects($this->at(2))->method('getControllerName')->will($this->returnValue('block')); $this->requestMock->expects($this->at(3))->method('getActionName')->will($this->returnValue('render')); $this->requestMock->expects($this->at(4))->method('getRequestUri')->will($this->returnValue('uri')); $this->requestMock->expects($this->at(5))->method('getParam')->with($this->equalTo('originalRequest'))->will($this->returnValue($originalRequest)); $this->requestMock->expects($this->at(10))->method('getParam')->with($this->equalTo('blocks'), $this->equalTo(''))->will($this->returnValue(json_encode($blocks))); $this->requestMock->expects($this->at(11))->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->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(); }
/** * Render current layout * * @param ResponseInterface $response * @return $this */ protected function render(ResponseInterface $response) { $output = $this->layout->getOutput(); $this->translateInline->processResponseBody($output); $response->appendBody($output); return $this; }
/** * Render HTML based on requested layout handle name * * @param string $handle * @return string */ protected function _getHtmlByHandle($handle) { $layout = $this->layoutFactory->create(); $layout->getUpdate()->load([$handle]); $layout->generateXml(); $layout->generateElements(); $output = $layout->getOutput(); $this->_translateInline->processResponseBody($output); return $output; }
/** * Render HTML based on requested layout handle name * * @param string $handle * @return string */ protected function _getHtmlByHandle($handle) { $layout = $this->_view->getLayout(); $layout->getUpdate()->addPageHandles(array($handle)); $layout->getUpdate()->load(); $layout->generateXml(); $layout->generateElements(); $output = $layout->getOutput(); $this->_translateInline->processResponseBody($output); return $output; }
/** * @dataProvider executeDataProvider * @param string $blockClass * @param bool $shouldSetHeaders */ public function testExecute($blockClass, $shouldSetHeaders) { $block = 'block'; $handles = ['handle1', 'handle2']; $html = 'some-html'; $mapData = [['blocks', '', json_encode([$block])], ['handles', '', json_encode($handles)]]; $blockInstance1 = $this->getMock($blockClass, ['toHtml'], [], '', false); $blockInstance1->expects($this->once())->method('toHtml')->will($this->returnValue($html)); $blockInstance1->setTtl(360); $this->requestMock->expects($this->any())->method('getParam')->will($this->returnValueMap($mapData)); $this->viewMock->expects($this->once())->method('loadLayout')->with($this->equalTo($handles)); $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)); if ($shouldSetHeaders) { $this->responseMock->expects($this->once())->method('setHeader')->with('X-Magento-Tags', implode(',', $blockInstance1->getIdentities())); } else { $this->responseMock->expects($this->never())->method('setHeader'); } $this->translateInline->expects($this->once())->method('processResponseBody')->with($html)->willReturnSelf(); $this->responseMock->expects($this->once())->method('appendBody')->with($this->equalTo($html)); $this->action->execute(); }
/** * Render element * * @param \Magento\Framework\Data\Form\Element\AbstractElement $element * @return string * * @see RendererInterface::render() */ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element) { $element->addClass('element-value-changer'); $valueName = $element->getValueName(); if ($valueName === '') { $valueName = '...'; } if ($element->getShowAsText()) { $html = ' <input type="hidden" class="hidden" id="' . $element->getHtmlId() . '" name="' . $element->getName() . '" value="' . $element->getValue() . '"/> ' . htmlspecialchars($valueName) . ' '; } else { $html = ' <span class="rule-param"' . ($element->getParamId() ? ' id="' . $element->getParamId() . '"' : '') . '>' . '<a href="javascript:void(0)" class="label">'; if ($this->inlineTranslate->isAllowed()) { $html .= $this->escapeHtml($valueName); } else { $html .= $this->escapeHtml($this->filterManager->truncate($valueName, ['length' => 33, 'etc' => '...'])); } $html .= '</a><span class="element"> ' . $element->getElementHtml(); if ($element->getExplicitApply()) { $html .= ' <a href="javascript:void(0)" class="rule-param-apply"><img src="' . $this->getViewFileUrl('images/rule_component_apply.gif') . '" class="v-middle" alt="' . __('Apply') . '" title="' . __('Apply') . '" /></a> '; } $html .= '</span></span> '; } return $html; }
/** * @param int|string $httpCode * @param string $headerName * @param string $headerValue * @param bool $replaceHeader * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $setHttpResponseCodeCount * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $setHeaderCount * @dataProvider renderResultDataProvider */ public function testRenderResult($httpCode, $headerName, $headerValue, $replaceHeader, $setHttpResponseCodeCount, $setHeaderCount) { $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')); $this->eventManager->expects($this->exactly(2))->method('dispatch')->withConsecutive(['layout_render_before'], ['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($layoutOutput); $this->resultLayout->setHttpResponseCode($httpCode); if ($headerName && $headerValue) { $this->resultLayout->setHeader($headerName, $headerValue, $replaceHeader); } $this->resultLayout->renderResult($response); }
/** * Rendering layout * * @param string $output * @return $this */ public function renderLayout($output = '') { if ($this->_actionFlag->get('', 'no-renderLayout')) { return $this; } \Magento\Framework\Profiler::start('LAYOUT'); \Magento\Framework\Profiler::start('layout_render'); if ('' !== $output) { $this->getLayout()->addOutputElement($output); } $this->_eventManager->dispatch('controller_action_layout_render_before'); $this->_eventManager->dispatch('controller_action_layout_render_before_' . $this->_request->getFullActionName()); $output = $this->getLayout()->getOutput(); $this->_translateInline->processResponseBody($output); $this->_response->appendBody($output); \Magento\Framework\Profiler::stop('layout_render'); \Magento\Framework\Profiler::stop('LAYOUT'); return $this; }
public function testExecute() { $blocks = ['block1', 'block2']; $handles = ['handle1', 'handle2']; $expectedData = ['block1' => 'data1', 'block2' => 'data2']; $blockInstance1 = $this->getMock('Magento\\PageCache\\Block\\Controller\\StubBlock', ['toHtml'], [], '', false); $blockInstance1->expects($this->once())->method('toHtml')->will($this->returnValue($expectedData['block1'])); $blockInstance2 = $this->getMock('Magento\\PageCache\\Block\\Controller\\StubBlock', ['toHtml'], [], '', false); $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->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->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(); }
/** * Set body to response * * @param string $body * @return void */ protected function setBody($body) { $this->_translateInline->processResponseBody($body); $this->getResponse()->setBody($body); }
/** * {@inheritdoc} */ protected function render(ResponseInterface $response) { $this->translateInline->processResponseBody($this->json, true); $response->representJson($this->json); return $this; }
/** * Add an additional html attribute if needed. * * @param mixed $tagName * @return string */ protected function _getAdditionalHtmlAttribute($tagName = null) { return $this->_translateInline->getAdditionalHtmlAttribute($tagName); }
/** * Translate html content * * @param string $html * @return string */ protected function _translateHtml($html) { $this->_translateInline->processResponseBody($html); return $html; }
/** * Encode the mixed $data into the JSON format. * * @param mixed $data * @return string */ public function encode($data) { $json = \Zend_Json::encode($data); $this->translateInline->processResponseBody($json, true); return $json; }
/** * Encode the mixed $data into the JSON format. * * @param mixed $data * @return string */ public function encode($data) { $this->translateInline->processResponseBody($data); return \Zend_Json::encode($data); }