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

API PULL - Block syncing when notifications are enabled #2326

Conversation

puntope
Copy link
Contributor

@puntope puntope commented Mar 20, 2024

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

  1. Set the option gla_wpcom_rest_api_status as "approved"
  2. Go to the Connection Test page
  3. Verify you see at the end WC STAtus APi is approved.
  4. Run Sync All Products --- You will get "Error submitting products to Google: Manual product sync will not run if the automatic data fetching is enabled. Please review your configuration in Google Listing and Ads settings."
  5. Run Delete All Synced Products --- You will get "Error deleting products from Google: Manual product sync will not run if the automatic data fetching is enabled. Please review your configuration in Google Listing and Ads settings."
  6. Run Clean up All Synced Products --- You will get "Error cleaning up products: Manual product sync will not run if the automatic data fetching is enabled. Please review your configuration in Google Listing and Ads settings."
  7. For the coupons there is no test in the Connection page available. So we can only test that the jobs (delete_coupon and update_coupon) are not scheduled when the coupon is created, updated or deleted. But this was tested already in previous works.

Disabled WC API

  1. Remove the option gla_wpcom_rest_api_status or click on "Disconnect" in the Connection test page under "WPCOM REST API Status" section
  2. Run Sync All Products successfully
  3. Run Delete All Synced Products successfully
  4. Run Clean up All Synced Products successfully
  5. For the coupons there is no test in the Connection page available. So we can only test that the jobs (delete_coupon and update_coupon) are scheduled when the coupon is created, updated or deleted. But this was tested already in previous works.

@github-actions github-actions bot added changelog: add A new feature, function, or functionality was added. type: enhancement The issue is a request for an enhancement. labels Mar 20, 2024
@puntope puntope changed the title Block syncing when notifications are enabled API PULL - Block syncing when notifications are enabled Mar 20, 2024
Copy link

codecov bot commented Mar 20, 2024

Codecov Report

Attention: Patch coverage is 71.28713% with 29 lines in your changes are missing coverage. Please review.

Project coverage is 61.9%. Comparing base (5365056) to head (ac18037).
Report is 12 commits behind head on add/grant-access-to-google-wpcom-app-in-setting-page.

Additional details and impacted files

Impacted file tree graph

@@                                  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     
Flag Coverage Δ
js-unit-tests 54.1% <ø> (+0.1%) ⬆️
php-unit-tests 63.5% <71.3%> (+<0.1%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
src/Coupon/CouponSyncer.php 72.5% <100.0%> (+1.2%) ⬆️
...ternal/DependencyManagement/JobServiceProvider.php 0.0% <ø> (ø)
src/Jobs/AbstractProductSyncerBatchedJob.php 100.0% <100.0%> (ø)
src/Jobs/AbstractProductSyncerJob.php 100.0% <100.0%> (ø)
src/MerchantCenter/MerchantCenterService.php 94.2% <100.0%> (+0.1%) ⬆️
src/Product/ProductSyncer.php 98.8% <100.0%> (+0.1%) ⬆️
src/API/WP/NotificationsService.php 90.2% <0.0%> (-4.9%) ⬇️
src/Jobs/AbstractCouponSyncerJob.php 0.0% <0.0%> (ø)
src/Options/Options.php 62.3% <0.0%> (-2.4%) ⬇️
src/Coupon/SyncerHooks.php 74.5% <62.5%> (-3.1%) ⬇️
... and 1 more

... and 6 files with indirect coverage changes

@puntope puntope self-assigned this Mar 21, 2024
@puntope puntope requested a review from a team March 21, 2024 18:44
@puntope puntope marked this pull request as ready for review March 21, 2024 18:44
@puntope
Copy link
Contributor Author

puntope commented Mar 22, 2024

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)

@puntope puntope mentioned this pull request Mar 22, 2024
35 tasks
Copy link
Contributor

@jorgemd24 jorgemd24 left a 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 {
Copy link
Contributor

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").

Copy link
Contributor Author

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 {
Copy link
Contributor

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.

Copy link
Contributor Author

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()

@@ -294,6 +316,25 @@ protected function handle_delete_product( int $product_id ) {
}
}

/**
* Send the notification for product deletion
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted

Comment on lines -57 to -65
/**
* 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();
}

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Comment on lines -128 to -146
$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 );
};

Copy link
Contributor

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.

Copy link
Contributor Author

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' ] ) );

https://github.com/woocommerce/google-listings-and-ads/pull/2326/files#diff-afa7e4fae2bc017bdf473aacdcf84a1720a7f73a22e83979eda07d4696b6cd41R271

Comment on lines 365 to 378
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'
)
);
}
Copy link
Contributor

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 );

Copy link
Contributor Author

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

Copy link
Contributor Author

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.

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() ) {
Copy link
Contributor

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?

Copy link
Contributor Author

@puntope puntope Mar 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

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.

@puntope
Copy link
Contributor Author

puntope commented Mar 25, 2024

Hi @jorgemd24 thanks for the review.

I applied the renaming suggestions and clarified the rest of the comments.

Can you take another look?

Thx

@puntope puntope requested a review from jorgemd24 March 25, 2024 12:28
Copy link
Contributor

@jorgemd24 jorgemd24 left a 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",
Copy link
Contributor

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() )
Copy link
Contributor

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.

Comment on lines -57 to -65
/**
* 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();
}

Copy link
Contributor

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.

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() ) {
Copy link
Contributor

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.

@puntope puntope merged commit 7b6fa24 into add/grant-access-to-google-wpcom-app-in-setting-page Mar 26, 2024
13 checks passed
@puntope puntope deleted the add/block-push-products branch March 26, 2024 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: add A new feature, function, or functionality was added. type: enhancement The issue is a request for an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants