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

Add - Product View Report Test #2261

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/API/Google/MerchantReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public function get_product_view_report( $next_page_token = null ): array {

try {
$product_view_data = [
'statuses' => [],
'next_page' => null,
'statuses' => [],
'next_page_token' => null,
];

$query = new MerchantProductViewReportQuery(
Expand Down
1 change: 0 additions & 1 deletion src/API/Google/Query/MerchantProductViewReportQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]
Expand Down
5 changes: 5 additions & 0 deletions src/Product/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public function find_ids( array $args = [], int $limit = -1, int $offset = 0 ):
* @return WC_Product[] Array of WooCommerce product objects
*/
public function find_by_ids( array $ids, array $args = [], int $limit = -1, int $offset = 0 ): array {
// If no product IDs are supplied then return early to avoid querying and loading every product.
if ( empty( $ids ) ) {
return [];
}

$args['include'] = $ids;

return $this->find( $args, $limit, $offset );
Expand Down
182 changes: 182 additions & 0 deletions tests/Unit/API/Google/MerchantReportTest.php
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.
Copy link
Contributor

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 in test_get_product_view_report it's not used.

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

*
* @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();
}
}
2 changes: 2 additions & 0 deletions tests/Unit/Product/ProductRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ public function test_find_by_ids() {
array_map( 'wc_get_product', $ids ),
$this->product_repository->find_by_ids( $ids )
);

$this->assertEquals( [], $this->product_repository->find_by_ids( [] ) );
}

public function test_find_synced_products() {
Expand Down
Loading