Skip to content

Commit bd642b4

Browse files
committed
Update test cases to handle ETag being required
Signed-off-by: Shyamsundar Gadde <[email protected]>
1 parent 8986003 commit bd642b4

File tree

3 files changed

+16
-28
lines changed

3 files changed

+16
-28
lines changed

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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 14 additions & 13 deletions
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

Lines changed: 1 addition & 15 deletions
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)