-
Notifications
You must be signed in to change notification settings - Fork 4k
/
class-admin-test.php
204 lines (178 loc) · 5.95 KB
/
class-admin-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
<?php
/**
* Admin Tests
*
* @package Gutenberg
*/
/**
* Test functions in register.php
*/
class Admin_Test extends WP_UnitTestCase {
/**
* Editor user ID.
*
* @var int
*/
protected static $editor_user_id;
/**
* ID for a post containing blocks.
*
* @var int
*/
protected static $post_with_blocks;
/**
* ID for a post without blocks.
*
* @var int
*/
protected static $post_without_blocks;
/**
* Set up before class.
*/
public static function wpSetUpBeforeClass() {
self::$editor_user_id = self::factory()->user->create(
array(
'role' => 'editor',
)
);
self::$post_with_blocks = self::factory()->post->create(
array(
'post_title' => 'Example',
'post_content' => "<!-- wp:core/text {\"dropCap\":true} -->\n<p class=\"has-drop-cap\">Tester</p>\n<!-- /wp:core/text -->",
)
);
self::$post_without_blocks = self::factory()->post->create(
array(
'post_title' => 'Example',
'post_content' => 'Tester',
)
);
}
/**
* Tests gutenberg_can_edit_post().
*
* @covers ::gutenberg_can_edit_post
*/
function test_gutenberg_can_edit_post() {
$this->assertFalse( gutenberg_can_edit_post( -1 ) );
$bogus_post_id = $this->factory()->post->create(
array(
'post_type' => 'bogus',
)
);
$this->assertFalse( gutenberg_can_edit_post( $bogus_post_id ) );
register_post_type(
'restless',
array(
'show_in_rest' => false,
)
);
$restless_post_id = $this->factory()->post->create(
array(
'post_type' => 'restless',
)
);
$this->assertFalse( gutenberg_can_edit_post( $restless_post_id ) );
$generic_post_id = $this->factory()->post->create();
wp_set_current_user( 0 );
$this->assertFalse( gutenberg_can_edit_post( $generic_post_id ) );
wp_set_current_user( self::$editor_user_id );
$this->assertTrue( gutenberg_can_edit_post( $generic_post_id ) );
$blog_page_without_content = self::factory()->post->create(
array(
'post_title' => 'Blog',
'post_content' => '',
)
);
update_option( 'page_for_posts', $blog_page_without_content );
$this->assertFalse( gutenberg_can_edit_post( $blog_page_without_content ) );
$blog_page_with_content = self::factory()->post->create(
array(
'post_title' => 'Blog',
'post_content' => 'Hello World!',
)
);
update_option( 'page_for_posts', $blog_page_with_content );
$this->assertTrue( gutenberg_can_edit_post( $blog_page_with_content ) );
add_filter( 'gutenberg_can_edit_post', '__return_false' );
$this->assertFalse( gutenberg_can_edit_post( $generic_post_id ) );
remove_filter( 'gutenberg_can_edit_post', '__return_false' );
add_filter( 'gutenberg_can_edit_post', '__return_true' );
$this->assertTrue( gutenberg_can_edit_post( $restless_post_id ) );
remove_filter( 'gutenberg_can_edit_post', '__return_true' );
}
/**
* Tests gutenberg_post_has_blocks().
*
* @covers ::gutenberg_post_has_blocks
* @expectedDeprecated gutenberg_post_has_blocks
*/
function test_gutenberg_post_has_blocks() {
$this->assertTrue( gutenberg_post_has_blocks( self::$post_with_blocks ) );
$this->assertFalse( gutenberg_post_has_blocks( self::$post_without_blocks ) );
}
/**
* Tests gutenberg_content_has_blocks().
*
* @covers ::gutenberg_content_has_blocks
* @expectedDeprecated gutenberg_content_has_blocks
*/
function test_gutenberg_content_has_blocks() {
$content_with_blocks = get_post_field( 'post_content', self::$post_with_blocks );
$content_without_blocks = get_post_field( 'post_content', self::$post_without_blocks );
$this->assertTrue( gutenberg_content_has_blocks( $content_with_blocks ) );
$this->assertFalse( gutenberg_content_has_blocks( $content_without_blocks ) );
}
/**
* Tests gutenberg_add_gutenberg_post_state().
*
* @covers ::gutenberg_add_gutenberg_post_state
*/
function test_add_gutenberg_post_state() {
// With blocks.
$post_states = apply_filters( 'display_post_states', array(), get_post( self::$post_with_blocks ) );
$this->assertEquals( array( 'Gutenberg' ), $post_states );
// Without blocks.
$post_states = apply_filters( 'display_post_states', array(), get_post( self::$post_without_blocks ) );
$this->assertEquals( array(), $post_states );
}
/**
* Test that the revisions 'return to editor' links are set correctly for Classic & Gutenberg editors.
*
* @covers ::gutenberg_revisions_link_to_editor
*/
function test_gutenberg_revisions_link_to_editor() {
global $pagenow;
// Set up $pagenow so the filter will work.
$pagenow = 'revision.php';
// Test the filter when Gutenberg is the active editor.
$_REQUEST['gutenberg'] = '1';
$link = apply_filters( 'get_edit_post_link', 'http:https://test.com' );
$this->assertEquals( 'http:https://test.com', $link );
// Test the filter when Gutenberg is not the active editor.
unset( $_REQUEST['gutenberg'] );
$link = apply_filters( 'get_edit_post_link', 'http:https://test.com' );
$this->assertEquals( 'http:https://test.com?classic-editor', $link );
}
/**
* Test that the revisions 'restore this revision' button links correctly for Classic & Gutenberg editors.
*/
function test_gutenberg_revisions_restore() {
// Test the filter when Gutenberg is the active editor.
$_REQUEST['gutenberg'] = '1';
$link = apply_filters( 'wp_prepare_revision_for_js', array( 'restoreUrl' => 'http:https://test.com' ) );
$this->assertEquals( array( 'restoreUrl' => 'http:https://test.com?gutenberg=1' ), $link );
// Test the filter when Gutenberg is not the active editor.
unset( $_REQUEST['gutenberg'] );
$link = apply_filters( 'wp_prepare_revision_for_js', array( 'restoreUrl' => 'http:https://test.com' ) );
$this->assertEquals( array( 'restoreUrl' => 'http:https://test.com' ), $link );
}
/**
* Ensure gutenberg_preload_api_request() works without notices in PHP 5.2.
*
* The array_reduce() function only accepts mixed variables starting with PHP 5.3.
*/
function test_preload_api_request_no_notices_php_52() {
$this->assertTrue( is_array( gutenberg_preload_api_request( 0, '/' ) ) );
}
}