From c733e088c29b4ad742efdbd504d584d02f0ac82a Mon Sep 17 00:00:00 2001 From: David Remer Date: Thu, 16 Feb 2023 11:53:16 +0200 Subject: [PATCH] Add test for run once setting [MAILPOET-4966] --- mailpoet/tests/DataFactories/Automation.php | 113 ++++++++++++ .../RunAutomationOnlyOnceSettingCest.php | 169 ++++++++++++++++++ 2 files changed, 282 insertions(+) create mode 100644 mailpoet/tests/DataFactories/Automation.php create mode 100644 mailpoet/tests/acceptance/Automation/RunAutomationOnlyOnceSettingCest.php diff --git a/mailpoet/tests/DataFactories/Automation.php b/mailpoet/tests/DataFactories/Automation.php new file mode 100644 index 0000000000..2096910ea8 --- /dev/null +++ b/mailpoet/tests/DataFactories/Automation.php @@ -0,0 +1,113 @@ +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; + } +} diff --git a/mailpoet/tests/acceptance/Automation/RunAutomationOnlyOnceSettingCest.php b/mailpoet/tests/acceptance/Automation/RunAutomationOnlyOnceSettingCest.php new file mode 100644 index 0000000000..c9b53e702b --- /dev/null +++ b/mailpoet/tests/acceptance/Automation/RunAutomationOnlyOnceSettingCest.php @@ -0,0 +1,169 @@ +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(); + } +}