Skip to content

Commit 9451e2b

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-53194' into BUGS
2 parents 3790cb9 + 8004707 commit 9451e2b

File tree

6 files changed

+232
-11
lines changed

6 files changed

+232
-11
lines changed

app/code/Magento/Customer/Controller/Adminhtml/Group/Save.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,14 @@ public function execute()
7777
$id = $this->getRequest()->getParam('id');
7878
$resultRedirect = $this->resultRedirectFactory->create();
7979
try {
80+
$customerGroupCode = (string)$this->getRequest()->getParam('code');
8081
if ($id !== null) {
8182
$customerGroup = $this->groupRepository->getById((int)$id);
83+
$customerGroupCode = $customerGroupCode ?: $customerGroup->getCode();
8284
} else {
8385
$customerGroup = $this->groupDataFactory->create();
8486
}
85-
$customerGroupCode = (string)$this->getRequest()->getParam('code');
86-
if (empty($customerGroupCode)) {
87-
$customerGroupCode = null;
88-
}
89-
$customerGroup->setCode($customerGroupCode);
87+
$customerGroup->setCode(!empty($customerGroupCode) ? $customerGroupCode : null);
9088
$customerGroup->setTaxClassId($taxClass);
9189

9290
$this->groupRepository->save($customerGroup);

app/code/Magento/Customer/Model/ResourceModel/GroupRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function save(\Magento\Customer\Api\Data\GroupInterface $group)
109109

110110
/** @var \Magento\Customer\Model\Group $groupModel */
111111
$groupModel = null;
112-
if ($group->getId()) {
112+
if ($group->getId() || (string)$group->getId() === '0') {
113113
$this->_verifyTaxClassModel($group->getTaxClassId(), $group);
114114
$groupModel = $this->groupRegistry->retrieve($group->getId());
115115
$groupDataAttributes = $this->dataObjectProcessor->buildOutputDataArray(
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Test\Unit\Controller\Adminhtml\Group;
7+
8+
use Magento\Customer\Api\Data\GroupInterfaceFactory;
9+
use Magento\Customer\Controller\Adminhtml\Group\Save;
10+
use Magento\Framework\Controller\Result\RedirectFactory;
11+
use Magento\Framework\Controller\Result\Redirect;
12+
use Magento\Framework\Reflection\DataObjectProcessor;
13+
14+
/**
15+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
16+
*/
17+
class SaveTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/** @var Save */
20+
protected $controller;
21+
22+
/** @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject */
23+
protected $contextMock;
24+
25+
/** @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject */
26+
protected $registryMock;
27+
28+
/** @var \Magento\Customer\Api\GroupRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
29+
protected $groupRepositoryMock;
30+
31+
/** @var GroupInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject */
32+
protected $groupInterfaceFactoryMock;
33+
34+
/** @var \Magento\Backend\Model\View\Result\ForwardFactory|\PHPUnit_Framework_MockObject_MockObject */
35+
protected $forwardFactoryMock;
36+
37+
/** @var \Magento\Framework\View\Result\PageFactory|\PHPUnit_Framework_MockObject_MockObject */
38+
protected $pageFactoryMock;
39+
40+
/** @var DataObjectProcessor|\PHPUnit_Framework_MockObject_MockObject */
41+
protected $dataObjectProcessorMock;
42+
43+
/** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
44+
protected $request;
45+
46+
/** @var RedirectFactory|\PHPUnit_Framework_MockObject_MockObject */
47+
protected $resultRedirectFactory;
48+
49+
/** @var Redirect|\PHPUnit_Framework_MockObject_MockObject */
50+
protected $resultRedirect;
51+
52+
/** @var \Magento\Customer\Api\Data\GroupInterface|\PHPUnit_Framework_MockObject_MockObject */
53+
protected $customerGroup;
54+
55+
/** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
56+
protected $messageManager;
57+
58+
/** @var \Magento\Backend\Model\View\Result\Forward|\PHPUnit_Framework_MockObject_MockObject */
59+
protected $resultForward;
60+
61+
/** @var \Magento\Customer\Api\Data\GroupInterface|\PHPUnit_Framework_MockObject_MockObject */
62+
protected $group;
63+
64+
/** @var \Magento\Backend\Model\Session|\PHPUnit_Framework_MockObject_MockObject */
65+
protected $session;
66+
67+
protected function setUp()
68+
{
69+
$this->contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
70+
->disableOriginalConstructor()
71+
->getMock();
72+
$this->registryMock = $this->getMockBuilder(\Magento\Framework\Registry::class)
73+
->getMockForAbstractClass();
74+
$this->groupRepositoryMock = $this->getMockBuilder(\Magento\Customer\Api\GroupRepositoryInterface::class)
75+
->getMockForAbstractClass();
76+
$this->groupInterfaceFactoryMock = $this->getMockBuilder(GroupInterfaceFactory::class)
77+
->disableOriginalConstructor()
78+
->getMock();
79+
$this->forwardFactoryMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\ForwardFactory::class)
80+
->disableOriginalConstructor()
81+
->setMethods(['create'])
82+
->getMock();
83+
$this->pageFactoryMock = $this->getMockBuilder(\Magento\Framework\View\Result\PageFactory::class)
84+
->disableOriginalConstructor()
85+
->getMock();
86+
$this->dataObjectProcessorMock = $this->getMockBuilder(DataObjectProcessor::class)
87+
->disableOriginalConstructor()
88+
->getMock();
89+
$this->request = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
90+
->getMockForAbstractClass();
91+
$this->resultRedirectFactory = $this->getMockBuilder(RedirectFactory::class)
92+
->disableOriginalConstructor()
93+
->getMock();
94+
$this->resultRedirect = $this->getMockBuilder(Redirect::class)
95+
->disableOriginalConstructor()
96+
->getMock();
97+
$this->customerGroup = $this->getMockBuilder(\Magento\Customer\Api\Data\GroupInterface::class)
98+
->getMockForAbstractClass();
99+
$this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
100+
->getMockForAbstractClass();
101+
$this->resultForward = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Forward::class)
102+
->disableOriginalConstructor()
103+
->getMock();
104+
$this->group = $this->getMockBuilder(\Magento\Customer\Api\Data\GroupInterface::class)
105+
->getMockForAbstractClass();
106+
$this->session = $this->getMockBuilder(\Magento\Backend\Model\Session::class)
107+
->disableOriginalConstructor()
108+
->setMethods(['setCustomerGroupData'])
109+
->getMock();
110+
111+
$this->contextMock->expects($this->once())
112+
->method('getMessageManager')
113+
->willReturn($this->messageManager);
114+
$this->contextMock->expects($this->once())
115+
->method('getRequest')
116+
->willReturn($this->request);
117+
$this->contextMock->expects($this->once())
118+
->method('getResultRedirectFactory')
119+
->willReturn($this->resultRedirectFactory);
120+
$this->contextMock->expects($this->once())
121+
->method('getSession')
122+
->willReturn($this->session);
123+
124+
$this->controller = new Save(
125+
$this->contextMock,
126+
$this->registryMock,
127+
$this->groupRepositoryMock,
128+
$this->groupInterfaceFactoryMock,
129+
$this->forwardFactoryMock,
130+
$this->pageFactoryMock,
131+
$this->dataObjectProcessorMock
132+
);
133+
}
134+
135+
public function testExecuteWithTaxClassAndException()
136+
{
137+
$taxClass = '3';
138+
$groupId = 0;
139+
$code = 'NOT LOGGED IN';
140+
141+
$this->request->expects($this->exactly(3))
142+
->method('getParam')
143+
->withConsecutive(
144+
['tax_class'],
145+
['id'],
146+
['code']
147+
)
148+
->willReturnOnConsecutiveCalls($taxClass, $groupId, null);
149+
$this->resultRedirectFactory->expects($this->once())
150+
->method('create')
151+
->willReturn($this->resultRedirect);
152+
$this->groupRepositoryMock->expects($this->once())
153+
->method('getById')
154+
->with($groupId)
155+
->willReturn($this->group);
156+
$this->group->expects($this->once())
157+
->method('getCode')
158+
->willReturn($code);
159+
$this->group->expects($this->once())
160+
->method('setCode')
161+
->with($code);
162+
$this->group->expects($this->once())
163+
->method('setTaxClassId')
164+
->with($taxClass);
165+
$this->groupRepositoryMock->expects($this->once())
166+
->method('save')
167+
->with($this->group);
168+
$this->messageManager->expects($this->once())
169+
->method('addSuccess')
170+
->with(__('You saved the customer group.'));
171+
$exception = new \Exception('Exception');
172+
$this->resultRedirect->expects($this->at(0))
173+
->method('setPath')
174+
->with('customer/group')
175+
->willThrowException($exception);
176+
$this->messageManager->expects($this->once())
177+
->method('addError')
178+
->with('Exception');
179+
$this->dataObjectProcessorMock->expects($this->once())
180+
->method('buildOutputDataArray')
181+
->with($this->group, '\Magento\Customer\Api\Data\GroupInterface')
182+
->willReturn(['code' => $code]);
183+
$this->session->expects($this->once())
184+
->method('setCustomerGroupData')
185+
->with(['customer_group_code' => $code]);
186+
$this->resultRedirect->expects($this->at(1))
187+
->method('setPath')
188+
->with('customer/group/edit', ['id' => $groupId]);
189+
$this->assertSame($this->resultRedirect, $this->controller->execute());
190+
}
191+
192+
public function testExecuteWithoutTaxClass()
193+
{
194+
$this->request->expects($this->once())
195+
->method('getParam')
196+
->with('tax_class')
197+
->willReturn(null);
198+
$this->forwardFactoryMock->expects($this->once())
199+
->method('create')
200+
->willReturn($this->resultForward);
201+
$this->resultForward->expects($this->once())
202+
->method('forward')
203+
->with('new')
204+
->willReturnSelf();
205+
$this->assertSame($this->resultForward, $this->controller->execute());
206+
}
207+
}

app/code/Magento/Customer/Test/Unit/Model/ResourceModel/GroupRepositoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ protected function setUp()
168168

169169
public function testSave()
170170
{
171-
$groupId = 23;
171+
$groupId = 0;
172172

173173
$taxClass = $this->getMockForAbstractClass('Magento\Tax\Api\Data\TaxClassInterface', [], '', false);
174174

lib/internal/Magento/Framework/Model/ResourceModel/Db/AbstractDb.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ protected function _checkUnique(\Magento\Framework\Model\AbstractModel $object)
594594
}
595595
}
596596

597-
if ($object->getId() || $object->getId() === '0') {
597+
if ($object->getId() || (string)$object->getId() === '0') {
598598
$select->where($this->getIdFieldName() . '!=?', $object->getId());
599599
}
600600

lib/internal/Magento/Framework/Model/Test/Unit/ResourceModel/Db/AbstractDbTest.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,15 +465,15 @@ public function testPrepareDataForUpdate()
465465
$abstractModelMock->setIdFieldName('id');
466466
$abstractModelMock->setData(
467467
[
468-
'id' => 12345,
468+
'id' => 0,
469469
'name' => 'Test Name',
470470
'value' => 'Test Value'
471471
]
472472
);
473473
$abstractModelMock->afterLoad();
474474
$this->assertEquals($abstractModelMock->getData(), $abstractModelMock->getStoredData());
475475
$newData = ['value' => 'Test Value New'];
476-
$this->_model->expects($this->once())->method('_prepareDataForTable')->will($this->returnValue($newData));
476+
$this->_model->expects($this->atLeastOnce())->method('_prepareDataForTable')->will($this->returnValue($newData));
477477
$abstractModelMock->addData($newData);
478478
$this->assertNotEquals($abstractModelMock->getData(), $abstractModelMock->getStoredData());
479479
$abstractModelMock->isObjectNew(false);
@@ -484,7 +484,23 @@ public function testPrepareDataForUpdate()
484484
$newData,
485485
'idFieldName'
486486
);
487-
487+
$select = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
488+
->disableOriginalConstructor()
489+
->getMock();
490+
$select->expects($this->once())
491+
->method('from')
492+
->with('tableName')
493+
->willReturnSelf();
494+
$connectionMock->expects($this->once())
495+
->method('select')
496+
->willReturn($select);
497+
$select->expects($this->once())
498+
->method('reset')
499+
->with(\Magento\Framework\DB\Select::WHERE);
500+
$select->expects($this->exactly(2))
501+
->method('where')
502+
->withConsecutive(['uniqueField IS NULL'], ['idFieldName!=?', 0]);
503+
$this->_model->addUniqueField(['field' => 'uniqueField']);
488504
$this->_model->save($abstractModelMock);
489505
}
490506

0 commit comments

Comments
 (0)