Skip to content

Commit 375077a

Browse files
committed
Merge pull request #460 from magento-vanilla/PR
[Vanilla] Bugs
2 parents 5eb3c9a + 5d831fe commit 375077a

File tree

5 files changed

+1120
-11
lines changed

5 files changed

+1120
-11
lines changed

app/code/Magento/Customer/Block/Adminhtml/Edit/Tab/Newsletter.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,23 +154,25 @@ public function initForm()
154154
[
155155
'label' => __('Subscribed to Newsletter'),
156156
'name' => 'subscription',
157-
'data-form-part' => $this->getData('target_form')
157+
'data-form-part' => $this->getData('target_form'),
158+
'onchange' => 'this.value = this.checked;'
158159
]
159160
);
160161

161162
if ($this->customerAccountManagement->isReadOnly($customerId)) {
162163
$form->getElement('subscription')->setReadonly(true, true);
163164
}
164-
165-
$form->getElement('subscription')->setIsChecked($subscriber->isSubscribed());
165+
$isSubscribed = $subscriber->isSubscribed();
166+
$form->setValues(['subscription' => $isSubscribed ? 'true' : 'false']);
167+
$form->getElement('subscription')->setIsChecked($isSubscribed);
166168

167169
$changedDate = $this->getStatusChangedDate();
168170
if ($changedDate) {
169171
$fieldset->addField(
170172
'change_status_date',
171173
'label',
172174
[
173-
'label' => $subscriber->isSubscribed() ? __('Last Date Subscribed') : __('Last Date Unsubscribed'),
175+
'label' => $isSubscribed ? __('Last Date Subscribed') : __('Last Date Unsubscribed'),
174176
'value' => $changedDate,
175177
'bold' => true
176178
]

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected function _extractData(
7474
\Magento\Customer\Model\Metadata\Form $metadataForm = null
7575
) {
7676
if ($metadataForm === null) {
77-
$metadataForm = $this->_objectManager->get('Magento\Customer\Model\Metadata\FormFactory')->create(
77+
$metadataForm = $this->_formFactory->create(
7878
$entityType,
7979
$formCode,
8080
[],
@@ -236,14 +236,16 @@ public function execute()
236236
$customerId = $customer->getId();
237237
}
238238

239-
$isSubscribed = false;
239+
$isSubscribed = null;
240240
if ($this->_authorization->isAllowed(null)) {
241-
$isSubscribed = $this->getRequest()->getPost('subscription') !== null;
241+
$isSubscribed = $this->getRequest()->getPost('subscription');
242242
}
243-
if ($isSubscribed) {
244-
$this->_subscriberFactory->create()->subscribeCustomerById($customerId);
245-
} else {
246-
$this->_subscriberFactory->create()->unsubscribeCustomerById($customerId);
243+
if ($isSubscribed !== null) {
244+
if ($isSubscribed !== 'false') {
245+
$this->_subscriberFactory->create()->subscribeCustomerById($customerId);
246+
} else {
247+
$this->_subscriberFactory->create()->unsubscribeCustomerById($customerId);
248+
}
247249
}
248250

249251
// After save
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Test\Unit\Block\Adminhtml\Edit\Tab;
7+
8+
use Magento\Customer\Controller\RegistryConstants;
9+
10+
class NewsletterTest extends \PHPUnit_Framework_TestCase
11+
{
12+
/**
13+
* @var \Magento\Customer\Block\Adminhtml\Edit\Tab\Newsletter
14+
*/
15+
protected $model;
16+
17+
/**
18+
* @var \PHPUnit_Framework_MockObject_MockObject
19+
*/
20+
protected $contextMock;
21+
22+
/**
23+
* @var \PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
protected $registryMock;
26+
27+
/**
28+
* @var \PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
protected $formFactoryMock;
31+
32+
/**
33+
* @var \PHPUnit_Framework_MockObject_MockObject
34+
*/
35+
protected $subscriberFactoryMock;
36+
37+
/**
38+
* @var \PHPUnit_Framework_MockObject_MockObject
39+
*/
40+
protected $accountManagementMock;
41+
42+
/**
43+
* @var \PHPUnit_Framework_MockObject_MockObject
44+
*/
45+
protected $urlBuilderMock;
46+
47+
public function setUp()
48+
{
49+
$this->contextMock = $this->getMock('\Magento\Backend\Block\Template\Context', [], [], '', false);
50+
$this->registryMock = $this->getMock('\Magento\Framework\Registry', [], [], '', false);
51+
$this->formFactoryMock = $this->getMock('\Magento\Framework\Data\FormFactory', [], [], '', false);
52+
$this->subscriberFactoryMock = $this->getMock(
53+
'\Magento\Newsletter\Model\SubscriberFactory',
54+
['create'],
55+
[],
56+
'',
57+
false
58+
);
59+
$this->accountManagementMock = $this->getMock(
60+
'\Magento\Customer\Api\AccountManagementInterface',
61+
[],
62+
[],
63+
'',
64+
false
65+
);
66+
$this->urlBuilderMock = $this->getMock('\Magento\Framework\UrlInterface', [], [], '', false);
67+
$this->contextMock->expects($this->once())->method('getUrlBuilder')->willReturn($this->urlBuilderMock);
68+
69+
$this->model = new \Magento\Customer\Block\Adminhtml\Edit\Tab\Newsletter(
70+
$this->contextMock,
71+
$this->registryMock,
72+
$this->formFactoryMock,
73+
$this->subscriberFactoryMock,
74+
$this->accountManagementMock
75+
);
76+
}
77+
78+
public function testInitFormCanNotShowTab()
79+
{
80+
$this->registryMock->expects($this->once())->method('registry')->with(RegistryConstants::CURRENT_CUSTOMER_ID)
81+
->willReturn(false);
82+
$this->assertSame($this->model, $this->model->initForm());
83+
}
84+
85+
public function testInitForm()
86+
{
87+
$subscriberMock = $this->getMock('\Magento\Newsletter\Model\Subscriber', [], [], '', false);
88+
$fieldsetMock = $this->getMock('\Magento\Framework\Data\Form\Element\Fieldset', [], [], '', false);
89+
$elementMock = $this->getMock('\Magento\Framework\Data\Form\Element\AbstractElement', [], [], '', false);
90+
$formMock = $this->getMock(
91+
'\Magento\Framework\Data\Form',
92+
['setHtmlIdPrefix', 'addFieldset', 'setValues', 'getElement', 'setForm', 'setParent', 'setBaseUrl'],
93+
[],
94+
'',
95+
false
96+
);
97+
$this->registryMock->expects($this->atLeastOnce())->method('registry')->willReturn($subscriberMock);
98+
$this->formFactoryMock->expects($this->once())->method('create')->willReturn($formMock);
99+
$formMock->expects($this->once())->method('setHtmlIdPrefix')->with('_newsletter');
100+
$this->subscriberFactoryMock->expects($this->once())->method('create')->willReturn($subscriberMock);
101+
$subscriberMock->expects($this->once())->method('loadByCustomerId')->with($subscriberMock)->willReturnSelf();
102+
$this->registryMock->expects($this->once())->method('register')->with('subscriber', $subscriberMock);
103+
$formMock->expects($this->once())->method('addFieldset')->willReturn($fieldsetMock);
104+
$this->accountManagementMock->expects($this->once())->method('isReadOnly')->with($subscriberMock)
105+
->willReturn(false);
106+
$subscriberMock->expects($this->once())->method('isSubscribed')->willReturn(true);
107+
$formMock->expects($this->once())->method('getElement')->willReturn($elementMock);
108+
$this->urlBuilderMock->expects($this->once())->method('getBaseUrl')->willReturn('domain.com');
109+
$this->assertSame($this->model, $this->model->initForm());
110+
}
111+
}

0 commit comments

Comments
 (0)