diff --git a/tests/phpunit/tests/admin/wpPluginInstallListTableViews.php b/tests/phpunit/tests/admin/wpPluginInstallListTableViews.php new file mode 100644 index 0000000000000..988f117895ebb --- /dev/null +++ b/tests/phpunit/tests/admin/wpPluginInstallListTableViews.php @@ -0,0 +1,364 @@ +original['globals'][ $name ] = $GLOBALS[ $name ] ?? null; + } + + $this->original['request'] = $_REQUEST; + $this->original['request_uri'] = $_SERVER['REQUEST_URI'] ?? null; + + $_REQUEST = array(); + $_SERVER['REQUEST_URI'] = '/wp-admin/plugin-install.php'; + } + + /** + * Restores the globals and the request URI. + */ + public function tear_down() { + foreach ( $this->original['globals'] as $name => $value ) { + $GLOBALS[ $name ] = $value; + } + + $_REQUEST = $this->original['request']; + + if ( null === $this->original['request_uri'] ) { + unset( $_SERVER['REQUEST_URI'] ); + } else { + $_SERVER['REQUEST_URI'] = $this->original['request_uri']; + } + + delete_site_transient( 'update_plugins' ); + + parent::tear_down(); + } + + /** + * Returns a new table. + * + * @return WP_Plugin_Install_List_Table The table. + */ + private function get_table() { + return _get_list_table( 'WP_Plugin_Install_List_Table', array( 'screen' => 'plugin-install' ) ); + } + + /** + * Calls a protected method of a table. + * + * @param WP_Plugin_Install_List_Table $table The table. + * @param string $method The name of the method. + * @return mixed The return value of the method. + */ + private function call_protected( $table, $method ) { + $reflection = new ReflectionMethod( $table, $method ); + if ( PHP_VERSION_ID < 80100 ) { + $reflection->setAccessible( true ); + } + + return $reflection->invoke( $table ); + } + + /** + * Returns the data of a plugin of the directory. + * + * @return array The plugin. + */ + private function get_plugin() { + return array( + 'name' => 'My Plugin', + 'slug' => 'my-plugin', + 'version' => '1.0', + 'author' => 'Jane Doe', + 'short_description' => 'Does things.', + 'rating' => 80, + 'num_ratings' => 10, + 'active_installs' => 1000, + 'last_updated' => gmdate( 'Y-m-d H:i:s', time() - DAY_IN_SECONDS ), + 'icons' => array( 'default' => 'https://example.org/default.png' ), + ); + } + + /** + * Tests that the capability to install plugins is required for Ajax requests. + * + * @ticket 66554 + * + * @covers WP_Plugin_Install_List_Table::ajax_user_can + */ + public function test_ajax_user_can_should_require_the_install_plugins_capability() { + $table = $this->get_table(); + + wp_set_current_user( self::factory()->user->create( array( 'role' => 'subscriber' ) ) ); + + $this->assertFalse( $table->ajax_user_can(), 'A user without the capability should not be allowed.' ); + + $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + + wp_set_current_user( $admin_id ); + + if ( is_multisite() ) { + $this->assertFalse( $table->ajax_user_can(), 'A site administrator should not be allowed on a network.' ); + + // Only super admins can install plugins on a network. + grant_super_admin( $admin_id ); + } + + try { + $this->assertTrue( $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 66554 + * + * @covers WP_Plugin_Install_List_Table::get_table_classes + */ + public function test_get_table_classes_should_return_the_classes_of_the_plugin_install_table() { + $this->assertSame( array( 'widefat', 'plugin-install' ), $this->call_protected( $this->get_table(), 'get_table_classes' ) ); + } + + /** + * Tests that the table has no columns. + * + * @ticket 66554 + * + * @covers WP_Plugin_Install_List_Table::get_columns + */ + public function test_get_columns_should_be_empty() { + $this->assertSame( array(), $this->get_table()->get_columns() ); + } + + /** + * Tests the slugs of the installed plugins. + * + * @ticket 66554 + * + * @covers WP_Plugin_Install_List_Table::get_installed_plugin_slugs + */ + public function test_get_installed_plugin_slugs_should_list_the_slugs_of_the_plugins_with_and_without_updates() { + set_site_transient( + 'update_plugins', + (object) array( + 'no_update' => array( + 'a/a.php' => (object) array( 'slug' => 'a' ), + 'no-slug/no-slug.php' => (object) array( 'plugin' => 'no-slug/no-slug.php' ), + ), + 'response' => array( 'b/b.php' => (object) array( 'slug' => 'b' ) ), + ) + ); + + $this->assertSame( array( 'a', 'b' ), $this->call_protected( $this->get_table(), 'get_installed_plugin_slugs' ) ); + } + + /** + * Tests that there are no slugs without plugins. + * + * @ticket 66554 + * + * @covers WP_Plugin_Install_List_Table::get_installed_plugin_slugs + */ + public function test_get_installed_plugin_slugs_should_be_empty_without_plugin_information() { + $this->assertSame( array(), $this->call_protected( $this->get_table(), 'get_installed_plugin_slugs' ) ); + } + + /** + * Tests the views of the tabs. + * + * @ticket 66554 + * + * @covers WP_Plugin_Install_List_Table::get_views + */ + public function test_get_views_should_link_the_tabs_and_leave_out_the_upload_tab() { + $GLOBALS['tabs'] = array( + 'featured' => 'Featured', + 'popular' => 'Popular', + 'upload' => 'Upload Plugin', + ); + $GLOBALS['tab'] = 'popular'; + + $views = $this->get_table()->get_views(); + + $this->assertSame( array( 'plugin-install-featured', 'plugin-install-popular' ), array_keys( $views ), 'The upload tab should not be listed.' ); + $this->assertSame( 'Featured', $views['plugin-install-featured'] ); + $this->assertSame( 'Popular', $views['plugin-install-popular'], 'The current tab should be marked.' ); + } + + /** + * Tests the introduction, the tabs and the search form above the table. + * + * @ticket 66554 + * + * @covers WP_Plugin_Install_List_Table::views + */ + public function test_views_should_output_the_introduction_the_tabs_and_the_search_form() { + $GLOBALS['tabs'] = array( + 'featured' => 'Featured', + 'popular' => 'Popular', + ); + $GLOBALS['tab'] = 'featured'; + + $output = get_echo( array( $this->get_table(), 'views' ) ); + + $this->assertStringContainsString( '
Plugins extend and expand the functionality of WordPress. You may install plugins from the WordPress Plugin Directory', $output, 'The introduction should be output.' ); + $this->assertStringContainsString( '