42 private $serializerMock;
45 private $inlineTranslateMock;
49 $this->cookieManagerMock = $this->getMockBuilder(CookieManagerInterface::class)
50 ->getMockForAbstractClass();
51 $this->cookieMetadataFactoryMock = $this->getMockBuilder(CookieMetadataFactory::class)
52 ->disableOriginalConstructor()
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)
60 $this->inlineTranslateMock = $this->getMockBuilder(InlineInterface::class)->getMockForAbstractClass();
63 $this->cookieManagerMock,
64 $this->cookieMetadataFactoryMock,
66 $this->interpretationStrategyMock,
67 $this->serializerMock,
68 $this->inlineTranslateMock
72 public function testAfterRenderResultJson()
75 $resultMock = $this->getMockBuilder(Json::class)
76 ->disableOriginalConstructor()
79 $this->cookieManagerMock->expects($this->never())
80 ->method(
'setPublicCookie');
82 $this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
85 public function testAfterRenderResult()
89 'type' =>
'message0type',
90 'text' =>
'message0text',
93 $messageType =
'message1type';
94 $messageText =
'message1text';
97 'type' => $messageType,
98 'text' => $messageText,
101 $messages = array_merge($existingMessages, $messages);
104 $resultMock = $this->getMockBuilder(Redirect::class)
105 ->disableOriginalConstructor()
109 $cookieMetadataMock = $this->getMockBuilder(PublicCookieMetadata::class)
110 ->disableOriginalConstructor()
113 $this->cookieMetadataFactoryMock->expects($this->once())
114 ->method(
'createPublicCookieMetadata')
115 ->willReturn($cookieMetadataMock);
117 $this->cookieManagerMock->expects($this->once())
118 ->method(
'setPublicCookie')
121 json_encode($messages),
124 $this->cookieManagerMock->expects($this->once())
125 ->method(
'getCookie')
129 ->willReturn(json_encode($existingMessages));
131 $this->serializerMock->expects($this->once())
132 ->method(
'unserialize')
133 ->willReturnCallback(
135 return json_decode(
$data,
true);
138 $this->serializerMock->expects($this->once())
139 ->method(
'serialize')
140 ->willReturnCallback(
142 return json_encode(
$data);
147 $messageMock = $this->getMockBuilder(MessageInterface::class)
149 $messageMock->expects($this->once())
151 ->willReturn($messageType);
153 $this->interpretationStrategyMock->expects($this->once())
154 ->method(
'interpret')
156 ->willReturn($messageText);
159 $collectionMock = $this->getMockBuilder(Collection::class)
160 ->disableOriginalConstructor()
162 $collectionMock->expects($this->once())
164 ->willReturn([$messageMock]);
166 $this->managerMock->expects($this->once())
167 ->method(
'getMessages')
169 ->willReturn($collectionMock);
171 $this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
174 public function testAfterRenderResultWithNoMessages()
177 $resultMock = $this->getMockBuilder(Redirect::class)
178 ->disableOriginalConstructor()
181 $this->cookieManagerMock->expects($this->once())
182 ->method(
'getCookie')
186 ->willReturn(json_encode([]));
188 $this->serializerMock->expects($this->once())
189 ->method(
'unserialize')
190 ->willReturnCallback(
192 return json_decode(
$data,
true);
195 $this->serializerMock->expects($this->never())
196 ->method(
'serialize');
199 $collectionMock = $this->getMockBuilder(Collection::class)
200 ->disableOriginalConstructor()
202 $collectionMock->expects($this->once())
206 $this->managerMock->expects($this->once())
207 ->method(
'getMessages')
209 ->willReturn($collectionMock);
211 $this->cookieMetadataFactoryMock->expects($this->never())
212 ->method(
'createPublicCookieMetadata')
215 $this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
218 public function testAfterRenderResultWithoutExisting()
220 $messageType =
'message1type';
221 $messageText =
'message1text';
224 'type' => $messageType,
225 'text' => $messageText,
230 $resultMock = $this->getMockBuilder(Redirect::class)
231 ->disableOriginalConstructor()
235 $cookieMetadataMock = $this->getMockBuilder(PublicCookieMetadata::class)
236 ->disableOriginalConstructor()
239 $this->cookieMetadataFactoryMock->expects($this->once())
240 ->method(
'createPublicCookieMetadata')
241 ->willReturn($cookieMetadataMock);
243 $this->cookieManagerMock->expects($this->once())
244 ->method(
'setPublicCookie')
247 json_encode($messages),
250 $this->cookieManagerMock->expects($this->once())
251 ->method(
'getCookie')
255 ->willReturn(json_encode([]));
257 $this->serializerMock->expects($this->once())
258 ->method(
'unserialize')
259 ->willReturnCallback(
261 return json_decode(
$data,
true);
264 $this->serializerMock->expects($this->once())
265 ->method(
'serialize')
266 ->willReturnCallback(
268 return json_encode(
$data);
273 $messageMock = $this->getMockBuilder(MessageInterface::class)
275 $messageMock->expects($this->once())
277 ->willReturn($messageType);
279 $this->interpretationStrategyMock->expects($this->once())
280 ->method(
'interpret')
282 ->willReturn($messageText);
285 $collectionMock = $this->getMockBuilder(Collection::class)
286 ->disableOriginalConstructor()
288 $collectionMock->expects($this->once())
290 ->willReturn([$messageMock]);
292 $this->managerMock->expects($this->once())
293 ->method(
'getMessages')
295 ->willReturn($collectionMock);
297 $this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
300 public function testAfterRenderResultWithWrongJson()
302 $messageType =
'message1type';
303 $messageText =
'message1text';
306 'type' => $messageType,
307 'text' => $messageText,
312 $resultMock = $this->getMockBuilder(Redirect::class)
313 ->disableOriginalConstructor()
317 $cookieMetadataMock = $this->getMockBuilder(PublicCookieMetadata::class)
318 ->disableOriginalConstructor()
321 $this->cookieMetadataFactoryMock->expects($this->once())
322 ->method(
'createPublicCookieMetadata')
323 ->willReturn($cookieMetadataMock);
325 $this->cookieManagerMock->expects($this->once())
326 ->method(
'setPublicCookie')
329 json_encode($messages),
332 $this->cookieManagerMock->expects($this->once())
333 ->method(
'getCookie')
339 $this->serializerMock->expects($this->never())
340 ->method(
'unserialize');
342 $this->serializerMock->expects($this->once())
343 ->method(
'serialize')
344 ->willReturnCallback(
346 return json_encode(
$data);
351 $messageMock = $this->getMockBuilder(MessageInterface::class)
353 $messageMock->expects($this->once())
355 ->willReturn($messageType);
357 $this->interpretationStrategyMock->expects($this->once())
358 ->method(
'interpret')
360 ->willReturn($messageText);
363 $collectionMock = $this->getMockBuilder(Collection::class)
364 ->disableOriginalConstructor()
366 $collectionMock->expects($this->once())
368 ->willReturn([$messageMock]);
370 $this->managerMock->expects($this->once())
371 ->method(
'getMessages')
373 ->willReturn($collectionMock);
375 $this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
378 public function testAfterRenderResultWithWrongArray()
380 $messageType =
'message1type';
381 $messageText =
'message1text';
384 'type' => $messageType,
385 'text' => $messageText,
390 $resultMock = $this->getMockBuilder(Redirect::class)
391 ->disableOriginalConstructor()
395 $cookieMetadataMock = $this->getMockBuilder(PublicCookieMetadata::class)
396 ->disableOriginalConstructor()
399 $this->cookieMetadataFactoryMock->expects($this->once())
400 ->method(
'createPublicCookieMetadata')
401 ->willReturn($cookieMetadataMock);
403 $this->cookieManagerMock->expects($this->once())
404 ->method(
'setPublicCookie')
407 json_encode($messages),
410 $this->cookieManagerMock->expects($this->once())
411 ->method(
'getCookie')
415 ->willReturn(json_encode(
'string'));
417 $this->serializerMock->expects($this->once())
418 ->method(
'unserialize')
419 ->willReturnCallback(
421 return json_decode(
$data,
true);
424 $this->serializerMock->expects($this->once())
425 ->method(
'serialize')
426 ->willReturnCallback(
428 return json_encode(
$data);
433 $messageMock = $this->getMockBuilder(MessageInterface::class)
435 $messageMock->expects($this->once())
437 ->willReturn($messageType);
439 $this->interpretationStrategyMock->expects($this->once())
440 ->method(
'interpret')
442 ->willReturn($messageText);
445 $collectionMock = $this->getMockBuilder(Collection::class)
446 ->disableOriginalConstructor()
448 $collectionMock->expects($this->once())
450 ->willReturn([$messageMock]);
452 $this->managerMock->expects($this->once())
453 ->method(
'getMessages')
455 ->willReturn($collectionMock);
457 $this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
463 public function testAfterRenderResultWithAllowedInlineTranslate(): void
465 $messageType =
'message1type';
466 $messageText =
'{{{message1text}}{{message1text}}{{message1text}}{{theme/luma}}}';
467 $expectedMessages = [
469 'type' => $messageType,
470 'text' =>
'message1text',
475 $resultMock = $this->getMockBuilder(Redirect::class)
476 ->disableOriginalConstructor()
480 $cookieMetadataMock = $this->getMockBuilder(PublicCookieMetadata::class)
481 ->disableOriginalConstructor()
484 $this->cookieMetadataFactoryMock->expects($this->once())
485 ->method(
'createPublicCookieMetadata')
486 ->willReturn($cookieMetadataMock);
488 $this->cookieManagerMock->expects($this->once())
489 ->method(
'setPublicCookie')
492 json_encode($expectedMessages),
495 $this->cookieManagerMock->expects($this->once())
496 ->method(
'getCookie')
500 ->willReturn(json_encode([]));
502 $this->serializerMock->expects($this->once())
503 ->method(
'unserialize')
504 ->willReturnCallback(
506 return json_decode(
$data,
true);
509 $this->serializerMock->expects($this->once())
510 ->method(
'serialize')
511 ->willReturnCallback(
513 return json_encode(
$data);
518 $messageMock = $this->getMockBuilder(MessageInterface::class)
520 $messageMock->expects($this->once())
522 ->willReturn($messageType);
524 $this->interpretationStrategyMock->expects($this->once())
525 ->method(
'interpret')
527 ->willReturn($messageText);
529 $this->inlineTranslateMock->expects($this->once())
530 ->method(
'isAllowed')
534 $collectionMock = $this->getMockBuilder(Collection::class)
535 ->disableOriginalConstructor()
537 $collectionMock->expects($this->once())
539 ->willReturn([$messageMock]);
541 $this->managerMock->expects($this->once())
542 ->method(
'getMessages')
544 ->willReturn($collectionMock);
546 $this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
$interpretationStrategyMock
const MESSAGES_COOKIES_NAME
$cookieMetadataFactoryMock