diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f46f553a1..aadff041b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -103,6 +103,7 @@ jobs: 'EndToEnd/general/plugin-screens', 'EndToEnd/integrations/divi-builder', 'EndToEnd/integrations/divi-theme', + 'EndToEnd/integrations/elementor', 'EndToEnd/integrations/other', 'EndToEnd/integrations/wlm', 'EndToEnd/integrations/woocommerce', diff --git a/tests/EndToEnd/forms/blocks-shortcodes/PageBlockFormBuilderCest.php b/tests/EndToEnd/forms/blocks-shortcodes/PageBlockFormBuilderCest.php index 2f02ca7da..2d9daacf0 100644 --- a/tests/EndToEnd/forms/blocks-shortcodes/PageBlockFormBuilderCest.php +++ b/tests/EndToEnd/forms/blocks-shortcodes/PageBlockFormBuilderCest.php @@ -155,7 +155,6 @@ public function testFormBuilderBlockWithDefaultConfiguration(EndToEndTester $I) $I->seeElementInDOM('button[type="submit"]'); // Confirm that the email address was added to Kit. - $I->wait(3); $I->apiCheckSubscriberExists( $I, emailAddress: $emailAddress, @@ -366,7 +365,6 @@ public function testFormBuilderBlockWithFormEnabled(EndToEndTester $I) // Confirm that the email address was added to Kit. $I->waitForElementVisible('.convertkit-form-builder-subscribed-message'); - $I->wait(3); $subscriber = $I->apiCheckSubscriberExists( $I, emailAddress: $emailAddress, @@ -471,7 +469,6 @@ public function testFormBuilderBlockWithTaggingEnabled(EndToEndTester $I) // Confirm that the email address was added to Kit. $I->waitForElementVisible('.convertkit-form-builder-subscribed-message'); - $I->wait(3); $subscriber = $I->apiCheckSubscriberExists( $I, emailAddress: $emailAddress, @@ -575,7 +572,6 @@ public function testFormBuilderBlockWithSequenceEnabled(EndToEndTester $I) // Confirm that the email address was added to Kit. $I->waitForElementVisible('.convertkit-form-builder-subscribed-message'); - $I->wait(3); $subscriber = $I->apiCheckSubscriberExists( $I, emailAddress: $emailAddress, @@ -710,7 +706,6 @@ public function testFormBuilderBlockWithCustomField(EndToEndTester $I) // Confirm that the email address was added to Kit. $I->waitForElementVisible('.convertkit-form-builder-subscribed-message'); - $I->wait(3); $subscriber = $I->apiCheckSubscriberExists( $I, emailAddress: $emailAddress, @@ -1033,7 +1028,6 @@ public function testFormBuilderWithRecaptchaEnabled(EndToEndTester $I) // Confirm that the email address was added to Kit. $I->waitForElementVisible('.convertkit-form-builder-subscribed-message'); - $I->wait(3); $I->apiCheckSubscriberExists( $I, emailAddress: $emailAddress, @@ -1096,7 +1090,6 @@ public function testFormBuilderWithRecaptchaEnabledAndHighMinimumScore(EndToEndT $I->click('div.wp-block-convertkit-form-builder button[type="submit"]'); // Confirm that the email address was not added to Kit, as reCAPTCHA score failed. - $I->wait(3); $I->apiCheckSubscriberDoesNotExist($I, $emailAddress); } @@ -1167,7 +1160,6 @@ public function testFormBuilderWithStoreEntriesEnabled(EndToEndTester $I) // Confirm that the email address was added to Kit. $I->waitForElementVisible('.convertkit-form-builder-subscribed-message'); - $I->wait(3); $subscriber = $I->apiCheckSubscriberExists( $I, emailAddress: $emailAddress, diff --git a/tests/EndToEnd/integrations/other/ElementorBroadcastsCest.php b/tests/EndToEnd/integrations/elementor/ElementorBroadcastsCest.php similarity index 100% rename from tests/EndToEnd/integrations/other/ElementorBroadcastsCest.php rename to tests/EndToEnd/integrations/elementor/ElementorBroadcastsCest.php diff --git a/tests/EndToEnd/integrations/other/ElementorFormCest.php b/tests/EndToEnd/integrations/elementor/ElementorFormCest.php similarity index 100% rename from tests/EndToEnd/integrations/other/ElementorFormCest.php rename to tests/EndToEnd/integrations/elementor/ElementorFormCest.php diff --git a/tests/EndToEnd/integrations/other/ElementorFormTriggerCest.php b/tests/EndToEnd/integrations/elementor/ElementorFormTriggerCest.php similarity index 100% rename from tests/EndToEnd/integrations/other/ElementorFormTriggerCest.php rename to tests/EndToEnd/integrations/elementor/ElementorFormTriggerCest.php diff --git a/tests/EndToEnd/integrations/other/ElementorProductCest.php b/tests/EndToEnd/integrations/elementor/ElementorProductCest.php similarity index 100% rename from tests/EndToEnd/integrations/other/ElementorProductCest.php rename to tests/EndToEnd/integrations/elementor/ElementorProductCest.php diff --git a/tests/EndToEnd/integrations/other/ContactForm7FormCest.php b/tests/EndToEnd/integrations/other/ContactForm7FormCest.php index e9a1cc2b4..cef401d2e 100644 --- a/tests/EndToEnd/integrations/other/ContactForm7FormCest.php +++ b/tests/EndToEnd/integrations/other/ContactForm7FormCest.php @@ -66,9 +66,6 @@ public function testSettingsContactForm7ToKitFormMapping(EndToEndTester $I) emailAddress: $emailAddress ); - // Wait for the API to update. - $I->wait(2); - // Confirm that the email address was added to Kit. $subscriber = $I->apiCheckSubscriberExists($I, $emailAddress); @@ -106,9 +103,6 @@ public function testSettingsContactForm7ToKitLegacyFormMapping(EndToEndTester $I emailAddress: $emailAddress ); - // Wait for the API to update. - $I->wait(2); - // Confirm that the email address was added to Kit. $I->apiCheckSubscriberExists($I, $emailAddress); } @@ -138,9 +132,6 @@ public function testSettingsContactForm7ToKitTagMapping(EndToEndTester $I) emailAddress: $emailAddress ); - // Wait for the API to update. - $I->wait(2); - // Confirm that the email address was added to Kit. $subscriber = $I->apiCheckSubscriberExists($I, $emailAddress); @@ -177,9 +168,6 @@ public function testSettingsContactForm7ToKitSequenceMapping(EndToEndTester $I) emailAddress: $emailAddress ); - // Wait for the API to update. - $I->wait(2); - // Confirm that the email address was added to Kit. $subscriber = $I->apiCheckSubscriberExists($I, $emailAddress); @@ -245,9 +233,6 @@ public function testSettingsContactForm7SubscribeOption(EndToEndTester $I) emailAddress: $emailAddress ); - // Wait for the API to update. - $I->wait(2); - // Confirm that the email address was added to Kit. $I->apiCheckSubscriberExists($I, $emailAddress); } diff --git a/tests/EndToEnd/integrations/other/ForminatorCest.php b/tests/EndToEnd/integrations/other/ForminatorCest.php index a37fdb5d1..b33200ed9 100644 --- a/tests/EndToEnd/integrations/other/ForminatorCest.php +++ b/tests/EndToEnd/integrations/other/ForminatorCest.php @@ -66,9 +66,6 @@ public function testSettingsForminatorFormToKitFormMapping(EndToEndTester $I) emailAddress: $emailAddress ); - // Wait for the API to update. - $I->wait(2); - // Confirm that the email address was added to Kit. $subscriber = $I->apiCheckSubscriberExists($I, $emailAddress); @@ -106,9 +103,6 @@ public function testSettingsForminatorFormToKitLegacyFormMapping(EndToEndTester emailAddress: $emailAddress ); - // Wait for the API to update. - $I->wait(2); - // Confirm that the email address was added to Kit. $I->apiCheckSubscriberExists($I, $emailAddress); } @@ -138,9 +132,6 @@ public function testSettingsForminatorFormToKitTagMapping(EndToEndTester $I) emailAddress: $emailAddress ); - // Wait for the API to update. - $I->wait(2); - // Confirm that the email address was added to Kit. $subscriber = $I->apiCheckSubscriberExists($I, $emailAddress); @@ -177,9 +168,6 @@ public function testSettingsForminatorFormToKitSequenceMapping(EndToEndTester $I emailAddress: $emailAddress ); - // Wait for the API to update. - $I->wait(2); - // Confirm that the email address was added to Kit. $subscriber = $I->apiCheckSubscriberExists($I, $emailAddress); @@ -270,9 +258,6 @@ public function testSettingsForminatorQuizToKitFormMapping(EndToEndTester $I) emailAddress: $emailAddress ); - // Wait for the API to update. - $I->wait(2); - // Confirm that the email address was added to Kit. $I->apiCheckSubscriberExists($I, $emailAddress); } @@ -302,9 +287,6 @@ public function testSettingsForminatorQuizToKitTagMapping(EndToEndTester $I) emailAddress: $emailAddress ); - // Wait for the API to update. - $I->wait(2); - // Confirm that the email address was added to Kit. $subscriber = $I->apiCheckSubscriberExists($I, $emailAddress); @@ -341,9 +323,6 @@ public function testSettingsForminatorQuizToKitSequenceMapping(EndToEndTester $I emailAddress: $emailAddress ); - // Wait for the API to update. - $I->wait(2); - // Confirm that the email address was added to Kit. $subscriber = $I->apiCheckSubscriberExists($I, $emailAddress); @@ -409,9 +388,6 @@ public function testSettingsForminatorQuizSubscribeOption(EndToEndTester $I) emailAddress: $emailAddress ); - // Wait for the API to update. - $I->wait(2); - // Confirm that the email address was added to Kit. $I->apiCheckSubscriberExists($I, $emailAddress); } diff --git a/tests/Support/Helper/KitAPI.php b/tests/Support/Helper/KitAPI.php index 8053a9851..5d69eba9e 100644 --- a/tests/Support/Helper/KitAPI.php +++ b/tests/Support/Helper/KitAPI.php @@ -49,20 +49,32 @@ public function apiEncodeState($returnTo, $clientID) */ public function apiCheckSubscriberExists($I, $emailAddress, $firstName = false) { - // Run request. - $results = $this->apiRequest( - 'subscribers', - 'GET', - [ - 'email_address' => $emailAddress, - 'include_total_count' => true, + // Wait for the API to update. + $I->wait(3); - // Check all subscriber states. - 'status' => 'all', - ] + // Retry the API request as sometimes there's a lag before the subscriber is queryable via the API. + $results = $this->retryUntil( + function () use ($emailAddress) { + $results = $this->apiRequest( + 'subscribers', + 'GET', + [ + 'email_address' => $emailAddress, + 'include_total_count' => true, + + // Check all subscriber states. + 'status' => 'all', + ] + ); + + // Return the results only if a subscriber was found, so + // retryUntil() will keep trying otherwise. + return ( $results['pagination']['total_count'] > 0 ) ? $results : false; + } ); // Check at least one subscriber was returned and it matches the email address. + $I->assertNotFalse($results); $I->assertGreaterThan(0, $results['pagination']['total_count']); $I->assertEquals($emailAddress, $results['subscribers'][0]['email_address']); @@ -192,6 +204,9 @@ public function apiCheckSubscriberHasNoTags($I, $subscriberID) */ public function apiCheckSubscriberDoesNotExist($I, $emailAddress) { + // Wait for the API to update. + $I->wait(3); + // Run request. $results = $this->apiRequest( 'subscribers', @@ -324,4 +339,39 @@ public function apiRequest($endpoint, $method = 'GET', $params = array()) // Return JSON decoded response. return json_decode($result->getBody()->getContents(), true); } + + /** + * Repeatedly invokes the given callback until it returns a truthy value, or + * the maximum number of attempts is reached. + * + * Use this to wrap API checks that can be flaky due to ingestion lag at + * Kit's end (e.g. a subscriber created via a form submission isn't always + * immediately queryable via the `subscribers` endpoint). + * + * @since 3.3.2 + * + * @param callable $callback Callback to invoke. Should return the value + * to use, or false/null to indicate the + * check has not yet succeeded. + * @param int $attempts Maximum number of attempts. + * @param int $delay Seconds to wait between attempts. + * @return mixed The truthy value returned by $callback, or + * false if all attempts are exhausted. + */ + private function retryUntil(callable $callback, $attempts = 4, $delay = 3) + { + for ($i = 0; $i < $attempts; $i++) { + $result = $callback(); + if ($result) { + return $result; + } + + // Don't sleep after the final attempt. + if ($i < $attempts - 1) { + sleep($delay); + } + } + + return false; + } }