Skip to content

Commit

Permalink
Merge pull request #2232 from woocommerce/tweak/check-notification-st…
Browse files Browse the repository at this point in the history
…atuses-on-processing

API PULL - Check notification statuses on processing
  • Loading branch information
puntope authored Feb 5, 2024
2 parents 6001cf9 + 1467bff commit 28790ee
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 7 deletions.
22 changes: 21 additions & 1 deletion src/Jobs/Notifications/ProductNotificationJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected function process_items( array $args ): void {
$item = $args[0];
$topic = $args[1];

if ( $this->notifications_service->notify( $item, $topic ) ) {
if ( $this->can_process( $item, $topic ) && $this->notifications_service->notify( $item, $topic ) ) {
$this->set_status( $item, $this->get_after_notification_status( $topic ) );
}
}
Expand Down Expand Up @@ -130,4 +130,24 @@ protected function get_after_notification_status( string $topic ): string {
return NotificationStatus::NOTIFICATION_UPDATED;
}
}

/**
* Checks if the item can be processed based on the topic.
* This is needed because the product can change the Notification Status before
* the Job process the item.
*
* @param int $product_id
* @param string $topic
* @return bool
*/
protected function can_process( int $product_id, string $topic ): bool {
$product = $this->product_helper->get_wc_product( $product_id );
if ( str_contains( $topic, '.create' ) ) {
return $this->product_helper->should_trigger_create_notification( $product );
} elseif ( str_contains( $topic, '.delete' ) ) {
return $this->product_helper->should_trigger_delete_notification( $product );
} else {
return $this->product_helper->should_trigger_update_notification( $product );
}
}
}
8 changes: 6 additions & 2 deletions src/Product/ProductHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,17 @@ public function should_trigger_delete_notification( WC_Product $product ): bool
*/
public function has_notified_creation( WC_Product $product ): bool {
$valid_has_notified_creation_statuses = [
NotificationStatus::NOTIFICATION_PENDING_CREATE,
NotificationStatus::NOTIFICATION_CREATED,
NotificationStatus::NOTIFICATION_UPDATED,
NotificationStatus::NOTIFICATION_PENDING_UPDATE,
NotificationStatus::NOTIFICATION_PENDING_DELETE,
];

return in_array( $this->meta_handler->get_notification_status( $product ), $valid_has_notified_creation_statuses, true ) || $this->is_product_synced( $product );
return in_array(
$this->meta_handler->get_notification_status( $product ),
$valid_has_notified_creation_statuses,
true
) || $this->is_product_synced( $product );
}

/**
Expand Down
68 changes: 64 additions & 4 deletions tests/Unit/Jobs/ProductNotificationJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,16 @@ public function test_process_items_calls_notify_and_set_status_on_success() {
->with( $id, $topic )
->willReturn( true );

$this->product_helper->expects( $this->once() )
$this->product_helper->expects( $this->exactly( 2 ) )
->method( 'get_wc_product' )
->with( $id )
->willReturn( $product );

$this->product_helper->expects( $this->once() )
->method( 'should_trigger_create_notification' )
->with( $product )
->willReturn( true );

$this->product_helper->expects( $this->once() )
->method( 'set_notification_status' )
->with( $product, NotificationStatus::NOTIFICATION_CREATED );
Expand All @@ -155,8 +160,15 @@ public function test_process_items_doesnt_calls_set_status_on_failure() {
->with( $id, $topic )
->willReturn( false );

$this->product_helper->expects( $this->never() )
->method( 'get_wc_product' );
$this->product_helper->expects( $this->once() )
->method( 'get_wc_product' )
->with( $id )
->willReturn( $product );

$this->product_helper->expects( $this->once() )
->method( 'should_trigger_create_notification' )
->with( $product )
->willReturn( true );

$this->product_helper->expects( $this->never() )
->method( 'set_notification_status' );
Expand All @@ -173,10 +185,25 @@ public function test_get_after_notification_status() {
->method( 'notify' )
->willReturn( true );

$this->product_helper->expects( $this->exactly( 3 ) )
$this->product_helper->expects( $this->exactly( 6 ) )
->method( 'get_wc_product' )
->willReturn( $product );

$this->product_helper->expects( $this->once() )
->method( 'should_trigger_create_notification' )
->with( $product )
->willReturn( true );

$this->product_helper->expects( $this->once() )
->method( 'should_trigger_update_notification' )
->with( $product )
->willReturn( true );

$this->product_helper->expects( $this->once() )
->method( 'should_trigger_delete_notification' )
->with( $product )
->willReturn( true );

$this->product_helper->expects( $this->exactly( 3 ) )
->method( 'set_notification_status' )
->willReturnCallback(
Expand All @@ -195,4 +222,37 @@ function ( $id, $topic ) {
$this->job->handle_process_items_action( [ $id, 'product.delete' ] );
$this->job->handle_process_items_action( [ $id, 'product.update' ] );
}

public function test_dont_process_item_if_status_changed() {
/** @var \WC_Product $product */
$product = WC_Helper_Product::create_simple_product();
$id = $product->get_id();

$this->notification_service->expects( $this->never() )->method( 'notify' );

$this->product_helper->expects( $this->exactly( 3 ) )
->method( 'get_wc_product' )
->willReturn( $product );

$this->product_helper->expects( $this->once() )
->method( 'should_trigger_create_notification' )
->with( $product )
->willReturn( false );

$this->product_helper->expects( $this->once() )
->method( 'should_trigger_update_notification' )
->with( $product )
->willReturn( false );

$this->product_helper->expects( $this->once() )
->method( 'should_trigger_delete_notification' )
->with( $product )
->willReturn( false );

$this->product_helper->expects( $this->never() )->method( 'set_notification_status' );

$this->job->handle_process_items_action( [ $id, 'product.create' ] );
$this->job->handle_process_items_action( [ $id, 'product.delete' ] );
$this->job->handle_process_items_action( [ $id, 'product.update' ] );
}
}

0 comments on commit 28790ee

Please sign in to comment.