ManagerTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Webapi\Test\Unit\Model\Plugin;
  7. use Magento\Integration\Model\Integration;
  8. class ManagerTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * Integration service mock
  12. *
  13. * @var \Magento\Integration\Api\IntegrationServiceInterface|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $integrationServiceMock;
  16. /**
  17. * Authorization service mock
  18. *
  19. * @var \Magento\Integration\Api\AuthorizationServiceInterface|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $integrationAuthorizationServiceMock;
  22. /**
  23. * API setup plugin
  24. *
  25. * @var \Magento\Webapi\Model\Plugin\Manager
  26. */
  27. protected $apiSetupPlugin;
  28. /**
  29. * @var \Magento\Integration\Model\ConfigBasedIntegrationManager|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $subjectMock;
  32. /**
  33. * @var \Magento\Integration\Model\IntegrationConfig|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $integrationConfigMock;
  36. protected function setUp()
  37. {
  38. $this->integrationServiceMock = $this->getMockBuilder(
  39. \Magento\Integration\Api\IntegrationServiceInterface::class
  40. )->disableOriginalConstructor()->setMethods(
  41. [
  42. 'findByName',
  43. 'update',
  44. 'create',
  45. 'get',
  46. 'findByConsumerId',
  47. 'findActiveIntegrationByConsumerId',
  48. 'delete',
  49. 'getSelectedResources'
  50. ]
  51. )->getMock();
  52. $this->integrationAuthorizationServiceMock = $this->getMockBuilder(
  53. \Magento\Integration\Api\AuthorizationServiceInterface::class
  54. )->disableOriginalConstructor()->setMethods(
  55. [
  56. 'grantPermissions',
  57. 'grantAllPermissions',
  58. 'removePermissions'
  59. ]
  60. )->getMock();
  61. $this->subjectMock = $this->createMock(\Magento\Integration\Model\ConfigBasedIntegrationManager::class);
  62. $this->integrationConfigMock = $this->getMockBuilder(\Magento\Integration\Model\IntegrationConfig::class)
  63. ->disableOriginalConstructor()
  64. ->setMethods([])
  65. ->getMock();
  66. $this->apiSetupPlugin = new \Magento\Webapi\Model\Plugin\Manager(
  67. $this->integrationAuthorizationServiceMock,
  68. $this->integrationServiceMock,
  69. $this->integrationConfigMock
  70. );
  71. }
  72. public function testAfterProcessIntegrationConfigNoIntegrations()
  73. {
  74. $this->integrationConfigMock->expects($this->never())->method('getIntegrations');
  75. $this->integrationServiceMock->expects($this->never())->method('findByName');
  76. $this->apiSetupPlugin->afterProcessIntegrationConfig($this->subjectMock, []);
  77. }
  78. /**
  79. * @return void
  80. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  81. */
  82. public function testAfterProcessIntegrationConfigSuccess()
  83. {
  84. $testIntegration1Resource = [
  85. 'Magento_Customer::manage',
  86. 'Magento_Customer::online',
  87. 'Magento_Sales::create',
  88. 'Magento_SalesRule::quote',
  89. ];
  90. $testIntegration2Resource = ['Magento_Catalog::product_read'];
  91. $this->integrationConfigMock->expects(
  92. $this->once()
  93. )->method(
  94. 'getIntegrations'
  95. )->will(
  96. $this->returnValue(
  97. [
  98. 'TestIntegration1' => ['resource' => $testIntegration1Resource],
  99. 'TestIntegration2' => ['resource' => $testIntegration2Resource],
  100. ]
  101. )
  102. );
  103. $firstIntegrationId = 1;
  104. $integrationsData1 = new \Magento\Framework\DataObject(
  105. [
  106. 'id' => $firstIntegrationId,
  107. Integration::NAME => 'TestIntegration1',
  108. Integration::EMAIL => 'test-integration1@magento.com',
  109. Integration::ENDPOINT => 'http://endpoint.com',
  110. Integration::SETUP_TYPE => 1,
  111. ]
  112. );
  113. $secondIntegrationId = 2;
  114. $integrationsData2 = new \Magento\Framework\DataObject(
  115. [
  116. 'id' => $secondIntegrationId,
  117. Integration::NAME => 'TestIntegration2',
  118. Integration::EMAIL => 'test-integration2@magento.com',
  119. Integration::SETUP_TYPE => 1,
  120. ]
  121. );
  122. $this->integrationServiceMock->expects(
  123. $this->at(0)
  124. )->method(
  125. 'findByName'
  126. )->with(
  127. 'TestIntegration1'
  128. )->will(
  129. $this->returnValue($integrationsData1)
  130. );
  131. $this->integrationServiceMock->expects(
  132. $this->at(1)
  133. )->method(
  134. 'findByName'
  135. )->with(
  136. 'TestIntegration2'
  137. )->will(
  138. $this->returnValue($integrationsData2)
  139. );
  140. $this->apiSetupPlugin->afterProcessIntegrationConfig(
  141. $this->subjectMock,
  142. ['TestIntegration1', 'TestIntegration2']
  143. );
  144. }
  145. public function testAfterProcessConfigBasedIntegrationsNoIntegrations()
  146. {
  147. $this->integrationServiceMock->expects($this->never())->method('findByName');
  148. $this->apiSetupPlugin->afterProcessConfigBasedIntegrations($this->subjectMock, []);
  149. }
  150. /**
  151. * @return void
  152. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  153. */
  154. public function testAfterProcessConfigBasedIntegrationsSuccess()
  155. {
  156. $firstIntegrationId = 1;
  157. $integrationsData1 = [
  158. 'id' => $firstIntegrationId,
  159. Integration::NAME => 'TestIntegration1',
  160. Integration::EMAIL => 'test-integration1@magento.com',
  161. Integration::ENDPOINT => 'http://endpoint.com',
  162. Integration::SETUP_TYPE => 1,
  163. 'resource' => [
  164. 'Magento_Customer::manage',
  165. 'Magento_Customer::online',
  166. 'Magento_Sales::create',
  167. 'Magento_SalesRule::quote',
  168. ]
  169. ];
  170. $integrationsData1Object = new \Magento\Framework\DataObject($integrationsData1);
  171. $secondIntegrationId = 2;
  172. $integrationsData2 = [
  173. 'id' => $secondIntegrationId,
  174. Integration::NAME => 'TestIntegration2',
  175. Integration::EMAIL => 'test-integration2@magento.com',
  176. Integration::SETUP_TYPE => 1,
  177. 'resource' => ['Magento_Catalog::product_read']
  178. ];
  179. $integrationsData2Object = new \Magento\Framework\DataObject($integrationsData2);
  180. $this->integrationServiceMock->expects(
  181. $this->at(0)
  182. )->method(
  183. 'findByName'
  184. )->with(
  185. 'TestIntegration1'
  186. )->will(
  187. $this->returnValue($integrationsData1Object)
  188. );
  189. $this->integrationServiceMock->expects(
  190. $this->at(1)
  191. )->method(
  192. 'findByName'
  193. )->with(
  194. 'TestIntegration2'
  195. )->will(
  196. $this->returnValue($integrationsData2Object)
  197. );
  198. $this->apiSetupPlugin->afterProcessConfigBasedIntegrations(
  199. $this->subjectMock,
  200. ['TestIntegration1' => $integrationsData1, 'TestIntegration2' => $integrationsData2]
  201. );
  202. }
  203. }