-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8755 from google/enhance/8754-pax-env
Make PAX env configurable via constant
- Loading branch information
Showing
3 changed files
with
198 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
103
tests/phpunit/integration/Modules/Ads/PAX_ConfigTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
), | ||
); | ||
} | ||
} |