Skip to content

Commit f14b38c

Browse files
author
Dmytro Poperechnyy
committed
Merge remote-tracking branch 'tango/MAGETWO-34559' into S53_bugs
2 parents 8a32981 + 7feedea commit f14b38c

File tree

2 files changed

+210
-7
lines changed

2 files changed

+210
-7
lines changed

lib/internal/Magento/Framework/Data/Form/Element/Editor.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ public function __construct(
4040
}
4141
}
4242

43+
/**
44+
* @return array
45+
*/
46+
protected function getButtonTranslations()
47+
{
48+
$buttonTranslations = [
49+
'Insert Image...' => $this->translate('Insert Image...'),
50+
'Insert Media...' => $this->translate('Insert Media...'),
51+
'Insert File...' => $this->translate('Insert File...'),
52+
];
53+
54+
return $buttonTranslations;
55+
}
56+
4357
/**
4458
* @return string
4559
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -71,12 +85,6 @@ public function getElementHtml()
7185
</script>';
7286

7387
if ($this->isEnabled()) {
74-
$translatedString = [
75-
'Insert Image...' => $this->translate('Insert Image...'),
76-
'Insert Media...' => $this->translate('Insert Media...'),
77-
'Insert File...' => $this->translate('Insert File...'),
78-
];
79-
8088
$jsSetupObject = 'wysiwyg' . $this->getHtmlId();
8189

8290
$forceLoad = '';
@@ -119,7 +127,7 @@ public function getElementHtml()
119127
"\n" .
120128
'(function($) {$.mage.translate.add(' .
121129
\Zend_Json::encode(
122-
$translatedString
130+
$this->getButtonTranslations()
123131
) .
124132
')})(jQuery);' .
125133
"\n" .
@@ -161,6 +169,17 @@ public function getElementHtml()
161169
// Display only buttons to additional features
162170
if ($this->getConfig('widget_window_url')) {
163171
$html = $this->_getButtonsHtml() . $js . parent::getElementHtml();
172+
if ($this->getConfig('add_widgets')) {
173+
$html .= '<script type="text/javascript">
174+
//<![CDATA[
175+
require(["jquery", "mage/translate", "mage/adminhtml/wysiwyg/widget"], function(jQuery){
176+
(function($) {
177+
$.mage.translate.add(' . \Zend_Json::encode($this->getButtonTranslations()) . ')
178+
})(jQuery);
179+
});
180+
//]]>
181+
</script>';
182+
}
164183
$html = $this->_wrapIntoContainer($html);
165184
return $html;
166185
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/**
8+
* Tests for \Magento\Framework\Data\Form\Element\Editor
9+
*/
10+
namespace Magento\Framework\Data\Test\Unit\Form\Element;
11+
12+
use Magento\Framework\Data\Form\Element\Editor;
13+
14+
class EditorTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* @var Editor
18+
*/
19+
protected $model;
20+
21+
/**
22+
* @var \Magento\Framework\Data\Form\Element\Factory|\PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
protected $factoryMock;
25+
26+
/**
27+
* @var \Magento\Framework\Data\Form\Element\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
protected $collectionFactoryMock;
30+
31+
/**
32+
* @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject
33+
*/
34+
protected $escaperMock;
35+
36+
/**
37+
* @var \Magento\Framework\Object|\PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
protected $formMock;
40+
41+
/**
42+
* @var \Magento\Framework\Object|\PHPUnit_Framework_MockObject_MockObject
43+
*/
44+
protected $configMock;
45+
46+
/**
47+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
48+
*/
49+
protected $objectManager;
50+
51+
protected function setUp()
52+
{
53+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
54+
$this->factoryMock = $this->getMock('\Magento\Framework\Data\Form\Element\Factory', [], [], '', false);
55+
$this->collectionFactoryMock = $this->getMock(
56+
'\Magento\Framework\Data\Form\Element\CollectionFactory',
57+
[],
58+
[],
59+
'',
60+
false
61+
);
62+
$this->escaperMock = $this->getMock('\Magento\Framework\Escaper', [], [], '', false);
63+
$this->configMock = $this->getMock('\Magento\Framework\Object', ['getData'], [], '', false);
64+
65+
$this->model = $this->objectManager->getObject(
66+
'Magento\Framework\Data\Form\Element\Editor',
67+
[
68+
'factoryElement' => $this->factoryMock,
69+
'factoryCollection' => $this->collectionFactoryMock,
70+
'escaper' => $this->escaperMock,
71+
'data' => ['config' => $this->configMock]
72+
]
73+
);
74+
75+
$this->formMock = $this->getMock(
76+
'Magento\Framework\Data\Form',
77+
['getHtmlIdPrefix', 'getHtmlIdSuffix'],
78+
[],
79+
'',
80+
false,
81+
false
82+
);
83+
$this->model->setForm($this->formMock);
84+
}
85+
86+
public function testConstruct()
87+
{
88+
$this->assertEquals('textarea', $this->model->getType());
89+
$this->assertEquals('textarea', $this->model->getExtType());
90+
$this->assertEquals(Editor::DEFAULT_ROWS, $this->model->getRows());
91+
$this->assertEquals(Editor::DEFAULT_COLS, $this->model->getCols());
92+
93+
$this->configMock->expects($this->once())->method('getData')->with('enabled')->willReturn(true);
94+
95+
$model = $this->objectManager->getObject(
96+
'Magento\Framework\Data\Form\Element\Editor',
97+
[
98+
'factoryElement' => $this->factoryMock,
99+
'factoryCollection' => $this->collectionFactoryMock,
100+
'escaper' => $this->escaperMock,
101+
'data' => ['config' => $this->configMock]
102+
]
103+
);
104+
105+
$this->assertEquals('wysiwyg', $model->getType());
106+
$this->assertEquals('wysiwyg', $model->getExtType());
107+
}
108+
109+
public function testGetElementHtml()
110+
{
111+
$html = $this->model->getElementHtml();
112+
$this->assertContains('</textarea>', $html);
113+
$this->assertContains('rows="2"', $html);
114+
$this->assertContains('cols="15"', $html);
115+
$this->assertRegExp('/class=\".*textarea.*\"/i', $html);
116+
$this->assertNotRegExp('/.*mage\/adminhtml\/wysiwyg\/widget.*/i', $html);
117+
118+
$this->configMock->expects($this->any())->method('getData')
119+
->willReturnMap(
120+
[
121+
['enabled', null, true],
122+
['hidden', null, null]
123+
]
124+
);
125+
$html = $this->model->getElementHtml();
126+
$this->assertRegExp('/.*mage\/adminhtml\/wysiwyg\/widget.*/i', $html);
127+
128+
$this->configMock->expects($this->any())->method('getData')
129+
->willReturnMap(
130+
[
131+
['enabled', null, null],
132+
['widget_window_url', null, 'localhost'],
133+
['add_widgets', null, true],
134+
['hidden', null, null]
135+
]
136+
);
137+
$html = $this->model->getElementHtml();
138+
$this->assertRegExp('/.*mage\/adminhtml\/wysiwyg\/widget.*/i', $html);
139+
}
140+
141+
public function testIsEnabled()
142+
{
143+
$this->assertEmpty($this->model->isEnabled());
144+
145+
$this->model->setData('wysiwyg', true);
146+
$this->assertTrue($this->model->isEnabled());
147+
148+
$this->model->unsetData('wysiwyg');
149+
$this->configMock->expects($this->once())->method('getData')->with('enabled')->willReturn(true);
150+
$this->assertTrue($this->model->isEnabled());
151+
}
152+
153+
public function testIsHidden()
154+
{
155+
$this->assertEmpty($this->model->isHidden());
156+
157+
$this->configMock->expects($this->once())->method('getData')->with('hidden')->willReturn(true);
158+
$this->assertTrue($this->model->isHidden());
159+
}
160+
161+
public function testTranslate()
162+
{
163+
$this->assertEquals('Insert Image...', $this->model->translate('Insert Image...'));
164+
}
165+
166+
public function testGetConfig()
167+
{
168+
$config = $this->getMock('\Magento\Framework\Object', ['getData'], [], '', false);
169+
$this->assertEquals($config, $this->model->getConfig());
170+
171+
$this->configMock->expects($this->once())->method('getData')->with('test')->willReturn('test');
172+
$this->assertEquals('test', $this->model->getConfig('test'));
173+
}
174+
175+
/**
176+
* Test protected `getTranslatedString` method via public `getElementHtml` method
177+
*/
178+
public function testGetTranslatedString()
179+
{
180+
$this->configMock->expects($this->any())->method('getData')->withConsecutive(['enabled'])->willReturn(true);
181+
$html = $this->model->getElementHtml();
182+
$this->assertRegExp('/.*"Insert Image...":"Insert Image...".*/i', $html);
183+
}
184+
}

0 commit comments

Comments
 (0)