Skip to content

Commit

Permalink
Update test cases to use the new ETag format
Browse files Browse the repository at this point in the history
  • Loading branch information
ShyamGadde committed Nov 28, 2024
1 parent e6078a9 commit 309f9bd
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ function ( OD_URL_Metric_Store_Request_Context $context ) use ( &$stored_context
*/
public function data_provider_invalid_params(): array {
$valid_element = $this->get_valid_params()['elements'][0];
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );

return array_map(
function ( $params ) {
Expand All @@ -156,10 +157,10 @@ function ( $params ) {
'hmac' => 'not even a hash',
),
'invalid_hmac' => array(
'hmac' => od_get_url_metrics_storage_hmac( od_get_url_metrics_slug( array( 'different' => 'query vars' ) ), '', home_url( '/' ) ),
'hmac' => od_get_url_metrics_storage_hmac( od_get_url_metrics_slug( array( 'different' => 'query vars' ) ), $current_etag, home_url( '/' ) ),
),
'invalid_hmac_with_queried_object' => array(
'hmac' => od_get_url_metrics_storage_hmac( od_get_url_metrics_slug( array() ), home_url( '/' ), 1 ),
'hmac' => od_get_url_metrics_storage_hmac( od_get_url_metrics_slug( array() ), $current_etag, home_url( '/' ), 1 ),
),
'invalid_viewport_type' => array(
'viewport' => '640x480',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
'set_up' => static function ( Test_OD_Optimization $test_case ): void {
ini_set( 'default_mimetype', 'text/html; charset=utf-8' ); // phpcs:ignore WordPress.PHP.IniSet.Risky

$tag_visitor_registry = new OD_Tag_Visitor_Registry();
$tag_visitor_registry->register( 'img', static function (): void {} );
$tag_visitor_registry->register( 'video', static function (): void {} );

$test_case->populate_url_metrics(
array(
array(
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]',
'isLCP' => true,
),
),
'img,video'
od_compute_current_etag( $tag_visitor_registry )
);
},
'buffer' => '
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
'isLCP' => false,
);
}
$test_case->populate_url_metrics( $elements, 'img,video', false );

$tag_visitor_registry = new OD_Tag_Visitor_Registry();
$tag_visitor_registry->register( 'img', static function (): void {} );
$tag_visitor_registry->register( 'video', static function (): void {} );

$test_case->populate_url_metrics( $elements, od_compute_current_etag( $tag_visitor_registry ), false );
},
'buffer' => '
<html lang="en">
Expand Down
6 changes: 5 additions & 1 deletion plugins/optimization-detective/tests/test-cases/video.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<?php
return array(
'set_up' => static function ( Test_OD_Optimization $test_case ): void {
$tag_visitor_registry = new OD_Tag_Visitor_Registry();
$tag_visitor_registry->register( 'img', static function (): void {} );
$tag_visitor_registry->register( 'video', static function (): void {} );

$test_case->populate_url_metrics(
array(
array(
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::VIDEO]',
'isLCP' => true,
),
),
'img,video',
od_compute_current_etag( $tag_visitor_registry ),
false
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ static function ( array $schema ): array {
$this->assertInstanceOf( OD_Element::class, $element );
$this->assertSame( $url_metric, $element->get_url_metric() );
$this->assertNull( $element->get_url_metric_group() );
$collection = new OD_URL_Metric_Group_Collection( array( $url_metric ), '', array(), 1, DAY_IN_SECONDS );
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
$collection = new OD_URL_Metric_Group_Collection( array( $url_metric ), $current_etag, array(), 1, DAY_IN_SECONDS );
$collection->add_url_metric( $url_metric );
$this->assertSame( iterator_to_array( $collection )[0], $element->get_url_metric_group() );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,66 +19,68 @@ class Test_OD_URL_Metric_Group_Collection extends WP_UnitTestCase {
* @return array<string, mixed> Data.
*/
public function data_provider_test_construction(): array {
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );

return array(
'no_breakpoints_ok' => array(
'url_metrics' => array(),
'current_etag' => '',
'current_etag' => $current_etag,
'breakpoints' => array(),
'sample_size' => 3,
'freshness_ttl' => HOUR_IN_SECONDS,
'exception' => '',
),
'negative_breakpoint_bad' => array(
'url_metrics' => array(),
'current_etag' => '',
'current_etag' => $current_etag,
'breakpoints' => array( -1 ),
'sample_size' => 3,
'freshness_ttl' => HOUR_IN_SECONDS,
'exception' => InvalidArgumentException::class,
),
'zero_breakpoint_bad' => array(
'url_metrics' => array(),
'current_etag' => '',
'current_etag' => $current_etag,
'breakpoints' => array( 0 ),
'sample_size' => 3,
'freshness_ttl' => HOUR_IN_SECONDS,
'exception' => InvalidArgumentException::class,
),
'max_breakpoint_bad' => array(
'url_metrics' => array(),
'current_etag' => '',
'current_etag' => $current_etag,
'breakpoints' => array( PHP_INT_MAX ),
'sample_size' => 3,
'freshness_ttl' => HOUR_IN_SECONDS,
'exception' => InvalidArgumentException::class,
),
'string_breakpoint_bad' => array(
'url_metrics' => array(),
'current_etag' => '',
'current_etag' => $current_etag,
'breakpoints' => array( 'narrow' ),
'sample_size' => 3,
'freshness_ttl' => HOUR_IN_SECONDS,
'exception' => InvalidArgumentException::class,
),
'negative_sample_size_bad' => array(
'url_metrics' => array(),
'current_etag' => '',
'current_etag' => $current_etag,
'breakpoints' => array( 400 ),
'sample_size' => -3,
'freshness_ttl' => HOUR_IN_SECONDS,
'exception' => InvalidArgumentException::class,
),
'negative_freshness_tll_bad' => array(
'url_metrics' => array(),
'current_etag' => '',
'current_etag' => $current_etag,
'breakpoints' => array( 400 ),
'sample_size' => 3,
'freshness_ttl' => -HOUR_IN_SECONDS,
'exception' => InvalidArgumentException::class,
),
'invalid_url_metrics_bad' => array(
'url_metrics' => array( 'bad' ),
'current_etag' => '',
'current_etag' => $current_etag,
'breakpoints' => array( 400 ),
'sample_size' => 3,
'freshness_ttl' => HOUR_IN_SECONDS,
Expand All @@ -89,7 +91,7 @@ public function data_provider_test_construction(): array {
$this->get_sample_url_metric( array( 'viewport_width' => 200 ) ),
$this->get_sample_url_metric( array( 'viewport_width' => 400 ) ),
),
'current_etag' => '',
'current_etag' => $current_etag,
'breakpoints' => array( 400 ),
'sample_size' => 3,
'freshness_ttl' => HOUR_IN_SECONDS,
Expand Down Expand Up @@ -194,7 +196,8 @@ public function data_provider_sample_size_and_breakpoints(): array {
* @dataProvider data_provider_sample_size_and_breakpoints
*/
public function test_add_url_metric( int $sample_size, array $breakpoints, array $viewport_widths, array $expected_counts ): void {
$group_collection = new OD_URL_Metric_Group_Collection( array(), '', $breakpoints, $sample_size, HOUR_IN_SECONDS );
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
$group_collection = new OD_URL_Metric_Group_Collection( array(), $current_etag, $breakpoints, $sample_size, HOUR_IN_SECONDS );

// Over-populate the sample size for the breakpoints by a dozen.
foreach ( $viewport_widths as $viewport_width => $count ) {
Expand Down Expand Up @@ -222,9 +225,10 @@ public function test_add_url_metric( int $sample_size, array $breakpoints, array
* @covers ::add_url_metric
*/
public function test_adding_pushes_out_old_metrics(): void {
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
$sample_size = 3;
$breakpoints = array( 400, 600 );
$group_collection = new OD_URL_Metric_Group_Collection( array(), '', $breakpoints, $sample_size, HOUR_IN_SECONDS );
$group_collection = new OD_URL_Metric_Group_Collection( array(), $current_etag, $breakpoints, $sample_size, HOUR_IN_SECONDS );

// Populate the groups with stale URL Metrics.
$viewport_widths = array( 300, 500, 700 );
Expand Down Expand Up @@ -345,7 +349,8 @@ function ( $viewport_width ) {
$viewport_widths
);

$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, '', $breakpoints, 3, HOUR_IN_SECONDS );
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, $current_etag, $breakpoints, 3, HOUR_IN_SECONDS );

$this->assertCount(
count( $breakpoints ) + 1,
Expand Down Expand Up @@ -509,7 +514,8 @@ public function data_provider_test_get_group_for_viewport_width(): array {
* @param array<int, bool> $expected_is_group_complete Expected is group complete.
*/
public function test_get_group_for_viewport_width( array $url_metrics, float $current_time, array $breakpoints, int $sample_size, int $freshness_ttl, array $expected_return, array $expected_is_group_complete ): void {
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, '', $breakpoints, $sample_size, $freshness_ttl );
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, $current_etag, $breakpoints, $sample_size, $freshness_ttl );
$this->assertSame(
$expected_return,
array_map(
Expand Down Expand Up @@ -541,9 +547,10 @@ static function ( OD_URL_Metric_Group $group ): array {
public function test_is_every_group_populated(): void {
$breakpoints = array( 480, 800 );
$sample_size = 3;
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
$group_collection = new OD_URL_Metric_Group_Collection(
array(),
'',
$current_etag,
$breakpoints,
$sample_size,
HOUR_IN_SECONDS
Expand Down Expand Up @@ -595,6 +602,7 @@ public function test_get_groups_by_lcp_element(): void {

$breakpoints = array( 480, 800 );
$sample_size = 3;
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
$group_collection = new OD_URL_Metric_Group_Collection(
array(
// Group 1: 0-480 viewport widths.
Expand All @@ -607,7 +615,7 @@ public function test_get_groups_by_lcp_element(): void {
$get_url_metric_with_one_lcp_element( 820, $first_child_image_xpath ),
$get_url_metric_with_one_lcp_element( 900, $first_child_image_xpath ),
),
'',
$current_etag,
$breakpoints,
$sample_size,
HOUR_IN_SECONDS
Expand Down Expand Up @@ -637,9 +645,10 @@ public function test_get_groups_by_lcp_element(): void {
public function test_get_common_lcp_element(): void {
$breakpoints = array( 480, 800 );
$sample_size = 3;
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
$group_collection = new OD_URL_Metric_Group_Collection(
array(),
'',
$current_etag,
$breakpoints,
$sample_size,
HOUR_IN_SECONDS
Expand Down Expand Up @@ -750,9 +759,10 @@ public function data_provider_element_max_intersection_ratios(): array {
* @param array<string, float> $expected Expected.
*/
public function test_get_all_element_max_intersection_ratios( array $url_metrics, array $expected ): void {
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
$breakpoints = array( 480, 600, 782 );
$sample_size = 3;
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, '', $breakpoints, $sample_size, 0 );
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, $current_etag, $breakpoints, $sample_size, 0 );
$actual = $group_collection->get_all_element_max_intersection_ratios();
$this->assertSame( $actual, $group_collection->get_all_element_max_intersection_ratios(), 'Cached result is identical.' );
$this->assertSame( $expected, $actual );
Expand Down Expand Up @@ -927,9 +937,10 @@ public function data_provider_get_all_elements_positioned_in_any_initial_viewpor
* @param array<string, bool> $expected Expected.
*/
public function test_get_all_elements_positioned_in_any_initial_viewport( array $url_metrics, array $expected ): void {
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
$breakpoints = array( 480, 600, 782 );
$sample_size = 3;
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, '', $breakpoints, $sample_size, 0 );
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, $current_etag, $breakpoints, $sample_size, 0 );
$actual = $group_collection->get_all_elements_positioned_in_any_initial_viewport();
$this->assertSame( $actual, $group_collection->get_all_elements_positioned_in_any_initial_viewport(), 'Cached result is identical.' );
$this->assertSame( $expected, $actual );
Expand All @@ -953,7 +964,7 @@ public function test_get_flattened_url_metrics(): void {

$group_collection = new OD_URL_Metric_Group_Collection(
$url_metrics,
'',
od_compute_current_etag( new OD_Tag_Visitor_Registry() ),
array( 500, 700 ),
3,
HOUR_IN_SECONDS
Expand Down Expand Up @@ -981,7 +992,7 @@ public function test_json_serialize(): void {

$group_collection = new OD_URL_Metric_Group_Collection(
$url_metrics,
'',
od_compute_current_etag( new OD_Tag_Visitor_Registry() ),
array( 500, 700 ),
3,
HOUR_IN_SECONDS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ public function data_provider_test_get_lcp_element(): array {
* @param array<int, string> $expected_lcp_element_xpaths Expected XPaths.
*/
public function test_get_lcp_element( array $breakpoints, array $url_metrics, array $expected_lcp_element_xpaths ): void {
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, '', $breakpoints, 10, HOUR_IN_SECONDS );
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, $current_etag, $breakpoints, 10, HOUR_IN_SECONDS );

$lcp_element_xpaths_by_minimum_viewport_widths = array();
foreach ( $group_collection as $group ) {
Expand Down
5 changes: 3 additions & 2 deletions plugins/optimization-detective/tests/test-detection.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,11 @@ static function ( array $urls ): array {
*/
public function test_od_get_detection_script_returns_script( Closure $set_up, array $expected_exports ): void {
$set_up();
$slug = od_get_url_metrics_slug( array( 'p' => '1' ) );
$slug = od_get_url_metrics_slug( array( 'p' => '1' ) );
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );

$breakpoints = array( 480, 600, 782 );
$group_collection = new OD_URL_Metric_Group_Collection( array(), '', $breakpoints, 3, HOUR_IN_SECONDS );
$group_collection = new OD_URL_Metric_Group_Collection( array(), $current_etag, $breakpoints, 3, HOUR_IN_SECONDS );

$script = od_get_detection_script( $slug, $group_collection );

Expand Down
11 changes: 6 additions & 5 deletions tests/class-optimization-detective-test-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ trait Optimization_Detective_Test_Helpers {
* Populates complete URL metrics for the provided element data.
*
* @phpstan-param ElementDataSubset[] $elements
* @param array[] $elements Element data.
* @param string $etag ETag to set for the URL metrics.
* @param bool $complete Whether to fully populate the groups.
* @param array[] $elements Element data.
* @param string|null $etag ETag to set for the URL metrics.
* @param bool $complete Whether to fully populate the groups.
* @throws Exception But it won't.
*/
public function populate_url_metrics( array $elements, string $etag = '', bool $complete = true ): void {
public function populate_url_metrics( array $elements, ?string $etag, bool $complete = true ): void {
$slug = od_get_url_metrics_slug( od_get_normalized_query_vars() );
$etag = $etag ?? od_compute_current_etag( new OD_Tag_Visitor_Registry() );
$sample_size = $complete ? od_get_url_metrics_breakpoint_sample_size() : 1;
foreach ( array_merge( od_get_breakpoint_max_widths(), array( 1000 ) ) as $viewport_width ) {
for ( $i = 0; $i < $sample_size; $i++ ) {
Expand Down Expand Up @@ -80,7 +81,7 @@ public function get_sample_dom_rect(): array {
public function get_sample_url_metric( array $params ): OD_URL_Metric {
$params = array_merge(
array(
'etag' => '',
'etag' => od_compute_current_etag( new OD_Tag_Visitor_Registry() ),
'url' => home_url( '/' ),
'viewport_width' => 480,
'elements' => array(),
Expand Down

0 comments on commit 309f9bd

Please sign in to comment.