MarkTraceur has uploaded a new change for review. https://gerrit.wikimedia.org/r/146140
Change subject: Test UploadWizardUtil and move single-use things to other classes ...................................................................... Test UploadWizardUtil and move single-use things to other classes Change-Id: I2913e2b218cb3c206c1b677f7c8d87dcc9cbcf76 Phabricator: http://fab.wmflabs.org/T452 --- M UploadWizardHooks.php M resources/mw.UploadWizardDetails.js M resources/mw.UploadWizardUpload.js M resources/mw.UploadWizardUtil.js A tests/qunit/mw.UploadWizardUtil.test.js 5 files changed, 130 insertions(+), 110 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/UploadWizard refs/changes/40/146140/1 diff --git a/UploadWizardHooks.php b/UploadWizardHooks.php index 97cae53..0f32680 100644 --- a/UploadWizardHooks.php +++ b/UploadWizardHooks.php @@ -660,6 +660,7 @@ $testModules['qunit']['ext.uploadWizard.unit.tests'] = array( 'scripts' => array( 'tests/qunit/mw.UploadWizard.test.js', + 'tests/qunit/mw.UploadWizardUtil.test.js', 'tests/qunit/mw.UploadWizardLicenseInput.test.js', 'tests/qunit/mw.FlickrChecker.test.js', ), diff --git a/resources/mw.UploadWizardDetails.js b/resources/mw.UploadWizardDetails.js index fefffd4..dd4cd09 100644 --- a/resources/mw.UploadWizardDetails.js +++ b/resources/mw.UploadWizardDetails.js @@ -399,7 +399,7 @@ } ); */ - mw.UploadWizardUtil.makeToggler( + this.makeToggler( moreDetailsCtrlDiv, moreDetailsDiv, 'mwe-upwiz-more-options' @@ -775,7 +775,7 @@ ) ); - mw.UploadWizardUtil.makeToggler( + this.makeToggler( this.copyMetadataCtrlDiv, $copyMetadataDiv, 'mwe-upwiz-copy-metadata' @@ -1606,6 +1606,49 @@ .addClass( 'mwe-upwiz-status-' + statusStr ); }, + /** + * Simple 'more options' toggle that opens more of a form. + * + * @param {jQuery} $toggleDiv the div which has the control to open and shut custom options + * @param {jQuery} $moreDiv the div containing the custom options + * @param {string} msg the UI message key to use for the toggler + * (with mwe-upwiz- prefix for UploadWizard messages) + */ + makeToggler: function ( $toggleDiv, $moreDiv, msg ) { + function toggle() { + var isOpen = $toggleLink.hasClass( 'mwe-upwiz-toggler-open' ); + if ( isOpen ) { + // hide the extra options + $moreDiv.slideUp( 250 ); + /* when closed, show control to open */ + $toggleLink.removeClass( 'mwe-upwiz-toggler-open' ); + } else { + // show the extra options + $moreDiv.slideDown( 250 ); + /* when open, show control to close */ + $toggleLink.addClass( 'mwe-upwiz-toggler-open' ); + } + } + + var $toggleLink, actualMsg; + + if ( typeof msg === 'object' ) { + actualMsg = mw.message.apply( this, msg ).text(); + } else { + actualMsg = mw.message( msg ).text(); + } + $toggleLink = $( '<a>' ) + .addClass( 'mwe-upwiz-toggler mwe-upwiz-more-options' ) + .text( actualMsg ); + $toggleDiv.append( $toggleLink ); + + $moreDiv.hide(); + + $toggleLink.click( function( e ) { e.stopPropagation(); toggle(); } ); + + $moreDiv.addClass( 'mwe-upwiz-toggled' ); + }, + dateInputCount: 0, /** diff --git a/resources/mw.UploadWizardUpload.js b/resources/mw.UploadWizardUpload.js index 364155c..f35ef33 100644 --- a/resources/mw.UploadWizardUpload.js +++ b/resources/mw.UploadWizardUpload.js @@ -335,6 +335,23 @@ }, /** + * Get the basename of a path. + * For error conditions, returns the empty string. + * + * @param {string} path + * @return {string} basename + */ + getBasename: function( path ) { + if ( path === undefined || path === null ) { + return ''; + } + + // find index of last path separator in the path, add 1. (If no separator found, yields 0) + // then take the entire string after that. + return path.substr( Math.max( path.lastIndexOf( '/' ), path.lastIndexOf( '\\' ) ) + 1 ); + }, + + /** * Called when the file is entered into the file input, bound to its change() event. * Checks for file validity, then extracts metadata. * Error out if filename or its contents are determined to be unacceptable @@ -359,7 +376,7 @@ // Check if filename is acceptable // TODO sanitize filename - basename = mw.UploadWizardUtil.getBasename( filename ), + basename = this.getBasename( filename ), tooManyFiles = files.length + this.wizard.uploads.length > mw.UploadWizard.config.maxUploads; function finishCallback() { @@ -680,24 +697,6 @@ this.imageinfo[key] = imageinfo[key]; } } - - /* BEFORE YOU UNCOMMENT: declare the extension variable at the top of this function. - if ( this.title.getExtension() === null ) { - // 1; - // TODO v1.1 what if we don't have an extension? Should be impossible as it is currently impossible to upload without extension, but you - // never know... theoretically there is no restriction on extensions if we are uploading to the stash, but the check is performed anyway. - - var extension = mw.UploadWizardUtil.getExtension( this.imageinfo.url ); - if ( !extension ) { - if ( this.imageinfo.mimetype ) { - if ( mw.UploadWizardUtil.mimetypeToExtension[ this.imageinfo.mimetype ] ) { - extension = mw.UploadWizardUtil.mimetypeToExtension[ this.imageinfo.mimetype ]; - } - } - } - - } - */ }, /** diff --git a/resources/mw.UploadWizardUtil.js b/resources/mw.UploadWizardUtil.js index fba14ce..a0b1045 100644 --- a/resources/mw.UploadWizardUtil.js +++ b/resources/mw.UploadWizardUtil.js @@ -1,96 +1,24 @@ /** + * @class mw.UploadWizardUtil + * @singleton * Miscellaneous utilities */ ( function ( mw, $ ) { -mw.UploadWizardUtil = { - - /** - * Simple 'more options' toggle that opens more of a form. - * - * @param toggleDiv the div which has the control to open and shut custom options - * @param moreDiv the div containing the custom options - * @param msg the UI message key to use for the toggler - * (with mwe-upwiz- prefix for UploadWizard messages) - */ - makeToggler: function ( toggleDiv, moreDiv, msg ) { - function toggle() { - var isOpen = $toggleLink.hasClass( 'mwe-upwiz-toggler-open' ); - if ( isOpen ) { - // hide the extra options - moreDiv.slideUp( 250 ); - /* when closed, show control to open */ - $toggleLink.removeClass( 'mwe-upwiz-toggler-open' ); - } else { - // show the extra options - moreDiv.slideDown( 250 ); - /* when open, show control to close */ - $toggleLink.addClass( 'mwe-upwiz-toggler-open' ); + mw.UploadWizardUtil = { + /** + * Remove an item from an array. Tests for === identity to remove the item + * XXX the entire rationale for this function may be wrong. + * XXX The jQuery way would be to query the DOM for objects, not to keep a separate array hanging around + * @param {Array} items Where we want to remove an item + * @param {Mixed} item The item to remove + */ + removeItem: function( items, item ) { + for ( var i = 0; i < items.length; i++ ) { + if ( items[i] === item ) { + items.splice( i, 1 ); + break; + } } } - - var $toggleLink, actualMsg; - - if ( typeof msg === 'object' ) { - actualMsg = mw.message.apply( this, msg ).text(); - } else { - actualMsg = mw.message( msg ).text(); - } - $toggleLink = $( '<a>' ) - .addClass( 'mwe-upwiz-toggler mwe-upwiz-more-options' ) - .text( actualMsg ); - $( toggleDiv ).append( $toggleLink ); - - moreDiv.hide(); - - $toggleLink.click( function( e ) { e.stopPropagation(); toggle(); } ); - - $( moreDiv ).addClass( 'mwe-upwiz-toggled' ); - }, - - /** - * remove an item from an array. Tests for === identity to remove the item - * XXX the entire rationale for this function may be wrong. - * XXX The jQuery way would be to query the DOM for objects, not to keep a separate array hanging around - * @param items the array where we want to remove an item - * @param item the item to remove - */ - removeItem: function( items, item ) { - for ( var i = 0; i < items.length; i++ ) { - if ( items[i] === item ) { - items.splice( i, 1 ); - break; - } - } - }, - - /** - * Get the basename of a path. - * For error conditions, returns the empty string. - * - * @param {String} path - * @return {String} basename - */ - getBasename: function( path ) { - if ( path === undefined || path === null ) { - return ''; - } - - // find index of last path separator in the path, add 1. (If no separator found, yields 0) - // then take the entire string after that. - return path.substr( Math.max( path.lastIndexOf( '/' ), path.lastIndexOf( '\\' ) ) + 1 ); - }, - - - - /** - * Last resort to guess a proper extension - */ - mimetypeToExtension: { - 'image/jpeg': 'jpg', - 'image/gif': 'gif' - // fill as needed - } - - -}; + }; }( mediaWiki, jQuery ) ); diff --git a/tests/qunit/mw.UploadWizardUtil.test.js b/tests/qunit/mw.UploadWizardUtil.test.js new file mode 100644 index 0000000..2b0b5d3 --- /dev/null +++ b/tests/qunit/mw.UploadWizardUtil.test.js @@ -0,0 +1,49 @@ +/* + * This file is part of the MediaWiki extension UploadWizard. + * + * UploadWizard is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * UploadWizard is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with UploadWizard. If not, see <http://www.gnu.org/licenses/>. + */ + +( function ( mw ) { + QUnit.module( 'mw.UploadWizardUtil', QUnit.newMwEnvironment() ); + + QUnit.test( 'removeItem', 3, function ( assert ) { + var arr = [ 1, 2, 3, 4, 5 ]; + mw.UploadWizardUtil.removeItem( arr, 3 ); + + assert.deepEqual( + arr, + [ 1, 2, 4, 5 ], + 'Removing number from array works' + ); + + arr = [ 'One', 'Two', 'Three' ]; + mw.UploadWizardUtil.removeItem( arr, 'Three' ); + + assert.deepEqual( + arr, + [ 'One', 'Two' ], + 'Removing string from array works' + ); + + arr.push( 'One' ); + mw.UploadWizardUtil.removeItem( arr, 'One' ); + + assert.deepEqual( + arr, + [ 'Two', 'One' ], + 'Only the first match is removed' + ); + } ); +}( mediaWiki, jQuery ) ); -- To view, visit https://gerrit.wikimedia.org/r/146140 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I2913e2b218cb3c206c1b677f7c8d87dcc9cbcf76 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/UploadWizard Gerrit-Branch: master Gerrit-Owner: MarkTraceur <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
