SaveTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Test\Unit\Controller\Adminhtml\Integration;
  7. use Magento\Integration\Block\Adminhtml\Integration\Edit\Tab\Info;
  8. use Magento\Integration\Controller\Adminhtml\Integration as IntegrationController;
  9. use Magento\Integration\Model\Integration as IntegrationModel;
  10. use Magento\Framework\Exception\IntegrationException;
  11. use Magento\Framework\Exception\State\UserLockedException;
  12. use Magento\Framework\Exception\AuthenticationException;
  13. class SaveTest extends \Magento\Integration\Test\Unit\Controller\Adminhtml\IntegrationTest
  14. {
  15. public function testSaveAction()
  16. {
  17. // Use real translate model
  18. $this->_translateModelMock = null;
  19. $this->_requestMock->expects($this->any())
  20. ->method('getPostValue')
  21. ->will($this->returnValue([IntegrationController::PARAM_INTEGRATION_ID => self::INTEGRATION_ID]));
  22. $this->_requestMock->expects($this->any())->method('getParam')->will($this->returnValue(self::INTEGRATION_ID));
  23. $intData = $this->_getSampleIntegrationData();
  24. $this->_integrationSvcMock->expects($this->any())
  25. ->method('get')
  26. ->with(self::INTEGRATION_ID)
  27. ->will($this->returnValue($intData));
  28. $this->_integrationSvcMock->expects($this->any())
  29. ->method('update')
  30. ->with($this->anything())
  31. ->will($this->returnValue($intData));
  32. // verify success message
  33. $this->_messageManager->expects($this->once())
  34. ->method('addSuccess')
  35. ->with(__('The integration \'%1\' has been saved.', $intData[Info::DATA_NAME]));
  36. $this->_escaper->expects($this->once())
  37. ->method('escapeHtml')
  38. ->willReturnArgument(0);
  39. $integrationContr = $this->_createIntegrationController('Save');
  40. $integrationContr->execute();
  41. }
  42. public function testSaveActionException()
  43. {
  44. $this->_requestMock->expects($this->any())->method('getParam')->will($this->returnValue(self::INTEGRATION_ID));
  45. // Have integration service throw an exception to test exception path
  46. $exceptionMessage = 'Internal error. Check exception log for details.';
  47. $this->_integrationSvcMock->expects($this->any())
  48. ->method('get')
  49. ->with(self::INTEGRATION_ID)
  50. ->will($this->throwException(new \Magento\Framework\Exception\LocalizedException(__($exceptionMessage))));
  51. // Verify error
  52. $this->_messageManager->expects($this->once())->method('addError')->with($this->equalTo($exceptionMessage));
  53. $integrationContr = $this->_createIntegrationController('Save');
  54. $integrationContr->execute();
  55. }
  56. public function testSaveActionIntegrationException()
  57. {
  58. $this->_requestMock->expects($this->any())->method('getParam')->will($this->returnValue(self::INTEGRATION_ID));
  59. // Have integration service throw an exception to test exception path
  60. $exceptionMessage = 'Internal error. Check exception log for details.';
  61. $this->_integrationSvcMock->expects(
  62. $this->any()
  63. )->method(
  64. 'get'
  65. )->with(
  66. self::INTEGRATION_ID
  67. )->will(
  68. $this->throwException(new IntegrationException(__($exceptionMessage)))
  69. );
  70. $this->_escaper->expects($this->once())
  71. ->method('escapeHtml')
  72. ->willReturnArgument(0);
  73. // Verify error
  74. $this->_messageManager->expects($this->once())->method('addError')->with($this->equalTo($exceptionMessage));
  75. $integrationContr = $this->_createIntegrationController('Save');
  76. $integrationContr->execute();
  77. }
  78. public function testSaveActionNew()
  79. {
  80. $integration = $this->_getSampleIntegrationData();
  81. //No id when New Integration is Post-ed
  82. $integration->unsetData([IntegrationModel::ID, 'id']);
  83. $this->_requestMock->expects(
  84. $this->any()
  85. )->method(
  86. 'getPostValue'
  87. )->will(
  88. $this->returnValue($integration->getData())
  89. );
  90. $integration->setData('id', self::INTEGRATION_ID);
  91. $this->_integrationSvcMock->expects(
  92. $this->any()
  93. )->method(
  94. 'create'
  95. )->with(
  96. $this->anything()
  97. )->will(
  98. $this->returnValue($integration)
  99. );
  100. $this->_integrationSvcMock->expects(
  101. $this->any()
  102. )->method(
  103. 'get'
  104. )->with(
  105. self::INTEGRATION_ID
  106. )->will(
  107. $this->returnValue(null)
  108. );
  109. // Use real translate model
  110. $this->_translateModelMock = null;
  111. // verify success message
  112. $this->_messageManager->expects(
  113. $this->once()
  114. )->method(
  115. 'addSuccess'
  116. )->with(
  117. __('The integration \'%1\' has been saved.', $integration->getName())
  118. );
  119. $this->_escaper->expects($this->once())
  120. ->method('escapeHtml')
  121. ->willReturnArgument(0);
  122. $integrationContr = $this->_createIntegrationController('Save');
  123. $integrationContr->execute();
  124. }
  125. public function testSaveActionExceptionDuringServiceCreation()
  126. {
  127. $exceptionMessage = 'Service could not be saved.';
  128. $integration = $this->_getSampleIntegrationData();
  129. // No id when New Integration is Post-ed
  130. $integration->unsetData([IntegrationModel::ID, 'id']);
  131. $this->_requestMock->expects(
  132. $this->any()
  133. )->method(
  134. 'getPostValue'
  135. )->will(
  136. $this->returnValue($integration->getData())
  137. );
  138. $integration->setData('id', self::INTEGRATION_ID);
  139. $this->_integrationSvcMock->expects(
  140. $this->any()
  141. )->method(
  142. 'create'
  143. )->with(
  144. $this->anything()
  145. )->will(
  146. $this->throwException(new IntegrationException(__($exceptionMessage)))
  147. );
  148. $this->_integrationSvcMock->expects(
  149. $this->any()
  150. )->method(
  151. 'get'
  152. )->with(
  153. self::INTEGRATION_ID
  154. )->will(
  155. $this->returnValue(null)
  156. );
  157. $this->_escaper->expects($this->once())
  158. ->method('escapeHtml')
  159. ->willReturnArgument(0);
  160. // Use real translate model
  161. $this->_translateModelMock = null;
  162. // Verify success message
  163. $this->_messageManager->expects($this->once())->method('addError')->with($exceptionMessage);
  164. $integrationController = $this->_createIntegrationController('Save');
  165. $integrationController->execute();
  166. }
  167. public function testSaveActionExceptionOnIntegrationsCreatedFromConfigFile()
  168. {
  169. $exceptionMessage = "The integrations created in the config file can't be edited.";
  170. $intData = new \Magento\Framework\DataObject(
  171. [
  172. Info::DATA_NAME => 'nameTest',
  173. Info::DATA_ID => self::INTEGRATION_ID,
  174. 'id' => self::INTEGRATION_ID,
  175. Info::DATA_EMAIL => 'test@magento.com',
  176. Info::DATA_ENDPOINT => 'http://magento.ll/endpoint',
  177. Info::DATA_SETUP_TYPE => IntegrationModel::TYPE_CONFIG,
  178. ]
  179. );
  180. $this->_requestMock->expects($this->any())->method('getParam')->will($this->returnValue(self::INTEGRATION_ID));
  181. $this->_integrationSvcMock
  182. ->expects($this->once())
  183. ->method('get')
  184. ->with(self::INTEGRATION_ID)
  185. ->will($this->returnValue($intData));
  186. $this->_escaper->expects($this->once())
  187. ->method('escapeHtml')
  188. ->willReturnArgument(0);
  189. // Verify error
  190. $this->_messageManager->expects($this->once())->method('addError')->with($this->equalTo($exceptionMessage));
  191. $integrationContr = $this->_createIntegrationController('Save');
  192. $integrationContr->execute();
  193. }
  194. /**
  195. * @return void
  196. */
  197. public function testSaveActionUserLockedException()
  198. {
  199. $exceptionMessage = __('Your account is temporarily disabled. Please try again later.');
  200. $passwordString = '1234567';
  201. $this->_requestMock->expects($this->exactly(2))
  202. ->method('getParam')
  203. ->withConsecutive(
  204. [\Magento\Integration\Controller\Adminhtml\Integration\Save::PARAM_INTEGRATION_ID],
  205. [\Magento\Integration\Block\Adminhtml\Integration\Edit\Tab\Info::DATA_CONSUMER_PASSWORD]
  206. )
  207. ->willReturnOnConsecutiveCalls(self::INTEGRATION_ID, $passwordString);
  208. $intData = $this->_getSampleIntegrationData();
  209. $this->_integrationSvcMock->expects($this->once())
  210. ->method('get')
  211. ->with(self::INTEGRATION_ID)
  212. ->willReturn($intData);
  213. $this->_userMock->expects($this->any())
  214. ->method('performIdentityCheck')
  215. ->with($passwordString)
  216. ->will($this->throwException(new UserLockedException(__($exceptionMessage))));
  217. $this->_authMock->expects($this->once())
  218. ->method('logout');
  219. $this->securityCookieMock->expects($this->once())
  220. ->method('setLogoutReasonCookie')
  221. ->with(\Magento\Security\Model\AdminSessionsManager::LOGOUT_REASON_USER_LOCKED);
  222. $integrationContr = $this->_createIntegrationController('Save');
  223. $integrationContr->execute();
  224. }
  225. /**
  226. * @return void
  227. */
  228. public function testSaveActionAuthenticationException()
  229. {
  230. $passwordString = '1234567';
  231. $exceptionMessage =
  232. __('The password entered for the current user is invalid. Verify the password and try again.');
  233. $this->_requestMock->expects($this->any())
  234. ->method('getParam')
  235. ->withConsecutive(
  236. [\Magento\Integration\Controller\Adminhtml\Integration\Save::PARAM_INTEGRATION_ID],
  237. [\Magento\Integration\Block\Adminhtml\Integration\Edit\Tab\Info::DATA_CONSUMER_PASSWORD]
  238. )
  239. ->willReturnOnConsecutiveCalls(self::INTEGRATION_ID, $passwordString);
  240. $intData = $this->_getSampleIntegrationData();
  241. $this->_integrationSvcMock->expects($this->once())
  242. ->method('get')
  243. ->with(self::INTEGRATION_ID)
  244. ->willReturn($intData);
  245. $this->_userMock->expects($this->any())
  246. ->method('performIdentityCheck')
  247. ->with($passwordString)
  248. ->will($this->throwException(new AuthenticationException(__($exceptionMessage))));
  249. // Verify error
  250. $this->_messageManager->expects($this->once())->method('addError')->with($this->equalTo($exceptionMessage));
  251. $integrationContr = $this->_createIntegrationController('Save');
  252. $integrationContr->execute();
  253. }
  254. }