Skip to content

Commit b0aa6b3

Browse files
committed
Tests: Cover plugin deletion, deactivation, and sandbox loading.
Verify plugin and translation-file deletion, uninstall callbacks, failure reporting, and update metadata cleanup. Cover site and network deactivation and sandbox loading, including symlinked plugins. Developed in: #14604, #14605, #14609 Props huzaifaalmesbah, mukesh27. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@65050 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 1c2cdbc commit b0aa6b3

3 files changed

Lines changed: 660 additions & 0 deletions

File tree

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<?php
2+
3+
/**
4+
* Tests for deactivate_plugins().
5+
*
6+
* @group admin
7+
* @group plugins
8+
*
9+
* @covers ::deactivate_plugins
10+
*/
11+
class Tests_Admin_Includes_Plugin_DeactivatePlugins extends WP_UnitTestCase {
12+
13+
/**
14+
* The deactivation hooks that fired, with their arguments.
15+
*
16+
* @var array[]
17+
*/
18+
private $calls = array();
19+
20+
/**
21+
* Resets the active plugins and records the deactivation hooks.
22+
*/
23+
public function set_up() {
24+
parent::set_up();
25+
26+
update_option( 'active_plugins', array( 'a/a.php', 'b/b.php', 'c.php' ) );
27+
update_site_option( 'active_sitewide_plugins', array() );
28+
29+
$this->calls = array();
30+
foreach ( array( 'deactivate_plugin', 'deactivate_a/a.php', 'deactivate_b/b.php', 'deactivated_plugin' ) as $hook ) {
31+
add_action(
32+
$hook,
33+
function ( ...$args ) use ( $hook ) {
34+
$this->calls[] = array( $hook, $args );
35+
},
36+
10,
37+
2
38+
);
39+
}
40+
}
41+
42+
/**
43+
* Tests that plugins are deactivated and the deactivation hooks fire for each.
44+
*
45+
* @ticket 65819
46+
*/
47+
public function test_should_deactivate_plugins_and_fire_hooks() {
48+
deactivate_plugins( array( 'a/a.php', 'b/b.php' ) );
49+
50+
$this->assertSame( array( 'c.php' ), array_values( get_option( 'active_plugins' ) ), 'The other plugin should stay active.' );
51+
$this->assertSame(
52+
array(
53+
array( 'deactivate_plugin', array( 'a/a.php', false ) ),
54+
array( 'deactivate_a/a.php', array( false ) ),
55+
array( 'deactivated_plugin', array( 'a/a.php', false ) ),
56+
array( 'deactivate_plugin', array( 'b/b.php', false ) ),
57+
array( 'deactivate_b/b.php', array( false ) ),
58+
array( 'deactivated_plugin', array( 'b/b.php', false ) ),
59+
),
60+
$this->calls,
61+
'The deactivation hooks should fire in order for each plugin.'
62+
);
63+
}
64+
65+
/**
66+
* Tests that a single plugin can be passed as a full path with surrounding whitespace.
67+
*
68+
* @ticket 65819
69+
*/
70+
public function test_should_accept_single_plugin_as_full_path() {
71+
deactivate_plugins( ' ' . WP_PLUGIN_DIR . '/a/a.php ' );
72+
73+
$this->assertSame( array( 'b/b.php', 'c.php' ), array_values( get_option( 'active_plugins' ) ) );
74+
}
75+
76+
/**
77+
* Tests that inactive plugins are skipped without hooks or an option update.
78+
*
79+
* @ticket 65819
80+
*/
81+
public function test_should_skip_inactive_plugins() {
82+
$updates = new MockAction();
83+
add_filter( 'pre_update_option_active_plugins', array( $updates, 'filter' ) );
84+
85+
deactivate_plugins( array( 'inactive/inactive.php' ) );
86+
87+
$this->assertSame( array( 'a/a.php', 'b/b.php', 'c.php' ), get_option( 'active_plugins' ), 'The active plugins should not change.' );
88+
$this->assertSame( array(), $this->calls, 'No deactivation hooks should fire.' );
89+
$this->assertSame( 0, $updates->get_call_count(), 'The option should not be updated.' );
90+
}
91+
92+
/**
93+
* Tests that no hooks fire in silent mode.
94+
*
95+
* @ticket 65819
96+
*/
97+
public function test_should_not_fire_hooks_in_silent_mode() {
98+
deactivate_plugins( 'a/a.php', true );
99+
100+
$this->assertSame( array( 'b/b.php', 'c.php' ), array_values( get_option( 'active_plugins' ) ), 'The plugin should be deactivated.' );
101+
$this->assertSame( array(), $this->calls, 'No deactivation hooks should fire.' );
102+
}
103+
104+
/**
105+
* Tests that a network-active plugin is deactivated network-wide by default.
106+
*
107+
* @ticket 65819
108+
*
109+
* @group ms-required
110+
*/
111+
public function test_should_deactivate_network_active_plugin_network_wide() {
112+
update_site_option(
113+
'active_sitewide_plugins',
114+
array(
115+
'a/a.php' => 1000,
116+
'd/d.php' => 1000,
117+
)
118+
);
119+
120+
deactivate_plugins( 'a/a.php' );
121+
122+
$this->assertSame( array( 'd/d.php' => 1000 ), get_site_option( 'active_sitewide_plugins' ), 'The plugin should be network deactivated.' );
123+
$this->assertSame( array( 'b/b.php', 'c.php' ), array_values( get_option( 'active_plugins' ) ), 'The plugin should also be deactivated on the site.' );
124+
$this->assertSame(
125+
array(
126+
array( 'deactivate_plugin', array( 'a/a.php', true ) ),
127+
array( 'deactivate_a/a.php', array( true ) ),
128+
array( 'deactivated_plugin', array( 'a/a.php', true ) ),
129+
),
130+
$this->calls,
131+
'The hooks should receive true.'
132+
);
133+
}
134+
135+
/**
136+
* Tests that only the network activation is removed when deactivating network-wide.
137+
*
138+
* @ticket 65819
139+
*
140+
* @group ms-required
141+
*/
142+
public function test_should_only_deactivate_network_wide_when_requested() {
143+
update_site_option( 'active_sitewide_plugins', array( 'a/a.php' => 1000 ) );
144+
145+
deactivate_plugins( 'a/a.php', false, true );
146+
147+
$this->assertSame( array(), get_site_option( 'active_sitewide_plugins' ), 'The plugin should be network deactivated.' );
148+
$this->assertSame( array( 'a/a.php', 'b/b.php', 'c.php' ), get_option( 'active_plugins' ), 'The site activation should be kept.' );
149+
}
150+
151+
/**
152+
* Tests that a plugin active only on the site is kept when deactivating network-wide.
153+
*
154+
* @ticket 65819
155+
*
156+
* @group ms-required
157+
*/
158+
public function test_should_keep_site_active_plugin_when_deactivating_network_wide() {
159+
deactivate_plugins( 'a/a.php', false, true );
160+
161+
$this->assertSame( array( 'a/a.php', 'b/b.php', 'c.php' ), get_option( 'active_plugins' ), 'The site activation should be kept.' );
162+
$this->assertSame(
163+
array( array( 'deactivate_plugin', array( 'a/a.php', false ) ) ),
164+
$this->calls,
165+
'Only deactivate_plugin should fire, although the plugin stays active.'
166+
);
167+
}
168+
169+
/**
170+
* Tests that a network activation is kept when deactivating on the site only.
171+
*
172+
* @ticket 65819
173+
*
174+
* @group ms-required
175+
*/
176+
public function test_should_keep_network_activation_when_deactivating_on_site() {
177+
update_site_option( 'active_sitewide_plugins', array( 'a/a.php' => 1000 ) );
178+
179+
deactivate_plugins( 'a/a.php', false, false );
180+
181+
$this->assertSame( array( 'a/a.php' => 1000 ), get_site_option( 'active_sitewide_plugins' ), 'The network activation should be kept.' );
182+
$this->assertSame( array( 'b/b.php', 'c.php' ), array_values( get_option( 'active_plugins' ) ), 'The site activation should be removed.' );
183+
}
184+
}

0 commit comments

Comments
 (0)