Skip to content

Commit c205bc4

Browse files
committed
Tests: Cover paused and deactivated plugin notices.
Verify notice messages, types, and links, and the conditions that suppress them. Cover site and network deactivation lists and their cleanup after display. Restore the super-admin grant after the test class. Developed in: #14172 Props huzaifaalmesbah. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64430 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d80f420 commit c205bc4

1 file changed

Lines changed: 375 additions & 0 deletions

File tree

Lines changed: 375 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,375 @@
1+
<?php
2+
3+
/**
4+
* Tests for paused_plugins_notice() and deactivated_plugins_notice().
5+
*
6+
* @group admin
7+
* @group plugins
8+
*/
9+
class Tests_Admin_Includes_Plugin_PluginNotices extends WP_UnitTestCase {
10+
11+
/**
12+
* Names of the globals changed by the tests.
13+
*
14+
* @var string[]
15+
*/
16+
const GLOBALS_USED = array( 'pagenow', '_paused_plugins', 'wp_version' );
17+
18+
/**
19+
* ID of a user who can manage plugins.
20+
*
21+
* @var int
22+
*/
23+
private static $admin_id;
24+
25+
/**
26+
* ID of a user who cannot manage plugins.
27+
*
28+
* @var int
29+
*/
30+
private static $subscriber_id;
31+
32+
/**
33+
* Original values of the globals that were set before the test, keyed by name.
34+
*
35+
* @var array
36+
*/
37+
private $orig_globals = array();
38+
39+
/**
40+
* Records the admin notices displayed during the test.
41+
*
42+
* @var MockAction
43+
*/
44+
private $notices;
45+
46+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
47+
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
48+
self::$subscriber_id = $factory->user->create( array( 'role' => 'subscriber' ) );
49+
50+
if ( is_multisite() ) {
51+
grant_super_admin( self::$admin_id );
52+
}
53+
}
54+
55+
public static function wpTearDownAfterClass() {
56+
if ( is_multisite() ) {
57+
revoke_super_admin( self::$admin_id );
58+
}
59+
}
60+
61+
public function set_up() {
62+
parent::set_up();
63+
64+
require_once ABSPATH . 'wp-admin/includes/plugin.php';
65+
66+
$this->orig_globals = array();
67+
68+
foreach ( self::GLOBALS_USED as $name ) {
69+
if ( array_key_exists( $name, $GLOBALS ) ) {
70+
$this->orig_globals[ $name ] = $GLOBALS[ $name ];
71+
}
72+
}
73+
74+
$GLOBALS['pagenow'] = 'index.php';
75+
$GLOBALS['wp_version'] = '9.9';
76+
77+
unset( $GLOBALS['_paused_plugins'] );
78+
79+
$this->notices = new MockAction();
80+
add_action( 'wp_admin_notice', array( $this->notices, 'action' ), 10, 2 );
81+
82+
wp_set_current_user( self::$admin_id );
83+
}
84+
85+
public function tear_down() {
86+
foreach ( self::GLOBALS_USED as $name ) {
87+
if ( array_key_exists( $name, $this->orig_globals ) ) {
88+
$GLOBALS[ $name ] = $this->orig_globals[ $name ];
89+
} else {
90+
unset( $GLOBALS[ $name ] );
91+
}
92+
}
93+
94+
parent::tear_down();
95+
}
96+
97+
/**
98+
* Tests that paused_plugins_notice() displays an error notice linking to the paused plugins.
99+
*
100+
* @ticket 65819
101+
*
102+
* @covers ::paused_plugins_notice
103+
*/
104+
public function test_paused_plugins_notice_displays_error_notice() {
105+
$GLOBALS['_paused_plugins'] = array( 'my-plugin' => array( 'message' => 'Fatal error' ) );
106+
107+
$output = get_echo( 'paused_plugins_notice' );
108+
109+
$this->assertSame(
110+
array(
111+
array(
112+
'<strong>One or more plugins failed to load properly.</strong><br>You can find more details and make changes on the Plugins screen.</p><p><a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/plugins.php?plugin_status=paused">Go to the Plugins screen</a>',
113+
array( 'type' => 'error' ),
114+
),
115+
),
116+
$this->notices->get_args(),
117+
'One error notice should be displayed.'
118+
);
119+
$this->assertStringContainsString( 'One or more plugins failed to load properly.', $output, 'The notice should be printed.' );
120+
}
121+
122+
/**
123+
* Tests that paused_plugins_notice() displays nothing when the notice does not apply.
124+
*
125+
* @ticket 65819
126+
*
127+
* @covers ::paused_plugins_notice
128+
*
129+
* @dataProvider data_paused_plugins_notice_is_not_displayed
130+
*
131+
* @param string $pagenow Current admin page.
132+
* @param bool $can_resume Whether the current user can resume plugins.
133+
* @param array|null $paused_plugins Value of the $_paused_plugins global, or null to leave it unset.
134+
*/
135+
public function test_paused_plugins_notice_is_not_displayed( $pagenow, $can_resume, $paused_plugins ) {
136+
$GLOBALS['pagenow'] = $pagenow;
137+
138+
if ( null !== $paused_plugins ) {
139+
$GLOBALS['_paused_plugins'] = $paused_plugins;
140+
}
141+
142+
wp_set_current_user( $can_resume ? self::$admin_id : self::$subscriber_id );
143+
144+
$this->assertSame( '', get_echo( 'paused_plugins_notice' ), 'Nothing should be printed.' );
145+
$this->assertSame( 0, $this->notices->get_call_count(), 'No notice should be displayed.' );
146+
}
147+
148+
/**
149+
* Data provider.
150+
*
151+
* @return array[]
152+
*/
153+
public static function data_paused_plugins_notice_is_not_displayed() {
154+
$paused_plugins = array( 'my-plugin' => array( 'message' => 'Fatal error' ) );
155+
156+
return array(
157+
'on the Plugins screen' => array( 'plugins.php', true, $paused_plugins ),
158+
'user cannot resume plugins' => array( 'index.php', false, $paused_plugins ),
159+
'paused plugins global not set' => array( 'index.php', true, null ),
160+
'no paused plugins' => array( 'index.php', true, array() ),
161+
);
162+
}
163+
164+
/**
165+
* Tests the notice displayed for each plugin deactivated during an upgrade.
166+
*
167+
* @ticket 65819
168+
*
169+
* @covers ::deactivated_plugins_notice
170+
*/
171+
public function test_deactivated_plugins_notice_displays_warning_for_each_plugin() {
172+
update_option(
173+
'wp_force_deactivated_plugins',
174+
array(
175+
'my-plugin/my-plugin.php' => array(
176+
'plugin_name' => 'My Plugin',
177+
'version_deactivated' => '1.0',
178+
'version_compatible' => '2.0',
179+
),
180+
'other-plugin/other-plugin.php' => array(
181+
'plugin_name' => 'Other Plugin',
182+
'version_deactivated' => '3.1',
183+
'version_compatible' => '',
184+
),
185+
'third-plugin/third-plugin.php' => array(
186+
'plugin_name' => 'Third Plugin',
187+
'version_deactivated' => '',
188+
'version_compatible' => '4.0',
189+
),
190+
),
191+
false
192+
);
193+
194+
if ( is_multisite() ) {
195+
update_site_option( 'wp_force_deactivated_plugins', array() );
196+
}
197+
198+
$link = '</p><p><a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/plugins.php?plugin_status=inactive">Go to the Plugins screen</a>';
199+
200+
get_echo( 'deactivated_plugins_notice' );
201+
202+
$this->assertSame(
203+
array(
204+
array(
205+
'<strong>My Plugin plugin deactivated during WordPress upgrade.</strong><br>My Plugin 1.0 was deactivated due to incompatibility with WordPress 9.9, please upgrade to My Plugin 2.0 or later.' . $link,
206+
array( 'type' => 'warning' ),
207+
),
208+
array(
209+
'<strong>Other Plugin plugin deactivated during WordPress upgrade.</strong><br>Other Plugin 3.1 was deactivated due to incompatibility with WordPress 9.9.' . $link,
210+
array( 'type' => 'warning' ),
211+
),
212+
array(
213+
'<strong>Third Plugin plugin deactivated during WordPress upgrade.</strong><br>Third Plugin was deactivated due to incompatibility with WordPress 9.9.' . $link,
214+
array( 'type' => 'warning' ),
215+
),
216+
),
217+
$this->notices->get_args(),
218+
'One warning notice should be displayed for each plugin.'
219+
);
220+
$this->assertSame( array(), get_option( 'wp_force_deactivated_plugins' ), 'The list of deactivated plugins should be emptied.' );
221+
}
222+
223+
/**
224+
* Tests that plugins deactivated across the network are included on multisite.
225+
*
226+
* @ticket 65819
227+
*
228+
* @group ms-required
229+
*
230+
* @covers ::deactivated_plugins_notice
231+
*/
232+
public function test_deactivated_plugins_notice_includes_network_plugins() {
233+
update_option( 'wp_force_deactivated_plugins', array(), false );
234+
update_site_option(
235+
'wp_force_deactivated_plugins',
236+
array(
237+
'network-plugin/network-plugin.php' => array(
238+
'plugin_name' => 'Network Plugin',
239+
'version_deactivated' => '1.0',
240+
'version_compatible' => '2.0',
241+
),
242+
)
243+
);
244+
245+
get_echo( 'deactivated_plugins_notice' );
246+
247+
$this->assertSame(
248+
array(
249+
array(
250+
'<strong>Network Plugin plugin deactivated during WordPress upgrade.</strong><br>Network Plugin 1.0 was deactivated due to incompatibility with WordPress 9.9, please upgrade to Network Plugin 2.0 or later.</p><p><a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/plugins.php?plugin_status=inactive">Go to the Plugins screen</a>',
251+
array( 'type' => 'warning' ),
252+
),
253+
),
254+
$this->notices->get_args(),
255+
'A warning notice should be displayed for the network plugin.'
256+
);
257+
$this->assertSame( array(), get_site_option( 'wp_force_deactivated_plugins' ), 'The network list of deactivated plugins should be emptied.' );
258+
}
259+
260+
/**
261+
* Tests that plugins deactivated on the site and across the network are both included on multisite.
262+
*
263+
* @ticket 65819
264+
*
265+
* @group ms-required
266+
*
267+
* @covers ::deactivated_plugins_notice
268+
*/
269+
public function test_deactivated_plugins_notice_includes_site_and_network_plugins() {
270+
update_option(
271+
'wp_force_deactivated_plugins',
272+
array(
273+
'site-plugin/site-plugin.php' => array(
274+
'plugin_name' => 'Site Plugin',
275+
'version_deactivated' => '1.0',
276+
'version_compatible' => '2.0',
277+
),
278+
),
279+
false
280+
);
281+
update_site_option(
282+
'wp_force_deactivated_plugins',
283+
array(
284+
'network-plugin/network-plugin.php' => array(
285+
'plugin_name' => 'Network Plugin',
286+
'version_deactivated' => '3.0',
287+
'version_compatible' => '4.0',
288+
),
289+
)
290+
);
291+
292+
$link = '</p><p><a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/plugins.php?plugin_status=inactive">Go to the Plugins screen</a>';
293+
294+
get_echo( 'deactivated_plugins_notice' );
295+
296+
$this->assertSame(
297+
array(
298+
array(
299+
'<strong>Site Plugin plugin deactivated during WordPress upgrade.</strong><br>Site Plugin 1.0 was deactivated due to incompatibility with WordPress 9.9, please upgrade to Site Plugin 2.0 or later.' . $link,
300+
array( 'type' => 'warning' ),
301+
),
302+
array(
303+
'<strong>Network Plugin plugin deactivated during WordPress upgrade.</strong><br>Network Plugin 3.0 was deactivated due to incompatibility with WordPress 9.9, please upgrade to Network Plugin 4.0 or later.' . $link,
304+
array( 'type' => 'warning' ),
305+
),
306+
),
307+
$this->notices->get_args(),
308+
'A warning notice should be displayed for the site plugin, then the network plugin.'
309+
);
310+
$this->assertSame( array(), get_option( 'wp_force_deactivated_plugins' ), 'The list of deactivated plugins should be emptied.' );
311+
$this->assertSame( array(), get_site_option( 'wp_force_deactivated_plugins' ), 'The network list of deactivated plugins should be emptied.' );
312+
}
313+
314+
/**
315+
* Tests that deactivated_plugins_notice() creates the option when it does not exist.
316+
*
317+
* @ticket 65819
318+
*
319+
* @covers ::deactivated_plugins_notice
320+
*/
321+
public function test_deactivated_plugins_notice_creates_missing_option() {
322+
delete_option( 'wp_force_deactivated_plugins' );
323+
delete_site_option( 'wp_force_deactivated_plugins' );
324+
325+
$this->assertSame( '', get_echo( 'deactivated_plugins_notice' ), 'Nothing should be printed.' );
326+
$this->assertSame( 0, $this->notices->get_call_count(), 'No notice should be displayed.' );
327+
$this->assertSame( array(), get_option( 'wp_force_deactivated_plugins' ), 'The option should be created as an empty array.' );
328+
$this->assertSame( array(), get_site_option( 'wp_force_deactivated_plugins' ), 'The network option should be an empty array.' );
329+
}
330+
331+
/**
332+
* Tests that deactivated_plugins_notice() displays nothing and keeps the list when the notice does not apply.
333+
*
334+
* @ticket 65819
335+
*
336+
* @covers ::deactivated_plugins_notice
337+
*
338+
* @dataProvider data_deactivated_plugins_notice_is_not_displayed
339+
*
340+
* @param string $pagenow Current admin page.
341+
* @param bool $can_activate Whether the current user can activate plugins.
342+
* @param array $deactivated_plugins Value of the wp_force_deactivated_plugins option.
343+
*/
344+
public function test_deactivated_plugins_notice_is_not_displayed( $pagenow, $can_activate, $deactivated_plugins ) {
345+
$GLOBALS['pagenow'] = $pagenow;
346+
347+
update_option( 'wp_force_deactivated_plugins', $deactivated_plugins, false );
348+
wp_set_current_user( $can_activate ? self::$admin_id : self::$subscriber_id );
349+
350+
$this->assertSame( '', get_echo( 'deactivated_plugins_notice' ), 'Nothing should be printed.' );
351+
$this->assertSame( 0, $this->notices->get_call_count(), 'No notice should be displayed.' );
352+
$this->assertSame( $deactivated_plugins, get_option( 'wp_force_deactivated_plugins' ), 'The list of deactivated plugins should be kept.' );
353+
}
354+
355+
/**
356+
* Data provider.
357+
*
358+
* @return array[]
359+
*/
360+
public static function data_deactivated_plugins_notice_is_not_displayed() {
361+
$deactivated_plugins = array(
362+
'my-plugin/my-plugin.php' => array(
363+
'plugin_name' => 'My Plugin',
364+
'version_deactivated' => '1.0',
365+
'version_compatible' => '2.0',
366+
),
367+
);
368+
369+
return array(
370+
'on the Plugins screen' => array( 'plugins.php', true, $deactivated_plugins ),
371+
'user cannot activate plugins' => array( 'index.php', false, $deactivated_plugins ),
372+
'no deactivated plugins' => array( 'index.php', true, array() ),
373+
);
374+
}
375+
}

0 commit comments

Comments
 (0)