Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DirectiveTest.php
Go to the documentation of this file.
1 <?php
7 
12 class DirectiveTest extends \PHPUnit\Framework\TestCase
13 {
14  const IMAGE_PATH = 'pub/media/wysiwyg/image.jpg';
15 
19  protected $wysiwygDirective;
20 
24  protected $actionContextMock;
25 
29  protected $requestMock;
30 
34  protected $urlDecoderMock;
35 
39  protected $objectManagerMock;
40 
45 
50 
54  protected $imageAdapterMock;
55 
59  protected $responseMock;
60 
64  protected $wysiwygConfigMock;
65 
69  protected $loggerMock;
70 
74  protected $rawFactoryMock;
75 
79  protected $rawMock;
80 
81  protected function setUp()
82  {
83  $this->actionContextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
84  ->disableOriginalConstructor()
85  ->getMock();
86  $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
87  ->disableOriginalConstructor()
88  ->getMock();
89  $this->urlDecoderMock = $this->getMockBuilder(\Magento\Framework\Url\DecoderInterface::class)
90  ->disableOriginalConstructor()
91  ->getMock();
92  $this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
93  ->disableOriginalConstructor()
94  ->getMock();
95  $this->templateFilterMock = $this->getMockBuilder(\Magento\Cms\Model\Template\Filter::class)
96  ->disableOriginalConstructor()
97  ->getMock();
98  $this->imageAdapterFactoryMock = $this->getMockBuilder(\Magento\Framework\Image\AdapterFactory::class)
99  ->disableOriginalConstructor()
100  ->getMock();
101  $this->imageAdapterMock = $this->getMockBuilder(\Magento\Framework\Image\Adapter\AdapterInterface::class)
102  ->disableOriginalConstructor()
103  ->setMethods(
104  [
105  'getMimeType',
106  'getColorAt',
107  'getImage',
108  'watermark',
109  'refreshImageDimensions',
110  'checkDependencies',
111  'createPngFromString',
112  'open',
113  'resize',
114  'crop',
115  'save',
116  'rotate'
117  ]
118  )
119  ->getMock();
120  $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
121  ->disableOriginalConstructor()
122  ->setMethods(['setHeader', 'setBody', 'sendResponse'])
123  ->getMock();
124  $this->wysiwygConfigMock = $this->getMockBuilder(\Magento\Cms\Model\Wysiwyg\Config::class)
125  ->disableOriginalConstructor()
126  ->getMock();
127  $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
128  ->disableOriginalConstructor()
129  ->getMock();
130  $this->rawFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\RawFactory::class)
131  ->setMethods(['create'])
132  ->disableOriginalConstructor()
133  ->getMock();
134  $this->rawMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Raw::class)
135  ->disableOriginalConstructor()
136  ->getMock();
137 
138  $this->actionContextMock->expects($this->any())
139  ->method('getRequest')
140  ->willReturn($this->requestMock);
141  $this->actionContextMock->expects($this->any())
142  ->method('getResponse')
143  ->willReturn($this->responseMock);
144  $this->actionContextMock->expects($this->any())
145  ->method('getObjectManager')
146  ->willReturn($this->objectManagerMock);
147 
148  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
149  $this->wysiwygDirective = $objectManager->getObject(
150  \Magento\Cms\Controller\Adminhtml\Wysiwyg\Directive::class,
151  [
152  'context' => $this->actionContextMock,
153  'urlDecoder' => $this->urlDecoderMock,
154  'resultRawFactory' => $this->rawFactoryMock
155  ]
156  );
157  }
158 
162  public function testExecute()
163  {
164  $mimeType = 'image/jpeg';
165  $imageBody = 'abcdefghijklmnopqrstuvwxyz0123456789';
166  $this->prepareExecuteTest();
167 
168  $this->imageAdapterMock->expects($this->once())
169  ->method('open')
170  ->with(self::IMAGE_PATH);
171  $this->imageAdapterMock->expects($this->once())
172  ->method('getMimeType')
173  ->willReturn($mimeType);
174  $this->rawMock->expects($this->once())
175  ->method('setHeader')
176  ->with('Content-Type', $mimeType)
177  ->willReturnSelf();
178  $this->rawMock->expects($this->once())
179  ->method('setContents')
180  ->with($imageBody)
181  ->willReturnSelf();
182  $this->imageAdapterMock->expects($this->once())
183  ->method('getImage')
184  ->willReturn($imageBody);
185  $this->rawFactoryMock->expects($this->any())
186  ->method('create')
187  ->willReturn($this->rawMock);
188 
189  $this->assertSame(
190  $this->rawMock,
191  $this->wysiwygDirective->execute()
192  );
193  }
194 
198  public function testExecuteException()
199  {
200  $exception = new \Exception('epic fail');
201  $placeholderPath = 'pub/static/adminhtml/Magento/backend/en_US/Magento_Cms/images/wysiwyg_skin_image.png';
202  $mimeType = 'image/png';
203  $imageBody = '0123456789abcdefghijklmnopqrstuvwxyz';
204  $this->prepareExecuteTest();
205 
206  $this->imageAdapterMock->expects($this->at(0))
207  ->method('open')
208  ->with(self::IMAGE_PATH)
209  ->willThrowException($exception);
210  $this->wysiwygConfigMock->expects($this->once())
211  ->method('getSkinImagePlaceholderPath')
212  ->willReturn($placeholderPath);
213  $this->imageAdapterMock->expects($this->at(1))
214  ->method('open')
215  ->with($placeholderPath);
216  $this->imageAdapterMock->expects($this->once())
217  ->method('getMimeType')
218  ->willReturn($mimeType);
219  $this->rawMock->expects($this->once())
220  ->method('setHeader')
221  ->with('Content-Type', $mimeType)
222  ->willReturnSelf();
223  $this->rawMock->expects($this->once())
224  ->method('setContents')
225  ->with($imageBody)
226  ->willReturnSelf();
227  $this->imageAdapterMock->expects($this->once())
228  ->method('getImage')
229  ->willReturn($imageBody);
230  $this->loggerMock->expects($this->once())
231  ->method('critical')
232  ->with($exception);
233  $this->rawFactoryMock->expects($this->any())
234  ->method('create')
235  ->willReturn($this->rawMock);
236 
237  $this->assertSame(
238  $this->rawMock,
239  $this->wysiwygDirective->execute()
240  );
241  }
242 
243  protected function prepareExecuteTest()
244  {
245  $directiveParam = 'e3ttZWRpYSB1cmw9Ind5c2l3eWcvYnVubnkuanBnIn19';
246  $directive = '{{media url="wysiwyg/image.jpg"}}';
247 
248  $this->requestMock->expects($this->once())
249  ->method('getParam')
250  ->with('___directive')
251  ->willReturn($directiveParam);
252  $this->urlDecoderMock->expects($this->once())
253  ->method('decode')
254  ->with($directiveParam)
255  ->willReturn($directive);
256  $this->objectManagerMock->expects($this->once())
257  ->method('create')
258  ->with(\Magento\Cms\Model\Template\Filter::class)
259  ->willReturn($this->templateFilterMock);
260  $this->templateFilterMock->expects($this->once())
261  ->method('filter')
262  ->with($directive)
263  ->willReturn(self::IMAGE_PATH);
264  $this->objectManagerMock->expects($this->any())
265  ->method('get')
266  ->willReturnMap(
267  [
268  [\Magento\Framework\Image\AdapterFactory::class, $this->imageAdapterFactoryMock],
269  [\Magento\Cms\Model\Wysiwyg\Config::class, $this->wysiwygConfigMock],
270  [\Psr\Log\LoggerInterface::class, $this->loggerMock]
271  ]
272  );
273  $this->imageAdapterFactoryMock->expects($this->once())
274  ->method('create')
275  ->willReturn($this->imageAdapterMock);
276  }
277 }
$objectManager
Definition: bootstrap.php:17