@@ -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 );
0 commit comments