Skip to content

Commit 434fb10

Browse files
Merge pull request #1879 from sarthak-19/add/code_coverage_auto_sizes
Improve Test Coverage for Auto Sizes Plugin
2 parents 1fc81a3 + 3a983f2 commit 434fb10

File tree

3 files changed

+112
-2
lines changed

3 files changed

+112
-2
lines changed

plugins/auto-sizes/auto-sizes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
if ( ! defined( 'ABSPATH' ) ) {
2020
exit; // Exit if accessed directly.
2121
}
22-
// @codeCoverageIgnoreEnd
2322

2423
// Define the constant.
2524
if ( defined( 'IMAGE_AUTO_SIZES_VERSION' ) ) {
@@ -31,3 +30,4 @@
3130
require_once __DIR__ . '/includes/auto-sizes.php';
3231
require_once __DIR__ . '/includes/improve-calculate-sizes.php';
3332
require_once __DIR__ . '/hooks.php';
33+
// @codeCoverageIgnoreEnd

plugins/auto-sizes/hooks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
if ( ! defined( 'ABSPATH' ) ) {
1111
exit; // Exit if accessed directly.
1212
}
13-
// @codeCoverageIgnoreEnd
1413

1514
/**
1615
* Displays the HTML generator tag for the plugin.
@@ -43,3 +42,4 @@ function auto_sizes_render_generator(): void {
4342
add_filter( 'render_block_core/post-featured-image', 'auto_sizes_filter_image_tag', 10, 3 );
4443
add_filter( 'get_block_type_uses_context', 'auto_sizes_filter_uses_context', 10, 2 );
4544
add_filter( 'render_block_context', 'auto_sizes_filter_render_block_context', 10, 3 );
45+
// @codeCoverageIgnoreEnd

plugins/auto-sizes/tests/test-auto-sizes.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,15 @@ public function data_provider_to_test_auto_sizes_update_content_img_tag(): array
266266
'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">',
267267
'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">',
268268
),
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+
269278
);
270279
}
271280

@@ -279,4 +288,105 @@ public function test_auto_sizes_update_content_img_tag( string $input, string $e
279288
auto_sizes_update_content_img_tag( $input )
280289
);
281290
}
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+
}
282392
}

0 commit comments

Comments
 (0)