-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathEnqueueFrontendTest.php
More file actions
100 lines (76 loc) · 2.67 KB
/
Copy pathEnqueueFrontendTest.php
File metadata and controls
100 lines (76 loc) · 2.67 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
<?php
/**
* Test cases for the Enqueue_Frontend class.
*
* @package accessibility-checker
*/
use EDAC\Inc\Enqueue_Frontend;
/**
* Tests for Enqueue_Frontend behavior.
*/
class EnqueueFrontendTest extends WP_UnitTestCase {
/**
* Stored filter callbacks to be removed in tearDown.
*
* @var array<string, callable>
*/
private array $added_filters = [];
/**
* Set up test state.
*/
protected function setUp(): void {
parent::setUp();
update_option( 'edac_post_types', [ 'post' ] );
global $wp_scripts, $wp_styles;
$wp_scripts = new \WP_Scripts();
$wp_styles = new \WP_Styles();
}
/**
* Clean up test state.
*/
protected function tearDown(): void {
foreach ( $this->added_filters as $hook => $callback ) {
remove_filter( $hook, $callback );
}
$this->added_filters = [];
delete_option( 'edac_post_types' );
global $wp_scripts, $wp_styles, $post;
unset( $wp_scripts, $wp_styles, $post );
wp_set_current_user( 0 );
parent::tearDown();
}
/**
* Ensure the localized scannerBundleUrl includes the plugin version as a query string parameter.
*/
public function testScannerBundleUrlIncludesVersionQueryString(): void {
$admin_id = $this->factory()->user->create( [ 'role' => 'administrator' ] );
wp_set_current_user( $admin_id );
$created_post = $this->factory()->post->create_and_get( [ 'post_type' => 'post' ] );
global $post;
$post = $created_post;
Enqueue_Frontend::maybe_enqueue_frontend_highlighter();
global $wp_scripts;
$localized_data = $wp_scripts->get_data( 'edac-frontend-highlighter-app', 'data' );
$this->assertNotEmpty( $localized_data );
$this->assertStringContainsString( 'scannerBundleUrl', $localized_data );
$this->assertStringContainsString( 'ver=' . EDAC_VERSION, $localized_data );
}
/**
* Ensure the highlighter uses the filtered post ID when determining scannable post types.
*/
public function testFrontendHighlighterUsesFilteredPostIdForScannableType(): void {
$admin_id = $this->factory()->user->create( [ 'role' => 'administrator' ] );
wp_set_current_user( $admin_id );
$scannable_post = $this->factory()->post->create_and_get( [ 'post_type' => 'post' ] );
$global_post = $this->factory()->post->create_and_get( [ 'post_type' => 'page' ] );
global $post;
$post = $global_post;
$filter_callback = static function () use ( $scannable_post ) {
return $scannable_post->ID;
};
$this->added_filters['edac_filter_frontend_highlight_post_id'] = $filter_callback;
add_filter( 'edac_filter_frontend_highlight_post_id', $filter_callback );
Enqueue_Frontend::maybe_enqueue_frontend_highlighter();
$this->assertTrue( wp_script_is( 'edac-frontend-highlighter-app', 'enqueued' ) );
}
}