Skip to content

Commit edd5f9b

Browse files
committed
MCP-218: Customer Group Limitations by Websites
- Add integration test;
1 parent 8eda29f commit edd5f9b

File tree

2 files changed

+179
-13
lines changed

2 files changed

+179
-13
lines changed

dev/tests/integration/testsuite/Magento/Customer/Model/GroupExcludedWebsiteTest.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Magento\Customer\Api\Data\GroupInterfaceFactory;
1919
use Magento\Customer\Api\GroupRepositoryInterface;
2020
use Magento\Catalog\Model\ResourceModel\Layer\Filter\Price;
21-
use Magento\Framework\Api\DataObjectHelper;
2221
use Magento\CatalogRule\Model\Rule;
2322
use Magento\Framework\Api\SearchCriteriaBuilder;
2423
use Magento\Framework\DB\Adapter\AdapterInterface;
@@ -28,7 +27,6 @@
2827
use Magento\Store\Api\StoreRepositoryInterface;
2928
use Magento\Framework\App\ResourceConnection;
3029
use Magento\CatalogRule\Model\ResourceModel\Rule as RuleResourceModel;
31-
use Magento\Framework\Encryption\EncryptorInterface;
3230
use Magento\Framework\ObjectManagerInterface;
3331
use Magento\Store\Model\Group;
3432
use Magento\Store\Model\ResourceModel\Group as StoreGroupResourceModel;
@@ -67,15 +65,6 @@ class GroupExcludedWebsiteTest extends \PHPUnit\Framework\TestCase
6765
/** @var CustomerInterfaceFactory */
6866
private $customerFactory;
6967

70-
/** @var DataObjectHelper */
71-
protected $dataObjectHelper;
72-
73-
/** @var EncryptorInterface */
74-
protected $encryptor;
75-
76-
/** @var CustomerRegistry */
77-
protected $customerRegistry;
78-
7968
/** @var GroupRepositoryInterface */
8069
private $groupRepository;
8170

@@ -111,8 +100,6 @@ protected function setUp(): void
111100
$this->objectManager = Bootstrap::getObjectManager();
112101
$this->customerRepository = $this->objectManager->create(CustomerRepositoryInterface::class);
113102
$this->customerFactory = $this->objectManager->create(CustomerInterfaceFactory::class);
114-
$this->dataObjectHelper = $this->objectManager->create(DataObjectHelper::class);
115-
$this->customerRegistry = $this->objectManager->create(CustomerRegistry::class);
116103
$this->groupRepository = $this->objectManager->create(GroupRepositoryInterface::class);
117104
$this->groupFactory = $this->objectManager->create(GroupInterfaceFactory::class);
118105
$this->websiteResourceModel = $this->objectManager->get(WebsiteResourceModel::class);
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Model\Plugin;
9+
10+
use Magento\Customer\Api\Data\GroupInterfaceFactory;
11+
use Magento\Customer\Api\GroupRepositoryInterface;
12+
use Magento\Customer\Model\ResourceModel\GroupExcludedWebsite;
13+
use Magento\Framework\Api\SearchCriteriaBuilder;
14+
use Magento\Framework\Exception\LocalizedException;
15+
use Magento\Framework\App\ResourceConnection;
16+
use Magento\Framework\ObjectManagerInterface;
17+
use Magento\Store\Api\WebsiteRepositoryInterface;
18+
use Magento\Store\Model\ResourceModel\Website as WebsiteResourceModel;
19+
use Magento\Store\Model\Website;
20+
use Magento\TestFramework\Helper\Bootstrap;
21+
use Magento\Framework\Registry;
22+
23+
/**
24+
* Checks that removal of website also deletes it from the customer group excluded website table.
25+
* @magentoAppArea adminhtml
26+
*/
27+
class DeleteCustomerGroupExcludedWebsiteTest extends \PHPUnit\Framework\TestCase
28+
{
29+
private const GROUP_CODE = 'Humans';
30+
private const STORE_WEBSITE_CODE = 'custom_website';
31+
32+
/** @var ObjectManagerInterface */
33+
private $objectManager;
34+
35+
/** @var GroupRepositoryInterface */
36+
private $groupRepository;
37+
38+
/** @var GroupInterfaceFactory */
39+
private $groupFactory;
40+
41+
/** @var WebsiteResourceModel */
42+
private $websiteResourceModel;
43+
44+
/** @var ResourceConnection */
45+
private $resourceConnection;
46+
47+
/** @var \Magento\Customer\Api\Data\GroupExtensionInterfaceFactory */
48+
private $groupExtensionInterfaceFactory;
49+
50+
/** @var WebsiteRepositoryInterface */
51+
private $websiteRepository;
52+
53+
/** @var Registry */
54+
private $registry;
55+
56+
/**
57+
* @inheritdoc
58+
*/
59+
protected function setUp(): void
60+
{
61+
$this->objectManager = Bootstrap::getObjectManager();
62+
$this->groupRepository = $this->objectManager->create(GroupRepositoryInterface::class);
63+
$this->groupFactory = $this->objectManager->create(GroupInterfaceFactory::class);
64+
$this->websiteResourceModel = $this->objectManager->get(WebsiteResourceModel::class);
65+
$this->resourceConnection = $this->objectManager->get(ResourceConnection::class);
66+
$this->groupExtensionInterfaceFactory = $this->objectManager
67+
->get(\Magento\Customer\Api\Data\GroupExtensionInterfaceFactory::class);
68+
$this->websiteRepository = $this->objectManager->create(WebsiteRepositoryInterface::class);
69+
$this->registry = $this->objectManager->create(Registry::class);
70+
}
71+
72+
/**
73+
* @inheritdoc
74+
*/
75+
protected function tearDown(): void
76+
{
77+
/** Marks area as secure so Product repository would allow group removal */
78+
$isSecuredAreaSystemState = $this->registry->registry('isSecuredArea');
79+
$this->registry->unregister('isSecureArea');
80+
$this->registry->register('isSecureArea', true);
81+
82+
/** Remove customer group */
83+
$groupId = $this->findGroupIdWithCode(self::GROUP_CODE);
84+
$group = $this->groupRepository->getById($groupId);
85+
$this->groupRepository->delete($group);
86+
87+
/** Revert mark area secured */
88+
$this->registry->unregister('isSecuredArea');
89+
$this->registry->register('isSecuredArea', $isSecuredAreaSystemState);
90+
}
91+
92+
/**
93+
* Test that deletion of website also deletes this website from customer group excluded websites.
94+
* @magentoDbIsolation disabled
95+
*/
96+
public function testDeleteExcludedWebsiteAfterWebsiteDelete(): void
97+
{
98+
/** Create website */
99+
/** @var Website $website */
100+
$website = $this->objectManager->create(Website::class);
101+
$website->setName('custom website for delete excluded website test')
102+
->setCode(self::STORE_WEBSITE_CODE);
103+
$website->isObjectNew(true);
104+
$this->websiteResourceModel->save($website);
105+
$websiteId = $this->websiteRepository->get(self::STORE_WEBSITE_CODE)->getId();
106+
107+
/** Create a new customer group */
108+
$group = $this->groupFactory->create()
109+
->setId(null)
110+
->setCode(self::GROUP_CODE)
111+
->setTaxClassId(3);
112+
$groupId = $this->groupRepository->save($group)->getId();
113+
self::assertNotNull($groupId);
114+
115+
/** Exclude website from customer group */
116+
$group = $this->groupRepository->getById($groupId);
117+
$customerGroupExtensionAttributes = $this->groupExtensionInterfaceFactory->create();
118+
$customerGroupExtensionAttributes->setExcludeWebsiteIds([$websiteId]);
119+
$group->setExtensionAttributes($customerGroupExtensionAttributes);
120+
$this->groupRepository->save($group);
121+
122+
/** Check that excluded website is in customer group excluded website table */
123+
$connection = $this->resourceConnection->getConnection();
124+
$selectExcludedWebsite = $connection->select();
125+
/** @var GroupExcludedWebsite $groupExcludedWebsiteResource */
126+
$groupExcludedWebsiteResource = $this->objectManager->create(GroupExcludedWebsite::class);
127+
$selectExcludedWebsite->from($groupExcludedWebsiteResource->getMainTable())
128+
->where('website_id = ?', $websiteId);
129+
$excludedWebsites = $connection->fetchAll($selectExcludedWebsite);
130+
self::assertCount(1, $excludedWebsites);
131+
132+
/** Marks area as secure so Product repository would allow website removal */
133+
$registry = $this->objectManager->get(Registry::class);
134+
$isSecuredAreaSystemState = $registry->registry('isSecuredArea');
135+
$registry->unregister('isSecureArea');
136+
$registry->register('isSecureArea', true);
137+
/** Remove website by id */
138+
/** @var \Magento\Store\Model\Website $website */
139+
$website = $this->objectManager->create(\Magento\Store\Model\Website::class);
140+
$website->load((int)$websiteId);
141+
$website->delete();
142+
143+
/** Revert mark area secured */
144+
$registry->unregister('isSecuredArea');
145+
$registry->register('isSecuredArea', $isSecuredAreaSystemState);
146+
147+
/** Check that excluded website is no longer in customer group excluded website table */
148+
$selectExcludedWebsite = $connection->select();
149+
/** @var GroupExcludedWebsite $groupExcludedWebsiteResource */
150+
$groupExcludedWebsiteResource = $this->objectManager->create(GroupExcludedWebsite::class);
151+
$selectExcludedWebsite->from($groupExcludedWebsiteResource->getMainTable())
152+
->where('website_id = ?', $websiteId);
153+
$excludedWebsites = $connection->fetchAll($selectExcludedWebsite);
154+
self::assertCount(0, $excludedWebsites);
155+
}
156+
157+
/**
158+
* Find the customer group with a given code.
159+
*
160+
* @param string $code
161+
* @return int
162+
* @throws LocalizedException
163+
*/
164+
private function findGroupIdWithCode(string $code): int
165+
{
166+
/** @var GroupRepositoryInterface $groupRepository */
167+
$groupRepository = $this->objectManager->create(GroupRepositoryInterface::class);
168+
/** @var SearchCriteriaBuilder $searchBuilder */
169+
$searchBuilder = $this->objectManager->create(SearchCriteriaBuilder::class);
170+
171+
foreach ($groupRepository->getList($searchBuilder->create())->getItems() as $group) {
172+
if ($group->getCode() === $code) {
173+
return (int)$group->getId();
174+
}
175+
}
176+
177+
return -1;
178+
}
179+
}

0 commit comments

Comments
 (0)