Prevent adding duplicate queue item by checking action and label values (https://github.com/woocommerce/woocommerce-admin/pull/5682)

* Prevent adding duplicate queue item by checking action and label values
This commit is contained in:
Moon 2020-11-18 16:30:22 -08:00 committed by GitHub
parent 4e5fa5cee9
commit 30cfb76e2e
2 changed files with 34 additions and 0 deletions

View File

@ -220,6 +220,16 @@ class CustomerEffortScoreTracks {
array()
);
$has_duplicate = array_filter(
$queue,
function ( $queue_item ) use ( $item ) {
return $queue_item['action'] === $item['action'];
}
);
if ( $has_duplicate ) {
return;
}
$queue[] = $item;
update_option(

View File

@ -49,4 +49,28 @@ class WC_Tests_CES_Tracks extends WC_Unit_Test_Case {
$this->assertCount( 1, $expected_queue_item );
}
/**
* Verify that the queue does not add duplicate item by cehcking
* action and label values.
*/
public function test_the_queue_does_not_allow_duplicate() {
// Fire the action twice to trigger the queueing process twice.
do_action( 'woocommerce_update_options' );
do_action( 'woocommerce_update_options' );
$ces = $this->ces;
$queue_items = get_option( $ces::CES_TRACKS_QUEUE_OPTION_NAME, array() );
$this->assertNotEmpty( $queue_items );
$expected_queue_item = array_filter(
$queue_items,
function ( $item ) use ( $ces ) {
return $ces::SETTINGS_CHANGE_ACTION_NAME === $item['action'];
}
);
$this->assertCount( 1, $expected_queue_item );
}
}