@@ -84,7 +84,7 @@ public function data_provider_test_filter_tag_visitors(): array {
84
84
}
85
85
86
86
/**
87
- * Test image_prioritizer_register_tag_visitors() .
87
+ * Test end-to-end .
88
88
*
89
89
* @covers ::image_prioritizer_register_tag_visitors
90
90
* @covers Image_Prioritizer_Tag_Visitor
@@ -97,7 +97,7 @@ public function data_provider_test_filter_tag_visitors(): array {
97
97
* @param callable|string $buffer Content before.
98
98
* @param callable|string $expected Expected content after.
99
99
*/
100
- public function test_image_prioritizer_register_tag_visitors ( callable $ set_up , $ buffer , $ expected ): void {
100
+ public function test_end_to_end ( callable $ set_up , $ buffer , $ expected ): void {
101
101
$ set_up ( $ this , $ this ::factory () );
102
102
103
103
$ buffer = is_string ( $ buffer ) ? $ buffer : $ buffer ();
@@ -219,7 +219,7 @@ public function data_provider_test_auto_sizes(): array {
219
219
* @dataProvider data_provider_test_auto_sizes
220
220
* @phpstan-param array{ xpath: string, isLCP: bool, intersectionRatio: int } $element_metrics
221
221
*/
222
- public function test_auto_sizes ( array $ element_metrics , string $ buffer , string $ expected ): void {
222
+ public function test_auto_sizes_end_to_end ( array $ element_metrics , string $ buffer , string $ expected ): void {
223
223
$ this ->populate_url_metrics ( array ( $ element_metrics ) );
224
224
225
225
$ html_start_doc = '<html lang="en"><head><meta charset="utf-8"><title>...</title></head><body> ' ;
@@ -236,30 +236,264 @@ public function test_auto_sizes( array $element_metrics, string $buffer, string
236
236
);
237
237
}
238
238
239
+ /**
240
+ * Test image_prioritizer_register_tag_visitors.
241
+ *
242
+ * @covers ::image_prioritizer_register_tag_visitors
243
+ */
244
+ public function test_image_prioritizer_register_tag_visitors (): void {
245
+ $ registry = new OD_Tag_Visitor_Registry ();
246
+ image_prioritizer_register_tag_visitors ( $ registry );
247
+ $ this ->assertTrue ( $ registry ->is_registered ( 'image-prioritizer/img ' ) );
248
+ $ this ->assertTrue ( $ registry ->is_registered ( 'image-prioritizer/background-image ' ) );
249
+ $ this ->assertTrue ( $ registry ->is_registered ( 'image-prioritizer/video ' ) );
250
+ }
251
+
252
+ /**
253
+ * Test image_prioritizer_filter_extension_module_urls.
254
+ *
255
+ * @covers ::image_prioritizer_filter_extension_module_urls
256
+ */
257
+ public function test_image_prioritizer_filter_extension_module_urls (): void {
258
+ $ initial_modules = array (
259
+ home_url ( '/module.js ' ),
260
+ );
261
+ $ filtered_modules = image_prioritizer_filter_extension_module_urls ( $ initial_modules );
262
+ $ this ->assertCount ( 2 , $ filtered_modules );
263
+ $ this ->assertSame ( $ initial_modules [0 ], $ filtered_modules [0 ] );
264
+ $ this ->assertStringContainsString ( 'detect. ' , $ filtered_modules [1 ] );
265
+ }
266
+
267
+ /**
268
+ * Test image_prioritizer_add_element_item_schema_properties.
269
+ *
270
+ * @covers ::image_prioritizer_add_element_item_schema_properties
271
+ */
272
+ public function test_image_prioritizer_add_element_item_schema_properties (): void {
273
+ $ initial_schema = array (
274
+ 'foo ' => array (
275
+ 'type ' => 'string ' ,
276
+ ),
277
+ );
278
+ $ filtered_schema = image_prioritizer_add_element_item_schema_properties ( $ initial_schema );
279
+ $ this ->assertCount ( 2 , $ filtered_schema );
280
+ $ this ->assertArrayHasKey ( 'foo ' , $ filtered_schema );
281
+ $ this ->assertArrayHasKey ( 'lcpElementExternalBackgroundImage ' , $ filtered_schema );
282
+ $ this ->assertSame ( 'object ' , $ filtered_schema ['lcpElementExternalBackgroundImage ' ]['type ' ] );
283
+ $ this ->assertSameSets ( array ( 'url ' , 'id ' , 'tag ' , 'class ' ), array_keys ( $ filtered_schema ['lcpElementExternalBackgroundImage ' ]['properties ' ] ) );
284
+ }
285
+
286
+ /**
287
+ * @return array<string, mixed>
288
+ */
289
+ public function data_provider_for_test_image_prioritizer_add_element_item_schema_properties_inputs (): array {
290
+ return array (
291
+ 'bad_type ' => array (
292
+ 'input_value ' => 'not_an_object ' ,
293
+ 'expected_exception ' => 'OD_URL_Metric[lcpElementExternalBackgroundImage] is not of type object. ' ,
294
+ 'output_value ' => null ,
295
+ ),
296
+ 'missing_props ' => array (
297
+ 'input_value ' => array (),
298
+ 'expected_exception ' => 'url is a required property of OD_URL_Metric[lcpElementExternalBackgroundImage]. ' ,
299
+ 'output_value ' => null ,
300
+ ),
301
+ 'bad_url_protocol ' => array (
302
+ 'input_value ' => array (
303
+ 'url ' => 'javascript:alert(1) ' ,
304
+ 'tag ' => 'DIV ' ,
305
+ 'id ' => null ,
306
+ 'class ' => null ,
307
+ ),
308
+ 'expected_exception ' => 'OD_URL_Metric[lcpElementExternalBackgroundImage][url] does not match pattern ^https?://. ' ,
309
+ 'output_value ' => null ,
310
+ ),
311
+ 'bad_url_format ' => array (
312
+ 'input_value ' => array (
313
+ 'url ' => 'https://not a valid URL!!! ' ,
314
+ 'tag ' => 'DIV ' ,
315
+ 'id ' => null ,
316
+ 'class ' => null ,
317
+ ),
318
+ 'expected_exception ' => null ,
319
+ 'output_value ' => array (
320
+ 'url ' => 'https://not%20a%20valid%20URL!!! ' , // This is due to sanitize_url() being used in core. More validation is needed.
321
+ 'tag ' => 'DIV ' ,
322
+ 'id ' => null ,
323
+ 'class ' => null ,
324
+ ),
325
+ ),
326
+ 'bad_url_length ' => array (
327
+ 'input_value ' => array (
328
+ 'url ' => 'https://example.com/ ' . str_repeat ( 'a ' , 501 ),
329
+ 'tag ' => 'DIV ' ,
330
+ 'id ' => null ,
331
+ 'class ' => null ,
332
+ ),
333
+ 'expected_exception ' => 'OD_URL_Metric[lcpElementExternalBackgroundImage][url] must be at most 500 characters long. ' ,
334
+ 'output_value ' => null ,
335
+ ),
336
+ 'bad_null_tag ' => array (
337
+ 'input_value ' => array (
338
+ 'url ' => 'https://example.com/ ' ,
339
+ 'tag ' => null ,
340
+ 'id ' => null ,
341
+ 'class ' => null ,
342
+ ),
343
+ 'expected_exception ' => 'OD_URL_Metric[lcpElementExternalBackgroundImage][tag] is not of type string. ' ,
344
+ 'output_value ' => null ,
345
+ ),
346
+ 'bad_format_tag ' => array (
347
+ 'input_value ' => array (
348
+ 'url ' => 'https://example.com/ ' ,
349
+ 'tag ' => 'bad tag name!! ' ,
350
+ 'id ' => null ,
351
+ 'class ' => null ,
352
+ ),
353
+ 'expected_exception ' => 'OD_URL_Metric[lcpElementExternalBackgroundImage][tag] does not match pattern ^[a-zA-Z0-9\-]+\z. ' ,
354
+ 'output_value ' => null ,
355
+ ),
356
+ 'bad_length_tag ' => array (
357
+ 'input_value ' => array (
358
+ 'url ' => 'https://example.com/ ' ,
359
+ 'tag ' => str_repeat ( 'a ' , 101 ),
360
+ 'id ' => null ,
361
+ 'class ' => null ,
362
+ ),
363
+ 'expected_exception ' => 'OD_URL_Metric[lcpElementExternalBackgroundImage][tag] must be at most 100 characters long. ' ,
364
+ 'output_value ' => null ,
365
+ ),
366
+ 'bad_type_id ' => array (
367
+ 'input_value ' => array (
368
+ 'url ' => 'https://example.com/ ' ,
369
+ 'tag ' => 'DIV ' ,
370
+ 'id ' => array ( 'bad ' ),
371
+ 'class ' => null ,
372
+ ),
373
+ 'expected_exception ' => 'OD_URL_Metric[lcpElementExternalBackgroundImage][id] is not of type string,null. ' ,
374
+ 'output_value ' => null ,
375
+ ),
376
+ 'bad_length_id ' => array (
377
+ 'input_value ' => array (
378
+ 'url ' => 'https://example.com/ ' ,
379
+ 'tag ' => 'DIV ' ,
380
+ 'id ' => str_repeat ( 'a ' , 101 ),
381
+ 'class ' => null ,
382
+ ),
383
+ 'expected_exception ' => 'OD_URL_Metric[lcpElementExternalBackgroundImage][id] must be at most 100 characters long. ' ,
384
+ 'output_value ' => null ,
385
+ ),
386
+ 'bad_type_class ' => array (
387
+ 'input_value ' => array (
388
+ 'url ' => 'https://example.com/ ' ,
389
+ 'tag ' => 'DIV ' ,
390
+ 'id ' => 'main ' ,
391
+ 'class ' => array ( 'bad ' ),
392
+ ),
393
+ 'expected_exception ' => 'OD_URL_Metric[lcpElementExternalBackgroundImage][class] is not of type string,null. ' ,
394
+ 'output_value ' => null ,
395
+ ),
396
+ 'bad_length_class ' => array (
397
+ 'input_value ' => array (
398
+ 'url ' => 'https://example.com/ ' ,
399
+ 'tag ' => 'DIV ' ,
400
+ 'id ' => 'main ' ,
401
+ 'class ' => str_repeat ( 'a ' , 501 ),
402
+ ),
403
+ 'expected_exception ' => 'OD_URL_Metric[lcpElementExternalBackgroundImage][class] must be at most 500 characters long. ' ,
404
+ 'output_value ' => null ,
405
+ ),
406
+ 'ok_minimal ' => array (
407
+ 'input_value ' => array (
408
+ 'url ' => 'https://example.com/bg.jpg ' ,
409
+ 'tag ' => 'DIV ' ,
410
+ 'id ' => null ,
411
+ 'class ' => null ,
412
+ ),
413
+ 'expected_exception ' => null ,
414
+ 'output_value ' => array (
415
+ 'url ' => 'https://example.com/bg.jpg ' ,
416
+ 'tag ' => 'DIV ' ,
417
+ 'id ' => null ,
418
+ 'class ' => null ,
419
+ ),
420
+ ),
421
+ 'ok_maximal ' => array (
422
+ 'input_value ' => array (
423
+ 'url ' => 'https://example.com/ ' . str_repeat ( 'a ' , 476 ) . '.jpg ' ,
424
+ 'tag ' => str_repeat ( 'a ' , 100 ),
425
+ 'id ' => str_repeat ( 'b ' , 100 ),
426
+ 'class ' => str_repeat ( 'c ' , 500 ),
427
+ ),
428
+ 'expected_exception ' => null ,
429
+ 'output_value ' => array (
430
+ 'url ' => 'https://example.com/ ' . str_repeat ( 'a ' , 476 ) . '.jpg ' ,
431
+ 'tag ' => str_repeat ( 'a ' , 100 ),
432
+ 'id ' => str_repeat ( 'b ' , 100 ),
433
+ 'class ' => str_repeat ( 'c ' , 500 ),
434
+ ),
435
+ ),
436
+ );
437
+ }
438
+
439
+ /**
440
+ * Test image_prioritizer_add_element_item_schema_properties for various inputs.
441
+ *
442
+ * @covers ::image_prioritizer_add_element_item_schema_properties
443
+ *
444
+ * @dataProvider data_provider_for_test_image_prioritizer_add_element_item_schema_properties_inputs
445
+ *
446
+ * @param mixed $input_value Input value.
447
+ * @param string|null $expected_exception Expected exception message.
448
+ * @param array<string, mixed>|null $output_value Output value.
449
+ */
450
+ public function test_image_prioritizer_add_element_item_schema_properties_inputs ( $ input_value , ?string $ expected_exception , ?array $ output_value ): void {
451
+ $ data = $ this ->get_sample_url_metric ( array () )->jsonSerialize ();
452
+ $ data ['lcpElementExternalBackgroundImage ' ] = $ input_value ;
453
+ $ exception_message = null ;
454
+ try {
455
+ $ url_metric = new OD_URL_Metric ( $ data );
456
+ } catch ( OD_Data_Validation_Exception $ e ) {
457
+ $ exception_message = $ e ->getMessage ();
458
+ }
459
+
460
+ $ this ->assertSame (
461
+ $ expected_exception ,
462
+ $ exception_message ,
463
+ isset ( $ url_metric ) ? 'Data: ' . wp_json_encode ( $ url_metric ->jsonSerialize (), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) : ''
464
+ );
465
+ if ( isset ( $ url_metric ) ) {
466
+ $ this ->assertSame ( $ output_value , $ url_metric ->jsonSerialize ()['lcpElementExternalBackgroundImage ' ] );
467
+ }
468
+ }
469
+
239
470
/**
240
471
* Test image_prioritizer_get_video_lazy_load_script.
241
472
*
242
473
* @covers ::image_prioritizer_get_video_lazy_load_script
474
+ * @covers ::image_prioritizer_get_asset_path
243
475
*/
244
476
public function test_image_prioritizer_get_video_lazy_load_script (): void {
245
- $ this ->assertGreaterThan ( 0 , strlen ( image_prioritizer_get_video_lazy_load_script () ) );
477
+ $ this ->assertStringContainsString ( ' new IntersectionObserver ' , image_prioritizer_get_video_lazy_load_script () );
246
478
}
247
479
248
480
/**
249
481
* Test image_prioritizer_get_lazy_load_bg_image_script.
250
482
*
251
483
* @covers ::image_prioritizer_get_lazy_load_bg_image_script
484
+ * @covers ::image_prioritizer_get_asset_path
252
485
*/
253
486
public function test_image_prioritizer_get_lazy_load_bg_image_script (): void {
254
- $ this ->assertGreaterThan ( 0 , strlen ( image_prioritizer_get_lazy_load_bg_image_script () ) );
487
+ $ this ->assertStringContainsString ( ' new IntersectionObserver ' , image_prioritizer_get_lazy_load_bg_image_script () );
255
488
}
256
489
257
490
/**
258
491
* Test image_prioritizer_get_lazy_load_bg_image_stylesheet.
259
492
*
260
493
* @covers ::image_prioritizer_get_lazy_load_bg_image_stylesheet
494
+ * @covers ::image_prioritizer_get_asset_path
261
495
*/
262
496
public function test_image_prioritizer_get_lazy_load_bg_image_stylesheet (): void {
263
- $ this ->assertGreaterThan ( 0 , strlen ( image_prioritizer_get_lazy_load_bg_image_stylesheet () ) );
497
+ $ this ->assertStringContainsString ( ' .od-lazy-bg-image ' , image_prioritizer_get_lazy_load_bg_image_stylesheet () );
264
498
}
265
499
}
0 commit comments