Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions tests/playwright/bin/create-wpforms-fixtures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
/**
* Create WPForms forms and associated pages for E2E tests.
*
* @package Google\Site_Kit
* @copyright 2026 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/

/**
* Gets the WPForms fixtures used by the Enhanced Conversions Playwright tests.
*
* @return array[] Form and page definitions.
*/
function google_site_kit_e2e_get_wpforms_fixtures() {
$email_field = array(
'type' => 'email',
'label' => 'Email Address',
);
$name_field = array(
'type' => 'name',
'label' => 'Name',
'size' => 'medium',
);
$phone_field = array(
'type' => 'text',
'label' => 'Phone Number',
);

return array(
array(
'title' => 'E2E WPForms Email',
'fields' => array( $email_field ),
),
array(
'title' => 'E2E WPForms Name',
'fields' => array( $name_field ),
),
array(
'title' => 'E2E WPForms Phone',
'fields' => array( $phone_field ),
),
array(
'title' => 'E2E WPForms All Fields',
'fields' => array( $email_field, $name_field, $phone_field ),
),
);
}

/**
* Creates a WPForms form for an Enhanced Conversions fixture.
*
* @param string $title Form title.
* @param array $fields Form fields.
* @return int Form ID.
*/
function google_site_kit_e2e_create_wpforms_form( $title, $fields ) {
// WPForms needs an explicit numeric field ID that matches the index.
foreach ( $fields as $field_id => $field ) {
$fields[ $field_id ]['id'] = (string) $field_id;
}

$forms = wpforms()->get( 'form' );
$form_id = $forms->add( $title );

$forms->update(
$form_id,
array(
'id' => $form_id,
'fields' => $fields,
'settings' => array(
'submit_text' => 'Submit',
'ajax_submit' => '1',
),
)
);

return $form_id;
}

/**
* Creates a page containing a WPForms form block.
*
* @param string $title Page title.
* @param int $form_id Form ID.
*/
function google_site_kit_e2e_create_wpforms_page( $title, $form_id ) {
wp_insert_post(
array(
'post_type' => 'page',
'post_status' => 'publish',
'post_title' => $title,
'post_content' => sprintf( '<!-- wp:wpforms/form-selector {"formId":"%d"} /-->', $form_id ),
)
);
}

/**
* Creates all WPForms fixtures for the Enhanced Conversions Playwright tests.
*/
function google_site_kit_e2e_create_wpforms_fixtures() {
foreach ( google_site_kit_e2e_get_wpforms_fixtures() as $fixture ) {
if ( get_page_by_path( sanitize_title( $fixture['title'] ), OBJECT, 'page' ) ) {
continue;
}

$form_id = google_site_kit_e2e_create_wpforms_form( $fixture['title'], $fixture['fields'] );

google_site_kit_e2e_create_wpforms_page( $fixture['title'], $form_id );
}

WP_CLI::success( 'The WPForms test fixtures are ready.' );
}

/**
* Run.
*/
google_site_kit_e2e_create_wpforms_fixtures();
41 changes: 33 additions & 8 deletions tests/playwright/bin/generate-backup-sql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
# This script:
# 1. Starts Docker services with WP 5.2.21 (oldest supported version)
# 2. Installs WordPress and configures it via WP-CLI
# 3. Exports the database to backup.sql
# 4. Tears down the containers
# 3. Seeds WPForms forms and their frontend pages
# 4. Exports the database to backup.sql
# 5. Tears down the containers

set -euo pipefail

Expand Down Expand Up @@ -76,17 +77,26 @@ wp post create --post_status=publish --post_title="Hello Milky Way!" --quiet
wp post create --post_status=publish --post_title="Hello Universe!" --quiet
wp post create --post_status=publish --post_title="Hello Spéçïåł čhāràćtęrß!" --quiet

echo "Activating theme and plugin..."
echo "Activating theme and plugins..."
wp theme activate twentynineteen --quiet
wp plugin activate google-site-kit --quiet
wp plugin activate wpforms-lite --quiet
wp plugin activate google-site-kit-test-plugins/enhanced-conversions.php --quiet

echo "Setting permalink structure..."
wp rewrite structure '%postname%' --hard --quiet

echo "Creating WPForms forms and pages..."
cat wpforms.php | wp eval-file - --user=admin

wp plugin deactivate wpforms-lite --quiet
wp plugin deactivate google-site-kit-test-plugins/enhanced-conversions.php --quiet

# Normalize the database so the dump is deterministic across runs.
echo "Normalizing database for deterministic output..."

FIXED_DATE="2025-01-01 00:00:00"
FIXED_TIMESTAMP="1735689600"

# Fix password hashes to a pre-computed phpass hash of "password".
FIXED_PASS_HASH='\$P\$BVGAi9V8sCdRMhCPxhAnRLpqqMBk720'
Expand All @@ -101,15 +111,30 @@ wp db query "UPDATE wp_comments SET comment_date = '$FIXED_DATE', comment_date_g
# Clear session_tokens so they don't vary between runs.
wp db query "DELETE FROM wp_usermeta WHERE meta_key = 'session_tokens'"

# Replace the cron option with a fixed empty cron array and remove transients.
wp option update cron --format=json '{"version":2}'
wp db query "DELETE FROM wp_options WHERE option_name LIKE '_transient_%' OR option_name LIKE '_site_transient_%'"
# Remove transient activation data and normalize WPForms timestamps.
wp db query "DELETE FROM wp_options WHERE option_name LIKE '_transient_%' OR option_name LIKE '_site_transient_%' OR option_name = 'recently_activated'"
wp db query "UPDATE wp_options SET option_value = '6.0.$FIXED_TIMESTAMP' WHERE option_name = 'schema-ActionScheduler_StoreSchema'"
wp db query "UPDATE wp_options SET option_value = '3.0.$FIXED_TIMESTAMP' WHERE option_name = 'schema-ActionScheduler_LoggerSchema'"
wp db query "UPDATE wp_options SET option_value = 'a:1:{s:4:\"lite\";i:$FIXED_TIMESTAMP;}' WHERE option_name = 'wpforms_activated'"
wp db query "UPDATE wp_options SET option_value = '$FIXED_TIMESTAMP' WHERE option_name = 'wpforms_forms_first_created'"

# Remove scheduled activation work that is irrelevant to the form tests.
wp db query "TRUNCATE TABLE wp_actionscheduler_logs"
wp db query "TRUNCATE TABLE wp_actionscheduler_actions"
wp db query "TRUNCATE TABLE wp_actionscheduler_claims"
wp db query "TRUNCATE TABLE wp_actionscheduler_groups"

# Replace the cron option last so no subsequent WordPress bootstrap can add
# environment-dependent timestamps back to it before the export.
wp db query "UPDATE wp_options SET option_value = 'a:1:{s:7:\"version\";i:2;}' WHERE option_name = 'cron'"

echo "Exporting database to backup.sql..."
docker compose exec -T mysql mysqldump -u root -pexample wordpress > "$BACKUP_FILE"

# Strip the mysqldump timestamp comment from the last line so diffs are clean.
sed -i '/^-- Dump completed on /d' "$BACKUP_FILE"
# Normalize the platform header and strip the mysqldump timestamp comment so
# the generated fixture is identical on macOS and Linux hosts.
sed -i.bak -e '1s/ ([^()]*)$//' -e '/^-- Dump completed on /d' "$BACKUP_FILE"
rm -f "$BACKUP_FILE.bak"
Comment thread
eugene-manuilov marked this conversation as resolved.

echo "Tearing down containers..."
docker compose --profile generate down -v
Expand Down
233 changes: 226 additions & 7 deletions tests/playwright/docker/mariadb/backup.sql

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Plugin Name: E2E Conversion Tracking
* Description: Enables Conversion Tracking during E2E tests based on a cookie set by the Playwright fixture.
*
* @package Google\Site_Kit
* @copyright 2026 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/

if ( ! empty( $_COOKIE['_wp_test_conversion_tracking'] ) ) {
add_filter(
'pre_option_googlesitekit_conversion_tracking',
function () {
return array( 'enabled' => true );
}
);
}
1 change: 1 addition & 0 deletions tests/playwright/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default defineConfig( {
testIgnore: [
'specs/golinks.spec.ts',
'specs/enhanced-conversions/enhanced-conversions.spec.ts',
'specs/enhanced-conversions/wpforms.spec.ts',
],
},
withDesktopViewport( 'chrome::desktop', DESKTOP_CHROME ),
Expand Down
Loading
Loading