Skip to content

Commit 7d42cac

Browse files
b1ink0westonruter
andcommitted
Fix failing test because of new source property
Co-authored-by: Weston Ruter <[email protected]>
1 parent 5e64b39 commit 7d42cac

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function data_provider_to_test_constructor(): array {
3131
return array(
3232
'valid_minimal' => array(
3333
'data' => array(
34+
// Note: The 'source' field is currently optional, so this data is still valid without it.
3435
'url' => home_url( '/' ),
3536
'etag' => md5( '' ),
3637
'viewport' => $viewport,
@@ -136,6 +137,17 @@ static function ( $value ) {
136137
),
137138
'error' => 'etag is a required property of OD_URL_Metric.',
138139
),
140+
'missing_source' => array(
141+
'data' => array(
142+
'uuid' => wp_generate_uuid4(),
143+
'etag' => md5( '' ),
144+
'url' => home_url( '/' ),
145+
'viewport' => $viewport,
146+
'timestamp' => microtime( true ),
147+
'elements' => array(),
148+
),
149+
// Note: Add error message 'source is a required property of OD_URL_Metric.' when 'source' becomes mandatory.
150+
),
139151
'missing_viewport' => array(
140152
'data' => array(
141153
'uuid' => wp_generate_uuid4(),
@@ -331,6 +343,15 @@ static function ( OD_Element $element ) {
331343
$this->assertSame( $data['etag'], $url_metric->get( 'etag' ) );
332344
$this->assertTrue( 1 === preg_match( '/^[a-f0-9]{32}$/', $url_metric->get_etag() ) );
333345

346+
// Note: When the 'source' field becomes required, the else statement can be removed.
347+
if ( array_key_exists( 'source', $data ) ) {
348+
$this->assertSame( $data['source'], $url_metric->get_source() );
349+
$this->assertSame( $data['source'], $url_metric->get( 'source' ) );
350+
$this->assertContains( $url_metric->get_source(), array( 'visitor', 'user', 'synthetic' ) );
351+
} else {
352+
$this->assertNull( $url_metric->get_source() );
353+
}
354+
334355
$this->assertTrue( wp_is_uuid( $url_metric->get_uuid() ) );
335356
$this->assertSame( $url_metric->get_uuid(), $url_metric->get( 'uuid' ) );
336357

@@ -919,7 +940,7 @@ public function test_get_json_schema_extensibility( Closure $set_up, Closure $as
919940
*/
920941
protected function check_schema_subset( array $schema, string $path, bool $extended = false ): void {
921942
$this->assertArrayHasKey( 'required', $schema, $path );
922-
if ( ! $extended ) {
943+
if ( ! $extended && 'root/source' !== $path ) {
923944
$this->assertTrue( $schema['required'], $path );
924945
}
925946
$this->assertArrayHasKey( 'type', $schema, $path );

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,23 @@ public function data_provider_test_is_complete(): array {
295295
'freshness_ttl' => -1,
296296
'expected_is_group_complete' => false,
297297
),
298+
// Note: The following test case will not be required once the 'source' is mandatory in a future release.
299+
'source_missing' => array(
300+
'url_metric' => new OD_URL_Metric(
301+
array(
302+
'url' => home_url( '/' ),
303+
'etag' => md5( '' ),
304+
'viewport' => array(
305+
'width' => 400,
306+
'height' => 700,
307+
),
308+
'timestamp' => microtime( true ),
309+
'elements' => array(),
310+
)
311+
),
312+
'freshness_ttl' => HOUR_IN_SECONDS,
313+
'expected_is_group_complete' => true,
314+
),
298315
);
299316
}
300317

0 commit comments

Comments
 (0)