diff --git a/tests/phpunit/tests/admin/wpPluginsListTable.php b/tests/phpunit/tests/admin/wpPluginsListTable.php index b6b8fd3d6002d..484c5a66c572b 100644 --- a/tests/phpunit/tests/admin/wpPluginsListTable.php +++ b/tests/phpunit/tests/admin/wpPluginsListTable.php @@ -44,6 +44,13 @@ class Tests_Admin_wpPluginsListTable extends WP_UnitTestCase { ), ); + /** + * The original values of the globals that a test changed, by name. + * + * @var array + */ + private $original_globals = array(); + /** * Creates an admin user before any tests run and backs up the `$s` global. */ @@ -76,9 +83,31 @@ public function tear_down() { $s = self::$original_s; + foreach ( $this->original_globals as $name => $value ) { + if ( null === $value ) { + unset( $GLOBALS[ $name ] ); + } else { + $GLOBALS[ $name ] = $value; + } + } + parent::tear_down(); } + /** + * Sets a global that the list table reads, and restores it after the test. + * + * @param string $name The name of the global. + * @param mixed $value The value of the global. + */ + private function set_global( $name, $value ) { + if ( ! array_key_exists( $name, $this->original_globals ) ) { + $this->original_globals[ $name ] = $GLOBALS[ $name ] ?? null; + } + + $GLOBALS[ $name ] = $value; + } + /** * @ticket 42066 * @@ -410,4 +439,931 @@ public function plugins_list_filter( $plugins_list ) { return $plugins_list; } + + /** + * Calls a protected method of the list table. + * + * @param string $method The name of the method. + * @param array $args Optional. The arguments. Default empty array. + * @return mixed The return value of the method. + */ + private function call_protected( $method, array $args = array() ) { + $reflection = new ReflectionMethod( $this->table, $method ); + if ( PHP_VERSION_ID < 80100 ) { + $reflection->setAccessible( true ); + } + + return $reflection->invokeArgs( $this->table, $args ); + } + + /** + * Returns the output of the extra table navigation. + * + * @param string $which The position of the navigation, `top` or `bottom`. + * @return string The output. + */ + private function get_extra_tablenav( $which ) { + ob_start(); + $this->call_protected( 'extra_tablenav', array( $which ) ); + + return ob_get_clean(); + } + + /** + * Renders a row of a plugin with the given columns. + * + * @param string $plugin_file The plugin file. + * @param array $plugin_data The data of the plugin. + * @param array $columns Optional. The columns, by name. Default `name` only. + * @param array $hidden Optional. The names of the hidden columns. Default none. + * @return string The output of the row. + */ + private function render_row( $plugin_file, array $plugin_data, array $columns = array( 'name' => 'Plugin' ), array $hidden = array() ) { + $status = $GLOBALS['status'] ?? 'all'; + $page = $GLOBALS['page'] ?? 1; + + $list_table = $this->getMockBuilder( 'WP_Plugins_List_Table' ) + ->setMethods( array( 'get_column_info' ) ) + ->getMock(); + + // The constructor sets these globals from the request. + $GLOBALS['status'] = $status; + $GLOBALS['page'] = $page; + + $list_table->method( 'get_column_info' )->willReturn( array( $columns, $hidden, array(), 'name' ) ); + + return get_echo( + array( $list_table, 'single_row' ), + array( + array( + $plugin_file, + array_merge( + array( + 'Name' => 'Row Plugin', + 'Description' => 'A plugin.', + 'Version' => '1.0.0', + 'Author' => 'An author', + 'AuthorURI' => '', + 'PluginURI' => '', + ), + $plugin_data + ), + ), + ) + ); + } + + /** + * Tests that the current search term is added to the request URI. + * + * @ticket 66552 + * + * @covers WP_Plugins_List_Table::__construct + */ + public function test_construct_should_add_the_search_term_to_the_request_uri() { + $request_uri = $_SERVER['REQUEST_URI']; + $search = $_REQUEST['s'] ?? null; + + $_SERVER['REQUEST_URI'] = '/wp-admin/plugins.php'; + $_REQUEST['s'] = 'my \\"plugin\\"'; + + try { + _get_list_table( 'WP_Plugins_List_Table', array( 'screen' => 'plugins' ) ); + $query = wp_parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY ); + } finally { + $_SERVER['REQUEST_URI'] = $request_uri; + + if ( null === $search ) { + unset( $_REQUEST['s'] ); + } else { + $_REQUEST['s'] = $search; + } + } + + parse_str( $query, $args ); + + $this->assertSame( 'my "plugin"', $args['s'], 'The search term should be added without slashes.' ); + } + + /** + * Tests that the capability to activate plugins is required for Ajax requests. + * + * @ticket 66552 + * + * @covers WP_Plugins_List_Table::ajax_user_can + */ + public function test_ajax_user_can_should_require_the_activate_plugins_capability() { + wp_set_current_user( self::factory()->user->create( array( 'role' => 'subscriber' ) ) ); + + $this->assertFalse( $this->table->ajax_user_can(), 'A user without the capability should not be allowed.' ); + + // Administrators cannot activate plugins on a network, but super admins can. + $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + + if ( is_multisite() ) { + grant_super_admin( $admin_id ); + } + + wp_set_current_user( $admin_id ); + + try { + $this->assertTrue( $this->table->ajax_user_can(), 'A user with the capability should be allowed.' ); + } finally { + if ( is_multisite() ) { + revoke_super_admin( $admin_id ); + } + } + } + + /** + * Tests the classes of the table. + * + * @ticket 66552 + * + * @covers WP_Plugins_List_Table::get_table_classes + */ + public function test_get_table_classes_should_return_the_classes_of_the_plugins_table() { + $this->assertSame( array( 'widefat', 'plugins' ), $this->call_protected( 'get_table_classes' ) ); + } + + /** + * Tests the primary column. + * + * @ticket 66552 + * + * @covers WP_Plugins_List_Table::get_primary_column_name + */ + public function test_get_primary_column_name_should_be_the_plugin_name() { + $this->assertSame( 'name', $this->call_protected( 'get_primary_column_name' ) ); + } + + /** + * Tests the default columns. + * + * @ticket 66552 + * + * @covers WP_Plugins_List_Table::get_columns + */ + public function test_get_columns_should_return_the_default_columns() { + add_filter( 'plugins_auto_update_enabled', '__return_false' ); + + $this->assertSame( + array( + 'cb' => '', + 'name' => 'Plugin', + 'description' => 'Description', + ), + _get_list_table( 'WP_Plugins_List_Table', array( 'screen' => 'plugins' ) )->get_columns() + ); + } + + /** + * Tests that the column for the automatic updates is added after the description. + * + * The 'ms-excluded' group is added as the column is only shown in the network admin on multisite. + * + * @ticket 66552 + * + * @group ms-excluded + * + * @covers WP_Plugins_List_Table::get_columns + */ + public function test_get_columns_should_add_the_autoupdates_column() { + add_filter( 'plugins_auto_update_enabled', '__return_true' ); + wp_set_current_user( self::$admin_id ); + + $columns = _get_list_table( 'WP_Plugins_List_Table', array( 'screen' => 'plugins' ) )->get_columns(); + + $this->assertSame( array( 'cb', 'name', 'description', 'auto-updates' ), array_keys( $columns ), 'The column should be added last.' ); + $this->assertSame( 'Automatic Updates', $columns['auto-updates'] ); + } + + /** + * Tests which plugins match a search term. + * + * @ticket 66552 + * + * @covers WP_Plugins_List_Table::_search_callback + * + * @dataProvider data_search_callback + * + * @param string $search The search term, as the `$s` global holds it. + * @param array $plugin The data of the plugin. + * @param bool $expected Whether the plugin should match. + */ + public function test_search_callback_should_match_the_values_of_the_plugin( $search, array $plugin, $expected ) { + $this->set_global( 's', $search ); + + $this->assertSame( $expected, $this->table->_search_callback( $plugin ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_search_callback() { + $plugin = array( + 'Name' => 'Contact Forms', + 'Description' => 'Build simple forms.', + 'Author' => 'Jane Doe', + 'Tags' => array( 'seo' ), + 'Network' => false, + ); + + return array( + 'the name' => array( 'Contact', $plugin, true ), + 'a part of the name' => array( 'tact for', $plugin, true ), + 'any case' => array( 'CONTACT forms', $plugin, true ), + 'the description without markup' => array( 'simple forms', $plugin, true ), + 'the author' => array( 'jane', $plugin, true ), + 'an encoded term' => array( 'jane+doe', $plugin, true ), + 'an encoded space' => array( 'contact%20forms', $plugin, true ), + 'the markup of the description' => array( 'strong', $plugin, false ), + 'a value that is not a string' => array( 'seo', $plugin, false ), + 'a term that is not in the data' => array( 'newsletter', $plugin, false ), + 'a plugin without data' => array( 'contact', array(), false ), + ); + } + + /** + * Tests the views of a status that has no label of its own. + * + * @ticket 66552 + * + * @covers WP_Plugins_List_Table::get_views + */ + public function test_get_views_should_use_the_status_as_the_label_of_other_statuses() { + $this->set_global( 'totals', array( 'custom' => 1234 ) ); + $this->set_global( 'status', 'all' ); + + $this->assertSame( + array( 'custom' => 'custom (1,234)' ), + $this->table->get_views() + ); + } + + /** + * Tests that the label of a status can be filtered. + * + * @ticket 66552 + * + * @covers WP_Plugins_List_Table::get_views + */ + public function test_get_views_should_use_the_label_of_the_plugins_list_status_text_filter() { + $this->set_global( 'totals', array( 'custom' => 3 ) ); + $this->set_global( 'status', 'custom' ); + + $filter = new MockAction(); + add_filter( 'plugins_list_status_text', array( $filter, 'filter' ), 10, 3 ); + add_filter( + 'plugins_list_status_text', + static function () { + return 'My status'; + }, + 11 + ); + + $views = $this->table->get_views(); + $args = $filter->get_args(); + + $this->assertSame( array( '', 3, 'custom' ), $args[0], 'The filter should receive the label, the count and the status.' ); + $this->assertSame( + array( 'custom' => 'My <b>status</b> (3)' ), + $views, + 'The label should be escaped.' + ); + } + + /** + * Tests that a label that is not a string is replaced with the status. + * + * @ticket 66552 + * + * @covers WP_Plugins_List_Table::get_views + */ + public function test_get_views_should_ignore_a_label_that_is_not_a_string() { + $this->set_global( 'totals', array( 'custom' => 3 ) ); + $this->set_global( 'status', 'all' ); + + add_filter( + 'plugins_list_status_text', + static function () { + return array( 'not a string' ); + } + ); + + $this->assertSame( + array( 'custom' => 'custom (3)' ), + $this->table->get_views() + ); + } + + /** + * Tests that statuses without plugins and the search are not listed. + * + * @ticket 66552 + * + * @covers WP_Plugins_List_Table::get_views + */ + public function test_get_views_should_skip_statuses_without_plugins_and_the_search() { + $this->set_global( + 'totals', + array( + 'all' => 2, + 'active' => 0, + 'search' => 1, + 'inactive' => 2, + ) + ); + $this->set_global( 'status', 'all' ); + + $this->assertSame( array( 'all', 'inactive' ), array_keys( $this->table->get_views() ) ); + } + + /** + * Tests the bulk actions that are not shown for Must-Use plugins and drop-ins. + * + * @ticket 66552 + * + * @covers WP_Plugins_List_Table::bulk_actions + * + * @dataProvider data_status_mustuse_and_dropins + * + * @param string $status The plugin status. + */ + public function test_bulk_actions_should_output_nothing_for_mustuse_and_dropins( $status ) { + $this->set_global( 'status', $status ); + + $this->assertSame( '', get_echo( array( $this->table, 'bulk_actions' ), array( 'top' ) ) ); + } + + /** + * Tests that the bulk actions are shown for the other statuses. + * + * @ticket 66552 + * + * @covers WP_Plugins_List_Table::bulk_actions + */ + public function test_bulk_actions_should_output_the_actions_for_other_statuses() { + $this->set_global( 'status', 'all' ); + wp_set_current_user( self::$admin_id ); + + $table = _get_list_table( 'WP_Plugins_List_Table', array( 'screen' => 'plugins' ) ); + + $output = get_echo( array( $table, 'bulk_actions' ), array( 'top' ) ); + + $this->assertStringContainsString( '