customerSession = $this->_objectManager->get(Session::class); $this->customerRepository = $this->_objectManager->get(CustomerRepositoryInterface::class); $this->formKey = $this->_objectManager->get(FormKey::class); $this->customerRegistry = $this->_objectManager->get(CustomerRegistry::class); } /** * @inheritdoc */ protected function tearDown(): void { $this->customerSession->logout(); parent::tearDown(); } /** * @magentoDataFixture Magento/Customer/_files/new_customer.php * * @dataProvider subscriptionDataProvider * * @param bool $isSubscribed * @param string $expectedMessage * @return void */ public function testSaveAction(bool $isSubscribed, string $expectedMessage): void { $this->loginCustomer('new_customer@example.com'); $this->dispatchSaveAction($isSubscribed); $this->assertSuccessSubscription($expectedMessage); } /** * @return array */ public function subscriptionDataProvider(): array { return [ 'subscribe_customer' => [ 'is_subscribed' => true, 'expected_message' => 'We have saved your subscription.', ], 'unsubscribe_customer' => [ 'is_subscribed' => false, 'expected_message' => 'We have updated your subscription.', ], ]; } /** * @magentoDataFixture Magento/Customer/_files/customer_confirmation_config_enable.php * @magentoDataFixture Magento/Customer/_files/new_customer.php * * @return void */ public function testSubscribeWithEnabledConfirmation(): void { $this->loginCustomer('new_customer@example.com'); $this->dispatchSaveAction(true); $this->assertSuccessSubscription('A confirmation request has been sent.'); } /** * @magentoDataFixture Magento/Newsletter/_files/customer_with_subscription.php * * @return void */ public function testUnsubscribeSubscribedCustomer(): void { $this->loginCustomer('new_customer@example.com'); $this->dispatchSaveAction(false); $this->assertSuccessSubscription('We have removed your newsletter subscription.'); } /** * Dispatch save action with parameters * * @param string $isSubscribed * @return void */ private function dispatchSaveAction(bool $isSubscribed): void { $this->_objectManager->removeSharedInstance(CustomerPlugin::class); $this->_objectManager->removeSharedInstance(CustomerSubscriberCache::class); $this->getRequest()->setParam('form_key', $this->formKey->getFormKey()) ->setParam('is_subscribed', $isSubscribed); $this->dispatch('newsletter/manage/save'); } /** * Login customer by email * * @param string $email * @return void */ private function loginCustomer(string $email): void { $customer = $this->customerRepository->get($email); $this->customerSession->loginById($customer->getId()); } /** * Assert that action was successfully done * * @param string $expectedMessage * @return void */ private function assertSuccessSubscription(string $expectedMessage): void { $this->assertRedirect($this->stringContains('customer/account/')); $this->assertSessionMessages($this->equalTo([(string)__($expectedMessage)]), MessageInterface::TYPE_SUCCESS); } }