Skip to content

Commit e02bb57

Browse files
committed
Tests: Cover Site Health plugin version checks.
Test `WP_Site_Health::get_test_plugin_version()` for active and inactive plugins, available updates, singular and plural descriptions, update links, and multisite behavior. Developed in: #14582 Props huzaifaalmesbah. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64667 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f73eda6 commit e02bb57

1 file changed

Lines changed: 275 additions & 0 deletions

File tree

Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
<?php
2+
3+
/**
4+
* Tests for WP_Site_Health::get_test_plugin_version().
5+
*
6+
* @group admin
7+
* @group site-health
8+
* @group plugins
9+
*
10+
* @covers WP_Site_Health::get_test_plugin_version
11+
*/
12+
class Tests_Admin_wpSiteHealthGetTestPluginVersion extends WP_UnitTestCase {
13+
14+
/**
15+
* Administrator user ID.
16+
*
17+
* @var int
18+
*/
19+
private static $admin_id;
20+
21+
/**
22+
* Loads the Site Health class and sets up the user.
23+
*
24+
* @param WP_UnitTest_Factory $factory Test factory.
25+
*/
26+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
27+
require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
28+
require_once ABSPATH . 'wp-admin/includes/update.php';
29+
30+
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
31+
32+
if ( is_multisite() ) {
33+
grant_super_admin( self::$admin_id );
34+
}
35+
}
36+
37+
/**
38+
* Revokes the super admin grant before the fixture users are deleted.
39+
*/
40+
public static function wpTearDownAfterClass() {
41+
if ( is_multisite() ) {
42+
revoke_super_admin( self::$admin_id );
43+
}
44+
}
45+
46+
/**
47+
* Resets the plugins cache, the active plugins and the update transient.
48+
*/
49+
public function set_up() {
50+
parent::set_up();
51+
52+
wp_set_current_user( self::$admin_id );
53+
54+
update_option( 'active_plugins', array() );
55+
update_site_option( 'active_sitewide_plugins', array() );
56+
delete_site_transient( 'update_plugins' );
57+
}
58+
59+
/**
60+
* Clears the plugins cache and the update transient.
61+
*/
62+
public function tear_down() {
63+
wp_cache_delete( 'plugins', 'plugins' );
64+
delete_site_transient( 'update_plugins' );
65+
66+
parent::tear_down();
67+
}
68+
69+
/**
70+
* Tests the result when every installed plugin is active and up to date.
71+
*
72+
* @ticket 65819
73+
*
74+
* @dataProvider data_active_plugins
75+
*
76+
* @param int $count The number of installed, active plugins.
77+
* @param string $sentence The expected sentence.
78+
*/
79+
public function test_should_report_active_plugins_up_to_date( $count, $sentence ) {
80+
$this->set_plugins( $count, $count );
81+
82+
$this->assertSame(
83+
array(
84+
'label' => 'Your plugins are all up to date',
85+
'status' => 'good',
86+
'badge' => array(
87+
'label' => 'Security',
88+
'color' => 'blue',
89+
),
90+
'description' => '<p>Plugins extend your site&#8217;s functionality with things like contact forms, ecommerce and much more. That means they have deep access to your site, so it&#8217;s vital to keep them up to date.</p><p>' . $sentence . '</p>',
91+
'actions' => '<p><a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/plugins.php">Manage your plugins</a></p>',
92+
'test' => 'plugin_version',
93+
),
94+
$this->get_result()
95+
);
96+
}
97+
98+
/**
99+
* Data provider.
100+
*
101+
* @return array[]
102+
*/
103+
public static function data_active_plugins() {
104+
return array(
105+
'one plugin' => array( 1, 'Your site has 1 active plugin, and it is up to date.' ),
106+
'two plugins' => array( 2, 'Your site has 2 active plugins, and they are all up to date.' ),
107+
);
108+
}
109+
110+
/**
111+
* Tests the result when active plugins have updates waiting.
112+
*
113+
* @ticket 65819
114+
*
115+
* @dataProvider data_outdated_plugins
116+
*
117+
* @param int $outdated The number of plugins with an update.
118+
* @param string $sentence The expected sentence.
119+
*/
120+
public function test_should_report_outdated_plugins( $outdated, $sentence ) {
121+
$this->set_plugins( 2, 2, $outdated );
122+
123+
$result = $this->get_result();
124+
125+
$this->assertSame( 'critical', $result['status'], 'The status should be critical.' );
126+
$this->assertSame( 'You have plugins waiting to be updated', $result['label'], 'The label should match.' );
127+
$this->assertStringEndsWith( '</p><p>' . $sentence . '</p>', $result['description'], 'The description should count the outdated plugins.' );
128+
$this->assertSame(
129+
'<p><a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/plugins.php">Manage your plugins</a></p>'
130+
. '<p><a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/' . ( is_multisite() ? 'network/' : '' ) . 'plugins.php?plugin_status=upgrade">Update your plugins</a></p>',
131+
$result['actions'],
132+
'The actions should link to the plugin updates.'
133+
);
134+
}
135+
136+
/**
137+
* Data provider.
138+
*
139+
* @return array[]
140+
*/
141+
public static function data_outdated_plugins() {
142+
return array(
143+
'one outdated plugin' => array( 1, 'Your site has 1 plugin waiting to be updated.' ),
144+
'two outdated plugins' => array( 2, 'Your site has 2 plugins waiting to be updated.' ),
145+
);
146+
}
147+
148+
/**
149+
* Tests that an update for a plugin that is not installed is ignored.
150+
*
151+
* @ticket 65819
152+
*/
153+
public function test_should_ignore_updates_for_plugins_not_installed() {
154+
$this->set_plugins( 1, 1 );
155+
156+
$update_plugins = get_site_transient( 'update_plugins' );
157+
$update_plugins->response['missing/missing.php'] = (object) array(
158+
'slug' => 'missing',
159+
'new_version' => '2.0',
160+
);
161+
set_site_transient( 'update_plugins', $update_plugins );
162+
163+
$this->assertSame( 'good', $this->get_result()['status'] );
164+
}
165+
166+
/**
167+
* Tests the result when some installed plugins are inactive.
168+
*
169+
* @ticket 65819
170+
*
171+
* @group ms-excluded
172+
*
173+
* @dataProvider data_inactive_plugins
174+
*
175+
* @param int $total The number of installed plugins.
176+
* @param int $active The number of active plugins.
177+
* @param string $sentence The expected sentence about inactive plugins.
178+
*/
179+
public function test_should_recommend_removing_inactive_plugins( $total, $active, $sentence ) {
180+
$this->set_plugins( $total, $active );
181+
182+
$result = $this->get_result();
183+
184+
$this->assertSame( 'recommended', $result['status'], 'The status should be recommended.' );
185+
$this->assertSame( 'You should remove inactive plugins', $result['label'], 'The label should match.' );
186+
$this->assertStringEndsWith(
187+
'<p>' . $sentence . ' Inactive plugins are tempting targets for attackers. If you are not going to use a plugin, you should consider removing it.</p>',
188+
$result['description'],
189+
'The description should count the inactive plugins.'
190+
);
191+
$this->assertStringEndsWith(
192+
'<p><a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/plugins.php?plugin_status=inactive">Manage inactive plugins</a></p>',
193+
$result['actions'],
194+
'The actions should link to the inactive plugins.'
195+
);
196+
}
197+
198+
/**
199+
* Data provider.
200+
*
201+
* @return array[]
202+
*/
203+
public static function data_inactive_plugins() {
204+
return array(
205+
'one inactive plugin' => array( 2, 1, 'Your site has 1 inactive plugin.' ),
206+
'two inactive plugins' => array( 3, 1, 'Your site has 2 inactive plugins.' ),
207+
);
208+
}
209+
210+
/**
211+
* Tests that inactive plugins are not reported on multisite.
212+
*
213+
* @ticket 65819
214+
*
215+
* @group ms-required
216+
*/
217+
public function test_should_not_report_inactive_plugins_on_multisite() {
218+
$this->set_plugins( 2, 1 );
219+
220+
$result = $this->get_result();
221+
222+
$this->assertSame( 'good', $result['status'], 'The status should stay good.' );
223+
$this->assertStringEndsWith( '</p><p>Your site has 1 active plugin, and it is up to date.</p>', $result['description'], 'Only the active plugin should be counted, and inactive plugins should not be mentioned.' );
224+
}
225+
226+
/**
227+
* Primes the plugins cache with test plugins, activates some and adds updates.
228+
*
229+
* @param int $total The number of installed plugins.
230+
* @param int $active The number of active plugins, from the first.
231+
* @param int $outdated The number of plugins with an update, from the first.
232+
*/
233+
private function set_plugins( $total, $active, $outdated = 0 ) {
234+
$plugins = array();
235+
$response = array();
236+
237+
for ( $i = 1; $i <= $total; $i++ ) {
238+
$file = "plugin-{$i}/plugin-{$i}.php";
239+
240+
$plugins[ $file ] = array(
241+
'Name' => "Plugin {$i}",
242+
'Version' => '1.0',
243+
);
244+
245+
if ( $i <= $outdated ) {
246+
$response[ $file ] = (object) array(
247+
'slug' => "plugin-{$i}",
248+
'new_version' => '2.0',
249+
);
250+
}
251+
}
252+
253+
wp_cache_set( 'plugins', array( '' => $plugins ), 'plugins' );
254+
255+
$active_plugins = array_slice( array_keys( $plugins ), 0, $active );
256+
if ( is_multisite() ) {
257+
update_site_option( 'active_sitewide_plugins', array_fill_keys( $active_plugins, time() ) );
258+
} else {
259+
update_option( 'active_plugins', $active_plugins );
260+
}
261+
262+
set_site_transient( 'update_plugins', (object) array( 'response' => $response ) );
263+
}
264+
265+
/**
266+
* Runs the test.
267+
*
268+
* @return array The test result.
269+
*/
270+
private function get_result() {
271+
$site_health = new WP_Site_Health();
272+
273+
return $site_health->get_test_plugin_version();
274+
}
275+
}

0 commit comments

Comments
 (0)