DeleteTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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\Model\Integration as IntegrationModel;
  9. use Magento\Framework\Exception\IntegrationException;
  10. class DeleteTest extends \Magento\Integration\Test\Unit\Controller\Adminhtml\IntegrationTest
  11. {
  12. /**
  13. * @var \Magento\Integration\Controller\Adminhtml\Integration\Delete
  14. */
  15. protected $integrationController;
  16. protected function setUp()
  17. {
  18. parent::setUp();
  19. $this->integrationController = $this->_createIntegrationController('Delete');
  20. $resultRedirect = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
  21. ->disableOriginalConstructor()
  22. ->getMock();
  23. $resultRedirect->expects($this->any())
  24. ->method('setPath')
  25. ->with('*/*/')
  26. ->willReturnSelf();
  27. $this->resultFactory->expects($this->any())
  28. ->method('create')
  29. ->with(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT)
  30. ->willReturn($resultRedirect);
  31. }
  32. public function testDeleteAction()
  33. {
  34. $intData = $this->_getSampleIntegrationData();
  35. $this->_requestMock->expects($this->once())
  36. ->method('getParam')
  37. ->willReturn(self::INTEGRATION_ID);
  38. $this->_integrationSvcMock->expects($this->any())
  39. ->method('get')
  40. ->with($this->anything())
  41. ->willReturn($intData);
  42. $this->_integrationSvcMock->expects($this->any())
  43. ->method('delete')
  44. ->with($this->anything())
  45. ->willReturn($intData);
  46. // Use real translate model
  47. $this->_translateModelMock = null;
  48. // verify success message
  49. $this->_messageManager->expects($this->once())
  50. ->method('addSuccess')
  51. ->with(__('The integration \'%1\' has been deleted.', $intData[Info::DATA_NAME]));
  52. $this->_escaper->expects($this->once())
  53. ->method('escapeHtml')
  54. ->willReturnArgument(0);
  55. $this->integrationController->execute();
  56. }
  57. public function testDeleteActionWithConsumer()
  58. {
  59. $intData = $this->_getSampleIntegrationData();
  60. $intData[Info::DATA_CONSUMER_ID] = 1;
  61. $this->_requestMock->expects($this->once())
  62. ->method('getParam')
  63. ->willReturn(self::INTEGRATION_ID);
  64. $this->_integrationSvcMock->expects($this->any())
  65. ->method('get')
  66. ->with($this->anything())
  67. ->willReturn($intData);
  68. $this->_integrationSvcMock->expects($this->once())
  69. ->method('delete')
  70. ->with($this->anything())
  71. ->willReturn($intData);
  72. $this->_oauthSvcMock->expects($this->once())
  73. ->method('deleteConsumer')
  74. ->with($this->anything())
  75. ->willReturn($intData);
  76. // Use real translate model
  77. $this->_translateModelMock = null;
  78. // verify success message
  79. $this->_messageManager->expects($this->once())
  80. ->method('addSuccess')
  81. ->with(__('The integration \'%1\' has been deleted.', $intData[Info::DATA_NAME]));
  82. $this->_escaper->expects($this->once())
  83. ->method('escapeHtml')
  84. ->willReturnArgument(0);
  85. $this->integrationController->execute();
  86. }
  87. public function testDeleteActionConfigSetUp()
  88. {
  89. $intData = $this->_getSampleIntegrationData();
  90. $intData[Info::DATA_SETUP_TYPE] = IntegrationModel::TYPE_CONFIG;
  91. $this->_requestMock->expects($this->once())
  92. ->method('getParam')
  93. ->willReturn(self::INTEGRATION_ID);
  94. $this->_integrationSvcMock->expects($this->any())
  95. ->method('get')
  96. ->with($this->anything())
  97. ->willReturn($intData);
  98. $this->_integrationHelperMock->expects($this->once())
  99. ->method('isConfigType')
  100. ->with($intData)
  101. ->willReturn(true);
  102. // verify error message
  103. $this->_messageManager->expects($this->once())
  104. ->method('addError')
  105. ->with(__('Uninstall the extension to remove integration \'%1\'.', $intData[Info::DATA_NAME]));
  106. $this->_integrationSvcMock->expects($this->never())->method('delete');
  107. // Use real translate model
  108. $this->_translateModelMock = null;
  109. // verify success message
  110. $this->_messageManager->expects($this->never())->method('addSuccess');
  111. $this->_escaper->expects($this->once())
  112. ->method('escapeHtml')
  113. ->willReturnArgument(0);
  114. $this->integrationController->execute();
  115. }
  116. public function testDeleteActionMissingId()
  117. {
  118. $this->_integrationSvcMock->expects($this->never())->method('get');
  119. $this->_integrationSvcMock->expects($this->never())->method('delete');
  120. // Use real translate model
  121. $this->_translateModelMock = null;
  122. // verify error message
  123. $this->_messageManager->expects($this->once())
  124. ->method('addError')
  125. ->with(__('Integration ID is not specified or is invalid.'));
  126. $this->integrationController->execute();
  127. }
  128. public function testDeleteActionForServiceIntegrationException()
  129. {
  130. $intData = $this->_getSampleIntegrationData();
  131. $this->_integrationSvcMock->expects($this->any())
  132. ->method('get')
  133. ->with($this->anything())
  134. ->willReturn($intData);
  135. $this->_requestMock->expects($this->once())
  136. ->method('getParam')
  137. ->willReturn(self::INTEGRATION_ID);
  138. // Use real translate model
  139. $this->_translateModelMock = null;
  140. $exceptionMessage = __('The integration with ID "%1" doesn\'t exist.', $intData[Info::DATA_ID]);
  141. $invalidIdException = new IntegrationException($exceptionMessage);
  142. $this->_integrationSvcMock->expects($this->once())
  143. ->method('delete')
  144. ->willThrowException($invalidIdException);
  145. $this->_messageManager->expects($this->once())->method('addError');
  146. $this->integrationController->execute();
  147. }
  148. public function testDeleteActionForServiceGenericException()
  149. {
  150. $intData = $this->_getSampleIntegrationData();
  151. $this->_integrationSvcMock->expects($this->any())
  152. ->method('get')
  153. ->with($this->anything())
  154. ->willReturn($intData);
  155. $this->_requestMock->expects($this->once())
  156. ->method('getParam')
  157. ->willReturn(self::INTEGRATION_ID);
  158. // Use real translate model
  159. $this->_translateModelMock = null;
  160. $exceptionMessage = __('The integration with ID "%1" doesn\'t exist.', $intData[Info::DATA_ID]);
  161. $invalidIdException = new \Exception($exceptionMessage);
  162. $this->_integrationSvcMock->expects($this->once())
  163. ->method('delete')
  164. ->willThrowException($invalidIdException);
  165. $this->_messageManager->expects($this->never())->method('addError');
  166. $this->integrationController->execute();
  167. }
  168. }