@@ -243,10 +243,67 @@ public function test_super_admin_with_valid_nonce_can_install() {
243243 $ this ->assert_valid_nonce_reaches_install_redirect ();
244244 }
245245
246+ /**
247+ * @group ms-required
248+ */
249+ public function test_super_admin_with_invalid_nonce_is_denied () {
250+ $ this ->set_multisite_user ( true );
251+
252+ $ this ->assert_invalid_nonce_is_denied ();
253+ }
254+
255+ /**
256+ * @group ms-required
257+ */
258+ public function test_subsite_administrator_with_valid_nonce_cannot_install () {
259+ $ this ->set_multisite_user ( false );
260+
261+ $ this ->assert_valid_nonce_install_is_denied ();
262+ }
263+
264+ /**
265+ * @group ms-excluded
266+ */
267+ public function test_single_site_administrator_can_deactivate_sqlite () {
268+ $ this ->set_single_site_user ( 'administrator ' );
269+
270+ $ this ->assertTrue ( current_user_can ( 'deactivate_plugin ' , plugin_basename ( SQLITE_MAIN_FILE ) ) );
271+ $ this ->assert_deactivation_removes_dropin ();
272+ }
273+
274+ /**
275+ * @group ms-excluded
276+ */
277+ public function test_single_site_plugin_manager_can_only_deactivate_sqlite_without_dropin () {
278+ $ this ->set_single_site_user ( 'administrator ' );
279+ wp_get_current_user ()->add_cap ( 'manage_options ' , false );
280+
281+ $ this ->assertFalse ( current_user_can ( 'manage_options ' ) );
282+ $ this ->assertTrue ( current_user_can ( 'deactivate_plugin ' , 'another-plugin/plugin.php ' ) );
283+ $ this ->assertFalse ( current_user_can ( 'deactivate_plugin ' , plugin_basename ( SQLITE_MAIN_FILE ) ) );
284+
285+ $ this ->run_without_dropin (
286+ function () {
287+ $ this ->assertTrue ( current_user_can ( 'deactivate_plugin ' , plugin_basename ( SQLITE_MAIN_FILE ) ) );
288+ }
289+ );
290+ }
291+
292+ /**
293+ * @group ms-required
294+ */
295+ public function test_super_admin_can_deactivate_sqlite () {
296+ $ this ->set_multisite_user ( true );
297+
298+ $ this ->assertTrue ( current_user_can ( 'deactivate_plugin ' , plugin_basename ( SQLITE_MAIN_FILE ) ) );
299+ $ this ->assert_deactivation_removes_dropin ();
300+ }
301+
246302 /**
247303 * @group ms-required
248304 */
249305 public function test_network_deactivation_context_is_forwarded_to_mysql_cleanup () {
306+ $ this ->set_multisite_user ( true );
250307 $ shutdown_hook_before = isset ( $ GLOBALS ['wp_filter ' ]['shutdown ' ] ) ? clone $ GLOBALS ['wp_filter ' ]['shutdown ' ] : null ;
251308
252309 try {
@@ -324,25 +381,58 @@ public function test_site_deactivation_updates_mysql_active_plugins() {
324381 /**
325382 * @group ms-required
326383 */
327- public function test_super_admin_with_invalid_nonce_is_denied () {
328- $ this ->set_multisite_user ( true );
384+ public function test_delegated_network_plugin_manager_can_only_deactivate_sqlite_without_dropin () {
385+ $ this ->run_as_delegated_network_plugin_manager (
386+ function () {
387+ $ this ->assertTrue ( current_user_can ( 'deactivate_plugin ' , 'another-plugin/plugin.php ' ) );
388+ $ this ->assertFalse ( current_user_can ( 'deactivate_plugin ' , plugin_basename ( SQLITE_MAIN_FILE ) ) );
389+
390+ $ this ->run_without_dropin (
391+ function () {
392+ $ this ->assertTrue ( current_user_can ( 'deactivate_plugin ' , plugin_basename ( SQLITE_MAIN_FILE ) ) );
393+ }
394+ );
395+ }
396+ );
397+ }
329398
330- $ this ->assert_invalid_nonce_is_denied ();
399+ private function set_single_site_user ( $ role ) {
400+ $ user_id = self ::factory ()->user ->create ( array ( 'role ' => $ role ) );
401+
402+ wp_set_current_user ( $ user_id );
331403 }
332404
333- /**
334- * @group ms-required
335- */
336- public function test_subsite_administrator_with_valid_nonce_cannot_install () {
405+ private function set_multisite_user ( $ is_super_admin ) {
406+ $ user_id = $ is_super_admin ? self ::$ super_admin_id : self ::$ subsite_admin_id ;
407+
408+ switch_to_blog ( self ::$ site_id );
409+ wp_set_current_user ( $ user_id );
410+ }
411+
412+ private function run_as_delegated_network_plugin_manager ( $ callback ) {
337413 $ this ->set_multisite_user ( false );
338414
339- $ this ->assert_valid_nonce_install_is_denied ();
415+ $ user = wp_get_current_user ();
416+ $ capabilities = array ( 'activate_plugins ' , 'manage_network_plugins ' );
417+
418+ foreach ( $ capabilities as $ capability ) {
419+ $ user ->add_cap ( $ capability );
420+ }
421+
422+ try {
423+ return call_user_func ( $ callback );
424+ } finally {
425+ foreach ( $ capabilities as $ capability ) {
426+ $ user ->remove_cap ( $ capability );
427+ }
428+ }
340429 }
341430
342- private function set_single_site_user ( $ role ) {
343- $ user_id = self :: factory ()-> user -> create ( array ( ' role ' => $ role ) );
431+ private function assert_deactivation_removes_dropin ( ) {
432+ $ filesystem = $ this -> run_with_deactivation_filesystem ( ' sqlite_plugin_remove_db_file ' );
344433
345- wp_set_current_user ( $ user_id );
434+ $ this ->assertSame ( WP_CONTENT_DIR . '/db.php ' , $ filesystem ->deleted_path );
435+ $ this ->assertFileExists ( WP_CONTENT_DIR . '/db.php ' );
346436 }
347437
348438 private function run_with_deactivation_filesystem ( $ callback ) {
@@ -402,16 +492,6 @@ public function update( $table, $data, $where ) {
402492 };
403493 }
404494
405- private function set_multisite_user ( $ is_super_admin ) {
406- $ user_id = $ is_super_admin ? self ::$ super_admin_id : self ::$ subsite_admin_id ;
407-
408- switch_to_blog ( self ::$ site_id );
409- wp_set_current_user ( $ user_id );
410-
411- $ this ->assertTrue ( current_user_can ( 'manage_options ' ) );
412- $ this ->assertSame ( $ is_super_admin , current_user_can ( 'manage_network_options ' ) );
413- }
414-
415495 private function reset_admin_menu () {
416496 $ global_names = array (
417497 'menu ' ,
0 commit comments