Skip to content

Commit

Permalink
Fix WCPay Tracks events recorded from Admin area (#8221)
Browse files Browse the repository at this point in the history
  • Loading branch information
malithsen authored Mar 7, 2024
1 parent ca8c7c0 commit a620f9d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 53 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-wcpay-admin-tracks
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: dev

Fix a bug in Tracks where admin events were not recorded properly
4 changes: 0 additions & 4 deletions includes/class-woopay-tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,6 @@ private function tracks_build_event_obj( $user, $event_name, $properties = [] )
*/
public function tracks_get_identity() {
$user_id = get_current_user_id();
// If the user is not trackable, return an empty array.
if ( ! $this->should_enable_tracking() ) {
return [];
}

// Meta is set, and user is still connected. Use WPCOM ID.
$wpcom_id = get_user_meta( $user_id, 'jetpack_tracks_wpcom_id', true );
Expand Down
138 changes: 89 additions & 49 deletions tests/unit/test-class-woopay-tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,102 +5,142 @@
* @package WooCommerce\Payments\Tests
*/

use PHPUnit\Framework\MockObject\MockObject;
use WCPay\WooPay_Tracker;
use WC_Payments_Account;
use WC_Payments_Http;

/**
* WooPay_Tracker unit tests.
*/
class WooPay_Tracker_Test extends WCPAY_UnitTestCase {

/**
* @var WooPay_Tracker
*/
private $tracker;

/**
* The HTTP client.
*
* @var WC_Payments_Http
*/
private $http_client_stub;

/**
* @var WC_Payments_Account|MockObject
* @var WC_Payments_Account
*/
private $mock_account;

/**
* Pre-test setup
* @var WCPay\Database_Cache
*/
private $mock_cache;

/**
* @var WCPay\Database_Cache
*/
public function set_up() {
parent::set_up();
private $cache;

$this->http_client_stub = $this->getMockBuilder( WC_Payments_Http::class )->disableOriginalConstructor()->setMethods( [ 'wpcom_json_api_request_as_user' ] )->getMock();
$this->tracker = new WCPay\WooPay_Tracker( $this->http_client_stub );
public function setUp(): void {
parent::setUp();

// Mock the main class's cache service.
$this->_cache = WC_Payments::get_database_cache();
$this->http_client_stub = $this->createMock( WC_Payments_Http::class );
$this->http_client_stub->method( 'is_user_connected' )->willReturn( true );
$this->http_client_stub->method( 'get_connected_user_data' )->willReturn( [ 'ID' => 1234 ] );

$this->tracker = new WooPay_Tracker( $this->http_client_stub );

$this->cache = WC_Payments::get_database_cache();
$this->mock_cache = $this->createMock( WCPay\Database_Cache::class );
WC_Payments::set_database_cache( $this->mock_cache );
WC_Payments::get_gateway()->enable();

$this->mock_account = $this->getMockBuilder( WC_Payments_Account::class )
->disableOriginalConstructor()
->getMock();
$this->mock_account = $this->createMock( WC_Payments_Account::class );
}

public function tear_down() {
// Restore the cache service in the main class.
WC_Payments::set_database_cache( $this->_cache );

parent::tear_down();
public function tearDown(): void {
WC_Payments::set_database_cache( $this->cache );
parent::tearDown();
}

public function test_tracks_obeys_woopay_flag() {
$this->set_account_connected( true );
WC_Payments::set_account_service( $this->mock_account );
$this->set_is_woopay_eligible( false );
$this->assertFalse( $this->tracker->should_enable_tracking( null ) );
public function test_tracks_obeys_woopay_flag(): void {
$is_woopay_eligible = false;
$is_account_connected = true;

$this->setup_woopay_environment( $is_woopay_eligible, $is_account_connected );
$this->assertFalse( $this->tracker->should_enable_tracking() );
}

public function test_does_not_track_admin_pages() {
wp_set_current_user( 1 );
$this->set_is_woopay_eligible( true );
$this->set_account_connected( true );
WC_Payments::set_account_service( $this->mock_account );
$this->set_is_admin( true );
$this->assertFalse( $this->tracker->should_enable_tracking( null ) );
public function test_does_not_track_admin_pages(): void {
$is_woopay_eligible = true;
$is_account_connected = true;
$is_admin_page = true;
$this->setup_woopay_environment( $is_woopay_eligible, $is_account_connected, $is_admin_page );
$this->assertFalse( $this->tracker->should_enable_tracking() );
}

public function test_does_track_non_admins() {
global $wp_roles;
$this->set_is_woopay_eligible( true );
$this->set_account_connected( true );
WC_Payments::get_gateway()->update_option( 'platform_checkout', 'yes' );
WC_Payments::set_account_service( $this->mock_account );
wp_set_current_user( 1 );
$this->set_is_admin( false );
public function test_does_track_non_admins(): void {
$is_woopay_eligible = true;
$is_account_connected = true;
$this->setup_woopay_environment( $is_woopay_eligible, $is_account_connected );

$all_roles = $wp_roles->get_names();
$all_roles = array_diff( $all_roles, [ 'administrator' ] );
global $wp_roles;
$all_roles = array_diff( $wp_roles->get_names(), [ 'administrator' ] );

foreach ( $all_roles as $role ) {
wp_get_current_user()->set_role( $role );
$this->assertTrue( $this->tracker->should_enable_tracking( null ) );
$this->assertTrue( $this->tracker->should_enable_tracking() );
}
}

public function test_does_not_track_when_account_not_connected() {
wp_set_current_user( 1 );
$this->set_is_woopay_eligible( true );
$this->set_account_connected( false );
public function test_does_not_track_when_account_not_connected(): void {
$is_woopay_eligible = true;
$is_account_connected = false;
$this->setup_woopay_environment( $is_woopay_eligible, $is_account_connected );
$this->assertFalse( $this->tracker->should_enable_tracking() );
}

public function test_tracks_build_event_obj_for_admin_events(): void {
$this->set_account_connected( true );
$event_name = 'wcadmin_test_event';
$properties = [ 'test_property' => 'value' ];

$event_obj = $this->invoke_method( $this->tracker, 'tracks_build_event_obj', [ wp_get_current_user(), $event_name, $properties ] );
$this->assertEquals( 'value', $event_obj->test_property );
$this->assertEquals( 1234, $event_obj->_ui );
$this->assertEquals( $event_name, $event_obj->_en );
}

public function test_tracks_build_event_obj_for_shopper_events() {
$this->set_account_connected( true );
$event_name = 'wcpay_test_event';
$properties = [ 'test_property' => 'value' ];

$event_obj = $this->invoke_method( $this->tracker, 'tracks_build_event_obj', [ wp_get_current_user(), $event_name, $properties ] );

$this->assertInstanceOf( Jetpack_Tracks_Event::class, $event_obj );
$this->assertEquals( 'value', $event_obj->test_property );
$this->assertEquals( 1234, $event_obj->_ui );
$this->assertEquals( $event_name, $event_obj->_en );
}

private function setup_woopay_environment( bool $is_woopay_eligible, bool $is_stripe_connected, bool $is_admin = false ): void {
$this->set_is_woopay_eligible( $is_woopay_eligible );
$this->set_account_connected( $is_stripe_connected );
$this->set_is_admin( $is_admin );
WC_Payments::set_account_service( $this->mock_account );
$is_admin_event = false;
$this->assertFalse( $this->tracker->should_enable_tracking( $is_admin_event ) );
}

/**
* Utility method to access protected methods for testing.
*/
protected function invoke_method( &$object, $method_name, array $parameters = [] ) {
$reflection = new \ReflectionClass( get_class( $object ) );
$method = $reflection->getMethod( $method_name );
$method->setAccessible( true );
return $method->invokeArgs( $object, $parameters );
}

/**
* Mock is_admin() function.
*
* @param bool $is_admin
*/
private function set_is_admin( bool $is_admin ) {
Expand Down

0 comments on commit a620f9d

Please sign in to comment.