Skip to content

Commit

Permalink
Consolidate restriction checks and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chriszarate committed Mar 7, 2025
1 parent d9e58ab commit fb76ef2
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 16 deletions.
25 changes: 18 additions & 7 deletions telemetry/pendo/class-pendo-javascript-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@

namespace Automattic\VIP\Telemetry\Pendo;

use Automattic\VIP\Telemetry\Pendo;
use WP_Error;
use function Automattic\VIP\Logstash\log2logstash;

/**
* This class enqueues the Pendo client library on pages that support it. In
* This class enqueues the Pendo client library when it is supported. In
* contrast to "Track events" which can be sent via server-to-server requests
* (see `Pendo_Track_Client`), this client library allows use of the "Pages" and
* "Features" events of Pendo, which must be sent client-side.
*
* https://support.pendo.io/hc/en-us/articles/23335876011803-Events-overview
*
* @see Pendo::enable_javascript_library()
*/
class Pendo_JavaScript_Library {
/**
Expand Down Expand Up @@ -51,7 +54,8 @@ private function __construct( string $api_key ) {

/**
* Standard singleton class except the caller does not need access to the
* instance, so we name it `init`.
* instance, so we name it `init`. Do not call this method directly; instead
* use `Pendo::enable_javascript_library()`.
*
* @param string $api_key The Pendo snippet API key.
* @return bool|WP_Error True on success or WP_Error if any error occured.
Expand Down Expand Up @@ -87,7 +91,7 @@ public static function init( string|null $api_key = null ): bool|WP_Error {
* Enqueue the Pendo client library script on supported pages.
*/
public function enqueue_scripts(): void {
if ( ! $this->should_enqueue_scripts() ) {
if ( true !== self::should_enqueue_script() ) {
return;
}

Expand Down Expand Up @@ -147,10 +151,17 @@ private function get_initialization_data(): array|WP_Error {

/**
* Determine if the Pendo client library should be enqueued for the current
* request. This is separate from the decision of whether to load Pendo at all,
* which is handled by the caller of ::init().
* request.
*/
private function should_enqueue_scripts(): bool {
return current_user_can( 'edit_posts' );
final public static function should_enqueue_script(): bool {
if ( true !== Pendo::is_pendo_enabled_for_environment() ) {
return false;
}

if ( ! current_user_can( 'edit_posts' ) ) {
return false;
}

return true;
}
}
15 changes: 6 additions & 9 deletions telemetry/pendo/class-pendo.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,26 +122,23 @@ private function get_global_event_properties( array $provided_global_event_prope
/**
* If allowed, inserts the Pendo script into the page to enable Page and
* Feature tracking.
*
* @see Pendo_JavaScript_Library::should_enqueue_script()
*/
public static function enable_javascript_library( string|null $snippet_api_key = null ): void {
// If the API key is not provided, check if it is defined in the environment.
if ( null === $snippet_api_key && defined( 'PENDO_SNIPPET_API_KEY' ) ) {
$snippet_api_key = constant( 'PENDO_SNIPPET_API_KEY' );
}

if ( true !== self::is_pendo_enabled_for_environment() ) {
public static function enable_javascript_library(): void {
if ( ! defined( 'VIP_PENDO_SNIPPET_API_KEY' ) ) {
return;
}

Pendo_JavaScript_Library::init( $snippet_api_key );
Pendo_JavaScript_Library::init( constant( 'VIP_PENDO_SNIPPET_API_KEY' ) );
}

/**
* Checks if Pendo telemetry is allowed in the current environment.
*
* @return bool
*/
private static function is_pendo_enabled_for_environment(): bool {
final public static function is_pendo_enabled_for_environment(): bool {
// Do not run if disabled via constant.
if ( defined( 'VIP_DISABLE_PENDO_TELEMETRY' ) && true === constant( 'VIP_DISABLE_PENDO_TELEMETRY' ) ) {
return false;
Expand Down
88 changes: 88 additions & 0 deletions tests/telemetry/pendo/test-class-pendo-javascript-library.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace Automattic\VIP\Telemetry;

use Automattic\Test\Constant_Mocker;
use Automattic\VIP\Telemetry\Pendo\Pendo_JavaScript_Library;
use WP_UnitTestCase;

class Pendo_JavaScript_Library_Test extends WP_UnitTestCase {
public function tearDown(): void {
Constant_Mocker::clear();
parent::tearDown();
}

public function test_enabled_for_users_with_edit_post_cap() {
$user = $this->factory()->user->create_and_get( [ 'role' => 'author' ] );
wp_set_current_user( $user->ID );

Constant_Mocker::define( 'VIP_GO_APP_ENVIRONMENT', 'production' );
Constant_Mocker::define( 'WPCOM_IS_VIP_ENV', true );

$this->assertTrue( Pendo_JavaScript_Library::should_enqueue_script() );
}

public function test_disabled_by_opt_out_constant() {
$user = $this->factory()->user->create_and_get( [ 'role' => 'author' ] );
wp_set_current_user( $user->ID );

Constant_Mocker::define( 'VIP_DISABLE_PENDO_TELEMETRY', true );
Constant_Mocker::define( 'VIP_GO_APP_ENVIRONMENT', 'production' );
Constant_Mocker::define( 'WPCOM_IS_VIP_ENV', true );

$this->assertFalse( Pendo_JavaScript_Library::should_enqueue_script() );
}

public function test_disabled_for_non_vip_environments() {
$user = $this->factory()->user->create_and_get( [ 'role' => 'author' ] );
wp_set_current_user( $user->ID );

Constant_Mocker::define( 'VIP_GO_APP_ENVIRONMENT', 'production' );

$this->assertFalse( Pendo_JavaScript_Library::should_enqueue_script() );
}

public function test_disabled_for_non_production_environments() {
$user = $this->factory()->user->create_and_get( [ 'role' => 'author' ] );
wp_set_current_user( $user->ID );

Constant_Mocker::define( 'VIP_GO_APP_ENVIRONMENT', 'preprod' );
Constant_Mocker::define( 'WPCOM_IS_VIP_ENV', true );

$this->assertFalse( Pendo_JavaScript_Library::should_enqueue_script() );
}

public function test_disabled_for_fedramp_environments() {
$user = $this->factory()->user->create_and_get( [ 'role' => 'author' ] );
wp_set_current_user( $user->ID );

Constant_Mocker::define( 'VIP_GO_APP_ENVIRONMENT', 'production' );
Constant_Mocker::define( 'VIP_IS_FEDRAMP', true );
Constant_Mocker::define( 'WPCOM_IS_VIP_ENV', true );

$this->assertFalse( Pendo_JavaScript_Library::should_enqueue_script() );
}

public function test_disabled_for_sandbox_environments() {
$user = $this->factory()->user->create_and_get( [ 'role' => 'author' ] );
wp_set_current_user( $user->ID );

Constant_Mocker::define( 'VIP_GO_APP_ENVIRONMENT', 'production' );
Constant_Mocker::define( 'WPCOM_IS_VIP_ENV', true );
Constant_Mocker::define( 'WPCOM_SANDBOXED', true );

$this->assertFalse( Pendo_JavaScript_Library::should_enqueue_script() );
}

public function test_disabled_for_users_without_edit_post_cap() {
$user = $this->factory()->user->create_and_get( [ 'role' => 'subscriber' ] );
wp_set_current_user( $user->ID );

Constant_Mocker::define( 'VIP_GO_APP_ENVIRONMENT', 'production' );
Constant_Mocker::define( 'WPCOM_IS_VIP_ENV', true );

$this->assertFalse( Pendo_JavaScript_Library::should_enqueue_script() );
}
}

0 comments on commit fb76ef2

Please sign in to comment.