Skip to content

Commit

Permalink
Merge pull request #460 from magento-vanilla/PR
Browse files Browse the repository at this point in the history
[Vanilla] Bugs
  • Loading branch information
vpelipenko committed Jul 14, 2015
2 parents 5eb3c9a + 5d831fe commit 375077a
Show file tree
Hide file tree
Showing 5 changed files with 1,120 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,25 @@ public function initForm()
[
'label' => __('Subscribed to Newsletter'),
'name' => 'subscription',
'data-form-part' => $this->getData('target_form')
'data-form-part' => $this->getData('target_form'),
'onchange' => 'this.value = this.checked;'
]
);

if ($this->customerAccountManagement->isReadOnly($customerId)) {
$form->getElement('subscription')->setReadonly(true, true);
}

$form->getElement('subscription')->setIsChecked($subscriber->isSubscribed());
$isSubscribed = $subscriber->isSubscribed();
$form->setValues(['subscription' => $isSubscribed ? 'true' : 'false']);
$form->getElement('subscription')->setIsChecked($isSubscribed);

$changedDate = $this->getStatusChangedDate();
if ($changedDate) {
$fieldset->addField(
'change_status_date',
'label',
[
'label' => $subscriber->isSubscribed() ? __('Last Date Subscribed') : __('Last Date Unsubscribed'),
'label' => $isSubscribed ? __('Last Date Subscribed') : __('Last Date Unsubscribed'),
'value' => $changedDate,
'bold' => true
]
Expand Down
16 changes: 9 additions & 7 deletions app/code/Magento/Customer/Controller/Adminhtml/Index/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function _extractData(
\Magento\Customer\Model\Metadata\Form $metadataForm = null
) {
if ($metadataForm === null) {
$metadataForm = $this->_objectManager->get('Magento\Customer\Model\Metadata\FormFactory')->create(
$metadataForm = $this->_formFactory->create(
$entityType,
$formCode,
[],
Expand Down Expand Up @@ -236,14 +236,16 @@ public function execute()
$customerId = $customer->getId();
}

$isSubscribed = false;
$isSubscribed = null;
if ($this->_authorization->isAllowed(null)) {
$isSubscribed = $this->getRequest()->getPost('subscription') !== null;
$isSubscribed = $this->getRequest()->getPost('subscription');
}
if ($isSubscribed) {
$this->_subscriberFactory->create()->subscribeCustomerById($customerId);
} else {
$this->_subscriberFactory->create()->unsubscribeCustomerById($customerId);
if ($isSubscribed !== null) {
if ($isSubscribed !== 'false') {
$this->_subscriberFactory->create()->subscribeCustomerById($customerId);
} else {
$this->_subscriberFactory->create()->unsubscribeCustomerById($customerId);
}
}

// After save
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Test\Unit\Block\Adminhtml\Edit\Tab;

use Magento\Customer\Controller\RegistryConstants;

class NewsletterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Customer\Block\Adminhtml\Edit\Tab\Newsletter
*/
protected $model;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $contextMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $registryMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $formFactoryMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $subscriberFactoryMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $accountManagementMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $urlBuilderMock;

public function setUp()
{
$this->contextMock = $this->getMock('\Magento\Backend\Block\Template\Context', [], [], '', false);
$this->registryMock = $this->getMock('\Magento\Framework\Registry', [], [], '', false);
$this->formFactoryMock = $this->getMock('\Magento\Framework\Data\FormFactory', [], [], '', false);
$this->subscriberFactoryMock = $this->getMock(
'\Magento\Newsletter\Model\SubscriberFactory',
['create'],
[],
'',
false
);
$this->accountManagementMock = $this->getMock(
'\Magento\Customer\Api\AccountManagementInterface',
[],
[],
'',
false
);
$this->urlBuilderMock = $this->getMock('\Magento\Framework\UrlInterface', [], [], '', false);
$this->contextMock->expects($this->once())->method('getUrlBuilder')->willReturn($this->urlBuilderMock);

$this->model = new \Magento\Customer\Block\Adminhtml\Edit\Tab\Newsletter(
$this->contextMock,
$this->registryMock,
$this->formFactoryMock,
$this->subscriberFactoryMock,
$this->accountManagementMock
);
}

public function testInitFormCanNotShowTab()
{
$this->registryMock->expects($this->once())->method('registry')->with(RegistryConstants::CURRENT_CUSTOMER_ID)
->willReturn(false);
$this->assertSame($this->model, $this->model->initForm());
}

public function testInitForm()
{
$subscriberMock = $this->getMock('\Magento\Newsletter\Model\Subscriber', [], [], '', false);
$fieldsetMock = $this->getMock('\Magento\Framework\Data\Form\Element\Fieldset', [], [], '', false);
$elementMock = $this->getMock('\Magento\Framework\Data\Form\Element\AbstractElement', [], [], '', false);
$formMock = $this->getMock(
'\Magento\Framework\Data\Form',
['setHtmlIdPrefix', 'addFieldset', 'setValues', 'getElement', 'setForm', 'setParent', 'setBaseUrl'],
[],
'',
false
);
$this->registryMock->expects($this->atLeastOnce())->method('registry')->willReturn($subscriberMock);
$this->formFactoryMock->expects($this->once())->method('create')->willReturn($formMock);
$formMock->expects($this->once())->method('setHtmlIdPrefix')->with('_newsletter');
$this->subscriberFactoryMock->expects($this->once())->method('create')->willReturn($subscriberMock);
$subscriberMock->expects($this->once())->method('loadByCustomerId')->with($subscriberMock)->willReturnSelf();
$this->registryMock->expects($this->once())->method('register')->with('subscriber', $subscriberMock);
$formMock->expects($this->once())->method('addFieldset')->willReturn($fieldsetMock);
$this->accountManagementMock->expects($this->once())->method('isReadOnly')->with($subscriberMock)
->willReturn(false);
$subscriberMock->expects($this->once())->method('isSubscribed')->willReturn(true);
$formMock->expects($this->once())->method('getElement')->willReturn($elementMock);
$this->urlBuilderMock->expects($this->once())->method('getBaseUrl')->willReturn('domain.com');
$this->assertSame($this->model, $this->model->initForm());
}
}
Loading

0 comments on commit 375077a

Please sign in to comment.