Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature tests for checking plugin with addon enabled #518

Merged
merged 27 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8b6abd0
Add feature tests for filters
ernilambar Jul 11, 2024
697526f
Separate class files in feature tests
ernilambar Jul 11, 2024
8bc5c7b
Remove before wp load hook
ernilambar Jul 18, 2024
06a018d
Merge branch 'trunk' into add/tests-for-filters
swissspidy Jul 18, 2024
7f43fdc
Remove unused use class import
ernilambar Jul 18, 2024
4139c9d
Revert removed hook
ernilambar Jul 19, 2024
a5df7a7
Merge branch 'trunk' into add/tests-for-filters
ernilambar Jul 30, 2024
82dc502
Move tests to separate feature file
ernilambar Jul 30, 2024
9c8e5cb
Remove PHP file after CLI invoke
ernilambar Aug 20, 2024
dd7e4ba
Merge branch 'trunk' into add/tests-for-filters
ernilambar Aug 20, 2024
3b24b72
Add abstract methods implementation in checks
ernilambar Aug 20, 2024
92dabcb
Update adding hook to CLI command
ernilambar Aug 20, 2024
f37a830
Add runtime checks in the feature tests
ernilambar Aug 20, 2024
3d7d8a3
Update condition for initialization
ernilambar Aug 21, 2024
82cc334
Remove can setup condition
ernilambar Aug 21, 2024
0fbf653
Only delete drop-in if it's ours
swissspidy Aug 30, 2024
dd7827b
Rewrite tests to make them easier to comprehend
swissspidy Aug 30, 2024
90db569
Switch to `after_wp_config_load`
swissspidy Aug 30, 2024
56eb443
Update condition as per suggestion
swissspidy Aug 30, 2024
b30b02f
Don't check for WP-CLI command name
swissspidy Aug 30, 2024
16f1b31
Fix condition
swissspidy Aug 30, 2024
2a8a2a8
Disable known broken tests
swissspidy Aug 30, 2024
714b53a
Merge branch 'trunk' into add/tests-for-filters
swissspidy Sep 2, 2024
c07dc62
Define consts, run in separate process
swissspidy Sep 5, 2024
2ce4dea
Merge branch 'trunk' into add/tests-for-filters
swissspidy Sep 5, 2024
9487cfe
Merge branch 'trunk' into add/tests-for-filters
swissspidy Sep 5, 2024
10b39a9
Add explanatory comment
swissspidy Sep 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,17 @@

// Create the CLI command instance and add to WP CLI.
$plugin_command = new Plugin_Check_Command( $context );
WP_CLI::add_command( 'plugin', $plugin_command );

WP_CLI::add_command(
'plugin',
$plugin_command,
array(
'after_invoke' => function () {
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
if ( file_exists( ABSPATH . 'wp-content/object-cache.php' ) ) {
unlink( ABSPATH . 'wp-content/object-cache.php' );
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds too easy to be true 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yah, looks like it does not work :-D

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2024-08-20 at 3 21 39 PM

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But tests are passing in GH Action.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. The "Perform runtime check for multi-file plugin" scenario is also failing locally.

When I manually go to the temp directory for that WP install and run the CLI command, the runtime checks are indeed not running.

The drop-in is successfully copied though.

So something is definitely not right 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you point me to where in the Behat tests the failing call is defined? I'm not familiar with Behat at all, so not sure whether I'm looking at the right place. Is it the second plugin check execution (in https://github.com/WordPress/plugin-check/pull/518/files#diff-303aba0b6bdf4d673c51a01f6bb7b91eccfc4717908349148e02927f9f3ac223R602) specifically that's failing? Or is even the first plugin check failing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. First command run good. When same command run again, issue appears.

Copy link
Member

@felixarntz felixarntz Aug 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Can you also clarify how that relates to runtime checks? As far as I can tell, the addon checks are all non-runtime checks, so what does that have to do with runtime checks / setting up the object-cache.php drop-in? As far as I can see, the drop-in is only used in the very last execution, since only there the CLI command is run with --requireing of that cli.php file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that file is deleted manually then above issue does not appear. This is not specific to runtime checks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But isn't that file only created after the failing second run, in https://github.com/WordPress/plugin-check/pull/518/files#diff-303aba0b6bdf4d673c51a01f6bb7b91eccfc4717908349148e02927f9f3ac223R672? Since only there the --require is used, so only that should place the drop-in. 🤔

},
)
);

/**
* Adds hook to set up the object-cache.php drop-in file.
Expand Down
280 changes: 280 additions & 0 deletions tests/behat/features/plugin-check.feature
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,283 @@ Feature: Test that the WP-CLI command works.
"""
Invalid plugin slug
"""

Scenario: Check a plugin with addon enabled with extra checks
Given a WP install with the Plugin Check plugin
And a wp-content/plugins/pcp-addon/class-postsperpage-check.php file:
"""
<?php
use WordPress\Plugin_Check\Checker\Checks\Abstract_PHP_CodeSniffer_Check;
use WordPress\Plugin_Check\Traits\Stable_Check;

class PostsPerPage_Check extends Abstract_PHP_CodeSniffer_Check {

use Stable_Check;

public function get_categories() {
return array( 'new_category' );
}

protected function get_args() {
return array(
'extensions' => 'php',
'standard' => plugin_dir_path( __FILE__ ) . 'postsperpage.xml',
);
}

public function get_description(): string {
return '';
}

public function get_documentation_url(): string {
return '';
}
}
"""
And a wp-content/plugins/pcp-addon/class-prohibited-text-check.php file:
"""
<?php
use WordPress\Plugin_Check\Checker\Check_Result;
use WordPress\Plugin_Check\Checker\Checks\Abstract_File_Check;
use WordPress\Plugin_Check\Traits\Amend_Check_Result;
use WordPress\Plugin_Check\Traits\Stable_Check;

class Prohibited_Text_Check extends Abstract_File_Check {

use Amend_Check_Result;
use Stable_Check;

public function get_categories() {
return array( 'new_category' );
}

protected function check_files( Check_Result $result, array $files ) {
$php_files = self::filter_files_by_extension( $files, 'php' );
$file = self::file_preg_match( '#I\sam\sbad#', $php_files );
if ( $file ) {
$this->add_result_error_for_file(
$result,
__( 'Prohibited text found.', 'pcp-addon' ),
'prohibited_text_detected',
$file,
0,
0,
'',
8
);
}
}

public function get_description(): string {
return '';
}

public function get_documentation_url(): string {
return '';
}
}
"""

And a wp-content/plugins/pcp-addon/pcp-addon.php file:
"""
<?php
/**
* Plugin Name: PCP Addon
* Plugin URI: https://example.com
* Description: Plugin Check addon.
* Version: 0.1.0
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/performance/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Requires Plugins: plugin-check
*/

add_filter(
'wp_plugin_check_categories',
function ( array $categories ) {
return array_merge( $categories, array( 'new_category' => esc_html__( 'New Category', 'pcp-addon' ) ) );
}
);

add_filter(
'wp_plugin_check_checks',
function ( array $checks ) {
require_once plugin_dir_path( __FILE__ ) . 'class-prohibited-text-check.php';
require_once plugin_dir_path( __FILE__ ) . 'class-postsperpage-check.php';

return array_merge(
$checks,
array(
'prohibited_text' => new Prohibited_Text_Check(),
'postsperpage' => new PostsPerPage_Check(),
)
);
}
);
"""
And a wp-content/plugins/pcp-addon/postsperpage.xml file:
"""
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PCPAddon" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
<rule ref="WordPress.WP.PostsPerPage">
<type>error</type>
<severity>9</severity>
</rule>
</ruleset>
"""
And I run the WP-CLI command `plugin activate pcp-addon`
And a wp-content/plugins/foo-sample/foo-sample.php file:
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
"""
<?php
/**
* Plugin Name: Foo Sample
* Plugin URI: https://example.com
* Description: Sample plugin.
* Version: 0.1.0
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/performance/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/

add_action(
'init',
function () {
echo absint( mt_rand( 10, 100 ) );

echo 'I am bad'; // This should trigger the error.

$qargs = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1000,
'no_found_rows' => true,
);
}
);
"""

When I run the WP-CLI command `plugin list --field=name --status=active`
Then STDOUT should contain:
"""
pcp-addon
"""
And STDOUT should contain:
"""
plugin-check
"""

When I run the WP-CLI command `plugin list-checks --fields=slug,category,stability --format=csv`
Then STDOUT should contain:
"""
prohibited_text,new_category,stable
"""
And STDOUT should contain:
"""
postsperpage,new_category,stable
"""

When I run the WP-CLI command `plugin list-check-categories --fields=slug,name --format=csv`
Then STDOUT should contain:
"""
new_category,"New Category"
"""

When I run the WP-CLI command `plugin list-checks --fields=slug,category --format=csv --categories=new_category`
Then STDOUT should contain:
"""
prohibited_text,new_category
"""
And STDOUT should contain:
"""
postsperpage,new_category
"""
And STDOUT should not contain:
"""
plugin_review_phpcs,plugin_repo
"""

When I run the WP-CLI command `plugin check foo-sample --fields=code,type --format=csv`
Then STDOUT should contain:
"""
WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR
"""
And STDOUT should contain:
"""
prohibited_text_detected,ERROR
"""
And STDOUT should contain:
"""
WordPress.WP.PostsPerPage.posts_per_page_posts_per_page,ERROR
"""

When I run the WP-CLI command `plugin check foo-sample --fields=code,type --format=csv`
Then STDOUT should contain:
"""
WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR
"""
And STDOUT should contain:
"""
prohibited_text_detected,ERROR
"""
And STDOUT should contain:
"""
WordPress.WP.PostsPerPage.posts_per_page_posts_per_page,ERROR
"""

When I run the WP-CLI command `plugin check foo-sample --fields=code,type --format=csv --categories=new_category`
Then STDOUT should not contain:
"""
WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR
"""
And STDOUT should contain:
"""
prohibited_text_detected,ERROR
"""
And STDOUT should contain:
"""
WordPress.WP.PostsPerPage.posts_per_page_posts_per_page,ERROR
"""

When I run the WP-CLI command `plugin check foo-sample --fields=code,type --format=csv --categories=plugin_repo`
Then STDOUT should contain:
"""
WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR
"""
And STDOUT should not contain:
"""
prohibited_text_detected,ERROR
"""
And STDOUT should not contain:
"""
WordPress.WP.PostsPerPage.posts_per_page_posts_per_page,ERROR
"""

When I run the WP-CLI command `plugin check foo-sample --fields=code,type --format=csv --checks=postsperpage`
Then STDOUT should not contain:
"""
WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR
"""
And STDOUT should not contain:
"""
prohibited_text_detected,ERROR
"""
And STDOUT should contain:
"""
WordPress.WP.PostsPerPage.posts_per_page_posts_per_page,ERROR
"""

When I run the WP-CLI command `plugin check foo-sample --fields=code,type --format=csv --exclude-checks=postsperpage`
Then STDOUT should contain:
"""
WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR
"""
And STDOUT should contain:
"""
prohibited_text_detected,ERROR
"""
And STDOUT should not contain:
"""
WordPress.WP.PostsPerPage.posts_per_page_posts_per_page,ERROR
"""
Loading