From 5de10d539b0e79c44c60c3e2ae15caa8f20cfa7d Mon Sep 17 00:00:00 2001 From: Jorge M Date: Sun, 18 Feb 2024 20:52:14 +0100 Subject: [PATCH 1/7] Remove unused field --- src/API/Google/Query/MerchantProductViewReportQuery.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/API/Google/Query/MerchantProductViewReportQuery.php b/src/API/Google/Query/MerchantProductViewReportQuery.php index 9519be3157..076928198f 100644 --- a/src/API/Google/Query/MerchantProductViewReportQuery.php +++ b/src/API/Google/Query/MerchantProductViewReportQuery.php @@ -45,7 +45,6 @@ protected function set_initial_columns() { $this->columns( [ 'id' => 'product_view.id', - 'offer_id' => 'product_view.offer_id', 'expiration_date' => 'product_view.expiration_date', 'status' => 'product_view.aggregated_destination_status', ] From ffec2f161f57aab2f6372a01ee5292dc16859d1b Mon Sep 17 00:00:00 2001 From: Jorge M Date: Sun, 18 Feb 2024 20:52:52 +0100 Subject: [PATCH 2/7] Fix default value next_page_token --- src/API/Google/MerchantReport.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/API/Google/MerchantReport.php b/src/API/Google/MerchantReport.php index f341183317..e3ec0f3f90 100644 --- a/src/API/Google/MerchantReport.php +++ b/src/API/Google/MerchantReport.php @@ -70,7 +70,7 @@ public function get_product_view_report( $next_page_token = null ): array { try { $product_view_data = [ 'statuses' => [], - 'next_page' => null, + 'next_page_token' => null, ]; $query = new MerchantProductViewReportQuery( From 502e7be7a0e255d59abe105758e83ad5a382bf2e Mon Sep 17 00:00:00 2001 From: Jorge M Date: Sun, 18 Feb 2024 20:53:28 +0100 Subject: [PATCH 3/7] Add tests for the MerchantReportTest Product View --- tests/Unit/API/Google/MerchantReportTest.php | 170 +++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 tests/Unit/API/Google/MerchantReportTest.php diff --git a/tests/Unit/API/Google/MerchantReportTest.php b/tests/Unit/API/Google/MerchantReportTest.php new file mode 100644 index 0000000000..517433afda --- /dev/null +++ b/tests/Unit/API/Google/MerchantReportTest.php @@ -0,0 +1,170 @@ +shopping_client = $this->createMock( ShoppingContent::class ); + $this->product_helper = $this->createMock( ProductHelper::class ); + $this->shopping_client->reports = $this->createMock( Reports::class ); + + $this->options = $this->createMock( OptionsInterface::class ); + $this->merchant_report = new MerchantReport( $this->shopping_client, $this->product_helper ); + $this->merchant_report->set_options_object( $this->options ); + } + + /** + * Creates a product view with the given id, status and expiration date. + * + * @param string $mc_id The MC center id. + * @param string $status The status of the product. + * @param DateTime|null $expiration_date The expiration date of the product. + * + * @return ProductView + */ + protected function create_product_view_product_status( string $mc_id, string $status = 'ELIGIBLE', $expiration_date = null ): ProductView { + $expiration_date = $expiration_date ?? new DateTime( 'tomorrow', wp_timezone() ); + $product_view = new ProductView(); + $google_date = new GoogleDate(); + $google_date->setYear( $expiration_date->format( 'Y' ) ); + $google_date->setMonth( $expiration_date->format( 'm' ) ); + $google_date->setDay( $expiration_date->format( 'd' ) ); + $product_view->setExpirationDate( $google_date ); + $product_view->setAggregatedDestinationStatus( $status ); + $product_view->setId( $mc_id ); + return $product_view; + } + + public function test_get_free_listing_metrics() { + $test_merchant_id = 432; + $wc_product_id_1 = 882; + $wc_product_id_2 = 883; + $wc_product_id_3 = 884; + + add_filter( + 'woocommerce_gla_product_view_report_page_size', + function () { + return 500; + } + ); + + $this->options->method( 'get_merchant_id' )->willReturn( $test_merchant_id ); + + $this->product_helper->method( 'get_wc_product_id' )->will( + $this->returnCallback( + function ( $mc_id ) use ( $wc_product_id_1, $wc_product_id_2, $wc_product_id_3 ) { + if ( $mc_id === 'online:en:ES:gla_' . $wc_product_id_1 ) { + return $wc_product_id_1; + } + + if ( $mc_id === 'online:en:ES:gla_' . $wc_product_id_2 ) { + return $wc_product_id_2; + } + + if ( $mc_id === 'online:en:ES:gla_' . $wc_product_id_3 ) { + return $wc_product_id_3; + } + + return 0; + } + ) + ); + + $product_view_1 = $this->create_product_view_product_status( 'online:en:ES:gla_' . $wc_product_id_1 ); + $product_view_2 = $this->create_product_view_product_status( 'online:en:ES:gla_' . $wc_product_id_2, 'NOT_ELIGIBLE_OR_DISAPPROVED' ); + $product_view_3 = $this->create_product_view_product_status( 'online:en:ES:external' . $wc_product_id_3 ); + + $report_row_1 = new ReportRow(); + $report_row_1->setProductView( $product_view_1 ); + + $report_row_2 = new ReportRow(); + $report_row_2->setProductView( $product_view_2 ); + + $report_row_3 = new ReportRow(); + $report_row_3->setProductView( $product_view_3 ); + + $response = $this->createMock( SearchResponse::class ); + $response->expects( $this->once() ) + ->method( 'getResults' ) + ->willReturn( [ $report_row_1, $report_row_2, $report_row_3 ] ); + + $response->expects( $this->once() ) + ->method( 'getNextPageToken' ) + ->willReturn( null ); + + $search_request = new SearchRequest(); + $search_request->setQuery( + 'SELECT product_view.id,product_view.expiration_date,product_view.aggregated_destination_status FROM ProductView' + ); + + $search_request->setPageSize( 500 ); + + $this->shopping_client->reports->expects( $this->once() ) + ->method( 'search' ) + ->with( $test_merchant_id, $search_request ) + ->willReturn( $response ); + + $this->assertEquals( + [ + 'statuses' => [ + $wc_product_id_1 => [ + 'product_id' => $wc_product_id_1, + 'status' => MCStatus::APPROVED, + 'expiration_date' => $this->convert_shopping_content_date( $product_view_1->getExpirationDate() ), + ], + $wc_product_id_2 => [ + 'product_id' => $wc_product_id_2, + 'status' => MCStatus::DISAPPROVED, + 'expiration_date' => $this->convert_shopping_content_date( $product_view_2->getExpirationDate() ), + ], + ], + 'next_page_token' => null, + ], + $this->merchant_report->get_product_view_report() + ); + } +} From 3b403a25ae861dc65aada7e1272d212e6593c692 Mon Sep 17 00:00:00 2001 From: Jorge M Date: Sun, 18 Feb 2024 20:59:49 +0100 Subject: [PATCH 4/7] Test exception --- tests/Unit/API/Google/MerchantReportTest.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/Unit/API/Google/MerchantReportTest.php b/tests/Unit/API/Google/MerchantReportTest.php index 517433afda..928a39ecef 100644 --- a/tests/Unit/API/Google/MerchantReportTest.php +++ b/tests/Unit/API/Google/MerchantReportTest.php @@ -15,8 +15,10 @@ use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\SearchRequest; use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\SearchResponse; use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Resource\Reports; +use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Exception as GoogleException; use Automattic\WooCommerce\GoogleListingsAndAds\API\Google\ShoppingContentDateTrait; use DateTime; +use Exception; use PHPUnit\Framework\MockObject\MockObject; defined( 'ABSPATH' ) || exit; @@ -79,7 +81,7 @@ protected function create_product_view_product_status( string $mc_id, string $st return $product_view; } - public function test_get_free_listing_metrics() { + public function test_get_product_view_report() { $test_merchant_id = 432; $wc_product_id_1 = 882; $wc_product_id_2 = 883; @@ -167,4 +169,18 @@ function ( $mc_id ) use ( $wc_product_id_1, $wc_product_id_2, $wc_product_id_3 ) $this->merchant_report->get_product_view_report() ); } + + public function test_get_product_view_report_with_exception() { + $this->options->method( 'get_merchant_id' )->willReturn( 432 ); + + $this->shopping_client->reports->expects( $this->once() ) + ->method( 'search' ) + ->will( + $this->throwException( new GoogleException( 'Test exception' ) ) + ); + + $this->expectException( Exception::class ); + $this->expectExceptionMessage( 'Unable to retrieve Product View Report.' ); + $this->merchant_report->get_product_view_report(); + } } From 952207c189482dd9762b41b1c3b5c715c4482d0f Mon Sep 17 00:00:00 2001 From: Jorge M Date: Mon, 19 Feb 2024 10:59:57 +0100 Subject: [PATCH 5/7] Test with a different page size --- tests/Unit/API/Google/MerchantReportTest.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/tests/Unit/API/Google/MerchantReportTest.php b/tests/Unit/API/Google/MerchantReportTest.php index 928a39ecef..e44c1bc7b9 100644 --- a/tests/Unit/API/Google/MerchantReportTest.php +++ b/tests/Unit/API/Google/MerchantReportTest.php @@ -85,12 +85,12 @@ public function test_get_product_view_report() { $test_merchant_id = 432; $wc_product_id_1 = 882; $wc_product_id_2 = 883; - $wc_product_id_3 = 884; + $page_size = 800; add_filter( 'woocommerce_gla_product_view_report_page_size', - function () { - return 500; + function () use ( $page_size ) { + return $page_size; } ); @@ -98,7 +98,7 @@ function () { $this->product_helper->method( 'get_wc_product_id' )->will( $this->returnCallback( - function ( $mc_id ) use ( $wc_product_id_1, $wc_product_id_2, $wc_product_id_3 ) { + function ( $mc_id ) use ( $wc_product_id_1, $wc_product_id_2 ) { if ( $mc_id === 'online:en:ES:gla_' . $wc_product_id_1 ) { return $wc_product_id_1; } @@ -107,10 +107,6 @@ function ( $mc_id ) use ( $wc_product_id_1, $wc_product_id_2, $wc_product_id_3 ) return $wc_product_id_2; } - if ( $mc_id === 'online:en:ES:gla_' . $wc_product_id_3 ) { - return $wc_product_id_3; - } - return 0; } ) @@ -118,7 +114,7 @@ function ( $mc_id ) use ( $wc_product_id_1, $wc_product_id_2, $wc_product_id_3 ) $product_view_1 = $this->create_product_view_product_status( 'online:en:ES:gla_' . $wc_product_id_1 ); $product_view_2 = $this->create_product_view_product_status( 'online:en:ES:gla_' . $wc_product_id_2, 'NOT_ELIGIBLE_OR_DISAPPROVED' ); - $product_view_3 = $this->create_product_view_product_status( 'online:en:ES:external' . $wc_product_id_3 ); + $product_view_3 = $this->create_product_view_product_status( 'online:en:ES:external' ); $report_row_1 = new ReportRow(); $report_row_1->setProductView( $product_view_1 ); @@ -143,7 +139,7 @@ function ( $mc_id ) use ( $wc_product_id_1, $wc_product_id_2, $wc_product_id_3 ) 'SELECT product_view.id,product_view.expiration_date,product_view.aggregated_destination_status FROM ProductView' ); - $search_request->setPageSize( 500 ); + $search_request->setPageSize( $page_size ); $this->shopping_client->reports->expects( $this->once() ) ->method( 'search' ) From 19b4db54cbdafc36c9af1f80aeefeffd8d79edf8 Mon Sep 17 00:00:00 2001 From: Jorge M Date: Mon, 19 Feb 2024 11:01:50 +0100 Subject: [PATCH 6/7] Fix phpcs --- src/API/Google/MerchantReport.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/API/Google/MerchantReport.php b/src/API/Google/MerchantReport.php index e3ec0f3f90..c27dbf3a37 100644 --- a/src/API/Google/MerchantReport.php +++ b/src/API/Google/MerchantReport.php @@ -69,7 +69,7 @@ public function get_product_view_report( $next_page_token = null ): array { try { $product_view_data = [ - 'statuses' => [], + 'statuses' => [], 'next_page_token' => null, ]; From 6a9a0f96d577930181961b6c7fed1587a6225513 Mon Sep 17 00:00:00 2001 From: Jorge M Date: Tue, 20 Feb 2024 17:06:39 +0100 Subject: [PATCH 7/7] Move get_merchant_id to setup --- tests/Unit/API/Google/MerchantReportTest.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/Unit/API/Google/MerchantReportTest.php b/tests/Unit/API/Google/MerchantReportTest.php index e44c1bc7b9..92f84fdced 100644 --- a/tests/Unit/API/Google/MerchantReportTest.php +++ b/tests/Unit/API/Google/MerchantReportTest.php @@ -44,6 +44,9 @@ class MerchantReportTest extends UnitTest { /** @var MerchantReport $merchant_report */ protected $merchant_report; + /** Merchant ID */ + protected const MERCHANT_ID = 432; + /** * Runs before each test is executed. @@ -57,6 +60,8 @@ public function setUp(): void { $this->options = $this->createMock( OptionsInterface::class ); $this->merchant_report = new MerchantReport( $this->shopping_client, $this->product_helper ); $this->merchant_report->set_options_object( $this->options ); + + $this->options->method( 'get_merchant_id' )->willReturn( self::MERCHANT_ID ); } /** @@ -82,10 +87,9 @@ protected function create_product_view_product_status( string $mc_id, string $st } public function test_get_product_view_report() { - $test_merchant_id = 432; - $wc_product_id_1 = 882; - $wc_product_id_2 = 883; - $page_size = 800; + $wc_product_id_1 = 882; + $wc_product_id_2 = 883; + $page_size = 800; add_filter( 'woocommerce_gla_product_view_report_page_size', @@ -94,8 +98,6 @@ function () use ( $page_size ) { } ); - $this->options->method( 'get_merchant_id' )->willReturn( $test_merchant_id ); - $this->product_helper->method( 'get_wc_product_id' )->will( $this->returnCallback( function ( $mc_id ) use ( $wc_product_id_1, $wc_product_id_2 ) { @@ -143,7 +145,7 @@ function ( $mc_id ) use ( $wc_product_id_1, $wc_product_id_2 ) { $this->shopping_client->reports->expects( $this->once() ) ->method( 'search' ) - ->with( $test_merchant_id, $search_request ) + ->with( self::MERCHANT_ID, $search_request ) ->willReturn( $response ); $this->assertEquals( @@ -167,8 +169,6 @@ function ( $mc_id ) use ( $wc_product_id_1, $wc_product_id_2 ) { } public function test_get_product_view_report_with_exception() { - $this->options->method( 'get_merchant_id' )->willReturn( 432 ); - $this->shopping_client->reports->expects( $this->once() ) ->method( 'search' ) ->will(