Skip to content

Commit

Permalink
Merge pull request #2738 from woocommerce/fix/2737-clear-presync-errors
Browse files Browse the repository at this point in the history
Clear previous errors after completing sync
  • Loading branch information
mikkamp authored Dec 19, 2024
2 parents 912976c + 8284510 commit d9c3385
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Product/ProductHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ public function mark_as_synced( WC_Product $product, GoogleProduct $google_produ
$google_ids = array_unique( array_merge( $current_google_ids, [ $google_product->getTargetCountry() => $google_product->getId() ] ) );
$this->meta_handler->update_google_ids( $product, $google_ids );

// check if product is synced completely and remove any previous errors if it is
// check if product is synced for main target country and remove any previous errors if it is
$synced_countries = array_keys( $google_ids );
$target_countries = $this->target_audience->get_target_countries();
if ( count( $synced_countries ) === count( $target_countries ) && empty( array_diff( $synced_countries, $target_countries ) ) ) {
if ( empty( array_diff( $synced_countries, $target_countries ) ) ) {
$this->meta_handler->delete_errors( $product );
$this->meta_handler->delete_failed_sync_attempts( $product );
$this->meta_handler->delete_sync_failed_at( $product );
Expand Down
34 changes: 25 additions & 9 deletions tests/Unit/Product/ProductHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function test_mark_as_synced_keeps_existing_google_ids( WC_Product $produ
*
* @dataProvider return_test_products
*/
public function test_mark_as_synced_doesnt_delete_errors_unless_all_target_countries_synced( WC_Product $product ) {
public function test_mark_as_synced_deletes_errors_when_main_target_countries_synced( WC_Product $product ) {
$google_product = $this->generate_google_product_mock();

$this->target_audience->expects( $this->any() )
Expand All @@ -114,19 +114,35 @@ public function test_mark_as_synced_doesnt_delete_errors_unless_all_target_count

$this->product_helper->mark_as_synced( $product, $google_product );

$this->assertEquals( [ 'Error 1', 'Error 2' ], $this->product_meta->get_errors( $product ) );
$this->assertEquals( 1, $this->product_meta->get_failed_sync_attempts( $product ) );
$this->assertEquals( 12345, $this->product_meta->get_sync_failed_at( $product ) );

$google_product_2 = $this->generate_google_product_mock( null, 'AU' );

$this->product_helper->mark_as_synced( $product, $google_product_2 );

$this->assertEmpty( $this->product_meta->get_errors( $product ) );
$this->assertEmpty( $this->product_meta->get_failed_sync_attempts( $product ) );
$this->assertEmpty( $this->product_meta->get_sync_failed_at( $product ) );
}

/**
* @param WC_Product $product
*
* @dataProvider return_test_products
*/
public function test_mark_as_synced_doesnt_delete_errors_unless_main_target_countries_synced( WC_Product $product ) {
$google_product = $this->generate_google_product_mock();

$this->target_audience->expects( $this->any() )
->method( 'get_target_countries' )
->willReturn( [ 'AU', 'CA' ] );

// add some random errors residue from previous sync attempts
$this->product_meta->update_errors( $product, [ 'Error 1', 'Error 2' ] );
$this->product_meta->update_failed_sync_attempts( $product, 1 );
$this->product_meta->update_sync_failed_at( $product, 12345 );

$this->product_helper->mark_as_synced( $product, $google_product );

$this->assertEquals( [ 'Error 1', 'Error 2' ], $this->product_meta->get_errors( $product ) );
$this->assertEquals( 1, $this->product_meta->get_failed_sync_attempts( $product ) );
$this->assertEquals( 12345, $this->product_meta->get_sync_failed_at( $product ) );
}

public function test_mark_as_synced_updates_both_variation_and_parent() {
$google_product = $this->generate_google_product_mock();
$parent = WC_Helper_Product::create_variation_product();
Expand Down

0 comments on commit d9c3385

Please sign in to comment.