Making WordPress.org

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

File wp-org-plugin-api-test-v2.diff, 17.4 KB (added by enej, 9 years ago)

Added tests for plugins_api function and the different actions that you can pass into it.

  • 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 wporg-plugin-api
     8 */
     9class Tests_Wporg_Plugin_API extends WP_UnitTestCase {
     10
     11        public function setUp() {
     12                parent::setup();
     13        }
     14
     15        function test_wp_org_plugin_api_serialize_php() {
     16                $args        = (object) array( 'slug' => 'hello-dolly' );
     17                $request     = array( 'action' => 'plugin_information', 'timeout' => 15, 'request' => serialize( $args ) );
     18                $url         = 'http://api.wordpress.org/plugins/info/1.0/';
     19                $response    = wp_remote_post( $url, array( 'body' => $request ) );
     20                $plugin_info = unserialize( $response['body'] );
     21
     22                $this->assertObjectHasAttribute( 'name', $plugin_info, 'Name exists' );
     23                $this->assertObjectHasAttribute( 'slug', $plugin_info, 'Slug exits' );
     24                $this->assertObjectHasAttribute( 'version', $plugin_info, 'Version exists' );
     25                $this->assertObjectHasAttribute( 'author', $plugin_info, 'Author exists' );
     26                $this->assertObjectHasAttribute( 'author_profile', $plugin_info, 'Author Profile exists' );
     27                $this->assertObjectHasAttribute( 'contributors', $plugin_info, 'Contributors exists' );
     28                $this->assertTrue( is_array( $plugin_info->contributors ), 'Contributors should be an array' );
     29                $this->assertObjectHasAttribute( 'requires', $plugin_info, 'Requires exists' );
     30                $this->assertObjectHasAttribute( 'tested', $plugin_info, 'Tested exists' );
     31                $this->assertObjectHasAttribute( 'compatibility', $plugin_info, 'Compatibility exists' );
     32                $this->assertTrue( is_array( $plugin_info->compatibility ), 'Compatibility should be an array' );
     33
     34                // Ratings
     35                $this->assertObjectHasAttribute( 'ratings', $plugin_info, 'Ratings exists' );
     36                $this->assertTrue( is_array( $plugin_info->ratings ), 'Ratings should be an array' );
     37                $this->assertArrayHasKey( '1', $plugin_info->ratings, 'Rating should have an attribute of 1' );
     38                $this->assertArrayHasKey( '2', $plugin_info->ratings, 'Rating should have an attribute of 2' );
     39                $this->assertArrayHasKey( '3', $plugin_info->ratings, 'Rating should have an attribute of 3' );
     40                $this->assertArrayHasKey( '4', $plugin_info->ratings, 'Rating should have an attribute of 4' );
     41                $this->assertArrayHasKey( '5', $plugin_info->ratings, 'Rating should have an attribute of 5' );
     42                $this->assertObjectHasAttribute( 'num_ratings', $plugin_info, 'Num ratings exists' );
     43                $this->assertTrue( is_numeric( $plugin_info->num_ratings ) );
     44
     45                // Downloaded
     46                $this->assertObjectHasAttribute( 'downloaded', $plugin_info, 'Downloaded exists' );
     47                $this->assertTrue( is_numeric( $plugin_info->downloaded ) );
     48
     49                $this->assertObjectHasAttribute( 'last_updated', $plugin_info, 'last_updated exists' );
     50                $this->assertObjectHasAttribute( 'added', $plugin_info, 'Added exists' );
     51                $this->assertObjectHasAttribute( 'homepage', $plugin_info, 'Homepage exists' );
     52                $this->assertObjectHasAttribute( 'sections', $plugin_info, 'Sections Exists' );
     53                $this->assertTrue( is_array( $plugin_info->sections ), 'Sections should be an array' );
     54
     55                // Download link
     56                $this->assertObjectHasAttribute( 'download_link', $plugin_info, 'Download Link exists' );
     57                $download_url_parsed = parse_url( $plugin_info->download_link );
     58                $this->assertEquals( $download_url_parsed['host'], 'downloads.wordpress.org' );
     59                $this->assertStringEndsWith( $plugin_info->slug . '.' . $plugin_info->version . '.zip', $download_url_parsed['path'], 'Download URL contains plugin slug and version' );
     60
     61                // Donate link
     62                $this->assertObjectHasAttribute( 'donate_link', $plugin_info, 'Donate link exists' );
     63
     64                // Tags should be key
     65                $this->assertObjectHasAttribute( 'tags', $plugin_info, 'Tags exists' );
     66                $this->assertTrue( is_array( $plugin_info->tags ), 'Tags should be an array' );
     67        }
     68
     69        function test_wp_org_plugin_api_serialize_php_get() {
     70                $url         = 'http://api.wordpress.org/plugins/info/1.0/hello-dolly';
     71                $response    = wp_remote_get( $url );
     72                $plugin_info = unserialize( $response['body'] );
     73
     74                $this->assertObjectHasAttribute( 'name', $plugin_info, 'Name exists' );
     75                $this->assertObjectHasAttribute( 'slug', $plugin_info, 'Slug exits' );
     76                $this->assertObjectHasAttribute( 'version', $plugin_info, 'Version exists' );
     77                $this->assertObjectHasAttribute( 'author', $plugin_info, 'Author exists' );
     78                $this->assertObjectHasAttribute( 'author_profile', $plugin_info, 'Author Profile exists' );
     79                $this->assertObjectHasAttribute( 'contributors', $plugin_info, 'Contributors exists' );
     80                $this->assertTrue( is_array( $plugin_info->contributors ), 'Contributors should be an array' );
     81                $this->assertObjectHasAttribute( 'requires', $plugin_info, 'Requires exists' );
     82                $this->assertObjectHasAttribute( 'tested', $plugin_info, 'Tested exists' );
     83                $this->assertObjectHasAttribute( 'compatibility', $plugin_info, 'Compatibility exists' );
     84                $this->assertTrue( is_array( $plugin_info->compatibility ), 'Compatibility should be an array' );
     85
     86                // Ratings
     87                $this->assertObjectHasAttribute( 'ratings', $plugin_info, 'Ratings exists' );
     88                $this->assertTrue( is_array( $plugin_info->ratings ), 'Ratings should be an array' );
     89                $this->assertArrayHasKey( '1', $plugin_info->ratings, 'Rating should have an attribute of 1' );
     90                $this->assertArrayHasKey( '2', $plugin_info->ratings, 'Rating should have an attribute of 2' );
     91                $this->assertArrayHasKey( '3', $plugin_info->ratings, 'Rating should have an attribute of 3' );
     92                $this->assertArrayHasKey( '4', $plugin_info->ratings, 'Rating should have an attribute of 4' );
     93                $this->assertArrayHasKey( '5', $plugin_info->ratings, 'Rating should have an attribute of 5' );
     94                $this->assertObjectHasAttribute( 'num_ratings', $plugin_info, 'Num ratings exists' );
     95                $this->assertTrue( is_numeric( $plugin_info->num_ratings ) );
     96
     97                // Downloaded
     98                $this->assertObjectHasAttribute( 'downloaded', $plugin_info, 'Downloaded exists' );
     99                $this->assertTrue( is_numeric( $plugin_info->downloaded ) );
     100
     101                $this->assertObjectHasAttribute( 'last_updated', $plugin_info, 'last_updated exists' );
     102                $this->assertObjectHasAttribute( 'added', $plugin_info, 'Added exists' );
     103                $this->assertObjectHasAttribute( 'homepage', $plugin_info, 'Homepage exists' );
     104                $this->assertObjectHasAttribute( 'sections', $plugin_info, 'Sections Exists' );
     105                $this->assertTrue( is_array( $plugin_info->sections ), 'Sections should be an array' );
     106
     107                // Download link
     108                $this->assertObjectHasAttribute( 'download_link', $plugin_info, 'Download Link exists' );
     109                $download_url_parsed = parse_url( $plugin_info->download_link );
     110                $this->assertEquals( $download_url_parsed['host'], 'downloads.wordpress.org' );
     111                $this->assertStringEndsWith( $plugin_info->slug . '.' . $plugin_info->version . '.zip', $download_url_parsed['path'], 'Download URL contains plugin slug and version' );
     112
     113                // Donate link
     114                $this->assertObjectHasAttribute( 'donate_link', $plugin_info, 'Donate link exists' );
     115
     116                // Tags should be key
     117                $this->assertObjectHasAttribute( 'tags', $plugin_info, 'Tags exists' );
     118                $this->assertTrue( is_array( $plugin_info->tags ), 'Tags should be an array' );
     119        }
     120
     121        function test_wp_org_plugin_api_json() {
     122                $url         = 'http://api.wordpress.org/plugins/info/1.0/hello-dolly.json';
     123                $response    = wp_remote_get( $url );
     124                $plugin_info = json_decode( $response['body'] );
     125
     126                $this->assertObjectHasAttribute( 'name', $plugin_info, 'Name exists' );
     127                $this->assertObjectHasAttribute( 'slug', $plugin_info, 'Slug exits' );
     128                $this->assertObjectHasAttribute( 'version', $plugin_info, 'Version exists' );
     129                $this->assertObjectHasAttribute( 'author', $plugin_info, 'Author exists' );
     130                $this->assertObjectHasAttribute( 'author_profile', $plugin_info, 'Author Profile exists' );
     131                $this->assertObjectHasAttribute( 'contributors', $plugin_info, 'Contributors exists' );
     132                $this->assertTrue( is_object( $plugin_info->contributors ), 'Contributors should be an object' );
     133                $this->assertObjectHasAttribute( 'requires', $plugin_info, 'Requires exists' );
     134                $this->assertObjectHasAttribute( 'tested', $plugin_info, 'Tested exists' );
     135                $this->assertObjectHasAttribute( 'compatibility', $plugin_info, 'Compatibility exists' );
     136                $this->assertTrue( is_array( $plugin_info->compatibility ), 'Compatibility should be an object' );
     137
     138                // Ratings
     139                $this->assertObjectHasAttribute( 'ratings', $plugin_info, 'Ratings exists' );
     140                $this->assertTrue( is_object( $plugin_info->ratings ), 'Ratings should be an object' );
     141
     142                // Num Ratings
     143                $this->assertObjectHasAttribute( 'num_ratings', $plugin_info, 'Num ratings exists' );
     144                $this->assertTrue( is_numeric( $plugin_info->num_ratings ) );
     145
     146                // Downloaded
     147                $this->assertObjectHasAttribute( 'downloaded', $plugin_info, 'Downloaded exists' );
     148                $this->assertTrue( is_numeric( $plugin_info->downloaded ) );
     149
     150                $this->assertObjectHasAttribute( 'last_updated', $plugin_info, 'last_updated exists' );
     151                $this->assertObjectHasAttribute( 'added', $plugin_info, 'Added exists' );
     152                $this->assertObjectHasAttribute( 'homepage', $plugin_info, 'Homepage exists' );
     153                $this->assertObjectHasAttribute( 'sections', $plugin_info, 'Sections Exists' );
     154                $this->assertTrue( is_object( $plugin_info->sections ), 'Sections should be an object' );
     155
     156                // Download link
     157                $this->assertObjectHasAttribute( 'download_link', $plugin_info, 'Download Link exists' );
     158                $download_url_parsed = parse_url( $plugin_info->download_link );
     159                $this->assertEquals( $download_url_parsed['host'], 'downloads.wordpress.org' );
     160                $this->assertStringEndsWith( $plugin_info->slug . '.' . $plugin_info->version . '.zip', $download_url_parsed['path'], 'Download URL contains plugin slug and version' );
     161
     162                // Donate link
     163                $this->assertObjectHasAttribute( 'donate_link', $plugin_info, 'Donate link exists' );
     164
     165                $this->assertObjectHasAttribute( 'short_description', $plugin_info, 'Short Description exists' );
     166
     167                // Tags should be key
     168                $this->assertObjectHasAttribute( 'tags', $plugin_info, 'Tags exists' );
     169                $this->assertTrue( is_array( $plugin_info->tags ), 'Tags should be an array' );
     170        }
     171
     172        function test_wp_org_plugins_api_function_action_plugin_information() {
     173
     174                $plugin_info = plugins_api( 'plugin_information', array( 'slug' => 'hello-dolly' ) );
     175
     176                $this->assertObjectHasAttribute( 'name', $plugin_info, 'Name exists' );
     177                $this->assertObjectHasAttribute( 'slug', $plugin_info, 'Slug exits' );
     178                $this->assertObjectHasAttribute( 'version', $plugin_info, 'Version exists' );
     179                $this->assertObjectHasAttribute( 'author', $plugin_info, 'Author exists' );
     180                $this->assertObjectHasAttribute( 'author_profile', $plugin_info, 'Author Profile exists' );
     181                $this->assertObjectHasAttribute( 'contributors', $plugin_info, 'Contributors exists' );
     182                $this->assertTrue( is_array( $plugin_info->contributors ), 'Contributors should be an array' );
     183                $this->assertObjectHasAttribute( 'requires', $plugin_info, 'Requires exists' );
     184                $this->assertObjectHasAttribute( 'tested', $plugin_info, 'Tested exists' );
     185                $this->assertObjectHasAttribute( 'compatibility', $plugin_info, 'Compatibility exists' );
     186                $this->assertTrue( is_array( $plugin_info->compatibility ), 'Compatibility should be an array' );
     187
     188                // Ratings
     189                $this->assertObjectHasAttribute( 'ratings', $plugin_info, 'Ratings exists' );
     190                $this->assertTrue( is_array( $plugin_info->ratings ), 'Ratings should be an array' );
     191                $this->assertArrayHasKey( '1', $plugin_info->ratings, 'Rating should have an attribute of 1' );
     192                $this->assertArrayHasKey( '2', $plugin_info->ratings, 'Rating should have an attribute of 2' );
     193                $this->assertArrayHasKey( '3', $plugin_info->ratings, 'Rating should have an attribute of 3' );
     194                $this->assertArrayHasKey( '4', $plugin_info->ratings, 'Rating should have an attribute of 4' );
     195                $this->assertArrayHasKey( '5', $plugin_info->ratings, 'Rating should have an attribute of 5' );
     196                $this->assertObjectHasAttribute( 'num_ratings', $plugin_info, 'Num ratings exists' );
     197                $this->assertTrue( is_numeric( $plugin_info->num_ratings ) );
     198
     199                // Downloaded
     200                $this->assertObjectHasAttribute( 'downloaded', $plugin_info, 'Downloaded exists' );
     201                $this->assertTrue( is_numeric( $plugin_info->downloaded ) );
     202
     203                $this->assertObjectHasAttribute( 'last_updated', $plugin_info, 'Last updated exists' );
     204                $this->assertObjectHasAttribute( 'added', $plugin_info, 'Added exists' );
     205                $this->assertObjectHasAttribute( 'homepage', $plugin_info, 'Homepage exists' );
     206                $this->assertObjectHasAttribute( 'sections', $plugin_info, 'Sections exists' );
     207                $this->assertTrue( is_array( $plugin_info->sections ), 'Sections should be an array' );
     208
     209                // Download link
     210                $this->assertObjectHasAttribute( 'download_link', $plugin_info, 'Download Link exists' );
     211                $download_url_parsed = parse_url( $plugin_info->download_link );
     212                $this->assertEquals( $download_url_parsed['host'], 'downloads.wordpress.org' );
     213                $this->assertStringEndsWith( $plugin_info->slug . '.' . $plugin_info->version . '.zip', $download_url_parsed['path'], 'Download URL contains plugin slug and version' );
     214
     215                // Donate link
     216                $this->assertObjectHasAttribute( 'donate_link', $plugin_info, 'Donate link exists' );
     217
     218                // Tags should be key
     219                $this->assertObjectHasAttribute( 'tags', $plugin_info, 'Tags exists' );
     220                $this->assertTrue( is_array( $plugin_info->tags ), 'Tags should be an array' );
     221
     222        }
     223
     224        function test_wp_org_plugins_api_function_action_query_plugins() {
     225
     226                $plugin_query = plugins_api( 'query_plugins', array( 'search' => 'hello' ) );
     227
     228                // Info
     229                $this->assertObjectHasAttribute( 'info', $plugin_query, 'Info exists' );
     230                $info = (object) $plugin_query->info;
     231
     232                $this->assertObjectHasAttribute( 'page', $info, 'Page exists' );
     233                $this->assertObjectHasAttribute( 'pages', $info, 'Pages exists' );
     234                $this->assertObjectHasAttribute( 'results', $info, 'Results exists' );
     235
     236                // Plugins
     237                $this->assertObjectHasAttribute( 'plugins', $plugin_query, 'Plugins exists' );
     238                $this->assertTrue( is_array( $plugin_query->plugins ), 'Plugins should be an array' );
     239
     240                $plugin_info = $plugin_query->plugins[0];
     241                $this->assertObjectHasAttribute( 'name', $plugin_info, 'Name exists' );
     242                $this->assertObjectHasAttribute( 'slug', $plugin_info, 'Slug exits' );
     243                $this->assertObjectHasAttribute( 'version', $plugin_info, 'Version exists' );
     244                $this->assertObjectHasAttribute( 'author', $plugin_info, 'Author exists' );
     245                $this->assertObjectHasAttribute( 'author_profile', $plugin_info, 'Author Profile exists' );
     246
     247                $this->assertObjectHasAttribute( 'contributors', $plugin_info, 'Contributors exists' );
     248                $this->assertTrue( is_array( $plugin_info->contributors ), 'Contributors should be an array' );
     249                $this->assertObjectHasAttribute( 'requires', $plugin_info, 'Requires exists' );
     250                $this->assertObjectHasAttribute( 'tested', $plugin_info, 'Tested exists' );
     251                $this->assertObjectHasAttribute( 'compatibility', $plugin_info, 'Compatibility exists' );
     252                $this->assertTrue( is_array( $plugin_info->compatibility ), 'Compatibility should be an array' );
     253
     254                // Ratings
     255                $this->assertObjectHasAttribute( 'ratings', $plugin_info, 'Ratings exists' );
     256                $this->assertTrue( is_array( $plugin_info->ratings ), 'Ratings should be an array' );
     257                $this->assertArrayHasKey( '1', $plugin_info->ratings, 'Rating should have an attribute of 1' );
     258                $this->assertArrayHasKey( '2', $plugin_info->ratings, 'Rating should have an attribute of 2' );
     259                $this->assertArrayHasKey( '3', $plugin_info->ratings, 'Rating should have an attribute of 3' );
     260                $this->assertArrayHasKey( '4', $plugin_info->ratings, 'Rating should have an attribute of 4' );
     261                $this->assertArrayHasKey( '5', $plugin_info->ratings, 'Rating should have an attribute of 5' );
     262                $this->assertObjectHasAttribute( 'num_ratings', $plugin_info, 'Num ratings exists' );
     263                $this->assertTrue( is_numeric( $plugin_info->num_ratings ) );
     264
     265                $this->assertObjectHasAttribute( 'homepage', $plugin_info, 'Homepage exists' );
     266                $this->assertObjectHasAttribute( 'description', $plugin_info, 'Description exists' );
     267                $this->assertObjectHasAttribute( 'short_description', $plugin_info, 'Short Description exists' );
     268        }
     269
     270        function test_wp_org_plugins_api_function_action_hot_categories() {
     271
     272                $plugin_query_hot_categories = plugins_api( 'hot_categories' );
     273                foreach ( (array) $plugin_query_hot_categories as $hot_category => $category_array ) {
     274
     275                        $this->assertTrue( is_array( $category_array ), 'Category array is array' );
     276                        $this->assertArrayHasKey( 'name', $category_array, 'Name exists' );
     277                        $this->assertArrayHasKey( 'slug', $category_array, 'Slug exists' );
     278                        break;
     279                }
     280
     281        }
     282
     283        function test_wp_org_plugins_api_function_action_hot_tags() {
     284
     285                $plugin_query_hot_tags = plugins_api( 'hot_tags' );
     286                foreach ( (array) $plugin_query_hot_tags as $hot_tag => $tag_array ) {
     287                        $this->assertTrue( is_array( $tag_array ), 'Tag Object is array' );
     288                        $this->assertArrayHasKey( 'name', $tag_array, 'Name exists' );
     289                        $this->assertArrayHasKey( 'slug', $tag_array, 'Slug exists' );
     290                        $this->assertArrayHasKey( 'count', $tag_array, 'Count exists' );
     291                        break;
     292                }
     293        }
     294}