From df29aeadc73a33088d0d1aa33a79a9fbe4d6868c Mon Sep 17 00:00:00 2001 From: Eason Su Date: Wed, 12 Jul 2023 17:57:03 +0800 Subject: [PATCH 1/2] Provide a more detailed reason when unable to retrieve a site meta token via `SiteVerification::get_token`. --- src/API/Google/SiteVerification.php | 16 +++++++++---- .../Unit/API/Google/SiteVerificationTest.php | 23 ++++++++++--------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/API/Google/SiteVerification.php b/src/API/Google/SiteVerification.php index b86612409f..da44ecfd44 100644 --- a/src/API/Google/SiteVerification.php +++ b/src/API/Google/SiteVerification.php @@ -3,6 +3,7 @@ namespace Automattic\WooCommerce\GoogleListingsAndAds\API\Google; +use Automattic\WooCommerce\GoogleListingsAndAds\Exception\ExceptionWithResponseData; use Automattic\WooCommerce\GoogleListingsAndAds\Internal\ContainerAwareTrait; use Automattic\WooCommerce\GoogleListingsAndAds\Internal\Interfaces\ContainerAwareInterface; use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareInterface; @@ -27,6 +28,7 @@ class SiteVerification implements ContainerAwareInterface, OptionsAwareInterface { use ContainerAwareTrait; + use ExceptionTrait; use OptionsAwareTrait; use PluginHelper; @@ -94,7 +96,7 @@ public function verify_site( string $site_url ) { * @param string $identifier The URL of the site to verify (including protocol). * * @return string The meta tag to be used for verification. - * @throws Exception When unable to retrieve meta token. + * @throws ExceptionWithResponseData When unable to retrieve meta token. */ protected function get_token( string $identifier ): string { /** @var SiteVerificationService $service */ @@ -116,9 +118,15 @@ protected function get_token( string $identifier ): string { $response = $service->webResource->getToken( $post_body ); } catch ( GoogleServiceException $e ) { do_action( 'woocommerce_gla_sv_client_exception', $e, __METHOD__ ); - throw new Exception( - __( 'Unable to retrieve site verification token.', 'google-listings-and-ads' ), - $e->getCode() + + $errors = $this->get_exception_errors( $e ); + + throw new ExceptionWithResponseData( + /* translators: %s Error message */ + sprintf( __( 'Unable to retrieve site verification token: %s', 'google-listings-and-ads' ), reset( $errors ) ), + $e->getCode(), + null, + [ 'errors' => $errors ] ); } diff --git a/tests/Unit/API/Google/SiteVerificationTest.php b/tests/Unit/API/Google/SiteVerificationTest.php index 6f508fdcbf..0a2fe51f12 100644 --- a/tests/Unit/API/Google/SiteVerificationTest.php +++ b/tests/Unit/API/Google/SiteVerificationTest.php @@ -4,8 +4,10 @@ namespace Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\API\Google; use Automattic\WooCommerce\GoogleListingsAndAds\API\Google\SiteVerification; +use Automattic\WooCommerce\GoogleListingsAndAds\Exception\ExceptionWithResponseData; use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsInterface; use Automattic\WooCommerce\GoogleListingsAndAds\Tests\Framework\UnitTest; +use Automattic\WooCommerce\GoogleListingsAndAds\Tests\Tools\HelperTrait\MerchantTrait; use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\League\Container\Container; use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\Exception as GoogleServiceException; use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\SiteVerification as SiteVerificationService; @@ -23,6 +25,8 @@ */ class SiteVerificationTest extends UnitTest { + use MerchantTrait; + /** @var MockObject|OptionsInterface $options */ protected $options; @@ -71,18 +75,15 @@ public function test_verify_site_invalid_url() { public function test_verify_site_token_exception() { $this->verification_service->webResource ->method( 'getToken' ) - ->willThrowException( new GoogleServiceException( 'error', 400 ) ); + ->willThrowException( $this->get_google_service_exception( 400, 'No available tokens' ) ); - try { - $this->verification->verify_site( $this->site_url ); - } catch ( Exception $e ) { - $this->assertEquals( 1, did_action( 'woocommerce_gla_site_verify_failure' ) ); - $this->assertEquals( 400, $e->getCode() ); - $this->assertEquals( - 'Unable to retrieve site verification token.', - $e->getMessage() - ); - } + $this->expectException( ExceptionWithResponseData::class ); + $this->expectExceptionCode( 400 ); + $this->expectExceptionMessage( 'Unable to retrieve site verification token: No available tokens' ); + + $this->verification->verify_site( $this->site_url ); + + $this->assertEquals( 1, did_action( 'woocommerce_gla_site_verify_failure' ) ); } public function test_verify_site_insert_exception() { From 2cab1cd369be2b7f5cfb7945617ba8160bc828bc Mon Sep 17 00:00:00 2001 From: Eason Su Date: Wed, 12 Jul 2023 17:58:05 +0800 Subject: [PATCH 2/2] Provide a more detailed reason when unable to verify a site token via `SiteVerification::insert`. --- src/API/Google/SiteVerification.php | 14 +++++++++---- .../Unit/API/Google/SiteVerificationTest.php | 21 ++++++++----------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/API/Google/SiteVerification.php b/src/API/Google/SiteVerification.php index da44ecfd44..96fc978bb5 100644 --- a/src/API/Google/SiteVerification.php +++ b/src/API/Google/SiteVerification.php @@ -139,7 +139,7 @@ protected function get_token( string $identifier ): string { * * @param string $identifier The URL of the site to verify (including protocol). * - * @throws Exception When unable to verify token. + * @throws ExceptionWithResponseData When unable to verify token. */ protected function insert( string $identifier ) { /** @var SiteVerificationService $service */ @@ -160,9 +160,15 @@ protected function insert( string $identifier ) { $service->webResource->insert( self::VERIFICATION_METHOD, $post_body ); } catch ( GoogleServiceException $e ) { do_action( 'woocommerce_gla_sv_client_exception', $e, __METHOD__ ); - throw new Exception( - __( 'Unable to insert site verification.', 'google-listings-and-ads' ), - $e->getCode() + + $errors = $this->get_exception_errors( $e ); + + throw new ExceptionWithResponseData( + /* translators: %s Error message */ + sprintf( __( 'Unable to insert site verification: %s', 'google-listings-and-ads' ), reset( $errors ) ), + $e->getCode(), + null, + [ 'errors' => $errors ] ); } } diff --git a/tests/Unit/API/Google/SiteVerificationTest.php b/tests/Unit/API/Google/SiteVerificationTest.php index 0a2fe51f12..1e1af42502 100644 --- a/tests/Unit/API/Google/SiteVerificationTest.php +++ b/tests/Unit/API/Google/SiteVerificationTest.php @@ -91,18 +91,15 @@ public function test_verify_site_insert_exception() { $this->verification_service->webResource ->method( 'insert' ) - ->willThrowException( new GoogleServiceException( 'error', 400 ) ); - - try { - $this->verification->verify_site( $this->site_url ); - } catch ( Exception $e ) { - $this->assertEquals( 1, did_action( 'woocommerce_gla_site_verify_failure' ) ); - $this->assertEquals( 400, $e->getCode() ); - $this->assertEquals( - 'Unable to insert site verification.', - $e->getMessage() - ); - } + ->willThrowException( $this->get_google_service_exception( 400, 'No necessary verification token.' ) ); + + $this->expectException( ExceptionWithResponseData::class ); + $this->expectExceptionCode( 400 ); + $this->expectExceptionMessage( 'Unable to insert site verification: No necessary verification token.' ); + + $this->verification->verify_site( $this->site_url ); + + $this->assertEquals( 1, did_action( 'woocommerce_gla_site_verify_failure' ) ); } public function test_verify_site() {