diff --git a/php/class-admin.php b/php/class-admin.php index 70b2ebb3e..0b213acf9 100644 --- a/php/class-admin.php +++ b/php/class-admin.php @@ -487,17 +487,25 @@ protected function save_settings( $submission, $data ) { $this->add_admin_notice( 'error_notice', __( 'Settings updated successfully', 'cloudinary' ), 'success' ); $analytics = $this->plugin->get_component( 'analytics' ); - if ( $analytics ) { - $analytics->track( - 'settings_saved', - 'settings', - null, - array( - 'page' => $submission, - 'changed_keys' => $changed_keys, - ) + if ( $analytics && $analytics->is_enabled() ) { + $params = array( + 'page' => $submission, + 'changed_keys' => $changed_keys, ); + // Saves of a feature page carry the full feature + // configuration as it stands after the save, flat like + // `feature_config_snapshot`, so every such save is a + // baseline and the changed keys show what was deliberately + // overridden. Other pages can't change it, so it is left + // out. Only allowlisted settings are included: secrets and + // free text never leave the site. + if ( isset( Analytics::FEATURE_SETTINGS[ $submission ] ) ) { + $params = array_merge( $analytics->get_feature_snapshot(), $params ); + } + + $analytics->track( 'settings_saved', 'settings', null, $params ); + if ( 'gallery' === $submission ) { $this->track_gallery_configured( $analytics, $data ); } diff --git a/php/class-analytics.php b/php/class-analytics.php index 61df3b29c..ac5ad4bc8 100644 --- a/php/class-analytics.php +++ b/php/class-analytics.php @@ -65,6 +65,7 @@ public function __construct( Plugin $plugin ) { add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_script_data' ) ); add_action( 'admin_init', array( $this, 'maybe_send_smoke_event' ) ); add_action( 'admin_init', array( $this, 'maybe_send_pending_activation' ) ); + add_action( 'admin_init', array( $this, 'maybe_send_feature_snapshot' ) ); add_action( 'cloudinary_uploaded_asset', array( $this, 'maybe_first_api_consumption' ), 10, 2 ); } @@ -75,6 +76,251 @@ public function __construct( Plugin $plugin ) { const LAST_ACTIVE = '_cloudinary_last_active'; const FIRST_API_FLAG = '_cloudinary_first_api_emitted'; + /** + * Option key marking the feature-config snapshot as sent (per cloud_name). + */ + const FEATURE_SNAPSHOT_FLAG = '_cloudinary_feature_snapshot_emitted'; + + /** + * Allowlist of feature settings whose values may be emitted, per settings + * page, mapped to how each value is normalised before it leaves the site. + * + * Anything not listed here is never sent as a value. Free-text fields are + * reduced to a presence boolean, since they can hold public IDs, overlay + * text or URLs. + * + * - bool: `on`/`off` toggle → true/false. + * - int: numeric field → int. + * - enum: select/radio → the value when it is one of the setting's + * own options, `other` when it isn't. + * - css_length: free-text CSS length → the length, lowercased, or dropped + * when it is anything else. + * - presence: free text → whether it is non-empty. + * - crops: per-size crop map → number of sizes with a custom crop. + * - derived: not sent as stored; get_feature_snapshot() works the value + * out from other settings. + */ + const FEATURE_SETTINGS = array( + 'image_settings' => array( + 'image_delivery' => 'bool', + 'image_optimization' => 'bool', + 'image_format' => 'enum', + 'image_quality' => 'enum', + 'image_freeform' => 'presence', + 'svg_support' => 'bool', + 'crop_sizes' => 'crops', + ), + 'lazy_loading' => array( + 'use_lazy_load' => 'bool', + 'lazy_threshold' => 'css_length', + 'lazy_placeholder' => 'enum', + 'lazy_animate' => 'bool', + 'dpr' => 'enum', + ), + 'responsive' => array( + 'enable_breakpoints' => 'bool', + 'pixel_step' => 'int', + 'breakpoints' => 'derived', + 'max_width' => 'int', + 'min_width' => 'int', + ), + 'video_settings' => array( + 'video_delivery' => 'bool', + 'video_player' => 'enum', + 'adaptive_streaming' => 'bool', + 'adaptive_streaming_mode' => 'enum', + 'video_controls' => 'bool', + 'video_loop' => 'bool', + 'video_autoplay_mode' => 'enum', + 'video_optimization' => 'bool', + 'video_format' => 'enum', + 'video_quality' => 'enum', + 'video_freeform' => 'presence', + ), + ); + + /** + * Normalises a feature setting value for emission. + * + * @param string $page The settings page slug the setting belongs to. + * @param string $key The setting slug. + * @param mixed $value The raw setting value. + * + * @return bool|int|string|null The normalised value, or null when the + * setting is not allowlisted for that page or + * its value is missing or unusable. + */ + public function normalize_feature_value( $page, $key, $value ) { + if ( ! isset( self::FEATURE_SETTINGS[ $page ][ $key ] ) || null === $value ) { + return null; + } + + switch ( self::FEATURE_SETTINGS[ $page ][ $key ] ) { + case 'bool': + return 'on' === $value || true === $value; + case 'int': + return is_numeric( $value ) ? (int) $value : null; + case 'css_length': + $value = is_string( $value ) ? trim( $value ) : ''; + + $value = strtolower( $value ); + + return preg_match( '/^\d+(\.\d+)?(px|%|r?em|vh|vw)?$/', $value ) ? $value : null; + case 'presence': + return is_string( $value ) && '' !== trim( $value ); + case 'crops': + // Each size maps to '' (default crop), '--' (crop disabled) or + // a custom transformation string. + if ( ! is_array( $value ) ) { + return 0; + } + + return count( + array_filter( + $value, + static function ( $crop ) { + return is_string( $crop ) && '' !== trim( $crop ) && '--' !== $crop; + } + ) + ); + case 'enum': + // Select and radio values are only sanitised as text, and + // filters can return anything, so only known options are sent. + if ( ! is_scalar( $value ) ) { + return null; + } + $options = $this->plugin->settings->get_setting( $key )->get_param( 'options' ); + + return is_array( $options ) && array_key_exists( (string) $value, $options ) ? (string) $value : 'other'; + default: + return null; + } + } + + /** + * Emits the one-time `feature_config_snapshot` adoption baseline. + * + * Captures the full state of the delivery features, including those left + * at their defaults, for sites that never save their settings. Sent on the + * first admin page load by an administrator once the site is connected, + * then again whenever the connected cloud_name changes (only the last one + * is kept, so switching A → B → A sends three). That covers wizard + * setups, sites connected through the `CLOUDINARY_CONNECTION_STRING` + * constant, existing installs upgrading to this version, and account + * switches. + * + * The `trigger` param tells the two cases apart: + * + * - `setup`: the site was installed, or seen unconnected, by a version + * that sends the snapshot, then connected (or switched + * account). The flag holds an empty string until then. + * - `upgrade`: the site was already connected the first time a version + * that sends the snapshot ran, so the flag was never set. + * + * A site connected through the constant before any admin page loads is + * only recognised as a setup on a fresh install. A reinstall after the + * uninstall clean-up (which keeps the plugin's DB version) and subsites of + * a network activation report `upgrade` instead. So can a site whose + * unconnected admin load writes the "not set up" marker while another + * admin load, already connected, is claiming the send: that request + * cached the flag as missing, so its add_option() overwrites the marker. + * + * @return void + */ + public function maybe_send_feature_snapshot() { + // `admin_init` also runs for admin-ajax requests, including logged-out + // ones, and for non-admin users visiting their profile. + if ( wp_doing_ajax() || ! Utils::user_can( 'analytics', 'manage_options' ) ) { + return; + } + + // Autoloaded, so once the flag is stored this check costs no query. + // It isn't stored while a connected site has analytics disabled, so + // the snapshot is still sent with the right trigger once they are + // enabled: each admin load then runs one option query. + $sent = get_option( self::FEATURE_SNAPSHOT_FLAG ); + + $connect = $this->plugin->get_component( 'connect' ); + $cloud = $connect && $connect->is_connected() ? (string) $connect->get_cloud_name() : ''; + if ( '' === $cloud ) { + if ( false === $sent ) { + // Remembers the site as not yet set up, so connecting it later + // counts as a setup rather than an upgrade. + add_option( self::FEATURE_SNAPSHOT_FLAG, '', '', true ); + } + return; + } + + if ( $sent === $cloud || ! $this->is_enabled() ) { + return; + } + + // Only one of several concurrent requests sends: add_option() upserts + // and update_option() updates, and a request writing the same cloud + // name as an earlier one changes no row, so it gets false back. + $claimed = false === $sent + ? add_option( self::FEATURE_SNAPSHOT_FLAG, $cloud, '', true ) + : update_option( self::FEATURE_SNAPSHOT_FLAG, $cloud, true ); + if ( ! $claimed ) { + return; + } + + $params = $this->get_feature_snapshot(); + $params['trigger'] = false === $sent ? 'upgrade' : 'setup'; + + $this->track( 'feature_config_snapshot', 'features', null, $params ); + } + + /** + * Reads the current, normalised value of every allowlisted feature setting. + * + * @return array + */ + public function get_feature_snapshot() { + $snapshot = array(); + foreach ( self::FEATURE_SETTINGS as $page => $settings ) { + foreach ( array_keys( $settings ) as $key ) { + $value = $this->normalize_feature_value( $page, $key, $this->plugin->settings->get_value( $key ) ); + if ( null !== $value ) { + $snapshot[ $key ] = $value; + } + } + } + + // "Max images" is only ever what the settings page derives from the + // other responsive fields, and is stored only once that page is saved. + $snapshot['breakpoints'] = $this->get_breakpoint_count(); + + return $snapshot; + } + + /** + * Counts the responsive image widths, as the settings page shows them in + * "Max images" (see `breakpoints-preview.js`): from the max width down to + * the min width, one image per breakpoint distance. + * + * @return int + */ + public function get_breakpoint_count() { + $settings = $this->plugin->settings; + $max = (int) $settings->get_value( 'max_width' ); + $min = (int) $settings->get_value( 'min_width' ); + $step = (int) $settings->get_value( 'pixel_step' ); + + // The same fallbacks as the settings page. + if ( $max < 1 ) { + $max = (int) $this->plugin->get_component( 'media' )->default_max_width(); + } + if ( $min < 1 ) { + $min = 100; + } + if ( $step < 1 ) { + $step = 50; + } + + return $max < $min ? 1 : 1 + (int) floor( ( $max - $min ) / $step ); + } + /** * Records the activation type on plugin activation (funnel step 1). * @@ -94,6 +340,9 @@ public static function stash_activation() { if ( empty( $db_version ) ) { $type = 'fresh_install'; $previous = null; + // A fresh install's first feature snapshot is a setup, even + // when it is connected through the constant from the start. + add_option( self::FEATURE_SNAPSHOT_FLAG, '', '', true ); } elseif ( version_compare( $db_version, $current, '<' ) ) { $type = 'upgrade'; $previous = $db_version; diff --git a/php/class-deactivation.php b/php/class-deactivation.php index 04cf82d69..a60fa7ea0 100644 --- a/php/class-deactivation.php +++ b/php/class-deactivation.php @@ -630,6 +630,8 @@ protected function cleanup_options() { '_cld_disable_http_upload', Report::REPORT_KEY, Media::GLOBAL_VIDEO_TRANSFORMATIONS, + Analytics::FIRST_API_FLAG, + Analytics::FEATURE_SNAPSHOT_FLAG, self::CLEANING_KEY, ) ); diff --git a/tests/e2e/features-analytics.spec.js b/tests/e2e/features-analytics.spec.js index 16e83f6f8..c007c8ff9 100644 --- a/tests/e2e/features-analytics.spec.js +++ b/tests/e2e/features-analytics.spec.js @@ -6,7 +6,7 @@ const { test, expect } = require( './fixtures' ); /** * Internal dependencies */ -const { wpEvalFile } = require( './utils/wizard' ); +const { wpCli, wpEvalFile } = require( './utils/wizard' ); const { fakeCloudinaryConnected } = require( './utils/connection' ); const { clearAnalyticsEvents, @@ -80,5 +80,50 @@ test.describe( 'Extensions & gallery analytics', () => { expect( events.length ).toBe( 1 ); expect( events[ 0 ].layout ).toBe( 'expand' ); expect( events[ 0 ].media_count ).toBe( 2 ); + + // The gallery page can't change the feature configuration, so its + // settings_saved event doesn't carry it. + const saved = findAnalyticsEvents( + readAnalyticsEvents(), + 'settings_saved' + ); + expect( saved.length ).toBe( 1 ); + expect( saved[ 0 ].page ).toBe( 'gallery' ); + expect( saved[ 0 ] ).not.toHaveProperty( 'use_lazy_load' ); + } ); +} ); + +// @serial: the snapshot's one-time flag is shared by the whole site, so a +// parallel worker's admin load could send it into that worker's log instead. +// The sending rules are covered by PHPUnit; this checks the admin wiring. +test.describe( 'Feature config snapshot', { tag: '@serial' }, () => { + test.beforeEach( () => { + fakeCloudinaryConnected(); + // Earlier admin loads already sent the snapshot for this cloud_name, + // so reset the one-time flag to simulate a fresh setup. + wpCli( [ 'option', 'delete', '_cloudinary_feature_snapshot_emitted' ] ); + clearAnalyticsEvents(); + } ); + + test( 'the first admin load once connected emits feature_config_snapshot once', async ( { + admin, + } ) => { + await admin.visitAdminPage( 'admin.php', 'page=cloudinary' ); + await admin.visitAdminPage( 'admin.php', 'page=cloudinary' ); + + const events = findAnalyticsEvents( + readAnalyticsEvents(), + 'feature_config_snapshot' + ); + expect( events.length ).toBe( 1 ); + expect( events[ 0 ].event_category ).toBe( 'features' ); + // The flag was deleted while the site was connected, as on a site + // that was already set up before upgrading. + expect( events[ 0 ].trigger ).toBe( 'upgrade' ); + + // Default values are reported even though they were never saved. + expect( typeof events[ 0 ].use_lazy_load ).toBe( 'boolean' ); + expect( typeof events[ 0 ].pixel_step ).toBe( 'number' ); + expect( typeof events[ 0 ].image_format ).toBe( 'string' ); } ); } ); diff --git a/tests/e2e/settings-analytics.spec.js b/tests/e2e/settings-analytics.spec.js index 27e294816..4661d2180 100644 --- a/tests/e2e/settings-analytics.spec.js +++ b/tests/e2e/settings-analytics.spec.js @@ -79,8 +79,73 @@ test.describe( 'Settings & navigation analytics', () => { expect( savedEvents.length ).toBe( 1 ); expect( savedEvents[ 0 ].page ).toBe( 'image_settings' ); expect( savedEvents[ 0 ].changed_keys ).toContain( 'image_quality' ); + expect( savedEvents[ 0 ].image_quality ).toBe( nextValue ); + // The whole feature configuration is sent, not just this page's. + expect( typeof savedEvents[ 0 ].use_lazy_load ).toBe( 'boolean' ); + expect( typeof savedEvents[ 0 ].breakpoints ).toBe( 'number' ); + expect( savedEvents[ 0 ] ).not.toHaveProperty( 'changed_values' ); } ); + // One feature page each, flipping a field no other spec writes to, so + // parallel workers can't change it between this test's read and save. + for ( const { page: slug, tag, field, values, expected } of [ + { + page: 'video_settings', + tag: 'select', + field: 'video_quality', + values: [ 'auto', '80' ], + expected: ( value ) => value, + }, + { + page: 'lazy_loading', + tag: 'select', + field: 'dpr', + values: [ '2X', 'max' ], + expected: ( value ) => value, + }, + { + page: 'responsive', + tag: 'input', + field: 'pixel_step', + values: [ '200', '250' ], + expected: ( value ) => Number( value ), + }, + ] ) { + test( `saving the ${ slug } page emits settings_saved with its values`, async ( { + admin, + page, + } ) => { + await admin.visitAdminPage( + 'admin.php', + `page=cloudinary_${ slug }` + ); + + // Set through the DOM: some of these fields are hidden until + // their feature is toggled on, but are still submitted. + const input = page.locator( `${ tag }[name$="[${ field }]"]` ); + const current = await input.inputValue(); + const nextValue = + values[ 0 ] === current ? values[ 1 ] : values[ 0 ]; + await input.evaluate( ( el, value ) => { + el.value = value; + el.dispatchEvent( new Event( 'input', { bubbles: true } ) ); + el.dispatchEvent( new Event( 'change', { bubbles: true } ) ); + }, nextValue ); + + await page.locator( SEL.saveButton ).click(); + await page.waitForLoadState( 'networkidle' ); + + const savedEvents = findAnalyticsEvents( + readAnalyticsEvents(), + 'settings_saved' + ); + expect( savedEvents.length ).toBe( 1 ); + expect( savedEvents[ 0 ].page ).toBe( slug ); + expect( savedEvents[ 0 ].changed_keys ).toContain( field ); + expect( savedEvents[ 0 ][ field ] ).toBe( expected( nextValue ) ); + } ); + } + test( 'dismissing an admin notice emits notice_dismissed', async ( { admin, page, diff --git a/tests/phpunit/tests/test-analytics-feature-snapshot.php b/tests/phpunit/tests/test-analytics-feature-snapshot.php new file mode 100644 index 000000000..4cc0a0ce4 --- /dev/null +++ b/tests/phpunit/tests/test-analytics-feature-snapshot.php @@ -0,0 +1,370 @@ +analytics = $plugin->get_component( 'analytics' ); + + $this->original_connected = $plugin->settings->get_param( 'connected', null ); + $this->original_cloud_name = $plugin->get_component( 'connect' )->get_cloud_name(); + $this->connect_as( 'first-cloud' ); + + delete_option( Analytics::FEATURE_SNAPSHOT_FLAG ); + wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); + + $this->sent = array(); + add_filter( 'pre_http_request', array( $this, 'capture_event' ), 1, 3 ); + } + + /** + * Restores the connection state. + * + * @return void + */ + public function tear_down() { + remove_filter( 'pre_http_request', array( $this, 'capture_event' ), 1 ); + + $plugin = get_plugin_instance(); + $plugin->settings->set_param( 'connected', $this->original_connected ); + $plugin->get_component( 'connect' )->set_credentials( array( 'cloud_name' => $this->original_cloud_name ) ); + + parent::tear_down(); + } + + /** + * Captures analytics events instead of sending them. + * + * @param false|array $preempt Whether to preempt the request. + * @param array $args The request arguments. + * @param string $url The request URL. + * + * @return false|array + */ + public function capture_event( $preempt, $args, $url ) { + if ( CLOUDINARY_ENDPOINTS_ANALYTICS !== $url ) { + return $preempt; + } + $this->sent[] = json_decode( $args['body'], true ); + + return array( + 'headers' => array(), + 'body' => '', + 'response' => array( + 'code' => 200, + 'message' => 'OK', + ), + 'cookies' => array(), + ); + } + + /** + * Marks the plugin as connected to the given cloud. + * + * @param string $cloud_name The cloud name. + * + * @return void + */ + protected function connect_as( $cloud_name ) { + $plugin = get_plugin_instance(); + $plugin->settings->set_param( 'connected', true ); + $plugin->get_component( 'connect' )->set_credentials( array( 'cloud_name' => $cloud_name ) ); + } + + /** + * Restores the plugin's install marker. + * + * @param string|false $db_version The value before the test. + * + * @return void + */ + protected function restore_db_version( $db_version ) { + if ( false === $db_version ) { + delete_option( Sync::META_KEYS['db_version'] ); + } else { + update_option( Sync::META_KEYS['db_version'], $db_version ); + } + } + + /** + * Returns the captured snapshot events. + * + * @return array[] + */ + protected function snapshots() { + return array_values( + array_filter( + $this->sent, + static function ( $event ) { + return 'feature_config_snapshot' === $event['event_name']; + } + ) + ); + } + + /** + * The snapshot is sent once per account, with the feature values. + * + * @return void + */ + public function test_sends_once_per_account() { + $this->analytics->maybe_send_feature_snapshot(); + $this->analytics->maybe_send_feature_snapshot(); + + $snapshots = $this->snapshots(); + $this->assertCount( 1, $snapshots ); + $this->assertSame( 'features', $snapshots[0]['event_category'] ); + $this->assertIsBool( $snapshots[0]['use_lazy_load'] ); + $this->assertArrayNotHasKey( 'cloudinary_url', $snapshots[0] ); + $this->assertSame( 'upgrade', $snapshots[0]['trigger'] ); + $this->assertSame( 'first-cloud', get_option( Analytics::FEATURE_SNAPSHOT_FLAG ) ); + } + + /** + * "Max images" is reported as the settings page shows it, derived from + * the other responsive fields, rather than as stored. + * + * @return void + */ + public function test_breakpoints_are_counted_like_the_settings_page() { + $settings = get_plugin_instance()->settings; + $original = array(); + $values = array( + 'max_width' => 2048, + 'min_width' => 200, + 'pixel_step' => 200, + 'breakpoints' => 42, + ); + try { + foreach ( $values as $key => $value ) { + $original[ $key ] = $settings->get_value( $key ); + $settings->get_setting( $key )->set_value( $value ); + } + + $snapshot = $this->analytics->get_feature_snapshot(); + + $settings->get_setting( 'pixel_step' )->set_value( 500 ); + $coarser = $this->analytics->get_breakpoint_count(); + } finally { + // The settings live on the plugin singleton, shared by every test. + foreach ( $original as $key => $value ) { + $settings->get_setting( $key )->set_value( $value ); + } + } + + // 2048, 1848, … 248. + $this->assertSame( 10, $snapshot['breakpoints'] ); + // 2048, 1548, 1048, 548. + $this->assertSame( 4, $coarser ); + } + + /** + * Switching to another account sends a new snapshot. + * + * @return void + */ + public function test_sends_again_after_an_account_switch() { + $this->analytics->maybe_send_feature_snapshot(); + $this->connect_as( 'second-cloud' ); + $this->analytics->maybe_send_feature_snapshot(); + + $snapshots = $this->snapshots(); + $this->assertCount( 2, $snapshots ); + $this->assertSame( 'setup', $snapshots[1]['trigger'] ); + $this->assertSame( 'second-cloud', get_option( Analytics::FEATURE_SNAPSHOT_FLAG ) ); + } + + /** + * A site seen unconnected before it is connected sends a setup snapshot. + * + * @return void + */ + public function test_connecting_after_an_unconnected_admin_load_is_a_setup() { + get_plugin_instance()->settings->set_param( 'connected', false ); + $this->analytics->maybe_send_feature_snapshot(); + $this->connect_as( 'first-cloud' ); + $this->analytics->maybe_send_feature_snapshot(); + + $snapshots = $this->snapshots(); + $this->assertCount( 1, $snapshots ); + $this->assertSame( 'setup', $snapshots[0]['trigger'] ); + } + + /** + * A fresh install's first snapshot is a setup, even when the site is + * connected from the start through the connection string constant. + * + * @return void + */ + public function test_fresh_install_is_a_setup() { + $db_version = get_option( Sync::META_KEYS['db_version'] ); + delete_option( Sync::META_KEYS['db_version'] ); + Analytics::stash_activation(); + delete_transient( Analytics::PENDING_ACTIVATION ); + $this->restore_db_version( $db_version ); + + $this->analytics->maybe_send_feature_snapshot(); + + $snapshots = $this->snapshots(); + $this->assertCount( 1, $snapshots ); + $this->assertSame( 'setup', $snapshots[0]['trigger'] ); + } + + /** + * An upgrade or reactivation doesn't mark the site as not set up. + * + * @return void + */ + public function test_upgrade_activation_leaves_the_flag_unset() { + $db_version = get_option( Sync::META_KEYS['db_version'] ); + update_option( Sync::META_KEYS['db_version'], '1.0.0' ); + Analytics::stash_activation(); + delete_transient( Analytics::PENDING_ACTIVATION ); + $this->restore_db_version( $db_version ); + + $this->assertFalse( get_option( Analytics::FEATURE_SNAPSHOT_FLAG ) ); + } + + /** + * Nothing is sent before the site is connected. + * + * @return void + */ + public function test_not_sent_while_disconnected() { + get_plugin_instance()->settings->set_param( 'connected', false ); + $this->analytics->maybe_send_feature_snapshot(); + + $this->assertCount( 0, $this->snapshots() ); + $this->assertSame( '', get_option( Analytics::FEATURE_SNAPSHOT_FLAG ) ); + } + + /** + * With analytics disabled nothing is sent, and the snapshot is still sent + * once they are enabled again. + * + * @return void + */ + public function test_deferred_while_analytics_are_disabled() { + add_filter( 'cloudinary_analytics_enabled', '__return_false' ); + $this->analytics->maybe_send_feature_snapshot(); + remove_filter( 'cloudinary_analytics_enabled', '__return_false' ); + + $this->assertCount( 0, $this->snapshots() ); + + $this->analytics->maybe_send_feature_snapshot(); + $this->assertCount( 1, $this->snapshots() ); + } + + /** + * Admin loads by non-administrators don't send the snapshot. + * + * @return void + */ + public function test_not_sent_for_non_administrators() { + wp_set_current_user( self::factory()->user->create( array( 'role' => 'subscriber' ) ) ); + $this->analytics->maybe_send_feature_snapshot(); + + $this->assertCount( 0, $this->snapshots() ); + } + + /** + * Admin-ajax requests don't send the snapshot. + * + * @return void + */ + public function test_not_sent_during_ajax_requests() { + add_filter( 'wp_doing_ajax', '__return_true' ); + $this->analytics->maybe_send_feature_snapshot(); + remove_filter( 'wp_doing_ajax', '__return_true' ); + + $this->assertCount( 0, $this->snapshots() ); + } + + /** + * When another request already added the flag, this one doesn't send. + * + * @return void + */ + public function test_only_the_request_that_adds_the_flag_sends() { + // A concurrent request has stored the flag, but this one's option + // caches were loaded before that: get_option() sees no flag, and + // add_option() skips its own existence check, so its upsert hits the + // existing row. The upsert must change nothing and nothing be sent. + add_option( Analytics::FEATURE_SNAPSHOT_FLAG, 'first-cloud', '', true ); + $alloptions = wp_load_alloptions(); + unset( $alloptions[ Analytics::FEATURE_SNAPSHOT_FLAG ] ); + wp_cache_set( 'alloptions', $alloptions, 'options' ); + wp_cache_set( 'notoptions', array( Analytics::FEATURE_SNAPSHOT_FLAG => true ), 'options' ); + + $this->assertFalse( get_option( Analytics::FEATURE_SNAPSHOT_FLAG ) ); + $this->analytics->maybe_send_feature_snapshot(); + wp_cache_delete( 'alloptions', 'options' ); + wp_cache_delete( 'notoptions', 'options' ); + + $this->assertCount( 0, $this->snapshots() ); + } + + /** + * When another request already claimed a site marked as not set up, this + * one doesn't send. + * + * @return void + */ + public function test_only_the_request_that_updates_the_flag_sends() { + // This request read the "not set up" marker, but a concurrent one has + // since stored the cloud name: the update_option() gate must fail. + add_option( Analytics::FEATURE_SNAPSHOT_FLAG, 'first-cloud', '', true ); + add_filter( 'pre_option_' . Analytics::FEATURE_SNAPSHOT_FLAG, '__return_empty_string' ); + $this->analytics->maybe_send_feature_snapshot(); + remove_filter( 'pre_option_' . Analytics::FEATURE_SNAPSHOT_FLAG, '__return_empty_string' ); + + $this->assertCount( 0, $this->snapshots() ); + } +} diff --git a/tests/phpunit/tests/test-analytics-feature-values.php b/tests/phpunit/tests/test-analytics-feature-values.php new file mode 100644 index 000000000..7d7b32d9c --- /dev/null +++ b/tests/phpunit/tests/test-analytics-feature-values.php @@ -0,0 +1,159 @@ +analytics = get_plugin_instance()->get_component( 'analytics' ); + } + + /** + * Settings outside the allowlist, including secrets, are never emitted. + * + * @return void + */ + public function test_non_allowlisted_settings_are_dropped() { + $this->assertNull( $this->analytics->normalize_feature_value( 'connect', 'cloudinary_url', 'cloudinary://key:secret@demo' ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'connect', 'cloudinary_folder', 'my-folder' ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'lazy_loading', 'lazy_custom_color', 'rgba(0,0,0,1)' ) ); + } + + /** + * A setting is only allowlisted on its own settings page. + * + * @return void + */ + public function test_settings_are_allowlisted_per_page() { + $this->assertSame( 'avif', $this->analytics->normalize_feature_value( 'image_settings', 'image_format', 'avif' ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'video_settings', 'image_format', 'avif' ) ); + } + + /** + * A missing value is left out rather than reported as off. + * + * @return void + */ + public function test_missing_values_are_dropped() { + $this->assertNull( $this->analytics->normalize_feature_value( 'lazy_loading', 'use_lazy_load', null ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'image_settings', 'image_freeform', null ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'image_settings', 'crop_sizes', null ) ); + } + + /** + * On/off toggles become booleans. + * + * @return void + */ + public function test_toggles_become_booleans() { + $this->assertTrue( $this->analytics->normalize_feature_value( 'lazy_loading', 'use_lazy_load', 'on' ) ); + $this->assertFalse( $this->analytics->normalize_feature_value( 'lazy_loading', 'use_lazy_load', 'off' ) ); + $this->assertFalse( $this->analytics->normalize_feature_value( 'video_settings', 'adaptive_streaming', '' ) ); + } + + /** + * Numeric fields become ints, and unreadable ones are dropped. + * + * @return void + */ + public function test_numbers_become_ints() { + $this->assertSame( 200, $this->analytics->normalize_feature_value( 'responsive', 'pixel_step', '200' ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'responsive', 'pixel_step', '' ) ); + } + + /** + * Select and radio values pass through when they are one of the + * setting's options. + * + * @return void + */ + public function test_known_options_stay_strings() { + $this->assertSame( 'avif', $this->analytics->normalize_feature_value( 'image_settings', 'image_format', 'avif' ) ); + $this->assertSame( 'auto:good', $this->analytics->normalize_feature_value( 'image_settings', 'image_quality', 'auto:good' ) ); + $this->assertSame( '80', $this->analytics->normalize_feature_value( 'image_settings', 'image_quality', 80 ) ); + $this->assertSame( 'on-scroll', $this->analytics->normalize_feature_value( 'video_settings', 'video_autoplay_mode', 'on-scroll' ) ); + } + + /** + * Select and radio values that aren't one of the setting's options are + * reported as `other`, so stored free text never leaves the site. + * + * @return void + */ + public function test_unknown_options_become_other() { + $this->assertSame( 'other', $this->analytics->normalize_feature_value( 'image_settings', 'image_quality', "Jane's shop" ) ); + $this->assertSame( 'other', $this->analytics->normalize_feature_value( 'lazy_loading', 'dpr', '3X' ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'image_settings', 'image_format', array( 'avif' ) ) ); + } + + /** + * "Max images" is never sent as stored. + * + * @return void + */ + public function test_derived_settings_are_not_normalised() { + $this->assertNull( $this->analytics->normalize_feature_value( 'responsive', 'breakpoints', '12' ) ); + } + + /** + * The lazy-loading threshold is only sent when it is a CSS length. + * + * @return void + */ + public function test_lazy_threshold_only_keeps_css_lengths() { + $this->assertSame( '100px', $this->analytics->normalize_feature_value( 'lazy_loading', 'lazy_threshold', ' 100px ' ) ); + $this->assertSame( '1.5em', $this->analytics->normalize_feature_value( 'lazy_loading', 'lazy_threshold', '1.5em' ) ); + $this->assertSame( '20%', $this->analytics->normalize_feature_value( 'lazy_loading', 'lazy_threshold', '20%' ) ); + $this->assertSame( '100px', $this->analytics->normalize_feature_value( 'lazy_loading', 'lazy_threshold', '100PX' ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'lazy_loading', 'lazy_threshold', 'jane@example.com' ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'lazy_loading', 'lazy_threshold', '' ) ); + } + + /** + * Free-text transformations are reduced to a presence flag. + * + * @return void + */ + public function test_freeform_is_reduced_to_presence() { + $this->assertTrue( $this->analytics->normalize_feature_value( 'image_settings', 'image_freeform', 'l_text:Arial_20:Jane Doe' ) ); + $this->assertFalse( $this->analytics->normalize_feature_value( 'video_settings', 'video_freeform', ' ' ) ); + } + + /** + * Crop sizes are summarised as the number of sizes with a custom crop. + * + * @return void + */ + public function test_crop_sizes_count_custom_crops() { + $crops = array( + '150x150' => 'c_thumb,g_face', + '300x300' => '', + '1024x768' => '--', + '768x0' => 'c_fill,g_auto', + ); + + $this->assertSame( 2, $this->analytics->normalize_feature_value( 'image_settings', 'crop_sizes', $crops ) ); + $this->assertSame( 0, $this->analytics->normalize_feature_value( 'image_settings', 'crop_sizes', '' ) ); + } +}