Add test for run once setting
[MAILPOET-4966]
This commit is contained in:
113
mailpoet/tests/DataFactories/Automation.php
Normal file
113
mailpoet/tests/DataFactories/Automation.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Test\DataFactories;
|
||||
|
||||
use MailPoet\Automation\Engine\Data\Automation as Entity;
|
||||
use MailPoet\Automation\Engine\Data\NextStep;
|
||||
use MailPoet\Automation\Engine\Data\Step;
|
||||
use MailPoet\Automation\Engine\Storage\AutomationStorage;
|
||||
use MailPoet\DI\ContainerWrapper;
|
||||
|
||||
class Automation {
|
||||
|
||||
|
||||
/**
|
||||
* @var AutomationStorage
|
||||
*/
|
||||
private $storage;
|
||||
|
||||
/**
|
||||
* @var Entity
|
||||
*/
|
||||
private $automation;
|
||||
|
||||
public function __construct() {
|
||||
$this->storage = ContainerWrapper::getInstance(WP_DEBUG)->get(AutomationStorage::class);
|
||||
$this->automation = new Entity(
|
||||
'', [
|
||||
'root' => new Step(
|
||||
'root',
|
||||
|
||||
Step::TYPE_ROOT,
|
||||
'core:root',
|
||||
[],
|
||||
[]
|
||||
),
|
||||
], new \WP_User());
|
||||
}
|
||||
|
||||
public function withName($name) {
|
||||
$this->automation->setName($name);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function withSteps(Step ...$steps) {
|
||||
$sortedSteps = [];
|
||||
foreach ($steps as $step) {
|
||||
$sortedSteps[$step->getId()] = $step;
|
||||
}
|
||||
$this->automation->setSteps($sortedSteps);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addStep(Step $step) {
|
||||
$steps = $this->automation->getSteps();
|
||||
$lastStep = end($steps);
|
||||
if (!$lastStep) {
|
||||
return $this->withSteps($step);
|
||||
}
|
||||
$lastStep->setNextSteps([new NextStep($step->getId())]);
|
||||
$steps[$step->getId()] = $step;
|
||||
return $this->withSteps(...$steps);
|
||||
}
|
||||
|
||||
public function withDelayAction() {
|
||||
$step = new Step(
|
||||
uniqid(),
|
||||
Step::TYPE_ACTION,
|
||||
'core:delay',
|
||||
[
|
||||
'delay_type' => 'MINUTES',
|
||||
'delay' => 1,
|
||||
],
|
||||
[]
|
||||
);
|
||||
return $this->addStep($step);
|
||||
}
|
||||
|
||||
public function withSomeoneSubscribesTrigger() {
|
||||
$step = new Step(
|
||||
uniqid(),
|
||||
Step::TYPE_TRIGGER,
|
||||
'mailpoet:someone-subscribes',
|
||||
[],
|
||||
[]
|
||||
);
|
||||
return $this->addStep($step);
|
||||
}
|
||||
|
||||
public function withMeta($key, $value) {
|
||||
$this->automation->setMeta($key, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function withStatus($status) {
|
||||
$this->automation->setStatus($status);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function withStatusActive() {
|
||||
$this->automation->setStatus(Entity::STATUS_ACTIVE);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function create() {
|
||||
$id = $this->storage->createAutomation($this->automation);
|
||||
$automation = $this->storage->getAutomation($id);
|
||||
if (!$automation) {
|
||||
throw new \Exception('Automation not found.');
|
||||
}
|
||||
$this->automation = $automation;
|
||||
return $this->automation;
|
||||
}
|
||||
}
|
@@ -0,0 +1,169 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Test\Acceptance;
|
||||
|
||||
use MailPoet\Automation\Engine\Data\Automation;
|
||||
use MailPoet\Automation\Engine\Storage\AutomationRunLogStorage;
|
||||
use MailPoet\Automation\Engine\Storage\AutomationRunStorage;
|
||||
use MailPoet\Automation\Engine\Storage\AutomationStorage;
|
||||
use MailPoet\DI\ContainerWrapper;
|
||||
use MailPoet\Test\DataFactories;
|
||||
|
||||
class RunAutomationOnlyOnceSettingCest {
|
||||
|
||||
/** @var DataFactories\Settings */
|
||||
private $settingsFactory;
|
||||
|
||||
/** @var ContainerWrapper */
|
||||
private $container;
|
||||
|
||||
/** @var AutomationStorage */
|
||||
private $automationStorage;
|
||||
|
||||
/** @var AutomationRunStorage */
|
||||
private $automationRunStorage;
|
||||
|
||||
/** @var AutomationRunLogStorage */
|
||||
private $automationRunLogStorage;
|
||||
|
||||
/** @var Automation */
|
||||
private $automation;
|
||||
|
||||
private $segment;
|
||||
|
||||
public function _before(\AcceptanceTester $i) {
|
||||
$this->container = ContainerWrapper::getInstance();
|
||||
$this->settingsFactory = new DataFactories\Settings();
|
||||
$this->settingsFactory->withCronTriggerMethod('Action Scheduler');
|
||||
$this->automationStorage = $this->container->get(AutomationStorage::class);
|
||||
$this->automationRunStorage = $this->container->get(AutomationRunStorage::class);
|
||||
$this->automationRunLogStorage = $this->container->get(AutomationRunLogStorage::class);
|
||||
|
||||
|
||||
$this->automation = (new DataFactories\Automation())
|
||||
->withName('runAutomationOnlyOnce Automation')
|
||||
->withSomeoneSubscribesTrigger()
|
||||
->withDelayAction()
|
||||
->withMeta('run_automation_once', false)
|
||||
->withStatusActive()
|
||||
->create();
|
||||
$this->segment = (new DataFactories\Segment())->withName('runAutomationOnlyOnce-segment')->create();
|
||||
}
|
||||
|
||||
public function runAutomationOnlyOnce(\AcceptanceTester $i) {
|
||||
|
||||
$subscriberEmail = 'run-automation-only-once-test@mailpoet.com';
|
||||
$i->wantTo('Ensure that a subscriber enters an automation only once when the setting is set');
|
||||
$i->login();
|
||||
$i->amOnMailpoetPage('automation');
|
||||
$i->waitForText('Edit');
|
||||
$i->dontSee('Entered 1');
|
||||
$i->see('Entered 0'); //Actually I see "0 Entered", but this CSS switch is not caught by the test
|
||||
$i->click($this->automation->getName());
|
||||
$i->waitForText('Automation settings');
|
||||
$i->waitForText('Run this automation only once per subscriber.');
|
||||
$i->click('.mailpoet-automation-run-only-once label');
|
||||
|
||||
$i->click('Trigger');
|
||||
$i->fillField('When someone subscribes to the following lists:', $this->segment->getName());
|
||||
$i->click('Update');
|
||||
$i->waitForText('The automation has been saved.');
|
||||
|
||||
$i->amOnPage('/wp-admin/admin.php?page=mailpoet-subscribers#/new');
|
||||
$i->fillField('#field_email', $subscriberEmail);
|
||||
$i->fillField('#field_first_name', 'automation-tester-firstname');
|
||||
$i->selectOptionInSelect2($this->segment->getName());
|
||||
$i->click('Save');
|
||||
|
||||
$i->amOnMailpoetPage('automation');
|
||||
$i->waitForText($this->automation->getName());
|
||||
$i->see('Entered 1'); //Actually I see "1 Entered", but this CSS switch is not caught by the test
|
||||
|
||||
$i->amOnMailpoetPage('subscribers');
|
||||
$i->searchFor($subscriberEmail);
|
||||
$i->waitForText($subscriberEmail);
|
||||
$i->click($subscriberEmail);
|
||||
$i->waitForElementNotVisible('.mailpoet_form_loading');
|
||||
$i->click('Remove item'); // Removes the newsletter list from the subscriber
|
||||
$i->click('Save');
|
||||
$i->searchFor($subscriberEmail);
|
||||
$i->waitForText($subscriberEmail);
|
||||
$i->click($subscriberEmail);
|
||||
$i->waitForElementNotVisible('.mailpoet_form_loading');
|
||||
$i->selectOptionInSelect2($this->segment->getName());
|
||||
$i->click('Save');
|
||||
|
||||
$i->amOnMailpoetPage('automation');
|
||||
$i->waitForText($this->automation->getName());
|
||||
// No new run has been created.
|
||||
$i->see('Entered 1');
|
||||
$i->dontSee('Entered 2');
|
||||
}
|
||||
|
||||
public function runAutomationMultipleTimes(\AcceptanceTester $i) {
|
||||
|
||||
$this->automation = (new DataFactories\Automation())
|
||||
->withName('runAutomationOnlyOnce Automation')
|
||||
->withSomeoneSubscribesTrigger()
|
||||
->withDelayAction()
|
||||
->withMeta('run_only_once', false)
|
||||
->withStatusActive()
|
||||
->create();
|
||||
|
||||
$subscriberEmail = 'run-automation-only-once-test@mailpoet.com';
|
||||
$i->wantTo('Ensure that a subscriber enters an automation only once when the setting is set');
|
||||
$i->login();
|
||||
$i->amOnMailpoetPage('automation');
|
||||
$i->waitForText('Edit');
|
||||
$i->dontSee('Entered 1');
|
||||
$i->dontSee('Entered 2');
|
||||
$i->see('Entered 0'); //Actually I see "0 Entered", but this CSS switch is not caught by the test
|
||||
$i->click($this->automation->getName());
|
||||
$i->waitForText('Run this automation only once per subscriber.');
|
||||
$i->click('.mailpoet-automation-run-only-once'); //yes
|
||||
$i->click('.mailpoet-automation-run-only-once'); //no
|
||||
|
||||
$i->click('Trigger');
|
||||
$i->fillField('When someone subscribes to the following lists:', $this->segment->getName());
|
||||
$i->click('Update');
|
||||
$i->waitForText('The automation has been saved.');
|
||||
|
||||
$i->amOnPage('/wp-admin/admin.php?page=mailpoet-subscribers#/new');
|
||||
$i->fillField('#field_email', $subscriberEmail);
|
||||
$i->fillField('#field_first_name', 'automation-tester-firstname');
|
||||
$i->selectOptionInSelect2($this->segment->getName());
|
||||
$i->click('Save');
|
||||
|
||||
$i->amOnMailpoetPage('automation');
|
||||
$i->waitForText($this->automation->getName());
|
||||
$i->see('Entered 1'); //Actually I see "1 Entered", but this CSS switch is not caught by the test
|
||||
$i->dontSee('Entered 2');
|
||||
|
||||
$i->amOnMailpoetPage('subscribers');
|
||||
$i->searchFor($subscriberEmail);
|
||||
$i->waitForText($subscriberEmail);
|
||||
$i->click($subscriberEmail);
|
||||
$i->waitForElementNotVisible('.mailpoet_form_loading');
|
||||
$i->click('Remove item'); // Removes the newsletter list from the subscriber
|
||||
$i->click('Save');
|
||||
$i->searchFor($subscriberEmail);
|
||||
$i->waitForText($subscriberEmail);
|
||||
$i->click($subscriberEmail);
|
||||
$i->waitForElementNotVisible('.mailpoet_form_loading');
|
||||
$i->selectOptionInSelect2($this->segment->getName());
|
||||
$i->click('Save');
|
||||
|
||||
$i->amOnMailpoetPage('automation');
|
||||
$i->waitForText($this->automation->getName());
|
||||
|
||||
// A new run has been created.
|
||||
$i->see('Entered 2');
|
||||
$i->dontSee('Entered 1');
|
||||
}
|
||||
|
||||
public function _after(\AcceptanceTester $i) {
|
||||
$this->automationStorage->truncate();
|
||||
$this->automationRunStorage->truncate();
|
||||
$this->automationRunLogStorage->truncate();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user