GroupManagementTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Api;
  7. use Magento\Customer\Model\Data\Group as CustomerGroup;
  8. use Magento\Customer\Model\GroupRegistry;
  9. use Magento\Customer\Model\ResourceModel\GroupRepository;
  10. use Magento\TestFramework\Helper\Bootstrap;
  11. use Magento\TestFramework\TestCase\WebapiAbstract;
  12. /**
  13. * Class GroupManagementTest
  14. */
  15. class GroupManagementTest extends WebapiAbstract
  16. {
  17. const SERVICE_NAME = "customerGroupManagementV1";
  18. const SERVICE_VERSION = "V1";
  19. const RESOURCE_PATH = "/V1/customerGroups";
  20. /**
  21. * @var GroupRegistry
  22. */
  23. private $groupRegistry;
  24. /**
  25. * @var GroupRepository
  26. */
  27. private $groupRepository;
  28. /**
  29. * Execute per test initialization.
  30. */
  31. public function setUp()
  32. {
  33. $objectManager = Bootstrap::getObjectManager();
  34. $this->groupRegistry = $objectManager->get(\Magento\Customer\Model\GroupRegistry::class);
  35. $this->groupRepository = $objectManager->get(\Magento\Customer\Model\ResourceModel\GroupRepository::class);
  36. }
  37. /**
  38. * Verify the retrieval of the default group for storeId equal to 1.
  39. *
  40. * @param int $storeId The store Id
  41. * @param array $defaultGroupData The default group data for the store with the specified Id.
  42. *
  43. * @dataProvider getDefaultGroupDataProvider
  44. */
  45. public function testGetDefaultGroup($storeId, $defaultGroupData)
  46. {
  47. $serviceInfo = [
  48. 'rest' => [
  49. 'resourcePath' => self::RESOURCE_PATH . "/default/$storeId",
  50. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  51. ],
  52. 'soap' => [
  53. 'service' => self::SERVICE_NAME,
  54. 'serviceVersion' => self::SERVICE_VERSION,
  55. 'operation' => 'customerGroupManagementV1GetDefaultGroup',
  56. ],
  57. ];
  58. $requestData = ['storeId' => $storeId];
  59. $groupData = $this->_webApiCall($serviceInfo, $requestData);
  60. $this->assertEquals($defaultGroupData, $groupData, "The default group does not match.");
  61. }
  62. /**
  63. * The testGetDefaultGroup data provider.
  64. *
  65. * @return array
  66. */
  67. public function getDefaultGroupDataProvider()
  68. {
  69. return [
  70. 'admin' => [
  71. 0,
  72. [
  73. CustomerGroup::ID => 1,
  74. CustomerGroup::CODE => 'General',
  75. CustomerGroup::TAX_CLASS_ID => 3,
  76. CustomerGroup::TAX_CLASS_NAME => 'Retail Customer'
  77. ],
  78. ],
  79. 'base' => [
  80. 1,
  81. [
  82. CustomerGroup::ID => 1,
  83. CustomerGroup::CODE => 'General',
  84. CustomerGroup::TAX_CLASS_ID => 3,
  85. CustomerGroup::TAX_CLASS_NAME => 'Retail Customer'
  86. ],
  87. ]
  88. ];
  89. }
  90. /**
  91. * Verify the retrieval of a non-existent storeId will return an expected fault.
  92. */
  93. public function testGetDefaultGroupNonExistentStore()
  94. {
  95. /* Store id should not exist */
  96. $nonExistentStoreId = 9876;
  97. $serviceInfo = [
  98. 'rest' => [
  99. 'resourcePath' => self::RESOURCE_PATH . "/default/$nonExistentStoreId",
  100. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  101. ],
  102. 'soap' => [
  103. 'service' => self::SERVICE_NAME,
  104. 'serviceVersion' => self::SERVICE_VERSION,
  105. 'operation' => 'customerGroupManagementV1GetDefaultGroup',
  106. ],
  107. ];
  108. $requestData = ['storeId' => $nonExistentStoreId];
  109. $expectedMessage = 'No such entity with %fieldName = %fieldValue';
  110. try {
  111. $this->_webApiCall($serviceInfo, $requestData);
  112. $this->fail("Expected exception");
  113. } catch (\SoapFault $e) {
  114. $this->assertContains(
  115. $expectedMessage,
  116. $e->getMessage(),
  117. "SoapFault does not contain expected message."
  118. );
  119. } catch (\Exception $e) {
  120. $this->assertContains(
  121. $expectedMessage,
  122. $e->getMessage(),
  123. "Exception does not contain expected message."
  124. );
  125. $this->assertContains((string)$nonExistentStoreId, $e->getMessage());
  126. }
  127. }
  128. /**
  129. * Verify that the group with the specified Id can or cannot be deleted.
  130. *
  131. * @param int $groupId The group Id
  132. * @param bool $isDeleteable Whether the group can or cannot be deleted.
  133. *
  134. * @dataProvider isReadonlyDataProvider
  135. */
  136. public function testIsReadonly($groupId, $isDeleteable)
  137. {
  138. $serviceInfo = [
  139. 'rest' => [
  140. 'resourcePath' => self::RESOURCE_PATH . "/$groupId/permissions",
  141. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  142. ],
  143. 'soap' => [
  144. 'service' => self::SERVICE_NAME,
  145. 'serviceVersion' => self::SERVICE_VERSION,
  146. 'operation' => 'customerGroupManagementV1IsReadonly',
  147. ],
  148. ];
  149. $requestData = [CustomerGroup::ID => $groupId];
  150. $isReadonly = $this->_webApiCall($serviceInfo, $requestData);
  151. $failureMessage = $isDeleteable
  152. ? 'The group should be deleteable.' : 'The group should not be deleteable.';
  153. $this->assertEquals($isDeleteable, !$isReadonly, $failureMessage);
  154. }
  155. /**
  156. * The testIsReadonly data provider.
  157. *
  158. * @return array
  159. */
  160. public function isReadonlyDataProvider()
  161. {
  162. return [
  163. 'NOT LOGGED IN' => [0, false],
  164. 'General' => [1, false],
  165. 'Wholesale' => [2, true],
  166. 'Retailer' => [3, true]
  167. ];
  168. }
  169. /**
  170. * Verify that the group with the specified Id can or cannot be deleted.
  171. */
  172. public function testIsReadonlyNoSuchGroup()
  173. {
  174. /* This group ID should not exist in the store. */
  175. $groupId = 9999;
  176. $serviceInfo = [
  177. 'rest' => [
  178. 'resourcePath' => self::RESOURCE_PATH . "/$groupId/permissions",
  179. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  180. ],
  181. 'soap' => [
  182. 'service' => self::SERVICE_NAME,
  183. 'serviceVersion' => self::SERVICE_VERSION,
  184. 'operation' => 'customerGroupManagementV1IsReadonly',
  185. ],
  186. ];
  187. $requestData = [CustomerGroup::ID => $groupId];
  188. $expectedMessage = 'No such entity with %fieldName = %fieldValue';
  189. try {
  190. $this->_webApiCall($serviceInfo, $requestData);
  191. $this->fail("Expected exception.");
  192. } catch (\SoapFault $e) {
  193. $this->assertContains(
  194. $expectedMessage,
  195. $e->getMessage(),
  196. "SoapFault does not contain expected message."
  197. );
  198. } catch (\Exception $e) {
  199. $this->assertContains(
  200. $expectedMessage,
  201. $e->getMessage(),
  202. "Exception does not contain expected message."
  203. );
  204. $this->assertContains((string)$groupId, $e->getMessage());
  205. }
  206. }
  207. }