Skip to content

Commit 93257ee

Browse files
authored
Merge pull request #1824 from ShyamGadde/update/make-etag-required
Make ETag a required property of the URL Metric
2 parents 522660b + bd642b4 commit 93257ee

6 files changed

+23
-46
lines changed

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

-5
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,6 @@ public function is_complete(): bool {
286286
return false;
287287
}
288288

289-
// The ETag is not populated yet, so this is stale. Eventually this will be required.
290-
if ( $url_metric->get_etag() === null ) {
291-
return false;
292-
}
293-
294289
// The ETag of the URL Metric does not match the current ETag for the collection, so it is stale.
295290
if ( ! hash_equals( $url_metric->get_etag(), $this->collection->get_current_etag() ) ) {
296291
return false;

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* }
4040
* @phpstan-type Data array{
4141
* uuid: non-empty-string,
42-
* etag?: non-empty-string,
42+
* etag: non-empty-string,
4343
* url: non-empty-string,
4444
* timestamp: float,
4545
* viewport: ViewportRect,
@@ -171,6 +171,7 @@ public function set_group( OD_URL_Metric_Group $group ): void {
171171
*
172172
* @since 0.1.0
173173
* @since 0.9.0 Added the 'etag' property to the schema.
174+
* @since n.e.x.t The 'etag' property is now required.
174175
*
175176
* @todo Cache the return value?
176177
*
@@ -230,7 +231,7 @@ public static function get_json_schema(): array {
230231
'pattern' => '^[0-9a-f]{32}\z',
231232
'minLength' => 32,
232233
'maxLength' => 32,
233-
'required' => false, // To be made required in a future release.
234+
'required' => true,
234235
'readonly' => true, // Omit from REST API.
235236
),
236237
'url' => array(
@@ -446,12 +447,12 @@ public function get_uuid(): string {
446447
* Gets ETag.
447448
*
448449
* @since 0.9.0
450+
* @since n.e.x.t No longer returns null as 'etag' is now required.
449451
*
450-
* @return non-empty-string|null ETag.
452+
* @return non-empty-string ETag.
451453
*/
452-
public function get_etag(): ?string {
453-
// Since the ETag is optional for now, return null for old URL Metrics that do not have one.
454-
return $this->data['etag'] ?? null;
454+
public function get_etag(): string {
455+
return $this->data['etag'];
455456
}
456457

457458
/**

plugins/optimization-detective/storage/class-od-url-metrics-post-type.php

-7
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,6 @@ public static function store_url_metric( string $slug, OD_URL_Metric $new_url_me
226226
}
227227

228228
$etag = $new_url_metric->get_etag();
229-
if ( null === $etag ) {
230-
// This case actually will never occur in practice because the store_url_metric function is only called
231-
// in the REST API endpoint where the ETag parameter is required. It is here exclusively for the sake of
232-
// PHPStan's static analysis. This entire condition can be removed in a future release when the 'etag'
233-
// property becomes required.
234-
return new WP_Error( 'missing_etag' );
235-
}
236229

237230
$group_collection = new OD_URL_Metric_Group_Collection(
238231
$url_metrics,

plugins/optimization-detective/tests/storage/test-class-od-url-metrics-post-type.php

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public function data_provider_test_get_url_metrics_from_post(): array {
9090
$valid_content = array(
9191
array(
9292
'url' => home_url( '/' ),
93+
'etag' => md5( '' ),
9394
'viewport' => array(
9495
'width' => 640,
9596
'height' => 480,

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

+14-13
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public function data_provider_to_test_constructor(): array {
3131
return array(
3232
'valid_minimal' => array(
3333
'data' => array(
34-
// Note: The 'etag' field is currently optional, so this data is still valid without it.
3534
'url' => home_url( '/' ),
35+
'etag' => md5( '' ),
3636
'viewport' => $viewport,
3737
'timestamp' => microtime( true ),
3838
'elements' => array(),
@@ -127,14 +127,14 @@ static function ( $value ) {
127127
'error' => 'OD_URL_Metric[etag] must be at most 32 characters long.',
128128
),
129129
'missing_etag' => array(
130-
'data' => array(
130+
'data' => array(
131131
'uuid' => wp_generate_uuid4(),
132132
'url' => home_url( '/' ),
133133
'viewport' => $viewport,
134134
'timestamp' => microtime( true ),
135135
'elements' => array(),
136136
),
137-
// Note: Add error message 'etag is a required property of OD_URL_Metric.' when 'etag' becomes mandatory.
137+
'error' => 'etag is a required property of OD_URL_Metric.',
138138
),
139139
'missing_viewport' => array(
140140
'data' => array(
@@ -327,14 +327,9 @@ static function ( OD_Element $element ) {
327327
$this->assertSame( $data['url'], $url_metric->get_url() );
328328
$this->assertSame( $data['url'], $url_metric->get( 'url' ) );
329329

330-
// Note: When the 'etag' field becomes required, the else statement can be removed.
331-
if ( array_key_exists( 'etag', $data ) ) {
332-
$this->assertSame( $data['etag'], $url_metric->get_etag() );
333-
$this->assertSame( $data['etag'], $url_metric->get( 'etag' ) );
334-
$this->assertTrue( 1 === preg_match( '/^[a-f0-9]{32}$/', $url_metric->get_etag() ) );
335-
} else {
336-
$this->assertNull( $url_metric->get_etag() );
337-
}
330+
$this->assertSame( $data['etag'], $url_metric->get_etag() );
331+
$this->assertSame( $data['etag'], $url_metric->get( 'etag' ) );
332+
$this->assertTrue( 1 === preg_match( '/^[a-f0-9]{32}$/', $url_metric->get_etag() ) );
338333

339334
$this->assertTrue( wp_is_uuid( $url_metric->get_uuid() ) );
340335
$this->assertSame( $url_metric->get_uuid(), $url_metric->get( 'uuid' ) );
@@ -370,6 +365,7 @@ public function data_provider_to_test_constructor_with_extended_schema(): array
370365

371366
$data = array(
372367
'url' => home_url( '/' ),
368+
'etag' => md5( '' ),
373369
'viewport' => $viewport,
374370
'timestamp' => microtime( true ),
375371
'elements' => array( $valid_element ),
@@ -390,6 +386,7 @@ static function ( array $properties ): array {
390386
},
391387
'data' => array(
392388
'url' => home_url( '/' ),
389+
'etag' => md5( '' ),
393390
'viewport' => $viewport,
394391
'timestamp' => microtime( true ),
395392
'elements' => array(),
@@ -424,6 +421,7 @@ static function ( array $properties ): array {
424421
},
425422
'data' => array(
426423
'url' => home_url( '/' ),
424+
'etag' => md5( '' ),
427425
'viewport' => $viewport,
428426
'timestamp' => microtime( true ),
429427
'elements' => array(),
@@ -455,6 +453,7 @@ static function ( array $properties ): array {
455453
},
456454
'data' => array(
457455
'url' => home_url( '/' ),
456+
'etag' => md5( '' ),
458457
'viewport' => $viewport,
459458
'timestamp' => microtime( true ),
460459
'elements' => array(),
@@ -478,6 +477,7 @@ static function ( array $properties ): array {
478477
},
479478
'data' => array(
480479
'url' => home_url( '/' ),
480+
'etag' => md5( '' ),
481481
'viewport' => $viewport,
482482
'timestamp' => microtime( true ),
483483
'elements' => array(
@@ -516,6 +516,7 @@ static function ( array $properties ): array {
516516
},
517517
'data' => array(
518518
'url' => home_url( '/' ),
519+
'etag' => md5( '' ),
519520
'viewport' => $viewport,
520521
'timestamp' => microtime( true ),
521522
'elements' => array( $valid_element ),
@@ -545,6 +546,7 @@ static function ( array $properties ): array {
545546
},
546547
'data' => array(
547548
'url' => home_url( '/' ),
549+
'etag' => md5( '' ),
548550
'viewport' => $viewport,
549551
'timestamp' => microtime( true ),
550552
'elements' => array(
@@ -917,8 +919,7 @@ public function test_get_json_schema_extensibility( Closure $set_up, Closure $as
917919
*/
918920
protected function check_schema_subset( array $schema, string $path, bool $extended = false ): void {
919921
$this->assertArrayHasKey( 'required', $schema, $path );
920-
// Skipping the check for 'root/etag' as it is currently optional.
921-
if ( ! $extended && 'root/etag' !== $path ) {
922+
if ( ! $extended ) {
922923
$this->assertTrue( $schema['required'], $path );
923924
}
924925
$this->assertArrayHasKey( 'type', $schema, $path );

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

+1-15
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public function data_provider_test_construction(): array {
7373
new OD_URL_Metric(
7474
array(
7575
'url' => home_url( '/' ),
76+
'etag' => md5( '' ),
7677
'viewport' => array(
7778
'width' => 1,
7879
'height' => 2,
@@ -263,21 +264,6 @@ public function data_provider_test_is_complete(): array {
263264
),
264265
'expected_is_group_complete' => false,
265266
),
266-
// Note: The following test case will not be required once the ETag is mandatory in a future release.
267-
'etag_missing' => array(
268-
'url_metric' => new OD_URL_Metric(
269-
array(
270-
'url' => home_url( '/' ),
271-
'viewport' => array(
272-
'width' => 400,
273-
'height' => 700,
274-
),
275-
'timestamp' => microtime( true ),
276-
'elements' => array(),
277-
)
278-
),
279-
'expected_is_group_complete' => false,
280-
),
281267
'etag_mismatch' => array(
282268
'url_metric' => $this->get_sample_url_metric( array( 'etag' => md5( 'different_etag' ) ) ),
283269
'expected_is_group_complete' => false,

0 commit comments

Comments
 (0)