SubscriberTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Newsletter\Model;
  7. use Magento\TestFramework\Mail\Template\TransportBuilderMock;
  8. class SubscriberTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var Subscriber
  12. */
  13. private $model;
  14. protected function setUp()
  15. {
  16. $this->model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  17. \Magento\Newsletter\Model\Subscriber::class
  18. );
  19. }
  20. /**
  21. * @magentoDataFixture Magento/Newsletter/_files/subscribers.php
  22. * @magentoConfigFixture current_store newsletter/subscription/confirm 1
  23. */
  24. public function testEmailConfirmation()
  25. {
  26. $this->model->subscribe('customer_confirm@example.com');
  27. /** @var TransportBuilderMock $transportBuilder */
  28. $transportBuilder = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  29. ->get(\Magento\TestFramework\Mail\Template\TransportBuilderMock::class);
  30. // confirmationCode 'ysayquyajua23iq29gxwu2eax2qb6gvy' is taken from fixture
  31. $this->assertContains(
  32. '/newsletter/subscriber/confirm/id/' . $this->model->getSubscriberId()
  33. . '/code/ysayquyajua23iq29gxwu2eax2qb6gvy',
  34. $transportBuilder->getSentMessage()->getRawMessage()
  35. );
  36. $this->assertEquals(Subscriber::STATUS_NOT_ACTIVE, $this->model->getSubscriberStatus());
  37. }
  38. /**
  39. * @magentoDataFixture Magento/Newsletter/_files/subscribers.php
  40. */
  41. public function testLoadByCustomerId()
  42. {
  43. $this->assertSame($this->model, $this->model->loadByCustomerId(1));
  44. $this->assertEquals('customer@example.com', $this->model->getSubscriberEmail());
  45. }
  46. /**
  47. * @magentoDataFixture Magento/Newsletter/_files/subscribers.php
  48. * @magentoAppArea frontend
  49. */
  50. public function testUnsubscribeSubscribe()
  51. {
  52. // Unsubscribe and verify
  53. $this->assertSame($this->model, $this->model->loadByCustomerId(1));
  54. $this->assertEquals($this->model, $this->model->unsubscribe());
  55. $this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $this->model->getSubscriberStatus());
  56. // Subscribe and verify
  57. $this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $this->model->subscribe('customer@example.com'));
  58. $this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $this->model->getSubscriberStatus());
  59. }
  60. /**
  61. * @magentoDataFixture Magento/Newsletter/_files/subscribers.php
  62. * @magentoAppArea frontend
  63. */
  64. public function testUnsubscribeSubscribeByCustomerId()
  65. {
  66. // Unsubscribe and verify
  67. $this->assertSame($this->model, $this->model->unsubscribeCustomerById(1));
  68. $this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $this->model->getSubscriberStatus());
  69. // Subscribe and verify
  70. $this->assertSame($this->model, $this->model->subscribeCustomerById(1));
  71. $this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $this->model->getSubscriberStatus());
  72. }
  73. /**
  74. * @magentoDataFixture Magento/Newsletter/_files/subscribers.php
  75. * @magentoConfigFixture current_store newsletter/subscription/confirm 1
  76. */
  77. public function testConfirm()
  78. {
  79. $customerEmail = 'customer_confirm@example.com';
  80. $this->model->subscribe($customerEmail);
  81. $this->model->loadByEmail($customerEmail);
  82. $this->model->confirm($this->model->getSubscriberConfirmCode());
  83. $transportBuilder = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  84. \Magento\TestFramework\Mail\Template\TransportBuilderMock::class
  85. );
  86. $this->assertContains(
  87. 'You have been successfully subscribed to our newsletter.',
  88. $transportBuilder->getSentMessage()->getRawMessage()
  89. );
  90. }
  91. }