-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathclass-wp-theme-json-resolver-test.php
491 lines (446 loc) · 14.8 KB
/
class-wp-theme-json-resolver-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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<?php
/**
* Test WP_Theme_JSON_Resolver_Gutenberg class.
*
* @package Gutenberg
*/
class WP_Theme_JSON_Resolver_Gutenberg_Test extends WP_UnitTestCase {
/**
* Administrator ID.
*
* @var int
*/
protected static $administrator_id;
/**
* WP_Theme_JSON_Resolver::$blocks_cache property.
*
* @var ReflectionProperty
*/
private static $property_blocks_cache;
/**
* Original value of the WP_Theme_JSON_Resolver::$blocks_cache property.
*
* @var array
*/
private static $property_blocks_cache_orig_value;
/**
* WP_Theme_JSON_Resolver::$core property.
*
* @var ReflectionProperty
*/
private static $property_core;
/**
* Original value of the WP_Theme_JSON_Resolver::$core property.
*
* @var WP_Theme_JSON
*/
private static $property_core_orig_value;
public static function set_up_before_class() {
parent::set_up_before_class();
self::$administrator_id = self::factory()->user->create(
array(
'role' => 'administrator',
'user_email' => 'administrator@example.com',
)
);
static::$property_blocks_cache = new ReflectionProperty( WP_Theme_JSON_Resolver_Gutenberg::class, 'blocks_cache' );
static::$property_blocks_cache->setAccessible( true );
static::$property_blocks_cache_orig_value = static::$property_blocks_cache->getValue();
static::$property_core = new ReflectionProperty( WP_Theme_JSON_Resolver_Gutenberg::class, 'core' );
static::$property_core->setAccessible( true );
static::$property_core_orig_value = static::$property_core->getValue();
}
public function set_up() {
parent::set_up();
$this->theme_root = realpath( __DIR__ . '/data/themedir1' );
$this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
// /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
$GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );
add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) );
add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );
$this->queries = array();
// Clear caches.
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
}
public function tear_down() {
$GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
parent::tear_down();
}
public function filter_set_theme_root() {
return $this->theme_root;
}
public function filter_set_locale_to_polish() {
return 'pl_PL';
}
public function filter_db_query( $query ) {
if ( preg_match( '#post_type = \'wp_global_styles\'#', $query ) ) {
$this->queries[] = $query;
}
return $query;
}
public function test_translations_are_applied() {
add_filter( 'locale', array( $this, 'filter_set_locale_to_polish' ) );
load_textdomain( 'block-theme', realpath( __DIR__ . '/data/languages/themes/block-theme-pl_PL.mo' ) );
switch_theme( 'block-theme' );
$actual = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data();
unload_textdomain( 'block-theme' );
remove_filter( 'locale', array( $this, 'filter_set_locale_to_polish' ) );
$this->assertSame( wp_get_theme()->get( 'TextDomain' ), 'block-theme' );
$this->assertSame(
array(
'color' => array(
'custom' => false,
'customGradient' => true,
'palette' => array(
'theme' => array(
array(
'slug' => 'light',
'name' => 'Jasny',
'color' => '#f5f7f9',
),
array(
'slug' => 'dark',
'name' => 'Ciemny',
'color' => '#000',
),
),
),
),
'typography' => array(
'customFontSize' => true,
'lineHeight' => false,
),
'spacing' => array(
'units' => false,
'padding' => false,
),
'blocks' => array(
'core/paragraph' => array(
'color' => array(
'palette' => array(
'theme' => array(
array(
'slug' => 'light',
'name' => 'Jasny',
'color' => '#f5f7f9',
),
),
),
),
),
),
),
$actual->get_settings()
);
$this->assertSame(
$actual->get_custom_templates(),
array(
'page-home' => array(
'title' => 'Szablon strony głównej',
'postTypes' => array( 'page' ),
),
)
);
$this->assertSame(
$actual->get_template_parts(),
array(
'small-header' => array(
'title' => 'Mały nagłówek',
'area' => 'header',
),
)
);
}
public function test_add_theme_supports_are_loaded_for_themes_without_theme_json() {
switch_theme( 'default' );
$color_palette = array(
array(
'name' => 'Primary',
'slug' => 'primary',
'color' => '#F00',
),
array(
'name' => 'Secondary',
'slug' => 'secondary',
'color' => '#0F0',
),
array(
'name' => 'Tertiary',
'slug' => 'tertiary',
'color' => '#00F',
),
);
add_theme_support( 'editor-color-palette', $color_palette );
add_theme_support( 'custom-line-height' );
$settings = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_settings();
remove_theme_support( 'custom-line-height' );
remove_theme_support( 'editor-color-palette' );
$this->assertFalse( wp_theme_has_theme_json() );
$this->assertTrue( $settings['typography']['lineHeight'] );
$this->assertSame( $color_palette, $settings['color']['palette']['theme'] );
}
/**
* Recursively applies ksort to an array.
*/
private static function recursive_ksort( &$array ) {
foreach ( $array as &$value ) {
if ( is_array( $value ) ) {
self::recursive_ksort( $value );
}
}
ksort( $array );
}
public function test_merges_child_theme_json_into_parent_theme_json() {
switch_theme( 'block-theme-child' );
$actual_settings = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_settings();
$expected_settings = array(
'color' => array(
'custom' => false,
'customGradient' => true,
'palette' => array(
'theme' => array(
array(
'slug' => 'light',
'name' => 'Light',
'color' => '#f3f4f6',
),
array(
'slug' => 'primary',
'name' => 'Primary',
'color' => '#3858e9',
),
array(
'slug' => 'dark',
'name' => 'Dark',
'color' => '#111827',
),
),
),
'link' => true,
),
'typography' => array(
'customFontSize' => true,
'lineHeight' => false,
),
'spacing' => array(
'units' => false,
'padding' => false,
),
'blocks' => array(
'core/paragraph' => array(
'color' => array(
'palette' => array(
'theme' => array(
array(
'slug' => 'light',
'name' => 'Light',
'color' => '#f5f7f9',
),
),
),
),
),
'core/post-title' => array(
'color' => array(
'palette' => array(
'theme' => array(
array(
'slug' => 'light',
'name' => 'Light',
'color' => '#f3f4f6',
),
),
),
),
),
),
);
self::recursive_ksort( $actual_settings );
self::recursive_ksort( $expected_settings );
// Should merge settings.
$this->assertSame(
$expected_settings,
$actual_settings
);
$this->assertSame(
WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_custom_templates(),
array(
'page-home' => array(
'title' => 'Homepage',
'postTypes' => array( 'page' ),
),
)
);
}
public function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries() {
// Switch to a theme that does have support.
switch_theme( 'block-theme' );
wp_set_current_user( self::$administrator_id );
$theme = wp_get_theme();
WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( $theme );
add_filter( 'query', array( $this, 'filter_db_query' ) );
$query_count = count( $this->queries );
for ( $i = 0; $i < 3; $i++ ) {
WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( $theme );
WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data();
}
$query_count = count( $this->queries ) - $query_count;
$this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' );
$user_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( $theme );
$this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' );
$user_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( $theme, true );
$this->assertNotEmpty( $user_cpt, 'User CPT is expected not to be empty.' );
$query_count = count( $this->queries );
for ( $i = 0; $i < 3; $i ++ ) {
$new_user_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( $theme );
WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data();
$this->assertSameSets( $user_cpt, $new_user_cpt, "User CPTs do not match on run {$i}." );
}
$query_count = count( $this->queries ) - $query_count;
$this->assertSame( 1, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type after creation.' );
}
/**
* @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
*/
public function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries_for_logged_out_users() {
$theme = wp_get_theme();
WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( $theme );
add_filter( 'query', array( $this, 'filter_db_query' ) );
$query_count = count( $this->queries );
for ( $i = 0; $i < 3; $i++ ) {
WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( $theme );
WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data();
}
$query_count = count( $this->queries ) - $query_count;
$this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' );
$user_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( $theme );
$this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' );
}
/**
* Test that get_merged_data returns the data merged up to the proper origin.
*
* @covers WP_Theme_JSON_Resolver::get_merged_data
*
* @dataProvider data_get_merged_data_returns_origin
*
* @param string $origin What origin to get data from.
* @param bool $core_palette Whether the core palette is present.
* @param string $core_palette_text Message.
* @param string $block_styles Whether the block styles are present.
* @param string $block_styles_text Message.
* @param bool $theme_palette Whether the theme palette is present.
* @param string $theme_palette_text Message.
* @param bool $user_palette Whether the user palette is present.
* @param string $user_palette_text Message.
*/
public function test_get_merged_data_returns_origin( $origin, $core_palette, $core_palette_text, $block_styles, $block_styles_text, $theme_palette, $theme_palette_text, $user_palette, $user_palette_text ) {
// Make sure there is data from the blocks origin.
register_block_type(
'my/block-with-styles',
array(
'api_version' => 2,
'attributes' => array(
'borderColor' => array(
'type' => 'string',
),
'style' => array(
'type' => 'object',
),
),
'supports' => array(
'__experimentalStyle' => array(
'typography' => array(
'fontSize' => '42rem',
),
),
),
)
);
// Make sure there is data from the theme origin.
switch_theme( 'block-theme' );
// Make sure there is data from the user origin.
wp_set_current_user( self::$administrator_id );
$user_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( wp_get_theme(), true );
$config = json_decode( $user_cpt['post_content'], true );
$config['settings']['color']['palette']['custom'] = array(
array(
'color' => 'hotpink',
'name' => 'My color',
'slug' => 'my-color',
),
);
$user_cpt['post_content'] = wp_json_encode( $config );
wp_update_post( $user_cpt, true, false );
$theme_json = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $origin );
$settings = $theme_json->get_settings();
$styles = $theme_json->get_styles_block_nodes();
$this->assertSame( $core_palette, isset( $settings['color']['palette']['default'] ), $core_palette_text );
$styles = array_filter(
$styles,
static function( $element ) {
return isset( $element['name'] ) && 'my/block-with-styles' === $element['name'];
}
);
$this->assertSame( $block_styles, count( $styles ) === 1, $block_styles_text );
$this->assertSame( $theme_palette, isset( $settings['color']['palette']['theme'] ), $theme_palette_text );
$this->assertSame( $user_palette, isset( $settings['color']['palette']['custom'] ), $user_palette_text );
unregister_block_type( 'my/block-with-styles' );
}
/**
* Data provider for test_get_merged_data_returns_origin
*
* @return array
*/
public function data_get_merged_data_returns_origin() {
return array(
'origin_default' => array(
'origin' => 'default',
'core_palette' => true,
'core_palette_text' => 'Core palette must be present',
'block_styles' => false,
'block_styles_text' => 'Block styles should not be present',
'theme_palette' => false,
'theme_palette_text' => 'Theme palette should not be present',
'user_palette' => false,
'user_palette_text' => 'User palette should not be present',
),
'origin_blocks' => array(
'origin' => 'blocks',
'core_palette' => true,
'core_palette_text' => 'Core palette must be present',
'block_styles' => true,
'block_styles_text' => 'Block styles must be present',
'theme_palette' => false,
'theme_palette_text' => 'Theme palette should not be present',
'user_palette' => false,
'user_palette_text' => 'User palette should not be present',
),
'origin_theme' => array(
'origin' => 'theme',
'core_palette' => true,
'core_palette_text' => 'Core palette must be present',
'block_styles' => true,
'block_styles_text' => 'Block styles must be present',
'theme_palette' => true,
'theme_palette_text' => 'Theme palette must be present',
'user_palette' => false,
'user_palette_text' => 'User palette should not be present',
),
'origin_custom' => array(
'origin' => 'custom',
'core_palette' => true,
'core_palette_text' => 'Core palette must be present',
'block_styles' => true,
'block_styles_text' => 'Block styles must be present',
'theme_palette' => true,
'theme_palette_text' => 'Theme palette must be present',
'user_palette' => true,
'user_palette_text' => 'User palette must be present',
),
);
}
}