Skip to content

Commit d1ee911

Browse files
Merge pull request #2140 from WordPress/add/code-coverage
Improve Test Coverage for Auto Sizes Plugin
2 parents 434fb10 + de106d9 commit d1ee911

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,34 @@ public function set_up(): void {
5252
wp_clean_theme_json_cache();
5353
}
5454

55+
/**
56+
* Test that AUTO_SIZES_CONSTRAINTS contains all expected keys.
57+
*/
58+
public function test_auto_sizes_constraints_keys_exist(): void {
59+
$expected_keys = array( 'full', 'wide', 'left', 'right', 'default', 'center' );
60+
$constraints = defined( 'AUTO_SIZES_CONSTRAINTS' ) ? constant( 'AUTO_SIZES_CONSTRAINTS' ) : $GLOBALS['AUTO_SIZES_CONSTRAINTS'];
61+
$this->assertIsArray( $constraints );
62+
foreach ( $expected_keys as $key ) {
63+
$this->assertArrayHasKey( $key, $constraints, "Key '{$key}' not found in AUTO_SIZES_CONSTRAINTS." );
64+
}
65+
}
66+
67+
/**
68+
* Test that AUTO_SIZES_CONSTRAINTS values are as expected.
69+
*/
70+
public function test_auto_sizes_constraints_values(): void {
71+
$expected = array(
72+
'full' => 0,
73+
'wide' => 1,
74+
'left' => 2,
75+
'right' => 2,
76+
'default' => 3,
77+
'center' => 3,
78+
);
79+
$constraints = defined( 'AUTO_SIZES_CONSTRAINTS' ) ? constant( 'AUTO_SIZES_CONSTRAINTS' ) : $GLOBALS['AUTO_SIZES_CONSTRAINTS'];
80+
$this->assertEquals( $expected, $constraints );
81+
}
82+
5583
/**
5684
* Test that if disable responsive image then it will not add sizes attribute.
5785
*/
@@ -1276,6 +1304,7 @@ public static function data_image_block_with_parent_columns_and_its_parent_group
12761304
* Test that the image block in a two-column layout renders the correct sizes attribute.
12771305
*
12781306
* @cover ::auto_sizes_filter_image_tag
1307+
* @cover ::auto_sizes_filter_render_block_context
12791308
*/
12801309
public function test_image_block_in_two_column_layout_renders_correct_sizes_attribute(): void {
12811310
// Skip test for WordPress versions below 6.8.
@@ -1303,6 +1332,7 @@ public function test_image_block_in_two_column_layout_renders_correct_sizes_attr
13031332
* Test that the image block in a three-column layout renders the correct sizes attribute.
13041333
*
13051334
* @cover ::auto_sizes_filter_image_tag
1335+
* @cover ::auto_sizes_filter_render_block_context
13061336
*/
13071337
public function test_image_block_in_three_column_layout_renders_correct_sizes_attribute(): void {
13081338
// Skip test for WordPress versions below 6.8.
@@ -1457,6 +1487,19 @@ public static function data_post_featured_image_block_alignment(): array {
14571487
);
14581488
}
14591489

1490+
/**
1491+
* Test that auto_sizes_get_featured_image_attachment_id() returns 0 when post_id is not found.
1492+
*
1493+
* @covers ::auto_sizes_get_featured_image_attachment_id
1494+
*/
1495+
public function test_auto_sizes_get_featured_image_attachment_id_returns_zero_for_zero_post_id(): void {
1496+
$this->assertSame(
1497+
0,
1498+
auto_sizes_get_featured_image_attachment_id( 0 ),
1499+
'Should return 0 when post_id is not found.'
1500+
);
1501+
}
1502+
14601503
/**
14611504
* Filter the theme.json data to include relative layout sizes.
14621505
*

0 commit comments

Comments
 (0)