Skip to content

Commit

Permalink
- Making integration_data data not mandatory for the extension to f…
Browse files Browse the repository at this point in the history
…unction.

- Redoing how 'is connected' user is identified.
- Adjusting unit tests to the new 'is connected' behaviour.
  • Loading branch information
message-dimke committed Oct 22, 2024
1 parent f48b082 commit b40dfc6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
26 changes: 15 additions & 11 deletions class-pinterest-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,11 @@ public function init_api_endpoints() {
*
* @since 1.0.0
*
* @return array
* @return array {
* Access Token.
*
* @type string $access_token Decrypted Access Token
* }
*/
public static function get_access_token() {

Expand Down Expand Up @@ -815,8 +819,9 @@ public static function disconnect(): bool {
try {
// Delete all the feeds for the merchant.
FeedRegistration::maybe_delete_stale_feeds_for_merchant( '' );
// Disconnect merchant from Pinterest.
// Delete Commerce Integration.
self::delete_commerce_integration();
// Remove stored data.
self::flush_options();
// At this point we're disconnected.
return true;
Expand All @@ -835,7 +840,6 @@ public static function disconnect(): bool {
* @throws \Automattic\WooCommerce\Admin\Notes\NotesUnavailableException If the notes API is not available.
*/
public static function reset_connection() {
self::save_data( 'integration_data', array() );
self::disconnect();

TokenInvalidFailure::possibly_add_note();
Expand Down Expand Up @@ -866,13 +870,11 @@ public static function action_scheduler_reset_connection( $action_id, $e ) {
* @return void
*/
private static function flush_options() {

// Flush the whole data option.
delete_option( PINTEREST_FOR_WOOCOMMERCE_DATA_NAME );
UserInteraction::flush_options();

// Remove settings that may cause issues if stale on disconnect.
self::save_setting( 'integration_data', array() );
self::save_setting( 'account_data', null );
self::save_setting( 'tracking_advertiser', null );
self::save_setting( 'tracking_tag', null );
Expand Down Expand Up @@ -1048,8 +1050,12 @@ public static function update_commerce_integration( string $external_business_id
*/
public static function delete_commerce_integration(): bool {
try {
$external_business_id = self::get_data( 'integration_data' )['external_business_id'];
$external_business_id = self::get_data( 'integration_data' )['external_business_id'] ?? '';
if ( empty( $external_business_id ) ) {
return true;
}
Pinterest\API\APIV5::delete_commerce_integration( $external_business_id );
self::remove_data( 'integration_data' );
return true;
} catch ( PinterestApiException $e ) {
Logger::log( $e->getMessage(), 'error' );
Expand Down Expand Up @@ -1097,13 +1103,12 @@ public static function generate_external_business_id(): string {
*/
public static function update_account_data() {
try {
$integration_data = self::get_data( 'integration_data' );
$account_data = Pinterest\API\APIV5::get_account_info();

$data = array(
'username' => $account_data['username'] ?? '',
'full_name' => '',
'id' => $integration_data['id'] ?? '',
'id' => $account_data['id'] ?? '',
'image_medium_url' => $account_data['profile_image'] ?? '',
// Partner is a user who is a business account not a pinner ('BUSINESS', 'PINNER' account types).
'is_partner' => 'BUSINESS' === ( $account_data['account_type'] ?? '' ),
Expand Down Expand Up @@ -1379,13 +1384,12 @@ public static function is_setup_complete() {


/**
* Checks if connected by checking if there is integration ID in the data store.
* Checks if connected by checking if there is an access token in the data store.
*
* @return boolean
*/
public static function is_connected() {
$integration = self::get_data( 'integration_data' );
return ! empty( $integration['id'] ?? '' );
return boolval( self::get_access_token()['access_token'] ?? '' );
}


Expand Down
4 changes: 2 additions & 2 deletions tests/E2e/Pinterest401DisconnectE2eTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected function setUp(): void {
'expires_in' => 2592000,
'refresh_token_expires_in' => 31536000,
'scope' => 'ads:read ads:write catalogs:read catalogs:write pins:read pins:write user_accounts:read user_accounts:write',
'refresh_date' => 1685694065,
'refresh_time' => 1685694065,
);
Pinterest_For_Woocommerce()::save_token_data( $token_data );
$info_data = array(
Expand Down Expand Up @@ -96,7 +96,7 @@ function ($response, $parsed_args, $url) {
),
'body' => json_encode(
array(
'id' => '6491114367052267731',
'id' => '58468345645685645',
'external_business_id' => 'wordpresspinterest-6479a6713160b',
'connected_merchant_id' => '1479839719476',
'connected_user_id' => '1144266355231574943',
Expand Down
3 changes: 2 additions & 1 deletion tests/E2e/PinterestConnectE2eTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function test_successful_pinterest_connect_sets_proper_settings() {
'expires_in' => 2592000,
'refresh_token_expires_in' => 31536000,
'scope' => 'ads:read ads:write catalogs:read catalogs:write pins:read pins:write user_accounts:read user_accounts:write',
'refresh_date' => 1685694065,
'refresh_time' => 1685694065,
);
Pinterest_For_Woocommerce()::save_token_data( $token_data );
$info_data = array (
Expand Down Expand Up @@ -156,6 +156,7 @@ function ( $response, $parsed_args, $url ) {
),
'body' => json_encode(
array(
'id' => '6491114367052267731',
'username' => 'dmytromaksiuta1',
'profile_image' => 'https://i.pinimg.com/600x600_R/42/f5/36/42f5364f737aff4749a8e9046510828f.jpg',
'account_type' => 'BUSINESS',
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/HeartbeatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function setUp(): void {
public function test_cancel_jobs_removes_daily_and_hourly_as_actions() {
// Jobs will schedule only if Pinterest is connected (means integration data is set and has the ID).
Pinterest_For_Woocommerce::save_data( 'integration_data', array( 'id' => '567891567892' ) );
Pinterest_For_Woocommerce::save_token_data( array( 'access_token' => 'def5020011e6faae77c53f97dc7d36875a55ae0402fbc99e28e3b2a43580562947330a5d31fa766cca66a0a1accfb72ffcc8106e1d57b2ee68a07b34d9158a4e70e164d035c80e63a77be40fd9d0955c6aa36ff96ffcce80a4dfc2963d3511e1c71ee76b766d8f26f53a713ab3b28b3e17053e9afd0e5dd1ccedecace7514e471641ea298631edcf0310294488da09a8ae0a1a3f9fdfd5fe1d3f215be42108e064cd5baea7d6f4eb970551aaf480cc986a1b0c7bfa83df5580' ) );

$this->heartbeat->schedule_events();

Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/RefreshTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function test_schedule_event_added_with_plugin() {
public function test_schedule_event_adds_daily_action() {
// Jobs will schedule only if Pinterest is connected (means integration data is set and has the ID).
Pinterest_For_Woocommerce::save_data( 'integration_data', array( 'id' => '567891567892' ) );
Pinterest_For_Woocommerce::save_token_data( array( 'access_token' => 'def5020011e6faae77c53f97dc7d36875a55ae0402fbc99e28e3b2a43580562947330a5d31fa766cca66a0a1accfb72ffcc8106e1d57b2ee68a07b34d9158a4e70e164d035c80e63a77be40fd9d0955c6aa36ff96ffcce80a4dfc2963d3511e1c71ee76b766d8f26f53a713ab3b28b3e17053e9afd0e5dd1ccedecace7514e471641ea298631edcf0310294488da09a8ae0a1a3f9fdfd5fe1d3f215be42108e064cd5baea7d6f4eb970551aaf480cc986a1b0c7bfa83df5580' ) );

RefreshToken::schedule_event();
$this->assertEquals( 20, has_action( Heartbeat::DAILY, array( RefreshToken::class, 'handle_refresh' ) ) );
Expand All @@ -51,7 +52,7 @@ public function test_handle_refresh_does_a_call_to_refresh_a_token_if_token_abou
'token_data' => array(
'refresh_token' => Crypto::encrypt( 'pinr.refresh_token' ),
'expires_in' => $expires_in, // Access Token expires in 30 days.
'refresh_date' => $refresh_date, // Token was refreshed more than 28 days ago. Refresh.
'refresh_time' => $refresh_date, // Token was refreshed more than 28 days ago. Refresh.
),
)
);
Expand Down Expand Up @@ -99,7 +100,7 @@ public function test_handle_refresh_updates_option_with_new_tokens() {
'token_data' => array(
'refresh_token' => Crypto::encrypt( 'pinr.refresh_token' ),
'expires_in' => $expires_in, // Access Token expires in 30 days.
'refresh_date' => $refresh_date, // Token was refreshed more than 28 days ago. Refresh.
'refresh_time' => $refresh_date, // Token was refreshed more than 28 days ago. Refresh.
),
)
);
Expand Down

0 comments on commit b40dfc6

Please sign in to comment.