ManagerTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Test\Unit\Model;
  7. use \Magento\Integration\Model\Integration;
  8. /**
  9. * Class to test Integration Manager
  10. */
  11. class ManagerTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * Integration service
  15. *
  16. * @var \Magento\Integration\Api\IntegrationServiceInterface
  17. */
  18. protected $integrationServiceMock;
  19. /**
  20. * @var \Magento\Authorization\Model\Acl\AclRetriever
  21. */
  22. protected $aclRetriever;
  23. /**
  24. * @var \Magento\Integration\Model\Config
  25. */
  26. protected $configMock;
  27. /**
  28. * Integration config
  29. *
  30. * @var \Magento\Integration\Model\ConfigBasedIntegrationManager
  31. */
  32. protected $integrationManager;
  33. protected function setUp()
  34. {
  35. $this->integrationServiceMock = $this->getMockBuilder(
  36. \Magento\Integration\Api\IntegrationServiceInterface::class
  37. )->disableOriginalConstructor()->setMethods(
  38. [
  39. 'findByName',
  40. 'update',
  41. 'create',
  42. 'get',
  43. 'findByConsumerId',
  44. 'findActiveIntegrationByConsumerId',
  45. 'delete',
  46. 'getSelectedResources'
  47. ]
  48. )->getMock();
  49. $this->aclRetriever = $this->getMockBuilder(\Magento\Authorization\Model\Acl\AclRetriever::class)
  50. ->disableOriginalConstructor()
  51. ->setMethods([])
  52. ->getMock();
  53. $this->configMock = $this->getMockBuilder(\Magento\Integration\Model\Config::class)
  54. ->disableOriginalConstructor()
  55. ->setMethods([])
  56. ->getMock();
  57. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  58. $this->integrationManager = $objectManagerHelper->getObject(
  59. \Magento\Integration\Model\ConfigBasedIntegrationManager::class,
  60. [
  61. 'integrationService' => $this->integrationServiceMock,
  62. 'aclRetriever' => $this->aclRetriever,
  63. 'integrationConfig' => $this->configMock
  64. ]
  65. );
  66. }
  67. public function tearDown()
  68. {
  69. unset($this->integrationServiceMock);
  70. unset($this->integrationManager);
  71. }
  72. public function testProcessIntegrationConfigNoIntegrations()
  73. {
  74. $this->configMock->expects($this->never())->method('getIntegrations');
  75. $this->integrationManager->processIntegrationConfig([]);
  76. }
  77. public function testProcessIntegrationConfigSuccess()
  78. {
  79. $this->configMock->expects(
  80. $this->once()
  81. )->method(
  82. 'getIntegrations'
  83. )->will(
  84. $this->returnValue(
  85. [
  86. 'TestIntegration1' => [
  87. 'email' => 'test-integration1@magento.com',
  88. 'endpoint_url' => 'http://endpoint.com',
  89. 'identity_link_url' => 'http://www.example.com/identity',
  90. ],
  91. 'TestIntegration2' => ['email' => 'test-integration2@magento.com'],
  92. ]
  93. )
  94. );
  95. $intLookupData1 = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
  96. ->disableOriginalConstructor()
  97. ->setMethods([])
  98. ->getMock();
  99. $intLookupData1->expects($this->any())->method('getId')->willReturn(1);
  100. $intLookupData2 = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
  101. ->disableOriginalConstructor()
  102. ->setMethods([])
  103. ->getMock();
  104. $intLookupData1->expects($this->any())->method('getId')->willReturn(false);
  105. $intUpdateData1 = [
  106. Integration::ID => 1,
  107. Integration::NAME => 'TestIntegration1',
  108. Integration::EMAIL => 'test-integration1@magento.com',
  109. Integration::ENDPOINT => 'http://endpoint.com',
  110. Integration::IDENTITY_LINK_URL => 'http://www.example.com/identity',
  111. Integration::SETUP_TYPE => 1,
  112. ];
  113. $integrationsData2 = [
  114. Integration::NAME => 'TestIntegration2',
  115. Integration::EMAIL => 'test-integration2@magento.com',
  116. Integration::SETUP_TYPE => 1,
  117. ];
  118. $this->integrationServiceMock->expects(
  119. $this->at(0)
  120. )->method(
  121. 'findByName'
  122. )->with(
  123. 'TestIntegration1'
  124. )->will(
  125. $this->returnValue($intLookupData1)
  126. );
  127. $this->integrationServiceMock->expects($this->once())->method('create')->with($integrationsData2);
  128. $this->integrationServiceMock->expects(
  129. $this->at(2)
  130. )->method(
  131. 'findByName'
  132. )->with(
  133. 'TestIntegration2'
  134. )->will(
  135. $this->returnValue($intLookupData2)
  136. );
  137. $this->integrationServiceMock->expects($this->at(1))->method('update')->with($intUpdateData1);
  138. $this->integrationManager->processIntegrationConfig(['TestIntegration1', 'TestIntegration2']);
  139. }
  140. public function testProcessConfigBasedIntegrationsRecreateUpdatedConfigAfterResourceChange()
  141. {
  142. $originalData = [
  143. Integration::ID => 1,
  144. Integration::NAME => 'TestIntegration1',
  145. Integration::EMAIL => 'test-integration1@magento.com',
  146. Integration::ENDPOINT => 'http://endpoint.com',
  147. Integration::IDENTITY_LINK_URL => 'http://www.example.com/identity',
  148. Integration::SETUP_TYPE => 1
  149. ];
  150. $integrations = [
  151. 'TestIntegration1' => [
  152. Integration::EMAIL => 'test-integration1@magento.com',
  153. Integration::ENDPOINT => 'http://endpoint.com',
  154. Integration::IDENTITY_LINK_URL => 'http://www.example.com/identity',
  155. 'resources' => [
  156. 'Magento_Customer::manage',
  157. 'Magento_Customer::customer'
  158. ]
  159. ]
  160. ];
  161. $originalResources = [
  162. 'Magento_Customer::manage'
  163. ];
  164. $newResources = [
  165. 'Magento_Customer::manage',
  166. 'Magento_Customer::customer'
  167. ];
  168. $integrationObject = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
  169. ->disableOriginalConstructor()
  170. ->setMethods([])
  171. ->getMock();
  172. // Integration already exists, so update with new data and recreate
  173. $this->integrationServiceMock->expects($this->at(0))->method('findByName')->with('TestIntegration1')->will(
  174. $this->returnValue($integrationObject)
  175. );
  176. $this->aclRetriever->expects($this->once())->method('getAllowedResourcesByUser')
  177. ->willReturn($originalResources);
  178. $integrationObject->expects($this->any())->method('getId')->willReturn($originalData[Integration::ID]);
  179. $this->integrationServiceMock->expects($this->once())->method('update')->willReturn($integrationObject);
  180. $integrationObject->expects($this->once())->method('getOrigData')->willReturn($originalData);
  181. $integrationObject->expects($this->once())->method('getData')->willReturn($newResources);
  182. $this->integrationServiceMock->expects($this->once())->method('create');
  183. $this->integrationManager->processConfigBasedIntegrations($integrations);
  184. }
  185. public function testProcessConfigBasedIntegrationsCreateNewIntegrations()
  186. {
  187. $integrations = [
  188. 'TestIntegration1' => [
  189. Integration::EMAIL => 'test-integration1@magento.com',
  190. Integration::ENDPOINT => 'http://endpoint.com',
  191. Integration::IDENTITY_LINK_URL => 'http://www.example.com/identity',
  192. 'resources' => [
  193. 'Magento_Customer::manage',
  194. 'Magento_Customer::customer'
  195. ]
  196. ],
  197. 'TestIntegration2' => [
  198. Integration::EMAIL => 'test-integration2@magento.com',
  199. Integration::ENDPOINT => 'http://endpoint.com',
  200. Integration::IDENTITY_LINK_URL => 'http://www.example.com/identity',
  201. ]
  202. ];
  203. $integrationObject = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
  204. ->disableOriginalConstructor()
  205. ->setMethods([])
  206. ->getMock();
  207. // Integration1 does not exist, so create it
  208. $this->integrationServiceMock->expects($this->at(0))->method('findByName')->with('TestIntegration1')->will(
  209. $this->returnValue($integrationObject)
  210. );
  211. $integrationObject->expects($this->any())->method('getId')->willReturn(false);
  212. $this->integrationServiceMock->expects($this->any())->method('create');
  213. // Integration2 does not exist, so create it
  214. $this->integrationServiceMock->expects($this->at(2))->method('findByName')->with('TestIntegration2')->will(
  215. $this->returnValue($integrationObject)
  216. );
  217. $this->integrationManager->processConfigBasedIntegrations($integrations);
  218. }
  219. }