SubscriberTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Newsletter\Controller;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. use Magento\TestFramework\TestCase\AbstractController;
  9. /**
  10. * Test Subscriber
  11. */
  12. class SubscriberTest extends AbstractController
  13. {
  14. protected function setUp()
  15. {
  16. parent::setUp();
  17. }
  18. public function testNewAction()
  19. {
  20. $this->getRequest()->setMethod('POST');
  21. $this->dispatch('newsletter/subscriber/new');
  22. $this->assertSessionMessages($this->isEmpty());
  23. $this->assertRedirect($this->anything());
  24. }
  25. /**
  26. * @magentoDbIsolation enabled
  27. */
  28. public function testNewActionUnusedEmail()
  29. {
  30. $this->getRequest()->setMethod('POST');
  31. $this->getRequest()->setPostValue([
  32. 'email' => 'not_used@example.com',
  33. ]);
  34. $this->dispatch('newsletter/subscriber/new');
  35. $this->assertSessionMessages($this->equalTo(['Thank you for your subscription.']));
  36. $this->assertRedirect($this->anything());
  37. }
  38. /**
  39. * @magentoDataFixture Magento/Customer/_files/customer.php
  40. */
  41. public function testNewActionUsedEmail()
  42. {
  43. $this->getRequest()->setMethod('POST');
  44. $this->getRequest()->setPostValue([
  45. 'email' => 'customer@example.com',
  46. ]);
  47. $this->dispatch('newsletter/subscriber/new');
  48. $this->assertSessionMessages($this->equalTo([
  49. 'Thank you for your subscription.',
  50. ]));
  51. $this->assertRedirect($this->anything());
  52. }
  53. /**
  54. * @magentoDataFixture Magento/Customer/_files/customer.php
  55. */
  56. public function testNewActionOwnerEmail()
  57. {
  58. $this->getRequest()->setMethod('POST');
  59. $this->getRequest()->setPostValue([
  60. 'email' => 'customer@example.com',
  61. ]);
  62. $this->login(1);
  63. $this->dispatch('newsletter/subscriber/new');
  64. $this->assertSessionMessages($this->equalTo(['Thank you for your subscription.']));
  65. $this->assertRedirect($this->anything());
  66. }
  67. /**
  68. * Login the user
  69. *
  70. * @param string $customerId Customer to mark as logged in for the session
  71. * @return void
  72. */
  73. protected function login($customerId)
  74. {
  75. /** @var \Magento\Customer\Model\Session $session */
  76. $session = Bootstrap::getObjectManager()
  77. ->get(\Magento\Customer\Model\Session::class);
  78. $session->loginById($customerId);
  79. }
  80. }