Making WordPress.org

Ticket #1614: 1614.diff

File 1614.diff, 8.7 KB (added by obenland, 8 years ago)
  • 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
     43                $this->_check_response_attributes( $plugin_info );
     44        }
     45
     46        function test_plugins_api_function_action_query_plugins() {
     47                $plugin_query = plugins_api( 'query_plugins', array( 'search' => 'hello' ) );
     48
     49                // Info.
     50                $this->assertObjectHasAttribute( 'info', $plugin_query, 'Info exists' );
     51                $info = $plugin_query->info;
     52
     53                $this->assertArrayHasKey( 'page', $info, 'Page exists' );
     54                $this->assertArrayHasKey( 'pages', $info, 'Pages exists' );
     55                $this->assertArrayHasKey( 'results', $info, 'Results exists' );
     56
     57                // Plugins.
     58                $this->assertObjectHasAttribute( 'plugins', $plugin_query, 'Plugins exists' );
     59                $this->assertAttributeInternalType( 'array', 'plugins', $plugin_query, 'Plugins should be an array' );
     60
     61                $plugin_info = $plugin_query->plugins[0];
     62                $this->assertObjectHasAttribute( 'name', $plugin_info, 'Name exists' );
     63                $this->assertObjectHasAttribute( 'slug', $plugin_info, 'Slug exits' );
     64                $this->assertObjectHasAttribute( 'version', $plugin_info, 'Version exists' );
     65                $this->assertObjectHasAttribute( 'author', $plugin_info, 'Author exists' );
     66                $this->assertObjectHasAttribute( 'author_profile', $plugin_info, 'Author Profile exists' );
     67                $this->assertObjectHasAttribute( 'contributors', $plugin_info, 'Contributors exists' );
     68                $this->assertAttributeInternalType( 'array', 'contributors', $plugin_info, 'Contributors should be an array' );
     69                $this->assertObjectHasAttribute( 'requires', $plugin_info, 'Requires exists' );
     70                $this->assertObjectHasAttribute( 'tested', $plugin_info, 'Tested exists' );
     71                $this->assertObjectHasAttribute( 'compatibility', $plugin_info, 'Compatibility exists' );
     72                $this->assertAttributeInternalType( 'array', 'compatibility', $plugin_info, 'Compatibility should be an array' );
     73
     74                // Ratings.
     75                $this->assertObjectHasAttribute( 'rating', $plugin_info, 'Rating exists' );
     76                $this->assertObjectHasAttribute( 'ratings', $plugin_info, 'Ratings exists' );
     77                $this->assertAttributeInternalType( 'array', 'ratings', $plugin_info, 'Ratings should be an array' );
     78                $this->assertArrayHasKey( '1', $plugin_info->ratings, 'Rating should have an attribute of 1' );
     79                $this->assertArrayHasKey( '2', $plugin_info->ratings, 'Rating should have an attribute of 2' );
     80                $this->assertArrayHasKey( '3', $plugin_info->ratings, 'Rating should have an attribute of 3' );
     81                $this->assertArrayHasKey( '4', $plugin_info->ratings, 'Rating should have an attribute of 4' );
     82                $this->assertArrayHasKey( '5', $plugin_info->ratings, 'Rating should have an attribute of 5' );
     83                $this->assertObjectHasAttribute( 'num_ratings', $plugin_info, 'Num ratings exists' );
     84                $this->assertTrue( is_numeric( $plugin_info->num_ratings ), 'Num ratings are numeric' );
     85
     86
     87                $this->assertObjectHasAttribute( 'homepage', $plugin_info, 'Homepage exists' );
     88                $this->assertObjectHasAttribute( 'description', $plugin_info, 'Description exists' );
     89                $this->assertObjectHasAttribute( 'short_description', $plugin_info, 'Short Description exists' );
     90        }
     91
     92        function test_plugins_api_function_action_hot_categories() {
     93                $plugin_query_hot_categories = plugins_api( 'hot_categories' );
     94
     95                $this->assertInternalType( 'array', $plugin_query_hot_categories, 'Response array is array' );
     96                foreach ( $plugin_query_hot_categories as $hot_category => $category_array ) {
     97                        $this->assertInternalType( 'array', $category_array, 'Category array is array' );
     98                        $this->assertArrayHasKey( 'name', $category_array, 'Name exists' );
     99                        $this->assertArrayHasKey( 'slug', $category_array, 'Slug exists' );
     100
     101                        // Only check the first result.
     102                        break;
     103                }
     104        }
     105
     106        function test_plugins_api_function_action_hot_tags() {
     107                $plugin_query_hot_tags = plugins_api( 'hot_tags' );
     108
     109                $this->assertInternalType( 'array', $plugin_query_hot_tags, 'Response array is array' );
     110                foreach ( $plugin_query_hot_tags as $hot_tag => $tag_array ) {
     111                        $this->assertInternalType( 'array', $tag_array, 'Tag array is array' );
     112                        $this->assertArrayHasKey( 'name', $tag_array, 'Name exists' );
     113                        $this->assertArrayHasKey( 'slug', $tag_array, 'Slug exists' );
     114                        $this->assertArrayHasKey( 'count', $tag_array, 'Count exists' );
     115
     116                        // Only check the first result.
     117                        break;
     118                }
     119        }
     120
     121        function _check_response_attributes( $plugin_info ) {
     122                $this->assertObjectHasAttribute( 'name', $plugin_info, 'Name exists' );
     123                $this->assertObjectHasAttribute( 'slug', $plugin_info, 'Slug exits' );
     124                $this->assertObjectHasAttribute( 'version', $plugin_info, 'Version exists' );
     125                $this->assertObjectHasAttribute( 'author', $plugin_info, 'Author exists' );
     126                $this->assertObjectHasAttribute( 'author_profile', $plugin_info, 'Author Profile exists' );
     127                $this->assertObjectHasAttribute( 'contributors', $plugin_info, 'Contributors exists' );
     128                $this->assertAttributeInternalType( 'array', 'contributors', $plugin_info, 'Contributors should be an array' );
     129                $this->assertObjectHasAttribute( 'requires', $plugin_info, 'Requires exists' );
     130                $this->assertObjectHasAttribute( 'tested', $plugin_info, 'Tested exists' );
     131                $this->assertObjectHasAttribute( 'compatibility', $plugin_info, 'Compatibility exists' );
     132                $this->assertAttributeInternalType( 'array', 'compatibility', $plugin_info, 'Compatibility should be an array' );
     133
     134                // Ratings.
     135                $this->assertObjectHasAttribute( 'rating', $plugin_info, 'Rating exists' );
     136                $this->assertObjectHasAttribute( 'ratings', $plugin_info, 'Ratings exists' );
     137                $this->assertAttributeInternalType( 'array', 'ratings', $plugin_info, 'Ratings should be an array' );
     138                $this->assertArrayHasKey( '1', $plugin_info->ratings, 'Rating should have an attribute of 1' );
     139                $this->assertArrayHasKey( '2', $plugin_info->ratings, 'Rating should have an attribute of 2' );
     140                $this->assertArrayHasKey( '3', $plugin_info->ratings, 'Rating should have an attribute of 3' );
     141                $this->assertArrayHasKey( '4', $plugin_info->ratings, 'Rating should have an attribute of 4' );
     142                $this->assertArrayHasKey( '5', $plugin_info->ratings, 'Rating should have an attribute of 5' );
     143                $this->assertObjectHasAttribute( 'num_ratings', $plugin_info, 'Num ratings exists' );
     144                $this->assertTrue( is_numeric( $plugin_info->num_ratings ), 'Num ratings are numeric' );
     145
     146                // Downloaded.
     147                $this->assertObjectHasAttribute( 'downloaded', $plugin_info, 'Downloaded exists' );
     148                $this->assertTrue( is_numeric( $plugin_info->downloaded ), 'Downloaded count is numeric' );
     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->assertAttributeInternalType( 'array', 'sections', $plugin_info, 'Sections should be an array' );
     155
     156                // Download link.
     157                $this->assertObjectHasAttribute( 'download_link', $plugin_info, 'Download Link exists' );
     158
     159                // Donate link.
     160                $this->assertObjectHasAttribute( 'donate_link', $plugin_info, 'Donate link exists' );
     161
     162                // Tags.
     163                $this->assertObjectHasAttribute( 'tags', $plugin_info, 'Tags exists' );
     164                $this->assertAttributeInternalType( 'array', 'tags', $plugin_info, 'Tags should be an array' );
     165        }
     166}