-
Notifications
You must be signed in to change notification settings - Fork 85
/
dominant-color-test.php
374 lines (340 loc) · 12.6 KB
/
dominant-color-test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
use Dominant_Color_Image\Tests\TestCase as DominantColorTestCase;
/**
* Tests for Dominant Color Images module.
*
* @package dominant-color-images
*/
class Dominant_Color_Test extends DominantColorTestCase {
/**
* Tests dominant_color_metadata().
*
* @dataProvider provider_get_dominant_color
*
* @covers ::dominant_color_metadata
*/
public function test_dominant_color_metadata( $image_path, $expected_color, $expected_transparency ) {
$mime_type = wp_check_filetype( $image_path )['type'];
if ( ! wp_image_editor_supports( array( 'mime_type' => $mime_type ) ) ) {
$this->markTestSkipped( "Mime type $mime_type is not supported." );
}
// Non existing attachment.
$dominant_color_metadata = dominant_color_metadata( array(), 1 );
$this->assertEmpty( $dominant_color_metadata );
// Creating attachment.
$attachment_id = self::factory()->attachment->create_upload_object( $image_path );
wp_maybe_generate_attachment_metadata( get_post( $attachment_id ) );
$dominant_color_metadata = dominant_color_metadata( array(), $attachment_id );
$this->assertArrayHasKey( 'dominant_color', $dominant_color_metadata );
$this->assertNotEmpty( $dominant_color_metadata['dominant_color'] );
$this->assertContains( $dominant_color_metadata['dominant_color'], $expected_color );
}
/**
* Tests dominant_color_get_dominant_color().
*
* @dataProvider provider_get_dominant_color
*
* @covers ::dominant_color_get_dominant_color
*/
public function test_dominant_color_get_dominant_color( $image_path, $expected_color, $expected_transparency ) {
$mime_type = wp_check_filetype( $image_path )['type'];
if ( ! wp_image_editor_supports( array( 'mime_type' => $mime_type ) ) ) {
$this->markTestSkipped( "Mime type $mime_type is not supported." );
}
// Creating attachment.
$attachment_id = self::factory()->attachment->create_upload_object( $image_path );
$this->assertContains( dominant_color_get_dominant_color( $attachment_id ), $expected_color );
}
/**
* Tests has_transparency_metadata().
*
* @dataProvider provider_get_dominant_color
*
* @covers ::dominant_color_metadata
*/
public function test_has_transparency_metadata( $image_path, $expected_color, $expected_transparency ) {
$mime_type = wp_check_filetype( $image_path )['type'];
if ( ! wp_image_editor_supports( array( 'mime_type' => $mime_type ) ) ) {
$this->markTestSkipped( "Mime type $mime_type is not supported." );
}
// Non-existing attachment.
$transparency_metadata = dominant_color_metadata( array(), 1 );
$this->assertEmpty( $transparency_metadata );
$attachment_id = self::factory()->attachment->create_upload_object( $image_path );
wp_maybe_generate_attachment_metadata( get_post( $attachment_id ) );
$transparency_metadata = dominant_color_metadata( array(), $attachment_id );
$this->assertArrayHasKey( 'has_transparency', $transparency_metadata );
$this->assertSame( $expected_transparency, $transparency_metadata['has_transparency'] );
}
/**
* Tests dominant_color_get_dominant_color().
*
* @dataProvider provider_get_dominant_color
*
* @covers ::dominant_color_get_dominant_color
*/
public function test_dominant_color_has_transparency( $image_path, $expected_color, $expected_transparency ) {
$mime_type = wp_check_filetype( $image_path )['type'];
if ( ! wp_image_editor_supports( array( 'mime_type' => $mime_type ) ) ) {
$this->markTestSkipped( "Mime type $mime_type is not supported." );
}
// Creating attachment.
$attachment_id = self::factory()->attachment->create_upload_object( $image_path );
$this->assertSame( $expected_transparency, dominant_color_has_transparency( $attachment_id ) );
}
/**
* Tests tag_add_adjust().
*
* @dataProvider provider_get_dominant_color
*
* @covers ::dominant_color_img_tag_add_dominant_color
*/
public function test_tag_add_adjust_to_image_attributes( $image_path, $expected_color, $expected_transparency ) {
$mime_type = wp_check_filetype( $image_path )['type'];
if ( ! wp_image_editor_supports( array( 'mime_type' => $mime_type ) ) ) {
$this->markTestSkipped( "Mime type $mime_type is not supported." );
}
$attachment_id = self::factory()->attachment->create_upload_object( $image_path );
wp_maybe_generate_attachment_metadata( get_post( $attachment_id ) );
list( $src, $width, $height ) = wp_get_attachment_image_src( $attachment_id );
// Testing tag_add_adjust() with image being lazy load.
$filtered_image_mock_lazy_load = sprintf( '<img loading="lazy" class="test" src="%s" width="%d" height="%d" />', $src, $width, $height );
$filtered_image_tags_added = dominant_color_img_tag_add_dominant_color( $filtered_image_mock_lazy_load, 'the_content', $attachment_id );
$this->assertStringContainsString( 'data-has-transparency="' . wp_json_encode( $expected_transparency ) . '"', $filtered_image_tags_added );
foreach ( $expected_color as $color ) {
if ( false !== strpos( $color, $filtered_image_tags_added ) ) {
$this->assertStringContainsString( 'style="--dominant-color: #' . $expected_color . ';"', $filtered_image_tags_added );
$this->assertStringContainsString( 'data-dominant-color="' . $expected_color . '"', $filtered_image_tags_added );
break;
}
}
// Deactivate filter.
add_filter( 'dominant_color_img_tag_add_dominant_color', '__return_false' );
$filtered_image_tags_not_added = dominant_color_img_tag_add_dominant_color( $filtered_image_mock_lazy_load, 'the_content', $attachment_id );
$this->assertEquals( $filtered_image_mock_lazy_load, $filtered_image_tags_not_added );
}
/**
* Tests that only img tags using double quotes are updated.
*
* @dataProvider data_dominant_color_img_tag_add_dominant_color_requires_proper_quotes
*
* @covers ::dominant_color_img_tag_add_dominant_color
*
* @param string $image The image markup.
* Must include %s for the 'src' value.
* @param bool $expected Whether the dominant color should be added.
*/
public function test_dominant_color_img_tag_add_dominant_color_requires_proper_quotes( $image, $expected ) {
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/red.jpg' );
wp_maybe_generate_attachment_metadata( get_post( $attachment_id ) );
$image_url = wp_get_attachment_image_url( $attachment_id );
$image = sprintf( $image, $image_url );
$result = dominant_color_img_tag_add_dominant_color( $image, 'the_content', $attachment_id );
if ( $expected ) {
$this->assertStringContainsString( ' data-dominant-color=', $result );
} else {
$this->assertStringNotContainsString( ' data-dominant-color=', $result );
}
}
/**
* Data provider for test_dominant_color_img_tag_add_dominant_color_requires_proper_quotes();
*
* @return array[]
*/
public function data_dominant_color_img_tag_add_dominant_color_requires_proper_quotes() {
return array(
'double quotes' => array(
'image' => '<img src="%s">',
'expected' => true,
),
'single quotes' => array(
'image' => "<img src='%s'>",
'expected' => false,
),
'escaped double quotes' => array(
'image' => '<img src=\"%s\">',
'expected' => false,
),
);
}
/**
* Tests that dominant_color_img_tag_add_dominant_color() does not replace existing inline styles.
*
* @dataProvider data_provider_dominant_color_check_inline_style
*
* @covers ::dominant_color_img_tag_add_dominant_color
*
* @param string $filtered_image The filtered image markup.
* Must include `src="%s" width="%d" height="%d"`.
* @param string $expected The expected style attribute and value.
*/
public function test_dominant_color_img_tag_add_dominant_color_should_add_dominant_color_inline_style( $filtered_image, $expected ) {
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/red.jpg' );
wp_maybe_generate_attachment_metadata( get_post( $attachment_id ) );
list( $src, $width, $height ) = wp_get_attachment_image_src( $attachment_id );
$filtered_image = sprintf( $filtered_image, $src, $width, $height );
$this->assertStringContainsString(
$expected,
dominant_color_img_tag_add_dominant_color( $filtered_image, 'the_content', $attachment_id )
);
}
/**
* Data provider for test_dominant_color_img_tag_add_dominant_color_should_add_dominant_color_inline_style().
*
* @return array[]
*/
public function data_provider_dominant_color_check_inline_style() {
return array(
'no existing inline styles' => array(
'filtered_image' => '<img src="%s" width="%d" height="%d" />',
'expected' => 'style="--dominant-color: #fe0000;"',
),
'existing inline styles' => array(
'filtered_image' => '<img style="color: #ffffff;" src="%s" width="%d" height="%d" />',
'expected' => 'style="--dominant-color: #fe0000; color: #ffffff;"',
),
);
}
/**
* Tests that the dominant color style always comes before other existing inline styles.
*
* @dataProvider data_provider_dominant_color_filter_check_inline_style
*
* @param string $style_attr The image style attribute.
* @param string $expected The expected style attribute and value.
*/
public function test_dominant_color_update_attachment_image_attributes( $style_attr, $expected ) {
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/red.jpg' );
$attachment_image = wp_get_attachment_image( $attachment_id, 'full', '', array( 'style' => $style_attr ) );
$this->assertStringContainsString( $expected, $attachment_image );
}
/**
* Data provider for test_dominant_color_update_attachment_image_attributes().
*
* @return array[]
*/
public function data_provider_dominant_color_filter_check_inline_style() {
return array(
'no inline styles' => array(
'style_attr' => '',
'expected' => 'style="--dominant-color: #fe0000;"',
),
'inline style with end semicolon' => array(
'style_attr' => 'color: #ffffff;',
'expected' => 'style="--dominant-color: #fe0000;color: #ffffff;"',
),
'inline style without end semicolon' => array(
'style_attr' => 'color: #ffffff',
'expected' => 'style="--dominant-color: #fe0000;color: #ffffff"',
),
);
}
/**
* Tests dominant_color_set_image_editors().
*
* @dataProvider provider_dominant_color_set_image_editors
*
* @covers ::dominant_color_set_image_editors
*/
public function test_dominant_color_set_image_editors( $existing, $expected ) {
$this->assertEqualSets( dominant_color_set_image_editors( $existing ), $expected );
}
public function provider_dominant_color_set_image_editors() {
return array(
'default' => array(
'existing' => array(
'WP_Image_Editor_GD',
'WP_Image_Editor_Imagick',
),
'expected' => array(
'Dominant_Color_Image_Editor_GD',
'Dominant_Color_Image_Editor_Imagick',
),
),
'filtered' => array(
'existing' => array(
'WP_Image_Editor_Filered_GD',
'WP_Image_Editor_Filered_Imagick',
),
'expected' => array(
'WP_Image_Editor_Filered_GD',
'WP_Image_Editor_Filered_Imagick',
),
),
'added' => array(
'existing' => array(
'WP_Image_Editor_Filered_GD',
'WP_Image_Editor_Filered_Imagick',
'WP_Image_Editor_GD',
'WP_Image_Editor_Imagick',
),
'expected' => array(
'WP_Image_Editor_Filered_GD',
'WP_Image_Editor_Filered_Imagick',
'Dominant_Color_Image_Editor_GD',
'Dominant_Color_Image_Editor_Imagick',
),
),
'empty' => array(
'existing' => array(),
'expected' => array(),
),
);
}
/**
* Tests dominant_color_rgb_to_hex().
*
* @dataProvider provider_get_hex_color
*
* @covers ::dominant_color_rgb_to_hex
*/
public function test_dominant_color_rgb_to_hex( $red, $green, $blue, $hex ) {
$this->assertSame( $hex, dominant_color_rgb_to_hex( $red, $green, $blue ) );
}
public function provider_get_hex_color() {
return array(
'black' => array(
'red' => 0,
'green' => 0,
'blue' => 0,
'hex' => '000000',
),
'white' => array(
'red' => 255,
'green' => 255,
'blue' => 255,
'hex' => 'ffffff',
),
'blue' => array(
'red' => 255,
'green' => 0,
'blue' => 0,
'hex' => 'ff0000',
),
'teal' => array(
'red' => 255,
'green' => 255,
'blue' => 0,
'hex' => 'ffff00',
),
'pink' => array(
'red' => 255,
'green' => 0,
'blue' => 255,
'hex' => 'ff00ff',
),
'purple' => array(
'red' => 88,
'green' => 42,
'blue' => 158,
'hex' => '582a9e',
),
'invalid' => array(
'red' => -1,
'green' => -1,
'blue' => -1,
'hex' => null,
),
);
}
}