Skip to content

Commit 9141164

Browse files
committed
Clamp negative freshness TTL to -1
1 parent 6ef63c1 commit 9141164

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

plugins/optimization-detective/class-od-url-metric-group-collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public function __construct( array $url_metrics, string $current_etag, array $br
168168
$this->sample_size = $sample_size;
169169

170170
// Set freshness TTL.
171-
$this->freshness_ttl = $freshness_ttl;
171+
$this->freshness_ttl = max( -1, $freshness_ttl );
172172

173173
// Create groups and the URL Metrics to them.
174174
$this->groups = $this->create_groups();

plugins/optimization-detective/class-od-url-metric-group.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function __construct( array $url_metrics, int $minimum_viewport_width, ?i
145145
}
146146
$this->sample_size = $sample_size;
147147

148-
$this->freshness_ttl = $freshness_ttl;
148+
$this->freshness_ttl = max( -1, $freshness_ttl );
149149
$this->collection = $collection;
150150
$this->url_metrics = $url_metrics;
151151
}

plugins/optimization-detective/tests/storage/test-data.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function test_negative_od_get_url_metric_freshness_ttl(): void {
5454
add_filter(
5555
'od_url_metric_freshness_ttl',
5656
static function (): int {
57-
return -1;
57+
return -12345;
5858
}
5959
);
6060

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ public function test_construction( array $url_metrics, string $current_etag, arr
135135
$this->assertSame( $current_etag, $group_collection->get_current_etag() );
136136
$this->assertSame( $sample_size, $group_collection->get_sample_size() );
137137
$this->assertSame( $breakpoints, $group_collection->get_breakpoints() );
138-
$this->assertSame( $freshness_ttl, $group_collection->get_freshness_ttl() );
138+
if ( $freshness_ttl < 0 ) {
139+
$this->assertSame( -1, $group_collection->get_freshness_ttl() );
140+
} else {
141+
$this->assertSame( $freshness_ttl, $group_collection->get_freshness_ttl() );
142+
}
139143
}
140144

141145
/**

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function data_provider_test_construction(): array {
5757
'minimum_viewport_width' => 0,
5858
'maximum_viewport_width' => 100,
5959
'sample_size' => 3,
60-
'freshness_ttl' => -HOUR_IN_SECONDS,
60+
'freshness_ttl' => -1,
6161
'exception' => '',
6262
),
6363
'good_empty_url_metrics' => array(
@@ -287,12 +287,12 @@ public function data_provider_test_is_complete(): array {
287287
'etag' => md5( '' ),
288288
)
289289
),
290-
'freshness_ttl' => -HOUR_IN_SECONDS,
290+
'freshness_ttl' => -1,
291291
'expected_is_group_complete' => true,
292292
),
293293
'negative_ttl_with_etag_mismatch' => array(
294294
'url_metric' => $this->get_sample_url_metric( array( 'etag' => md5( 'different_etag' ) ) ),
295-
'freshness_ttl' => -HOUR_IN_SECONDS,
295+
'freshness_ttl' => -1,
296296
'expected_is_group_complete' => false,
297297
),
298298
);
@@ -302,6 +302,8 @@ public function data_provider_test_is_complete(): array {
302302
* Test is_complete().
303303
*
304304
* @covers ::is_complete
305+
* @covers ::get_freshness_ttl
306+
* @covers OD_URL_Metric_Group_Collection::get_freshness_ttl
305307
*
306308
* @dataProvider data_provider_test_is_complete
307309
*/
@@ -312,6 +314,13 @@ public function test_is_complete( OD_URL_Metric $url_metric, int $freshness_ttl,
312314
$group->add_url_metric( $url_metric );
313315

314316
$this->assertSame( $expected_is_group_complete, $group->is_complete() );
317+
if ( $freshness_ttl < 0 ) {
318+
$this->assertSame( -1, $collection->get_freshness_ttl() );
319+
$this->assertSame( -1, $group->get_freshness_ttl() );
320+
} else {
321+
$this->assertSame( $freshness_ttl, $collection->get_freshness_ttl() );
322+
$this->assertSame( $freshness_ttl, $group->get_freshness_ttl() );
323+
}
315324
}
316325

317326
/**

0 commit comments

Comments
 (0)