-
Notifications
You must be signed in to change notification settings - Fork 21
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
API PULL - Block syncing when notifications are enabled #2326
API PULL - Block syncing when notifications are enabled #2326
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## add/grant-access-to-google-wpcom-app-in-setting-page #2326 +/- ##
======================================================================================
Coverage 61.9% 61.9%
- Complexity 4398 4415 +17
======================================================================================
Files 761 761
Lines 22308 22369 +61
Branches 533 534 +1
======================================================================================
+ Hits 13802 13849 +47
- Misses 8054 8067 +13
- Partials 452 453 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
|
…nto add/block-push-products
Notice I didn't include testing steps for Sync Product. This is because of an issue in bookings https://github.com/woocommerce/woocommerce-bookings/issues/3784 However, if bookings is not installed it should work perfectly. (Blocked when WPCOM API is authorized, working otherwise) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @puntope for working on this! It worked as expected however I left some comments about the code that we could discuss a bit more.
@@ -103,7 +103,7 @@ public function is_google_connected(): bool { | |||
* @since 1.13.0 | |||
* @return boolean | |||
*/ | |||
public function is_ready_for_syncing(): bool { | |||
public function is_ready(): bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think if we add a @SInCE indicating that this function was renamed? Otherwise, it seems like is_ready()
has been there since 1.13.0. However, I'm not sure if renaming the function to is_ready
is necessary. To me, "syncing" implies that my site and the MC will be synchronized, regardless of the method used ("push" or "pull").
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adjusted (I rolled back the renaming)
* @return bool | ||
* @since x.x.x | ||
*/ | ||
public function should_sync(): bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be better to say something like "should_notify"? To me, "sync" is ambiguous as I mentioned earlier; it could refer to either a push or pull method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it to should_push()
src/Product/SyncerHooks.php
Outdated
@@ -294,6 +316,25 @@ protected function handle_delete_product( int $product_id ) { | |||
} | |||
} | |||
|
|||
/** | |||
* Send the notification for product deletion |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd phrase it as "schedule a job to send the product deletion notification" because it doesn't directly send the notification.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adjusted
/** | ||
* Get whether Merchant Center is connected and ready for syncing data. | ||
* | ||
* @return bool | ||
*/ | ||
public function is_mc_ready_for_syncing(): bool { | ||
return $this->merchant_center->is_ready_for_syncing(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about we leave this as it is and simply replace $this->merchant_center->is_ready_for_syncing()
with something like $this->merchant_center->is_ready_for_syncing() && ! $this->merchant_center->should_notify()
? We could apply the same change to the other jobs as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm towards keeping it like it's. That function seems a bit redundant and has attached a full interface for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Besides that, all the is_mc_ready_for_syncing are doing the same in all the classes. So doesn't seem a very useful function.
Is there any reason you found for keeping it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have any specific reason; I was simply curious why we need to remove it now when we'll be removing all this code in the next stages anyway.
$update_by_object = function ( int $product_id, WC_Product $product ) { | ||
$this->handle_update_products( [ $product ] ); | ||
}; | ||
|
||
$update_by_id = function ( int $product_id ) { | ||
$product = $this->wc->maybe_get_product( $product_id ); | ||
if ( $product instanceof WC_Product ) { | ||
$this->handle_update_products( [ $product ] ); | ||
} | ||
}; | ||
|
||
$pre_delete = function ( int $product_id ) { | ||
$this->handle_pre_delete_product( $product_id ); | ||
}; | ||
|
||
$delete = function ( int $product_id ) { | ||
$this->handle_delete_product( $product_id ); | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason for moving these as methods instead of keeping them as anonymous functions? My understanding was that the idea behind using anonymous functions was to make it more difficult for other plugins to unhook these actions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is mainly for the testing.
For example for doing
$this->assertFalse( has_action( 'woocommerce_new_coupon', [ $this->syncer_hooks, 'update_by_id' ] ) );
src/Product/ProductSyncer.php
Outdated
if ( ! $this->merchant_center->should_sync() ) { | ||
do_action( | ||
'woocommerce_gla_error', | ||
'Cannot sync any products because they are being fetched automatically.', | ||
__METHOD__ | ||
); | ||
|
||
throw new ProductSyncerException( | ||
__( | ||
'Manual product sync will not run if the automatic data fetching is enabled. Please review your configuration in Google Listing and Ads settings.', | ||
'google-listings-and-ads' | ||
) | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to test these errors, but I noticed that validate_merchant_center_setup
is only triggered when the product syncers job is processing the items. So theoretically, we shouldn't encounter this error. However, I think it's a good idea to keep it just in case. While reviewing the code, I saw that for the notification part, we don't have anything similar to "validate_merchant_center_setup". We only check if the notification service is enabled, but not the MC setup. should we add something similar to the notification? Or am I missing something?
return apply_filters( 'woocommerce_gla_notification_job_can_schedule', $this->notifications_service->is_enabled() && parent::can_schedule( $args ), $this->get_job_name(), $args ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to test these errors, but I noticed that validate_merchant_center_setup is only triggered when the product syncers job is processing the items. So theoretically, we shouldn't encounter this error.
You can trigger it in ConnectionTest Page for example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only check if the notification service is enabled, but not the MC setup. should we add something similar to the notification? Or am I missing something?
We can do so in a follow up. I added the item to the list.
src/Product/ProductSyncer.php
Outdated
do_action( 'woocommerce_gla_error', 'Cannot sync any products before setting up Google Merchant Center.', __METHOD__ ); | ||
|
||
throw new ProductSyncerException( __( 'Google Merchant Center has not been set up correctly. Please review your configuration.', 'google-listings-and-ads' ) ); | ||
} | ||
|
||
if ( ! $this->merchant_center->should_sync() ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also what do you think if we add some tests for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests were already added in
ProductSyncerTest - https://github.com/woocommerce/google-listings-and-ads/pull/2326/files#diff-e1a250ff800d7687a728767bcc4817c6ca40f2fa9e97d9846e4b967ff338ee1fR551
CouponSyncerTest - https://github.com/woocommerce/google-listings-and-ads/pull/2326/files#diff-19117240c30f979181c4332c987f76f2ab19a5fd4b460d3345c8e68d195a1116R149
Do you miss any case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my other comments about the tests.
Hi @jorgemd24 thanks for the review. I applied the renaming suggestions and clarified the rest of the comments. Can you take another look? Thx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @puntope for adjusting the naming, I think it's clearer now. I've left some comments about the tests, and I'm not sure why the package-lock.json
is committed in this PR. Anyway, the PR worked as expected so I am approving this PR in advanced! Thanks!
@@ -6,7 +6,7 @@ | |||
"packages": { | |||
"": { | |||
"name": "google-listings-and-ads", | |||
"version": "2.6.2", | |||
"version": "2.6.3", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why the package-lock-json has been commited
@@ -548,6 +548,54 @@ protected function mock_google_service( $successful_products, $failed_products ) | |||
->willReturnCallback( $callback ); | |||
} | |||
|
|||
public function test_update_throws_exception_when_mc_is_blocked() { | |||
$merchant_center = $this->createMock( MerchantCenterService::class ); | |||
$merchant_center->expects( $this->any() ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to test the exception when should_push
returns false
, then we should mock is_ready_for_syncing
to return true; otherwise, should_push
never gets called. Also, I think it's better to expect an exact number instead of any, as in this case the test would pass without the method being called.
/** | ||
* Get whether Merchant Center is connected and ready for syncing data. | ||
* | ||
* @return bool | ||
*/ | ||
public function is_mc_ready_for_syncing(): bool { | ||
return $this->merchant_center->is_ready_for_syncing(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have any specific reason; I was simply curious why we need to remove it now when we'll be removing all this code in the next stages anyway.
src/Product/ProductSyncer.php
Outdated
do_action( 'woocommerce_gla_error', 'Cannot sync any products before setting up Google Merchant Center.', __METHOD__ ); | ||
|
||
throw new ProductSyncerException( __( 'Google Merchant Center has not been set up correctly. Please review your configuration.', 'google-listings-and-ads' ) ); | ||
} | ||
|
||
if ( ! $this->merchant_center->should_sync() ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my other comments about the tests.
7b6fa24
into
add/grant-access-to-google-wpcom-app-in-setting-page
Changes proposed in this Pull Request:
This PR disables Product and Coupon Sync when WC API is authorized.
ℹ️ WC API is authorized when the option
gla_wpcom_rest_api_status = approved
Detailed test instructions:
Enabled WC API
gla_wpcom_rest_api_status
as "approved"Disabled WC API
gla_wpcom_rest_api_status
or click on "Disconnect" in the Connection test page under "WPCOM REST API Status" section