Skip to content

Commit 9f3e012

Browse files
authored
Merge pull request #578 from woocommerce/fix/STORMA-39-ensure-settings-defaults-populated
Fix STORMA-39: Backfill missing settings defaults on fresh activation
2 parents 9322c1c + f9cf37d commit 9f3e012

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

includes/class-wc-google-analytics.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ public function __construct() {
3838
$this->init_form_fields();
3939
$this->init_settings();
4040

41+
// Backfill any settings keys not yet stored in the database.
42+
// On a fresh activation, maybe_set_defaults() writes a partial settings array before
43+
// init_settings() runs. WC_Integration::init_settings() only applies defaults when no
44+
// settings exist at all, so keys missing from a partial array would be absent from
45+
// $this->settings. We fill them in here using the same fallback logic as get_option().
46+
foreach ( $this->get_form_fields() as $key => $field ) {
47+
if ( ! array_key_exists( $key, $this->settings ) ) {
48+
$this->settings[ $key ] = $this->get_field_default( $field );
49+
}
50+
}
51+
4152
add_action( 'admin_notices', array( $this, 'universal_analytics_upgrade_notice' ) );
4253

4354
include_once 'class-wc-abstract-google-analytics-js.php';

tests/unit-tests/Configuration.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,48 @@ public function test_track_settings_linker_allow_incoming_normalization() {
372372
$this->assertEquals( 'no', $data['wc-google-analytics']['linker_allow_incoming_enabled'] );
373373
}
374374

375+
/**
376+
* Test that $this->settings is fully populated when the database contains only the partial
377+
* defaults written by maybe_set_defaults() on a fresh activation.
378+
*
379+
* @return void
380+
*/
381+
public function test_settings_are_complete_after_partial_activation_defaults() {
382+
// Simulate what maybe_set_defaults() writes on a fresh install.
383+
update_option( 'woocommerce_google_analytics_settings', [ 'ga_product_identifier' => 'product_id' ] );
384+
385+
$ga = new WC_Google_Analytics();
386+
387+
$reflection = new \ReflectionProperty( WC_Google_Analytics::class, 'settings' );
388+
$reflection->setAccessible( true );
389+
$settings = $reflection->getValue( $ga );
390+
391+
$expected_keys = [
392+
'ga_product_identifier',
393+
'ga_id',
394+
'ga_support_display_advertising',
395+
'ga_404_tracking_enabled',
396+
'ga_linker_allow_incoming_enabled',
397+
'ga_ecommerce_tracking_enabled',
398+
'ga_event_tracking_enabled',
399+
'ga_enhanced_remove_from_cart_enabled',
400+
'ga_enhanced_product_impression_enabled',
401+
'ga_enhanced_product_click_enabled',
402+
'ga_enhanced_product_detail_view_enabled',
403+
'ga_enhanced_checkout_process_enabled',
404+
'ga_linker_cross_domains',
405+
];
406+
407+
foreach ( $expected_keys as $key ) {
408+
$this->assertArrayHasKey( $key, $settings, "Expected '{$key}' to be present in settings after construction with partial defaults." );
409+
}
410+
411+
// The value set by maybe_set_defaults() should be preserved, not overwritten.
412+
$this->assertEquals( 'product_id', $settings['ga_product_identifier'] );
413+
414+
delete_option( 'woocommerce_google_analytics_settings' );
415+
}
416+
375417
/**
376418
* Call a protected or private method via reflection.
377419
*

0 commit comments

Comments
 (0)