Skip to content

Commit

Permalink
TESTS: TracksAnalyticsTest
Browse files Browse the repository at this point in the history
  • Loading branch information
chriszarate committed Dec 17, 2024
1 parent 987cd5c commit 8b4be91
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
53 changes: 39 additions & 14 deletions tests/inc/Analytics/TracksAnalyticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use PHPUnit\Framework\MockObject\MockObject;
use RemoteDataBlocks\Analytics\TracksAnalytics;
use RemoteDataBlocks\Analytics\EnvironmentConfig;
use RemoteDataBlocks\Config\QueryContext\HttpQueryContext;
use RemoteDataBlocks\Config\DataSource\HttpDataSource;
use RemoteDataBlocks\Config\Query\HttpQuery;
use RemoteDataBlocks\Editor\BlockManagement\ConfigStore;
use RemoteDataBlocks\ExampleApi\Queries\ExampleApiDataSource;
use RemoteDataBlocks\Integrations\Shopify\ShopifyDataSource;

// Define a mock class for Tracks.
Expand Down Expand Up @@ -133,7 +133,7 @@ public function testTrackPluginDeactivationDoesRecordEventIfPluginIsRDB(): void
/** @var MockObject|EnvironmentConfig */
$env_config_mock = $this->getMockBuilder( EnvironmentConfig::class )->onlyMethods( [ 'is_remote_data_blocks_plugin' ] )->getMock();
$env_config_mock->method( 'is_remote_data_blocks_plugin' )->with()->willReturn( true );

/** @var MockTracks|MockObject */
$tracks_mock = $this->getMockBuilder( MockTracks::class )->onlyMethods( [ 'record_event' ] )->getMock();
$tracks_mock->expects( $this->exactly( 1 ) )->method( 'record_event' )->with( 'remotedatablocks_plugin_toggle', $this->isType( 'array' ) );
Expand Down Expand Up @@ -161,11 +161,11 @@ public function testTrackRemoteDataBlocksUsageDoesNotTrackEventIfPostStatusIsNot
/** @var MockObject|EnvironmentConfig */
$env_config_mock = $this->getMockBuilder( EnvironmentConfig::class )->onlyMethods( [ 'should_track_post_having_remote_data_blocks' ] )->getMock();
$env_config_mock->method( 'should_track_post_having_remote_data_blocks' )->with( 1 )->willReturn( true );

/** @var MockTracks|MockObject */
$tracks_mock = $this->getMockBuilder( MockTracks::class )->onlyMethods( [ 'record_event' ] )->getMock();
$tracks_mock->expects( $this->exactly( 0 ) )->method( 'record_event' );

set_private_property( TracksAnalytics::class, null, 'instance', $tracks_mock );
TracksAnalytics::init( $env_config_mock );
TracksAnalytics::track_remote_data_blocks_usage( 1, (object) [ 'post_status' => 'draft' ] );
Expand All @@ -178,8 +178,15 @@ public function testTrackRemoteDataBlocksUsageDoesNotTrackEventIfPostContentHave

// Setup data sources.
ConfigStore::init();
ConfigStore::set_configuration( 'remote-data-blocks/shopify-vip-store', [
'queries' => [ new HttpQueryContext( ShopifyDataSource::create( 'access_token', 'name' ) ) ],
ConfigStore::set_block_configuration( 'remote-data-blocks/shopify-vip-store', [
'queries' => [
'display' => HttpQuery::from_array( [
'data_source' => ShopifyDataSource::from_array( [
'access_token' => 'token',
'store_name' => 'B. Walton',
] ),
] ),
],
] );

/** @var MockTracks|MockObject */
Expand All @@ -202,14 +209,32 @@ public function testTrackRemoteDataBlocksUsageDoesTrackEventIfPostContentHaveRem

// Setup data sources.
ConfigStore::init();
ConfigStore::set_configuration( 'remote-data-blocks/shopify-vip-store', [
'queries' => [ new HttpQueryContext( ShopifyDataSource::create( 'access_token', 'name' ) ) ],
ConfigStore::set_block_configuration( 'remote-data-blocks/shopify-vip-store', [
'queries' => [
'display' => HttpQuery::from_array( [
'data_source' => ShopifyDataSource::from_array( [
'service_config' => [
'__version' => 1,
'access_token' => 'token',
'store_name' => 'B. Walton',
],
] ),
'output_schema' => [ 'type' => 'string' ],
] ),
],
] );
ConfigStore::set_configuration( 'remote-data-blocks/conference-event', [

ConfigStore::set_block_configuration( 'remote-data-blocks/conference-event', [
'queries' => [
new HttpQueryContext( ExampleApiDataSource::from_array( [
'service' => 'example_api',
] ) ),
'display' => HttpQuery::from_array( [
'data_source' => HttpDataSource::from_array( [
'service_config' => [
'__version' => 1,
'endpoint' => 'https://example.com/api/v1',
],
] ),
'output_schema' => [ 'type' => 'string' ],
] ),
],
] );

Expand All @@ -220,7 +245,7 @@ public function testTrackRemoteDataBlocksUsageDoesTrackEventIfPostContentHaveRem
'post_type' => 'post',
'shopify_data_source_count' => 2,
'remote_data_blocks_total_count' => 3,
'example_api_data_source_count' => 1,
'generic-http_data_source_count' => 1,
] );

set_private_property( TracksAnalytics::class, null, 'instance', $tracks_mock );
Expand Down
8 changes: 8 additions & 0 deletions tests/inc/stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,18 @@ function get_bloginfo( $_property ): void {
// Do nothing
}

function plugins_url( string $path ): string {
return sprintf( 'https://example.com/%s/', $path );
}

function sanitize_title( string $title ): string {
return str_replace( ' ', '-', strtolower( $title ) );
}

function sanitize_title_with_dashes( string $title ): string {
return preg_replace( '/[^a-z0-9-]/', '-', sanitize_title( $title ) );
}

function sanitize_text_field( string $text ): string {
// phpcs:ignore WordPressVIPMinimum.Functions.StripTags.StripTagsOneParameter
$text = strip_tags( $text );
Expand Down

0 comments on commit 8b4be91

Please sign in to comment.