Files
piratepoet/tests/acceptance/ManageSubscriptionLinkCest.php
2020-01-28 20:18:39 +00:00

128 lines
4.2 KiB
PHP

<?php
namespace MailPoet\Test\Acceptance;
use Codeception\Util\Locator;
use MailPoet\Test\DataFactories\Settings;
use PHPUnit\Framework\Exception;
class ManageSubscriptionLinkCest {
/** @var Settings */
private $settings;
/** @var string */
private $newsletterTitle;
public function __construct() {
$this->newsletterTitle = 'Subscription links Email ' . \MailPoet\Util\Security::generateRandomString();
}
protected function _inject(Settings $settings) {
$this->settings = $settings;
}
public function _before() {
$this->settings
->withConfirmationEmailEnabled()
->withCronTriggerMethod('WordPress');
}
public function manageSubscriptionLink(\AcceptanceTester $i) {
$i->wantTo('Verify that "manage subscription" link works and subscriber status can be updated');
$this->sendEmail($i);
$i->amOnMailboxAppPage();
$i->click(Locator::contains('span.subject', $this->newsletterTitle));
$i->switchToIframe('preview-html');
$i->waitForElementChange(
\Codeception\Util\Locator::contains('a', 'Manage your subscription'), function ($el) {
return $el->getAttribute('target') === "_blank";
}, 100
);
$i->click('Manage your subscription');
$i->switchToNextTab();
$i->waitForText('Manage your subscription');
$formStatusElement = '[data-automation-id="form_status"]';
// set status to unsubscribed
$approximateSaveButtonHeight = 50; // Used for scroll offset to ensure that button is not hidden above the top fold
$i->selectOption($formStatusElement, 'Unsubscribed');
$i->scrollTo('[data-automation-id="subscribe-submit-button"]', 0, -$approximateSaveButtonHeight);
$i->click('Save');
$i->waitForElement($formStatusElement);
$i->seeOptionIsSelected($formStatusElement, 'Unsubscribed');
// change status back to subscribed
$i->selectOption($formStatusElement, 'Subscribed');
$i->scrollTo('[data-automation-id="subscribe-submit-button"]', 0, -$approximateSaveButtonHeight);
$i->click('Save');
$i->waitForElement($formStatusElement);
$i->seeOptionIsSelected($formStatusElement, 'Subscribed');
$i->seeNoJSErrors();
}
public function unsubscribeLink(\AcceptanceTester $i) {
$i->wantTo('Verify that "unsubscribe" link works and subscriber status is set to unsubscribed');
$this->sendEmail($i);
$formStatusElement = '[data-automation-id="form_status"]';
$i->amOnMailboxAppPage();
$i->click(Locator::contains('span.subject', $this->newsletterTitle));
$i->switchToIframe('preview-html');
$i->waitForElementChange(
\Codeception\Util\Locator::contains('a', 'Unsubscribe'), function ($el) {
return $el->getAttribute('target') === "_blank";
}, 100
);
$i->click('Unsubscribe');
$i->switchToNextTab();
$i->waitForText('You are now unsubscribed');
$i->click('Manage your subscription');
$i->seeOptionIsSelected($formStatusElement, 'Unsubscribed');
$i->seeNoJSErrors();
}
private function sendEmail(\AcceptanceTester $i) {
$segmentName = $i->createListWithSubscriber();
$i->login();
$i->amOnMailpoetPage('Emails');
// step 1 - select type
$i->click('[data-automation-id="create_standard"]');
// step 2 - select template
$firstTemplateElement = '[data-automation-id="select_template_0"]';
$i->waitForElement($firstTemplateElement);
$i->click($firstTemplateElement);
// step 3 - design newsletter (update subject)
$titleElement = '[data-automation-id="newsletter_title"]';
$i->waitForElement($titleElement);
$i->fillField($titleElement, $this->newsletterTitle);
$i->click('Next');
// step 4 - send
$searchFieldElement = 'input.select2-search__field';
$i->waitForElement($searchFieldElement);
$i->selectOptionInSelect2($segmentName);
$i->click('Send');
// Reloading page is faster than waiting for regular AJAX request to refresh it
for ($index = 0; $index < 15; $index++) {
try {
$i->wait(2);
$i->reloadPage();
$i->waitForListingItemsToLoad();
$i->see('Sent to 1 of 1');
return;
} catch (Exception $e) {
continue;
}
}
$i->see('Sent to 1 of 1');
}
}