MassSubscribeTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Customer\Controller\Adminhtml\Index;
  8. use Magento\Backend\Model\Session;
  9. use Magento\Framework\Message\MessageInterface;
  10. use Magento\Newsletter\Model\Subscriber;
  11. use Magento\Newsletter\Model\SubscriberFactory;
  12. use Magento\TestFramework\Helper\Bootstrap;
  13. use Magento\Customer\Api\CustomerRepositoryInterface;
  14. use Magento\Customer\Api\Data\CustomerInterface;
  15. /**
  16. * @magentoAppArea adminhtml
  17. */
  18. class MassSubscribeTest extends \Magento\TestFramework\TestCase\AbstractBackendController
  19. {
  20. /**
  21. * Base controller URL
  22. *
  23. * @var string
  24. */
  25. protected $baseControllerUrl = 'http://localhost/index.php/backend/customer/index/index';
  26. protected function tearDown()
  27. {
  28. /**
  29. * Unset customer data
  30. */
  31. Bootstrap::getObjectManager()->get(Session::class)->setCustomerData(null);
  32. /**
  33. * Unset messages
  34. */
  35. Bootstrap::getObjectManager()->get(Session::class)->getMessages(true);
  36. }
  37. /**
  38. * Tests subscriber status of customers.
  39. *
  40. * @magentoDataFixture Magento/Customer/_files/five_repository_customers.php
  41. * @magentoDbIsolation disabled
  42. */
  43. public function testMassSubscriberAction()
  44. {
  45. /** @var SubscriberFactory $subscriberFactory */
  46. $subscriberFactory = Bootstrap::getObjectManager()->get(SubscriberFactory::class);
  47. $customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
  48. $this->assertNull(
  49. $subscriberFactory->create()
  50. ->loadByEmail('customer1@example.com')
  51. ->getSubscriberStatus()
  52. );
  53. $this->assertNull(
  54. $subscriberFactory->create()
  55. ->loadByEmail('customer2@example.com')
  56. ->getSubscriberStatus()
  57. );
  58. /** @var CustomerInterface $customer1 */
  59. $customer1 = $customerRepository->get('customer1@example.com');
  60. /** @var CustomerInterface $customer2 */
  61. $customer2 = $customerRepository->get('customer2@example.com');
  62. $params = [
  63. 'selected' => [
  64. $customer1->getId(),
  65. $customer2->getId(),
  66. ],
  67. 'namespace' => 'customer_listing',
  68. ];
  69. $this->getRequest()->setParams($params);
  70. $this->dispatch('backend/customer/index/massSubscribe');
  71. // Assertions
  72. $this->assertRedirect($this->stringStartsWith($this->baseControllerUrl));
  73. $this->assertSessionMessages(
  74. self::equalTo(['A total of 2 record(s) were updated.']),
  75. MessageInterface::TYPE_SUCCESS
  76. );
  77. $this->assertEquals(
  78. Subscriber::STATUS_SUBSCRIBED,
  79. $subscriberFactory->create()
  80. ->loadByEmail('customer1@example.com')
  81. ->getSubscriberStatus()
  82. );
  83. $this->assertEquals(
  84. Subscriber::STATUS_SUBSCRIBED,
  85. $subscriberFactory->create()
  86. ->loadByEmail('customer2@example.com')
  87. ->getSubscriberStatus()
  88. );
  89. }
  90. /**
  91. * @magentoAppIsolation enabled
  92. * @magentoDbIsolation enabled
  93. */
  94. public function testMassSubscriberActionNoSelection()
  95. {
  96. $params = [
  97. 'namespace' => 'customer_listing'
  98. ];
  99. $this->getRequest()->setParams($params);
  100. $this->dispatch('backend/customer/index/massSubscribe');
  101. $this->assertRedirect($this->stringStartsWith($this->baseControllerUrl));
  102. $this->assertSessionMessages(
  103. self::equalTo(['An item needs to be selected. Select and try again.']),
  104. MessageInterface::TYPE_ERROR
  105. );
  106. }
  107. }