Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MessagePluginTest.php
Go to the documentation of this file.
1 <?php
7 
20 
24 class MessagePluginTest extends \PHPUnit\Framework\TestCase
25 {
27  protected $model;
28 
30  protected $cookieManagerMock;
31 
34 
36  protected $managerMock;
37 
40 
42  private $serializerMock;
43 
45  private $inlineTranslateMock;
46 
47  protected function setUp()
48  {
49  $this->cookieManagerMock = $this->getMockBuilder(CookieManagerInterface::class)
50  ->getMockForAbstractClass();
51  $this->cookieMetadataFactoryMock = $this->getMockBuilder(CookieMetadataFactory::class)
52  ->disableOriginalConstructor()
53  ->getMock();
54  $this->managerMock = $this->getMockBuilder(ManagerInterface::class)
55  ->getMockForAbstractClass();
56  $this->interpretationStrategyMock = $this->getMockBuilder(InterpretationStrategyInterface::class)
57  ->getMockForAbstractClass();
58  $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
59  ->getMock();
60  $this->inlineTranslateMock = $this->getMockBuilder(InlineInterface::class)->getMockForAbstractClass();
61 
62  $this->model = new MessagePlugin(
63  $this->cookieManagerMock,
64  $this->cookieMetadataFactoryMock,
65  $this->managerMock,
66  $this->interpretationStrategyMock,
67  $this->serializerMock,
68  $this->inlineTranslateMock
69  );
70  }
71 
72  public function testAfterRenderResultJson()
73  {
75  $resultMock = $this->getMockBuilder(Json::class)
76  ->disableOriginalConstructor()
77  ->getMock();
78 
79  $this->cookieManagerMock->expects($this->never())
80  ->method('setPublicCookie');
81 
82  $this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
83  }
84 
85  public function testAfterRenderResult()
86  {
87  $existingMessages = [
88  [
89  'type' => 'message0type',
90  'text' => 'message0text',
91  ],
92  ];
93  $messageType = 'message1type';
94  $messageText = 'message1text';
95  $messages = [
96  [
97  'type' => $messageType,
98  'text' => $messageText,
99  ],
100  ];
101  $messages = array_merge($existingMessages, $messages);
102 
104  $resultMock = $this->getMockBuilder(Redirect::class)
105  ->disableOriginalConstructor()
106  ->getMock();
107 
109  $cookieMetadataMock = $this->getMockBuilder(PublicCookieMetadata::class)
110  ->disableOriginalConstructor()
111  ->getMock();
112 
113  $this->cookieMetadataFactoryMock->expects($this->once())
114  ->method('createPublicCookieMetadata')
115  ->willReturn($cookieMetadataMock);
116 
117  $this->cookieManagerMock->expects($this->once())
118  ->method('setPublicCookie')
119  ->with(
121  json_encode($messages),
122  $cookieMetadataMock
123  );
124  $this->cookieManagerMock->expects($this->once())
125  ->method('getCookie')
126  ->with(
128  )
129  ->willReturn(json_encode($existingMessages));
130 
131  $this->serializerMock->expects($this->once())
132  ->method('unserialize')
133  ->willReturnCallback(
134  function ($data) {
135  return json_decode($data, true);
136  }
137  );
138  $this->serializerMock->expects($this->once())
139  ->method('serialize')
140  ->willReturnCallback(
141  function ($data) {
142  return json_encode($data);
143  }
144  );
145 
147  $messageMock = $this->getMockBuilder(MessageInterface::class)
148  ->getMock();
149  $messageMock->expects($this->once())
150  ->method('getType')
151  ->willReturn($messageType);
152 
153  $this->interpretationStrategyMock->expects($this->once())
154  ->method('interpret')
155  ->with($messageMock)
156  ->willReturn($messageText);
157 
159  $collectionMock = $this->getMockBuilder(Collection::class)
160  ->disableOriginalConstructor()
161  ->getMock();
162  $collectionMock->expects($this->once())
163  ->method('getItems')
164  ->willReturn([$messageMock]);
165 
166  $this->managerMock->expects($this->once())
167  ->method('getMessages')
168  ->with(true, null)
169  ->willReturn($collectionMock);
170 
171  $this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
172  }
173 
174  public function testAfterRenderResultWithNoMessages()
175  {
177  $resultMock = $this->getMockBuilder(Redirect::class)
178  ->disableOriginalConstructor()
179  ->getMock();
180 
181  $this->cookieManagerMock->expects($this->once())
182  ->method('getCookie')
183  ->with(
185  )
186  ->willReturn(json_encode([]));
187 
188  $this->serializerMock->expects($this->once())
189  ->method('unserialize')
190  ->willReturnCallback(
191  function ($data) {
192  return json_decode($data, true);
193  }
194  );
195  $this->serializerMock->expects($this->never())
196  ->method('serialize');
197 
199  $collectionMock = $this->getMockBuilder(Collection::class)
200  ->disableOriginalConstructor()
201  ->getMock();
202  $collectionMock->expects($this->once())
203  ->method('getItems')
204  ->willReturn([]);
205 
206  $this->managerMock->expects($this->once())
207  ->method('getMessages')
208  ->with(true, null)
209  ->willReturn($collectionMock);
210 
211  $this->cookieMetadataFactoryMock->expects($this->never())
212  ->method('createPublicCookieMetadata')
213  ->willReturn(null);
214 
215  $this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
216  }
217 
218  public function testAfterRenderResultWithoutExisting()
219  {
220  $messageType = 'message1type';
221  $messageText = 'message1text';
222  $messages = [
223  [
224  'type' => $messageType,
225  'text' => $messageText,
226  ],
227  ];
228 
230  $resultMock = $this->getMockBuilder(Redirect::class)
231  ->disableOriginalConstructor()
232  ->getMock();
233 
235  $cookieMetadataMock = $this->getMockBuilder(PublicCookieMetadata::class)
236  ->disableOriginalConstructor()
237  ->getMock();
238 
239  $this->cookieMetadataFactoryMock->expects($this->once())
240  ->method('createPublicCookieMetadata')
241  ->willReturn($cookieMetadataMock);
242 
243  $this->cookieManagerMock->expects($this->once())
244  ->method('setPublicCookie')
245  ->with(
247  json_encode($messages),
248  $cookieMetadataMock
249  );
250  $this->cookieManagerMock->expects($this->once())
251  ->method('getCookie')
252  ->with(
254  )
255  ->willReturn(json_encode([]));
256 
257  $this->serializerMock->expects($this->once())
258  ->method('unserialize')
259  ->willReturnCallback(
260  function ($data) {
261  return json_decode($data, true);
262  }
263  );
264  $this->serializerMock->expects($this->once())
265  ->method('serialize')
266  ->willReturnCallback(
267  function ($data) {
268  return json_encode($data);
269  }
270  );
271 
273  $messageMock = $this->getMockBuilder(MessageInterface::class)
274  ->getMock();
275  $messageMock->expects($this->once())
276  ->method('getType')
277  ->willReturn($messageType);
278 
279  $this->interpretationStrategyMock->expects($this->once())
280  ->method('interpret')
281  ->with($messageMock)
282  ->willReturn($messageText);
283 
285  $collectionMock = $this->getMockBuilder(Collection::class)
286  ->disableOriginalConstructor()
287  ->getMock();
288  $collectionMock->expects($this->once())
289  ->method('getItems')
290  ->willReturn([$messageMock]);
291 
292  $this->managerMock->expects($this->once())
293  ->method('getMessages')
294  ->with(true, null)
295  ->willReturn($collectionMock);
296 
297  $this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
298  }
299 
300  public function testAfterRenderResultWithWrongJson()
301  {
302  $messageType = 'message1type';
303  $messageText = 'message1text';
304  $messages = [
305  [
306  'type' => $messageType,
307  'text' => $messageText,
308  ],
309  ];
310 
312  $resultMock = $this->getMockBuilder(Redirect::class)
313  ->disableOriginalConstructor()
314  ->getMock();
315 
317  $cookieMetadataMock = $this->getMockBuilder(PublicCookieMetadata::class)
318  ->disableOriginalConstructor()
319  ->getMock();
320 
321  $this->cookieMetadataFactoryMock->expects($this->once())
322  ->method('createPublicCookieMetadata')
323  ->willReturn($cookieMetadataMock);
324 
325  $this->cookieManagerMock->expects($this->once())
326  ->method('setPublicCookie')
327  ->with(
329  json_encode($messages),
330  $cookieMetadataMock
331  );
332  $this->cookieManagerMock->expects($this->once())
333  ->method('getCookie')
334  ->with(
336  )
337  ->willReturn(null);
338 
339  $this->serializerMock->expects($this->never())
340  ->method('unserialize');
341 
342  $this->serializerMock->expects($this->once())
343  ->method('serialize')
344  ->willReturnCallback(
345  function ($data) {
346  return json_encode($data);
347  }
348  );
349 
351  $messageMock = $this->getMockBuilder(MessageInterface::class)
352  ->getMock();
353  $messageMock->expects($this->once())
354  ->method('getType')
355  ->willReturn($messageType);
356 
357  $this->interpretationStrategyMock->expects($this->once())
358  ->method('interpret')
359  ->with($messageMock)
360  ->willReturn($messageText);
361 
363  $collectionMock = $this->getMockBuilder(Collection::class)
364  ->disableOriginalConstructor()
365  ->getMock();
366  $collectionMock->expects($this->once())
367  ->method('getItems')
368  ->willReturn([$messageMock]);
369 
370  $this->managerMock->expects($this->once())
371  ->method('getMessages')
372  ->with(true, null)
373  ->willReturn($collectionMock);
374 
375  $this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
376  }
377 
378  public function testAfterRenderResultWithWrongArray()
379  {
380  $messageType = 'message1type';
381  $messageText = 'message1text';
382  $messages = [
383  [
384  'type' => $messageType,
385  'text' => $messageText,
386  ],
387  ];
388 
390  $resultMock = $this->getMockBuilder(Redirect::class)
391  ->disableOriginalConstructor()
392  ->getMock();
393 
395  $cookieMetadataMock = $this->getMockBuilder(PublicCookieMetadata::class)
396  ->disableOriginalConstructor()
397  ->getMock();
398 
399  $this->cookieMetadataFactoryMock->expects($this->once())
400  ->method('createPublicCookieMetadata')
401  ->willReturn($cookieMetadataMock);
402 
403  $this->cookieManagerMock->expects($this->once())
404  ->method('setPublicCookie')
405  ->with(
407  json_encode($messages),
408  $cookieMetadataMock
409  );
410  $this->cookieManagerMock->expects($this->once())
411  ->method('getCookie')
412  ->with(
414  )
415  ->willReturn(json_encode('string'));
416 
417  $this->serializerMock->expects($this->once())
418  ->method('unserialize')
419  ->willReturnCallback(
420  function ($data) {
421  return json_decode($data, true);
422  }
423  );
424  $this->serializerMock->expects($this->once())
425  ->method('serialize')
426  ->willReturnCallback(
427  function ($data) {
428  return json_encode($data);
429  }
430  );
431 
433  $messageMock = $this->getMockBuilder(MessageInterface::class)
434  ->getMock();
435  $messageMock->expects($this->once())
436  ->method('getType')
437  ->willReturn($messageType);
438 
439  $this->interpretationStrategyMock->expects($this->once())
440  ->method('interpret')
441  ->with($messageMock)
442  ->willReturn($messageText);
443 
445  $collectionMock = $this->getMockBuilder(Collection::class)
446  ->disableOriginalConstructor()
447  ->getMock();
448  $collectionMock->expects($this->once())
449  ->method('getItems')
450  ->willReturn([$messageMock]);
451 
452  $this->managerMock->expects($this->once())
453  ->method('getMessages')
454  ->with(true, null)
455  ->willReturn($collectionMock);
456 
457  $this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
458  }
459 
463  public function testAfterRenderResultWithAllowedInlineTranslate(): void
464  {
465  $messageType = 'message1type';
466  $messageText = '{{{message1text}}{{message1text}}{{message1text}}{{theme/luma}}}';
467  $expectedMessages = [
468  [
469  'type' => $messageType,
470  'text' => 'message1text',
471  ],
472  ];
473 
475  $resultMock = $this->getMockBuilder(Redirect::class)
476  ->disableOriginalConstructor()
477  ->getMock();
478 
480  $cookieMetadataMock = $this->getMockBuilder(PublicCookieMetadata::class)
481  ->disableOriginalConstructor()
482  ->getMock();
483 
484  $this->cookieMetadataFactoryMock->expects($this->once())
485  ->method('createPublicCookieMetadata')
486  ->willReturn($cookieMetadataMock);
487 
488  $this->cookieManagerMock->expects($this->once())
489  ->method('setPublicCookie')
490  ->with(
492  json_encode($expectedMessages),
493  $cookieMetadataMock
494  );
495  $this->cookieManagerMock->expects($this->once())
496  ->method('getCookie')
497  ->with(
499  )
500  ->willReturn(json_encode([]));
501 
502  $this->serializerMock->expects($this->once())
503  ->method('unserialize')
504  ->willReturnCallback(
505  function ($data) {
506  return json_decode($data, true);
507  }
508  );
509  $this->serializerMock->expects($this->once())
510  ->method('serialize')
511  ->willReturnCallback(
512  function ($data) {
513  return json_encode($data);
514  }
515  );
516 
518  $messageMock = $this->getMockBuilder(MessageInterface::class)
519  ->getMock();
520  $messageMock->expects($this->once())
521  ->method('getType')
522  ->willReturn($messageType);
523 
524  $this->interpretationStrategyMock->expects($this->once())
525  ->method('interpret')
526  ->with($messageMock)
527  ->willReturn($messageText);
528 
529  $this->inlineTranslateMock->expects($this->once())
530  ->method('isAllowed')
531  ->willReturn(true);
532 
534  $collectionMock = $this->getMockBuilder(Collection::class)
535  ->disableOriginalConstructor()
536  ->getMock();
537  $collectionMock->expects($this->once())
538  ->method('getItems')
539  ->willReturn([$messageMock]);
540 
541  $this->managerMock->expects($this->once())
542  ->method('getMessages')
543  ->with(true, null)
544  ->willReturn($collectionMock);
545 
546  $this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
547  }
548 }