Skip to content

Commit

Permalink
Add PHPUnit test case for GA4 conversion events datapoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
nfmohit committed Jan 17, 2023
1 parent fbe15ea commit df6afc9
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion tests/phpunit/integration/Modules/Analytics_4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,55 @@ public function test_report__no_property_id( $access_token ) {
$this->assertEquals( array( 'status' => 500 ), $data->get_error_data( 'missing_required_setting' ) );
}

/**
* @dataProvider data_access_token
*
* When an access token is provided, the user will be authenticated for the test.
*
* @param string $access_token Access token, or empty string if none.
*/
public function test_get_conversion_events( $access_token ) {
$this->setup_user_authentication( $access_token );

$property_id = '123456789';

$this->analytics->get_settings()->merge(
array(
'propertyID' => $property_id,
)
);

// Grant scopes so request doesn't fail.
$this->authentication->get_oauth_client()->set_granted_scopes(
$this->analytics->get_scopes()
);

$http_client = $this->create_fake_http_client( $property_id );
$this->analytics->get_client()->setHttpClient( $http_client );
$this->analytics->register();

// Fetch conversion events.
$data = $this->analytics->get_data(
'conversion-events',
array(
'propertyID' => $property_id,
)
);

$this->assertNotWPError( $data );

// Verify the conversion events are returned by checking an event name.
$this->assertEquals( 'some-event', $data['modelData'][0]['eventName'] );

// Verify the request URL and params were correctly generated.
$this->assertCount( 1, $this->request_handler_calls );

$request_url = $this->request_handler_calls[0]['url'];

$this->assertEquals( 'analyticsadmin.googleapis.com', $request_url['host'] );
$this->assertEquals( "/v1beta/properties/$property_id/conversionEvents", $request_url['path'] );
}

/**
* Returns a date string for the given number of days ago.
*
Expand Down Expand Up @@ -954,7 +1003,13 @@ function ( Request $request ) use ( $property_id ) {
'params' => $params,
);

if ( 'analyticsdata.googleapis.com' !== $url['host'] ) {
if (
! in_array(
$url['host'],
array( 'analyticsdata.googleapis.com', 'analyticsadmin.googleapis.com' ),
true
)
) {
return new Response( 200 );
}

Expand Down Expand Up @@ -984,6 +1039,23 @@ function ( Request $request ) use ( $property_id ) {
)
);

case "/v1beta/properties/$property_id/conversionEvents":
// Return a mock conversion event.
return new Response(
200,
array(),
Stream::factory(
json_encode(
array(
array(
'eventName' => 'some-event',
'name' => "properties/$property_id/conversionEvents/some-name",
),
)
)
)
);

default:
return new Response( 200 );
}
Expand Down

0 comments on commit df6afc9

Please sign in to comment.