Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SubscriberTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class SubscriberTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $newsletterData;
19 
23  protected $scopeConfig;
24 
28  protected $transportBuilder;
29 
33  protected $storeManager;
34 
38  protected $customerSession;
39 
44 
49 
53  protected $inlineTranslation;
54 
58  protected $resource;
59 
63  protected $objectManager;
64 
68  private $dataObjectHelper;
69 
73  private $customerFactory;
74 
78  protected $subscriber;
79 
80  protected function setUp()
81  {
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',
86  'setTemplateOptions',
87  'setTemplateVars',
88  'setFrom',
89  'addTo',
90  'getTransport'
91  ]);
92  $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
93  $this->customerSession = $this->createPartialMock(\Magento\Customer\Model\Session::class, [
94  'isLoggedIn',
95  'getCustomerDataObject',
96  'getCustomerId'
97  ]);
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, [
102  'loadByEmail',
103  'getIdFieldName',
104  'save',
105  'loadByCustomerData',
106  'received'
107  ]);
108  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
109 
110  $this->customerFactory = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterfaceFactory::class)
111  ->setMethods(['create'])
112  ->disableOriginalConstructor()
113  ->getMock();
114  $this->dataObjectHelper = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
115  ->disableOriginalConstructor()
116  ->getMock();
117 
118  $this->subscriber = $this->objectManager->getObject(
119  \Magento\Newsletter\Model\Subscriber::class,
120  [
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
132  ]
133  );
134  }
135 
136  public function testSubscribe()
137  {
139  $storeId = 1;
140  $customerData = ['store_id' => $storeId, 'email' => $email];
141  $storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
142  ->disableOriginalConstructor()
143  ->getMock();
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(
149  $customer,
151  \Magento\Customer\Api\Data\CustomerInterface::class
152  );
153  $this->resource->expects($this->any())->method('loadByCustomerData')->with($customer)->willReturn(
154  [
155  'subscriber_status' => Subscriber::STATUS_UNSUBSCRIBED,
156  'subscriber_email' => $email,
157  'name' => 'subscriber_name'
158  ]
159  );
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);
169  $this->sendEmailCheck();
170  $this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
171 
172  $this->assertEquals(Subscriber::STATUS_NOT_ACTIVE, $this->subscriber->subscribe($email));
173  }
174 
175  public function testSubscribeNotLoggedIn()
176  {
178  $storeId = 1;
179  $customerData = ['store_id' => $storeId, 'email' => $email];
180  $storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
181  ->disableOriginalConstructor()
182  ->getMock();
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(
188  $customer,
190  \Magento\Customer\Api\Data\CustomerInterface::class
191  );
192  $this->resource->expects($this->any())->method('loadByCustomerData')->with($customer)->willReturn(
193  [
194  'subscriber_status' => Subscriber::STATUS_UNSUBSCRIBED,
195  'subscriber_email' => $email,
196  'name' => 'subscriber_name'
197  ]
198  );
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);
208  $this->sendEmailCheck();
209  $this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
210 
211  $this->assertEquals(Subscriber::STATUS_NOT_ACTIVE, $this->subscriber->subscribe($email));
212  }
213 
214  public function testUpdateSubscription()
215  {
216  $customerId = 1;
217  $customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
218  ->getMock();
219  $this->customerRepository->expects($this->atLeastOnce())
220  ->method('getById')
221  ->with($customerId)->willReturn($customerDataMock);
222  $this->resource->expects($this->atLeastOnce())
223  ->method('loadByCustomerData')
224  ->with($customerDataMock)
225  ->willReturn(
226  [
227  'subscriber_id' => 1,
228  'subscriber_status' => Subscriber::STATUS_SUBSCRIBED
229  ]
230  );
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')
235  ->with($customerId)
236  ->willReturn('account_confirmation_required');
237  $customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
238  $customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
239 
240  $storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
241  ->disableOriginalConstructor()
242  ->setMethods(['getId'])
243  ->getMock();
244  $this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel);
245 
246  $this->assertEquals($this->subscriber, $this->subscriber->updateSubscription($customerId));
247  }
248 
249  public function testUnsubscribeCustomerById()
250  {
251  $customerId = 1;
252  $customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
253  ->getMock();
254  $this->customerRepository->expects($this->atLeastOnce())
255  ->method('getById')
256  ->with($customerId)->willReturn($customerDataMock);
257  $this->resource->expects($this->atLeastOnce())
258  ->method('loadByCustomerData')
259  ->with($customerDataMock)
260  ->willReturn(
261  [
262  'subscriber_id' => 1,
263  'subscriber_status' => Subscriber::STATUS_SUBSCRIBED
264  ]
265  );
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');
270  $this->sendEmailCheck();
271 
272  $this->subscriber->unsubscribeCustomerById($customerId);
273  }
274 
275  public function testSubscribeCustomerById()
276  {
277  $customerId = 1;
278  $customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
279  ->getMock();
280  $this->customerRepository->expects($this->atLeastOnce())
281  ->method('getById')
282  ->with($customerId)->willReturn($customerDataMock);
283  $this->resource->expects($this->atLeastOnce())
284  ->method('loadByCustomerData')
285  ->with($customerDataMock)
286  ->willReturn(
287  [
288  'subscriber_id' => 1,
289  'subscriber_status' => Subscriber::STATUS_UNSUBSCRIBED
290  ]
291  );
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');
296  $this->sendEmailCheck();
297 
298  $this->subscriber->subscribeCustomerById($customerId);
299  }
300 
301  public function testSubscribeCustomerById1()
302  {
303  $customerId = 1;
304  $customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
305  ->getMock();
306  $this->customerRepository->expects($this->atLeastOnce())
307  ->method('getById')
308  ->with($customerId)->willReturn($customerDataMock);
309  $this->resource->expects($this->atLeastOnce())
310  ->method('loadByCustomerData')
311  ->with($customerDataMock)
312  ->willReturn(
313  [
314  'subscriber_id' => 1,
315  'subscriber_status' => Subscriber::STATUS_UNSUBSCRIBED
316  ]
317  );
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');
322  $this->sendEmailCheck();
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);
327 
328  $this->subscriber->subscribeCustomerById($customerId);
329  $this->assertEquals(Subscriber::STATUS_NOT_ACTIVE, $this->subscriber->getStatus());
330  }
331 
333  {
334  $customerId = 1;
335  $customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
336  ->getMock();
337  $this->customerRepository->expects($this->atLeastOnce())
338  ->method('getById')
339  ->with($customerId)->willReturn($customerDataMock);
340  $this->resource->expects($this->atLeastOnce())
341  ->method('loadByCustomerData')
342  ->with($customerDataMock)
343  ->willReturn(
344  [
345  'subscriber_id' => 1,
346  'subscriber_status' => Subscriber::STATUS_UNCONFIRMED
347  ]
348  );
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');
353  $this->sendEmailCheck();
354  $this->customerAccountManagement->expects($this->never())->method('getConfirmationStatus');
355  $this->scopeConfig->expects($this->atLeastOnce())->method('getValue')->with()->willReturn(true);
356 
357  $this->subscriber->updateSubscription($customerId);
358  $this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $this->subscriber->getStatus());
359  }
360 
361  public function testUnsubscribe()
362  {
363  $this->resource->expects($this->once())->method('save')->willReturnSelf();
364  $this->sendEmailCheck();
365 
366  $this->assertEquals($this->subscriber, $this->subscriber->unsubscribe());
367  }
368 
373  public function testUnsubscribeException()
374  {
375  $this->subscriber->setCode(111);
376  $this->subscriber->setCheckCode(222);
377 
378  $this->subscriber->unsubscribe();
379  }
380 
381  public function testGetSubscriberFullName()
382  {
383  $this->subscriber->setFirstname('John');
384  $this->subscriber->setLastname('Doe');
385 
386  $this->assertEquals('John Doe', $this->subscriber->getSubscriberFullName());
387  }
388 
389  public function testConfirm()
390  {
391  $code = 111;
392  $this->subscriber->setCode($code);
393  $this->resource->expects($this->once())->method('save')->willReturnSelf();
394 
395  $this->assertTrue($this->subscriber->confirm($code));
396  }
397 
398  public function testConfirmWrongCode()
399  {
400  $code = 111;
401  $this->subscriber->setCode(222);
402 
403  $this->assertFalse($this->subscriber->confirm($code));
404  }
405 
406  public function testReceived()
407  {
408  $queue = $this->getMockBuilder(\Magento\Newsletter\Model\Queue::class)
409  ->disableOriginalConstructor()
410  ->getMock();
411  $this->resource->expects($this->once())->method('received')->with($this->subscriber, $queue)->willReturnSelf();
412 
413  $this->assertEquals($this->subscriber, $this->subscriber->received($queue));
414  }
415 
419  protected function sendEmailCheck()
420  {
421  $storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
422  ->disableOriginalConstructor()
423  ->setMethods(['getId'])
424  ->getMock();
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();
438 
439  return $this;
440  }
441 }
$queue
Definition: queue.php:21
$customerData
$customer
Definition: customers.php:11
$email
Definition: details.phtml:13
$code
Definition: info.phtml:12