68 private $dataObjectHelper;
73 private $customerFactory;
82 $this->newsletterData = $this->createMock(\
Magento\Newsletter\Helper\Data::class);
83 $this->scopeConfig = $this->createMock(\
Magento\Framework\
App\Config\ScopeConfigInterface::class);
84 $this->transportBuilder = $this->createPartialMock(\
Magento\Framework\Mail\Template\TransportBuilder::class, [
85 'setTemplateIdentifier',
92 $this->storeManager = $this->createMock(\
Magento\Store\Model\StoreManagerInterface::class);
93 $this->customerSession = $this->createPartialMock(\
Magento\Customer\Model\Session::class, [
95 'getCustomerDataObject',
98 $this->customerRepository = $this->createMock(\
Magento\Customer\Api\CustomerRepositoryInterface::class);
99 $this->customerAccountManagement = $this->createMock(\
Magento\Customer\Api\AccountManagementInterface::class);
100 $this->inlineTranslation = $this->createMock(\
Magento\Framework\Translate\
Inline\StateInterface::class);
101 $this->resource = $this->createPartialMock(\
Magento\Newsletter\Model\
ResourceModel\Subscriber::class, [
105 'loadByCustomerData',
108 $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
110 $this->customerFactory = $this->getMockBuilder(\
Magento\Customer\Api\Data\CustomerInterfaceFactory::class)
111 ->setMethods([
'create'])
112 ->disableOriginalConstructor()
114 $this->dataObjectHelper = $this->getMockBuilder(\
Magento\Framework\Api\DataObjectHelper::class)
115 ->disableOriginalConstructor()
118 $this->subscriber = $this->objectManager->getObject(
119 \
Magento\Newsletter\Model\Subscriber::class,
121 'newsletterData' => $this->newsletterData,
122 'scopeConfig' => $this->scopeConfig,
123 'transportBuilder' => $this->transportBuilder,
124 'storeManager' => $this->storeManager,
125 'customerSession' => $this->customerSession,
126 'customerRepository' => $this->customerRepository,
127 'customerAccountManagement' => $this->customerAccountManagement,
128 'inlineTranslation' => $this->inlineTranslation,
129 'resource' => $this->resource,
130 'customerFactory' => $this->customerFactory,
131 'dataObjectHelper' => $this->dataObjectHelper
141 $storeModel = $this->getMockBuilder(\
Magento\Store\Model\Store::class)
142 ->disableOriginalConstructor()
144 $this->storeManager->expects($this->any())->method(
'getStore')->willReturn($storeModel);
145 $storeModel->expects($this->any())->method(
'getId')->willReturn(
$storeId);
146 $customer = $this->createMock(\
Magento\Customer\Api\Data\CustomerInterface::class);
147 $this->customerFactory->expects($this->once())->method(
'create')->willReturn(
$customer);
148 $this->dataObjectHelper->expects($this->once())->method(
'populateWithArray')->with(
151 \
Magento\Customer\Api\Data\CustomerInterface::class
153 $this->resource->expects($this->any())->method(
'loadByCustomerData')->with(
$customer)->willReturn(
156 'subscriber_email' =>
$email,
157 'name' =>
'subscriber_name' 160 $this->scopeConfig->expects($this->any())->method(
'getValue')->willReturn(
true);
161 $this->customerSession->expects($this->any())->method(
'isLoggedIn')->willReturn(
true);
162 $customerDataModel = $this->createMock(\
Magento\Customer\Api\Data\CustomerInterface::class);
163 $this->customerSession->expects($this->any())->method(
'getCustomerDataObject')->willReturn($customerDataModel);
164 $this->customerSession->expects($this->any())->method(
'getCustomerId')->willReturn(1);
165 $customerDataModel->expects($this->any())->method(
'getEmail')->willReturn(
$email);
166 $this->customerRepository->expects($this->any())->method(
'getById')->willReturn($customerDataModel);
167 $customerDataModel->expects($this->any())->method(
'getStoreId')->willReturn(
$storeId);
168 $customerDataModel->expects($this->any())->method(
'getId')->willReturn(1);
170 $this->resource->expects($this->atLeastOnce())->method(
'save')->willReturnSelf();
180 $storeModel = $this->getMockBuilder(\
Magento\Store\Model\Store::class)
181 ->disableOriginalConstructor()
183 $this->storeManager->expects($this->any())->method(
'getStore')->willReturn($storeModel);
184 $storeModel->expects($this->any())->method(
'getId')->willReturn(
$storeId);
185 $customer = $this->createMock(\
Magento\Customer\Api\Data\CustomerInterface::class);
186 $this->customerFactory->expects($this->once())->method(
'create')->willReturn(
$customer);
187 $this->dataObjectHelper->expects($this->once())->method(
'populateWithArray')->with(
190 \
Magento\Customer\Api\Data\CustomerInterface::class
192 $this->resource->expects($this->any())->method(
'loadByCustomerData')->with(
$customer)->willReturn(
195 'subscriber_email' =>
$email,
196 'name' =>
'subscriber_name' 199 $this->scopeConfig->expects($this->any())->method(
'getValue')->willReturn(
true);
200 $this->customerSession->expects($this->any())->method(
'isLoggedIn')->willReturn(
false);
201 $customerDataModel = $this->createMock(\
Magento\Customer\Api\Data\CustomerInterface::class);
202 $this->customerSession->expects($this->any())->method(
'getCustomerDataObject')->willReturn($customerDataModel);
203 $this->customerSession->expects($this->any())->method(
'getCustomerId')->willReturn(1);
204 $customerDataModel->expects($this->any())->method(
'getEmail')->willReturn(
$email);
205 $this->customerRepository->expects($this->any())->method(
'getById')->willReturn($customerDataModel);
206 $customerDataModel->expects($this->any())->method(
'getStoreId')->willReturn(
$storeId);
207 $customerDataModel->expects($this->any())->method(
'getId')->willReturn(1);
209 $this->resource->expects($this->atLeastOnce())->method(
'save')->willReturnSelf();
217 $customerDataMock = $this->getMockBuilder(\
Magento\Customer\Api\Data\CustomerInterface::class)
219 $this->customerRepository->expects($this->atLeastOnce())
221 ->with(
$customerId)->willReturn($customerDataMock);
222 $this->resource->expects($this->atLeastOnce())
223 ->method(
'loadByCustomerData')
224 ->with($customerDataMock)
227 'subscriber_id' => 1,
231 $customerDataMock->expects($this->atLeastOnce())->method(
'getId')->willReturn(
'id');
232 $this->resource->expects($this->atLeastOnce())->method(
'save')->willReturnSelf();
233 $this->customerAccountManagement->expects($this->once())
234 ->method(
'getConfirmationStatus')
236 ->willReturn(
'account_confirmation_required');
237 $customerDataMock->expects($this->once())->method(
'getStoreId')->willReturn(
'store_id');
238 $customerDataMock->expects($this->once())->method(
'getEmail')->willReturn(
'email');
240 $storeModel = $this->getMockBuilder(\
Magento\Store\Model\Store::class)
241 ->disableOriginalConstructor()
242 ->setMethods([
'getId'])
244 $this->storeManager->expects($this->any())->method(
'getStore')->willReturn($storeModel);
246 $this->assertEquals($this->subscriber, $this->subscriber->updateSubscription(
$customerId));
252 $customerDataMock = $this->getMockBuilder(\
Magento\Customer\Api\Data\CustomerInterface::class)
254 $this->customerRepository->expects($this->atLeastOnce())
256 ->with(
$customerId)->willReturn($customerDataMock);
257 $this->resource->expects($this->atLeastOnce())
258 ->method(
'loadByCustomerData')
259 ->with($customerDataMock)
262 'subscriber_id' => 1,
266 $customerDataMock->expects($this->atLeastOnce())->method(
'getId')->willReturn(
'id');
267 $this->resource->expects($this->atLeastOnce())->method(
'save')->willReturnSelf();
268 $customerDataMock->expects($this->once())->method(
'getStoreId')->willReturn(
'store_id');
269 $customerDataMock->expects($this->once())->method(
'getEmail')->willReturn(
'email');
272 $this->subscriber->unsubscribeCustomerById(
$customerId);
278 $customerDataMock = $this->getMockBuilder(\
Magento\Customer\Api\Data\CustomerInterface::class)
280 $this->customerRepository->expects($this->atLeastOnce())
282 ->with(
$customerId)->willReturn($customerDataMock);
283 $this->resource->expects($this->atLeastOnce())
284 ->method(
'loadByCustomerData')
285 ->with($customerDataMock)
288 'subscriber_id' => 1,
292 $customerDataMock->expects($this->atLeastOnce())->method(
'getId')->willReturn(
'id');
293 $this->resource->expects($this->atLeastOnce())->method(
'save')->willReturnSelf();
294 $customerDataMock->expects($this->once())->method(
'getStoreId')->willReturn(
'store_id');
295 $customerDataMock->expects($this->once())->method(
'getEmail')->willReturn(
'email');
298 $this->subscriber->subscribeCustomerById(
$customerId);
304 $customerDataMock = $this->getMockBuilder(\
Magento\Customer\Api\Data\CustomerInterface::class)
306 $this->customerRepository->expects($this->atLeastOnce())
308 ->with(
$customerId)->willReturn($customerDataMock);
309 $this->resource->expects($this->atLeastOnce())
310 ->method(
'loadByCustomerData')
311 ->with($customerDataMock)
314 'subscriber_id' => 1,
318 $customerDataMock->expects($this->atLeastOnce())->method(
'getId')->willReturn(
'id');
319 $this->resource->expects($this->atLeastOnce())->method(
'save')->willReturnSelf();
320 $customerDataMock->expects($this->once())->method(
'getStoreId')->willReturn(
'store_id');
321 $customerDataMock->expects($this->once())->method(
'getEmail')->willReturn(
'email');
323 $this->customerAccountManagement->expects($this->once())
324 ->method(
'getConfirmationStatus')
325 ->willReturn(\
Magento\Customer\Api\AccountManagementInterface::ACCOUNT_CONFIRMATION_NOT_REQUIRED);
326 $this->scopeConfig->expects($this->atLeastOnce())->method(
'getValue')->with()->willReturn(
true);
328 $this->subscriber->subscribeCustomerById(
$customerId);
335 $customerDataMock = $this->getMockBuilder(\
Magento\Customer\Api\Data\CustomerInterface::class)
337 $this->customerRepository->expects($this->atLeastOnce())
339 ->with(
$customerId)->willReturn($customerDataMock);
340 $this->resource->expects($this->atLeastOnce())
341 ->method(
'loadByCustomerData')
342 ->with($customerDataMock)
345 'subscriber_id' => 1,
349 $customerDataMock->expects($this->atLeastOnce())->method(
'getId')->willReturn(
'id');
350 $this->resource->expects($this->atLeastOnce())->method(
'save')->willReturnSelf();
351 $customerDataMock->expects($this->once())->method(
'getStoreId')->willReturn(
'store_id');
352 $customerDataMock->expects($this->once())->method(
'getEmail')->willReturn(
'email');
354 $this->customerAccountManagement->expects($this->never())->method(
'getConfirmationStatus');
355 $this->scopeConfig->expects($this->atLeastOnce())->method(
'getValue')->with()->willReturn(
true);
357 $this->subscriber->updateSubscription(
$customerId);
363 $this->resource->expects($this->once())->method(
'save')->willReturnSelf();
366 $this->assertEquals($this->subscriber, $this->subscriber->unsubscribe());
375 $this->subscriber->setCode(111);
376 $this->subscriber->setCheckCode(222);
378 $this->subscriber->unsubscribe();
383 $this->subscriber->setFirstname(
'John');
384 $this->subscriber->setLastname(
'Doe');
386 $this->assertEquals(
'John Doe', $this->subscriber->getSubscriberFullName());
392 $this->subscriber->setCode(
$code);
393 $this->resource->expects($this->once())->method(
'save')->willReturnSelf();
395 $this->assertTrue($this->subscriber->confirm(
$code));
401 $this->subscriber->setCode(222);
403 $this->assertFalse($this->subscriber->confirm(
$code));
408 $queue = $this->getMockBuilder(\
Magento\Newsletter\Model\Queue::class)
409 ->disableOriginalConstructor()
411 $this->resource->expects($this->once())->method(
'received')->with($this->subscriber,
$queue)->willReturnSelf();
413 $this->assertEquals($this->subscriber, $this->subscriber->received(
$queue));
421 $storeModel = $this->getMockBuilder(\
Magento\Store\Model\Store::class)
422 ->disableOriginalConstructor()
423 ->setMethods([
'getId'])
425 $transport = $this->createMock(\
Magento\Framework\Mail\TransportInterface::class);
426 $this->scopeConfig->expects($this->any())->method(
'getValue')->willReturn(
true);
427 $this->transportBuilder->expects($this->once())->method(
'setTemplateIdentifier')->willReturnSelf();
428 $this->transportBuilder->expects($this->once())->method(
'setTemplateOptions')->willReturnSelf();
429 $this->transportBuilder->expects($this->once())->method(
'setTemplateVars')->willReturnSelf();
430 $this->transportBuilder->expects($this->once())->method(
'setFrom')->willReturnSelf();
431 $this->transportBuilder->expects($this->once())->method(
'addTo')->willReturnSelf();
432 $this->storeManager->expects($this->any())->method(
'getStore')->willReturn($storeModel);
433 $storeModel->expects($this->any())->method(
'getId')->willReturn(1);
434 $this->transportBuilder->expects($this->once())->method(
'getTransport')->willReturn($transport);
435 $transport->expects($this->once())->method(
'sendMessage')->willReturnSelf();
436 $this->inlineTranslation->expects($this->once())->method(
'suspend')->willReturnSelf();
437 $this->inlineTranslation->expects($this->once())->method(
'resume')->willReturnSelf();
testUnsubscribeException()
testGetSubscriberFullName()
testSubscribeCustomerById1()
testSubscribeCustomerByIdAfterConfirmation()
testSubscribeCustomerById()
testUnsubscribeCustomerById()
const STATUS_UNSUBSCRIBED
testSubscribeNotLoggedIn()
$customerAccountManagement