Make WordPress Core

Ticket #39264: 39264.6.diff

File 39264.6.diff, 16.5 KB (added by jnylen0, 8 years ago)

Cleaned up formatting, naming, etc.

  • Gruntfile.js

    diff --git a/Gruntfile.js b/Gruntfile.js
    index 5a9317c..92aa12d 100644
    a b module.exports = function(grunt) { 
    439439                        'external-http': {
    440440                                cmd: 'phpunit',
    441441                                args: ['-c', 'phpunit.xml.dist', '--group', 'external-http']
     442                        },
     443                        'restapi-jsclient': {
     444                                cmd: 'phpunit',
     445                                args: ['-c', 'phpunit.xml.dist', '--group', 'restapi-jsclient']
    442446                        }
    443447                },
    444448                uglify: {
    module.exports = function(grunt) { 
    672676                'jshint:media'
    673677        ] );
    674678
     679        grunt.registerTask( 'restapi-jsclient', [
     680                'phpunit:restapi-jsclient',
     681                'qunit'
     682        ] );
     683
    675684        grunt.renameTask( 'watch', '_watch' );
    676685
    677686        grunt.registerTask( 'watch', function() {
  • tests/phpunit/tests/rest-api/rest-schema-setup.php

    diff --git a/tests/phpunit/tests/rest-api/rest-schema-setup.php b/tests/phpunit/tests/rest-api/rest-schema-setup.php
    index e69de29..cda1869 100644
    a b  
     1<?php
     2/**
     3 * Unit tests covering schema initialization.
     4 *
     5 * Also generates the fixture data used by the wp-api.js QUnit tests.
     6 *
     7 * @package WordPress
     8 * @subpackage REST API
     9 */
     10
     11/**
     12 * @group restapi
     13 * @group restapi-jsclient
     14 */
     15class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase {
     16
     17        public function setUp() {
     18                parent::setUp();
     19
     20                /** @var WP_REST_Server $wp_rest_server */
     21                global $wp_rest_server;
     22                $this->server = $wp_rest_server = new Spy_REST_Server;
     23                do_action( 'rest_api_init' );
     24        }
     25
     26        public function tearDown() {
     27                parent::tearDown();
     28                remove_filter( 'rest_url', array( $this, 'test_rest_url_for_leading_slash' ), 10, 2 );
     29                /** @var WP_REST_Server $wp_rest_server */
     30                global $wp_rest_server;
     31                $wp_rest_server = null;
     32        }
     33
     34        public function test_expected_routes_in_schema() {
     35                $routes = $this->server->get_routes();
     36
     37                $this->assertTrue( is_array( $routes ), '`get_routes` should return an array.' );
     38                $this->assertTrue( ! empty( $routes ), 'Routes should not be empty.' );
     39
     40                $expected_routes = array(
     41                        '/',
     42                        '/oembed/1.0',
     43                        '/oembed/1.0/embed',
     44                        '/wp/v2',
     45                        '/wp/v2/posts',
     46                        '/wp/v2/posts/(?P<id>[\\d]+)',
     47                        '/wp/v2/posts/(?P<parent>[\\d]+)/revisions',
     48                        '/wp/v2/posts/(?P<parent>[\\d]+)/revisions/(?P<id>[\\d]+)',
     49                        '/wp/v2/pages',
     50                        '/wp/v2/pages/(?P<id>[\\d]+)',
     51                        '/wp/v2/pages/(?P<parent>[\\d]+)/revisions',
     52                        '/wp/v2/pages/(?P<parent>[\\d]+)/revisions/(?P<id>[\\d]+)',
     53                        '/wp/v2/media',
     54                        '/wp/v2/media/(?P<id>[\\d]+)',
     55                        '/wp/v2/types',
     56                        '/wp/v2/types/(?P<type>[\\w-]+)',
     57                        '/wp/v2/statuses',
     58                        '/wp/v2/statuses/(?P<status>[\\w-]+)',
     59                        '/wp/v2/taxonomies',
     60                        '/wp/v2/taxonomies/(?P<taxonomy>[\\w-]+)',
     61                        '/wp/v2/categories',
     62                        '/wp/v2/categories/(?P<id>[\\d]+)',
     63                        '/wp/v2/tags',
     64                        '/wp/v2/tags/(?P<id>[\\d]+)',
     65                        '/wp/v2/users',
     66                        '/wp/v2/users/(?P<id>[\\d]+)',
     67                        '/wp/v2/users/me',
     68                        '/wp/v2/comments',
     69                        '/wp/v2/comments/(?P<id>[\\d]+)',
     70                        '/wp/v2/settings',
     71                );
     72
     73                $this->assertEquals( $expected_routes, array_keys( $routes ) );
     74        }
     75
     76        public function test_build_wp_api_client_fixtures() {
     77                // Set up for testing the individual endpoints.
     78                // Set a current admin user.
     79                $administrator = $this->factory->user->create( array(
     80                        'role' => 'administrator',
     81                ) );
     82                wp_set_current_user( $administrator );
     83
     84                // Set up data for endpoints.
     85                $post_id  = $this->factory->post->create();
     86                $page_id  = $this->factory->post->create( array( 'post_type' => 'page' ) );
     87                $tag_id   = $this->factory->tag->create( array( 'name' => 'test' ) );
     88                $media_id = $this->factory->attachment->create_object( '/tmp/canola.jpg', 0, array(
     89                        'post_mime_type' => 'image/jpeg',
     90                        'post_excerpt'   => 'A sample caption',
     91                ) );
     92                wp_update_post( array( 'post_content' => 'Updated content.', 'ID' => $post_id ) );
     93                wp_update_post( array( 'post_content' => 'Updated content.', 'ID' => $page_id ) );
     94                $comment_id = $this->factory->comment->create( array(
     95                        'comment_approved' => 1,
     96                        'comment_post_ID'  => $post_id,
     97                        'user_id'          => 0,
     98                ) );
     99
     100                // Generate route data for subsequent QUnit tests.
     101                $routes_to_generate_data = array(
     102                        array(
     103                                'route' => '/',
     104                                'name'  => 'Schema',
     105                        ),
     106                        array(
     107                                'route' => '/oembed/1.0',
     108                                'name'  => 'oembed',
     109                        ),
     110                        array(
     111                                'route' => '/oembed/1.0/embed',
     112                                'name'  => 'oembeds',
     113                        ),
     114                        array(
     115                                'route' => '/wp/v2/posts',
     116                                'name'  => 'PostsCollection',
     117                        ),
     118                        array(
     119                                'route' => '/wp/v2/posts/' . $post_id,
     120                                'name'  => 'PostModel',
     121                        ),
     122                        array(
     123                                'route' => '/wp/v2/posts/' . $post_id . '/revisions',
     124                                'name'  => 'postRevisions',
     125                        ),
     126                        array(
     127                                'route' => '/wp/v2/posts/' . $post_id . '/revisions/1',
     128                                'name'  => 'revision',
     129                        ),
     130                        array(
     131                                'route' => '/wp/v2/pages',
     132                                'name'  => 'PagesCollection',
     133                        ),
     134                        array(
     135                                'route' => '/wp/v2/pages/' . $page_id,
     136                                'name'  => 'PageModel',
     137                        ),
     138                        array(
     139                                'route' => '/wp/v2/pages/'. $page_id . '/revisions',
     140                                'name'  => 'pageRevisions',
     141                        ),
     142                        array(
     143                                'route' => '/wp/v2/pages/'. $page_id . '/revisions/1',
     144                                'name'  => 'pageRevision',
     145                        ),
     146                        array(
     147                                'route' => '/wp/v2/media',
     148                                'name'  => 'MediaCollection',
     149                        ),
     150                        array(
     151                                'route' => '/wp/v2/media/' . $media_id,
     152                                'name'  => 'MediaModel',
     153                        ),
     154                        array(
     155                                'route' => '/wp/v2/types',
     156                                'name'  => 'TypesCollection',
     157                        ),
     158                        array(
     159                                'route' => '/wp/v2/types/',
     160                                'name'  => 'TypeModel',
     161                        ),
     162                        array(
     163                                'route' => '/wp/v2/statuses',
     164                                'name'  => 'StatusesCollection',
     165                        ),
     166                        array(
     167                                'route' => '/wp/v2/statuses/publish',
     168                                'name'  => 'StatusModel',
     169                        ),
     170                        array(
     171                                'route' => '/wp/v2/taxonomies',
     172                                'name'  => 'TaxonomiesCollection',
     173                        ),
     174                        array(
     175                                'route' => '/wp/v2/taxonomies/category',
     176                                'name'  => 'TaxonomyModel',
     177                        ),
     178                        array(
     179                                'route' => '/wp/v2/categories',
     180                                'name'  => 'CategoriesCollection',
     181                        ),
     182                        array(
     183                                'route' => '/wp/v2/categories/1',
     184                                'name'  => 'CategoryModel',
     185                        ),
     186                        array(
     187                                'route' => '/wp/v2/tags',
     188                                'name'  => 'TagsCollection',
     189                        ),
     190                        array(
     191                                'route' => '/wp/v2/tags/' . $tag_id,
     192                                'name'  => 'TagModel',
     193                        ),
     194                        array(
     195                                'route' => '/wp/v2/users',
     196                                'name'  => 'UsersCollection',
     197                        ),
     198                        array(
     199                                'route' => '/wp/v2/users/1',
     200                                'name'  => 'UserModel',
     201                        ),
     202                        array(
     203                                'route' => '/wp/v2/users/me',
     204                                'name'  => 'me',
     205                        ),
     206                        array(
     207                                'route' => '/wp/v2/comments',
     208                                'name'  => 'CommentsCollection',
     209                        ),
     210                        array(
     211                                'route' => '/wp/v2/comments/1',
     212                                'name'  => 'CommentModel',
     213                        ),
     214                        array(
     215                                'route' => '/wp/v2/settings',
     216                                'name'  => 'settings',
     217                        ),
     218                );
     219
     220                $mocked_responses = "/**\n";
     221                $mocked_responses .= " * DO NOT EDIT\n";
     222                $mocked_responses .= " * Auto-generated by test_build_wp_api_client_fixtures\n";
     223                $mocked_responses .= " */\n";
     224                $mocked_responses .= "var mockedApiResponse = {};\n\n";
     225
     226                foreach ( $routes_to_generate_data as $route ) {
     227                        $request = new WP_REST_Request( 'GET', $route['route'] );
     228                        $response = $this->server->dispatch( $request );
     229                        $data = $response->get_data();
     230
     231                        $this->assertTrue( ! empty( $data ), $route['name'] . ' route should return data.' );
     232
     233                        $mocked_responses .= 'mockedApiResponse.' . $route['name'] . ' = ' . json_encode( $data ) . ";\n\n";
     234                }
     235
     236                // Save the route object for QUnit tests.
     237                $file = './tests/qunit/fixtures/wp-api-generated.js';
     238                file_put_contents( $file, $mocked_responses );
     239
     240                // Clean up our test data.
     241                wp_delete_post( $post_id, true );
     242                wp_delete_post( $page_id, true );
     243                wp_delete_term( $tag_id, 'tags' );
     244                wp_delete_attachment( $media_id );
     245                wp_delete_comment( $comment_id );
     246        }
     247}
  • tests/qunit/fixtures/wp-api.js

    diff --git a/tests/qunit/fixtures/wp-api.js b/tests/qunit/fixtures/wp-api.js
    index e69de29..51946b0 100644
    a b  
     1/**
     2 * @var mockedApiResponse defined in wp-api-generated.js
     3 */
     4
     5var pathToData = {
     6        'wp-json/wp/v2/': mockedApiResponse.Schema,
     7        'wp-json/wp/v2/categories': mockedApiResponse.CategoriesCollection,
     8        'wp-json/wp/v2/comments': mockedApiResponse.CommentsCollection,
     9        'wp-json/wp/v2/media': mockedApiResponse.MediaCollection,
     10        'wp-json/wp/v2/pages': mockedApiResponse.PagesCollection,
     11        'wp-json/wp/v2/posts': mockedApiResponse.PostsCollection,
     12        'wp-json/wp/v2/statuses': mockedApiResponse.StatusesCollection,
     13        'wp-json/wp/v2/tags': mockedApiResponse.TagsCollection,
     14        'wp-json/wp/v2/taxonomies': mockedApiResponse.TaxonomiesCollection,
     15        'wp-json/wp/v2/types': mockedApiResponse.TypesCollection,
     16        'wp-json/wp/v2/users': mockedApiResponse.UsersCollection,
     17        'wp-json/wp/v2/category': mockedApiResponse.CategoryModel,
     18        'wp-json/wp/v2/media1': mockedApiResponse.MediaModel,
     19        'wp-json/wp/v2/page': mockedApiResponse.PageModel,
     20        'wp-json/wp/v2/post': mockedApiResponse.PostModel,
     21        'wp-json/wp/v2/tag': mockedApiResponse.TagModel,
     22        'wp-json/wp/v2/user': mockedApiResponse.UserModel,
     23        'wp-json/wp/v2/taxonomy': mockedApiResponse.TaxonomyModel,
     24        'wp-json/wp/v2/status': mockedApiResponse.StatusModel,
     25        'wp-json/wp/v2/type': mockedApiResponse.TypeModel
     26};
     27
     28/**
     29 * Mock the ajax callbacks for our tests.
     30 *
     31 * @param  {object} param The parameters sent to the ajax request.
     32 *
     33 * @return {Object}       A jQuery defered object that resolves with the mapped data.
     34 */
     35Backbone.ajax = function ( param ) {
     36
     37        var data,
     38                request = param.url.replace( 'http://localhost/', '' );
     39
     40        if ( pathToData[ request ] ) {
     41                data = pathToData[ request ];
     42        }
     43
     44        // Call success handler.
     45        param.success( data );
     46        var deferred = jQuery.Deferred();
     47
     48        // Resolve the deferred with the mocked data
     49        deferred.resolve( data );
     50
     51        // Return the deferred promise that will resolve with the expected data.
     52        return deferred.promise();
     53
     54};
  • tests/qunit/index.html

    diff --git a/tests/qunit/index.html b/tests/qunit/index.html
    index 58591e4..0b6930f 100644
    a b  
    1717                                }
    1818                        };
    1919                </script>
     20                <script>
     21                        var wpApiSettings = {
     22                                'root': 'http://localhost/wp-json/'
     23                        };
     24                </script>
    2025                <script src="../../src/wp-includes/js/wp-util.js"></script>
    2126                <script src="../../src/wp-includes/js/wp-a11y.js"></script>
    2227
     
    3439                        <script src="fixtures/customize-settings.js"></script>
    3540                        <script src="fixtures/customize-menus.js"></script>
    3641                        <script src="fixtures/customize-widgets.js"></script>
     42                        <script src="fixtures/wp-api-generated.js"></script>
     43                        <script src="fixtures/wp-api.js"></script>
    3744                </div>
    3845                <p><a href="editor">TinyMCE tests</a></p>
    3946
     
    4350                <script src="../../src/wp-includes/js/customize-models.js"></script>
    4451                <script src="../../src/wp-includes/js/shortcode.js"></script>
    4552                <script src="../../src/wp-admin/js/customize-controls.js"></script>
     53                <script src="../../src/wp-includes/js/wp-api.js"></script>
    4654
    4755                <script type='text/javascript' src='../../src/wp-includes/js/jquery/ui/core.js'></script>
    4856                <script type='text/javascript' src='../../src/wp-includes/js/jquery/ui/widget.js'></script>
     
    6169                <script src="wp-admin/js/customize-base.js"></script>
    6270                <script src="wp-admin/js/customize-header.js"></script>
    6371                <script src="wp-includes/js/shortcode.js"></script>
     72                <script src="wp-includes/js/wp-api.js"></script>
    6473                <script src="wp-admin/js/customize-controls.js"></script>
    6574                <script src="wp-admin/js/customize-controls-utils.js"></script>
    6675                <script src="wp-admin/js/customize-nav-menus.js"></script>
  • tests/qunit/wp-includes/js/wp-api.js

    diff --git a/tests/qunit/wp-includes/js/wp-api.js b/tests/qunit/wp-includes/js/wp-api.js
    index e69de29..e8634f0 100644
    a b  
     1/* global wp */
     2( function( QUnit ) {
     3        module( 'wpapi' );
     4
     5        QUnit.test( 'API Loaded correctly', function( assert ) {
     6                var done = assert.async();
     7                assert.expect( 2 );
     8
     9                assert.ok( wp.api.loadPromise );
     10
     11                wp.api.loadPromise.done( function() {
     12                        assert.ok( wp.api.models );
     13                        done();
     14                } );
     15
     16        } );
     17
     18        // The list of collections we should check.
     19        var collectionClassNames = [
     20                'Categories',
     21                'Comments',
     22                'Media',
     23                'Pages',
     24                'Posts',
     25                'Statuses',
     26                'Tags',
     27                'Taxonomies',
     28                'Types',
     29                'Users'
     30        ];
     31
     32        // Collections that should get helpers tested.
     33        var collectionHelperTests = [
     34                {
     35                        'collectionType':   'Posts',
     36                        'returnsModelType': 'post',
     37                        'supportsMethods':  {
     38                                'getDate':          'getDate',
     39                                'getRevisions':     'getRevisions',
     40                                'getTags':          'getTags',
     41                                'getCategories':    'getCategories',
     42                                'getAuthorUser':    'getAuthorUser',
     43                                'getFeaturedMedia': 'getFeaturedMedia'
     44                                /*'getMeta':        'getMeta', currently not supported */
     45                        }
     46                },
     47                {
     48                        'collectionType':   'Pages',
     49                        'returnsModelType': 'page',
     50                        'supportsMethods':  {
     51                                'getDate':          'getDate',
     52                                'getRevisions':     'getRevisions',
     53                                'getAuthorUser':    'getAuthorUser',
     54                                'getFeaturedMedia': 'getFeaturedMedia'
     55                        }
     56                }
     57        ];
     58
     59        _.each( collectionClassNames, function( className ) {
     60                QUnit.test( 'Testing ' + className + ' collection.', function( assert ) {
     61                        var done = assert.async();
     62
     63                        wp.api.loadPromise.done( function() {
     64                                var theCollection = new wp.api.collections[ className ]();
     65                                assert.ok(
     66                                        theCollection,
     67                                        'We can instantiate wp.api.collections.' + className
     68                                );
     69                                theCollection.fetch().done( function( response ) {
     70                                        assert.equal(
     71                                                1,
     72                                                theCollection.state.currentPage,
     73                                                'We should be on page 1 of the collection in ' + className
     74                                        );
     75
     76                                                // Should this collection have helper methods?
     77                                                var collectionHelperTest = _.findWhere( collectionHelperTests, { 'collectionType': className } );
     78
     79                                                // If we found a match, run the tests against it.
     80                                                if ( ! _.isUndefined( collectionHelperTest ) ) {
     81
     82                                                        // Test the first returned model.
     83                                                        var firstModel = theCollection.at( 0 );
     84
     85                                                        // Is the model the right type?
     86                                                        assert.equal(
     87                                                                collectionHelperTest.returnsModelType,
     88                                                                firstModel.get( 'type' ),
     89                                                                'The wp.api.collections.' + className + ' is of type ' + collectionHelperTest.returnsModelType
     90                                                        );
     91
     92                                                        // Does the model have all of the expected supported methods?
     93                                                        _.each( collectionHelperTest.supportsMethods, function( method ) {
     94                                                                assert.equal(
     95                                                                        'function',
     96                                                                        typeof firstModel[ method ],
     97                                                                        className + '.' + method + ' is a function.'
     98                                                                );
     99                                                        } );
     100                                                }
     101
     102                                        // Trigger Qunit async completion.
     103                                        done();
     104                                } );
     105
     106                        } );
     107
     108                } );
     109        } );
     110
     111        // The list of models we should check.
     112        var modelsWithIdsClassNames = [
     113                'Category',
     114                'Media',
     115                'Page',
     116                'Post',
     117                'Tag',
     118                'User'
     119        ];
     120
     121        _.each( modelsWithIdsClassNames, function( className ) {
     122
     123                QUnit.test( 'Checking ' + className + ' model.' , function( assert ) {
     124                        var done = assert.async();
     125
     126                        assert.expect( 2 );
     127
     128                        wp.api.loadPromise.done( function() {
     129                                var theModel = new wp.api.models[ className ]();
     130                                assert.ok( theModel, 'We can instantiate wp.api.models.' + className );
     131                                theModel.fetch().done( function(  ) {
     132                                        var theModel2 = new wp.api.models[ className ]();
     133                                        theModel2.set( 'id', theModel.attributes[0].id );
     134                                        theModel2.fetch().done( function( response ) {
     135
     136                                                // We were able to retrieve the model.
     137                                                assert.equal(
     138                                                        theModel.attributes[0].id,
     139                                                        theModel2.get( 'id' ) ,
     140                                                        'We should be able to get a ' + className
     141                                                );
     142
     143                                                // Trigger Qunit async completion.
     144                                                done();
     145                                        } );
     146                                } );
     147
     148                        } );
     149
     150                } );
     151        } );
     152
     153        var modelsWithIndexes = [
     154                'Taxonomy',
     155                'Status',
     156                'Type'
     157        ];
     158
     159        _.each( modelsWithIndexes, function( className ) {
     160
     161                QUnit.test( 'Testing ' + className + ' model.' , function( assert ) {
     162                        var done = assert.async();
     163
     164                        assert.expect( 2 );
     165
     166                        wp.api.loadPromise.done( function(  ) {
     167
     168                                var theModel = new wp.api.models[ className ]();
     169                                assert.ok( theModel, 'We can instantiate wp.api.models.' + className );
     170                                theModel.fetch().done( function(  ) {
     171                                        var theModel2 = new wp.api.models[ className ]();
     172
     173                                        if ( ! _.isUndefined( theModel.attributes[0] ) ) {
     174                                                theModel2.set( 'id', theModel.attributes[0].id );
     175                                        }
     176
     177                                        theModel2.fetch().done( function( response ) {
     178                                                // We were able to retrieve the model.
     179                                                assert.notEqual(
     180                                                        0,
     181                                                        _.keys( theModel2.attributes ).length ,
     182                                                        'We should be able to get a ' + className
     183                                                );
     184
     185                                                // Trigger Qunit async completion.
     186                                                done();
     187                                        } );
     188                                } );
     189
     190                        } );
     191
     192                } );
     193        } );
     194
     195} )( window.QUnit );