Skip to content

Commit 306dcf8

Browse files
committed
Update test cases to use the new ETag format
1 parent 644545b commit 306dcf8

9 files changed

+63
-34
lines changed

plugins/optimization-detective/tests/storage/test-rest-api.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ function ( OD_URL_Metric_Store_Request_Context $context ) use ( &$stored_context
138138
*/
139139
public function data_provider_invalid_params(): array {
140140
$valid_element = $this->get_valid_params()['elements'][0];
141+
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
141142

142143
return array_map(
143144
function ( $params ) {
@@ -156,10 +157,10 @@ function ( $params ) {
156157
'hmac' => 'not even a hash',
157158
),
158159
'invalid_hmac' => array(
159-
'hmac' => od_get_url_metrics_storage_hmac( od_get_url_metrics_slug( array( 'different' => 'query vars' ) ), '', home_url( '/' ) ),
160+
'hmac' => od_get_url_metrics_storage_hmac( od_get_url_metrics_slug( array( 'different' => 'query vars' ) ), $current_etag, home_url( '/' ) ),
160161
),
161162
'invalid_hmac_with_queried_object' => array(
162-
'hmac' => od_get_url_metrics_storage_hmac( od_get_url_metrics_slug( array() ), home_url( '/' ), 1 ),
163+
'hmac' => od_get_url_metrics_storage_hmac( od_get_url_metrics_slug( array() ), $current_etag, home_url( '/' ), 1 ),
163164
),
164165
'invalid_viewport_type' => array(
165166
'viewport' => '640x480',

plugins/optimization-detective/tests/test-cases/complete-url-metrics.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
'set_up' => static function ( Test_OD_Optimization $test_case ): void {
44
ini_set( 'default_mimetype', 'text/html; charset=utf-8' ); // phpcs:ignore WordPress.PHP.IniSet.Risky
55

6+
$tag_visitor_registry = new OD_Tag_Visitor_Registry();
7+
$tag_visitor_registry->register( 'img', static function (): void {} );
8+
$tag_visitor_registry->register( 'video', static function (): void {} );
9+
610
$test_case->populate_url_metrics(
711
array(
812
array(
913
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]',
1014
'isLCP' => true,
1115
),
1216
),
13-
'img,video'
17+
od_compute_current_etag( $tag_visitor_registry )
1418
);
1519
},
1620
'buffer' => '

plugins/optimization-detective/tests/test-cases/many-images.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
'isLCP' => false,
99
);
1010
}
11-
$test_case->populate_url_metrics( $elements, 'img,video', false );
11+
12+
$tag_visitor_registry = new OD_Tag_Visitor_Registry();
13+
$tag_visitor_registry->register( 'img', static function (): void {} );
14+
$tag_visitor_registry->register( 'video', static function (): void {} );
15+
16+
$test_case->populate_url_metrics( $elements, od_compute_current_etag( $tag_visitor_registry ), false );
1217
},
1318
'buffer' => '
1419
<html lang="en">

plugins/optimization-detective/tests/test-cases/video.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
<?php
22
return array(
33
'set_up' => static function ( Test_OD_Optimization $test_case ): void {
4+
$tag_visitor_registry = new OD_Tag_Visitor_Registry();
5+
$tag_visitor_registry->register( 'img', static function (): void {} );
6+
$tag_visitor_registry->register( 'video', static function (): void {} );
7+
48
$test_case->populate_url_metrics(
59
array(
610
array(
711
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::VIDEO]',
812
'isLCP' => true,
913
),
1014
),
11-
'img,video',
15+
od_compute_current_etag( $tag_visitor_registry ),
1216
false
1317
);
1418
},

plugins/optimization-detective/tests/test-class-od-element.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ static function ( array $schema ): array {
7171
$this->assertInstanceOf( OD_Element::class, $element );
7272
$this->assertSame( $url_metric, $element->get_url_metric() );
7373
$this->assertNull( $element->get_url_metric_group() );
74-
$collection = new OD_URL_Metric_Group_Collection( array( $url_metric ), '', array(), 1, DAY_IN_SECONDS );
74+
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
75+
$collection = new OD_URL_Metric_Group_Collection( array( $url_metric ), $current_etag, array(), 1, DAY_IN_SECONDS );
7576
$collection->add_url_metric( $url_metric );
7677
$this->assertSame( iterator_to_array( $collection )[0], $element->get_url_metric_group() );
7778

plugins/optimization-detective/tests/test-class-od-url-metrics-group-collection.php

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,66 +19,68 @@ class Test_OD_URL_Metric_Group_Collection extends WP_UnitTestCase {
1919
* @return array<string, mixed> Data.
2020
*/
2121
public function data_provider_test_construction(): array {
22+
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
23+
2224
return array(
2325
'no_breakpoints_ok' => array(
2426
'url_metrics' => array(),
25-
'current_etag' => '',
27+
'current_etag' => $current_etag,
2628
'breakpoints' => array(),
2729
'sample_size' => 3,
2830
'freshness_ttl' => HOUR_IN_SECONDS,
2931
'exception' => '',
3032
),
3133
'negative_breakpoint_bad' => array(
3234
'url_metrics' => array(),
33-
'current_etag' => '',
35+
'current_etag' => $current_etag,
3436
'breakpoints' => array( -1 ),
3537
'sample_size' => 3,
3638
'freshness_ttl' => HOUR_IN_SECONDS,
3739
'exception' => InvalidArgumentException::class,
3840
),
3941
'zero_breakpoint_bad' => array(
4042
'url_metrics' => array(),
41-
'current_etag' => '',
43+
'current_etag' => $current_etag,
4244
'breakpoints' => array( 0 ),
4345
'sample_size' => 3,
4446
'freshness_ttl' => HOUR_IN_SECONDS,
4547
'exception' => InvalidArgumentException::class,
4648
),
4749
'max_breakpoint_bad' => array(
4850
'url_metrics' => array(),
49-
'current_etag' => '',
51+
'current_etag' => $current_etag,
5052
'breakpoints' => array( PHP_INT_MAX ),
5153
'sample_size' => 3,
5254
'freshness_ttl' => HOUR_IN_SECONDS,
5355
'exception' => InvalidArgumentException::class,
5456
),
5557
'string_breakpoint_bad' => array(
5658
'url_metrics' => array(),
57-
'current_etag' => '',
59+
'current_etag' => $current_etag,
5860
'breakpoints' => array( 'narrow' ),
5961
'sample_size' => 3,
6062
'freshness_ttl' => HOUR_IN_SECONDS,
6163
'exception' => InvalidArgumentException::class,
6264
),
6365
'negative_sample_size_bad' => array(
6466
'url_metrics' => array(),
65-
'current_etag' => '',
67+
'current_etag' => $current_etag,
6668
'breakpoints' => array( 400 ),
6769
'sample_size' => -3,
6870
'freshness_ttl' => HOUR_IN_SECONDS,
6971
'exception' => InvalidArgumentException::class,
7072
),
7173
'negative_freshness_tll_bad' => array(
7274
'url_metrics' => array(),
73-
'current_etag' => '',
75+
'current_etag' => $current_etag,
7476
'breakpoints' => array( 400 ),
7577
'sample_size' => 3,
7678
'freshness_ttl' => -HOUR_IN_SECONDS,
7779
'exception' => InvalidArgumentException::class,
7880
),
7981
'invalid_url_metrics_bad' => array(
8082
'url_metrics' => array( 'bad' ),
81-
'current_etag' => '',
83+
'current_etag' => $current_etag,
8284
'breakpoints' => array( 400 ),
8385
'sample_size' => 3,
8486
'freshness_ttl' => HOUR_IN_SECONDS,
@@ -89,7 +91,7 @@ public function data_provider_test_construction(): array {
8991
$this->get_sample_url_metric( array( 'viewport_width' => 200 ) ),
9092
$this->get_sample_url_metric( array( 'viewport_width' => 400 ) ),
9193
),
92-
'current_etag' => '',
94+
'current_etag' => $current_etag,
9395
'breakpoints' => array( 400 ),
9496
'sample_size' => 3,
9597
'freshness_ttl' => HOUR_IN_SECONDS,
@@ -194,7 +196,8 @@ public function data_provider_sample_size_and_breakpoints(): array {
194196
* @dataProvider data_provider_sample_size_and_breakpoints
195197
*/
196198
public function test_add_url_metric( int $sample_size, array $breakpoints, array $viewport_widths, array $expected_counts ): void {
197-
$group_collection = new OD_URL_Metric_Group_Collection( array(), '', $breakpoints, $sample_size, HOUR_IN_SECONDS );
199+
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
200+
$group_collection = new OD_URL_Metric_Group_Collection( array(), $current_etag, $breakpoints, $sample_size, HOUR_IN_SECONDS );
198201

199202
// Over-populate the sample size for the breakpoints by a dozen.
200203
foreach ( $viewport_widths as $viewport_width => $count ) {
@@ -222,9 +225,10 @@ public function test_add_url_metric( int $sample_size, array $breakpoints, array
222225
* @covers ::add_url_metric
223226
*/
224227
public function test_adding_pushes_out_old_metrics(): void {
228+
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
225229
$sample_size = 3;
226230
$breakpoints = array( 400, 600 );
227-
$group_collection = new OD_URL_Metric_Group_Collection( array(), '', $breakpoints, $sample_size, HOUR_IN_SECONDS );
231+
$group_collection = new OD_URL_Metric_Group_Collection( array(), $current_etag, $breakpoints, $sample_size, HOUR_IN_SECONDS );
228232

229233
// Populate the groups with stale URL Metrics.
230234
$viewport_widths = array( 300, 500, 700 );
@@ -345,7 +349,8 @@ function ( $viewport_width ) {
345349
$viewport_widths
346350
);
347351

348-
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, '', $breakpoints, 3, HOUR_IN_SECONDS );
352+
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
353+
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, $current_etag, $breakpoints, 3, HOUR_IN_SECONDS );
349354

350355
$this->assertCount(
351356
count( $breakpoints ) + 1,
@@ -509,7 +514,8 @@ public function data_provider_test_get_group_for_viewport_width(): array {
509514
* @param array<int, bool> $expected_is_group_complete Expected is group complete.
510515
*/
511516
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 {
512-
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, '', $breakpoints, $sample_size, $freshness_ttl );
517+
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
518+
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, $current_etag, $breakpoints, $sample_size, $freshness_ttl );
513519
$this->assertSame(
514520
$expected_return,
515521
array_map(
@@ -541,9 +547,10 @@ static function ( OD_URL_Metric_Group $group ): array {
541547
public function test_is_every_group_populated(): void {
542548
$breakpoints = array( 480, 800 );
543549
$sample_size = 3;
550+
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
544551
$group_collection = new OD_URL_Metric_Group_Collection(
545552
array(),
546-
'',
553+
$current_etag,
547554
$breakpoints,
548555
$sample_size,
549556
HOUR_IN_SECONDS
@@ -595,6 +602,7 @@ public function test_get_groups_by_lcp_element(): void {
595602

596603
$breakpoints = array( 480, 800 );
597604
$sample_size = 3;
605+
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
598606
$group_collection = new OD_URL_Metric_Group_Collection(
599607
array(
600608
// Group 1: 0-480 viewport widths.
@@ -607,7 +615,7 @@ public function test_get_groups_by_lcp_element(): void {
607615
$get_url_metric_with_one_lcp_element( 820, $first_child_image_xpath ),
608616
$get_url_metric_with_one_lcp_element( 900, $first_child_image_xpath ),
609617
),
610-
'',
618+
$current_etag,
611619
$breakpoints,
612620
$sample_size,
613621
HOUR_IN_SECONDS
@@ -637,9 +645,10 @@ public function test_get_groups_by_lcp_element(): void {
637645
public function test_get_common_lcp_element(): void {
638646
$breakpoints = array( 480, 800 );
639647
$sample_size = 3;
648+
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
640649
$group_collection = new OD_URL_Metric_Group_Collection(
641650
array(),
642-
'',
651+
$current_etag,
643652
$breakpoints,
644653
$sample_size,
645654
HOUR_IN_SECONDS
@@ -750,9 +759,10 @@ public function data_provider_element_max_intersection_ratios(): array {
750759
* @param array<string, float> $expected Expected.
751760
*/
752761
public function test_get_all_element_max_intersection_ratios( array $url_metrics, array $expected ): void {
762+
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
753763
$breakpoints = array( 480, 600, 782 );
754764
$sample_size = 3;
755-
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, '', $breakpoints, $sample_size, 0 );
765+
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, $current_etag, $breakpoints, $sample_size, 0 );
756766
$actual = $group_collection->get_all_element_max_intersection_ratios();
757767
$this->assertSame( $actual, $group_collection->get_all_element_max_intersection_ratios(), 'Cached result is identical.' );
758768
$this->assertSame( $expected, $actual );
@@ -927,9 +937,10 @@ public function data_provider_get_all_elements_positioned_in_any_initial_viewpor
927937
* @param array<string, bool> $expected Expected.
928938
*/
929939
public function test_get_all_elements_positioned_in_any_initial_viewport( array $url_metrics, array $expected ): void {
940+
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
930941
$breakpoints = array( 480, 600, 782 );
931942
$sample_size = 3;
932-
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, '', $breakpoints, $sample_size, 0 );
943+
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, $current_etag, $breakpoints, $sample_size, 0 );
933944
$actual = $group_collection->get_all_elements_positioned_in_any_initial_viewport();
934945
$this->assertSame( $actual, $group_collection->get_all_elements_positioned_in_any_initial_viewport(), 'Cached result is identical.' );
935946
$this->assertSame( $expected, $actual );
@@ -953,7 +964,7 @@ public function test_get_flattened_url_metrics(): void {
953964

954965
$group_collection = new OD_URL_Metric_Group_Collection(
955966
$url_metrics,
956-
'',
967+
od_compute_current_etag( new OD_Tag_Visitor_Registry() ),
957968
array( 500, 700 ),
958969
3,
959970
HOUR_IN_SECONDS
@@ -981,7 +992,7 @@ public function test_json_serialize(): void {
981992

982993
$group_collection = new OD_URL_Metric_Group_Collection(
983994
$url_metrics,
984-
'',
995+
od_compute_current_etag( new OD_Tag_Visitor_Registry() ),
985996
array( 500, 700 ),
986997
3,
987998
HOUR_IN_SECONDS

plugins/optimization-detective/tests/test-class-od-url-metrics-group.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ public function data_provider_test_get_lcp_element(): array {
325325
* @param array<int, string> $expected_lcp_element_xpaths Expected XPaths.
326326
*/
327327
public function test_get_lcp_element( array $breakpoints, array $url_metrics, array $expected_lcp_element_xpaths ): void {
328-
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, '', $breakpoints, 10, HOUR_IN_SECONDS );
328+
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
329+
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, $current_etag, $breakpoints, 10, HOUR_IN_SECONDS );
329330

330331
$lcp_element_xpaths_by_minimum_viewport_widths = array();
331332
foreach ( $group_collection as $group ) {

plugins/optimization-detective/tests/test-detection.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,11 @@ static function ( array $urls ): array {
127127
*/
128128
public function test_od_get_detection_script_returns_script( Closure $set_up, array $expected_exports ): void {
129129
$set_up();
130-
$slug = od_get_url_metrics_slug( array( 'p' => '1' ) );
130+
$slug = od_get_url_metrics_slug( array( 'p' => '1' ) );
131+
$current_etag = od_compute_current_etag( new OD_Tag_Visitor_Registry() );
131132

132133
$breakpoints = array( 480, 600, 782 );
133-
$group_collection = new OD_URL_Metric_Group_Collection( array(), '', $breakpoints, 3, HOUR_IN_SECONDS );
134+
$group_collection = new OD_URL_Metric_Group_Collection( array(), $current_etag, $breakpoints, 3, HOUR_IN_SECONDS );
134135

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

tests/class-optimization-detective-test-helpers.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ trait Optimization_Detective_Test_Helpers {
2121
* Populates complete URL metrics for the provided element data.
2222
*
2323
* @phpstan-param ElementDataSubset[] $elements
24-
* @param array[] $elements Element data.
25-
* @param string $etag ETag to set for the URL metrics.
26-
* @param bool $complete Whether to fully populate the groups.
24+
* @param array[] $elements Element data.
25+
* @param string|null $etag ETag to set for the URL metrics.
26+
* @param bool $complete Whether to fully populate the groups.
2727
* @throws Exception But it won't.
2828
*/
29-
public function populate_url_metrics( array $elements, string $etag = '', bool $complete = true ): void {
29+
public function populate_url_metrics( array $elements, ?string $etag, bool $complete = true ): void {
3030
$slug = od_get_url_metrics_slug( od_get_normalized_query_vars() );
31+
$etag = $etag ?? od_compute_current_etag( new OD_Tag_Visitor_Registry() );
3132
$sample_size = $complete ? od_get_url_metrics_breakpoint_sample_size() : 1;
3233
foreach ( array_merge( od_get_breakpoint_max_widths(), array( 1000 ) ) as $viewport_width ) {
3334
for ( $i = 0; $i < $sample_size; $i++ ) {
@@ -80,7 +81,7 @@ public function get_sample_dom_rect(): array {
8081
public function get_sample_url_metric( array $params ): OD_URL_Metric {
8182
$params = array_merge(
8283
array(
83-
'etag' => '',
84+
'etag' => od_compute_current_etag( new OD_Tag_Visitor_Registry() ),
8485
'url' => home_url( '/' ),
8586
'viewport_width' => 480,
8687
'elements' => array(),

0 commit comments

Comments
 (0)