Skip to content

Commit e67433a

Browse files
committed
Optimize the uninstall check and update tests
1 parent 9dc3fde commit e67433a

7 files changed

Lines changed: 144 additions & 170 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Class Plugin_Uninstall_Check.
4+
*
5+
* @package plugin-check
6+
*/
7+
8+
namespace WordPress\Plugin_Check\Checker\Checks\Plugin_Repo;
9+
10+
use WordPress\Plugin_Check\Checker\Check_Categories;
11+
use WordPress\Plugin_Check\Checker\Check_Result;
12+
use WordPress\Plugin_Check\Checker\Checks\Abstract_File_Check;
13+
use WordPress\Plugin_Check\Traits\Amend_Check_Result;
14+
use WordPress\Plugin_Check\Traits\Stable_Check;
15+
16+
/**
17+
* Check for plugin uninstallation.
18+
*
19+
* @since 1.6.0
20+
*/
21+
class Plugin_Uninstall_Check extends Abstract_File_Check {
22+
23+
use Amend_Check_Result;
24+
use Stable_Check;
25+
26+
/**
27+
* Gets the categories for the check.
28+
*
29+
* Every check must have at least one category.
30+
*
31+
* @since 1.6.0
32+
*
33+
* @return array The categories for the check.
34+
*/
35+
public function get_categories() {
36+
return array( Check_Categories::CATEGORY_PLUGIN_REPO );
37+
}
38+
39+
/**
40+
* Amends the given result by running the check on the given list of files.
41+
*
42+
* @since 1.6.0
43+
*
44+
* @param Check_Result $result The check result to amend, including the plugin context to check.
45+
* @param array $files List of absolute file paths.
46+
*/
47+
protected function check_files( Check_Result $result, array $files ) {
48+
$plugin_path = $result->plugin()->path();
49+
50+
$plugin_uninstall_file = $plugin_path . 'uninstall.php';
51+
52+
if ( file_exists( $plugin_uninstall_file ) ) {
53+
// Check the uninstall constant.
54+
$this->check_constant( $result, $plugin_uninstall_file );
55+
}
56+
}
57+
58+
/**
59+
* Checks the WP_UNINSTALL_PLUGIN constant in uninstall file.
60+
*
61+
* @since 1.6.0
62+
*
63+
* @param Check_Result $result The Check Result to amend.
64+
* @param string $uninstall_file Uninstall file.
65+
*/
66+
private function check_constant( Check_Result $result, string $uninstall_file ) {
67+
$constant_regex = '#defined\s*\(.*WP_UNINSTALL_PLUGIN.*\)#';
68+
$matches = array();
69+
70+
$uninstall_constant = self::file_preg_match( $constant_regex, array( $uninstall_file ), $matches );
71+
72+
if ( ! $uninstall_constant ) {
73+
$this->add_result_error_for_file(
74+
$result,
75+
sprintf(
76+
/* translators: %s: Constant name. */
77+
__( 'Check for %s constant missing.', 'plugin-check' ),
78+
'WP_UNINSTALL_PLUGIN'
79+
),
80+
'uninstall_missing_constant_check',
81+
$uninstall_file,
82+
0,
83+
0,
84+
'https://developer.wordpress.org/plugins/plugin-basics/uninstall-methods/#method-2-uninstall-php',
85+
7
86+
);
87+
}
88+
}
89+
90+
/**
91+
* Gets the description for the check.
92+
*
93+
* Every check must have a short description explaining what the check does.
94+
*
95+
* @since 1.6.0
96+
*
97+
* @return string Description.
98+
*/
99+
public function get_description(): string {
100+
return __( 'Checks related to plugin uninstallation.', 'plugin-check' );
101+
}
102+
103+
/**
104+
* Gets the documentation URL for the check.
105+
*
106+
* Every check must have a URL with further information about the check.
107+
*
108+
* @since 1.6.0
109+
*
110+
* @return string The documentation URL.
111+
*/
112+
public function get_documentation_url(): string {
113+
return __( 'https://developer.wordpress.org/plugins/plugin-basics/uninstall-methods/#method-2-uninstall-php', 'plugin-check' );
114+
}
115+
}

includes/Checker/Checks/Plugin_Repo/Plugin_Uninstall_Constant_Check.php

Lines changed: 0 additions & 104 deletions
This file was deleted.

tests/phpunit/testdata/plugins/test-plugin-uninstall-constant-errors/inside/uninstall.php

Whitespace-only changes.

tests/phpunit/testdata/plugins/test-plugin-uninstall-constant-without-errors/load.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/phpunit/testdata/plugins/test-plugin-uninstall-constant-without-errors/uninstall.php

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Tests for the Plugin_Uninstall_Check class.
4+
*
5+
* @package plugin-check
6+
*/
7+
8+
use WordPress\Plugin_Check\Checker\Check_Context;
9+
use WordPress\Plugin_Check\Checker\Check_Result;
10+
use WordPress\Plugin_Check\Checker\Checks\Plugin_Repo\Plugin_Uninstall_Check;
11+
12+
class Plugin_Uninstall_Check_Tests extends WP_UnitTestCase {
13+
14+
public function test_run_with_missing_constant_check() {
15+
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-uninstall-constant-errors/load.php' );
16+
$check_result = new Check_Result( $check_context );
17+
18+
$check = new Plugin_Uninstall_Check();
19+
$check->run( $check_result );
20+
21+
$errors = $check_result->get_errors();
22+
23+
$this->assertNotEmpty( $errors );
24+
$this->assertArrayHasKey( 'uninstall.php', $errors );
25+
26+
// Check for missing constant check.
27+
$this->assertCount( 1, wp_list_filter( $errors['uninstall.php'][0][0], array( 'code' => 'uninstall_missing_constant_check' ) ) );
28+
}
29+
}

tests/phpunit/tests/Checker/Checks/Plugin_Uninstall_Constant_Check.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)