14
14
use Magento \Framework \Stdlib \Cookie \CookieMetadataFactory ;
15
15
use Magento \Framework \Stdlib \Cookie \PublicCookieMetadata ;
16
16
use Magento \Framework \Stdlib \CookieManagerInterface ;
17
+ use Magento \Framework \Translate \InlineInterface ;
17
18
use Magento \Framework \View \Element \Message \InterpretationStrategyInterface ;
18
19
use Magento \Theme \Controller \Result \MessagePlugin ;
19
20
@@ -40,6 +41,9 @@ class MessagePluginTest extends \PHPUnit\Framework\TestCase
40
41
/** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
41
42
private $ serializerMock ;
42
43
44
+ /** @var InlineInterface|\PHPUnit_Framework_MockObject_MockObject */
45
+ private $ inlineTranslateMock ;
46
+
43
47
protected function setUp ()
44
48
{
45
49
$ this ->cookieManagerMock = $ this ->getMockBuilder (CookieManagerInterface::class)
@@ -53,13 +57,15 @@ protected function setUp()
53
57
->getMockForAbstractClass ();
54
58
$ this ->serializerMock = $ this ->getMockBuilder (\Magento \Framework \Serialize \Serializer \Json::class)
55
59
->getMock ();
60
+ $ this ->inlineTranslateMock = $ this ->getMockBuilder (InlineInterface::class)->getMockForAbstractClass ();
56
61
57
62
$ this ->model = new MessagePlugin (
58
63
$ this ->cookieManagerMock ,
59
64
$ this ->cookieMetadataFactoryMock ,
60
65
$ this ->managerMock ,
61
66
$ this ->interpretationStrategyMock ,
62
- $ this ->serializerMock
67
+ $ this ->serializerMock ,
68
+ $ this ->inlineTranslateMock
63
69
);
64
70
}
65
71
@@ -450,4 +456,93 @@ function ($data) {
450
456
451
457
$ this ->assertEquals ($ resultMock , $ this ->model ->afterRenderResult ($ resultMock , $ resultMock ));
452
458
}
459
+
460
+ /**
461
+ * @return void
462
+ */
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
+
474
+ /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $resultMock */
475
+ $ resultMock = $ this ->getMockBuilder (Redirect::class)
476
+ ->disableOriginalConstructor ()
477
+ ->getMock ();
478
+
479
+ /** @var PublicCookieMetadata|\PHPUnit_Framework_MockObject_MockObject $cookieMetadataMock */
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 (
491
+ MessagePlugin::MESSAGES_COOKIES_NAME ,
492
+ json_encode ($ expectedMessages ),
493
+ $ cookieMetadataMock
494
+ );
495
+ $ this ->cookieManagerMock ->expects ($ this ->once ())
496
+ ->method ('getCookie ' )
497
+ ->with (
498
+ MessagePlugin::MESSAGES_COOKIES_NAME
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
+
517
+ /** @var MessageInterface|\PHPUnit_Framework_MockObject_MockObject $messageMock */
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
+
533
+ /** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
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
+ }
453
548
}
0 commit comments