From 7c8b8d2100baebba65125d6916cff8b1d84df214 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Wed, 18 Dec 2024 10:40:21 +0000 Subject: [PATCH] Retain hash when included in URL --- resources/frontend/js/convertkit.js | 2 +- .../general/SubscriberIDURLCest.php | 83 +++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/resources/frontend/js/convertkit.js b/resources/frontend/js/convertkit.js index 42f62072..8567f7be 100644 --- a/resources/frontend/js/convertkit.js +++ b/resources/frontend/js/convertkit.js @@ -104,7 +104,7 @@ function convertKitRemoveSubscriberIDFromURL( url ) { } // Update history. - window.history.pushState( null, title, url_object.pathname + params ); + window.history.pushState( null, title, url_object.pathname + params + url_object.hash ); } diff --git a/tests/acceptance/general/SubscriberIDURLCest.php b/tests/acceptance/general/SubscriberIDURLCest.php index 735ee910..403138cd 100644 --- a/tests/acceptance/general/SubscriberIDURLCest.php +++ b/tests/acceptance/general/SubscriberIDURLCest.php @@ -50,6 +50,7 @@ public function testSubscriberIDRemovedFromURL(AcceptanceTester $I) $I->checkNoWarningsAndNoticesOnScreen($I); $I->wait(2); $I->assertStringNotContainsString('ck_subscriber_id=' . $_ENV['CONVERTKIT_API_SUBSCRIBER_ID'], $I->grabFromCurrentUrl()); + $I->assertStringNotContainsString('#', $I->grabFromCurrentUrl()); // Load the Page with UTM parameters at the end. $I->amOnPage('/convertkit-subscriber-id-url?ck_subscriber_id=' . $_ENV['CONVERTKIT_API_SUBSCRIBER_ID'] . '&utm_source=email&utm_medium=email'); @@ -57,6 +58,7 @@ public function testSubscriberIDRemovedFromURL(AcceptanceTester $I) $I->wait(2); $I->assertStringNotContainsString('ck_subscriber_id=' . $_ENV['CONVERTKIT_API_SUBSCRIBER_ID'], $I->grabFromCurrentUrl()); $I->assertStringContainsString('?utm_source=email&utm_medium=email', $I->grabFromCurrentUrl()); + $I->assertStringNotContainsString('#', $I->grabFromCurrentUrl()); // Load the Page with UTM parameters at the start. $I->amOnPage('/convertkit-subscriber-id-url?utm_source=email&utm_medium=email&ck_subscriber_id=' . $_ENV['CONVERTKIT_API_SUBSCRIBER_ID']); @@ -64,6 +66,57 @@ public function testSubscriberIDRemovedFromURL(AcceptanceTester $I) $I->wait(2); $I->assertStringNotContainsString('ck_subscriber_id=' . $_ENV['CONVERTKIT_API_SUBSCRIBER_ID'], $I->grabFromCurrentUrl()); $I->assertStringContainsString('?utm_source=email&utm_medium=email', $I->grabFromCurrentUrl()); + $I->assertStringNotContainsString('#', $I->grabFromCurrentUrl()); + } + + /** + * Test that the ck_subscriber_id parameter is removed from the URL + * and that a hash is retained if specified in the URL + * + * @since 2.7.0 + * + * @param AcceptanceTester $I Tester. + */ + public function testSubscriberIDRemovedFromURLAndHashRetainedWhenSpecified(AcceptanceTester $I) + { + // Create Page. + $I->havePageInDatabase( + [ + 'post_name' => 'convertkit-subscriber-id-url-hash', + 'post_content' => 'Test', + ] + ); + + // Confirm that a blank ck_subscriber_id does not cause a fatal error. + $I->amOnPage('/convertkit-subscriber-id-url-hash?ck_subscriber_id=#hash'); + $I->checkNoWarningsAndNoticesOnScreen($I); + + // Confirm that a non-numeric ck_subscriber_id does not cause a fatal error. + $I->amOnPage('/convertkit-subscriber-id-url-hash?ck_subscriber_id=abcde#hash'); + $I->checkNoWarningsAndNoticesOnScreen($I); + + // Confirm that the ck_subscriber_id was removed. + $I->amOnPage('/convertkit-subscriber-id-url-hash?ck_subscriber_id=' . $_ENV['CONVERTKIT_API_SUBSCRIBER_ID'] . '#hash'); + $I->checkNoWarningsAndNoticesOnScreen($I); + $I->wait(2); + $I->assertStringNotContainsString('ck_subscriber_id=' . $_ENV['CONVERTKIT_API_SUBSCRIBER_ID'], $I->grabFromCurrentUrl()); + $I->assertStringContainsString('#hash', $I->grabFromCurrentUrl()); + + // Load the Page with UTM parameters at the end. + $I->amOnPage('/convertkit-subscriber-id-url-hash?ck_subscriber_id=' . $_ENV['CONVERTKIT_API_SUBSCRIBER_ID'] . '&utm_source=email&utm_medium=email#hash'); + $I->checkNoWarningsAndNoticesOnScreen($I); + $I->wait(2); + $I->assertStringNotContainsString('ck_subscriber_id=' . $_ENV['CONVERTKIT_API_SUBSCRIBER_ID'], $I->grabFromCurrentUrl()); + $I->assertStringContainsString('?utm_source=email&utm_medium=email', $I->grabFromCurrentUrl()); + $I->assertStringContainsString('#hash', $I->grabFromCurrentUrl()); + + // Load the Page with UTM parameters at the start. + $I->amOnPage('/convertkit-subscriber-id-url-hash?utm_source=email&utm_medium=email&ck_subscriber_id=' . $_ENV['CONVERTKIT_API_SUBSCRIBER_ID'] . '#hash'); + $I->checkNoWarningsAndNoticesOnScreen($I); + $I->wait(2); + $I->assertStringNotContainsString('ck_subscriber_id=' . $_ENV['CONVERTKIT_API_SUBSCRIBER_ID'], $I->grabFromCurrentUrl()); + $I->assertStringContainsString('?utm_source=email&utm_medium=email', $I->grabFromCurrentUrl()); + $I->assertStringContainsString('#hash', $I->grabFromCurrentUrl()); } /** @@ -91,6 +144,36 @@ public function testQuerySeparatorNotAppendedToURLWhenCookieExists(AcceptanceTes $I->checkNoWarningsAndNoticesOnScreen($I); $I->wait(2); $I->assertStringNotContainsString('?', $I->grabFromCurrentUrl()); + $I->assertStringNotContainsString('#', $I->grabFromCurrentUrl()); + } + + /** + * Test that no query separator is appended to the URL when a valid ck_subscriber_id exists + * and that a hash is retained if specified in the URL + * + * @since 2.7.0 + * + * @param AcceptanceTester $I Tester. + */ + public function testQuerySeparatorNotAppendedToURLWhenCookieExistsAndHashRetainedWhenSpecified(AcceptanceTester $I) + { + // Create Page. + $I->havePageInDatabase( + [ + 'post_name' => 'convertkit-subscriber-id-cookie-hash', + 'post_content' => 'Test', + ] + ); + + // Set the ck_subscriber_id cookie. + $I->setCookie('ck_subscriber_id', $_ENV['CONVERTKIT_API_SUBSCRIBER_ID']); + + // Confirm that no query parameters does not append a separator/question mark. + $I->amOnPage('/convertkit-subscriber-id-url#hash'); + $I->checkNoWarningsAndNoticesOnScreen($I); + $I->wait(2); + $I->assertStringNotContainsString('?', $I->grabFromCurrentUrl()); + $I->assertStringContainsString('#hash', $I->grabFromCurrentUrl()); } /**