mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Plugins: Move misplaced return
statement in wp_get_plugin_action_button()
.
#6757
Closed
costdev
wants to merge
9
commits into
WordPress:trunk
from
costdev:move_return_line_in_wp_get_plugin_action_button
Closed
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3260652
Add test to demonstrate the issue.
costdev 69d4de1
Move the return statement outside the condition branch.
costdev 0a6c1f1
Skip multisite on single-site test.
costdev fd99490
Add multisite test.
costdev f13f6de
Add ticket number.
costdev db79ee1
Swap `group` and `dataProvider` annotations.
costdev 7534c53
Differentiate single site and multisite tests.
costdev 3010506
Improve assertion accuracy for what's tested.
costdev 9f517ab
Add empty string to return annotation.
hellofromtonya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
tests/phpunit/tests/admin/includes/wpGetPluginActionButton.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?php | ||
|
||
/** | ||
* Tests for wp_get_plugin_action_button(). | ||
* | ||
* @group plugins | ||
* @group admin | ||
* | ||
* @covers ::wp_get_plugin_action_button | ||
*/ | ||
class Tests_Admin_Includes_WpGetPluginActionButton extends WP_UnitTestCase { | ||
|
||
/** | ||
* User role. | ||
* | ||
* @var WP_Role | ||
*/ | ||
private static $role; | ||
|
||
/** | ||
* User ID. | ||
* | ||
* @var int | ||
*/ | ||
private static $user_id; | ||
|
||
/** | ||
* Test plugin data. | ||
* | ||
* @var stdClass | ||
*/ | ||
private static $test_plugin; | ||
|
||
/** | ||
* Sets up properties and adds a test plugin before any tests run. | ||
*/ | ||
public static function set_up_before_class() { | ||
parent::set_up_before_class(); | ||
|
||
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; | ||
|
||
$role_name = 'wp_get_plugin_action_button-test-role'; | ||
add_role( $role_name, 'Test Role' ); | ||
|
||
self::$role = get_role( $role_name ); | ||
self::$user_id = self::factory()->user->create( array( 'role' => $role_name ) ); | ||
self::$test_plugin = (object) array( | ||
'name' => 'My Plugin', | ||
'slug' => 'my-plugin', | ||
'version' => '1.0.0', | ||
); | ||
|
||
mkdir( WP_PLUGIN_DIR . '/' . self::$test_plugin->slug ); | ||
file_put_contents( | ||
WP_PLUGIN_DIR . '/' . self::$test_plugin->slug . '/my_plugin.php', | ||
"<?php\n/**\n* Plugin Name: " . self::$test_plugin->name . "\n* Version: " . self::$test_plugin->version . "\n*/" | ||
); | ||
} | ||
|
||
/** | ||
* Removes the test plugin and its directory after all tests run. | ||
*/ | ||
public static function tear_down_after_class() { | ||
parent::tear_down_after_class(); | ||
|
||
remove_role( self::$role->name ); | ||
|
||
unlink( WP_PLUGIN_DIR . '/' . self::$test_plugin->slug . '/my_plugin.php' ); | ||
rmdir( WP_PLUGIN_DIR . '/' . self::$test_plugin->slug ); | ||
} | ||
|
||
/** | ||
* Tests that an empty string is returned when the user does not have the correct capabilities. | ||
* | ||
* @ticket 61400 | ||
*/ | ||
public function test_should_return_empty_string_without_proper_capabilities() { | ||
wp_set_current_user( self::$user_id ); | ||
|
||
$actual = wp_get_plugin_action_button( | ||
self::$test_plugin->name, | ||
self::$test_plugin, | ||
true, | ||
true | ||
); | ||
|
||
$this->assertIsString( $actual, 'A string should be returned.' ); | ||
$this->assertEmpty( $actual, 'An empty string should be returned.' ); | ||
} | ||
|
||
/** | ||
* Tests that an empty string is not returned when the user | ||
* has the correct capabilities on single site. | ||
* | ||
* @ticket 61400 | ||
* | ||
* @group ms-excluded | ||
* | ||
* @dataProvider data_capabilities | ||
* | ||
* @param string $capability The name of the capability. | ||
*/ | ||
public function test_should_not_return_empty_string_with_proper_capabilities_single_site( $capability ) { | ||
self::$role->add_cap( $capability ); | ||
|
||
wp_set_current_user( self::$user_id ); | ||
|
||
$actual = wp_get_plugin_action_button( | ||
self::$test_plugin->name, | ||
self::$test_plugin, | ||
true, | ||
true | ||
); | ||
|
||
self::$role->remove_cap( $capability ); | ||
|
||
$this->assertIsString( $actual, 'A string should be returned.' ); | ||
$this->assertNotEmpty( $actual, 'An empty string should not be returned.' ); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @return array[] | ||
*/ | ||
public function data_capabilities() { | ||
return self::text_array_to_dataprovider( array( 'install_plugins', 'update_plugins' ) ); | ||
} | ||
|
||
/** | ||
* Tests that an empty string is not returned when the user | ||
* has the correct capabilities on multisite. | ||
* | ||
* @ticket 61400 | ||
* | ||
* @group ms-required | ||
*/ | ||
public function test_should_not_return_empty_string_with_proper_capabilities_multisite() { | ||
wp_set_current_user( self::$user_id ); | ||
|
||
grant_super_admin( self::$user_id ); | ||
|
||
$actual = wp_get_plugin_action_button( | ||
self::$test_plugin->name, | ||
self::$test_plugin, | ||
true, | ||
true | ||
); | ||
|
||
revoke_super_admin( self::$user_id ); | ||
|
||
$this->assertIsString( $actual, 'A string should be returned.' ); | ||
$this->assertNotEmpty( $actual, 'An empty string should not be returned.' ); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This approach works well for the specific needs of this ticket and the issue it addresses. That said, the function under test will need additional plugin mashups (i.e. different
$data
) for its various test scenarios.It might be good to start off with a helper function that builds / creates the plugin for each test.
Is there already examples / usage within the test suite? Yes.
Example,
WP_REST_Plugins_Controller_Test::create_test_plugin()
.What do you think @costdev?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The more I think about this -> let's tackle it when adding more tests. The current approach is within scope for this ticket.