Merge "mediawiki.api.category: Avoid exceptions"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 2 Feb 2018 18:23:52 +0000 (18:23 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 2 Feb 2018 18:23:52 +0000 (18:23 +0000)
resources/src/mediawiki/api/category.js
tests/qunit/suites/resources/mediawiki.api/mediawiki.api.category.test.js

index 04462e3..85df90e 100644 (file)
                        var apiPromise = this.get( {
                                formatversion: 2,
                                prop: 'categoryinfo',
-                               titles: String( title )
+                               titles: [ String( title ) ]
                        } );
 
                        return apiPromise
                                .then( function ( data ) {
-                                       return !!data.query.pages[ 0 ].categoryinfo;
+                                       return !!(
+                                               data.query && // query is missing on title=""
+                                               data.query.pages && // query.pages is missing on title="#" or title="mw:"
+                                               data.query.pages[ 0 ].categoryinfo
+                                       );
                                } )
                                .promise( { abort: apiPromise.abort } );
                },
                        var apiPromise = this.get( {
                                formatversion: 2,
                                prop: 'categories',
-                               titles: String( title )
+                               titles: [ String( title ) ]
                        } );
 
                        return apiPromise
                                .then( function ( data ) {
-                                       var page = data.query.pages[ 0 ];
+                                       var page;
 
+                                       if ( !data.query || !data.query.pages ) {
+                                               return false;
+                                       }
+                                       page = data.query.pages[ 0 ];
                                        if ( !page.categories ) {
                                                return false;
                                        }
index 8ad1290..50fa6d1 100644 (file)
                        );
                } );
        } );
+
+       QUnit.test( '.isCategory("")', function ( assert ) {
+               this.server.respondWith( /titles=$/, [
+                       200,
+                       { 'Content-Type': 'application/json' },
+                       '{"batchcomplete":true}'
+               ] );
+               return new mw.Api().isCategory( '' ).then( function ( response ) {
+                       assert.equal( response, false );
+               } );
+       } );
+
+       QUnit.test( '.isCategory("#")', function ( assert ) {
+               this.server.respondWith( /titles=%23$/, [
+                       200,
+                       { 'Content-Type': 'application/json' },
+                       '{"batchcomplete":true,"query":{"normalized":[{"fromencoded":false,"from":"#","to":""}]}}'
+               ] );
+               return new mw.Api().isCategory( '#' ).then( function ( response ) {
+                       assert.equal( response, false );
+               } );
+       } );
+
+       QUnit.test( '.isCategory("mw:")', function ( assert ) {
+               this.server.respondWith( /titles=mw%3A$/, [
+                       200,
+                       { 'Content-Type': 'application/json' },
+                       '{"batchcomplete":true,"query":{"interwiki":[{"title":"mw:","iw":"mw"}]}}'
+               ] );
+               return new mw.Api().isCategory( 'mw:' ).then( function ( response ) {
+                       assert.equal( response, false );
+               } );
+       } );
+
+       QUnit.test( '.isCategory("|")', function ( assert ) {
+               this.server.respondWith( /titles=%1F%7C$/, [
+                       200,
+                       { 'Content-Type': 'application/json' },
+                       '{"batchcomplete":true,"query":{"pages":[{"title":"|","invalidreason":"The requested page title contains invalid characters: \\"|\\".","invalid":true}]}}'
+               ] );
+               return new mw.Api().isCategory( '|' ).then( function ( response ) {
+                       assert.equal( response, false );
+               } );
+       } );
+
+       QUnit.test( '.getCategories("")', function ( assert ) {
+               this.server.respondWith( /titles=$/, [
+                       200,
+                       { 'Content-Type': 'application/json' },
+                       '{"batchcomplete":true}'
+               ] );
+               return new mw.Api().getCategories( '' ).then( function ( response ) {
+                       assert.equal( response, false );
+               } );
+       } );
+
+       QUnit.test( '.getCategories("#")', function ( assert ) {
+               this.server.respondWith( /titles=%23$/, [
+                       200,
+                       { 'Content-Type': 'application/json' },
+                       '{"batchcomplete":true,"query":{"normalized":[{"fromencoded":false,"from":"#","to":""}]}}'
+               ] );
+               return new mw.Api().getCategories( '#' ).then( function ( response ) {
+                       assert.equal( response, false );
+               } );
+       } );
+
+       QUnit.test( '.getCategories("mw:")', function ( assert ) {
+               this.server.respondWith( /titles=mw%3A$/, [
+                       200,
+                       { 'Content-Type': 'application/json' },
+                       '{"batchcomplete":true,"query":{"interwiki":[{"title":"mw:","iw":"mw"}]}}'
+               ] );
+               return new mw.Api().getCategories( 'mw:' ).then( function ( response ) {
+                       assert.equal( response, false );
+               } );
+       } );
+
+       QUnit.test( '.getCategories("|")', function ( assert ) {
+               this.server.respondWith( /titles=%1F%7C$/, [
+                       200,
+                       { 'Content-Type': 'application/json' },
+                       '{"batchcomplete":true,"query":{"pages":[{"title":"|","invalidreason":"The requested page title contains invalid characters: \\"|\\".","invalid":true}]}}'
+               ] );
+               return new mw.Api().getCategories( '|' ).then( function ( response ) {
+                       assert.equal( response, false );
+               } );
+       } );
+
 }( mediaWiki ) );