Skip to content

Commit

Permalink
Merge branch 'feature/google-api-project' into tweak/check-notificati…
Browse files Browse the repository at this point in the history
…on-statuses-on-processing

# Conflicts:
#	src/Product/ProductHelper.php
#	tests/Unit/Jobs/ProductNotificationJobTest.php
  • Loading branch information
puntope committed Jan 31, 2024
2 parents fd6db69 + e79488c commit 1467bff
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 200 deletions.
2 changes: 1 addition & 1 deletion src/Google/NotificationsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private function notification_error( int $item_id, string $topic, string $error
* @param array $args
* @return array|\WP_Error
*/
protected function do_request( array $args ): \WP_Error|array {
protected function do_request( array $args ) {
return Client::remote_request( $args, wp_json_encode( $args['body'] ) );
}

Expand Down
103 changes: 0 additions & 103 deletions src/Infrastructure/GoogleListingsAndAdsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ final class GoogleListingsAndAdsPlugin implements Plugin {
*/
private $registered_services;

/**
* The client ID.
* @var string
*/
private $client_id;

/**
* GoogleListingsAndAdsPlugin constructor.
*
Expand Down Expand Up @@ -115,13 +109,6 @@ function () {
}
}
);

add_action( 'login_form_jetpack_json_api_authorization', array( $this, 'login_form_json_api_authorization' ) );

add_filter('jetpack_xmlrpc_test_connection_response', function (){
return '1.40';
});

}

/**
Expand Down Expand Up @@ -150,94 +137,4 @@ protected function maybe_register_services(): void {

$registered = true;
}

/**
* Handles the login action for Authorizing the JSON API
*/
public function login_form_json_api_authorization() {
add_action( 'wp_login', array( $this, 'store_json_api_authorization_token' ), 10, 2 );
add_action( 'login_form', array( $this, 'preserve_action_in_login_form_for_json_api_authorization' ) );
add_filter( 'site_url', array( $this, 'post_login_form_to_signed_url' ), 10, 3 );
}

/**
* If someone logs in to approve API access, store the Access Code in usermeta.
*
* @param string $user_login Unused.
* @param WP_User $user User logged in.
*/
public function store_json_api_authorization_token( $user_login, $user ) {
$data = json_decode( base64_decode( stripslashes( $_REQUEST['data'] ) ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
$this->client_id = $data->client_id;
add_filter( 'login_redirect', array( $this, 'add_token_to_login_redirect_json_api_authorization' ), 10, 3 );
add_filter( 'allowed_redirect_hosts', array( $this, 'allow_wpcom_public_api_domain' ) );
$token = wp_generate_password( 32, false );
update_user_meta( $user->ID, 'jetpack_json_api_' . $this->client_id, $token );
}

/**
* Make sure the POSTed request is handled by the same action.
*/
public function preserve_action_in_login_form_for_json_api_authorization() {
$http_host = isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- escaped with esc_url below.
$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- escaped with esc_url below.
echo "<input type='hidden' name='action' value='jetpack_json_api_authorization' />\n";
echo "<input type='hidden' name='jetpack_json_api_original_query' value='" . esc_url( set_url_scheme( $http_host . $request_uri ) ) . "' />\n";
}

/**
* Make sure the login form is POSTed to the signed URL so we can reverify the request.
*
* @param string $url Redirect URL.
* @param string $path Path.
* @param string $scheme URL Scheme.
*/
public function post_login_form_to_signed_url( $url, $path, $scheme ) {
if ( 'wp-login.php' !== $path || ( 'login_post' !== $scheme && 'login' !== $scheme ) ) {
return $url;
}
$query_string = isset( $_SERVER['QUERY_STRING'] ) ? wp_unslash( $_SERVER['QUERY_STRING'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$parsed_url = wp_parse_url( $url );
$url = strtok( $url, '?' );
$url = "$url?{$query_string}";
if ( ! empty( $parsed_url['query'] ) ) {
$url .= "&{$parsed_url['query']}";
}

return $url;
}

/**
* Add the Access Code details to the public-api.wordpress.com redirect.
*
* @param string $redirect_to URL.
* @param string $original_redirect_to URL.
* @param WP_User $user WP_User for the redirect.
*
* @return string
*/
public function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) {
return add_query_arg(
urlencode_deep(
array(
'jetpack-code' => get_user_meta( $user->ID, 'jetpack_json_api_' . $this->client_id, true ),
'jetpack-user-id' => (int) $user->ID,
'jetpack-state' => '',
)
),
$redirect_to
);
}

/**
* Add public-api.wordpress.com to the safe redirect allowed list - only added when someone allows API access.
*
* To be used with a filter of allowed domains for a redirect.
*
* @param array $domains Allowed WP.com Environments.
*/
public function allow_wpcom_public_api_domain( $domains ) {
$domains[] = 'public-api.wordpress.com';
return $domains;
}
}
169 changes: 88 additions & 81 deletions tests/Unit/Google/NotificationsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,107 +10,114 @@

/**
* Class NotificationsServiceTest
*
* @group Notifications
* @package Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\Google
*/
class NotificationsServiceTest extends UnitTest {
class NotificationsServiceTest extends UnitTest {

/**
* @var NotificationsService
*/
public $service;
/**
* @var NotificationsService
*/
public $service;

const DUMMY_BLOG_ID = "123";
public const DUMMY_BLOG_ID = '123';

/**
* Runs before each test is executed.
*/
public function setUp(): void {
parent::setUp();
/**
* Runs before each test is executed.
*/
public function setUp(): void {
parent::setUp();

// Mock the Blog ID from Jetpack
add_filter('jetpack_options', function ( $value, $name ) {
// Mock the Blog ID from Jetpack
add_filter(
'jetpack_options',
function ( $value, $name ) {
if ( $name === 'id' ) {
return self::DUMMY_BLOG_ID;
}

return $value;
}, 10, 2 );

}
},
10,
2
);
}

/**
* Test if the route is correct
*/
public function test_route() {
$this->service = $this->get_mock( [] );
/**
* Test if the route is correct
*/
public function test_route() {
$this->service = $this->get_mock( [] );

$blog_id = self::DUMMY_BLOG_ID;
$this->assertEquals( $this->service->get_notification_url(), "https://public-api.wordpress.com/wpcom/v2/sites/{$blog_id}/partners/google/notifications" );
}

$blog_id = self::DUMMY_BLOG_ID;
$this->assertEquals( $this->service->get_notification_url(), "https://public-api.wordpress.com/wpcom/v2/sites/{$blog_id}/partners/google/notifications" );
}

/**
* Test notify() function with a call with success response.
*/
public function test_notify() {
$this->service = $this->get_mock();

$topic = 'product.create';
$item_id = 1;

$args = [
'method' => 'POST',
'timeout' => 30,
'headers' => [
'x-woocommerce-topic' => $topic,
],
'body' => [
'item_id' => $item_id,
],
'url' => $this->service->get_notification_url(),
];

$this->service->expects( $this->once() )->method( 'do_request' )->with( $args )->willReturn( [ 'code' => 200 ] );
$this->assertTrue( $this->service->notify( $item_id, $topic ) );
}

/**
* Test notify() function with a call with success response.
*/
public function test_notify() {
$this->service = $this->get_mock();

/**
* Test notify() function with a call with wp_error response.
*/
public function test_notify_wp_error() {
$this->service = $this->get_mock();

$topic = 'product.create';
$item_id = 1;
$this->service->expects( $this->once() )->method( 'do_request' )->willReturn( new \WP_Error( 'error', 'error message' ) );
$this->assertFalse( $this->service->notify( 1, 'topic' ) );
$this->assertEquals( did_action( 'woocommerce_gla_error' ), 1 );
}

$args = [
'method' => 'POST',
'timeout' => 30,
'headers' => [
'x-woocommerce-topic' => $topic
],
'body' => [
'item_id' => $item_id,
/**
* Test notify() function with a call with an error response.
*/
public function test_notify_response_error() {
$this->service = $this->get_mock();

$this->service->expects( $this->once() )->method( 'do_request' )->willReturn(
[
'response' => [
'code' => 400,
'body' => 'Bad request',
],
'url' => $this->service->get_notification_url(),
];

$this->service->expects( $this->once() )->method( 'do_request' )->with( $args )->willReturn( [ 'code' => 200 ] );
$this->assertTrue( $this->service->notify( $item_id , $topic ) );

}


/**
* Test notify() function with a call with wp_error response.
*/
public function test_notify_wp_error() {
$this->service = $this->get_mock();


$this->service->expects( $this->once() )->method( 'do_request' )->willReturn( new \WP_Error( 'error', 'error message' ) );
$this->assertFalse( $this->service->notify( 1 , 'topic') );
$this->assertEquals( did_action( 'woocommerce_gla_error' ), 1 );
}

/**
* Test notify() function with a call with an error response.
*/
public function test_notify_response_error() {

$this->service = $this->get_mock();

$this->service->expects( $this->once() )->method( 'do_request' )->willReturn( [ 'response' => [ 'code' => 400, 'body' => 'Bad request' ] ] );
$this->assertFalse( $this->service->notify( 1 , 'topic') );
$this->assertEquals( did_action( 'woocommerce_gla_error' ), 1 );
}

/**
* Mocks the service
* @return NotificationsService
*/
public function get_mock() {
return $this->getMockBuilder( NotificationsService::class)
->onlyMethods( [ 'do_request' ] )
->getMock();
}
]
);
$this->assertFalse( $this->service->notify( 1, 'topic' ) );
$this->assertEquals( did_action( 'woocommerce_gla_error' ), 1 );
}

/**
* Mocks the service
*
* @return NotificationsService
*/
public function get_mock() {
return $this->getMockBuilder( NotificationsService::class )
->onlyMethods( [ 'do_request' ] )
->getMock();
}
}
4 changes: 3 additions & 1 deletion tests/Unit/Product/ProductHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

/**
* Class ProductHelperTest
*
* @group Helpers
* @package Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\Product
*/
Expand Down Expand Up @@ -1130,12 +1131,13 @@ public function test_should_trigger_delete_notification() {

/**
* Set and save product to make it Notification Ready
*
* @param WC_Product $product
* @return WC_Product
*/
public function get_notification_ready_product( $product ) {
$product->set_status( 'publish' );
$product->add_meta_data( '_wc_gla_visibility', ChannelVisibility::SYNC_AND_SHOW , true );
$product->add_meta_data( '_wc_gla_visibility', ChannelVisibility::SYNC_AND_SHOW, true );
$product->save();
return $product;
}
Expand Down
Loading

0 comments on commit 1467bff

Please sign in to comment.