@@ -266,6 +266,15 @@ public function data_provider_to_test_auto_sizes_update_content_img_tag(): array
266
266
'input ' => '<img src="https://example.com/foo-300x225.jpg" srcset="https://example.com/foo-300x225.jpg 300w, https://example.com/foo-1024x768.jpg 1024w, https://example.com/foo-768x576.jpg 768w, https://example.com/foo-1536x1152.jpg 1536w, https://example.com/foo-2048x1536.jpg 2048w" sizes=",,,,,,,,,auto, (max-width: 650px) 100vw, 650px" loading="lazy"> ' ,
267
267
'expected ' => '<img src="https://example.com/foo-300x225.jpg" srcset="https://example.com/foo-300x225.jpg 300w, https://example.com/foo-1024x768.jpg 1024w, https://example.com/foo-768x576.jpg 768w, https://example.com/foo-1536x1152.jpg 1536w, https://example.com/foo-2048x1536.jpg 2048w" sizes="auto, ,,,,,,,,,auto, (max-width: 650px) 100vw, 650px" loading="lazy"> ' ,
268
268
),
269
+ 'expected_when_no_img_tag ' => array (
270
+ 'input ' => '<p>No image here</p> ' ,
271
+ 'expected ' => '<p>No image here</p> ' ,
272
+ ),
273
+ 'expected_when_not_responsive ' => array (
274
+ 'input ' => '<img src="https://example.com/foo.jpg" loading="lazy"> ' ,
275
+ 'expected ' => '<img src="https://example.com/foo.jpg" loading="lazy"> ' ,
276
+ ),
277
+
269
278
);
270
279
}
271
280
@@ -279,4 +288,105 @@ public function test_auto_sizes_update_content_img_tag( string $input, string $e
279
288
auto_sizes_update_content_img_tag ( $ input )
280
289
);
281
290
}
291
+
292
+ /**
293
+ * @covers ::auto_sizes_attribute_includes_valid_auto
294
+ */
295
+ public function test_auto_sizes_attribute_includes_valid_auto (): void {
296
+ // Test when 'auto' is the first item in the list.
297
+ $ this ->assertTrue ( auto_sizes_attribute_includes_valid_auto ( 'auto, 100vw ' ) );
298
+
299
+ // Test when 'auto' is not the first item in the list.
300
+ $ this ->assertFalse ( auto_sizes_attribute_includes_valid_auto ( '100vw, auto ' ) );
301
+ }
302
+
303
+ /**
304
+ * Provides data for the auto_sizes_update_image_attributes test.
305
+ *
306
+ * @return array<string, array<string, array<string, string>>> The data for the test cases.
307
+ */
308
+ public function data_provider_for_auto_sizes_update_image_attributes (): array {
309
+ return array (
310
+ 'does_not_modify_eager_image ' => array (
311
+ 'attr ' => array (
312
+ 'loading ' => 'eager ' ,
313
+ 'sizes ' => '100vw ' ,
314
+ ),
315
+ 'expected ' => array (
316
+ 'loading ' => 'eager ' ,
317
+ 'sizes ' => '100vw ' ,
318
+ ),
319
+ ),
320
+
321
+ 'does_not_modify_non_responsive_image ' => array (
322
+ 'attr ' => array (
323
+ 'loading ' => 'lazy ' ,
324
+ ),
325
+ 'expected ' => array (
326
+ 'loading ' => 'lazy ' ,
327
+ ),
328
+ ),
329
+
330
+ 'does_not_duplicate_auto_in_sizes ' => array (
331
+ 'attr ' => array (
332
+ 'loading ' => 'lazy ' ,
333
+ 'sizes ' => 'auto, 100vw ' ,
334
+ ),
335
+ 'expected ' => array (
336
+ 'loading ' => 'lazy ' ,
337
+ 'sizes ' => 'auto, 100vw ' ,
338
+ ),
339
+ ),
340
+
341
+ 'adds_auto_to_sizes_when_needed ' => array (
342
+ 'attr ' => array (
343
+ 'loading ' => 'lazy ' ,
344
+ 'sizes ' => '100vw ' ,
345
+ ),
346
+ 'expected ' => array (
347
+ 'loading ' => 'lazy ' ,
348
+ 'sizes ' => 'auto, 100vw ' ,
349
+ ),
350
+ ),
351
+ );
352
+ }
353
+
354
+ /**
355
+ * @covers ::auto_sizes_update_image_attributes
356
+ *
357
+ * @dataProvider data_provider_for_auto_sizes_update_image_attributes
358
+ *
359
+ * @param array<string, mixed> $attr Attributes to be updated.
360
+ * @param array<string, mixed> $expected Expected updated attributes.
361
+ */
362
+ public function test_auto_sizes_update_image_attributes ( array $ attr , array $ expected ): void {
363
+ $ this ->assertSame ( $ expected , auto_sizes_update_image_attributes ( $ attr ) );
364
+ }
365
+
366
+ /**
367
+ * @covers ::auto_sizes_update_content_img_tag
368
+ */
369
+ public function test_auto_sizes_update_content_img_tag_non_string_input (): void {
370
+ /*
371
+ * These tests are separate from the data provider approach because the function
372
+ * auto_sizes_update_content_img_tag() expects a string as an argument. Passing a non-string
373
+ * value would cause a TypeError. These tests ensure that the function behaves as expected
374
+ * when it receives non-string inputs.
375
+ */
376
+ $ this ->assertSame ( '' , auto_sizes_update_content_img_tag ( array () ) );
377
+ }
378
+
379
+ /**
380
+ * @covers ::auto_sizes_update_image_attributes
381
+ */
382
+ public function test_auto_sizes_update_image_attributes_with_null_input (): void {
383
+ /*
384
+ * This test is separate because the main test method uses a data provider
385
+ * that expects the $attr parameter to be an array. Passing null directly
386
+ * would cause a TypeError due to the type hint. By testing null separately,
387
+ * we ensure that the function can handle null inputs gracefully without
388
+ * modifying the type hint in the main test method.
389
+ */
390
+ $ this ->assertSame ( array (), auto_sizes_update_image_attributes ( null ) );
391
+ }
282
392
}
0 commit comments