Skip to content

Commit

Permalink
Merge pull request #8755 from google/enhance/8754-pax-env
Browse files Browse the repository at this point in the history
Make PAX env configurable via constant
  • Loading branch information
tofumatt authored May 24, 2024
2 parents 956116e + ce2d17a commit bdade3f
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 16 deletions.
20 changes: 4 additions & 16 deletions includes/Modules/Ads.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@

namespace Google\Site_Kit\Modules;

use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Assets\Asset;
use Google\Site_Kit\Core\Assets\Assets;
use Google\Site_Kit\Core\Assets\Script;
use Google\Site_Kit\Core\Assets\Script_Data;
use Google\Site_Kit\Core\Authentication\Authentication;
use Google\Site_Kit\Core\Modules\Module;
use Google\Site_Kit\Core\Modules\Module_Settings;
use Google\Site_Kit\Core\Modules\Module_With_Assets;
Expand All @@ -31,8 +28,7 @@
use Google\Site_Kit\Core\Modules\Tags\Module_Tag_Matchers;
use Google\Site_Kit\Core\Permissions\Permissions;
use Google\Site_Kit\Core\Site_Health\Debug_Data;
use Google\Site_Kit\Core\Storage\Options;
use Google\Site_Kit\Core\Storage\User_Options;
use Google\Site_Kit\Modules\Ads\PAX_Config;
use Google\Site_Kit\Modules\Ads\Settings;
use Google\Site_Kit\Modules\Ads\Has_Tag_Guard;
use Google\Site_Kit\Modules\Ads\Tag_Matchers;
Expand Down Expand Up @@ -122,17 +118,9 @@ protected function setup_assets() {
return array();
}

return array(
'authAccess' => array(
'oauthTokenAccess' => array(
'token' => (string) $this->authentication->get_oauth_client()->get_access_token(),
),
),
'locale' => substr( $this->context->get_locale( 'user' ), 0, 2 ),
'debuggingConfig' => array(
'env' => 'PROD',
),
);
$config = new PAX_Config( $this->context, $this->authentication->token() );

return $config->get();
},
)
);
Expand Down
91 changes: 91 additions & 0 deletions includes/Modules/Ads/PAX_Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* Class Google\Site_Kit\Modules\Ads\PAX_Config
*
* @package Google\Site_Kit
* @copyright 2024 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/

namespace Google\Site_Kit\Modules\Ads;

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

/**
* Class representing PAX configuration.
*
* @since n.e.x.t
* @access private
* @ignore
*/
class PAX_Config {

/**
* Context instance.
*
* @since n.e.x.t
* @var Context
*/
private $context;

/**
* Token instance.
*
* @since n.e.x.t
* @var Token
*/
private $token;

/**
* Constructor.
*
* @since n.e.x.t
*
* @param Context $context Context instance.
* @param Token $token Token instance.
*/
public function __construct( Context $context, Token $token ) {
$this->context = $context;
$this->token = $token;
}

/**
* Gets the configuration data.
*
* @since n.e.x.t
* @return array
*/
public function get() {
$token = $this->token->get();

return array(
'authAccess' => array(
'oauthTokenAccess' => array(
'token' => $token['access_token'] ?? '',
),
),
'locale' => substr( $this->context->get_locale( 'user' ), 0, 2 ),
'debuggingConfig' => array(
'env' => $this->get_env(),
),
);
}

/**
* Gets the environment configuration.
*
* @since n.e.x.t
* @return string
*/
protected function get_env() {
$allowed = array( 'PROD', 'QA_PROD' );

if ( defined( 'GOOGLESITEKIT_PAX_ENV' ) && in_array( GOOGLESITEKIT_PAX_ENV, $allowed, true ) ) {
return GOOGLESITEKIT_PAX_ENV;
}

return 'PROD';
}
}
103 changes: 103 additions & 0 deletions tests/phpunit/integration/Modules/Ads/PAX_ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* PAX_ConfigTest
*
* @package Google\Site_Kit\Tests\Modules\Ads
* @copyright 2024 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\Modules\Ads;

use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Authentication\Token;
use Google\Site_Kit\Core\Storage\User_Options;
use Google\Site_Kit\Modules\Ads\PAX_Config;
use Google\Site_Kit\Tests\TestCase;
use Google\Site_Kit\Tests\UserAuthenticationTrait;

/**
* @group Modules
* @group Ads
* @group PAX
*/
class PAX_ConfigTest extends TestCase {
use UserAuthenticationTrait;

private $context;
private $user_options;
private $token;

public function set_up() {
parent::set_up();
$this->context = new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE );
$this->user_options = new User_Options( $this->context );
$this->token = new Token( $this->user_options );
}

public function test_get() {
$config = new PAX_Config( $this->context, $this->token );
$this->assertFalse( $this->token->has() );

$data = $config->get();

$this->assertEquals(
array(
'authAccess' => array(
'oauthTokenAccess' => array(
'token' => '', // No token yet.
),
),
'debuggingConfig' => array(
'env' => 'PROD',
),
'locale' => 'en',
),
$data
);
}

public function test_get__token() {
$user_id = $this->factory()->user->create();
$this->user_options->switch_user( $user_id );
$this->set_user_access_token( $user_id, 'test-access-token' );
$config = new PAX_Config( $this->context, $this->token );

$data = $config->get();

$this->assertEquals( 'test-access-token', $data['authAccess']['oauthTokenAccess']['token'] );
}

/**
* @param string $const_value
* @param string $expected
* @dataProvider data_envs
* @runInSeparateProcess
*/
public function test_get__env( $const_value, $expected ) {
$config = new PAX_Config( $this->context, $this->token );
define( 'GOOGLESITEKIT_PAX_ENV', $const_value );

$data = $config->get();

$this->assertEquals( $expected, $data['debuggingConfig']['env'] );
}

public function data_envs() {
return array(
'PROD' => array(
'PROD',
'PROD',
),
'QA_PROD' => array(
'QA_PROD',
'QA_PROD',
),
'OTHER' => array(
'OTHER',
'PROD',
),
);
}
}

0 comments on commit bdade3f

Please sign in to comment.