Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/Telemetry/Telemetry/Telemetry.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,17 +390,27 @@ protected function get_option() {
* Gets the args for sending data to the telemetry server.
*
* @since 1.0.0
* @since TBD - Updated to include the opted in user with the telemetry json.
*
* @return array
*/
protected function get_send_data_args() {
$opt_in_user = get_option( Status::OPTION_NAME_USER_INFO, [] );
$telemetry = $this->provider->get_data();

if ( count( $opt_in_user ) > 0 ) {
$telemetry['opt_in_user'] = $opt_in_user;
}

$args = [
'token' => $this->get_token(),
'telemetry' => wp_json_encode( $telemetry ),
'stellar_slugs' => wp_json_encode( $this->opt_in_status->get_opted_in_plugins() ),
];

return apply_filters(
'stellarwp/telemetry/' . Config::get_hook_prefix() . 'send_data_args',
[
'token' => $this->get_token(),
'telemetry' => wp_json_encode( $this->provider->get_data() ),
'stellar_slugs' => wp_json_encode( $this->opt_in_status->get_opted_in_plugins() ),
]
$args
);
}

Expand Down
103 changes: 103 additions & 0 deletions tests/wpunit/Telemetry_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,107 @@ public function should_not_send_data_without_authorization(): void {

$this->assertFalse( $sent );
}

/**
* It should include the opt-in user when sending data.
*
* @test
*/
public function should_send_data_with_optin_user(): void {
wp_set_current_user( 1 );

$mock_response = [
'headers' => [],
'body' => json_encode( // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
[
'status' => 'success',
'token' => '1234567890',
]
),
'response' => [
'code' => false,
'message' => false,
],
'cookies' => [],
'http_response' => null,
];
$call_url = null;
$call_args = null;
$this->set_fn_return(
'wp_remote_post',
static function ( string $url, array $args ) use ( $mock_response, &$call_url, &$call_args ) {
$call_url = $url;
$call_args = $args;

return $mock_response;
},
true
);

$opt_in_status = new Status();
$telemetry = new Telemetry( new Null_Data_Provider(), $opt_in_status );

$opt_in_status->set_status( true );

$telemetry->register_user();
$telemetry->save_token( '2389' );
$sent = $telemetry->send_data();

$this->assertEquals( Config::get_server_url() . '/telemetry', $call_url );
$this->assertArrayHasKey( 'opt_in_user', json_decode( $call_args['body']['telemetry'], true ) );
$this->assertTrue( $sent );
}

/**
* It should include the opt-in user when sending data.
*
* @test
*/
public function should_send_data_without_optin_user_if_missing(): void {
wp_set_current_user( 1 );

$mock_response = [
'headers' => [],
'body' => json_encode( // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
[
'status' => 'success',
'token' => '1234567890',
]
),
'response' => [
'code' => false,
'message' => false,
],
'cookies' => [],
'http_response' => null,
];
$call_url = null;
$call_args = null;
$this->set_fn_return(
'wp_remote_post',
static function ( string $url, array $args ) use ( $mock_response, &$call_url, &$call_args ) {
$call_url = $url;
$call_args = $args;

return $mock_response;
},
true
);

$opt_in_status = new Status();
$telemetry = new Telemetry( new Null_Data_Provider(), $opt_in_status );

$opt_in_status->set_status( true );

$telemetry->register_user();
$telemetry->save_token( '2389' );

delete_option( Status::OPTION_NAME_USER_INFO );

$sent = $telemetry->send_data();

$this->assertEquals( Config::get_server_url() . '/telemetry', $call_url );
$this->assertArrayNotHasKey( 'opt_in_user', json_decode( $call_args['body']['telemetry'], true ) );
$this->assertTrue( $sent );
}
}