ManageTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Newsletter\Controller;
  7. /**
  8. * @magentoDbIsolation enabled
  9. */
  10. class ManageTest extends \Magento\TestFramework\TestCase\AbstractController
  11. {
  12. /**
  13. * @var \Magento\Customer\Model\Session
  14. */
  15. protected $customerSession;
  16. /**
  17. * @var \Magento\Framework\Session\Generic
  18. */
  19. protected $coreSession;
  20. /**
  21. * Test setup
  22. */
  23. protected function setUp()
  24. {
  25. parent::setUp();
  26. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  27. $this->customerSession = $objectManager->get(\Magento\Customer\Model\Session::class);
  28. $this->customerSession->setCustomerId(1);
  29. $this->coreSession = $objectManager->get(\Magento\Framework\Session\Generic::class);
  30. $this->coreSession->setData('_form_key', 'formKey');
  31. }
  32. /**
  33. * test tearDown
  34. */
  35. protected function tearDown()
  36. {
  37. $this->customerSession->setCustomerId(null);
  38. $this->coreSession->unsetData('_form_key');
  39. }
  40. /**
  41. * @magentoDataFixture Magento/Customer/_files/customer.php
  42. */
  43. public function testSaveAction()
  44. {
  45. $this->getRequest()
  46. ->setParam('form_key', 'formKey')
  47. ->setParam('is_subscribed', '1');
  48. $this->dispatch('newsletter/manage/save');
  49. $this->assertRedirect($this->stringContains('customer/account/'));
  50. /**
  51. * Check that errors
  52. */
  53. $this->assertSessionMessages($this->isEmpty(), \Magento\Framework\Message\MessageInterface::TYPE_ERROR);
  54. /**
  55. * Check that success message
  56. */
  57. $this->assertSessionMessages(
  58. $this->equalTo(['We have saved your subscription.']),
  59. \Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
  60. );
  61. }
  62. /**
  63. * @magentoDataFixture Magento/Customer/_files/customer.php
  64. */
  65. public function testSaveActionRemoveSubscription()
  66. {
  67. $this->getRequest()
  68. ->setParam('form_key', 'formKey')
  69. ->setParam('is_subscribed', '0');
  70. $this->dispatch('newsletter/manage/save');
  71. $this->assertRedirect($this->stringContains('customer/account/'));
  72. /**
  73. * Check that errors
  74. */
  75. $this->assertSessionMessages($this->isEmpty(), \Magento\Framework\Message\MessageInterface::TYPE_ERROR);
  76. /**
  77. * Check that success message
  78. */
  79. $this->assertSessionMessages(
  80. $this->equalTo(['We have updated your subscription.']),
  81. \Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
  82. );
  83. }
  84. }