diff --git a/tests/phpunit/tests/admin/wpPluginInstallListTableDisplayRows.php b/tests/phpunit/tests/admin/wpPluginInstallListTableDisplayRows.php new file mode 100644 index 0000000000000..0f82d09eaf4bb --- /dev/null +++ b/tests/phpunit/tests/admin/wpPluginInstallListTableDisplayRows.php @@ -0,0 +1,601 @@ +user->create( array( 'role' => 'administrator' ) ); + + if ( is_multisite() ) { + grant_super_admin( self::$admin_id ); + } + } + + /** + * Revokes the super admin fixture. + */ + public static function wpTearDownAfterClass() { + if ( is_multisite() ) { + revoke_super_admin( self::$admin_id ); + } + } + + /** + * Uses a stable version, and the administrator. + */ + public function set_up() { + parent::set_up(); + + $this->original_version = $GLOBALS['wp_version']; + + $GLOBALS['wp_version'] = '7.0'; + + wp_set_current_user( self::$admin_id ); + set_site_transient( 'update_plugins', (object) array() ); + } + + /** + * Restores the version. + */ + public function tear_down() { + $GLOBALS['wp_version'] = $this->original_version; + + delete_site_transient( 'update_plugins' ); + + parent::tear_down(); + } + + /** + * Returns the data of a plugin of the directory. + * + * @param array $overrides Optional. Values that replace the defaults. Default none. + * @return array The plugin. + */ + private function get_plugin( array $overrides = array() ) { + return array_merge( + array( + 'name' => 'Contact Forms', + 'slug' => 'contact-forms', + 'version' => '2.1', + 'author' => 'Jane Doe', + 'short_description' => 'Build simple forms.', + 'requires' => '5.0', + 'requires_php' => '7.0', + 'tested' => '7.0', + 'rating' => 90, + 'num_ratings' => 1234, + 'active_installs' => 5000, + 'last_updated' => gmdate( 'Y-m-d H:i:s', time() - ( 3 * DAY_IN_SECONDS ) ), + 'icons' => array( 'default' => 'https://example.org/default.png' ), + ), + $overrides + ); + } + + /** + * Renders the rows of plugins. + * + * @param array[] $plugins The plugins. + * @param string[] $groups Optional. The names of the groups, by slug. Default none. + * @return string The output. + */ + private function render( array $plugins, array $groups = array() ) { + $table = _get_list_table( 'WP_Plugin_Install_List_Table', array( 'screen' => 'plugin-install' ) ); + $table->items = $plugins; + $table->groups = $groups; + + return get_echo( array( $table, 'display_rows' ) ); + } + + /** + * Tests that nothing is output without plugins. + * + * @ticket 66554 + */ + public function test_should_output_nothing_without_plugins() { + $this->assertSame( '', $this->render( array() ) ); + } + + /** + * Tests the card of a plugin. + * + * @ticket 66554 + */ + public function test_should_output_the_card_of_a_plugin() { + $output = $this->render( array( $this->get_plugin() ) ); + + $this->assertStringContainsString( '
', $output, 'The card should have the slug as a class.' ); + $this->assertMatchesRegularExpression( '/

\s*]*class="thickbox open-plugin-details-modal">\s*Contact Forms\s*assertStringContainsString( '

Build simple forms.

', $output, 'The description should be output.' ); + $this->assertStringContainsString( 'By
Jane Doe', $output, 'The author should be output.' ); + $this->assertStringContainsString( 'plugin-install.php?tab=plugin-information&plugin=contact-forms&TB_iframe=true&width=600&height=550', $output, 'The link to the details should be output.' ); + $this->assertStringContainsString( 'data-title="Contact Forms 2.1">More Details', $output, 'The name and the version should name the details.' ); + $this->assertStringContainsString( 'aria-label="More information about Contact Forms 2.1"', $output ); + } + + /** + * Tests that the objects of the API are shown like arrays. + * + * @ticket 66554 + */ + public function test_should_output_a_plugin_that_is_an_object() { + $plugin = (object) $this->get_plugin(); + + $this->assertStringContainsString( 'plugin-card-contact-forms', $this->render( array( $plugin ) ) ); + } + + /** + * Tests that every plugin has a card. + * + * @ticket 66554 + */ + public function test_should_output_a_card_for_each_plugin() { + $output = $this->render( + array( + $this->get_plugin(), + $this->get_plugin( + array( + 'name' => 'Seo Tools', + 'slug' => 'seo-tools', + ) + ), + ) + ); + + $this->assertSame( 2, substr_count( $output, '
forms.', + 'author' => 'Jane ', + ) + ), + ) + ); + + $this->assertStringNotContainsString( '