Skip to content

Improve Test Coverage for Auto Sizes Plugin #1879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/auto-sizes/auto-sizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
// @codeCoverageIgnoreEnd

// Define the constant.
if ( defined( 'IMAGE_AUTO_SIZES_VERSION' ) ) {
Expand All @@ -31,3 +30,4 @@
require_once __DIR__ . '/includes/auto-sizes.php';
require_once __DIR__ . '/includes/improve-calculate-sizes.php';
require_once __DIR__ . '/hooks.php';
// @codeCoverageIgnoreEnd
2 changes: 1 addition & 1 deletion plugins/auto-sizes/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
// @codeCoverageIgnoreEnd

/**
* Displays the HTML generator tag for the plugin.
Expand Down Expand Up @@ -43,3 +42,4 @@ function auto_sizes_render_generator(): void {
add_filter( 'render_block_core/post-featured-image', 'auto_sizes_filter_image_tag', 10, 3 );
add_filter( 'get_block_type_uses_context', 'auto_sizes_filter_uses_context', 10, 2 );
add_filter( 'render_block_context', 'auto_sizes_filter_render_block_context', 10, 3 );
// @codeCoverageIgnoreEnd
2 changes: 1 addition & 1 deletion plugins/auto-sizes/includes/improve-calculate-sizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function auto_sizes_prime_attachment_caches( $content ): string {
*/
function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $block ): string {
if ( ! is_string( $content ) ) {
return '';
return ''; // @codeCoverageIgnore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto. I don't think this should be ignored because technically this is a code path that can be tested.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion to test for with auto_sizes_prime_attachment_caches() is a bit tricky though. For one, an assertion can assert that the input $content is identical to the return value. But then otherwise, when there is a class with an attachment ID in it or no class attribute at all, then this means _prime_post_caches() is called or not. I guess you could check to see if a query was done on the DB or not?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return ''; // @codeCoverageIgnore
return '';

}

$processor = new WP_HTML_Tag_Processor( $content );
Expand Down
106 changes: 106 additions & 0 deletions plugins/auto-sizes/tests/test-auto-sizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,15 @@ public function data_provider_to_test_auto_sizes_update_content_img_tag(): array
'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">',
'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">',
),
'expected_when_no_img_tag' => array(
'input' => '<p>No image here</p>',
'expected' => '<p>No image here</p>',
),
'expected_when_not_responsive' => array(
'input' => '<img src="https://example.com/foo.jpg" loading="lazy">',
'expected' => '<img src="https://example.com/foo.jpg" loading="lazy">',
),

);
}

Expand All @@ -279,4 +288,101 @@ public function test_auto_sizes_update_content_img_tag( string $input, string $e
auto_sizes_update_content_img_tag( $input )
);
}

/**
* @covers ::auto_sizes_attribute_includes_valid_auto
*/
public function test_auto_sizes_attribute_includes_valid_auto(): void {
// Test when 'auto' is the first item in the list.
$this->assertTrue( auto_sizes_attribute_includes_valid_auto( 'auto, 100vw' ) );

// Test when 'auto' is not the first item in the list.
$this->assertFalse( auto_sizes_attribute_includes_valid_auto( '100vw, auto' ) );
}

/**
* Provides data for the auto_sizes_update_image_attributes test.
*
* @return array<int,mixed[]> The data for the test cases.
*/
public function data_provider_for_auto_sizes_update_image_attributes(): array {
return array(
array(
array(
'loading' => 'eager',
'sizes' => '100vw',
),
array(
'loading' => 'eager',
'sizes' => '100vw',
),
),

// Test when the image is not responsive.
array( array( 'loading' => 'lazy' ), array( 'loading' => 'lazy' ) ),

// Test when 'auto' already exists in the sizes attribute.
array(
array(
'loading' => 'lazy',
'sizes' => 'auto, 100vw',
),
array(
'loading' => 'lazy',
'sizes' => 'auto, 100vw',
),
),

// Test when 'auto' needs to be added to the sizes attribute.
array(
array(
'loading' => 'lazy',
'sizes' => '100vw',
),
array(
'loading' => 'lazy',
'sizes' => 'auto, 100vw',
),
),
Comment on lines +310 to +346
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be better as associative arrays as then it becomes self-documenting, and the array keys of the top-level can be used in filtering.

For example for the first item it would be better as:

			'eager_something_blah_blah' => array(
				'attr'     => array(
					'loading' => 'eager',
					'sizes'   => '100vw',
				),
				'expected' => array(
					'loading' => 'eager',
					'sizes'   => '100vw',
				),
			),

Naturally you can use the appropriate labels for the keys 😄

);
}

/**
* @covers ::auto_sizes_update_image_attributes
*
* @dataProvider data_provider_for_auto_sizes_update_image_attributes
*
* @param array<string, mixed> $attr Attributes to be updated.
* @param array<string, mixed> $expected Expected updated attributes.
*/
public function test_auto_sizes_update_image_attributes( array $attr, array $expected ): void {
$this->assertSame( $expected, auto_sizes_update_image_attributes( $attr ) );
}

/**
* @covers ::auto_sizes_update_content_img_tag
*/
public function test_auto_sizes_update_content_img_tag_non_string_input(): void {
/*
* These tests are separate from the data provider approach because the function
* auto_sizes_update_content_img_tag() expects a string as an argument. Passing a non-string
* value would cause a TypeError. These tests ensure that the function behaves as expected
* when it receives non-string inputs.
*/
$this->assertSame( '', auto_sizes_update_content_img_tag( array() ) );
}

/**
* @covers ::auto_sizes_update_image_attributes
*/
public function test_auto_sizes_update_image_attributes_with_null_input(): void {
/*
* This test is separate because the main test method uses a data provider
* that expects the $attr parameter to be an array. Passing null directly
* would cause a TypeError due to the type hint. By testing null separately,
* we ensure that the function can handle null inputs gracefully without
* modifying the type hint in the main test method.
*/
$this->assertSame( array(), auto_sizes_update_image_attributes( null ) );
}
}
Loading