diff --git a/plugins/dominant-color-images/load.php b/plugins/dominant-color-images/load.php index 41672f9b5d..223204885d 100644 --- a/plugins/dominant-color-images/load.php +++ b/plugins/dominant-color-images/load.php @@ -19,7 +19,7 @@ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. } -// @codeCoverageIgnoreEnd + // Define required constants. if ( defined( 'DOMINANT_COLOR_IMAGES_VERSION' ) ) { @@ -30,3 +30,4 @@ require_once __DIR__ . '/helper.php'; require_once __DIR__ . '/hooks.php'; +// @codeCoverageIgnoreEnd diff --git a/plugins/dominant-color-images/tests/test-dominant-color-image-editor-imagick.php b/plugins/dominant-color-images/tests/test-dominant-color-image-editor-imagick.php index 5dde0bae6a..4f9a66b326 100644 --- a/plugins/dominant-color-images/tests/test-dominant-color-image-editor-imagick.php +++ b/plugins/dominant-color-images/tests/test-dominant-color-image-editor-imagick.php @@ -8,7 +8,12 @@ */ use Dominant_Color_Images\Tests\TestCase; +use Imagick; +use ImagickPixel; +/** + * @coversDefaultClass Dominant_Color_Image_Editor_Imagick + */ class Test_Dominant_Color_Image_Editor_Imagick extends TestCase { /** @@ -33,4 +38,164 @@ static function ( $editor ): bool { } ); } + + /** + * @covers ::get_dominant_color + */ + public function test_failure_handling(): void { + $editor = new Dominant_Color_Image_Editor_Imagick( '/image.jpg' ); + $result = $editor->get_dominant_color(); + $this->assertWPError( $result ); + $this->assertEquals( 'image_editor_dominant_color_error_no_image', $result->get_error_code() ); + } + + /** + * @covers ::get_dominant_color + */ + public function test_get_dominant_color_no_image(): void { + $editor = new Dominant_Color_Image_Editor_Imagick( null ); + $result = $editor->get_dominant_color(); + + $this->assertWPError( $result ); + $this->assertEquals( 'image_editor_dominant_color_error_no_image', $result->get_error_code() ); + } + + /** + * @covers ::get_dominant_color + */ + public function test_get_dominant_color_exception(): void { + // Creating mock that will throw an exception. + $mock = $this->getMockBuilder( Dominant_Color_Image_Editor_Imagick::class ) + ->onlyMethods( array( '__construct' ) ) + ->disableOriginalConstructor() + ->getMock(); + + $reflection = new ReflectionClass( $mock ); + $property = $reflection->getProperty( 'image' ); + $property->setAccessible( true ); + $property->setValue( $mock, new Imagick() ); + + $result = $mock->get_dominant_color(); + + $this->assertWPError( $result ); + $this->assertEquals( 'image_editor_dominant_color_error', $result->get_error_code() ); + } + + /** + * @covers ::has_transparency + */ + public function test_has_transparency_no_image(): void { + $editor = new Dominant_Color_Image_Editor_Imagick( null ); + $result = $editor->has_transparency(); + + $this->assertWPError( $result ); + $this->assertEquals( 'image_editor_has_transparency_error_no_image', $result->get_error_code() ); + } + + /** + * @covers ::has_transparency + */ + public function test_has_no_transparency(): void { + $imagick = new Imagick(); + $imagick->newImage( 1, 1, new ImagickPixel( 'red' ) ); + + $editor = new Dominant_Color_Image_Editor_Imagick( null ); + $reflection = new ReflectionClass( $editor ); + $property = $reflection->getProperty( 'image' ); + $property->setAccessible( true ); + $property->setValue( $editor, $imagick ); + + $result = $editor->has_transparency(); + + $this->assertFalse( $result ); + } + + /** + * @covers ::has_transparency + */ + public function test_has_transparency_exception(): void { + // Creating mock that will throw an exception. + $mock = $this->getMockBuilder( Dominant_Color_Image_Editor_Imagick::class ) + ->onlyMethods( array( '__construct' ) ) + ->disableOriginalConstructor() + ->getMock(); + + $reflection = new ReflectionClass( $mock ); + $property = $reflection->getProperty( 'image' ); + $property->setAccessible( true ); + $property->setValue( $mock, new Imagick() ); + + $result = $mock->has_transparency(); + + $this->assertWPError( $result ); + $this->assertEquals( 'image_editor_has_transparency_error', $result->get_error_code() ); + } + + /** + * @covers ::get_dominant_color + */ + public function test_get_dominant_color_success(): void { + // Create a red test image. + $imagick = new Imagick(); + $imagick->newImage( 1, 1, new ImagickPixel( '#FF0000' ) ); + + $editor = new Dominant_Color_Image_Editor_Imagick( null ); + $reflection = new ReflectionClass( $editor ); + $property = $reflection->getProperty( 'image' ); + $property->setAccessible( true ); + $property->setValue( $editor, $imagick ); + + $result = $editor->get_dominant_color(); + + $this->assertEquals( 'ff0000', $result ); + } + + /** + * @covers ::has_transparency + */ + public function test_has_transparency_with_transparency(): void { + // Create an image with transparency. + $imagick = new Imagick(); + $imagick->newImage( 1, 1, new ImagickPixel( 'rgba(255,0,0,0.5)' ) ); + + $editor = new Dominant_Color_Image_Editor_Imagick( null ); + $reflection = new ReflectionClass( $editor ); + $property = $reflection->getProperty( 'image' ); + $property->setAccessible( true ); + $property->setValue( $editor, $imagick ); + + $result = $editor->has_transparency(); + + $this->assertTrue( $result ); + } + + /** + * @covers ::has_transparency + */ + public function test_has_transparency_no_alpha_channel_method(): void { + // Mock the Imagick object to simulate when getImageAlphaChannel method doesn't exist. + $imagick_mock = $this->getMockBuilder( Imagick::class ) + ->disableOriginalConstructor() + ->getMock(); + + $imagick_mock->method( 'getImageWidth' )->willReturn( 1 ); + $imagick_mock->method( 'getImageHeight' )->willReturn( 1 ); + + $pixel = $this->getMockBuilder( ImagickPixel::class ) + ->disableOriginalConstructor() + ->getMock(); + $pixel->method( 'getColor' )->willReturn( array( 'a' => 0 ) ); + + $imagick_mock->method( 'getImagePixelColor' )->willReturn( $pixel ); + + $editor = new Dominant_Color_Image_Editor_Imagick( null ); + $reflection = new ReflectionClass( $editor ); + $property = $reflection->getProperty( 'image' ); + $property->setAccessible( true ); + $property->setValue( $editor, $imagick_mock ); + + $result = $editor->has_transparency(); + + $this->assertFalse( $result ); + } } diff --git a/plugins/dominant-color-images/tests/test-dominant-color.php b/plugins/dominant-color-images/tests/test-dominant-color.php index 9c321c9c8e..f899b58595 100644 --- a/plugins/dominant-color-images/tests/test-dominant-color.php +++ b/plugins/dominant-color-images/tests/test-dominant-color.php @@ -42,7 +42,7 @@ public function test_dominant_color_metadata( string $image_path, array $expecte * * @dataProvider provider_get_dominant_color * - * @covers ::dominant_color_get_dominant_color + * @covers helper::dominant_color_get_dominant_color * * @param string $image_path Image path. * @param string[] $expected_color Expected color. @@ -91,7 +91,7 @@ public function test_has_transparency_metadata( string $image_path, array $expec * * @dataProvider provider_get_dominant_color * - * @covers ::dominant_color_get_dominant_color + * @covers helper::dominant_color_get_dominant_color * * @param string $image_path Image path. * @param string[] $expected_color Expected color. @@ -113,7 +113,7 @@ public function test_dominant_color_has_transparency( string $image_path, array * * @dataProvider provider_get_dominant_color * - * @covers ::dominant_color_img_tag_add_dominant_color + * @covers hooks::dominant_color_img_tag_add_dominant_color * * @param string $image_path Image path. * @param string[] $expected_color Expected color. @@ -199,7 +199,7 @@ public function data_dominant_color_img_tag_add_dominant_color_requires_proper_q * * @dataProvider data_provider_dominant_color_check_inline_style * - * @covers ::dominant_color_img_tag_add_dominant_color + * @covers hooks::dominant_color_img_tag_add_dominant_color * * @param string $filtered_image The filtered image markup. * Must include `src="%s" width="%d" height="%d"`. @@ -408,4 +408,105 @@ public function test_dominant_color_render_generator(): void { $this->assertStringContainsString( 'generator', $tag ); $this->assertStringContainsString( 'dominant-color-images ' . DOMINANT_COLOR_IMAGES_VERSION, $tag ); } + + /** + * @covers Dominant_Color_Image_Editor_GD::get_dominant_color + */ + public function test_invalid_image_type(): void { + $editor = new Dominant_Color_Image_Editor_GD( '/invalid/type' ); + $result = $editor->get_dominant_color(); + $this->assertWPError( $result ); + $this->assertEquals( 'image_editor_dominant_color_error_no_image', $result->get_error_code() ); + } + + /** + * @covers Dominant_Color_Image_Editor_GD::get_dominant_color + */ + public function test_corrupted_image_file(): void { + $editor = new Dominant_Color_Image_Editor_GD( 'path/to/corrupted/file.jpg' ); + $result = $editor->get_dominant_color(); + $this->assertWPError( $result ); + $this->assertEquals( 'image_editor_dominant_color_error_no_image', $result->get_error_code() ); + } + + /** + * @covers ::dominant_color_add_inline_style + */ + public function test_dominant_color_add_inline_style(): void { + dominant_color_add_inline_style(); + + // Verify the style was registered. + global $wp_styles; + $this->assertTrue( isset( $wp_styles->registered['dominant-color-styles'] ) ); + + // Verify the style was enqueued. + $this->assertTrue( wp_style_is( 'dominant-color-styles', 'enqueued' ) ); + + // Verify the inline style was added. + $inline_styles = $wp_styles->get_data( 'dominant-color-styles', 'after' ); + $this->assertNotEmpty( $inline_styles ); + $this->assertStringContainsString( + 'img[data-dominant-color]:not(.has-transparency) { background-color: var(--dominant-color); }', + implode( '', $inline_styles ) + ); + } + + /** + * @covers ::dominant_color_admin_inline_style + */ + public function test_dominant_color_admin_inline_style(): void { + dominant_color_admin_inline_style(); + + // Verify the style was registered. + global $wp_styles; + $this->assertTrue( isset( $wp_styles->registered['dominant-color-admin-styles'] ) ); + + // Verify the style was enqueued. + $this->assertTrue( wp_style_is( 'dominant-color-admin-styles', 'enqueued' ) ); + + // Verify the inline style was added. + $inline_styles = $wp_styles->get_data( 'dominant-color-admin-styles', 'after' ); + $this->assertNotEmpty( $inline_styles ); + $this->assertStringContainsString( + '.wp-core-ui .attachment-preview[data-dominant-color]:not(.has-transparency) { background-color: var(--dominant-color); }', + implode( '', $inline_styles ) + ); + } + + /** + * @covers ::dominant_color_admin_script + */ + public function test_dominant_color_admin_script(): void { + ob_start(); + dominant_color_admin_script(); + $output = ob_get_clean(); + + // Verify if script tag exists. + $this->assertStringEndsWith( '', trim( $output ) ); + + // Verify the key elements of the script. + $this->assertStringContainsString( 'tmpl.textContent.replace', $output ); + $this->assertStringContainsString( 'data-dominant-color', $output ); + $this->assertStringContainsString( 'data-has-transparency', $output ); + $this->assertStringContainsString( '--dominant-color', $output ); + } + + /** + * @covers ::dominant_color_prepare_attachment_for_js + */ + public function test_dominant_color_prepare_attachment_for_js(): void { + $attachment = self::factory()->post->create_and_get( array( 'post_type' => 'attachment' ) ); + + $meta = array( + 'dominant_color' => 'ff0000', + 'has_transparency' => true, + ); + + $response = dominant_color_prepare_attachment_for_js( array(), $attachment, $meta ); + + $this->assertArrayHasKey( 'dominantColor', $response ); + $this->assertArrayHasKey( 'hasTransparency', $response ); + $this->assertEquals( 'ff0000', $response['dominantColor'] ); + $this->assertTrue( $response['hasTransparency'] ); + } } diff --git a/plugins/optimization-detective/tests/test-class-od-html-tag-processor.php b/plugins/optimization-detective/tests/test-class-od-html-tag-processor.php index 0c6953c654..d483efa996 100644 --- a/plugins/optimization-detective/tests/test-class-od-html-tag-processor.php +++ b/plugins/optimization-detective/tests/test-class-od-html-tag-processor.php @@ -451,7 +451,7 @@ public function test_next_tag_and_get_xpath( string $document, array $open_tags, $actual_xpath_breadcrumbs_mapping[ $xpath ] = $p->get_breadcrumbs(); $transitional_xpath = $p->get_xpath(); - $this->assertRegExp( + $this->assertMatchesRegularExpression( '#^/HTML( /HEAD(/\*\[\d+]\[self::\w+])? |