Skip to content
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

Improve Test Coverage for Auto Sizes Plugin #1879

Open
wants to merge 8 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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 @@ -42,3 +41,4 @@ function auto_sizes_render_generator(): void {
add_filter( 'render_block_core/cover', '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, 2 );
// @codeCoverageIgnoreEnd
2 changes: 1 addition & 1 deletion plugins/auto-sizes/includes/auto-sizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function auto_sizes_update_image_attributes( $attr ): array {
*/
function auto_sizes_update_content_img_tag( $html ): string {
if ( ! is_string( $html ) ) {
$html = '';
$html = '';// @codeCoverageIgnore

Choose a reason for hiding this comment

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

Suggested change
$html = '';// @codeCoverageIgnore
$html = ''; // @codeCoverageIgnore

Copy link
Member

Choose a reason for hiding this comment

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

Thanks @pravinparmar2404 for contribution.

}

$processor = new WP_HTML_Tag_Processor( $html );
Expand Down
6 changes: 3 additions & 3 deletions plugins/auto-sizes/includes/improve-calculate-sizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
function auto_sizes_prime_attachment_caches( $content ): 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.

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

}

$processor = new WP_HTML_Tag_Processor( $content );
Expand All @@ -26,7 +26,7 @@ function auto_sizes_prime_attachment_caches( $content ): string {
$class = $processor->get_attribute( 'class' );

if ( ! is_string( $class ) ) {
continue;
continue;// @codeCoverageIgnore
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
continue;// @codeCoverageIgnore
continue; // @codeCoverageIgnore

}

if ( preg_match( '/(?:^|\s)wp-image-([1-9][0-9]*)(?:\s|$)/', $class, $class_id ) === 1 ) {
Expand Down Expand Up @@ -63,7 +63,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.

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

}

$processor = new WP_HTML_Tag_Processor( $content );
Expand Down
87 changes: 87 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,82 @@ 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.
$sizes_attr = 'auto, 100vw';
$this->assertTrue( auto_sizes_attribute_includes_valid_auto( $sizes_attr ) );

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

/**
* 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',
),
),

// Test when $attr is not an array.
array(
'attr' => array(),
'expected' => array(),
),
);
}

/**
* @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 ) );
}
}
Loading