-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathEnqueueAdminTest.php
More file actions
447 lines (348 loc) · 13.9 KB
/
Copy pathEnqueueAdminTest.php
File metadata and controls
447 lines (348 loc) · 13.9 KB
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
<?php
/**
* Test cases for the Enqueue_Admin class.
*
* @package accessibility-checker
*/
use EDAC\Admin\Enqueue_Admin;
/**
* Tests for functionality of the Enqueue_Admin class.
*/
class EnqueueAdminTest extends WP_UnitTestCase {
/**
* Holds the instance of the Enqueue_Admin class.
*
* @var Enqueue_Admin the instance of the Enqueue_Admin class.
*/
private $enqueue_admin;
/**
* Holds the test admin user ID.
*
* @var int
*/
private $test_admin_user_id;
/**
* Setup the option, global wp_scripts and the Enqueue_Admin instance.
*
* @return void
*/
protected function setUp(): void {
parent::setUp();
$this->test_admin_user_id = self::factory()->user->create(
[
'role' => 'administrator',
]
);
wp_set_current_user( $this->test_admin_user_id );
update_option( 'edac_post_types', [ 'post', 'page' ] );
global $wp_scripts, $wp_styles;
$wp_scripts = new \WP_Scripts();
$wp_styles = new \WP_Styles();
$this->enqueue_admin = new Enqueue_Admin();
}
/**
* Clean up the option, global wp_scripts and the Enqueue_Admin instance.
*
* @return void
*/
protected function tearDown(): void {
if ( $this->test_admin_user_id ) {
delete_user_meta( $this->test_admin_user_id, 'show_sr_text_in_editor' );
}
wp_set_current_user( 0 );
if ( $this->test_admin_user_id ) {
wp_delete_user( $this->test_admin_user_id );
$this->test_admin_user_id = 0;
}
parent::tearDown();
delete_option( 'edac_post_types' );
global $wp_scripts, $wp_styles;
unset( $wp_scripts, $wp_styles, $GLOBALS['current_screen'] );
unset( $this->enqueue_admin );
}
/**
* Test that the base script is enqueued in the admin on non-editor pages.
*
* @return void
*/
public function testEnqueueBaseScriptInAdminNonEditorPage() {
global $wp_scripts;
$this->enqueue_admin::maybe_enqueue_admin_and_editor_app_scripts();
$this->assertTrue( wp_script_is( 'edac', 'enqueued' ) );
$this->assertFalse( wp_script_is( 'edac-editor-app', 'enqueued' ) );
$localized_data = $wp_scripts->get_data( 'edac', 'data' );
$this->assertIsString( $localized_data );
$this->assertStringContainsString( 'utm_content=__name__', $localized_data );
$this->assertStringNotContainsString( 'utm-content=__name__', $localized_data );
}
/**
* Test localized pro URL includes the expected underscore UTM content key.
*
* @return void
*/
public function testLocalizedProUrlUsesUnderscoreUtmContentKey() {
global $wp_scripts;
$this->enqueue_admin::maybe_enqueue_admin_and_editor_app_scripts();
$localized_data = (string) $wp_scripts->get_data( 'edac', 'data' );
$this->assertStringContainsString( 'utm_content=__name__', $localized_data );
$this->assertStringNotContainsString( 'utm-content=__name__', $localized_data );
}
/**
* Test that the base script and editor script is enqueued in the editor for an existing page.
*
* @return void
*/
public function testEnqueueBaseAndEditorScriptsInAdminEditorExisting() {
global $post;
$post = $this->factory()->post->create_and_get();
global $pagenow;
$pagenow = 'post.php';
$this->enqueue_admin::maybe_enqueue_admin_and_editor_app_scripts();
$this->assertTrue( wp_script_is( 'edac', 'enqueued' ) );
$this->assertTrue( wp_script_is( 'edac-editor-app', 'enqueued' ) );
}
/**
* Test that the base script and editor script is enqueued in the editor for a new page.
*
* @return void
*/
public function testEnqueueBaseAndEditorScriptsInAdminEditorNew() {
global $post;
$post = $this->factory()->post->create_and_get();
global $pagenow;
$pagenow = 'post-new.php';
$this->enqueue_admin::maybe_enqueue_admin_and_editor_app_scripts();
$this->assertTrue( wp_script_is( 'edac', 'enqueued' ) );
$this->assertTrue( wp_script_is( 'edac-editor-app', 'enqueued' ) );
}
/**
* Test that scanUrl uses permalink for frontpage.
*
* @return void
*/
public function testScanUrlUsesPermalinkForFrontpage() {
global $post, $pagenow, $wp_scripts;
// Create a post and set it as the frontpage.
$post = $this->factory()->post->create_and_get( [ 'post_type' => 'page' ] );
update_option( 'page_on_front', $post->ID );
$pagenow = 'post.php';
$this->enqueue_admin::maybe_enqueue_admin_and_editor_app_scripts();
$localized_data = $wp_scripts->get_data( 'edac-editor-app', 'data' );
$this->assertStringContainsString( 'edac_pageScanner', $localized_data );
// In WP 6.9 there were changes to the flags passed to wp_json_encode() that made slashes no longer get escaped by default.
// We should check for both possibilities here to ensure compatibility across versions.
// See: https://github.com/WordPress/wordpress-develop/pull/9557 for more details.
if ( version_compare( get_bloginfo( 'version' ), '6.9', '>=' ) ) {
$this->assertStringContainsString( esc_url_raw( get_permalink( $post->ID ) ), $localized_data );
} else {
$this->assertStringContainsString( str_replace( '/', '\\/', esc_url_raw( get_permalink( $post->ID ) ) ), $localized_data );
}
$this->assertStringNotContainsString( 'preview=true', $localized_data );
// Cleanup.
delete_option( 'page_on_front' );
}
/**
* Test that scanUrl uses permalink for posts page.
*
* @return void
*/
public function testScanUrlUsesPermalinkForPostsPage() {
global $post, $pagenow, $wp_scripts;
// Create a post and set it as the posts page.
$post = $this->factory()->post->create_and_get( [ 'post_type' => 'page' ] );
update_option( 'page_for_posts', $post->ID );
$pagenow = 'post.php';
$this->enqueue_admin::maybe_enqueue_admin_and_editor_app_scripts();
$localized_data = $wp_scripts->get_data( 'edac-editor-app', 'data' );
$this->assertStringContainsString( 'edac_pageScanner', $localized_data );
// In WP 6.9 there were changes to the flags passed to wp_json_encode() that made slashes no longer get escaped by default.
// We should check for both possibilities here to ensure compatibility across versions.
// See: https://github.com/WordPress/wordpress-develop/pull/9557 for more details.
if ( version_compare( get_bloginfo( 'version' ), '6.9', '>=' ) ) {
$this->assertStringContainsString( esc_url_raw( get_permalink( $post->ID ) ), $localized_data );
} else {
$this->assertStringContainsString( str_replace( '/', '\\/', esc_url_raw( get_permalink( $post->ID ) ) ), $localized_data );
}
$this->assertStringNotContainsString( 'preview=true', $localized_data );
// Cleanup.
delete_option( 'page_for_posts' );
}
/**
* Test that scanUrl uses preview link for regular posts.
*
* @return void
*/
public function testScanUrlUsesPreviewLinkForRegularPost() {
global $post, $pagenow, $wp_scripts;
// Create a regular post (not frontpage or posts page).
$post = $this->factory()->post->create_and_get();
$pagenow = 'post.php';
$this->enqueue_admin::maybe_enqueue_admin_and_editor_app_scripts();
$localized_data = $wp_scripts->get_data( 'edac-editor-app', 'data' );
$this->assertStringContainsString( 'edac_pageScanner', $localized_data );
$this->assertStringContainsString( 'preview=true', $localized_data );
}
/**
* Test that the sidebar assets enqueue only in the block editor for scannable post types.
*/
public function testSidebarScriptEnqueuesInBlockEditorForScannablePost() {
global $post, $pagenow;
$post = $this->factory()->post->create_and_get();
$pagenow = 'post.php';
$this->set_mock_screen( true );
Enqueue_Admin::maybe_enqueue_sidebar_script();
$this->assertTrue( wp_script_is( 'edac-sidebar', 'enqueued' ) );
$this->assertTrue( wp_style_is( 'edac-sidebar', 'enqueued' ) );
}
/**
* Test that the sidebar assets do not enqueue when not in the block editor.
*/
public function testSidebarScriptDoesNotEnqueueInClassicEditor() {
global $post, $pagenow;
$post = $this->factory()->post->create_and_get();
$pagenow = 'post.php';
$this->set_mock_screen( false );
Enqueue_Admin::maybe_enqueue_sidebar_script();
$this->assertFalse( wp_script_is( 'edac-sidebar', 'enqueued' ) );
$this->assertFalse( wp_style_is( 'edac-sidebar', 'enqueued' ) );
}
/**
* Test that the sidebar assets do not enqueue for non-scannable post types.
*/
public function testSidebarScriptDoesNotEnqueueForNonScannablePostType() {
update_option( 'edac_post_types', [ 'page' ] );
global $post, $pagenow;
$post = $this->factory()->post->create_and_get( [ 'post_type' => 'post' ] );
$pagenow = 'post.php';
$this->set_mock_screen( true );
Enqueue_Admin::maybe_enqueue_sidebar_script();
$this->assertFalse( wp_script_is( 'edac-sidebar', 'enqueued' ) );
$this->assertFalse( wp_style_is( 'edac-sidebar', 'enqueued' ) );
}
/**
* Test that the sr-only format script enqueues on post.php for a scannable post type.
*/
public function testSrOnlyFormatEnqueuesOnPostEditor() {
global $post, $pagenow;
$post = $this->factory()->post->create_and_get();
$pagenow = 'post.php';
$this->set_mock_screen( true );
Enqueue_Admin::maybe_enqueue_sr_only_format();
$this->assertTrue( wp_script_is( 'edac-sr-only-format', 'enqueued' ) );
}
/**
* Test that the sr-only format script enqueues on post-new.php for a scannable post type.
*/
public function testSrOnlyFormatEnqueuesOnPostNewEditor() {
global $post, $pagenow;
$post = $this->factory()->post->create_and_get();
$pagenow = 'post-new.php';
$this->set_mock_screen( true );
Enqueue_Admin::maybe_enqueue_sr_only_format();
$this->assertTrue( wp_script_is( 'edac-sr-only-format', 'enqueued' ) );
}
/**
* Test that the sr-only format script does not enqueue on post.php for a non-scannable post type.
*/
public function testSrOnlyFormatDoesNotEnqueueForNonScannablePostType() {
update_option( 'edac_post_types', [ 'page' ] );
global $post, $pagenow;
$post = $this->factory()->post->create_and_get( [ 'post_type' => 'post' ] );
$pagenow = 'post.php';
$this->set_mock_screen( true );
Enqueue_Admin::maybe_enqueue_sr_only_format();
$this->assertFalse( wp_script_is( 'edac-sr-only-format', 'enqueued' ) );
}
/**
* Test that the sr-only format script enqueues on the Full Site Editor.
*/
public function testSrOnlyFormatEnqueuesInFullSiteEditor() {
global $pagenow;
$pagenow = 'site-editor.php';
$this->set_mock_screen( true );
Enqueue_Admin::maybe_enqueue_sr_only_format();
$this->assertTrue( wp_script_is( 'edac-sr-only-format', 'enqueued' ) );
}
/**
* Test that the sr-only format script does not enqueue on site-editor.php when not in block editor.
*/
public function testSrOnlyFormatDoesNotEnqueueInFseWhenNotBlockEditor() {
global $pagenow;
$pagenow = 'site-editor.php';
$this->set_mock_screen( false );
Enqueue_Admin::maybe_enqueue_sr_only_format();
$this->assertFalse( wp_script_is( 'edac-sr-only-format', 'enqueued' ) );
}
/**
* Test that the sr-only format script does not enqueue on unrelated admin pages.
*/
public function testSrOnlyFormatDoesNotEnqueueOnOtherAdminPages() {
global $pagenow;
$pagenow = 'edit.php';
$this->set_mock_screen( true );
Enqueue_Admin::maybe_enqueue_sr_only_format();
$this->assertFalse( wp_script_is( 'edac-sr-only-format', 'enqueued' ) );
}
/**
* Test that edac_filter_admin_post_id overrides the post ID localized into edac_script_vars.
*
* @return void
*/
public function testAdminPostIdFilterOverridesLocalizedPostId() {
global $post, $pagenow, $wp_scripts;
$original_post = $this->factory()->post->create_and_get();
$alternate_post = $this->factory()->post->create_and_get();
$post = $original_post;
$pagenow = 'post.php';
$filter_callback = static function () use ( $alternate_post ) {
return $alternate_post->ID;
};
add_filter( 'edac_filter_admin_post_id', $filter_callback );
$this->enqueue_admin::maybe_enqueue_admin_and_editor_app_scripts();
remove_filter( 'edac_filter_admin_post_id', $filter_callback );
$localized_data = $wp_scripts->get_data( 'edac', 'data' );
$this->assertStringContainsString( (string) $alternate_post->ID, $localized_data );
}
/**
* Test that edac_filter_post_is_latest_posts_home causes the scan URL to use the home URL.
*
* When show_on_front=posts and page_for_posts=0, the standard WP options cannot identify a
* virtual homepage post. The filter lets extensions signal this so we use get_home_url()
* for the scanner iframe rather than an invalid preview URL.
*
* @return void
*/
public function testScanUrlUsesHomeUrlWhenLatestPostsHomeFilterReturnsTrue() {
global $post, $pagenow, $wp_scripts;
$post = $this->factory()->post->create_and_get( [ 'post_type' => 'page' ] );
$pagenow = 'post.php';
update_option( 'show_on_front', 'posts' );
update_option( 'page_for_posts', 0 );
$filter_callback = static function () {
return true;
};
add_filter( 'edac_filter_post_is_latest_posts_home', $filter_callback );
$this->enqueue_admin::maybe_enqueue_admin_and_editor_app_scripts();
remove_filter( 'edac_filter_post_is_latest_posts_home', $filter_callback );
delete_option( 'show_on_front' );
delete_option( 'page_for_posts' );
$localized_data = $wp_scripts->get_data( 'edac-editor-app', 'data' );
$this->assertStringContainsString( 'edac_pageScanner', $localized_data );
$this->assertStringNotContainsString( 'preview=true', $localized_data );
// The scan URL should be based on the home URL, not a preview link.
$this->assertStringContainsString( trailingslashit( get_home_url() ), $localized_data );
}
/**
* Helper to set a mock current screen with block editor context.
*
* @param bool $is_block_editor Whether the screen should behave as block editor.
*/
private function set_mock_screen( bool $is_block_editor ): void {
// As of WP 7.0, get_current_screen() requires an actual WP_Screen
// instance. Use WP_Screen::get() to obtain one without the side effects
// of set_current_screen() (e.g. setting $hook_suffix, $typenow, firing
// the current_screen action).
$GLOBALS['current_screen'] = WP_Screen::get( 'post' );
$GLOBALS['current_screen']->is_block_editor = $is_block_editor;
}
}