PreviewTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Newsletter\Test\Unit\Block\Adminhtml\Template;
  7. use Magento\Framework\App\TemplateTypesInterface;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. class PreviewTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /** @var \Magento\Newsletter\Block\Adminhtml\Template\Preview */
  12. protected $preview;
  13. /** @var ObjectManagerHelper */
  14. protected $objectManagerHelper;
  15. /** @var \Magento\Newsletter\Model\Template|\PHPUnit_Framework_MockObject_MockObject */
  16. protected $template;
  17. /** @var \Magento\Newsletter\Model\SubscriberFactory|\PHPUnit_Framework_MockObject_MockObject */
  18. protected $subscriberFactory;
  19. /** @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject */
  20. protected $appState;
  21. /** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $storeManager;
  23. /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  24. protected $request;
  25. protected function setUp()
  26. {
  27. $this->request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  28. $this->appState = $this->createMock(\Magento\Framework\App\State::class);
  29. $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  30. $this->template = $this->createPartialMock(\Magento\Newsletter\Model\Template::class, [
  31. 'setTemplateType',
  32. 'setTemplateText',
  33. 'setTemplateStyles',
  34. 'isPlain',
  35. 'emulateDesign',
  36. 'revertDesign',
  37. 'getProcessedTemplate',
  38. 'load'
  39. ]);
  40. $templateFactory = $this->createPartialMock(\Magento\Newsletter\Model\TemplateFactory::class, ['create']);
  41. $templateFactory->expects($this->once())->method('create')->willReturn($this->template);
  42. $this->subscriberFactory = $this->createPartialMock(
  43. \Magento\Newsletter\Model\SubscriberFactory::class,
  44. ['create']
  45. );
  46. $this->objectManagerHelper = new ObjectManagerHelper($this);
  47. $this->preview = $this->objectManagerHelper->getObject(
  48. \Magento\Newsletter\Block\Adminhtml\Template\Preview::class,
  49. [
  50. 'appState' => $this->appState,
  51. 'storeManager' => $this->storeManager,
  52. 'request' => $this->request,
  53. 'templateFactory' => $templateFactory,
  54. 'subscriberFactory' => $this->subscriberFactory
  55. ]
  56. );
  57. }
  58. public function testToHtml()
  59. {
  60. $this->request->expects($this->any())->method('getParam')->willReturnMap(
  61. [
  62. ['id', null, 1],
  63. ['store', null, 1]
  64. ]
  65. );
  66. $this->template->expects($this->atLeastOnce())->method('emulateDesign')->with(1);
  67. $this->template->expects($this->atLeastOnce())->method('revertDesign');
  68. $this->appState->expects($this->atLeastOnce())->method('emulateAreaCode')
  69. ->with(
  70. \Magento\Newsletter\Model\Template::DEFAULT_DESIGN_AREA,
  71. [$this->template, 'getProcessedTemplate'],
  72. [['subscriber' => null]]
  73. )
  74. ->willReturn('Processed Template');
  75. $this->assertEquals('Processed Template', $this->preview->toHtml());
  76. }
  77. public function testToHtmlForNewTemplate()
  78. {
  79. $this->request->expects($this->any())->method('getParam')->willReturnMap(
  80. [
  81. ['type', null, TemplateTypesInterface::TYPE_TEXT],
  82. ['text', null, 'Processed Template'],
  83. ['styles', null, '.class-name{color:red;}']
  84. ]
  85. );
  86. $this->template->expects($this->once())->method('setTemplateType')->with(TemplateTypesInterface::TYPE_TEXT)
  87. ->willReturnSelf();
  88. $this->template->expects($this->once())->method('setTemplateText')->with('Processed Template')
  89. ->willReturnSelf();
  90. $this->template->expects($this->once())->method('setTemplateStyles')->with('.class-name{color:red;}')
  91. ->willReturnSelf();
  92. $this->template->expects($this->atLeastOnce())->method('isPlain')->willReturn(true);
  93. $this->template->expects($this->atLeastOnce())->method('emulateDesign')->with(1);
  94. $this->template->expects($this->atLeastOnce())->method('revertDesign');
  95. $store = $this->createMock(\Magento\Store\Model\Store::class);
  96. $store->expects($this->atLeastOnce())->method('getId')->willReturn(1);
  97. $this->storeManager->expects($this->atLeastOnce())->method('getStores')->willReturn([$store]);
  98. $this->appState->expects($this->atLeastOnce())->method('emulateAreaCode')
  99. ->with(
  100. \Magento\Newsletter\Model\Template::DEFAULT_DESIGN_AREA,
  101. [
  102. $this->template,
  103. 'getProcessedTemplate'
  104. ],
  105. [
  106. [
  107. 'subscriber' => null
  108. ]
  109. ]
  110. )
  111. ->willReturn('Processed Template');
  112. $this->assertEquals('<pre>Processed Template</pre>', $this->preview->toHtml());
  113. }
  114. public function testToHtmlWithSubscriber()
  115. {
  116. $this->request->expects($this->any())->method('getParam')->willReturnMap(
  117. [
  118. ['id', null, 2],
  119. ['store', null, 1],
  120. ['subscriber', null, 3]
  121. ]
  122. );
  123. $subscriber = $this->createMock(\Magento\Newsletter\Model\Subscriber::class);
  124. $subscriber->expects($this->atLeastOnce())->method('load')->with(3)->willReturnSelf();
  125. $this->subscriberFactory->expects($this->atLeastOnce())->method('create')->willReturn($subscriber);
  126. $this->template->expects($this->atLeastOnce())->method('emulateDesign')->with(1);
  127. $this->template->expects($this->atLeastOnce())->method('revertDesign');
  128. $this->appState->expects($this->atLeastOnce())->method('emulateAreaCode')
  129. ->with(
  130. \Magento\Newsletter\Model\Template::DEFAULT_DESIGN_AREA,
  131. [
  132. $this->template,
  133. 'getProcessedTemplate'
  134. ],
  135. [
  136. [
  137. 'subscriber' => $subscriber
  138. ]
  139. ]
  140. )
  141. ->willReturn('Processed Template');
  142. $this->assertEquals('Processed Template', $this->preview->toHtml());
  143. }
  144. }