Skip to content

Commit

Permalink
Fix: code commenting.
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitrox committed Jul 4, 2024
1 parent bba5233 commit 60b0d70
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 88 deletions.
15 changes: 7 additions & 8 deletions includes/Core/Modules/Modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,13 @@ final class Modules {
* @var string[] Core module class names.
*/
private $core_modules = array(
Site_Verification::MODULE_SLUG => Site_Verification::class,
Search_Console::MODULE_SLUG => Search_Console::class,
Ads::MODULE_SLUG => Ads::class,
Analytics_4::MODULE_SLUG => Analytics_4::class,
Tag_Manager::MODULE_SLUG => Tag_Manager::class,
AdSense::MODULE_SLUG => AdSense::class,
PageSpeed_Insights::MODULE_SLUG => PageSpeed_Insights::class,
Reader_Revenue_Manager::MODULE_SLUG => Reader_Revenue_Manager::class,
Site_Verification::MODULE_SLUG => Site_Verification::class,
Search_Console::MODULE_SLUG => Search_Console::class,
Ads::MODULE_SLUG => Ads::class,
Analytics_4::MODULE_SLUG => Analytics_4::class,
Tag_Manager::MODULE_SLUG => Tag_Manager::class,
AdSense::MODULE_SLUG => AdSense::class,
PageSpeed_Insights::MODULE_SLUG => PageSpeed_Insights::class,
);

/**
Expand Down
41 changes: 19 additions & 22 deletions includes/Modules/Reader_Revenue_Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@

namespace Google\Site_Kit\Modules;

use Google\Site_Kit_Dependencies\Google\Service\SubscribewithGoogle as Google_Service_SubscribewithGoogle;
use Google\Site_Kit\Core\Authentication\Clients\Google_Site_Kit_Client;
use Google\Site_Kit\Core\Assets\Script;
use Google\Site_Kit\Core\Modules\Module;
use Google\Site_Kit\Core\Modules\Module_With_Assets;
use Google\Site_Kit\Core\Modules\Module_With_Assets_Trait;
use Google\Site_Kit\Core\Modules\Module_With_Scopes;
use Google\Site_Kit\Core\Modules\Module_With_Assets_Trait;
use Google\Site_Kit\Core\Modules\Module_With_Scopes_Trait;
use Google\Site_Kit\Core\Modules\Module_With_Service_Entity;
use Google\Site_Kit\Core\REST_API\Data_Request;
use Google\Site_Kit\Modules\Search_Console\Settings as Search_Console_Settings;
use Google\Site_Kit\Core\Util\URL;
use Google\Site_Kit\Modules\Search_Console\Settings as Search_Console_Settings;
use Google\Site_Kit_Dependencies\Google\Service\SubscribewithGoogle as Google_Service_SubscribewithGoogle;
use Exception;

/**
Expand Down Expand Up @@ -52,7 +51,7 @@ public function register() {
/**
* Gets required Google OAuth scopes for the module.
*
* @since 1.130.0
* @since n.e.x.t
*
* @return array List of Google OAuth scopes.
*/
Expand All @@ -68,7 +67,7 @@ public function get_scopes() {
* This method is invoked once by {@see Module::get_service()} to lazily set up the services when one is requested
* for the first time.
*
* @since 1.30.0
* @since n.e.x.t
*
* @param Google_Site_Kit_Client $client Google client instance.
* @return array Google services as $identifier => $service_instance pairs. Every $service_instance must be an
Expand All @@ -83,7 +82,7 @@ public function setup_services( Google_Site_Kit_Client $client ) {
/**
* Checks if the current user has access to the current configured service entity.
*
* @since 1.70.0
* @since n.e.x.t
*
* @return boolean|WP_Error
*/
Expand All @@ -101,19 +100,21 @@ public function check_service_entity_access() {
'pageSize' => 1,
)
);

return true;

} catch ( Exception $e ) {
if ( $e->getCode() === 403 ) {
return false;
}
return $this->exception_to_error( $e );
}

}

/**
* Gets map of datapoint to definition data for each.
*
* @since 1.30.0
* @since n.e.x.t
*
* @return array Map of datapoints to their definitions.
*/
Expand All @@ -126,16 +127,12 @@ protected function get_datapoint_definitions() {
/**
* Creates a request object for the given datapoint.
*
* @since 1.30.0
* @since n.e.x.t
*
* @param Data_Request $data Data request object.
* @return RequestInterface|callable|WP_Error Request object or callable on success, or WP_Error on failure.
*
* @throws Invalid_Datapoint_Exception Thrown if the datapoint does not exist.
* @throws Invalid_Param_Exception Thrown if a parameter is invalid.
* @throws Missing_Required_Param_Exception Thrown if a required parameter is missing or empty.
*
* phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
*/
protected function create_data_request( Data_Request $data ) {
switch ( "{$data->method}:{$data->datapoint}" ) {
Expand All @@ -146,7 +143,7 @@ protected function create_data_request( Data_Request $data ) {
* @var Google_Service_SubscribewithGoogle
*/
$subscribewithgoogle = $this->get_service( 'subscribewithgoogle' );
return $subscribewithgoogle->publications->listPublications( $this->get_publication_filters() );
return $subscribewithgoogle->publications->listPublications( array( 'filter' => $this->get_publication_filter() ) );
}

return parent::create_data_request( $data );
Expand All @@ -155,7 +152,7 @@ protected function create_data_request( Data_Request $data ) {
/**
* Parses a response for the given datapoint.
*
* @since 1.30.0
* @since n.e.x.t
*
* @param Data_Request $data Data request object.
* @param mixed $response Request response.
Expand All @@ -175,7 +172,7 @@ protected function parse_data_response( Data_Request $data, $response ) {
/**
* Sets up information about the module.
*
* @since 1.130.0
* @since n.e.x.t
*
* @return array Associative array of module info.
*/
Expand All @@ -190,11 +187,13 @@ protected function setup_info() {
}

/**
* Gets the filters for retrieving publications for the current property.
* Gets the filters for retrieving publications for the current site.
*
* @since n.e.x.t
*
* @return array Permutations for site hosts or URL.
*/
private function get_publication_filters() {
private function get_publication_filter() {
$sc_settings = $this->options->get( Search_Console_Settings::OPTION );
$sc_property_id = $sc_settings['propertyID'];
$raw_url = str_replace(
Expand Down Expand Up @@ -225,9 +224,7 @@ function ( $host ) {
);
}

return array(
'filter' => $filter,
);
return $filter;
}

/**
Expand Down
25 changes: 16 additions & 9 deletions tests/phpunit/integration/Core/Modules/ModulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ function ( $instance ) {

$this->assertEqualSetsWithIndex(
array(
'ads' => 'Google\\Site_Kit\\Modules\\Ads',
'adsense' => 'Google\\Site_Kit\\Modules\\AdSense',
'analytics-4' => 'Google\\Site_Kit\\Modules\\Analytics_4',
'pagespeed-insights' => 'Google\\Site_Kit\\Modules\\PageSpeed_Insights',
'search-console' => 'Google\\Site_Kit\\Modules\\Search_Console',
'site-verification' => 'Google\\Site_Kit\\Modules\\Site_Verification',
'tagmanager' => 'Google\\Site_Kit\\Modules\\Tag_Manager',
'reader-revenue-manager' => 'Google\\Site_Kit\\Modules\\Reader_Revenue_Manager',
'ads' => 'Google\\Site_Kit\\Modules\\Ads',
'adsense' => 'Google\\Site_Kit\\Modules\\AdSense',
'analytics-4' => 'Google\\Site_Kit\\Modules\\Analytics_4',
'pagespeed-insights' => 'Google\\Site_Kit\\Modules\\PageSpeed_Insights',
'search-console' => 'Google\\Site_Kit\\Modules\\Search_Console',
'site-verification' => 'Google\\Site_Kit\\Modules\\Site_Verification',
'tagmanager' => 'Google\\Site_Kit\\Modules\\Tag_Manager',
),
$available
);
Expand Down Expand Up @@ -394,7 +393,6 @@ public function provider_googlesitekit_available_modules_filter() {
Analytics_4::MODULE_SLUG,
PageSpeed_Insights::MODULE_SLUG,
Tag_Manager::MODULE_SLUG,
Reader_Revenue_Manager::MODULE_SLUG,
);

yield 'should return all the modules if filter does not change the modules keys' => array(
Expand Down Expand Up @@ -504,7 +502,16 @@ public function provider_feature_flag_modules() {
Analytics_4::MODULE_SLUG,
PageSpeed_Insights::MODULE_SLUG,
Tag_Manager::MODULE_SLUG,
);

yield 'should include the `reader-revenue-manager` module when enabled' => array(
// Module feature flag.
'rrmModule',
// Module enabled or disabled
true,
Reader_Revenue_Manager::MODULE_SLUG,
// Expected
array_merge( $default_modules, array( Reader_Revenue_Manager::MODULE_SLUG ) ),
);

yield 'should not include the `reader-revenue-manager` module when disabled' => array(
Expand Down
2 changes: 0 additions & 2 deletions tests/phpunit/integration/Modules/Analytics_4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4431,8 +4431,6 @@ public function grant_scope( $scope ) {
}

protected function set_test_resource_data_availability_dates() {
$this->enable_feature( 'audienceSegmentation' );

$test_resource_slug_audience = 'properties/12345678/audiences/12345';
$test_resource_slug_custom_dimension = 'googlesitekit_post_type';
$test_resource_slug_property = '12345678';
Expand Down
67 changes: 20 additions & 47 deletions tests/phpunit/integration/Modules/Reader_Revenue_ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,24 @@
use Google\Site_Kit\Core\Storage\Options;
use Google\Site_Kit_Dependencies\GuzzleHttp\Psr7\Request;
use Google\Site_Kit_Dependencies\GuzzleHttp\Psr7\Response;
use Google\Site_Kit\Tests\Core\Modules\Module_With_Service_Entity_ContractTests;
use Google\Site_Kit_Dependencies\Google\Service\SubscribewithGoogle as Google_Service_SubscribewithGoogle;

/**
* @group Modules
* @group Reader_Revenue_Manager
*/
class Reader_Revenue_ManagerTest extends TestCase {

use Module_With_Service_Entity_ContractTests;

/**
* Context instance.
*
* @var Context
*/
private $context;

/**
* Options object.
*
* @var Options
*/
private $options;

/**
* User object.
*
* @var WP_User
*/
private $user;

/**
* User Options object.
*
* @var User_Options
*/
private $user_options;

/**
* Authentication object.
*
Expand All @@ -71,11 +55,13 @@ public function set_up() {
parent::set_up();

$this->context = new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE );
$this->options = new Options( $this->context );
$this->user = $this->factory()->user->create_and_get( array( 'role' => 'administrator' ) );
$this->user_options = new User_Options( $this->context, $this->user->ID );
$this->authentication = new Authentication( $this->context, $this->options, $this->user_options );
$this->reader_revenue_manager = new Reader_Revenue_Manager( $this->context, $this->options, $this->user_options, $this->authentication );
$options = new Options( $this->context );
$user = $this->factory()->user->create_and_get( array( 'role' => 'administrator' ) );
$user_options = new User_Options( $this->context, $user->ID );
$this->authentication = new Authentication( $this->context, $options, $user_options );
$this->reader_revenue_manager = new Reader_Revenue_Manager( $this->context, $options, $user_options, $this->authentication );

$this->enable_feature( 'rrmModule' );
}

public function test_register() {
Expand Down Expand Up @@ -112,7 +98,7 @@ class_exists( 'Google\Site_Kit_Dependencies\Google\Service\SubscribewithGoogle'
);
}

public function test_get_data_endpoints() {
public function test_get_datapoints() {
$this->assertEqualSets(
array(
'publications',
Expand All @@ -121,7 +107,7 @@ public function test_get_data_endpoints() {
);
}

public function test_service_entity_has_access() {
public function test_get_publications() {
FakeHttp::fake_google_http_handler(
$this->reader_revenue_manager->get_client(),
function ( Request $request ) {
Expand All @@ -142,26 +128,13 @@ function ( Request $request ) {

$data = $this->reader_revenue_manager->get_data( 'publications' );
$this->assertNotWPError( $data );
$this->assertIsArray( $data );
}

public function test_service_entity_has_no_access() {
FakeHttp::fake_google_http_handler(
$this->reader_revenue_manager->get_client(),
function ( Request $request ) {
$url = parse_url( $request->getUri() );

switch ( $url['path'] ) {
case '/v1/publications':
return new Response( 200 );
}
}
);

$this->reader_revenue_manager->register();
$data = $this->reader_revenue_manager->get_data( 'publications' );
$this->assertWPError( $data );

$error_data = $data->get_error_data();
$this->assertEquals( 403, $error_data['status'] );
/**
* @return Reader_Revenue_Manager
*/
protected function get_module_with_service_entity() {
return $this->reader_revenue_manager;
}
}

0 comments on commit 60b0d70

Please sign in to comment.