Skip to content

Commit

Permalink
Merge pull request #9504 from google/fix/#9432-property-change-behaviour
Browse files Browse the repository at this point in the history
Retain choice to display visitor groups on reset
  • Loading branch information
hussain-t authored Oct 15, 2024
2 parents 897ae08 + c5abb4f commit b021e95
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 11 deletions.
18 changes: 13 additions & 5 deletions includes/Core/User/Audience_Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ protected function get_default() {
* Merges an array of settings to update.
*
* @since 1.124.0
* @since n.e.x.t Allow setting `null` for `configuredAudiences`.
*
* @param array $partial Partial settings array to save.
* @return bool True on success, false on failure.
Expand All @@ -66,14 +67,17 @@ public function merge( array $partial ) {
$settings = $this->get();
$partial = array_filter(
$partial,
function ( $value ) {
return null !== $value;
}
function ( $value, $key ) {
// Allow setting `null` for `configuredAudiences`.
return 'configuredAudiences' === $key ? true : null !== $value;
},
ARRAY_FILTER_USE_BOTH
);

$allowed_settings = array(
'configuredAudiences' => true,
'isAudienceSegmentationWidgetHidden' => true,
'didSetAudiences' => true,
);

$updated = array_intersect_key( $partial, $allowed_settings );
Expand All @@ -93,6 +97,7 @@ function ( $value ) {
* Gets the callback for sanitizing the setting's value before saving.
*
* @since 1.124.0
* @since n.e.x.t Allow setting `null` for `configuredAudiences`.
*
* @return callable Sanitize callback.
*/
Expand All @@ -104,8 +109,11 @@ protected function get_sanitize_callback() {

$sanitized_settings = array();

if ( isset( $settings['configuredAudiences'] ) ) {
$sanitized_settings['configuredAudiences'] = Sanitize::sanitize_string_list( $settings['configuredAudiences'] );
// Allow setting `null` for `configuredAudiences`.
if ( array_key_exists( 'configuredAudiences', $settings ) ) {
$sanitized_settings['configuredAudiences'] = is_null( $settings['configuredAudiences'] )
? null
: Sanitize::sanitize_string_list( $settings['configuredAudiences'] );
}

if ( isset( $settings['isAudienceSegmentationWidgetHidden'] ) ) {
Expand Down
11 changes: 9 additions & 2 deletions includes/Modules/Analytics_4/Reset_Audiences.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,15 @@ public function reset_audience_data() {
}
}

// Reset the users audience settings, such as configured audiences.
$this->audience_settings->delete();
// Reset the user's audience settings.
if ( $this->audience_settings->has() ) {
$this->audience_settings->merge(
array(
'configuredAudiences' => null,
'didSetAudiences' => false,
),
);
}
}

// Restore original user.
Expand Down
47 changes: 44 additions & 3 deletions tests/phpunit/integration/Core/User/Audience_SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ public function data_audience_settings() {
array( 'configuredAudiences' => array( 'validAudienceResourceName1', 'validAudienceResourceName2', 'validAudienceResourceName3' ) ),
array( 'configuredAudiences' => array( 'validAudienceResourceName1', 'validAudienceResourceName2', 'validAudienceResourceName3' ) ),
),
'null configuredAudiences setting' => array(
array(
'configuredAudiences' => null,
),
array(
'configuredAudiences' => null,
),
),
'null isAudienceSegmentationWidgetHidden flag' => array(
array(
'isAudienceSegmentationWidgetHidden' => null,
Expand Down Expand Up @@ -251,15 +259,36 @@ public function test_merge() {
$this->audience_settings->get()
);

// Make sure that we can't set wrong format for the isAudienceSegmentationWidgetHidden property
// Make sure that we can't set wrong format (or `null`) for the isAudienceSegmentationWidgetHidden property
$this->audience_settings->set( $original_settings );
$this->audience_settings->merge( array( 'isAudienceSegmentationWidgetHidden' => null ) );
$this->assertEqualSetsWithIndex( $original_settings, $this->audience_settings->get() );

// Make sure that we can't set wrong format for the configuredAudiences property
// Make sure that we can't set wrong format for the configuredAudiences property.
$this->audience_settings->set( $original_settings );
$this->audience_settings->merge( array( 'configuredAudiences' => false ) );
$this->assertEqualSetsWithIndex(
array_merge(
$original_settings,
array(
'configuredAudiences' => array(),
)
),
$this->audience_settings->get()
);

// Make sure we can set `null` for the configuredAudiences property.
$this->audience_settings->set( $original_settings );
$this->audience_settings->merge( array( 'configuredAudiences' => null ) );
$this->assertEqualSetsWithIndex( $original_settings, $this->audience_settings->get() );
$this->assertEqualSetsWithIndex(
array_merge(
$original_settings,
array(
'configuredAudiences' => null,
)
),
$this->audience_settings->get()
);
}

public function test_merge__did_set_audiences() {
Expand Down Expand Up @@ -295,5 +324,17 @@ public function test_merge__did_set_audiences() {
),
$this->audience_settings->get()
);

// Make sure that we can't set wrong format (or `null`) for the
// `didSetAudiences` property.
$this->audience_settings->merge( array( 'didSetAudiences' => null ) );
$this->assertEqualSetsWithIndex(
array(
'configuredAudiences' => array(),
'isAudienceSegmentationWidgetHidden' => false,
'didSetAudiences' => true,
),
$this->audience_settings->get()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,15 @@ public function test_reset_audience_data() {
$this->assertFalse( array_key_exists( $dismissed_item, $user_dismissed_items ) );
}

// Confirm the user's audience settings have been reset.
// Confirm the user's applicable audience settings have been reset.
$audience_settings = $this->audience_settings->get();
foreach ( array_keys( $default_user_audience_settings ) as $key ) {
// `isAudienceSegmentationWidgetHidden` should not be reset.
if ( 'isAudienceSegmentationWidgetHidden' === $key ) {
$this->assertEquals( $activated_user_audience_settings[ $key ], $audience_settings[ $key ] );
} else {
$this->assertEquals( $default_user_audience_settings[ $key ], $audience_settings[ $key ] );
}
}
}

Expand Down

0 comments on commit b021e95

Please sign in to comment.