|
1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
|
2
|
|
|
/** |
|
3
|
|
|
* Test methods from Automattic\Jetpack\Blocks |
|
4
|
|
|
* |
|
5
|
|
|
* @since 9.0.0 |
|
6
|
|
|
* |
|
7
|
|
|
* @package automattic/jetpack-blocks |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Automattic\Jetpack; |
|
11
|
|
|
|
|
12
|
|
|
use Brain\Monkey; |
|
13
|
|
|
use Brain\Monkey\Functions; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Test_Blocks |
|
18
|
|
|
*/ |
|
19
|
|
|
class Test_Blocks extends TestCase { |
|
20
|
|
|
use \Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Test block name. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
public $block_name = 'jetpack/apple'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Setup runs before each test. |
|
31
|
|
|
* |
|
32
|
|
|
* @before |
|
33
|
|
|
*/ |
|
34
|
|
|
public function set_up() { |
|
35
|
|
|
Monkey\setUp(); |
|
36
|
|
|
// Register a test block. |
|
37
|
|
|
Blocks::jetpack_register_block( $this->block_name ); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Teardown runs after each test. |
|
42
|
|
|
* |
|
43
|
|
|
* @after |
|
44
|
|
|
*/ |
|
45
|
|
|
public function tear_down() { |
|
46
|
|
|
Monkey\tearDown(); |
|
47
|
|
|
// Unregister the test Jetpack block we may have created for our tests. |
|
48
|
|
|
unregister_block_type( $this->block_name ); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Test the different inputs and matching output for Classes. |
|
53
|
|
|
* |
|
54
|
|
|
* @since 9.0.0 |
|
55
|
|
|
* |
|
56
|
|
|
* @covers Automattic\Jetpack\Blocks::classes |
|
57
|
|
|
*/ |
|
58
|
|
|
public function test_block_classes() { |
|
59
|
|
|
$block_name = 'foo'; |
|
60
|
|
|
$attr = array( |
|
61
|
|
|
'bar' => 'baz', |
|
62
|
|
|
'align' => 'wide', |
|
63
|
|
|
'className' => 'editorclass', |
|
64
|
|
|
); |
|
65
|
|
|
$extra = array( 'extraclass' ); |
|
66
|
|
|
|
|
67
|
|
|
$block_classes = Blocks::classes( $block_name, $attr, $extra ); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertStringContainsString( 'wp-block-jetpack-foo', $block_classes ); // a general class is created from the block name. |
|
70
|
|
|
$this->assertStringNotContainsString( 'bar', $block_classes ); // The extra 'bar' attribute should be dropped. |
|
71
|
|
|
$this->assertStringNotContainsString( 'baz', $block_classes ); // The extra 'baz' attribute should be dropped. |
|
72
|
|
|
$this->assertStringNotContainsString( 'align ', $block_classes ); // The align attribute should only be used to create a new attribute. |
|
73
|
|
|
$this->assertStringNotContainsString( 'className', $block_classes ); // The className attribute should be dropped, only the editorclass value should remain. |
|
74
|
|
|
$this->assertStringContainsString( 'alignwide', $block_classes ); // an alignment class is created. |
|
75
|
|
|
$this->assertStringContainsString( 'editorclass', $block_classes ); // className classes are passed. |
|
76
|
|
|
$this->assertStringContainsString( 'extraclass', $block_classes ); // Extra class remains. |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Test for invalid alignment values. |
|
81
|
|
|
* |
|
82
|
|
|
* @since 9.0.0 |
|
83
|
|
|
* |
|
84
|
|
|
* @covers Automattic\Jetpack\Blocks::classes |
|
85
|
|
|
*/ |
|
86
|
|
|
public function test_block_classes_invalid_align() { |
|
87
|
|
|
$attr = array( 'align' => 'test' ); |
|
88
|
|
|
$block_classes = Blocks::classes( 'test', $attr ); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertStringNotContainsString( 'aligntest', $block_classes ); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Test whether we can detect an AMP view. |
|
95
|
|
|
* |
|
96
|
|
|
* @since 9.0.0 |
|
97
|
|
|
* |
|
98
|
|
|
* @covers Automattic\Jetpack\Blocks::is_amp_request |
|
99
|
|
|
*/ |
|
100
|
|
|
public function test_is_amp_request() { |
|
101
|
|
|
add_filter( 'jetpack_is_amp_request', '__return_true' ); |
|
102
|
|
|
try { |
|
103
|
|
|
$this->assertTrue( Blocks::is_amp_request() ); |
|
104
|
|
|
} finally { |
|
105
|
|
|
remove_filter( 'jetpack_is_amp_request', '__return_true' ); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Test whether we can detect an AMP view. |
|
111
|
|
|
* |
|
112
|
|
|
* @since 9.0.0 |
|
113
|
|
|
* |
|
114
|
|
|
* @covers Automattic\Jetpack\Blocks::is_amp_request |
|
115
|
|
|
*/ |
|
116
|
|
|
public function test_is_not_amp_request() { |
|
117
|
|
|
$this->assertFalse( Blocks::is_amp_request() ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Test WordPress and Gutenberg version requirements. |
|
122
|
|
|
* |
|
123
|
|
|
* @covers Automattic\Jetpack\Blocks::is_gutenberg_version_available |
|
124
|
|
|
*/ |
|
125
|
|
|
public function test_returns_false_if_core_wp_version_less_than_minimum() { |
|
126
|
|
|
$version_gated = Blocks::is_gutenberg_version_available( |
|
127
|
|
|
array( |
|
128
|
|
|
'wp' => '999999', |
|
129
|
|
|
'gutenberg' => '999999', |
|
130
|
|
|
), |
|
131
|
|
|
'gated_block' |
|
132
|
|
|
); |
|
133
|
|
|
$this->assertFalse( false, $version_gated ); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Test WordPress and Gutenberg version requirements. |
|
138
|
|
|
* |
|
139
|
|
|
* @covers Automattic\Jetpack\Blocks::is_gutenberg_version_available |
|
140
|
|
|
*/ |
|
141
|
|
|
public function test_returns_true_if_core_wp_version_greater_or_equal_to_minimum() { |
|
142
|
|
|
$version_gated = Blocks::is_gutenberg_version_available( |
|
143
|
|
|
array( |
|
144
|
|
|
'wp' => '1', |
|
145
|
|
|
'gutenberg' => '999999', |
|
146
|
|
|
), |
|
147
|
|
|
'ungated_block' |
|
148
|
|
|
); |
|
149
|
|
|
$this->assertTrue( true, $version_gated ); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Testing removing the Jetpack prefix from a block slug. |
|
154
|
|
|
* |
|
155
|
|
|
* @covers Automattic\Jetpack\Blocks::remove_extension_prefix |
|
156
|
|
|
* |
|
157
|
|
|
* @dataProvider get_extension_name_provider |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $extension_slug Block / Extension name. |
|
160
|
|
|
* @param string $expected_short_slug Extension name without Jetpack prefix. |
|
161
|
|
|
*/ |
|
162
|
|
|
public function test_remove_extension_prefix( $extension_slug, $expected_short_slug ) { |
|
163
|
|
|
$short_slug = Blocks::remove_extension_prefix( $extension_slug ); |
|
164
|
|
|
|
|
165
|
|
|
$this->assertEquals( $expected_short_slug, $short_slug ); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Get different possible block names. |
|
170
|
|
|
* |
|
171
|
|
|
* Data provider for test_remove_extension_prefix. |
|
172
|
|
|
*/ |
|
173
|
|
|
public function get_extension_name_provider() { |
|
174
|
|
|
return array( |
|
175
|
|
|
'not_jetpack' => array( |
|
176
|
|
|
'woocommerce/product-best-sellers', |
|
177
|
|
|
'woocommerce/product-best-sellers', |
|
178
|
|
|
), |
|
179
|
|
|
'jetpack_dash' => array( |
|
180
|
|
|
'jetpack/shortlinks', |
|
181
|
|
|
'shortlinks', |
|
182
|
|
|
), |
|
183
|
|
|
'jetpack_hyphen' => array( |
|
184
|
|
|
'jetpack-shortlinks', |
|
185
|
|
|
'shortlinks', |
|
186
|
|
|
), |
|
187
|
|
|
); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Test to ensure that an extension is returned as registered. |
|
192
|
|
|
* |
|
193
|
|
|
* @covers Automattic\Jetpack\Blocks::is_registered |
|
194
|
|
|
*/ |
|
195
|
|
|
public function test_is_extension_registered() { |
|
196
|
|
|
// Test for the block that is registered for all tests here. |
|
197
|
|
|
$this->assertTrue( Blocks::is_registered( $this->block_name ) ); |
|
198
|
|
|
// Test for a non-existing block. |
|
199
|
|
|
$this->assertFalse( Blocks::is_registered( 'foo/bar' ) ); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Ensure blocks cannot be registered twice. |
|
204
|
|
|
* |
|
205
|
|
|
* @covers Automattic\Jetpack\Blocks::jetpack_register_block |
|
206
|
|
|
*/ |
|
207
|
|
|
public function test_jetpack_register_block_twice() { |
|
208
|
|
|
$result = Blocks::jetpack_register_block( $this->block_name ); |
|
209
|
|
|
$this->assertFalse( $result ); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Test to ensure blocks without a Jetpack prefix are registered, but with a jetpack prefix. |
|
214
|
|
|
* |
|
215
|
|
|
* @expectedIncorrectUsage Automattic\Jetpack\Blocks::jetpack_register_block |
|
216
|
|
|
* @covers Automattic\Jetpack\Blocks::jetpack_register_block |
|
217
|
|
|
*/ |
|
218
|
|
|
public function test_jetpack_register_block_without_jetpack() { |
|
219
|
|
|
$result = Blocks::jetpack_register_block( 'doing-it-wrong' ); |
|
220
|
|
|
$this->assertEquals( 'jetpack/doing-it-wrong', $result->name ); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Test that we can detect an FSE theme. |
|
225
|
|
|
* |
|
226
|
|
|
* @since 9.8.0 |
|
227
|
|
|
* |
|
228
|
|
|
* @covers Automattic\Jetpack\Blocks::is_standalone_block |
|
229
|
|
|
*/ |
|
230
|
|
|
public function test_is_not_fse_theme() { |
|
231
|
|
|
$this->assertFalse( Blocks::is_fse_theme() ); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Test that we can detect an FSE theme using the provided filter. |
|
236
|
|
|
* |
|
237
|
|
|
* @since 9.8.0 |
|
238
|
|
|
* |
|
239
|
|
|
* @covers Automattic\Jetpack\Blocks::is_standalone_block |
|
240
|
|
|
*/ |
|
241
|
|
|
public function test_is_fse_theme_via_filter() { |
|
242
|
|
|
add_filter( 'jetpack_is_fse_theme', '__return_true' ); |
|
243
|
|
|
try { |
|
244
|
|
|
$this->assertTrue( Blocks::is_fse_theme() ); |
|
245
|
|
|
} finally { |
|
246
|
|
|
remove_filter( 'jetpack_is_fse_theme', '__return_true' ); |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Test that we can detect an FSE theme using the provided core function, gutenberg_is_fse_theme. |
|
252
|
|
|
* |
|
253
|
|
|
* @since 9.8.0 |
|
254
|
|
|
* |
|
255
|
|
|
* @covers Automattic\Jetpack\Blocks::is_standalone_block |
|
256
|
|
|
*/ |
|
257
|
|
|
public function test_is_fse_theme_via_core_function() { |
|
258
|
|
|
Functions\when( 'gutenberg_is_fse_theme' )->justReturn( true ); |
|
259
|
|
|
|
|
260
|
|
|
$this->assertTrue( Blocks::is_fse_theme() ); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Test that by default we are not running in a Jetpack plugin context. |
|
265
|
|
|
* |
|
266
|
|
|
* @since 9.6.0 |
|
267
|
|
|
* |
|
268
|
|
|
* @covers Automattic\Jetpack\Blocks::is_standalone_block |
|
269
|
|
|
*/ |
|
270
|
|
|
public function test_is_standalone_block() { |
|
271
|
|
|
$this->assertTrue( Blocks::is_standalone_block() ); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Test that we are running in a Jetpack plugin context, and not |
|
276
|
|
|
* as a standalone block. |
|
277
|
|
|
* |
|
278
|
|
|
* @since 9.6.0 |
|
279
|
|
|
* |
|
280
|
|
|
* @covers Automattic\Jetpack\Blocks::is_standalone_block |
|
281
|
|
|
*/ |
|
282
|
|
|
public function test_is_not_standalone_block() { |
|
283
|
|
|
add_filter( 'jetpack_is_standalone_block', '__return_false' ); |
|
284
|
|
|
try { |
|
285
|
|
|
$this->assertFalse( Blocks::is_standalone_block() ); |
|
286
|
|
|
} finally { |
|
287
|
|
|
remove_filter( 'jetpack_is_standalone_block', '__return_false' ); |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* Test to ensure registering a Jetpack block does not add in an editor style dependency, |
|
293
|
|
|
* when the Jetpack_Gutenberg class is not available. |
|
294
|
|
|
* |
|
295
|
|
|
* @since 9.6.0 |
|
296
|
|
|
* |
|
297
|
|
|
* @covers Automattic\Jetpack\Blocks::jetpack_register_block |
|
298
|
|
|
*/ |
|
299
|
|
|
public function test_jetpack_register_block_without_editor_style() { |
|
300
|
|
|
$result = Blocks::jetpack_register_block( 'jetpack/block-without-editor-style' ); |
|
301
|
|
|
$this->assertEquals( 'jetpack/block-without-editor-style', $result->name ); |
|
302
|
|
|
$this->assertNull( $result->editor_style ); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Test to ensure registering a Jetpack block adds in an editor style dependency, |
|
307
|
|
|
* when the Jetpack_Gutenberg class is available. |
|
308
|
|
|
* |
|
309
|
|
|
* @since 9.6.0 |
|
310
|
|
|
* |
|
311
|
|
|
* @covers Automattic\Jetpack\Blocks::jetpack_register_block |
|
312
|
|
|
*/ |
|
313
|
|
View Code Duplication |
public function test_jetpack_register_block_with_editor_style() { |
|
314
|
|
|
add_filter( 'jetpack_is_standalone_block', '__return_false' ); |
|
315
|
|
|
try { |
|
316
|
|
|
$result = Blocks::jetpack_register_block( 'jetpack/block-with-editor-style' ); |
|
317
|
|
|
$this->assertEquals( 'jetpack/block-with-editor-style', $result->name ); |
|
318
|
|
|
$this->assertEquals( 'jetpack-blocks-editor', $result->editor_style ); |
|
319
|
|
|
} finally { |
|
320
|
|
|
remove_filter( 'jetpack_is_standalone_block', '__return_false' ); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* Test to ensure registering a Jetpack block does not override an existing dependency, |
|
327
|
|
|
* when the Jetpack_Gutenberg class is available. |
|
328
|
|
|
* |
|
329
|
|
|
* @since 9.6.0 |
|
330
|
|
|
* |
|
331
|
|
|
* @covers Automattic\Jetpack\Blocks::jetpack_register_block |
|
332
|
|
|
*/ |
|
333
|
|
View Code Duplication |
public function test_jetpack_register_block_with_existing_editor_style() { |
|
334
|
|
|
add_filter( 'jetpack_is_standalone_block', '__return_false' ); |
|
335
|
|
|
try { |
|
336
|
|
|
$result = Blocks::jetpack_register_block( |
|
337
|
|
|
'jetpack/block-with-existing-editor-style', |
|
338
|
|
|
array( |
|
339
|
|
|
'editor_style' => 'custom-editor-style', |
|
340
|
|
|
) |
|
341
|
|
|
); |
|
342
|
|
|
$this->assertEquals( 'jetpack/block-with-existing-editor-style', $result->name ); |
|
343
|
|
|
$this->assertEquals( 'custom-editor-style', $result->editor_style ); |
|
344
|
|
|
} finally { |
|
345
|
|
|
remove_filter( 'jetpack_is_standalone_block', '__return_false' ); |
|
346
|
|
|
} |
|
347
|
|
|
} |
|
348
|
|
|
} |
|
349
|
|
|
|