Skip to content

Commit 2e48f6f

Browse files
authored
Merge branch 'trunk' into fix/od-on-non-https-site
2 parents d5679ec + ad15727 commit 2e48f6f

File tree

31 files changed

+488
-342
lines changed

31 files changed

+488
-342
lines changed

package-lock.json

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"@wordpress/scripts": "^30.12.0",
1818
"commander": "13.1.0",
1919
"copy-webpack-plugin": "^13.0.0",
20-
"css-minimizer-webpack-plugin": "^7.0.0",
20+
"css-minimizer-webpack-plugin": "^7.0.2",
2121
"fast-glob": "^3.3.3",
2222
"fs-extra": "^11.3.0",
2323
"husky": "^9.1.7",

plugins/dominant-color-images/helper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
*/
1818
function dominant_color_set_image_editors( array $editors ): array {
1919
if ( ! class_exists( 'Dominant_Color_Image_Editor_GD' ) ) {
20-
require_once __DIR__ . '/class-dominant-color-image-editor-gd.php';
20+
require_once __DIR__ . '/class-dominant-color-image-editor-gd.php';// @codeCoverageIgnore
2121
}
2222
if ( ! class_exists( 'Dominant_Color_Image_Editor_Imagick' ) ) {
23-
require_once __DIR__ . '/class-dominant-color-image-editor-imagick.php';
23+
require_once __DIR__ . '/class-dominant-color-image-editor-imagick.php';// @codeCoverageIgnore
2424
}
2525

2626
$replaces = array(

plugins/dominant-color-images/load.php

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

2424
// Define required constants.
2525
if ( defined( 'DOMINANT_COLOR_IMAGES_VERSION' ) ) {
@@ -30,3 +30,4 @@
3030

3131
require_once __DIR__ . '/helper.php';
3232
require_once __DIR__ . '/hooks.php';
33+
// @codeCoverageIgnoreEnd

plugins/dominant-color-images/tests/data/class-testcase.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,31 @@
55
use WP_UnitTestCase;
66

77
abstract class TestCase extends WP_UnitTestCase {
8+
9+
/**
10+
* Runs the routine before each test is executed.
11+
*/
12+
public function set_up(): void {
13+
parent::set_up();
14+
$this->reset_wp_dependencies();
15+
}
16+
17+
/**
18+
* After a test method runs, resets any state in WordPress the test method might have changed.
19+
*/
20+
public function tear_down(): void {
21+
parent::tear_down();
22+
$this->reset_wp_dependencies();
23+
}
24+
25+
/**
26+
* Reset WP_Scripts and WP_Styles.
27+
*/
28+
private function reset_wp_dependencies(): void {
29+
$GLOBALS['wp_scripts'] = null;
30+
$GLOBALS['wp_styles'] = null;
31+
}
32+
833
/**
934
* Data provider for test_get_dominant_color_GD.
1035
*

plugins/dominant-color-images/tests/test-dominant-color-image-editor-imagick.php

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
* @since 1.2.0
66
*
77
* @package dominant-color-images
8+
* @noinspection PhpComposerExtensionStubsInspection
9+
* @noinspection PhpUnhandledExceptionInspection
810
*/
911

1012
use Dominant_Color_Images\Tests\TestCase;
1113

14+
/**
15+
* @coversDefaultClass Dominant_Color_Image_Editor_Imagick
16+
*/
1217
class Test_Dominant_Color_Image_Editor_Imagick extends TestCase {
1318

1419
/**
@@ -33,4 +38,164 @@ static function ( $editor ): bool {
3338
}
3439
);
3540
}
41+
42+
/**
43+
* @covers ::get_dominant_color
44+
*/
45+
public function test_failure_handling(): void {
46+
$editor = new Dominant_Color_Image_Editor_Imagick( '/image.jpg' );
47+
$result = $editor->get_dominant_color();
48+
$this->assertWPError( $result );
49+
$this->assertEquals( 'image_editor_dominant_color_error_no_image', $result->get_error_code() );
50+
}
51+
52+
/**
53+
* @covers ::get_dominant_color
54+
*/
55+
public function test_get_dominant_color_no_image(): void {
56+
$editor = new Dominant_Color_Image_Editor_Imagick( null );
57+
$result = $editor->get_dominant_color();
58+
59+
$this->assertWPError( $result );
60+
$this->assertEquals( 'image_editor_dominant_color_error_no_image', $result->get_error_code() );
61+
}
62+
63+
/**
64+
* @covers ::get_dominant_color
65+
*/
66+
public function test_get_dominant_color_exception(): void {
67+
// Creating mock that will throw an exception.
68+
$mock = $this->getMockBuilder( Dominant_Color_Image_Editor_Imagick::class )
69+
->onlyMethods( array( '__construct' ) )
70+
->disableOriginalConstructor()
71+
->getMock();
72+
73+
$reflection = new ReflectionClass( $mock );
74+
$property = $reflection->getProperty( 'image' );
75+
$property->setAccessible( true );
76+
$property->setValue( $mock, new Imagick() );
77+
78+
$result = $mock->get_dominant_color();
79+
80+
$this->assertWPError( $result );
81+
$this->assertEquals( 'image_editor_dominant_color_error', $result->get_error_code() );
82+
}
83+
84+
/**
85+
* @covers ::has_transparency
86+
*/
87+
public function test_has_transparency_no_image(): void {
88+
$editor = new Dominant_Color_Image_Editor_Imagick( null );
89+
$result = $editor->has_transparency();
90+
91+
$this->assertWPError( $result );
92+
$this->assertEquals( 'image_editor_has_transparency_error_no_image', $result->get_error_code() );
93+
}
94+
95+
/**
96+
* @covers ::has_transparency
97+
*/
98+
public function test_has_no_transparency(): void {
99+
$imagick = new Imagick();
100+
$imagick->newImage( 1, 1, new ImagickPixel( 'red' ) );
101+
102+
$editor = new Dominant_Color_Image_Editor_Imagick( null );
103+
$reflection = new ReflectionClass( $editor );
104+
$property = $reflection->getProperty( 'image' );
105+
$property->setAccessible( true );
106+
$property->setValue( $editor, $imagick );
107+
108+
$result = $editor->has_transparency();
109+
110+
$this->assertFalse( $result );
111+
}
112+
113+
/**
114+
* @covers ::has_transparency
115+
*/
116+
public function test_has_transparency_exception(): void {
117+
// Creating mock that will throw an exception.
118+
$mock = $this->getMockBuilder( Dominant_Color_Image_Editor_Imagick::class )
119+
->onlyMethods( array( '__construct' ) )
120+
->disableOriginalConstructor()
121+
->getMock();
122+
123+
$reflection = new ReflectionClass( $mock );
124+
$property = $reflection->getProperty( 'image' );
125+
$property->setAccessible( true );
126+
$property->setValue( $mock, new Imagick() );
127+
128+
$result = $mock->has_transparency();
129+
130+
$this->assertWPError( $result );
131+
$this->assertEquals( 'image_editor_has_transparency_error', $result->get_error_code() );
132+
}
133+
134+
/**
135+
* @covers ::get_dominant_color
136+
*/
137+
public function test_get_dominant_color_success(): void {
138+
// Create a red test image.
139+
$imagick = new Imagick();
140+
$imagick->newImage( 1, 1, new ImagickPixel( '#FF0000' ) );
141+
142+
$editor = new Dominant_Color_Image_Editor_Imagick( null );
143+
$reflection = new ReflectionClass( $editor );
144+
$property = $reflection->getProperty( 'image' );
145+
$property->setAccessible( true );
146+
$property->setValue( $editor, $imagick );
147+
148+
$result = $editor->get_dominant_color();
149+
150+
$this->assertEquals( 'ff0000', $result );
151+
}
152+
153+
/**
154+
* @covers ::has_transparency
155+
*/
156+
public function test_has_transparency_with_transparency(): void {
157+
// Create an image with transparency.
158+
$imagick = new Imagick();
159+
$imagick->newImage( 1, 1, new ImagickPixel( 'rgba(255,0,0,0.5)' ) );
160+
161+
$editor = new Dominant_Color_Image_Editor_Imagick( null );
162+
$reflection = new ReflectionClass( $editor );
163+
$property = $reflection->getProperty( 'image' );
164+
$property->setAccessible( true );
165+
$property->setValue( $editor, $imagick );
166+
167+
$result = $editor->has_transparency();
168+
169+
$this->assertTrue( $result );
170+
}
171+
172+
/**
173+
* @covers ::has_transparency
174+
*/
175+
public function test_has_transparency_no_alpha_channel_method(): void {
176+
// Mock the Imagick object to simulate when getImageAlphaChannel method doesn't exist.
177+
$imagick_mock = $this->getMockBuilder( Imagick::class )
178+
->disableOriginalConstructor()
179+
->getMock();
180+
181+
$imagick_mock->method( 'getImageWidth' )->willReturn( 1 );
182+
$imagick_mock->method( 'getImageHeight' )->willReturn( 1 );
183+
184+
$pixel = $this->getMockBuilder( ImagickPixel::class )
185+
->disableOriginalConstructor()
186+
->getMock();
187+
$pixel->method( 'getColor' )->willReturn( array( 'a' => 0 ) );
188+
189+
$imagick_mock->method( 'getImagePixelColor' )->willReturn( $pixel );
190+
191+
$editor = new Dominant_Color_Image_Editor_Imagick( null );
192+
$reflection = new ReflectionClass( $editor );
193+
$property = $reflection->getProperty( 'image' );
194+
$property->setAccessible( true );
195+
$property->setValue( $editor, $imagick_mock );
196+
197+
$result = $editor->has_transparency();
198+
199+
$this->assertFalse( $result );
200+
}
36201
}

0 commit comments

Comments
 (0)