Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EditorTest.php
Go to the documentation of this file.
1 <?php
11 
13 
14 class EditorTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $model;
20 
24  protected $factoryMock;
25 
30 
34  protected $escaperMock;
35 
39  protected $formMock;
40 
44  protected $configMock;
45 
49  protected $objectManager;
50 
54  private $serializer;
55 
56  protected function setUp()
57  {
58  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
59  $this->factoryMock = $this->createMock(\Magento\Framework\Data\Form\Element\Factory::class);
60  $this->collectionFactoryMock = $this->createMock(\Magento\Framework\Data\Form\Element\CollectionFactory::class);
61  $this->escaperMock = $this->createMock(\Magento\Framework\Escaper::class);
62  $this->configMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getData']);
63 
64  $this->serializer = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class);
65 
66  $this->model = $this->objectManager->getObject(
67  \Magento\Framework\Data\Form\Element\Editor::class,
68  [
69  'factoryElement' => $this->factoryMock,
70  'factoryCollection' => $this->collectionFactoryMock,
71  'escaper' => $this->escaperMock,
72  'data' => ['config' => $this->configMock],
73  'serializer' => $this->serializer
74  ]
75  );
76 
77  $this->formMock =
78  $this->createPartialMock(\Magento\Framework\Data\Form::class, ['getHtmlIdPrefix', 'getHtmlIdSuffix']);
79  $this->model->setForm($this->formMock);
80  }
81 
82  public function testConstruct()
83  {
84  $this->assertEquals('textarea', $this->model->getType());
85  $this->assertEquals('textarea', $this->model->getExtType());
86  $this->assertEquals(Editor::DEFAULT_ROWS, $this->model->getRows());
87  $this->assertEquals(Editor::DEFAULT_COLS, $this->model->getCols());
88 
89  $this->configMock->expects($this->once())->method('getData')->with('enabled')->willReturn(true);
90 
91  $model = $this->objectManager->getObject(
92  \Magento\Framework\Data\Form\Element\Editor::class,
93  [
94  'factoryElement' => $this->factoryMock,
95  'factoryCollection' => $this->collectionFactoryMock,
96  'escaper' => $this->escaperMock,
97  'data' => ['config' => $this->configMock]
98  ]
99  );
100 
101  $this->assertEquals('wysiwyg', $model->getType());
102  $this->assertEquals('wysiwyg', $model->getExtType());
103  }
104 
105  public function testGetElementHtml()
106  {
107  $html = $this->model->getElementHtml();
108  $this->assertContains('</textarea>', $html);
109  $this->assertContains('rows="2"', $html);
110  $this->assertContains('cols="15"', $html);
111  $this->assertRegExp('/class=\".*textarea.*\"/i', $html);
112  $this->assertNotRegExp('/.*mage\/adminhtml\/wysiwyg\/widget.*/i', $html);
113 
114  $this->configMock->expects($this->any())->method('getData')
115  ->willReturnMap(
116  [
117  ['enabled', null, true],
118  ['hidden', null, null]
119  ]
120  );
121  $html = $this->model->getElementHtml();
122  $this->assertRegExp('/.*mage\/adminhtml\/wysiwyg\/widget.*/i', $html);
123 
124  $this->configMock->expects($this->any())->method('getData')
125  ->willReturnMap(
126  [
127  ['enabled', null, null],
128  ['widget_window_url', null, 'localhost'],
129  ['add_widgets', null, true],
130  ['hidden', null, null]
131  ]
132  );
133  $html = $this->model->getElementHtml();
134  $this->assertRegExp('/.*mage\/adminhtml\/wysiwyg\/widget.*/i', $html);
135  }
136 
144  public function testIsEnabled($expected, $globalFlag, $attributeFlag = null)
145  {
146  $this->configMock
147  ->expects($this->once())
148  ->method('getData')
149  ->with('enabled')
150  ->willReturn($globalFlag);
151 
152  if ($attributeFlag !== null) {
153  $this->model->setData('wysiwyg', $attributeFlag);
154  }
155  $this->assertEquals($expected, $this->model->isEnabled());
156  }
157 
161  public function isEnabledDataProvider()
162  {
163  return [
164  'Global disabled, attribute isnt set' => [false, false],
165  'Global disabled, attribute disabled' => [false, false, false],
166  'Global disabled, attribute enabled' => [false, false, true],
167 
168  'Global enabled, attribute isnt set' => [true, true],
169  'Global enabled, attribute disabled' => [false, true, false],
170  'Global enabled, attribute enabled' => [true, true, true]
171  ];
172  }
173 
174  public function testIsHidden()
175  {
176  $this->assertEmpty($this->model->isHidden());
177 
178  $this->configMock->expects($this->once())->method('getData')->with('hidden')->willReturn(true);
179  $this->assertTrue($this->model->isHidden());
180  }
181 
182  public function testTranslate()
183  {
184  $this->assertEquals('Insert Image...', $this->model->translate('Insert Image...'));
185  }
186 
187  public function testGetConfig()
188  {
189  $config = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getData']);
190  $this->assertEquals($config, $this->model->getConfig());
191 
192  $this->configMock->expects($this->once())->method('getData')->with('test')->willReturn('test');
193  $this->assertEquals('test', $this->model->getConfig('test'));
194  }
195 
199  public function testGetTranslatedString()
200  {
201  $callback = function ($params) {
202  return json_encode($params);
203  };
204 
205  $this->configMock->expects($this->any())->method('getData')->withConsecutive(['enabled'])->willReturn(true);
206  $this->serializer->expects($this->any())
207  ->method('serialize')
208  ->willReturnCallback($callback);
209 
210  $html = $this->model->getElementHtml();
211 
212  $this->assertRegExp('/.*"Insert Image...":"Insert Image...".*/i', $html);
213  }
214 }
$config
Definition: fraud_order.php:17
testIsEnabled($expected, $globalFlag, $attributeFlag=null)
Definition: EditorTest.php:144
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18