IntegrationTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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\Plugin;
  7. use Magento\Integration\Model\Integration;
  8. /**
  9. * Unit test for \Magento\Integration\Model\Plugin\Integration
  10. */
  11. class IntegrationTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * API setup plugin
  15. *
  16. * @var \Magento\Integration\Model\Plugin\Integration
  17. */
  18. protected $integrationPlugin;
  19. /**
  20. * @var \Magento\Integration\Api\IntegrationServiceInterface|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $subjectMock;
  23. /**
  24. * @var \Magento\Authorization\Model\Acl\AclRetriever|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $aclRetrieverMock;
  27. /**
  28. * @var \Magento\Integration\Api\AuthorizationServiceInterface|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $integrationAuthServiceMock;
  31. /**
  32. * @var \Magento\Integration\Model\IntegrationConfig|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $integrationConfigMock;
  35. /**
  36. * @var \Magento\Integration\Model\ConsolidatedConfig|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $consolidatedConfigMock;
  39. protected function setUp()
  40. {
  41. $this->subjectMock = $this->createMock(\Magento\Integration\Model\IntegrationService::class);
  42. $this->integrationAuthServiceMock = $this->createPartialMock(
  43. \Magento\Integration\Api\AuthorizationServiceInterface::class,
  44. ['removePermissions', 'grantAllPermissions', 'grantPermissions']
  45. );
  46. $this->aclRetrieverMock = $this->createPartialMock(
  47. \Magento\Authorization\Model\Acl\AclRetriever::class,
  48. ['getAllowedResourcesByUser']
  49. );
  50. $this->integrationConfigMock = $this->getMockBuilder(\Magento\Integration\Model\IntegrationConfig::class)
  51. ->disableOriginalConstructor()
  52. ->setMethods([])
  53. ->getMock();
  54. $this->consolidatedConfigMock = $this->getMockBuilder(\Magento\Integration\Model\ConsolidatedConfig::class)
  55. ->disableOriginalConstructor()
  56. ->setMethods([])
  57. ->getMock();
  58. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  59. $this->integrationPlugin = $objectManagerHelper->getObject(
  60. \Magento\Integration\Model\Plugin\Integration::class,
  61. [
  62. 'integrationAuthorizationService' => $this->integrationAuthServiceMock,
  63. 'aclRetriever' => $this->aclRetrieverMock,
  64. 'integrationConfig' => $this->integrationConfigMock,
  65. 'consolidatedConfig' => $this->consolidatedConfigMock
  66. ]
  67. );
  68. }
  69. public function testAfterDelete()
  70. {
  71. $integrationId = 1;
  72. $integrationsData = [
  73. Integration::ID => $integrationId,
  74. Integration::NAME => 'TestIntegration1',
  75. Integration::EMAIL => 'test-integration1@magento.com',
  76. Integration::ENDPOINT => 'http://endpoint.com',
  77. Integration::SETUP_TYPE => 1,
  78. ];
  79. $this->integrationAuthServiceMock->expects($this->once())
  80. ->method('removePermissions')
  81. ->with($integrationId);
  82. $this->integrationPlugin->afterDelete($this->subjectMock, $integrationsData);
  83. }
  84. public function testAfterCreateAllResources()
  85. {
  86. $integrationId = 1;
  87. $integrationModelMock = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
  88. ->disableOriginalConstructor()
  89. ->getMock();
  90. $integrationModelMock->expects($this->exactly(2))
  91. ->method('getId')
  92. ->will($this->returnValue($integrationId));
  93. $integrationModelMock->expects($this->once())
  94. ->method('getData')
  95. ->with('all_resources')
  96. ->will($this->returnValue(1));
  97. $this->integrationAuthServiceMock->expects($this->once())
  98. ->method('grantAllPermissions')
  99. ->with($integrationId);
  100. $this->integrationPlugin->afterCreate($this->subjectMock, $integrationModelMock);
  101. }
  102. public function testAfterCreateSomeResources()
  103. {
  104. $integrationId = 1;
  105. $integrationModelMock = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
  106. ->disableOriginalConstructor()
  107. ->getMock();
  108. $integrationModelMock->expects($this->exactly(2))
  109. ->method('getId')
  110. ->will($this->returnValue($integrationId));
  111. $integrationModelMock->expects($this->at(2))
  112. ->method('getData')
  113. ->with('all_resources')
  114. ->will($this->returnValue(null));
  115. $integrationModelMock->expects($this->at(3))
  116. ->method('getData')
  117. ->with('resource')
  118. ->will($this->returnValue(['testResource']));
  119. $integrationModelMock->expects($this->at(5))
  120. ->method('getData')
  121. ->with('resource')
  122. ->will($this->returnValue(['testResource']));
  123. $this->integrationAuthServiceMock->expects($this->once())
  124. ->method('grantPermissions')
  125. ->with($integrationId, ['testResource']);
  126. $this->integrationPlugin->afterCreate($this->subjectMock, $integrationModelMock);
  127. }
  128. public function testAfterCreateNoResource()
  129. {
  130. $integrationId = 1;
  131. $integrationModelMock = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
  132. ->disableOriginalConstructor()
  133. ->getMock();
  134. $integrationModelMock->expects($this->exactly(2))
  135. ->method('getId')
  136. ->will($this->returnValue($integrationId));
  137. $integrationModelMock->expects($this->at(2))
  138. ->method('getData')
  139. ->with('all_resources')
  140. ->will($this->returnValue(null));
  141. $integrationModelMock->expects($this->at(3))
  142. ->method('getData')
  143. ->with('resource')
  144. ->will($this->returnValue(null));
  145. $this->integrationAuthServiceMock->expects($this->once())
  146. ->method('grantPermissions')
  147. ->with($integrationId, []);
  148. $this->integrationPlugin->afterCreate($this->subjectMock, $integrationModelMock);
  149. }
  150. public function testAfterUpdateAllResources()
  151. {
  152. $integrationId = 1;
  153. $integrationModelMock = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
  154. ->disableOriginalConstructor()
  155. ->getMock();
  156. $integrationModelMock->expects($this->exactly(2))
  157. ->method('getId')
  158. ->will($this->returnValue($integrationId));
  159. $integrationModelMock->expects($this->once())
  160. ->method('getData')
  161. ->with('all_resources')
  162. ->will($this->returnValue(1));
  163. $this->integrationAuthServiceMock->expects($this->once())
  164. ->method('grantAllPermissions')
  165. ->with($integrationId);
  166. $this->integrationPlugin->afterUpdate($this->subjectMock, $integrationModelMock);
  167. }
  168. public function testAfterGet()
  169. {
  170. $integrationId = 1;
  171. $integrationModelMock = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
  172. ->disableOriginalConstructor()
  173. ->getMock();
  174. $integrationModelMock->expects($this->exactly(2))
  175. ->method('getId')
  176. ->will($this->returnValue($integrationId));
  177. $integrationModelMock->expects($this->once())
  178. ->method('setData')
  179. ->with('resource', ['testResource']);
  180. $deprecatedIntegrationsData = [
  181. Integration::ID => $integrationId,
  182. Integration::NAME => 'TestIntegration1',
  183. Integration::EMAIL => 'test-integration1@magento.com',
  184. Integration::ENDPOINT => 'http://endpoint.com',
  185. Integration::SETUP_TYPE => 1,
  186. 'resource' => ['testResource']
  187. ];
  188. $consolidatedIntegrationsData = [
  189. Integration::ID => 2,
  190. Integration::NAME => 'TestIntegration2',
  191. Integration::EMAIL => 'test-integration2@magento.com',
  192. Integration::ENDPOINT => 'http://endpoint2.com',
  193. Integration::SETUP_TYPE => 1,
  194. 'resource' => ['testResource']
  195. ];
  196. $this->integrationConfigMock->method('getIntegrations')->willReturn($deprecatedIntegrationsData);
  197. $this->consolidatedConfigMock->method('getIntegrations')->willReturn($consolidatedIntegrationsData);
  198. $this->aclRetrieverMock->expects($this->once())
  199. ->method('getAllowedResourcesByUser')
  200. ->with(\Magento\Authorization\Model\UserContextInterface::USER_TYPE_INTEGRATION, $integrationId)
  201. ->will($this->returnValue(['testResource']));
  202. $this->integrationPlugin->afterGet($this->subjectMock, $integrationModelMock);
  203. }
  204. }