Skip to content

Commit 9b178d0

Browse files
committed
MC-31945: New customer creation via the admin does not honor default customer group
1 parent 047f5b9 commit 9b178d0

File tree

2 files changed

+198
-8
lines changed

2 files changed

+198
-8
lines changed

app/code/Magento/Customer/Model/AttributeMetadataResolver.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
*/
77
namespace Magento\Customer\Model;
88

9+
use Magento\Customer\Api\Data\AddressInterface;
10+
use Magento\Customer\Api\Data\CustomerInterface;
11+
use Magento\Customer\Model\Config\Share as ShareConfig;
912
use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryWithWebsites;
13+
use Magento\Eav\Api\Data\AttributeInterface;
1014
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
11-
use Magento\Customer\Api\Data\AddressInterface;
12-
use Magento\Ui\DataProvider\EavValidationRules;
13-
use Magento\Ui\Component\Form\Field;
1415
use Magento\Eav\Model\Entity\Type;
15-
use Magento\Eav\Api\Data\AttributeInterface;
16+
use Magento\Framework\App\ObjectManager;
1617
use Magento\Framework\View\Element\UiComponent\ContextInterface;
17-
use Magento\Customer\Api\Data\CustomerInterface;
18-
use Magento\Customer\Model\Config\Share as ShareConfig;
19-
use Magento\Customer\Model\FileUploaderDataResolver;
18+
use Magento\Ui\Component\Form\Field;
19+
use Magento\Ui\DataProvider\EavValidationRules;
2020

2121
/**
2222
* Class to build meta data of the customer or customer address attribute
@@ -75,25 +75,33 @@ class AttributeMetadataResolver
7575
*/
7676
private $shareConfig;
7777

78+
/**
79+
* @var GroupManagement
80+
*/
81+
private $groupManagement;
82+
7883
/**
7984
* @param CountryWithWebsites $countryWithWebsiteSource
8085
* @param EavValidationRules $eavValidationRules
8186
* @param FileUploaderDataResolver $fileUploaderDataResolver
8287
* @param ContextInterface $context
8388
* @param ShareConfig $shareConfig
89+
* @param GroupManagement|null $groupManagement
8490
*/
8591
public function __construct(
8692
CountryWithWebsites $countryWithWebsiteSource,
8793
EavValidationRules $eavValidationRules,
8894
FileUploaderDataResolver $fileUploaderDataResolver,
8995
ContextInterface $context,
90-
ShareConfig $shareConfig
96+
ShareConfig $shareConfig,
97+
?GroupManagement $groupManagement = null
9198
) {
9299
$this->countryWithWebsiteSource = $countryWithWebsiteSource;
93100
$this->eavValidationRules = $eavValidationRules;
94101
$this->fileUploaderDataResolver = $fileUploaderDataResolver;
95102
$this->context = $context;
96103
$this->shareConfig = $shareConfig;
104+
$this->groupManagement = $groupManagement ?? ObjectManager::getInstance()->get(GroupManagement::class);
97105
}
98106

99107
/**
@@ -111,6 +119,7 @@ public function getAttributesMeta(
111119
bool $allowToShowHiddenAttributes
112120
): array {
113121
$meta = $this->modifyBooleanAttributeMeta($attribute);
122+
$this->modifyGroupAttributeMeta($attribute);
114123
// use getDataUsingMethod, since some getters are defined and apply additional processing of returning value
115124
foreach (self::$metaProperties as $metaName => $origName) {
116125
$value = $attribute->getDataUsingMethod($origName);
@@ -196,6 +205,21 @@ private function modifyBooleanAttributeMeta(AttributeInterface $attribute): arra
196205
return $meta;
197206
}
198207

208+
/**
209+
* Modify group attribute meta data
210+
*
211+
* @param AttributeInterface $attribute
212+
* @return void
213+
*/
214+
private function modifyGroupAttributeMeta(AttributeInterface $attribute): void
215+
{
216+
if ($attribute->getAttributeCode() === 'group_id') {
217+
$defaultGroup = $this->groupManagement->getDefaultGroup();
218+
$defaultGroupId = !empty($defaultGroup) ? $defaultGroup->getId() : null;
219+
$attribute->setDataUsingMethod(self::$metaProperties['default'], $defaultGroupId);
220+
}
221+
}
222+
199223
/**
200224
* Add global scope parameter and filter options to website meta
201225
*
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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\Test\Unit\Model;
9+
10+
use Magento\Customer\Model\Attribute;
11+
use Magento\Customer\Model\AttributeMetadataResolver;
12+
use Magento\Customer\Model\Config\Share as ShareConfig;
13+
use Magento\Customer\Model\FileUploaderDataResolver;
14+
use Magento\Customer\Model\GroupManagement;
15+
use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryWithWebsites;
16+
use Magento\Eav\Model\Entity\Type;
17+
use Magento\Framework\View\Element\UiComponent\ContextInterface;
18+
use Magento\Ui\DataProvider\EavValidationRules;
19+
use PHPUnit\Framework\MockObject\MockObject;
20+
use PHPUnit\Framework\TestCase;
21+
22+
/**
23+
* Class AttributeMetadataResolverTest
24+
*
25+
* Validate attributeMetadata contains correct values in meta data array
26+
*/
27+
class AttributeMetadataResolverTest extends TestCase
28+
{
29+
/**
30+
* @var CountryWithWebsites|MockObject
31+
*/
32+
private $countryWithWebsiteSource;
33+
34+
/**
35+
* @var EavValidationRules|MockObject
36+
*/
37+
private $eavValidationRules;
38+
39+
/**
40+
* @var FileUploaderDataResolver|MockObject
41+
*/
42+
private $fileUploaderDataResolver;
43+
44+
/**
45+
* @var ShareConfig|MockObject
46+
*/
47+
private $shareConfig;
48+
49+
/**
50+
* @var GroupManagement|MockObject
51+
*/
52+
private $groupManagement;
53+
54+
/**
55+
* @var ContextInterface|MockObject
56+
*/
57+
private $context;
58+
59+
/**
60+
* @var AttributeMetadataResolver
61+
*/
62+
private $model;
63+
64+
/**
65+
* @var Attribute|MockObject
66+
*/
67+
private $attribute;
68+
69+
/**
70+
* @inheritdoc
71+
*/
72+
public function setUp()
73+
{
74+
$this->countryWithWebsiteSource = $this->getMockBuilder(CountryWithWebsites::class)
75+
->setMethods(['getAllOptions'])
76+
->disableOriginalConstructor()
77+
->getMock();
78+
$this->eavValidationRules = $this->getMockBuilder(EavValidationRules::class)
79+
->setMethods(['build'])
80+
->disableOriginalConstructor()
81+
->getMock();
82+
$this->fileUploaderDataResolver = $this->getMockBuilder(FileUploaderDataResolver::class)
83+
->setMethods(['overrideFileUploaderMetadata'])
84+
->disableOriginalConstructor()
85+
->getMock();
86+
$this->context = $this->getMockBuilder(ContextInterface::class)
87+
->disableOriginalConstructor()
88+
->getMock();
89+
$this->shareConfig = $this->getMockBuilder(ShareConfig::class)
90+
->disableOriginalConstructor()
91+
->getMock();
92+
$this->groupManagement = $this->getMockBuilder(GroupManagement::class)
93+
->setMethods(['getId', 'getDefaultGroup'])
94+
->disableOriginalConstructor()
95+
->getMock();
96+
$this->attribute = $this->getMockBuilder(Attribute::class)
97+
->setMethods([
98+
'usesSource',
99+
'getDataUsingMethod',
100+
'getAttributeCode',
101+
'getFrontendInput',
102+
'getSource',
103+
'setDataUsingMethod'
104+
])
105+
->disableOriginalConstructor()
106+
->getMock();
107+
108+
$this->model = new AttributeMetadataResolver(
109+
$this->countryWithWebsiteSource,
110+
$this->eavValidationRules,
111+
$this->fileUploaderDataResolver,
112+
$this->context,
113+
$this->shareConfig,
114+
$this->groupManagement
115+
);
116+
}
117+
118+
/**
119+
* Test to get meta data of the customer or customer address attribute
120+
*
121+
* @return void
122+
*/
123+
public function testGetAttributesMetaHasDefaultAttributeValue(): void
124+
{
125+
$rules = [
126+
'required-entry' => true
127+
];
128+
$defaultGroupId = '3';
129+
$allowToShowHiddenAttributes = false;
130+
$usesSource = false;
131+
$entityType = $this->getMockBuilder(Type::class)
132+
->disableOriginalConstructor()
133+
->getMock();
134+
$this->attribute->expects($this->once())
135+
->method('usesSource')
136+
->willReturn($usesSource);
137+
$this->attribute->expects($this->once())
138+
->method('getAttributeCode')
139+
->willReturn('group_id');
140+
$this->groupManagement->expects($this->once())
141+
->method('getDefaultGroup')
142+
->willReturnSelf();
143+
$this->groupManagement->expects($this->once())
144+
->method('getId')
145+
->willReturn($defaultGroupId);
146+
$this->attribute->expects($this->at(9))
147+
->method('getDataUsingMethod')
148+
->with('default_value')
149+
->willReturn($defaultGroupId);
150+
$this->attribute->expects($this->once())
151+
->method('setDataUsingMethod')
152+
->willReturnSelf();
153+
$this->eavValidationRules->expects($this->once())
154+
->method('build')
155+
->with($this->attribute)
156+
->willReturn($rules);
157+
$this->fileUploaderDataResolver->expects($this->once())
158+
->method('overrideFileUploaderMetadata')
159+
->with($entityType, $this->attribute)
160+
->willReturnSelf();
161+
162+
$meta = $this->model->getAttributesMeta($this->attribute, $entityType, $allowToShowHiddenAttributes);
163+
$this->assertArrayHasKey('default', $meta['arguments']['data']['config']);
164+
$this->assertEquals($defaultGroupId, $meta['arguments']['data']['config']['default']);
165+
}
166+
}

0 commit comments

Comments
 (0)