|
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
|
|
|
// Please refer to https://github.com/Automattic/jetpack/pull/16657 regarding the reasoning behind skipping this tests. |
|
239
|
|
|
$this->markTestSkipped( 'This test needs to be refactored as it requires extending this class with WP_UnitTestCase and have a full WordPress instance running' ); |
|
240
|
|
|
return; |
|
241
|
|
|
// phpcs:disable |
|
242
|
|
|
$attachment_id = $this->create_upload_object( dirname( __FILE__ ) . '/wp-logo.jpg', 0 ); |
|
|
|
|
|
|
243
|
|
|
$content = sprintf( '[gallery ids="%d"]', $attachment_id ); |
|
244
|
|
|
$instance = Jetpack_Lazy_Images::instance(); |
|
245
|
|
|
|
|
246
|
|
|
$instance->setup_filters(); |
|
247
|
|
|
$gallery_output = do_shortcode( $content ); |
|
248
|
|
|
$instance->remove_filters(); |
|
249
|
|
|
|
|
250
|
|
|
$this->assertContains( 'srcset="placeholder.jpg"', $gallery_output ); |
|
251
|
|
|
|
|
252
|
|
|
$instance->setup_filters(); |
|
253
|
|
|
add_filter( 'wp_get_attachment_image_attributes', array( $this, 'add_skip_lazy_class_to_attributes' ) ); |
|
254
|
|
|
$gallery_output = do_shortcode( $content ); |
|
255
|
|
|
remove_filter( 'wp_get_attachment_image_attributes', array( $this, 'add_skip_lazy_class_to_attributes' ) ); |
|
256
|
|
|
$instance->remove_filters(); |
|
257
|
|
|
|
|
258
|
|
|
$this->assertNotContains( 'srcset="placeholder.jpg"', $gallery_output ); |
|
259
|
|
|
// phpcs:enable |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Test the process_image method. |
|
264
|
|
|
* |
|
265
|
|
|
* @param array $image_parts Image parts. |
|
266
|
|
|
* @param string $expected_html Expected HTML. |
|
267
|
|
|
* |
|
268
|
|
|
* @dataProvider get_process_image_test_data |
|
269
|
|
|
*/ |
|
270
|
|
|
public function test_process_image( $image_parts, $expected_html ) { |
|
271
|
|
|
$actual_html = Jetpack_Lazy_Images::process_image( $image_parts ); |
|
272
|
|
|
|
|
273
|
|
|
$this->assertEquals( $expected_html, $actual_html ); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Test the add_image_placeholders method. |
|
278
|
|
|
*/ |
|
279
|
|
|
public function test_add_image_placeholders() { |
|
280
|
|
|
$this->assertSame( $this->get_output_content(), Jetpack_Lazy_Images::instance()->add_image_placeholders( $this->get_input_content() ) ); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Test the process_image_attributes method. |
|
285
|
|
|
* |
|
286
|
|
|
* @param array $input Input attributes. |
|
287
|
|
|
* @param array $expected_output Expected output. |
|
288
|
|
|
* |
|
289
|
|
|
* @dataProvider get_process_image_attributes_data |
|
290
|
|
|
*/ |
|
291
|
|
|
public function test_process_image_attributes( $input, $expected_output ) { |
|
292
|
|
|
$this->assertSame( Jetpack_Lazy_Images::process_image_attributes( $input ), $expected_output ); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Test compatibility with the wp_kses_post function. |
|
297
|
|
|
*/ |
|
298
|
|
|
public function test_compat_with_wp_kses_post() { |
|
299
|
|
|
global $wp_version; |
|
300
|
|
|
if ( version_compare( $wp_version, 5.0, '>=' ) ) { |
|
301
|
|
|
$this->markTestSkipped( 'WP 5.0 allow all data attributes' ); |
|
302
|
|
|
return; |
|
303
|
|
|
} |
|
304
|
|
|
$instance = Jetpack_Lazy_Images::instance(); |
|
305
|
|
|
remove_filter( 'wp_kses_allowed_html', array( $instance, 'allow_lazy_attributes' ) ); |
|
306
|
|
|
|
|
307
|
|
|
$sample_image_srcset = '<img src="placeholder.jpg" data-lazy-src="image.jpg" data-lazy-srcset="medium.jpg 1000w, large.jpg 2000w">'; |
|
308
|
|
|
$sample_img_sizes = '<img src="placeholder.jpg" data-lazy-src="image.jpg" data-lazy-sizes="(min-width: 36em) 33.3vw, 100vw">'; |
|
309
|
|
|
|
|
310
|
|
|
// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
311
|
|
|
$allowed = wp_kses_allowed_html(); |
|
|
|
|
|
|
312
|
|
|
|
|
313
|
|
|
// First, test existence of issue if we don't filter. |
|
314
|
|
|
$no_lazy_srcset = wp_kses_post( $sample_image_srcset ); |
|
315
|
|
|
$no_lazy_sizes = wp_kses_post( $sample_img_sizes ); |
|
316
|
|
|
|
|
317
|
|
|
$this->assertNotContains( 'data-lazy-src', $no_lazy_srcset ); |
|
318
|
|
|
$this->assertNotContains( 'data-lazy-src', $no_lazy_sizes ); |
|
319
|
|
|
$this->assertNotContains( 'data-lazy-srcset', $no_lazy_srcset ); |
|
320
|
|
|
$this->assertNotContains( 'data-lazy-size', $no_lazy_sizes ); |
|
321
|
|
|
|
|
322
|
|
|
add_filter( 'wp_kses_allowed_html', array( $instance, 'allow_lazy_attributes' ) ); |
|
323
|
|
|
|
|
324
|
|
|
// Second, test that the issue is fixed when we filter. |
|
325
|
|
|
$with_lazy_srcset = wp_kses_post( $sample_image_srcset ); |
|
326
|
|
|
$with_lazy_sizes = wp_kses_post( $sample_img_sizes ); |
|
327
|
|
|
|
|
328
|
|
|
$this->assertContains( 'data-lazy-src', $with_lazy_srcset ); |
|
329
|
|
|
$this->assertContains( 'data-lazy-src', $with_lazy_sizes ); |
|
330
|
|
|
$this->assertContains( 'data-lazy-srcset', $with_lazy_srcset ); |
|
331
|
|
|
$this->assertContains( 'data-lazy-size', $with_lazy_sizes ); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* Test that images with classes are not processed. |
|
336
|
|
|
* |
|
337
|
|
|
* @param string $input Input content. |
|
338
|
|
|
* @param bool $should_skip Whether or not it lazy images treatment should be skipped. |
|
339
|
|
|
* |
|
340
|
|
|
* @dataProvider get_dont_process_images_with_classes_data |
|
341
|
|
|
*/ |
|
342
|
|
View Code Duplication |
public function test_dont_process_images_with_classes( $input, $should_skip = true ) { |
|
343
|
|
|
$instance = Jetpack_Lazy_Images::instance(); |
|
344
|
|
|
$output = $instance->add_image_placeholders( $input ); |
|
345
|
|
|
|
|
346
|
|
|
if ( $should_skip ) { |
|
347
|
|
|
$this->assertNotContains( 'srcset="placeholder.jpg"', $output ); |
|
348
|
|
|
} else { |
|
349
|
|
|
$this->assertContains( 'srcset="placeholder.jpg"', $output ); |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* Data provider for test. |
|
355
|
|
|
* |
|
356
|
|
|
* @return array |
|
357
|
|
|
*/ |
|
358
|
|
|
public function get_dont_process_images_with_classes_data() { |
|
359
|
|
|
return array( |
|
360
|
|
|
'skip_lazy' => array( |
|
361
|
|
|
'<img src="image.jpg" srcset="medium.jpg 1000w, large.jpg 2000w" class="skip-lazy"/>', |
|
362
|
|
|
), |
|
363
|
|
|
'gazette_theme_featured_image' => array( |
|
364
|
|
|
'<img src="image.jpg" srcset="medium.jpg 1000w, large.jpg 2000w" class="attachment-gazette-featured-content-thumbnail wp-post-image"/>', |
|
365
|
|
|
), |
|
366
|
|
|
'does_not-skip' => array( |
|
367
|
|
|
'<img src="image.jpg" srcset="medium.jpg 1000w, large.jpg 2000w" class="wp-post-image"/>', |
|
368
|
|
|
false, |
|
369
|
|
|
), |
|
370
|
|
|
); |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
/** |
|
374
|
|
|
* Test that images with the skip lazy data attribute are skipped. |
|
375
|
|
|
* |
|
376
|
|
|
* @param string $input Input content. |
|
377
|
|
|
* @param bool $should_skip Whether or not it lazy images treatment should be skipped. |
|
378
|
|
|
* |
|
379
|
|
|
* @dataProvider get_dont_process_images_with_skip_lazy_data_attribute_data |
|
380
|
|
|
*/ |
|
381
|
|
View Code Duplication |
public function test_dont_process_images_with_skip_lazy_data_attribute( $input, $should_skip = true ) { |
|
382
|
|
|
$instance = Jetpack_Lazy_Images::instance(); |
|
383
|
|
|
$output = $instance->add_image_placeholders( $input ); |
|
384
|
|
|
|
|
385
|
|
|
if ( $should_skip ) { |
|
386
|
|
|
$this->assertNotContains( 'srcset="placeholder.jpg"', $output ); |
|
387
|
|
|
} else { |
|
388
|
|
|
$this->assertContains( 'srcset="placeholder.jpg"', $output ); |
|
389
|
|
|
} |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
/** |
|
393
|
|
|
* Data provider for test. |
|
394
|
|
|
* |
|
395
|
|
|
* @return array |
|
396
|
|
|
*/ |
|
397
|
|
|
public function get_dont_process_images_with_skip_lazy_data_attribute_data() { |
|
398
|
|
|
return array( |
|
399
|
|
|
'skip_lazy_attr_only' => array( |
|
400
|
|
|
'<img src="image.jpg" srcset="medium.jpg 1000w, large.jpg 2000w" data-skip-lazy/>', |
|
401
|
|
|
), |
|
402
|
|
|
'skip-lazy-attr-true' => array( |
|
403
|
|
|
'<img src="image.jpg" srcset="medium.jpg 1000w, large.jpg 2000w" data-skip-lazy="true"/>', |
|
404
|
|
|
), |
|
405
|
|
|
'skip-lazy-attr-1' => array( |
|
406
|
|
|
'<img src="image.jpg" srcset="medium.jpg 1000w, large.jpg 2000w" data-skip-lazy="1"/>', |
|
407
|
|
|
), |
|
408
|
|
|
); |
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
|
|
/** |
|
412
|
|
|
* Test that images with the blocked class should be skipped |
|
413
|
|
|
* |
|
414
|
|
|
* @param bool $expected Expected result. |
|
415
|
|
|
* @param string $input A string of space-separated classes. |
|
416
|
|
|
* @param bool $empty_blocked_classes Empty block classes. |
|
417
|
|
|
* |
|
418
|
|
|
* @dataProvider get_should_skip_image_with_blocked_class_data |
|
419
|
|
|
*/ |
|
420
|
|
|
public function test_should_skip_image_with_blocked_class( $expected, $input, $empty_blocked_classes = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
421
|
|
|
$this->assertSame( $expected, Jetpack_Lazy_Images::should_skip_image_with_blocked_class( $input ) ); |
|
422
|
|
|
} |
|
423
|
|
|
|
|
424
|
|
|
/** |
|
425
|
|
|
* Data provider for test. |
|
426
|
|
|
* |
|
427
|
|
|
* @return array |
|
428
|
|
|
*/ |
|
429
|
|
|
public function get_should_skip_image_with_blocked_class_data() { |
|
430
|
|
|
return array( |
|
431
|
|
|
'wp-post-image' => array( |
|
432
|
|
|
false, |
|
433
|
|
|
'wp-post-image', |
|
434
|
|
|
), |
|
435
|
|
|
'skip-lazy' => array( |
|
436
|
|
|
true, |
|
437
|
|
|
'wp-post-image skip-lazy', |
|
438
|
|
|
), |
|
439
|
|
|
'gazette-feature' => array( |
|
440
|
|
|
true, |
|
441
|
|
|
'wp-post-image attachment-gazette-featured-content-thumbnail', |
|
442
|
|
|
), |
|
443
|
|
|
); |
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
/** |
|
447
|
|
|
* Test that images with filtered empty blocklist should be skipped. |
|
448
|
|
|
* |
|
449
|
|
|
* @param string $classes A string of space-separated classes. TODO: Check type. |
|
450
|
|
|
* |
|
451
|
|
|
* @dataProvider get_should_skip_image_with_filtered_empty_blocked_data |
|
452
|
|
|
*/ |
|
453
|
|
|
public function test_should_skip_image_with_filtered_empty_blocklist( $classes ) { |
|
454
|
|
|
$filter_callbacks = array( |
|
455
|
|
|
'__return_empty_string', |
|
456
|
|
|
'__return_empty_array', |
|
457
|
|
|
); |
|
458
|
|
|
|
|
459
|
|
|
foreach ( $filter_callbacks as $callback ) { |
|
460
|
|
|
add_filter( 'jetpack_lazy_images_blocked_classes', $callback ); |
|
461
|
|
|
$this->assertSame( false, Jetpack_Lazy_Images::should_skip_image_with_blocked_class( $classes ) ); |
|
462
|
|
|
remove_filter( 'jetpack_lazy_images_blocked_classes', $callback ); |
|
463
|
|
|
} |
|
464
|
|
|
} |
|
465
|
|
|
|
|
466
|
|
|
/** |
|
467
|
|
|
* Data provider for test. |
|
468
|
|
|
* |
|
469
|
|
|
* @return array |
|
470
|
|
|
*/ |
|
471
|
|
|
public function get_should_skip_image_with_filtered_empty_blocked_data() { |
|
472
|
|
|
return array( |
|
473
|
|
|
'wp-post-image' => array( |
|
474
|
|
|
'wp-post-image', |
|
475
|
|
|
), |
|
476
|
|
|
'skip-lazy' => array( |
|
477
|
|
|
'wp-post-image skip-lazy', |
|
478
|
|
|
), |
|
479
|
|
|
'gazette-feature' => array( |
|
480
|
|
|
'wp-post-image attachment-gazette-featured-content-thumbnail', |
|
481
|
|
|
), |
|
482
|
|
|
); |
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
/** |
|
486
|
|
|
* Test that Jetpack lazy images skip image with attributes filter. |
|
487
|
|
|
* |
|
488
|
|
|
* @param string $filter_name filter name. |
|
489
|
|
|
* |
|
490
|
|
|
* @dataProvider get_skip_image_with_attributes_data |
|
491
|
|
|
*/ |
|
492
|
|
|
public function test_jetpack_lazy_images_skip_image_with_attributes_filter( $filter_name ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
493
|
|
|
$instance = Jetpack_Lazy_Images::instance(); |
|
494
|
|
|
$src = '<img src="image.jpg" srcset="medium.jpg 1000w, large.jpg 2000w" class="wp-post-image"/>'; |
|
495
|
|
|
|
|
496
|
|
|
$this->assertContains( 'srcset="placeholder.jpg"', $instance->add_image_placeholders( $src ) ); |
|
497
|
|
|
|
|
498
|
|
|
add_filter( 'jetpack_lazy_images_skip_image_with_attributes', '__return_true' ); |
|
499
|
|
|
$this->assertNotContains( 'srcset="placeholder.jpg"', $instance->add_image_placeholders( $src ) ); |
|
500
|
|
|
remove_filter( 'jetpack_lazy_images_skip_image_with_attributes', '__return_true' ); |
|
501
|
|
|
|
|
502
|
|
|
add_filter( 'jetpack_lazy_images_skip_image_with_attributes', array( $this, 'skip_if_srcset' ), 10, 2 ); |
|
503
|
|
|
$this->assertNotContains( 'srcset="placeholder.jpg"', $instance->add_image_placeholders( $src ) ); |
|
504
|
|
|
$this->assertContains( 'srcset="placeholder.jpg"', $instance->add_image_placeholders( '<img src="image.jpg" />' ) ); |
|
505
|
|
|
remove_filter( 'jetpack_lazy_images_skip_image_with_attributes', array( $this, 'skip_if_srcset' ), 10, 2 ); |
|
506
|
|
|
} |
|
507
|
|
|
|
|
508
|
|
|
/** |
|
509
|
|
|
* Data provider for test. |
|
510
|
|
|
* |
|
511
|
|
|
* @return array |
|
512
|
|
|
*/ |
|
513
|
|
|
public function get_skip_image_with_attributes_data() { |
|
514
|
|
|
return array( |
|
515
|
|
|
'deprecated_filter_name_with_typo' => array( |
|
516
|
|
|
'jetpack_lazy_images_skip_image_with_atttributes', |
|
517
|
|
|
), |
|
518
|
|
|
'correct_filter_name' => array( |
|
519
|
|
|
'jetpack_lazy_images_skip_image_with_attributes', |
|
520
|
|
|
), |
|
521
|
|
|
); |
|
522
|
|
|
} |
|
523
|
|
|
|
|
524
|
|
|
/* |
|
525
|
|
|
* Helpers |
|
526
|
|
|
*/ |
|
527
|
|
|
|
|
528
|
|
|
/** |
|
529
|
|
|
* Override image placeholder. |
|
530
|
|
|
* |
|
531
|
|
|
* @return string |
|
532
|
|
|
*/ |
|
533
|
|
|
public function override_image_placeholder() { |
|
534
|
|
|
return 'placeholder.jpg'; |
|
535
|
|
|
} |
|
536
|
|
|
|
|
537
|
|
|
/** |
|
538
|
|
|
* Set height attribute. |
|
539
|
|
|
* |
|
540
|
|
|
* @param array $attributes Attributes. |
|
541
|
|
|
* |
|
542
|
|
|
* @return array |
|
543
|
|
|
*/ |
|
544
|
|
|
public function set_height_attribute( $attributes ) { |
|
545
|
|
|
if ( ! empty( $attributes['height'] ) ) { |
|
546
|
|
|
$attributes['style'] = sprintf( 'height: %dpx;', $attributes['height'] ); |
|
547
|
|
|
unset( $attributes['height'] ); |
|
548
|
|
|
} |
|
549
|
|
|
return $attributes; |
|
550
|
|
|
} |
|
551
|
|
|
|
|
552
|
|
|
/** |
|
553
|
|
|
* Get input content. |
|
554
|
|
|
* |
|
555
|
|
|
* @return string |
|
556
|
|
|
*/ |
|
557
|
|
View Code Duplication |
public function get_input_content() { |
|
558
|
|
|
ob_start(); |
|
559
|
|
|
|
|
560
|
|
|
require_once dirname( __FILE__ ) . '/pre-image-placeholder-content.html'; |
|
561
|
|
|
|
|
562
|
|
|
$contents = trim( ob_get_contents() ); |
|
563
|
|
|
ob_end_clean(); |
|
564
|
|
|
|
|
565
|
|
|
return trim( $contents ); |
|
566
|
|
|
} |
|
567
|
|
|
|
|
568
|
|
|
/** |
|
569
|
|
|
* Get output content. |
|
570
|
|
|
* |
|
571
|
|
|
* @return string |
|
572
|
|
|
*/ |
|
573
|
|
View Code Duplication |
public function get_output_content() { |
|
574
|
|
|
ob_start(); |
|
575
|
|
|
|
|
576
|
|
|
require_once dirname( __FILE__ ) . '/post-image-placeholder-content.html'; |
|
577
|
|
|
|
|
578
|
|
|
$contents = trim( ob_get_contents() ); |
|
579
|
|
|
ob_end_clean(); |
|
580
|
|
|
|
|
581
|
|
|
return trim( $contents ); |
|
582
|
|
|
} |
|
583
|
|
|
|
|
584
|
|
|
/** |
|
585
|
|
|
* Check is the srcset attribute it set. |
|
586
|
|
|
* |
|
587
|
|
|
* @param bool $should_skip Whether or not it lazy images treatment should be skipped. |
|
588
|
|
|
* @param array $attributes Attributes. |
|
589
|
|
|
* |
|
590
|
|
|
* @return bool |
|
591
|
|
|
*/ |
|
592
|
|
|
public function skip_if_srcset( $should_skip, $attributes ) { |
|
593
|
|
|
return isset( $attributes['srcset'] ); |
|
594
|
|
|
} |
|
595
|
|
|
|
|
596
|
|
|
/** |
|
597
|
|
|
* Add skip lazy class to attributes. |
|
598
|
|
|
* |
|
599
|
|
|
* @param array $attributes attributes. |
|
600
|
|
|
* |
|
601
|
|
|
* @return mixed |
|
602
|
|
|
*/ |
|
603
|
|
|
public function add_skip_lazy_class_to_attributes( $attributes ) { |
|
604
|
|
|
$attributes['class'] .= ' skip-lazy'; |
|
605
|
|
|
return $attributes; |
|
606
|
|
|
} |
|
607
|
|
|
} |
|
608
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.