SubscriberTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Newsletter\Test\Unit\Model;
  7. use Magento\Newsletter\Model\Subscriber;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class SubscriberTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Newsletter\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $newsletterData;
  17. /**
  18. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $scopeConfig;
  21. /**
  22. * @var \Magento\Framework\Mail\Template\TransportBuilder|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $transportBuilder;
  25. /**
  26. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $storeManager;
  29. /**
  30. * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $customerSession;
  33. /**
  34. * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $customerRepository;
  37. /**
  38. * @var \Magento\Customer\Api\AccountManagementInterface|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $customerAccountManagement;
  41. /**
  42. * @var \Magento\Framework\Translate\Inline\StateInterface|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $inlineTranslation;
  45. /**
  46. * @var \Magento\Newsletter\Model\ResourceModel\Subscriber|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. protected $resource;
  49. /**
  50. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  51. */
  52. protected $objectManager;
  53. /**
  54. * @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
  55. */
  56. private $dataObjectHelper;
  57. /**
  58. * @var \Magento\Customer\Api\Data\CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  59. */
  60. private $customerFactory;
  61. /**
  62. * @var \Magento\Newsletter\Model\Subscriber
  63. */
  64. protected $subscriber;
  65. protected function setUp()
  66. {
  67. $this->newsletterData = $this->createMock(\Magento\Newsletter\Helper\Data::class);
  68. $this->scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  69. $this->transportBuilder = $this->createPartialMock(\Magento\Framework\Mail\Template\TransportBuilder::class, [
  70. 'setTemplateIdentifier',
  71. 'setTemplateOptions',
  72. 'setTemplateVars',
  73. 'setFrom',
  74. 'addTo',
  75. 'getTransport'
  76. ]);
  77. $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  78. $this->customerSession = $this->createPartialMock(\Magento\Customer\Model\Session::class, [
  79. 'isLoggedIn',
  80. 'getCustomerDataObject',
  81. 'getCustomerId'
  82. ]);
  83. $this->customerRepository = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
  84. $this->customerAccountManagement = $this->createMock(\Magento\Customer\Api\AccountManagementInterface::class);
  85. $this->inlineTranslation = $this->createMock(\Magento\Framework\Translate\Inline\StateInterface::class);
  86. $this->resource = $this->createPartialMock(\Magento\Newsletter\Model\ResourceModel\Subscriber::class, [
  87. 'loadByEmail',
  88. 'getIdFieldName',
  89. 'save',
  90. 'loadByCustomerData',
  91. 'received'
  92. ]);
  93. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  94. $this->customerFactory = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterfaceFactory::class)
  95. ->setMethods(['create'])
  96. ->disableOriginalConstructor()
  97. ->getMock();
  98. $this->dataObjectHelper = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
  99. ->disableOriginalConstructor()
  100. ->getMock();
  101. $this->subscriber = $this->objectManager->getObject(
  102. \Magento\Newsletter\Model\Subscriber::class,
  103. [
  104. 'newsletterData' => $this->newsletterData,
  105. 'scopeConfig' => $this->scopeConfig,
  106. 'transportBuilder' => $this->transportBuilder,
  107. 'storeManager' => $this->storeManager,
  108. 'customerSession' => $this->customerSession,
  109. 'customerRepository' => $this->customerRepository,
  110. 'customerAccountManagement' => $this->customerAccountManagement,
  111. 'inlineTranslation' => $this->inlineTranslation,
  112. 'resource' => $this->resource,
  113. 'customerFactory' => $this->customerFactory,
  114. 'dataObjectHelper' => $this->dataObjectHelper
  115. ]
  116. );
  117. }
  118. public function testSubscribe()
  119. {
  120. $email = 'subscriber_email@magento.com';
  121. $storeId = 1;
  122. $customerData = ['store_id' => $storeId, 'email' => $email];
  123. $storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  124. ->disableOriginalConstructor()
  125. ->getMock();
  126. $this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel);
  127. $storeModel->expects($this->any())->method('getId')->willReturn($storeId);
  128. $customer = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  129. $this->customerFactory->expects($this->once())->method('create')->willReturn($customer);
  130. $this->dataObjectHelper->expects($this->once())->method('populateWithArray')->with(
  131. $customer,
  132. $customerData,
  133. \Magento\Customer\Api\Data\CustomerInterface::class
  134. );
  135. $this->resource->expects($this->any())->method('loadByCustomerData')->with($customer)->willReturn(
  136. [
  137. 'subscriber_status' => Subscriber::STATUS_UNSUBSCRIBED,
  138. 'subscriber_email' => $email,
  139. 'name' => 'subscriber_name'
  140. ]
  141. );
  142. $this->scopeConfig->expects($this->any())->method('getValue')->willReturn(true);
  143. $this->customerSession->expects($this->any())->method('isLoggedIn')->willReturn(true);
  144. $customerDataModel = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  145. $this->customerSession->expects($this->any())->method('getCustomerDataObject')->willReturn($customerDataModel);
  146. $this->customerSession->expects($this->any())->method('getCustomerId')->willReturn(1);
  147. $customerDataModel->expects($this->any())->method('getEmail')->willReturn($email);
  148. $this->customerRepository->expects($this->any())->method('getById')->willReturn($customerDataModel);
  149. $customerDataModel->expects($this->any())->method('getStoreId')->willReturn($storeId);
  150. $customerDataModel->expects($this->any())->method('getId')->willReturn(1);
  151. $this->sendEmailCheck();
  152. $this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
  153. $this->assertEquals(Subscriber::STATUS_NOT_ACTIVE, $this->subscriber->subscribe($email));
  154. }
  155. public function testSubscribeNotLoggedIn()
  156. {
  157. $email = 'subscriber_email@magento.com';
  158. $storeId = 1;
  159. $customerData = ['store_id' => $storeId, 'email' => $email];
  160. $storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  161. ->disableOriginalConstructor()
  162. ->getMock();
  163. $this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel);
  164. $storeModel->expects($this->any())->method('getId')->willReturn($storeId);
  165. $customer = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  166. $this->customerFactory->expects($this->once())->method('create')->willReturn($customer);
  167. $this->dataObjectHelper->expects($this->once())->method('populateWithArray')->with(
  168. $customer,
  169. $customerData,
  170. \Magento\Customer\Api\Data\CustomerInterface::class
  171. );
  172. $this->resource->expects($this->any())->method('loadByCustomerData')->with($customer)->willReturn(
  173. [
  174. 'subscriber_status' => Subscriber::STATUS_UNSUBSCRIBED,
  175. 'subscriber_email' => $email,
  176. 'name' => 'subscriber_name'
  177. ]
  178. );
  179. $this->scopeConfig->expects($this->any())->method('getValue')->willReturn(true);
  180. $this->customerSession->expects($this->any())->method('isLoggedIn')->willReturn(false);
  181. $customerDataModel = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  182. $this->customerSession->expects($this->any())->method('getCustomerDataObject')->willReturn($customerDataModel);
  183. $this->customerSession->expects($this->any())->method('getCustomerId')->willReturn(1);
  184. $customerDataModel->expects($this->any())->method('getEmail')->willReturn($email);
  185. $this->customerRepository->expects($this->any())->method('getById')->willReturn($customerDataModel);
  186. $customerDataModel->expects($this->any())->method('getStoreId')->willReturn($storeId);
  187. $customerDataModel->expects($this->any())->method('getId')->willReturn(1);
  188. $this->sendEmailCheck();
  189. $this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
  190. $this->assertEquals(Subscriber::STATUS_NOT_ACTIVE, $this->subscriber->subscribe($email));
  191. }
  192. public function testUpdateSubscription()
  193. {
  194. $storeId = 2;
  195. $customerId = 1;
  196. $customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  197. ->getMock();
  198. $this->customerRepository->expects($this->atLeastOnce())
  199. ->method('getById')
  200. ->with($customerId)->willReturn($customerDataMock);
  201. $this->resource->expects($this->atLeastOnce())
  202. ->method('loadByCustomerData')
  203. ->with($customerDataMock)
  204. ->willReturn(
  205. [
  206. 'subscriber_id' => 1,
  207. 'subscriber_status' => Subscriber::STATUS_SUBSCRIBED
  208. ]
  209. );
  210. $customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
  211. $this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
  212. $this->customerAccountManagement->expects($this->once())
  213. ->method('getConfirmationStatus')
  214. ->with($customerId)
  215. ->willReturn('account_confirmation_required');
  216. $customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
  217. $customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
  218. $storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  219. ->disableOriginalConstructor()
  220. ->setMethods(['getId'])
  221. ->getMock();
  222. $this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel);
  223. $this->assertEquals($this->subscriber, $this->subscriber->updateSubscription($customerId));
  224. }
  225. public function testUnsubscribeCustomerById()
  226. {
  227. $storeId = 2;
  228. $customerId = 1;
  229. $customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  230. ->getMock();
  231. $this->customerRepository->expects($this->atLeastOnce())
  232. ->method('getById')
  233. ->with($customerId)->willReturn($customerDataMock);
  234. $this->resource->expects($this->atLeastOnce())
  235. ->method('loadByCustomerData')
  236. ->with($customerDataMock)
  237. ->willReturn(
  238. [
  239. 'subscriber_id' => 1,
  240. 'subscriber_status' => Subscriber::STATUS_SUBSCRIBED
  241. ]
  242. );
  243. $customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
  244. $this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
  245. $customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
  246. $customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
  247. $this->sendEmailCheck();
  248. $this->subscriber->unsubscribeCustomerById($customerId);
  249. }
  250. public function testSubscribeCustomerById()
  251. {
  252. $storeId = 2;
  253. $customerId = 1;
  254. $customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  255. ->getMock();
  256. $this->customerRepository->expects($this->atLeastOnce())
  257. ->method('getById')
  258. ->with($customerId)->willReturn($customerDataMock);
  259. $this->resource->expects($this->atLeastOnce())
  260. ->method('loadByCustomerData')
  261. ->with($customerDataMock)
  262. ->willReturn(
  263. [
  264. 'subscriber_id' => 1,
  265. 'subscriber_status' => Subscriber::STATUS_UNSUBSCRIBED
  266. ]
  267. );
  268. $customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
  269. $this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
  270. $customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
  271. $customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
  272. $this->sendEmailCheck();
  273. $this->subscriber->subscribeCustomerById($customerId);
  274. }
  275. public function testSubscribeCustomerById1()
  276. {
  277. $storeId = 2;
  278. $customerId = 1;
  279. $customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  280. ->getMock();
  281. $this->customerRepository->expects($this->atLeastOnce())
  282. ->method('getById')
  283. ->with($customerId)->willReturn($customerDataMock);
  284. $this->resource->expects($this->atLeastOnce())
  285. ->method('loadByCustomerData')
  286. ->with($customerDataMock)
  287. ->willReturn(
  288. [
  289. 'subscriber_id' => 1,
  290. 'subscriber_status' => Subscriber::STATUS_UNSUBSCRIBED
  291. ]
  292. );
  293. $customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
  294. $this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
  295. $customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
  296. $customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
  297. $this->sendEmailCheck();
  298. $this->customerAccountManagement->expects($this->once())
  299. ->method('getConfirmationStatus')
  300. ->willReturn(\Magento\Customer\Api\AccountManagementInterface::ACCOUNT_CONFIRMATION_NOT_REQUIRED);
  301. $this->scopeConfig->expects($this->atLeastOnce())->method('getValue')->with()->willReturn(true);
  302. $this->subscriber->subscribeCustomerById($customerId);
  303. $this->assertEquals(Subscriber::STATUS_NOT_ACTIVE, $this->subscriber->getStatus());
  304. }
  305. public function testSubscribeCustomerByIdAfterConfirmation()
  306. {
  307. $storeId = 2;
  308. $customerId = 1;
  309. $customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  310. ->getMock();
  311. $this->customerRepository->expects($this->atLeastOnce())
  312. ->method('getById')
  313. ->with($customerId)->willReturn($customerDataMock);
  314. $this->resource->expects($this->atLeastOnce())
  315. ->method('loadByCustomerData')
  316. ->with($customerDataMock)
  317. ->willReturn(
  318. [
  319. 'subscriber_id' => 1,
  320. 'subscriber_status' => Subscriber::STATUS_UNCONFIRMED
  321. ]
  322. );
  323. $customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
  324. $this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
  325. $customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
  326. $customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
  327. $this->sendEmailCheck();
  328. $this->customerAccountManagement->expects($this->never())->method('getConfirmationStatus');
  329. $this->scopeConfig->expects($this->atLeastOnce())->method('getValue')->with()->willReturn(true);
  330. $this->subscriber->updateSubscription($customerId);
  331. $this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $this->subscriber->getStatus());
  332. }
  333. public function testUnsubscribe()
  334. {
  335. $this->resource->expects($this->once())->method('save')->willReturnSelf();
  336. $this->sendEmailCheck();
  337. $this->assertEquals($this->subscriber, $this->subscriber->unsubscribe());
  338. }
  339. /**
  340. * @expectedException \Magento\Framework\Exception\LocalizedException
  341. * @expectedExceptionMessage This is an invalid subscription confirmation code.
  342. */
  343. public function testUnsubscribeException()
  344. {
  345. $this->subscriber->setCode(111);
  346. $this->subscriber->setCheckCode(222);
  347. $this->subscriber->unsubscribe();
  348. }
  349. public function testGetSubscriberFullName()
  350. {
  351. $this->subscriber->setFirstname('John');
  352. $this->subscriber->setLastname('Doe');
  353. $this->assertEquals('John Doe', $this->subscriber->getSubscriberFullName());
  354. }
  355. public function testConfirm()
  356. {
  357. $code = 111;
  358. $this->subscriber->setCode($code);
  359. $this->resource->expects($this->once())->method('save')->willReturnSelf();
  360. $this->assertTrue($this->subscriber->confirm($code));
  361. }
  362. public function testConfirmWrongCode()
  363. {
  364. $code = 111;
  365. $this->subscriber->setCode(222);
  366. $this->assertFalse($this->subscriber->confirm($code));
  367. }
  368. public function testReceived()
  369. {
  370. $queue = $this->getMockBuilder(\Magento\Newsletter\Model\Queue::class)
  371. ->disableOriginalConstructor()
  372. ->getMock();
  373. $this->resource->expects($this->once())->method('received')->with($this->subscriber, $queue)->willReturnSelf();
  374. $this->assertEquals($this->subscriber, $this->subscriber->received($queue));
  375. }
  376. /**
  377. * @return $this
  378. */
  379. protected function sendEmailCheck()
  380. {
  381. $storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  382. ->disableOriginalConstructor()
  383. ->setMethods(['getId'])
  384. ->getMock();
  385. $transport = $this->createMock(\Magento\Framework\Mail\TransportInterface::class);
  386. $this->scopeConfig->expects($this->any())->method('getValue')->willReturn(true);
  387. $this->transportBuilder->expects($this->once())->method('setTemplateIdentifier')->willReturnSelf();
  388. $this->transportBuilder->expects($this->once())->method('setTemplateOptions')->willReturnSelf();
  389. $this->transportBuilder->expects($this->once())->method('setTemplateVars')->willReturnSelf();
  390. $this->transportBuilder->expects($this->once())->method('setFrom')->willReturnSelf();
  391. $this->transportBuilder->expects($this->once())->method('addTo')->willReturnSelf();
  392. $this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel);
  393. $storeModel->expects($this->any())->method('getId')->willReturn(1);
  394. $this->transportBuilder->expects($this->once())->method('getTransport')->willReturn($transport);
  395. $transport->expects($this->once())->method('sendMessage')->willReturnSelf();
  396. $this->inlineTranslation->expects($this->once())->method('suspend')->willReturnSelf();
  397. $this->inlineTranslation->expects($this->once())->method('resume')->willReturnSelf();
  398. return $this;
  399. }
  400. }