Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions app/code/Magento/Newsletter/Model/Plugin/CustomerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
namespace Magento\Newsletter\Model\Plugin;

use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
use Magento\Customer\Api\Data\CustomerExtensionInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Newsletter\Model\SubscriberFactory;
use Magento\Framework\Api\ExtensionAttributesFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Newsletter\Model\ResourceModel\Subscriber;
use Magento\Customer\Api\Data\CustomerExtensionInterface;
use Magento\Newsletter\Model\SubscriberFactory;
use Magento\Store\Model\StoreManagerInterface;

class CustomerPlugin
{
Expand All @@ -36,21 +38,29 @@ class CustomerPlugin
*/
private $customerSubscriptionStatus = [];

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* Initialize dependencies.
*
* @param SubscriberFactory $subscriberFactory
* @param ExtensionAttributesFactory $extensionFactory
* @param Subscriber $subscriberResource
* @param StoreManagerInterface|null $storeManager
*/
public function __construct(
SubscriberFactory $subscriberFactory,
ExtensionAttributesFactory $extensionFactory,
Subscriber $subscriberResource
Subscriber $subscriberResource,
StoreManagerInterface $storeManager = null
) {
$this->subscriberFactory = $subscriberFactory;
$this->extensionFactory = $extensionFactory;
$this->subscriberResource = $subscriberResource;
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
}

/**
Expand Down Expand Up @@ -151,7 +161,8 @@ public function afterDelete(CustomerRepository $subject, $result, CustomerInterf
public function afterGetById(CustomerRepository $subject, CustomerInterface $customer)
{
$extensionAttributes = $customer->getExtensionAttributes();

$storeId = $this->storeManager->getStore()->getId();
$customer->setStoreId($storeId);
if ($extensionAttributes === null) {
/** @var CustomerExtensionInterface $extensionAttributes */
$extensionAttributes = $this->extensionFactory->create(CustomerInterface::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Magento\Customer\Api\Data\CustomerExtensionInterface;
use Magento\Framework\Api\ExtensionAttributesFactory;
use Magento\Newsletter\Model\ResourceModel\Subscriber;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;

class CustomerPluginTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -53,6 +55,11 @@ class CustomerPluginTest extends \PHPUnit\Framework\TestCase
*/
private $customerMock;

/**
* @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $storeManagerMock;

protected function setUp()
{
$this->subscriberFactory = $this->getMockBuilder(\Magento\Newsletter\Model\SubscriberFactory::class)
Expand Down Expand Up @@ -87,6 +94,8 @@ protected function setUp()
->setMethods(['getExtensionAttributes'])
->disableOriginalConstructor()
->getMockForAbstractClass();
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class);

$this->subscriberFactory->expects($this->any())->method('create')->willReturn($this->subscriber);
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);

Expand All @@ -96,6 +105,7 @@ protected function setUp()
'subscriberFactory' => $this->subscriberFactory,
'extensionFactory' => $this->extensionFactoryMock,
'subscriberResource' => $this->subscriberResourceMock,
'storeManager' => $this->storeManagerMock,
]
);
}
Expand Down Expand Up @@ -206,6 +216,7 @@ public function testAfterGetByIdCreatesExtensionAttributesIfItIsNotSet(
) {
$subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
$subscriber = [$subscriberStatusKey => $subscriberStatusValue];
$this->prepareStoreData();

$this->extensionFactoryMock->expects($this->any())
->method('create')
Expand Down Expand Up @@ -233,6 +244,7 @@ public function testAfterGetByIdSetsIsSubscribedFlagIfItIsNotSet()
{
$subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
$subscriber = ['subscriber_id' => 1, 'subscriber_status' => 1];
$this->prepareStoreData();

$this->customerMock->expects($this->any())
->method('getExtensionAttributes')
Expand Down Expand Up @@ -267,4 +279,17 @@ public function afterGetByIdDataProvider()
[null, null, false],
];
}

/**
* Prepare store information
*
* @return void
*/
private function prepareStoreData()
{
$storeId = 1;
$storeMock = $this->createMock(Store::class);
$storeMock->expects($this->any())->method('getId')->willReturn($storeId);
$this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
}
}