-
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
Add - Product View Report Test #2261
Merged
jorgemd24
merged 8 commits into
feature/refactor-product-stats
from
add/merchant-center-report-test
Feb 20, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5de10d5
Remove unused field
jorgemd24 ffec2f1
Fix default value next_page_token
jorgemd24 502e7be
Add tests for the MerchantReportTest Product View
jorgemd24 3b403a2
Test exception
jorgemd24 952207c
Test with a different page size
jorgemd24 19b4db5
Fix phpcs
jorgemd24 71e2c3d
Merge branch 'feature/refactor-product-stats' into add/merchant-cente…
jorgemd24 6a9a0f9
Move get_merchant_id to setup
jorgemd24 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
<?php | ||
declare( strict_types=1 ); | ||
|
||
namespace Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\API\Google; | ||
|
||
use Automattic\WooCommerce\GoogleListingsAndAds\API\Google\MerchantReport; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Date as GoogleDate; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Value\MCStatus; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsInterface; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductHelper; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Tests\Framework\UnitTest; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\ProductView; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\ReportRow; | ||
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; | ||
|
||
/** | ||
* Class MerchantReportTest | ||
* | ||
* @package Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\API\Google | ||
*/ | ||
class MerchantReportTest extends UnitTest { | ||
|
||
use ShoppingContentDateTrait; | ||
|
||
/** @var MockObject|ShoppingContent $shopping_client */ | ||
protected $shopping_client; | ||
|
||
/** @var MockObject|ProductHelper $product_helper */ | ||
protected $product_helper; | ||
|
||
/** @var MockObject|OptionsInterface $options */ | ||
protected $options; | ||
|
||
/** @var MerchantReport $merchant_report */ | ||
protected $merchant_report; | ||
|
||
/** Merchant ID */ | ||
protected const MERCHANT_ID = 432; | ||
|
||
|
||
/** | ||
* Runs before each test is executed. | ||
*/ | ||
public function setUp(): void { | ||
parent::setUp(); | ||
$this->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 ); | ||
|
||
$this->options->method( 'get_merchant_id' )->willReturn( self::MERCHANT_ID ); | ||
} | ||
|
||
/** | ||
* 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_product_view_report() { | ||
$wc_product_id_1 = 882; | ||
$wc_product_id_2 = 883; | ||
$page_size = 800; | ||
|
||
add_filter( | ||
'woocommerce_gla_product_view_report_page_size', | ||
function () use ( $page_size ) { | ||
return $page_size; | ||
} | ||
); | ||
|
||
$this->product_helper->method( 'get_wc_product_id' )->will( | ||
$this->returnCallback( | ||
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; | ||
} | ||
|
||
if ( $mc_id === 'online:en:ES:gla_' . $wc_product_id_2 ) { | ||
return $wc_product_id_2; | ||
} | ||
|
||
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' ); | ||
|
||
$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( $page_size ); | ||
|
||
$this->shopping_client->reports->expects( $this->once() ) | ||
->method( 'search' ) | ||
->with( self::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() | ||
); | ||
} | ||
|
||
public function test_get_product_view_report_with_exception() { | ||
$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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we need the
$expiration_date
parameter? When we set up the product view's intest_get_product_view_report
it's not used.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 use the
$expiration_date
to create a "GoogleDate", which is then used to simulate the expiration_date here:https://github.com/woocommerce/google-listings-and-ads/pull/2261/files#diff-91e7d57ff211a17cd8684387ddd428407e3530dfca1487dd2ccc4fa022bec0eaR160
I could set a DateTime in the expiration_date response, but I believe this way allows us to simulate a more realistic scenario.