GroupManagementTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model;
  7. use Magento\Store\Model\ScopeInterface;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. /**
  10. * Test for Magento\Customer\Model\GroupManagement
  11. */
  12. class GroupManagementTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Framework\ObjectManagerInterface
  16. */
  17. protected $objectManager;
  18. /**
  19. * @var \Magento\Customer\Api\GroupManagementInterface
  20. */
  21. protected $groupManagement;
  22. protected function setUp()
  23. {
  24. $this->objectManager = Bootstrap::getObjectManager();
  25. $this->groupManagement = $this->objectManager->get(\Magento\Customer\Api\GroupManagementInterface::class);
  26. }
  27. /**
  28. * @param $testGroup
  29. * @param $storeId
  30. *
  31. * @dataProvider getDefaultGroupDataProvider
  32. */
  33. public function testGetDefaultGroupWithStoreId($testGroup, $storeId)
  34. {
  35. $this->assertDefaultGroupMatches($testGroup, $storeId);
  36. }
  37. /**
  38. * @magentoDataFixture Magento/Store/_files/core_second_third_fixturestore.php
  39. */
  40. public function testGetDefaultGroupWithNonDefaultStoreId()
  41. {
  42. /** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
  43. $storeManager = Bootstrap::getObjectManager()->get(\Magento\Store\Model\StoreManagerInterface::class);
  44. $nonDefaultStore = $storeManager->getStore('secondstore');
  45. $nonDefaultStoreId = $nonDefaultStore->getId();
  46. /** @var \Magento\Framework\App\MutableScopeConfig $scopeConfig */
  47. $scopeConfig = $this->objectManager->get(\Magento\Framework\App\MutableScopeConfig::class);
  48. $scopeConfig->setValue(
  49. \Magento\Customer\Model\GroupManagement::XML_PATH_DEFAULT_ID,
  50. 2,
  51. ScopeInterface::SCOPE_STORE,
  52. 'secondstore'
  53. );
  54. $testGroup = ['id' => 2, 'code' => 'Wholesale', 'tax_class_id' => 3, 'tax_class_name' => 'Retail Customer'];
  55. $this->assertDefaultGroupMatches($testGroup, $nonDefaultStoreId);
  56. }
  57. /**
  58. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  59. */
  60. public function testGetDefaultGroupWithInvalidStoreId()
  61. {
  62. $storeId = 1234567;
  63. $this->groupManagement->getDefaultGroup($storeId);
  64. }
  65. public function testIsReadonlyWithGroupId()
  66. {
  67. $testGroup = ['id' => 3, 'code' => 'General', 'tax_class_id' => 3, 'tax_class_name' => 'Retail Customer'];
  68. $this->assertEquals(false, $this->groupManagement->isReadonly($testGroup['id']));
  69. }
  70. /**
  71. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  72. */
  73. public function testIsReadonlyWithInvalidGroupId()
  74. {
  75. $testGroup = ['id' => 4, 'code' => 'General', 'tax_class_id' => 3, 'tax_class_name' => 'Retail Customer'];
  76. $this->groupManagement->isReadonly($testGroup['id']);
  77. }
  78. public function testGetNotLoggedInGroup()
  79. {
  80. $notLoggedInGroup = $this->groupManagement->getNotLoggedInGroup();
  81. $this->assertEquals(GroupManagement::NOT_LOGGED_IN_ID, $notLoggedInGroup->getId());
  82. }
  83. public function testGetLoggedInGroups()
  84. {
  85. $loggedInGroups = $this->groupManagement->getLoggedInGroups();
  86. foreach ($loggedInGroups as $group) {
  87. $this->assertNotEquals(GroupManagement::NOT_LOGGED_IN_ID, $group->getId());
  88. $this->assertNotEquals(GroupManagement::CUST_GROUP_ALL, $group->getId());
  89. }
  90. }
  91. public function testGetAllGroup()
  92. {
  93. $allGroup = $this->groupManagement->getAllCustomersGroup();
  94. $this->assertEquals(32000, $allGroup->getId());
  95. }
  96. /**
  97. * @return array
  98. */
  99. public function getDefaultGroupDataProvider()
  100. {
  101. /** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
  102. $storeManager = Bootstrap::getObjectManager()->get(\Magento\Store\Model\StoreManagerInterface::class);
  103. $defaultStoreId = $storeManager->getStore()->getId();
  104. return [
  105. 'no store id' => [
  106. ['id' => 1, 'code' => 'General', 'tax_class_id' => 3, 'tax_class_name' => 'Retail Customer'],
  107. null,
  108. ],
  109. 'default store id' => [
  110. ['id' => 1, 'code' => 'General', 'tax_class_id' => 3, 'tax_class_name' => 'Retail Customer'],
  111. $defaultStoreId,
  112. ],
  113. ];
  114. }
  115. /**
  116. * @param $testGroup
  117. * @param $storeId
  118. */
  119. private function assertDefaultGroupMatches($testGroup, $storeId)
  120. {
  121. $group = $this->groupManagement->getDefaultGroup($storeId);
  122. $this->assertEquals($testGroup['id'], $group->getId());
  123. $this->assertEquals($testGroup['code'], $group->getCode());
  124. $this->assertEquals($testGroup['tax_class_id'], $group->getTaxClassId());
  125. $this->assertEquals($testGroup['tax_class_name'], $group->getTaxClassName());
  126. }
  127. }