From 279adcfbcf4bc1cce6e11b99b7ef3dae8c6d8e8c Mon Sep 17 00:00:00 2001 From: David Remer Date: Fri, 4 Nov 2022 11:38:19 +0200 Subject: [PATCH] Test user registration triggers automation [MAILPOET-4773] --- .../UserRegistrationTriggerCest.php | 153 ++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 mailpoet/tests/acceptance/Automation/UserRegistrationTriggerCest.php diff --git a/mailpoet/tests/acceptance/Automation/UserRegistrationTriggerCest.php b/mailpoet/tests/acceptance/Automation/UserRegistrationTriggerCest.php new file mode 100644 index 0000000000..d3a9103b8c --- /dev/null +++ b/mailpoet/tests/acceptance/Automation/UserRegistrationTriggerCest.php @@ -0,0 +1,153 @@ +withFeatureEnabled(FeaturesController::AUTOMATION); + $this->container = ContainerWrapper::getInstance(); + $migrator = $this->container->get(Migrator::class); + $migrator->deleteSchema(); + $migrator->createSchema(); + + $this->settingsFactory = new Settings(); + + $this->settingsFactory->withCronTriggerMethod('Action Scheduler'); + $this->workflowStorage = $this->container->get(WorkflowStorage::class); + $this->workflowRunStorage = $this->container->get(WorkflowRunStorage::class); + $this->workflowRunLogStorage = $this->container->get(WorkflowRunLogStorage::class); + } + + public function workflowTriggeredByRegistrationWithoutConfirmationNeeded(\AcceptanceTester $i) { + $i->wantTo("Activate a trigger by registering."); + $this->settingsFactory + ->withSubscribeOnRegisterEnabled() + ->withConfirmationEmailDisabled(); + $this->createWorkflow(); + + $i->login(); + + $i->amOnMailpoetPage('automation'); + $i->waitForText('Entered'); + $i->see('Entered 0'); //The visible text is 0 Entered, but in the DOM it's the other way around. + $i->dontSee('Entered 1'); + $i->logOut(); + + $this->registerWith($i,'workflow-triggered-by-registration', 'test@mailpoet.com'); + + $i->login(); + $i->amOnMailpoetPage('automation'); + $i->waitForText('Entered'); + $i->dontSee('Entered 0'); + $i->see('Entered 1'); //The visible text is 1 Entered, but in the DOM it's the other way around. + } + + public function workflowTriggeredByRegistrationWitConfirmationNeeded(\AcceptanceTester $i) { + $i->wantTo("Activate a trigger by registering."); + $this->settingsFactory + ->withSubscribeOnRegisterEnabled() + ->withConfirmationEmailEnabled(); + $this->createWorkflow(); + + $i->login(); + + $i->amOnMailpoetPage('automation'); + $i->waitForText('Entered'); + $i->see('Entered 0'); //The visible text is 0 Entered, but in the DOM it's the other way around. + $i->dontSee('Entered 1'); + $i->logOut(); + + $this->registerWith($i,'workflow-triggered-by-registration', 'test@mailpoet.com'); + + $i->login(); + + $i->amOnMailpoetPage('automation'); + $i->waitForText('Entered'); + $i->see('Entered 0'); //The visible text is 0 Entered, but in the DOM it's the other way around. + $i->dontSee('Entered 1'); + + $i->amOnMailboxAppPage(); + $i->click(Locator::contains('span.subject', 'Confirm your subscription')); + $i->switchToIframe('#preview-html'); + $i->click('Click here to confirm your subscription.'); + + $i->amonUrl('http://test.local'); + $i->amOnMailpoetPage('automation'); + $i->waitForText('Entered'); + $i->dontSee('Entered 0'); + $i->see('Entered 1'); //The visible text is 1 Entered, but in the DOM it's the other way around. + } + + private function registerWith(\AcceptanceTester $i, string $username, string $email, bool $signup = true) { + $i->amOnPage("/wp-login.php?action=register"); + $i->wait(1);// this needs to be here, Username is not filled properly without this line + $i->fillField('Username', $username); + $i->fillField('Email', $email); + if ($signup) { + $i->click('#mailpoet_subscribe_on_register'); + } + $i->click('Register'); + $i->waitForText('Registration complete.', 10); + } + + private function createWorkflow(): Workflow { + $someoneSubscribesTrigger = $this->container->get(UserRegistrationTrigger::class); + $delayStep = $this->container->get(DelayAction::class); + $steps = [ + 'root' => new Step('root', Step::TYPE_ROOT, 'root', [], [new NextStep('t')]), + 't' => new Step('t', Step::TYPE_TRIGGER, $someoneSubscribesTrigger->getKey(), ['roles' => []], [new NextStep('a1')]), + 'a1' => new Step('a1', Step::TYPE_ACTION, $delayStep->getKey(), ['delay' => 1, 'delay_type' => 'HOURS'], []), + ]; + $workflow = new Workflow( + 'test', + $steps, + new \WP_User(1) + ); + $workflow->setStatus(Workflow::STATUS_ACTIVE); + $id = $this->workflowStorage->createWorkflow($workflow); + $storedWorkflow = $this->workflowStorage->getWorkflow($id); + if (!$storedWorkflow) { + throw new \Exception("Workflow not found."); + } + return $storedWorkflow; + } + + public function _after() { + $this->workflowStorage->truncate(); + $this->workflowRunStorage->truncate(); + $this->workflowRunLogStorage->truncate(); + } +}