Skip to content

Commit 37daacb

Browse files
committed
MC-32325: New customer creation via the admin does not honor default customer group for multi websites
1 parent 70cf0c2 commit 37daacb

File tree

4 files changed

+217
-1
lines changed

4 files changed

+217
-1
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Customer\Test\Unit\ViewModel\Customer;
10+
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use PHPUnit\Framework\TestCase;
13+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
14+
use Magento\Customer\ViewModel\Customer\Website as CustomerWebsite;
15+
use Magento\Store\Model\System\Store as SystemStore;
16+
17+
/**
18+
* Test for customer's website view model
19+
*/
20+
class WebsiteTest extends TestCase
21+
{
22+
/** @var ObjectManagerHelper */
23+
private $objectManagerHelper;
24+
25+
/**
26+
* @var CustomerWebsite
27+
*/
28+
private $customerWebsite;
29+
30+
/**
31+
* @var SystemStore
32+
*/
33+
private $systemStore;
34+
35+
/**
36+
* @var ScopeConfigInterface
37+
*/
38+
private $scopeConfig;
39+
40+
protected function setUp()
41+
{
42+
$this->systemStore = $this->createMock(SystemStore::class);
43+
$this->scopeConfig = $this->createMock(ScopeConfigInterface::class);
44+
$this->objectManagerHelper = new ObjectManagerHelper($this);
45+
$this->customerWebsite = $this->objectManagerHelper->getObject(
46+
CustomerWebsite::class,
47+
[
48+
'systemStore' => $this->systemStore,
49+
'scopeConfig' => $this->scopeConfig
50+
]
51+
);
52+
}
53+
54+
/**
55+
* Test that method return correct array of options
56+
*
57+
* @param array $options
58+
* @dataProvider dataProviderOptionsArray
59+
* @return void
60+
*/
61+
public function testToOptionArray(array $options): void
62+
{
63+
$this->scopeConfig->method('getValue')
64+
->willReturn(1);
65+
66+
$this->systemStore->method('getWebsiteValuesForForm')
67+
->willReturn([
68+
[
69+
'label' => 'Main Website',
70+
'value' => '1',
71+
],
72+
[
73+
'label' => 'Second Website',
74+
'value' => '2',
75+
],
76+
]);
77+
78+
$this->assertEquals($options, $this->customerWebsite->toOptionArray());
79+
}
80+
81+
/**
82+
* Data provider for testToOptionArray test
83+
*
84+
* @return array
85+
*/
86+
public function dataProviderOptionsArray(): array
87+
{
88+
return [
89+
[
90+
'options' => [
91+
[
92+
'label' => 'Main Website',
93+
'value' => '1',
94+
'group_id' => '1',
95+
],
96+
[
97+
'label' => 'Second Website',
98+
'value' => '2',
99+
'group_id' => '1',
100+
],
101+
],
102+
],
103+
];
104+
}
105+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Customer\ViewModel\Customer;
10+
11+
use Magento\Customer\Model\GroupManagement;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Magento\Framework\Data\OptionSourceInterface;
14+
use Magento\Store\Model\ScopeInterface;
15+
use Magento\Store\Model\System\Store as SystemStore;
16+
17+
/**
18+
* Customer's website view model
19+
*/
20+
class Website implements OptionSourceInterface
21+
{
22+
/**
23+
* @var SystemStore
24+
*/
25+
private $systemStore;
26+
27+
/**
28+
* @var ScopeConfigInterface
29+
*/
30+
private $scopeConfig;
31+
32+
/**
33+
* Store constructor.
34+
*
35+
* @param SystemStore $systemStore
36+
* @param ScopeConfigInterface $scopeConfig
37+
*/
38+
public function __construct(
39+
SystemStore $systemStore,
40+
ScopeConfigInterface $scopeConfig
41+
) {
42+
$this->systemStore = $systemStore;
43+
$this->scopeConfig = $scopeConfig;
44+
}
45+
46+
/**
47+
* @inheritdoc
48+
*/
49+
public function toOptionArray(): array
50+
{
51+
return $this->getWebsiteOptions();
52+
}
53+
54+
/**
55+
* Adding group ID to options list
56+
*
57+
* @return array
58+
*/
59+
private function getWebsiteOptions(): array
60+
{
61+
$options = $this->systemStore->getWebsiteValuesForForm();
62+
foreach ($options as $key => $option) {
63+
$websiteId = $option['value'];
64+
$groupId = $this->scopeConfig->getValue(
65+
GroupManagement::XML_PATH_DEFAULT_ID,
66+
ScopeInterface::SCOPE_WEBSITE,
67+
$websiteId
68+
);
69+
$options[$key]['group_id'] = $groupId;
70+
}
71+
72+
return $options;
73+
}
74+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'Magento_Ui/js/form/element/website',
8+
'uiRegistry'
9+
], function (Website, registry) {
10+
'use strict';
11+
12+
return Website.extend({
13+
/**
14+
* On value change handler.
15+
*
16+
* @param {String} value
17+
*/
18+
onUpdate: function (value) {
19+
var groupIdFieldKey = 'group_id',
20+
groupId = registry.get('index = ' + groupIdFieldKey),
21+
option = this.getOption(value);
22+
23+
if (groupId) {
24+
groupId.value(option[groupIdFieldKey]);
25+
}
26+
27+
return this._super();
28+
}
29+
});
30+
});

app/code/Magento/Customer/view/base/ui_component/customer_form.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
<visible>false</visible>
118118
</settings>
119119
</field>
120-
<field name="website_id" component="Magento_Ui/js/form/element/website" formElement="select">
120+
<field name="website_id" component="Magento_Customer/js/form/element/website" formElement="select">
121121
<argument name="data" xsi:type="array">
122122
<item name="config" xsi:type="array">
123123
<item name="source" xsi:type="string">customer</item>
@@ -136,6 +136,13 @@
136136
<link name="customerId">${ $.provider }:data.customer.entity_id</link>
137137
</imports>
138138
</settings>
139+
<formElements>
140+
<select>
141+
<settings>
142+
<options class="Magento\Customer\ViewModel\Customer\Website"/>
143+
</settings>
144+
</select>
145+
</formElements>
139146
</field>
140147
<field name="prefix" formElement="input">
141148
<argument name="data" xsi:type="array">

0 commit comments

Comments
 (0)