|
1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
|
2
|
|
|
|
|
3
|
|
|
require dirname( __FILE__ ) . '/../../src/lazy-images.php'; |
|
4
|
|
|
|
|
5
|
|
|
use Automattic\Jetpack\Jetpack_Lazy_Images; |
|
6
|
|
|
use WorDBless\BaseTestCase; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class WP_Test_Lazy_Images |
|
10
|
|
|
*/ |
|
11
|
|
|
class WP_Test_Lazy_Images extends BaseTestCase { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Setup. |
|
15
|
|
|
*/ |
|
16
|
|
|
public function setUp() { |
|
17
|
|
|
parent::setUp(); |
|
18
|
|
|
|
|
19
|
|
|
add_filter( 'lazyload_images_placeholder_image', array( $this, 'override_image_placeholder' ) ); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Data provider for test. |
|
24
|
|
|
* |
|
25
|
|
|
* @return array |
|
26
|
|
|
*/ |
|
27
|
|
|
public function get_process_image_test_data() { |
|
28
|
|
|
return array( |
|
29
|
|
|
'img_with_no_src' => array( |
|
30
|
|
|
array( |
|
31
|
|
|
'<img id="img" />', |
|
32
|
|
|
'img', |
|
33
|
|
|
' id="img"', |
|
34
|
|
|
), |
|
35
|
|
|
'<img id="img" />', |
|
36
|
|
|
), |
|
37
|
|
|
'img_simple' => array( |
|
38
|
|
|
array( |
|
39
|
|
|
'<img src="image.jpg" />', |
|
40
|
|
|
'img', |
|
41
|
|
|
' src="image.jpg"', |
|
42
|
|
|
), |
|
43
|
|
|
'<img src="image.jpg" data-lazy-src="http://image.jpg?is-pending-load=1" srcset="placeholder.jpg" class=" jetpack-lazy-image"><noscript><img src="image.jpg" /></noscript>', |
|
44
|
|
|
), |
|
45
|
|
|
'img_with_other_attributes' => array( |
|
46
|
|
|
array( |
|
47
|
|
|
'<img src="image.jpg" alt="Alt!" />', |
|
48
|
|
|
'img', |
|
49
|
|
|
' src="image.jpg" alt="Alt!"', |
|
50
|
|
|
), |
|
51
|
|
|
'<img src="image.jpg" alt="Alt!" data-lazy-src="http://image.jpg?is-pending-load=1" srcset="placeholder.jpg" class=" jetpack-lazy-image"><noscript><img src="image.jpg" alt="Alt!" /></noscript>', |
|
52
|
|
|
), |
|
53
|
|
|
'img_with_srcset' => array( |
|
54
|
|
|
array( |
|
55
|
|
|
'<img src="image.jpg" srcset="medium.jpg 1000w, large.jpg 2000w" />', |
|
56
|
|
|
'img', |
|
57
|
|
|
' src="image.jpg" srcset="medium.jpg 1000w, large.jpg 2000w"', |
|
58
|
|
|
|
|
59
|
|
|
), |
|
60
|
|
|
'<img src="image.jpg" data-lazy-srcset="medium.jpg 1000w, large.jpg 2000w" data-lazy-src="http://image.jpg?is-pending-load=1" srcset="placeholder.jpg" class=" jetpack-lazy-image"><noscript><img src="image.jpg" srcset="medium.jpg 1000w, large.jpg 2000w" /></noscript>', |
|
61
|
|
|
), |
|
62
|
|
|
'img_with_sizes' => array( |
|
63
|
|
|
array( |
|
64
|
|
|
'<img src="image.jpg" sizes="(min-width: 36em) 33.3vw, 100vw" />', |
|
65
|
|
|
'img', |
|
66
|
|
|
' src="image.jpg" sizes="(min-width: 36em) 33.3vw, 100vw"', |
|
67
|
|
|
|
|
68
|
|
|
), |
|
69
|
|
|
'<img src="image.jpg" data-lazy-sizes="(min-width: 36em) 33.3vw, 100vw" data-lazy-src="http://image.jpg?is-pending-load=1" srcset="placeholder.jpg" class=" jetpack-lazy-image"><noscript><img src="image.jpg" sizes="(min-width: 36em) 33.3vw, 100vw" /></noscript>', |
|
70
|
|
|
), |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Data provider for test. |
|
76
|
|
|
* |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
|
|
public function get_process_image_attributes_data() { |
|
80
|
|
|
return array( |
|
81
|
|
|
'img_with_no_src' => array( |
|
82
|
|
|
array( |
|
83
|
|
|
'width' => 10, |
|
84
|
|
|
'height' => 10, |
|
85
|
|
|
), |
|
86
|
|
|
array( |
|
87
|
|
|
'width' => 10, |
|
88
|
|
|
'height' => 10, |
|
89
|
|
|
), |
|
90
|
|
|
), |
|
91
|
|
|
'img_simple' => array( |
|
92
|
|
|
array( |
|
93
|
|
|
'src' => 'image.jpg', |
|
94
|
|
|
'width' => 10, |
|
95
|
|
|
'height' => 10, |
|
96
|
|
|
), |
|
97
|
|
|
array( |
|
98
|
|
|
'src' => 'image.jpg', |
|
99
|
|
|
'width' => 10, |
|
100
|
|
|
'height' => 10, |
|
101
|
|
|
'data-lazy-src' => 'http://image.jpg?is-pending-load=1', |
|
102
|
|
|
'srcset' => 'placeholder.jpg', |
|
103
|
|
|
'class' => ' jetpack-lazy-image', |
|
104
|
|
|
), |
|
105
|
|
|
), |
|
106
|
|
|
'img_with_srcset' => array( |
|
107
|
|
|
array( |
|
108
|
|
|
'src' => 'image.jpg', |
|
109
|
|
|
'width' => 10, |
|
110
|
|
|
'height' => 10, |
|
111
|
|
|
'srcset' => 'medium.jpg 1000w, large.jpg 2000w', |
|
112
|
|
|
), |
|
113
|
|
|
array( |
|
114
|
|
|
'src' => 'image.jpg', |
|
115
|
|
|
'width' => 10, |
|
116
|
|
|
'height' => 10, |
|
117
|
|
|
'data-lazy-srcset' => 'medium.jpg 1000w, large.jpg 2000w', |
|
118
|
|
|
'data-lazy-src' => 'http://image.jpg?is-pending-load=1', |
|
119
|
|
|
'srcset' => 'placeholder.jpg', |
|
120
|
|
|
'class' => ' jetpack-lazy-image', |
|
121
|
|
|
), |
|
122
|
|
|
), |
|
123
|
|
|
'img_with_sizes' => array( |
|
124
|
|
|
array( |
|
125
|
|
|
'src' => 'image.jpg', |
|
126
|
|
|
'width' => 10, |
|
127
|
|
|
'height' => 10, |
|
128
|
|
|
'sizes' => '(min-width: 36em) 33.3vw, 100vw', |
|
129
|
|
|
), |
|
130
|
|
|
array( |
|
131
|
|
|
'src' => 'image.jpg', |
|
132
|
|
|
'width' => 10, |
|
133
|
|
|
'height' => 10, |
|
134
|
|
|
'data-lazy-sizes' => '(min-width: 36em) 33.3vw, 100vw', |
|
135
|
|
|
'data-lazy-src' => 'http://image.jpg?is-pending-load=1', |
|
136
|
|
|
'srcset' => 'placeholder.jpg', |
|
137
|
|
|
'class' => ' jetpack-lazy-image', |
|
138
|
|
|
), |
|
139
|
|
|
), |
|
140
|
|
|
'gazette_theme_featured_image' => array( |
|
141
|
|
|
array( |
|
142
|
|
|
'src' => 'image.jpg', |
|
143
|
|
|
'width' => 10, |
|
144
|
|
|
'height' => 10, |
|
145
|
|
|
'class' => 'attachment-gazette-featured-content-thumbnail wp-post-image', |
|
146
|
|
|
), |
|
147
|
|
|
// Should be unmodified. |
|
148
|
|
|
array( |
|
149
|
|
|
'src' => 'image.jpg', |
|
150
|
|
|
'width' => 10, |
|
151
|
|
|
'height' => 10, |
|
152
|
|
|
'class' => 'attachment-gazette-featured-content-thumbnail wp-post-image', |
|
153
|
|
|
), |
|
154
|
|
|
), |
|
155
|
|
|
); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Test the process image attribute filter. |
|
160
|
|
|
*/ |
|
161
|
|
|
public function test_process_image_attribute_filter() { |
|
162
|
|
|
add_filter( 'jetpack_lazy_images_new_attributes', array( $this, 'set_height_attribute' ) ); |
|
163
|
|
|
|
|
164
|
|
|
$html = Jetpack_Lazy_Images::process_image( |
|
165
|
|
|
array( |
|
166
|
|
|
'<img src="image.jpg" height="100px" />', |
|
167
|
|
|
'img', |
|
168
|
|
|
' src="image.jpg" height="100px"', |
|
169
|
|
|
) |
|
170
|
|
|
); |
|
171
|
|
|
|
|
172
|
|
|
remove_filter( 'jetpack_lazy_images_new_attributes', array( $this, 'set_height_attribute' ) ); |
|
173
|
|
|
|
|
174
|
|
|
$this->assertContains( 'style="height: 100px;"', $html ); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Create a upload |
|
179
|
|
|
* |
|
180
|
|
|
* @param string $file File path. |
|
181
|
|
|
* @param integer $parent Parent post ID. |
|
182
|
|
|
* @return integer |
|
183
|
|
|
*/ |
|
184
|
|
|
public function create_upload_object( $file, $parent = 0 ) { |
|
185
|
|
|
$contents = file_get_contents( $file ); //phpcs:ignore |
|
186
|
|
|
$upload = wp_upload_bits( basename( $file ), null, $contents ); |
|
187
|
|
|
|
|
188
|
|
|
$type = ''; |
|
189
|
|
|
if ( ! empty( $upload['type'] ) ) { |
|
190
|
|
|
$type = $upload['type']; |
|
191
|
|
|
} else { |
|
192
|
|
|
$mime = wp_check_filetype( $upload['file'], null ); |
|
193
|
|
|
if ( $mime ) { |
|
194
|
|
|
$type = $mime['type']; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
$attachment = array( |
|
199
|
|
|
'post_title' => basename( $upload['file'] ), |
|
200
|
|
|
'post_content' => '', |
|
201
|
|
|
'post_type' => 'attachment', |
|
202
|
|
|
'post_parent' => $parent, |
|
203
|
|
|
'post_mime_type' => $type, |
|
204
|
|
|
'guid' => $upload['url'], |
|
205
|
|
|
); |
|
206
|
|
|
|
|
207
|
|
|
// Save the data. |
|
208
|
|
|
|
|
209
|
|
|
$id = wp_insert_attachment( $attachment, $upload['file'], $parent ); |
|
210
|
|
|
|
|
211
|
|
|
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); |
|
212
|
|
|
|
|
213
|
|
|
return $id; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Test that the wp_get_attachment_image function output gets the lazy treatment. |
|
218
|
|
|
*/ |
|
219
|
|
|
public function test_wp_get_attachment_image_gets_lazy_treatment() { |
|
220
|
|
|
|
|
221
|
|
|
$attachment_id = $this->create_upload_object( dirname( __FILE__ ) . '/wp-logo.jpg', 0 ); |
|
222
|
|
|
add_filter( 'wp_get_attachment_image_attributes', array( 'Automattic\\Jetpack\\Jetpack_Lazy_Images', 'process_image_attributes' ), PHP_INT_MAX ); |
|
223
|
|
|
$image = wp_get_attachment_image( $attachment_id ); |
|
224
|
|
|
remove_filter( 'wp_get_attachment_image_attributes', array( 'Automattic\\Jetpack\\Jetpack_Lazy_Images', 'process_image_attributes' ), PHP_INT_MAX ); |
|
225
|
|
|
|
|
226
|
|
|
$this->assertContains( 'srcset="placeholder.jpg"', $image ); |
|
227
|
|
|
$this->assertContains( |
|
228
|
|
|
sprintf( 'data-lazy-srcset="%s"', wp_get_attachment_image_srcset( $attachment_id, 'thumbnail' ) ), |
|
229
|
|
|
$image |
|
230
|
|
|
); |
|
231
|
|
|
// phpcs:enable |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Test that the wp_get_attachment_image function output does not get the lazy treatment when lazy images feature is skipped. |
|
236
|
|
|
*/ |
|
237
|
|
|
public function test_wp_get_attachment_image_does_not_get_lazy_treatment_when_skip_lazy_added() { |
|
238
|
|
|
$attachment_id = $this->create_upload_object( dirname( __FILE__ ) . '/wp-logo.jpg', 0 ); |
|
239
|
|
|
$instance = Jetpack_Lazy_Images::instance(); |
|
240
|
|
|
|
|
241
|
|
|
$instance->setup_filters(); |
|
242
|
|
|
add_filter( 'wp_get_attachment_image_attributes', array( $this, 'add_skip_lazy_class_to_attributes' ) ); |
|
243
|
|
|
$image = wp_get_attachment_image( $attachment_id ); |
|
244
|
|
|
remove_filter( 'wp_get_attachment_image_attributes', array( $this, 'add_skip_lazy_class_to_attributes' ) ); |
|
245
|
|
|
$instance->remove_filters(); |
|
246
|
|
|
|
|
247
|
|
|
$this->assertNotContains( 'srcset="placeholder.jpg"', $image ); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Test the process_image method. |
|
252
|
|
|
* |
|
253
|
|
|
* @param array $image_parts Image parts. |
|
254
|
|
|
* @param string $expected_html Expected HTML. |
|
255
|
|
|
* |
|
256
|
|
|
* @dataProvider get_process_image_test_data |
|
257
|
|
|
*/ |
|
258
|
|
|
public function test_process_image( $image_parts, $expected_html ) { |
|
259
|
|
|
$actual_html = Jetpack_Lazy_Images::process_image( $image_parts ); |
|
260
|
|
|
|
|
261
|
|
|
$this->assertEquals( $expected_html, $actual_html ); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Test the add_image_placeholders method. |
|
266
|
|
|
*/ |
|
267
|
|
|
public function test_add_image_placeholders() { |
|
268
|
|
|
$this->assertSame( $this->get_output_content(), Jetpack_Lazy_Images::instance()->add_image_placeholders( $this->get_input_content() ) ); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Test the process_image_attributes method. |
|
273
|
|
|
* |
|
274
|
|
|
* @param array $input Input attributes. |
|
275
|
|
|
* @param array $expected_output Expected output. |
|
276
|
|
|
* |
|
277
|
|
|
* @dataProvider get_process_image_attributes_data |
|
278
|
|
|
*/ |
|
279
|
|
|
public function test_process_image_attributes( $input, $expected_output ) { |
|
280
|
|
|
$this->assertSame( Jetpack_Lazy_Images::process_image_attributes( $input ), $expected_output ); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Test compatibility with the wp_kses_post function. |
|
285
|
|
|
*/ |
|
286
|
|
|
public function test_compat_with_wp_kses_post() { |
|
287
|
|
|
global $wp_version; |
|
288
|
|
|
if ( version_compare( $wp_version, 5.0, '>=' ) ) { |
|
289
|
|
|
$this->markTestSkipped( 'WP 5.0 allow all data attributes' ); |
|
290
|
|
|
return; |
|
291
|
|
|
} |
|
292
|
|
|
$instance = Jetpack_Lazy_Images::instance(); |
|
293
|
|
|
remove_filter( 'wp_kses_allowed_html', array( $instance, 'allow_lazy_attributes' ) ); |
|
294
|
|
|
|
|
295
|
|
|
$sample_image_srcset = '<img src="placeholder.jpg" data-lazy-src="image.jpg" data-lazy-srcset="medium.jpg 1000w, large.jpg 2000w">'; |
|
296
|
|
|
$sample_img_sizes = '<img src="placeholder.jpg" data-lazy-src="image.jpg" data-lazy-sizes="(min-width: 36em) 33.3vw, 100vw">'; |
|
297
|
|
|
|
|
298
|
|
|
// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
299
|
|
|
$allowed = wp_kses_allowed_html(); |
|
|
|
|
|
|
300
|
|
|
|
|
301
|
|
|
// First, test existence of issue if we don't filter. |
|
302
|
|
|
$no_lazy_srcset = wp_kses_post( $sample_image_srcset ); |
|
303
|
|
|
$no_lazy_sizes = wp_kses_post( $sample_img_sizes ); |
|
304
|
|
|
|
|
305
|
|
|
$this->assertNotContains( 'data-lazy-src', $no_lazy_srcset ); |
|
306
|
|
|
$this->assertNotContains( 'data-lazy-src', $no_lazy_sizes ); |
|
307
|
|
|
$this->assertNotContains( 'data-lazy-srcset', $no_lazy_srcset ); |
|
308
|
|
|
$this->assertNotContains( 'data-lazy-size', $no_lazy_sizes ); |
|
309
|
|
|
|
|
310
|
|
|
add_filter( 'wp_kses_allowed_html', array( $instance, 'allow_lazy_attributes' ) ); |
|
311
|
|
|
|
|
312
|
|
|
// Second, test that the issue is fixed when we filter. |
|
313
|
|
|
$with_lazy_srcset = wp_kses_post( $sample_image_srcset ); |
|
314
|
|
|
$with_lazy_sizes = wp_kses_post( $sample_img_sizes ); |
|
315
|
|
|
|
|
316
|
|
|
$this->assertContains( 'data-lazy-src', $with_lazy_srcset ); |
|
317
|
|
|
$this->assertContains( 'data-lazy-src', $with_lazy_sizes ); |
|
318
|
|
|
$this->assertContains( 'data-lazy-srcset', $with_lazy_srcset ); |
|
319
|
|
|
$this->assertContains( 'data-lazy-size', $with_lazy_sizes ); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* Test that images with classes are not processed. |
|
324
|
|
|
* |
|
325
|
|
|
* @param string $input Input content. |
|
326
|
|
|
* @param bool $should_skip Whether or not it lazy images treatment should be skipped. |
|
327
|
|
|
* |
|
328
|
|
|
* @dataProvider get_dont_process_images_with_classes_data |
|
329
|
|
|
*/ |
|
330
|
|
View Code Duplication |
public function test_dont_process_images_with_classes( $input, $should_skip = true ) { |
|
331
|
|
|
$instance = Jetpack_Lazy_Images::instance(); |
|
332
|
|
|
$output = $instance->add_image_placeholders( $input ); |
|
333
|
|
|
|
|
334
|
|
|
if ( $should_skip ) { |
|
335
|
|
|
$this->assertNotContains( 'srcset="placeholder.jpg"', $output ); |
|
336
|
|
|
} else { |
|
337
|
|
|
$this->assertContains( 'srcset="placeholder.jpg"', $output ); |
|
338
|
|
|
} |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* Data provider for test. |
|
343
|
|
|
* |
|
344
|
|
|
* @return array |
|
345
|
|
|
*/ |
|
346
|
|
|
public function get_dont_process_images_with_classes_data() { |
|
347
|
|
|
return array( |
|
348
|
|
|
'skip_lazy' => array( |
|
349
|
|
|
'<img src="image.jpg" srcset="medium.jpg 1000w, large.jpg 2000w" class="skip-lazy"/>', |
|
350
|
|
|
), |
|
351
|
|
|
'gazette_theme_featured_image' => array( |
|
352
|
|
|
'<img src="image.jpg" srcset="medium.jpg 1000w, large.jpg 2000w" class="attachment-gazette-featured-content-thumbnail wp-post-image"/>', |
|
353
|
|
|
), |
|
354
|
|
|
'does_not-skip' => array( |
|
355
|
|
|
'<img src="image.jpg" srcset="medium.jpg 1000w, large.jpg 2000w" class="wp-post-image"/>', |
|
356
|
|
|
false, |
|
357
|
|
|
), |
|
358
|
|
|
); |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
/** |
|
362
|
|
|
* Test that images with the skip lazy data attribute are skipped. |
|
363
|
|
|
* |
|
364
|
|
|
* @param string $input Input content. |
|
365
|
|
|
* @param bool $should_skip Whether or not it lazy images treatment should be skipped. |
|
366
|
|
|
* |
|
367
|
|
|
* @dataProvider get_dont_process_images_with_skip_lazy_data_attribute_data |
|
368
|
|
|
*/ |
|
369
|
|
View Code Duplication |
public function test_dont_process_images_with_skip_lazy_data_attribute( $input, $should_skip = true ) { |
|
370
|
|
|
$instance = Jetpack_Lazy_Images::instance(); |
|
371
|
|
|
$output = $instance->add_image_placeholders( $input ); |
|
372
|
|
|
|
|
373
|
|
|
if ( $should_skip ) { |
|
374
|
|
|
$this->assertNotContains( 'srcset="placeholder.jpg"', $output ); |
|
375
|
|
|
} else { |
|
376
|
|
|
$this->assertContains( 'srcset="placeholder.jpg"', $output ); |
|
377
|
|
|
} |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
/** |
|
381
|
|
|
* Data provider for test. |
|
382
|
|
|
* |
|
383
|
|
|
* @return array |
|
384
|
|
|
*/ |
|
385
|
|
|
public function get_dont_process_images_with_skip_lazy_data_attribute_data() { |
|
386
|
|
|
return array( |
|
387
|
|
|
'skip_lazy_attr_only' => array( |
|
388
|
|
|
'<img src="image.jpg" srcset="medium.jpg 1000w, large.jpg 2000w" data-skip-lazy/>', |
|
389
|
|
|
), |
|
390
|
|
|
'skip-lazy-attr-true' => array( |
|
391
|
|
|
'<img src="image.jpg" srcset="medium.jpg 1000w, large.jpg 2000w" data-skip-lazy="true"/>', |
|
392
|
|
|
), |
|
393
|
|
|
'skip-lazy-attr-1' => array( |
|
394
|
|
|
'<img src="image.jpg" srcset="medium.jpg 1000w, large.jpg 2000w" data-skip-lazy="1"/>', |
|
395
|
|
|
), |
|
396
|
|
|
); |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
/** |
|
400
|
|
|
* Test that images with the blocked class should be skipped |
|
401
|
|
|
* |
|
402
|
|
|
* @param bool $expected Expected result. |
|
403
|
|
|
* @param string $input A string of space-separated classes. |
|
404
|
|
|
* @param bool $empty_blocked_classes Empty block classes. |
|
405
|
|
|
* |
|
406
|
|
|
* @dataProvider get_should_skip_image_with_blocked_class_data |
|
407
|
|
|
*/ |
|
408
|
|
|
public function test_should_skip_image_with_blocked_class( $expected, $input, $empty_blocked_classes = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
409
|
|
|
$this->assertSame( $expected, Jetpack_Lazy_Images::should_skip_image_with_blocked_class( $input ) ); |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
/** |
|
413
|
|
|
* Data provider for test. |
|
414
|
|
|
* |
|
415
|
|
|
* @return array |
|
416
|
|
|
*/ |
|
417
|
|
|
public function get_should_skip_image_with_blocked_class_data() { |
|
418
|
|
|
return array( |
|
419
|
|
|
'wp-post-image' => array( |
|
420
|
|
|
false, |
|
421
|
|
|
'wp-post-image', |
|
422
|
|
|
), |
|
423
|
|
|
'skip-lazy' => array( |
|
424
|
|
|
true, |
|
425
|
|
|
'wp-post-image skip-lazy', |
|
426
|
|
|
), |
|
427
|
|
|
'gazette-feature' => array( |
|
428
|
|
|
true, |
|
429
|
|
|
'wp-post-image attachment-gazette-featured-content-thumbnail', |
|
430
|
|
|
), |
|
431
|
|
|
); |
|
432
|
|
|
} |
|
433
|
|
|
|
|
434
|
|
|
/** |
|
435
|
|
|
* Test that images with filtered empty blocklist should be skipped. |
|
436
|
|
|
* |
|
437
|
|
|
* @param string $classes A string of space-separated classes. TODO: Check type. |
|
438
|
|
|
* |
|
439
|
|
|
* @dataProvider get_should_skip_image_with_filtered_empty_blocked_data |
|
440
|
|
|
*/ |
|
441
|
|
|
public function test_should_skip_image_with_filtered_empty_blocklist( $classes ) { |
|
442
|
|
|
$filter_callbacks = array( |
|
443
|
|
|
'__return_empty_string', |
|
444
|
|
|
'__return_empty_array', |
|
445
|
|
|
); |
|
446
|
|
|
|
|
447
|
|
|
foreach ( $filter_callbacks as $callback ) { |
|
448
|
|
|
add_filter( 'jetpack_lazy_images_blocked_classes', $callback ); |
|
449
|
|
|
$this->assertSame( false, Jetpack_Lazy_Images::should_skip_image_with_blocked_class( $classes ) ); |
|
450
|
|
|
remove_filter( 'jetpack_lazy_images_blocked_classes', $callback ); |
|
451
|
|
|
} |
|
452
|
|
|
} |
|
453
|
|
|
|
|
454
|
|
|
/** |
|
455
|
|
|
* Data provider for test. |
|
456
|
|
|
* |
|
457
|
|
|
* @return array |
|
458
|
|
|
*/ |
|
459
|
|
|
public function get_should_skip_image_with_filtered_empty_blocked_data() { |
|
460
|
|
|
return array( |
|
461
|
|
|
'wp-post-image' => array( |
|
462
|
|
|
'wp-post-image', |
|
463
|
|
|
), |
|
464
|
|
|
'skip-lazy' => array( |
|
465
|
|
|
'wp-post-image skip-lazy', |
|
466
|
|
|
), |
|
467
|
|
|
'gazette-feature' => array( |
|
468
|
|
|
'wp-post-image attachment-gazette-featured-content-thumbnail', |
|
469
|
|
|
), |
|
470
|
|
|
); |
|
471
|
|
|
} |
|
472
|
|
|
|
|
473
|
|
|
/** |
|
474
|
|
|
* Test that Jetpack lazy images skip image with attributes filter. |
|
475
|
|
|
* |
|
476
|
|
|
* @param string $filter_name filter name. |
|
477
|
|
|
* |
|
478
|
|
|
* @dataProvider get_skip_image_with_attributes_data |
|
479
|
|
|
*/ |
|
480
|
|
|
public function test_jetpack_lazy_images_skip_image_with_attributes_filter( $filter_name ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
481
|
|
|
$instance = Jetpack_Lazy_Images::instance(); |
|
482
|
|
|
$src = '<img src="image.jpg" srcset="medium.jpg 1000w, large.jpg 2000w" class="wp-post-image"/>'; |
|
483
|
|
|
|
|
484
|
|
|
$this->assertContains( 'srcset="placeholder.jpg"', $instance->add_image_placeholders( $src ) ); |
|
485
|
|
|
|
|
486
|
|
|
add_filter( 'jetpack_lazy_images_skip_image_with_attributes', '__return_true' ); |
|
487
|
|
|
$this->assertNotContains( 'srcset="placeholder.jpg"', $instance->add_image_placeholders( $src ) ); |
|
488
|
|
|
remove_filter( 'jetpack_lazy_images_skip_image_with_attributes', '__return_true' ); |
|
489
|
|
|
|
|
490
|
|
|
add_filter( 'jetpack_lazy_images_skip_image_with_attributes', array( $this, 'skip_if_srcset' ), 10, 2 ); |
|
491
|
|
|
$this->assertNotContains( 'srcset="placeholder.jpg"', $instance->add_image_placeholders( $src ) ); |
|
492
|
|
|
$this->assertContains( 'srcset="placeholder.jpg"', $instance->add_image_placeholders( '<img src="image.jpg" />' ) ); |
|
493
|
|
|
remove_filter( 'jetpack_lazy_images_skip_image_with_attributes', array( $this, 'skip_if_srcset' ), 10, 2 ); |
|
494
|
|
|
} |
|
495
|
|
|
|
|
496
|
|
|
/** |
|
497
|
|
|
* Data provider for test. |
|
498
|
|
|
* |
|
499
|
|
|
* @return array |
|
500
|
|
|
*/ |
|
501
|
|
|
public function get_skip_image_with_attributes_data() { |
|
502
|
|
|
return array( |
|
503
|
|
|
'deprecated_filter_name_with_typo' => array( |
|
504
|
|
|
'jetpack_lazy_images_skip_image_with_atttributes', |
|
505
|
|
|
), |
|
506
|
|
|
'correct_filter_name' => array( |
|
507
|
|
|
'jetpack_lazy_images_skip_image_with_attributes', |
|
508
|
|
|
), |
|
509
|
|
|
); |
|
510
|
|
|
} |
|
511
|
|
|
|
|
512
|
|
|
/* |
|
513
|
|
|
* Helpers |
|
514
|
|
|
*/ |
|
515
|
|
|
|
|
516
|
|
|
/** |
|
517
|
|
|
* Override image placeholder. |
|
518
|
|
|
* |
|
519
|
|
|
* @return string |
|
520
|
|
|
*/ |
|
521
|
|
|
public function override_image_placeholder() { |
|
522
|
|
|
return 'placeholder.jpg'; |
|
523
|
|
|
} |
|
524
|
|
|
|
|
525
|
|
|
/** |
|
526
|
|
|
* Set height attribute. |
|
527
|
|
|
* |
|
528
|
|
|
* @param array $attributes Attributes. |
|
529
|
|
|
* |
|
530
|
|
|
* @return array |
|
531
|
|
|
*/ |
|
532
|
|
|
public function set_height_attribute( $attributes ) { |
|
533
|
|
|
if ( ! empty( $attributes['height'] ) ) { |
|
534
|
|
|
$attributes['style'] = sprintf( 'height: %dpx;', $attributes['height'] ); |
|
535
|
|
|
unset( $attributes['height'] ); |
|
536
|
|
|
} |
|
537
|
|
|
return $attributes; |
|
538
|
|
|
} |
|
539
|
|
|
|
|
540
|
|
|
/** |
|
541
|
|
|
* Get input content. |
|
542
|
|
|
* |
|
543
|
|
|
* @return string |
|
544
|
|
|
*/ |
|
545
|
|
View Code Duplication |
public function get_input_content() { |
|
546
|
|
|
ob_start(); |
|
547
|
|
|
|
|
548
|
|
|
require_once dirname( __FILE__ ) . '/pre-image-placeholder-content.html'; |
|
549
|
|
|
|
|
550
|
|
|
$contents = trim( ob_get_contents() ); |
|
551
|
|
|
ob_end_clean(); |
|
552
|
|
|
|
|
553
|
|
|
return trim( $contents ); |
|
554
|
|
|
} |
|
555
|
|
|
|
|
556
|
|
|
/** |
|
557
|
|
|
* Get output content. |
|
558
|
|
|
* |
|
559
|
|
|
* @return string |
|
560
|
|
|
*/ |
|
561
|
|
View Code Duplication |
public function get_output_content() { |
|
562
|
|
|
ob_start(); |
|
563
|
|
|
|
|
564
|
|
|
require_once dirname( __FILE__ ) . '/post-image-placeholder-content.html'; |
|
565
|
|
|
|
|
566
|
|
|
$contents = trim( ob_get_contents() ); |
|
567
|
|
|
ob_end_clean(); |
|
568
|
|
|
|
|
569
|
|
|
return trim( $contents ); |
|
570
|
|
|
} |
|
571
|
|
|
|
|
572
|
|
|
/** |
|
573
|
|
|
* Check is the srcset attribute it set. |
|
574
|
|
|
* |
|
575
|
|
|
* @param bool $should_skip Whether or not it lazy images treatment should be skipped. |
|
576
|
|
|
* @param array $attributes Attributes. |
|
577
|
|
|
* |
|
578
|
|
|
* @return bool |
|
579
|
|
|
*/ |
|
580
|
|
|
public function skip_if_srcset( $should_skip, $attributes ) { |
|
581
|
|
|
return isset( $attributes['srcset'] ); |
|
582
|
|
|
} |
|
583
|
|
|
|
|
584
|
|
|
/** |
|
585
|
|
|
* Add skip lazy class to attributes. |
|
586
|
|
|
* |
|
587
|
|
|
* @param array $attributes attributes. |
|
588
|
|
|
* |
|
589
|
|
|
* @return mixed |
|
590
|
|
|
*/ |
|
591
|
|
|
public function add_skip_lazy_class_to_attributes( $attributes ) { |
|
592
|
|
|
$attributes['class'] .= ' skip-lazy'; |
|
593
|
|
|
return $attributes; |
|
594
|
|
|
} |
|
595
|
|
|
} |
|
596
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.