Skip to content

Commit

Permalink
Extract setting user access token to a new trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
techanvil committed Jan 12, 2023
1 parent 70e4d78 commit 93a6b27
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
34 changes: 34 additions & 0 deletions tests/phpunit/includes/UserAuthenticationTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* UserAuthenticationTrait
*
* @package Google\Site_Kit\Tests
* @copyright 2023 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/

namespace Google\Site_Kit\Tests;

use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Authentication\Token;
use Google\Site_Kit\Core\Storage\User_Options;

trait UserAuthenticationTrait {

/**
* Sets the access token for the specified user.
*
* @param int $user_id The user ID to set the access token for.
* @param string $access_token The access token to use.
*/
protected function set_user_access_token( $user_id, $access_token ) {
$user_options = new User_Options( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ), $user_id );
$token = new Token( $user_options );
$token->set(
array(
'access_token' => $access_token,
)
);
}
}
11 changes: 3 additions & 8 deletions tests/phpunit/integration/Modules/Analytics_4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Authentication\Authentication;
use Google\Site_Kit\Core\Authentication\Token;
use Google\Site_Kit\Core\Dismissals\Dismissed_Items;
use Google\Site_Kit\Core\Modules\Module;
use Google\Site_Kit\Core\Modules\Module_Sharing_Settings;
Expand All @@ -32,6 +31,7 @@
use Google\Site_Kit\Tests\Core\Modules\Module_With_Settings_ContractTests;
use Google\Site_Kit\Tests\FakeHttpClient;
use Google\Site_Kit\Tests\TestCase;
use Google\Site_Kit\Tests\UserAuthenticationTrait;
use Google\Site_Kit_Dependencies\Google\Service\GoogleAnalyticsAdmin\GoogleAnalyticsAdminV1betaDataStream;
use Google\Site_Kit_Dependencies\Google\Service\GoogleAnalyticsAdmin\GoogleAnalyticsAdminV1betaDataStreamWebStreamData;
use Google\Site_Kit_Dependencies\GuzzleHttp\Message\Request;
Expand All @@ -47,6 +47,7 @@ class Analytics_4Test extends TestCase {
use Module_With_Settings_ContractTests;
use Module_With_Owner_ContractTests;
use Module_With_Service_Entity_ContractTests;
use UserAuthenticationTrait;

/**
* Context object.
Expand Down Expand Up @@ -1024,13 +1025,7 @@ protected function setup_user_authentication( $access_token, $user_id = null ) {
$user_id = $this->user->ID;
}

$restore_user = $this->user_options->switch_user( $user_id );
$this->authentication->get_oauth_client()->set_token(
array(
'access_token' => $access_token,
)
);
$restore_user();
$this->set_user_access_token( $user_id, $access_token );
}

/**
Expand Down

0 comments on commit 93a6b27

Please sign in to comment.