Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear previous errors after completing sync #2738

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading