mediawiki.Title: Add normalizeExtension method
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.Title.test.js
index 225b5f1..af055be 100644 (file)
                                // testing custom / localized namespace
                                100: 'Penguins'
                        },
+                       // jscs: disable requireCamelCaseOrUpperCaseIdentifiers
                        wgNamespaceIds: {
-                               'media': -2,
-                               'special': -1,
+                               media: -2,
+                               special: -1,
                                '': 0,
-                               'talk': 1,
-                               'user': 2,
-                               'user_talk': 3,
-                               'wikipedia': 4,
-                               'wikipedia_talk': 5,
-                               'file': 6,
-                               'file_talk': 7,
-                               'mediawiki': 8,
-                               'mediawiki_talk': 9,
-                               'template': 10,
-                               'template_talk': 11,
-                               'help': 12,
-                               'help_talk': 13,
-                               'category': 14,
-                               'category_talk': 15,
-                               'image': 6,
-                               'image_talk': 7,
-                               'project': 4,
-                               'project_talk': 5,
+                               talk: 1,
+                               user: 2,
+                               user_talk: 3,
+                               wikipedia: 4,
+                               wikipedia_talk: 5,
+                               file: 6,
+                               file_talk: 7,
+                               mediawiki: 8,
+                               mediawiki_talk: 9,
+                               template: 10,
+                               template_talk: 11,
+                               help: 12,
+                               help_talk: 13,
+                               category: 14,
+                               category_talk: 15,
+                               image: 6,
+                               image_talk: 7,
+                               project: 4,
+                               project_talk: 5,
                                // Testing custom namespaces and aliases
-                               'penguins': 100,
-                               'antarctic_waterfowl': 100
+                               penguins: 100,
+                               antarctic_waterfowl: 100
                        },
+                       // jscs: enable requireCamelCaseOrUpperCaseIdentifiers
                        wgCaseSensitiveNamespaces: []
                }
        } ) );
                }
        } );
 
-       QUnit.test( 'newFromUserInput', 8, function ( assert ) {
+       QUnit.test( 'normalizeExtension', 5, function ( assert ) {
+               var extension, i, thisCase, prefix,
+                       cases = [
+                               {
+                                       extension: 'png',
+                                       expected: 'png',
+                                       description: 'Extension already in canonical form'
+                               },
+                               {
+                                       extension: 'PNG',
+                                       expected: 'png',
+                                       description: 'Extension lowercased in canonical form'
+                               },
+                               {
+                                       extension: 'jpeg',
+                                       expected: 'jpg',
+                                       description: 'Extension changed in canonical form'
+                               },
+                               {
+                                       extension: 'JPEG',
+                                       expected: 'jpg',
+                                       description: 'Extension lowercased and changed in canonical form'
+                               },
+                               {
+                                       extension: '~~~',
+                                       expected: '',
+                                       description: 'Extension invalid and discarded'
+                               }
+                       ];
+
+               for ( i = 0; i < cases.length; i++ ) {
+                       thisCase = cases[ i ];
+                       extension = mw.Title.normalizeExtension( thisCase.extension );
+
+                       prefix = '[' + thisCase.description + '] ';
+                       assert.equal( extension, thisCase.expected, prefix + 'Extension as expected' );
+               }
+       } );
+
+       QUnit.test( 'newFromUserInput', 12, function ( assert ) {
                var title, i, thisCase, prefix,
                        cases = [
                                {
                                        title: 'DCS0001557854455.JPG',
-                                       defaultNamespace: 0,
                                        options: {
                                                fileExtension: 'PNG'
                                        },
                                },
                                {
                                        title: 'MediaWiki:Msg-awesome',
-                                       defaultNamespace: undefined,
                                        expected: 'MediaWiki:Msg-awesome',
                                        description: 'Full title (page in MediaWiki namespace) supplied as string'
                                },
                                        },
                                        expected: 'File:The/Mw/Sound.kml',
                                        description: 'Page in File-namespace without explicit options'
+                               },
+                               {
+                                       title: 'File:Foo.JPEG',
+                                       options: {
+                                               fileExtension: 'jpg'
+                                       },
+                                       expected: 'File:Foo.jpg',
+                                       description: 'Page in File-namespace with non-canonical extension'
+                               },
+                               {
+                                       title: 'File:Foo.JPEG  ',
+                                       options: {
+                                               fileExtension: 'jpg'
+                                       },
+                                       expected: 'File:Foo.jpg',
+                                       description: 'Page in File-namespace with trailing whitespace'
                                }
                        ];
 
                }
        } );
 
-       QUnit.test( 'newFromFileName', 62, function ( assert ) {
+       QUnit.test( 'newFromFileName', 66, function ( assert ) {
                var title, i, thisCase, prefix,
                        cases = [
                                {
                                        fileName: 'DCS0001557854455.JPG',
                                        typeOfName: 'Standard camera output',
                                        nameText: 'DCS0001557854455',
-                                       prefixedText: 'File:DCS0001557854455.JPG',
+                                       prefixedText: 'File:DCS0001557854455.jpg',
                                        extensionDesired: 'jpg'
                                },
                                {
                                        fileName: 'Treppe 2222 Test upload.jpg',
                                        typeOfName: 'File name with spaces in it and lower case file extension',
                                        nameText: 'Treppe 2222 Test upload',
-                                       prefixedText: 'File:Treppe 2222 Test upload.jpg',
+                                       prefixedText: 'File:Treppe 2222 Test upload.JPG',
                                        extensionDesired: 'JPG'
                                },
                                {
                                assert.notStrictEqual( title, null, prefix + 'Parses successfully' );
                                assert.equal( title.getNameText(), thisCase.nameText, prefix + 'Filename matches original' );
                                assert.equal( title.getPrefixedText(), thisCase.prefixedText, prefix + 'File page title matches original' );
+                               if ( thisCase.extensionDesired !== undefined ) {
+                                       assert.equal( title.getExtension(), thisCase.extensionDesired, prefix + 'Extension matches desired' );
+                               }
                                assert.equal( title.getNamespaceId(), 6, prefix + 'Namespace ID matches File namespace' );
                        } else {
                                assert.strictEqual( title, null, thisCase.typeOfName + ', should not produce an mw.Title object' );