-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTestFeature.php
More file actions
298 lines (251 loc) · 10.2 KB
/
TestFeature.php
File metadata and controls
298 lines (251 loc) · 10.2 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
<?php
/**
* SiteActivePlugins Tests
*
* @package multisyde-unit-tests
*/
declare( strict_types=1 );
namespace Syde\MultiSyde\Modules\SiteActivePlugins\tests\unit;
use Brain\Monkey\Functions;
use Brain\Monkey\Filters;
use Syde\MultiSyde\Modules\SiteActivePlugins\Feature;
use Syde\MultiSydeUnitTests\UnitTestCase;
/**
* Test the SiteActivePlugins class.
*/
final class TestFeature extends UnitTestCase {
/**
* Test the init method.
*
* @runInSeparateProcess
* @return void
*/
public function test_init(): void {
Functions\expect( 'is_network_admin' )->once()->andReturn( true );
Feature::init();
}
/**
* Test the add_thickbox method in the right page.
*
* @return void
*/
public function test_add_thickbox_right_page(): void {
Functions\expect( 'get_current_screen' )->once()->andReturn( (object) array( 'id' => 'plugins-network' ) );
Functions\expect( 'add_thickbox' )->once();
Feature::add_thickbox();
}
/**
* Test the add_thickbox method in the wrong page.
*
* @return void
*/
public function test_add_thickbox_wrong_page(): void {
$wp_screen = (object) array( 'id' => 'plugins' );
Functions\expect( 'get_current_screen' )->once()->andReturn( $wp_screen );
Functions\expect( 'add_thickbox' )->never();
Feature::add_thickbox();
}
/**
* Test the maybe_show_notice method in other admin pages than the network.
*
* @return void
*/
public function test_maybe_show_notice_no_network(): void {
Functions\expect( 'is_network_admin' )->once()->andReturn( false );
Feature::maybe_show_notice();
$this->expectOutputString( '' );
}
/**
* Test the maybe_show_notice method in the network admin page.
*
* @return void
*/
public function test_maybe_show_notice_network_no_globals(): void {
Functions\expect( 'is_network_admin' )->once()->andReturn( true );
Feature::maybe_show_notice();
$this->expectOutputString( '' );
}
/**
* Test the bulk_deactivate method with a user that is not a network admin.
*
* @return void
*/
public function test_bulk_deactivate_no_network_admin(): void {
Functions\expect( 'current_user_can' )->once()->with( 'manage_network_plugins' )->andReturn( false );
Feature::bulk_deactivate();
}
/**
* Test the bulk_deactivate method with a user that is a network admin but there are no POST vars set.
*
* @return void
*/
public function test_bulk_deactivate_network_admin_no_globals(): void {
Functions\expect( 'current_user_can' )->once()->with( 'manage_network_plugins' )->andReturn( true );
Feature::bulk_deactivate();
}
/**
* Test the add_action_link method with an empty active_plugins array.
*
* @return void
*/
public function test_add_action_link_no_active_plugins(): void {
$obj = new Feature();
$links = array(
'deactivate' => '<a href="https://example.com/wp-admin/plugins.php?action=deactivate&plugin=plugin/plugin.php" class="edit">Deactivate</a>',
);
$plugin_file = 'plugin/plugin.php';
$this->assertSame( $links, $obj->add_action_link( $links, $plugin_file ) );
}
/**
* Data provider for add_action_link.
*
* @return array<string, array<string, mixed>>
*/
public static function provide_data_for_add_action_link(): array {
return array(
'with active plugins one link' => array(
'sites' => array( 1 ),
'active_plugins' => array( 'plugin/plugin.php' ),
'plugin_data' => array( 'Name' => 'Plugin' ),
'given_links' => array(
'delete' => '<a href="#delete-link" class="edit">Delete</a>',
),
'expected_links' => array(
'delete' => '<a href="#delete-link" class="edit">Delete</a>',
'site_deactivate' => '<a class="thickbox" title=""Plugin" is active in 1 site" style="display: inline-block" href="#TB_inline?width=600&height=550&inlineId=395f8f9a42dd48eb350e968ed5a81a0a">Sites deactivate</a>',
),
),
'with active plugins two links' => array(
'sites' => array( 1 ),
'active_plugins' => array( 'plugin/plugin.php' ),
'plugin_data' => array( 'Name' => 'Plugin' ),
'given_links' => array(
'activate' => '<a href="#activate-link" class="edit">Activate</a>',
'delete' => '<a href="#delete-link" class="edit">Delete</a>',
),
'expected_links' => array(
'activate' => '<a href="#activate-link" class="edit">Activate</a>',
'site_deactivate' => '<a class="thickbox" title=""Plugin" is active in 1 site" style="display: inline-block" href="#TB_inline?width=600&height=550&inlineId=395f8f9a42dd48eb350e968ed5a81a0a">Sites deactivate</a>',
'delete' => '<a href="#delete-link" class="edit">Delete</a>',
),
),
);
}
/**
* Test the add_action_link method with an active_plugins array.
*
* @dataProvider provide_data_for_add_action_link
*
* @param int[] $sites The sites.
* @param string[] $active_plugins The active plugins.
* @param array<string, string> $plugin_data The plugin data.
* @param array<string, string> $given_links The given links.
* @param array<string, string> $expected_links The expected links.
*
* @return void
*/
public function test_add_action_link_with_active_plugins( array $sites, array $active_plugins, array $plugin_data, array $given_links, array $expected_links ): void {
Functions\expect( 'get_sites' )->once()->andReturn( $sites );
Functions\expect( 'get_blog_option' )->once()->with( $sites[0], 'active_plugins' )->andReturn( $active_plugins );
Functions\expect( 'is_plugin_active_for_network' )->once()->andReturn( false );
Functions\expect( 'get_plugin_data' )->once()->andReturn( $plugin_data );
$obj = new Feature();
$obj->populate_active_plugins();
$this->assertSame( $expected_links, $obj->add_action_link( $given_links, $active_plugins[0] ) );
}
/**
* Test the populate_active_plugins method with an network-wide active plugin.
*
* @return void
*/
public function test_populate_active_plugins_network_active(): void {
Functions\expect( 'get_sites' )->once()->andReturn( array( 1 ) );
Functions\expect( 'get_blog_option' )->once()->with( 1, 'active_plugins' )->andReturn( array( 'plugin/plugin.php' ) );
Functions\expect( 'is_plugin_active_for_network' )->once()->andReturn( true );
( new Feature() )->populate_active_plugins();
}
/**
* Test the populate_active_plugins method with a maximum number of sites.
*
* @return void
*/
public function test_populate_active_plugins_max_sites(): void {
$max_sites = 5;
Filters\expectApplied( Feature::FILTER_MAX_SITES )->once()->with( 100 )->andReturn( $max_sites );
$args = array(
'fields' => 'ids',
'number' => $max_sites,
);
Functions\expect( 'get_sites' )->once()->with( $args )->andReturn( range( 1, $max_sites ) );
Functions\expect( 'get_blog_option' )->times( $max_sites )->andReturn( array( 'plugin/plugin.php' ) );
Functions\expect( 'is_plugin_active_for_network' )->times( $max_sites )->andReturn( false );
( new Feature() )->populate_active_plugins();
}
/**
* Test the print_row_styles method with an empty active_plugins array.
*
* @return void
*/
public function test_print_row_styles_empty(): void {
( new Feature() )->print_row_styles();
$this->expectOutputString( '' );
}
/**
* Test the print_row_styles method.
*
* @return void
*/
public function test_print_row_styles(): void {
Functions\expect( 'get_sites' )->once()->andReturn( array( 1 ) );
Functions\expect( 'get_blog_option' )->once()->with( 1, 'active_plugins' )->andReturn( array( 'plugin/plugin.php' ) );
Functions\expect( 'is_plugin_active_for_network' )->once()->andReturn( false );
$obj = new Feature();
$obj->populate_active_plugins();
$obj->print_row_styles();
$this->expectOutputString( '<style>tr[data-plugin="plugin/plugin.php"]{ background-color: #f6f7f7 !important; }</style>' );
}
/**
* Test the print_thickbox_content method on another screen than plugins-network.
*/
public function test_print_thickbox_content_not_plugins_network(): void {
Functions\expect( 'get_current_screen' )->once()->andReturn( (object) array( 'id' => 'plugins' ) );
$obj = new Feature();
$obj->print_thickbox_content();
$this->expectOutputString( '' );
}
/**
* Test the print_thickbox_content method with a populated active_plugins array.
*
* @return void
*/
public function test_print_thickbox_content_populated(): void {
Functions\expect( 'get_current_screen' )->once()->andReturn( (object) array( 'id' => 'plugins-network' ) );
$obj = new Feature();
$obj->print_thickbox_content();
}
/**
* Test the print_thickbox_content method with an empty active_plugins array.
*
* @return void
*/
public function test_print_thickbox_content(): void {
Functions\expect( 'get_current_screen' )->once()->andReturn( (object) array( 'id' => 'plugins-network' ) );
Functions\expect( 'get_sites' )->once()->andReturn( array( 1 ) );
Functions\expect( 'get_blog_option' )->once()->with( 1, 'active_plugins' )->andReturn( array( 'plugin/plugin.php' ) );
Functions\expect( 'is_plugin_active_for_network' )->once()->andReturn( false );
Functions\expect( 'add_query_arg' )->once()->andReturn( '#some-abitrary-url' );
Functions\expect( 'wp_nonce_field' )->once();
Functions\expect( 'get_blog_details' )->once()->andReturn(
(object) array(
'blog_name' => 'ABC',
'siteurl' => '/abc',
)
);
Functions\expect( 'get_admin_url' )->once()->with( 1, 'plugins.php' )->andReturn( '/abc/wp-admin/plugins.php' );
Functions\expect( 'submit_button' )->once()->andReturn( '<button type="submit" class="button button-primary">Deactivate on selected sites</button>' );
$obj = new Feature();
$obj->populate_active_plugins();
$obj->print_thickbox_content();
$this->expectOutputString( '<div id="395f8f9a42dd48eb350e968ed5a81a0a" style="display:none"><p>Select the sites where you want to deactivate this plugin. Clicking on a site name will open the plugin screen for that site.</p><form method="post" action="http://#some-abitrary-url"><input type="hidden" name="action" value="bulk_deactivate" /><input type="hidden" name="plugin_file" value="plugin/plugin.php" /><ul><li><label><input type="checkbox" name="site_ids[]" value="1" /> <a href="http:///abc/wp-admin/plugins.php" target="_blank" rel="noopener noreferrer">/abc</a></label></li></ul></form></div>' );
}
}