LoadOptionsTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Widget\Test\Unit\Controller\Adminhtml\Widget;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. use Magento\Widget\Controller\Adminhtml\Widget\LoadOptions;
  9. use Magento\Backend\App\Action\Context;
  10. use Magento\Framework\App\ViewInterface;
  11. use Magento\Widget\Helper\Conditions as ConditionsHelper;
  12. use Magento\Framework\Exception\LocalizedException;
  13. use Magento\Framework\App\ResponseInterface;
  14. use Magento\Framework\ObjectManagerInterface;
  15. use Magento\Framework\App\RequestInterface;
  16. /**
  17. * Test class for \Magento\Widget\Controller\Adminhtml\Widget\LoadOptions
  18. */
  19. class LoadOptionsTest extends \PHPUnit\Framework\TestCase
  20. {
  21. /**
  22. * @var ObjectManagerHelper
  23. */
  24. private $objectManagerHelper;
  25. /**
  26. * @var Context|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $contextMock;
  29. /**
  30. * @var ViewInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $viewMock;
  33. /**
  34. * @var ConditionsHelper|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $conditionsHelperMock;
  37. /**
  38. * @var ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $responseMock;
  41. /**
  42. * @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $objectManagerMock;
  45. /**
  46. * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. private $requestMock;
  49. /**
  50. * @var LoadOptions
  51. */
  52. private $loadOptions;
  53. /**
  54. * return void
  55. */
  56. protected function setUp()
  57. {
  58. $this->objectManagerHelper = new ObjectManagerHelper($this);
  59. $this->objectManagerMock = $this->getMockForAbstractClass(ObjectManagerInterface::class);
  60. $this->viewMock = $this->getMockForAbstractClass(ViewInterface::class);
  61. $this->requestMock = $this->getMockForAbstractClass(RequestInterface::class);
  62. $this->responseMock = $this->getMockBuilder(ResponseInterface::class)
  63. ->setMethods(['representJson'])
  64. ->getMockForAbstractClass();
  65. $this->contextMock = $this->getMockBuilder(Context::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->contextMock->expects($this->once())
  69. ->method('getView')
  70. ->willReturn($this->viewMock);
  71. $this->contextMock->expects($this->once())
  72. ->method('getRequest')
  73. ->willReturn($this->requestMock);
  74. $this->contextMock->expects($this->once())
  75. ->method('getResponse')
  76. ->willReturn($this->responseMock);
  77. $this->contextMock->expects($this->once())
  78. ->method('getObjectManager')
  79. ->willReturn($this->objectManagerMock);
  80. $this->conditionsHelperMock = $this->getMockBuilder(ConditionsHelper::class)
  81. ->disableOriginalConstructor()
  82. ->getMock();
  83. $this->loadOptions = $this->objectManagerHelper->getObject(
  84. LoadOptions::class,
  85. ['context' => $this->contextMock]
  86. );
  87. $this->objectManagerHelper->setBackwardCompatibleProperty(
  88. $this->loadOptions,
  89. 'conditionsHelper',
  90. $this->conditionsHelperMock
  91. );
  92. }
  93. /**
  94. * @return void
  95. */
  96. public function dtestExecuteWithException()
  97. {
  98. $jsonResult = '{"error":true,"message":"Some error"}';
  99. $errorMessage = 'Some error';
  100. /** @var \Magento\Framework\Json\Helper\Data|\PHPUnit_Framework_MockObject_MockObject $jsonDataHelperMock */
  101. $jsonDataHelperMock = $this->getMockBuilder(\Magento\Framework\Json\Helper\Data::class)
  102. ->disableOriginalConstructor()
  103. ->getMock();
  104. $jsonDataHelperMock->expects($this->once())
  105. ->method('jsonEncode')
  106. ->with(['error' => true, 'message' => $errorMessage])
  107. ->willReturn($jsonResult);
  108. $this->viewMock->expects($this->once())
  109. ->method('loadLayout')
  110. ->willThrowException(new LocalizedException(__($errorMessage)));
  111. $this->objectManagerMock->expects($this->once())
  112. ->method('get')
  113. ->with(\Magento\Framework\Json\Helper\Data::class)
  114. ->willReturn($jsonDataHelperMock);
  115. $this->responseMock->expects($this->once())
  116. ->method('representJson')
  117. ->with($jsonResult)
  118. ->willReturnArgument(0);
  119. $this->loadOptions->execute();
  120. }
  121. /**
  122. * @return void
  123. */
  124. public function testExecute()
  125. {
  126. $widgetType = 'Magento\SomeWidget';
  127. $conditionsEncoded = 'encoded conditions';
  128. $conditionsDecoded = [
  129. 'value' => 1,
  130. 'operator' => '==',
  131. 'attribute' => 'id',
  132. ];
  133. $widgetJsonParams = '{"widget_type":"Magento\\Widget","values":{"title":"&quot;Test&quot;", "":}}';
  134. $widgetArrayParams = [
  135. 'widget_type' => $widgetType,
  136. 'values' => [
  137. 'title' => '&quot;Test&quot;',
  138. 'conditions_encoded' => $conditionsEncoded,
  139. ],
  140. ];
  141. $resultWidgetArrayParams = [
  142. 'widget_type' => $widgetType,
  143. 'values' => [
  144. 'title' => '"Test"',
  145. 'conditions_encoded' => $conditionsEncoded,
  146. 'conditions' => $conditionsDecoded,
  147. ],
  148. ];
  149. /** @var \Magento\Framework\Json\Helper\Data|\PHPUnit_Framework_MockObject_MockObject $jsonDataHelperMock */
  150. $jsonDataHelperMock = $this->getMockBuilder(\Magento\Framework\Json\Helper\Data::class)
  151. ->disableOriginalConstructor()
  152. ->getMock();
  153. $jsonDataHelperMock->expects($this->once())
  154. ->method('jsonDecode')
  155. ->with($widgetJsonParams)
  156. ->willReturn($widgetArrayParams);
  157. $this->viewMock->expects($this->once())
  158. ->method('loadLayout');
  159. $this->requestMock->expects($this->once())
  160. ->method('getParam')
  161. ->with('widget')
  162. ->willReturn($widgetJsonParams);
  163. $this->objectManagerMock->expects($this->once())
  164. ->method('get')
  165. ->with(\Magento\Framework\Json\Helper\Data::class)
  166. ->willReturn($jsonDataHelperMock);
  167. /** @var \Magento\Framework\View\Element\BlockInterface|\PHPUnit_Framework_MockObject_MockObject $blockMock */
  168. $blockMock = $this->getMockBuilder(\Magento\Framework\View\Element\BlockInterface::class)
  169. ->setMethods(['setWidgetType', 'setWidgetValues'])
  170. ->getMockForAbstractClass();
  171. $blockMock->expects($this->once())
  172. ->method('setWidgetType')
  173. ->with($widgetType)
  174. ->willReturnSelf();
  175. $blockMock->expects($this->once())
  176. ->method('setWidgetValues')
  177. ->with($resultWidgetArrayParams['values'])
  178. ->willReturnSelf();
  179. /** @var \Magento\Framework\View\LayoutInterface|\PHPUnit_Framework_MockObject_MockObject $layoutMock */
  180. $layoutMock = $this->getMockForAbstractClass(\Magento\Framework\View\LayoutInterface::class);
  181. $layoutMock->expects($this->once())
  182. ->method('getBlock')
  183. ->with('wysiwyg_widget.options')
  184. ->willReturn($blockMock);
  185. $this->conditionsHelperMock->expects($this->once())
  186. ->method('decode')
  187. ->with($conditionsEncoded)
  188. ->willReturn($conditionsDecoded);
  189. $this->viewMock->expects($this->once())
  190. ->method('getLayout')
  191. ->willReturn($layoutMock);
  192. $this->viewMock->expects($this->once())
  193. ->method('renderLayout');
  194. $this->loadOptions->execute();
  195. }
  196. }