Making WordPress.org

Ticket #1614: wp-org-plugin-api-test-v3.diff

File wp-org-plugin-api-test-v3.diff, 13.6 KB (added by enej, 8 years ago)

v3 added more tests and assertions

  • src/wp-includes/class-wp-comment.php

     
    200200
    201201                if ( ! $_comment ) {
    202202                        $_comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id ) );
    203 
    204203                        if ( ! $_comment ) {
    205204                                return false;
    206205                        }
  • tests/phpunit/tests/wporg-plugin-api.php

     
     1<?php
     2
     3require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
     4
     5/**
     6 *
     7 * @group plugins-api
     8 */
     9class Tests_Plugins_API extends WP_UnitTestCase {
     10
     11    public function setUp() {
     12        parent::setup();
     13    }
     14
     15    function test_wporg_plugin_api_serialize_php() {
     16        $response    = wp_remote_post( 'http://api.wordpress.org/plugins/info/1.0/', array( 'body' => array(
     17                'action' => 'plugin_information',
     18                'timeout' => 15,
     19                'request' => serialize( (object) array( 'slug' => 'jetpack' ) ),
     20        ) ) );
     21        $plugin_info = maybe_unserialize( wp_remote_retrieve_body( $response ) );
     22
     23        $this->_check_response_attributes( $plugin_info );
     24    }
     25
     26    function test_wporg_plugin_api_serialize_php_get() {
     27        $response    = wp_remote_get( 'http://api.wordpress.org/plugins/info/1.0/jetpack' );
     28        $plugin_info = maybe_unserialize( wp_remote_retrieve_body( $response ) );
     29
     30        $this->_check_response_attributes( $plugin_info );
     31    }
     32
     33    function test_wporg_plugin_api_json() {
     34        $response    = wp_remote_get( 'http://api.wordpress.org/plugins/info/1.0/jetpack.json' );
     35        $plugin_info = (object) json_decode( wp_remote_retrieve_body( $response ), true );
     36
     37        $this->_check_response_attributes( $plugin_info );
     38    }
     39
     40    function test_plugins_api_function_action_plugin_information() {
     41        $plugin_info = plugins_api( 'plugin_information', array( 'slug' => 'jetpack' ) );
     42        $this->_check_response_attributes( $plugin_info );
     43    }
     44
     45    function test_plugins_api_function_action_query_plugins_search() {
     46        $per_page = 4;
     47        $page = 2;
     48        $plugin_query = plugins_api( 'query_plugins', array( 'search' => 'hello-dolly', 'per_page' => $per_page, 'page' => $page ) );
     49        // Info.
     50        $this->_check_response_plugin_query( $plugin_query, $per_page, $page );
     51    }
     52
     53    function test_plugins_api_function_action_query_plugins_tag() {
     54        $plugin_query = plugins_api( 'query_plugins', array( 'tag' => 'widget' ) );
     55        // Info.
     56        $this->_check_response_plugin_query( $plugin_query );
     57    }
     58
     59    function test_plugins_api_function_action_query_plugins_author() {
     60        $plugin_query = plugins_api( 'query_plugins', array( 'author' => 'wordpressdotorg' ) );
     61        // Info.
     62        $this->_check_response_plugin_query( $plugin_query );
     63    }
     64
     65    function test_plugins_api_function_action_query_plugins_user() {
     66        $plugin_query = plugins_api( 'query_plugins', array( 'user' => 'markjaquith' ) );
     67        // Info.
     68        $this->_check_response_plugin_query( $plugin_query );
     69    }
     70
     71    function test_plugins_api_function_action_query_plugins_browse() {
     72        $plugin_query = plugins_api( 'query_plugins', array( 'browse' => 'popular' ) );
     73        // Info.
     74        $this->_check_response_plugin_query( $plugin_query );
     75    }
     76
     77    function test_plugins_api_function_action_query_plugins_installed_plugins() {
     78        $plugin_query = plugins_api( 'query_plugins', array( 'installed_plugins' => array( 'jetpack' ) ) );
     79        // Info.
     80        $this->_check_response_plugin_query( $plugin_query );
     81    }
     82
     83    function test_plugins_api_function_action_query_plugins_fields() {
     84        $opposite_of_default_fields = array(
     85                'short_description' => false,
     86                'description' => true,
     87                'sections' => true,
     88                'tested' => false,
     89                'requires' => false,
     90                'rating' => false,
     91                'ratings' => false,
     92                'downloaded' => false,
     93                'downloadlink' => false,
     94                'last_updated' => false,
     95                'added' => false,
     96                'tags' => false,
     97                'compatibility' => false,
     98                'homepage' => false,
     99                'versions' => true,
     100                'donate_link' => false,
     101                'reviews' => true,
     102                'banners' => true,
     103                'icons' => true,
     104                'active_installs' => true,
     105                // 'group' => true,
     106                'contributors' => true
     107        );
     108        $plugin_query = plugins_api( 'query_plugins', array( 'browse' => 'popular', 'fields' => $opposite_of_default_fields ) );
     109
     110        // Info.
     111        $this->_check_response_plugin_query_fields( $plugin_query, $opposite_of_default_fields );
     112    }
     113
     114    function test_plugins_api_function_action_query_plugins_local() {
     115        $plugin_query = plugins_api( 'query_plugins', array( 'local' => 'hello' ) );
     116        $this->_check_response_plugin_query( $plugin_query );
     117    }
     118
     119    function test_plugins_api_function_action_hot_categories() {
     120        $number = 2;
     121        $plugin_query_hot_categories = plugins_api( 'hot_categories', array( 'number' => $number ) );
     122
     123        // $this->assertEquals( $number, count( $plugin_query_hot_categories ) );
     124        $this->assertInternalType( 'array', $plugin_query_hot_categories, 'Response array is array' );
     125        foreach ( $plugin_query_hot_categories as $hot_category => $category_array ) {
     126                $this->assertInternalType( 'array', $category_array, 'Category array is array' );
     127                $this->assertArrayHasKey( 'name', $category_array, 'Name exists' );
     128                $this->assertArrayHasKey( 'slug', $category_array, 'Slug exists' );
     129
     130                // Only check the first result.
     131                break;
     132        }
     133    }
     134
     135    function test_plugins_api_function_action_hot_tags() {
     136        $number = 3;
     137        $plugin_query_hot_tags = plugins_api( 'hot_tags',  array( 'number' => $number ) );
     138
     139        $this->assertEquals( $number, count( $plugin_query_hot_tags ) );
     140        $this->assertInternalType( 'array', $plugin_query_hot_tags, 'Response array is array' );
     141        foreach ( $plugin_query_hot_tags as $hot_tag => $tag_array ) {
     142                $this->assertInternalType( 'array', $tag_array, 'Tag array is array' );
     143                $this->assertArrayHasKey( 'name', $tag_array, 'Name exists' );
     144                $this->assertArrayHasKey( 'slug', $tag_array, 'Slug exists' );
     145                $this->assertArrayHasKey( 'count', $tag_array, 'Count exists' );
     146
     147                // Only check the first result.
     148                break;
     149        }
     150    }
     151
     152    function _check_response_attributes( $plugin_info ) {
     153        $this->assertObjectHasAttribute( 'name', $plugin_info, 'Name exists' );
     154        $this->assertObjectHasAttribute( 'slug', $plugin_info, 'Slug exits' );
     155        $this->assertObjectHasAttribute( 'version', $plugin_info, 'Version exists' );
     156        $this->assertObjectHasAttribute( 'author', $plugin_info, 'Author exists' );
     157        $this->assertObjectHasAttribute( 'author_profile', $plugin_info, 'Author Profile exists' );
     158        $this->assertObjectHasAttribute( 'contributors', $plugin_info, 'Contributors exists' );
     159        $this->assertAttributeInternalType( 'array', 'contributors', $plugin_info, 'Contributors should be an array' );
     160        $this->assertObjectHasAttribute( 'requires', $plugin_info, 'Requires exists' );
     161        $this->assertObjectHasAttribute( 'tested', $plugin_info, 'Tested exists' );
     162        $this->assertObjectHasAttribute( 'compatibility', $plugin_info, 'Compatibility exists' );
     163        $this->assertAttributeInternalType( 'array', 'compatibility', $plugin_info, 'Compatibility should be an array' );
     164
     165        // Ratings.
     166        $this->assertObjectHasAttribute( 'rating', $plugin_info, 'Rating exists' );
     167        $this->assertObjectHasAttribute( 'ratings', $plugin_info, 'Ratings exists' );
     168        $this->assertAttributeInternalType( 'array', 'ratings', $plugin_info, 'Ratings should be an array' );
     169        $this->assertArrayHasKey( '1', $plugin_info->ratings, 'Rating should have an attribute of 1' );
     170        $this->assertArrayHasKey( '2', $plugin_info->ratings, 'Rating should have an attribute of 2' );
     171        $this->assertArrayHasKey( '3', $plugin_info->ratings, 'Rating should have an attribute of 3' );
     172        $this->assertArrayHasKey( '4', $plugin_info->ratings, 'Rating should have an attribute of 4' );
     173        $this->assertArrayHasKey( '5', $plugin_info->ratings, 'Rating should have an attribute of 5' );
     174        $this->assertObjectHasAttribute( 'num_ratings', $plugin_info, 'Num ratings exists' );
     175        $this->assertTrue( is_numeric( $plugin_info->num_ratings ), 'Num ratings are numeric' );
     176
     177        // Downloaded.
     178        $this->assertObjectHasAttribute( 'downloaded', $plugin_info, 'Downloaded exists' );
     179        $this->assertTrue( is_numeric( $plugin_info->downloaded ), 'Downloaded count is numeric' );
     180
     181        $this->assertObjectHasAttribute( 'last_updated', $plugin_info, 'last_updated exists' );
     182        $this->assertObjectHasAttribute( 'added', $plugin_info, 'Added exists' );
     183        $this->assertObjectHasAttribute( 'homepage', $plugin_info, 'Homepage exists' );
     184        $this->assertObjectHasAttribute( 'sections', $plugin_info, 'Sections Exists' );
     185        $this->assertAttributeInternalType( 'array', 'sections', $plugin_info, 'Sections should be an array' );
     186
     187        // Download link.
     188        $this->assertObjectHasAttribute( 'download_link', $plugin_info, 'Download Link exists' );
     189
     190        // Donate link.
     191        $this->assertObjectHasAttribute( 'donate_link', $plugin_info, 'Donate link exists' );
     192
     193        // Tags.
     194        $this->assertObjectHasAttribute( 'tags', $plugin_info, 'Tags exists' );
     195        $this->assertAttributeInternalType( 'array', 'tags', $plugin_info, 'Tags should be an array' );
     196    }
     197
     198    function _check_response_plugin_query( $plugin_query, $per_page = 24, $page = 1 ) {
     199        $this->assertObjectHasAttribute( 'info', $plugin_query, 'Info exists' );
     200        $info = $plugin_query->info;
     201
     202        $this->assertArrayHasKey( 'page', $info, 'Page exists' );
     203        $this->assertEquals( $info['page'], $page, 'Page equals to ' . $page );
     204
     205        $this->assertArrayHasKey( 'pages', $info, 'Pages exists' );
     206        $this->assertArrayHasKey( 'results', $info, 'Results exists' );
     207
     208        // Plugins.
     209        $this->assertObjectHasAttribute( 'plugins', $plugin_query, 'Plugins exists' );
     210        $this->assertAttributeInternalType( 'array', 'plugins', $plugin_query, 'Plugins should be an array' );
     211
     212        $this->greaterThanOrEqual(  count( $plugin_query->plugins ), $per_page );
     213
     214        $plugin_info = $plugin_query->plugins[0];
     215        $this->assertObjectHasAttribute( 'name', $plugin_info, 'Name exists' );
     216        $this->assertObjectHasAttribute( 'slug', $plugin_info, 'Slug exits' );
     217        $this->assertObjectHasAttribute( 'version', $plugin_info, 'Version exists' );
     218        $this->assertObjectHasAttribute( 'author', $plugin_info, 'Author exists' );
     219        $this->assertObjectHasAttribute( 'author_profile', $plugin_info, 'Author Profile exists' );
     220        $this->assertObjectHasAttribute( 'contributors', $plugin_info, 'Contributors exists' );
     221        $this->assertAttributeInternalType( 'array', 'contributors', $plugin_info, 'Contributors should be an array' );
     222        $this->assertObjectHasAttribute( 'requires', $plugin_info, 'Requires exists' );
     223        $this->assertObjectHasAttribute( 'tested', $plugin_info, 'Tested exists' );
     224        $this->assertObjectHasAttribute( 'compatibility', $plugin_info, 'Compatibility exists' );
     225        $this->assertAttributeInternalType( 'array', 'compatibility', $plugin_info, 'Compatibility should be an array' );
     226
     227        // Ratings.
     228        $this->assertObjectHasAttribute( 'rating', $plugin_info, 'Rating exists' );
     229        $this->assertObjectHasAttribute( 'ratings', $plugin_info, 'Ratings exists' );
     230        $this->assertAttributeInternalType( 'array', 'ratings', $plugin_info, 'Ratings should be an array' );
     231        $this->assertArrayHasKey( '1', $plugin_info->ratings, 'Rating should have an attribute of 1' );
     232        $this->assertArrayHasKey( '2', $plugin_info->ratings, 'Rating should have an attribute of 2' );
     233        $this->assertArrayHasKey( '3', $plugin_info->ratings, 'Rating should have an attribute of 3' );
     234        $this->assertArrayHasKey( '4', $plugin_info->ratings, 'Rating should have an attribute of 4' );
     235        $this->assertArrayHasKey( '5', $plugin_info->ratings, 'Rating should have an attribute of 5' );
     236        $this->assertObjectHasAttribute( 'num_ratings', $plugin_info, 'Num ratings exists' );
     237        $this->assertTrue( is_numeric( $plugin_info->num_ratings ), 'Num ratings are numeric' );
     238
     239
     240        $this->assertObjectHasAttribute( 'homepage', $plugin_info, 'Homepage exists' );
     241
     242        $this->assertObjectHasAttribute( 'description', $plugin_info, 'Description exists' );
     243        $this->assertObjectHasAttribute( 'short_description', $plugin_info, 'Short Description exists' );
     244    }
     245
     246    function _check_response_plugin_query_fields( $plugin_query, $fields ) {
     247                $plugin_info = $plugin_query->plugins[0];
     248                // error_log( json_encode( $plugin_info ) );
     249                foreach( $fields as $field => $set ) {
     250                        if( $field == 'reviews' ) {
     251                                $this->assertArrayHasKey( $field, $plugin_info->sections, $field . ' exists' );
     252                                continue;
     253                        }
     254
     255                        if ( $set ) {
     256                                $this->assertObjectHasAttribute( $field, $plugin_info, $field . ' exists' );
     257                        } else {
     258                                $this->assertObjectNotHasAttribute( $field, $plugin_info, $field . ' doesn\'t exists' );
     259                        }
     260                }
     261    }
     262}