resourceloader: Add tests for disallowing access to private modules
[lhc/web/wiklou.git] / resources / src / startup / mediawiki.js
index b0355b0..ab7f3a0 100644 (file)
         * @class mw
         */
        mw = {
-               redefineFallbacksForTest: function () {
-                       if ( !window.QUnit ) {
-                               throw new Error( 'Not allowed' );
-                       }
-                       defineFallbacks();
-               },
+               redefineFallbacksForTest: window.QUnit && defineFallbacks,
 
                /**
                 * Get the current time, measured in milliseconds since January 1, 1970 (UTC).
                 */
                config: new Map( $VARS.wgLegacyJavaScriptGlobals ),
 
-               /**
-                * Empty object for third-party libraries, for cases where you don't
-                * want to add a new global, or the global is bad and needs containment
-                * or wrapping.
-                *
-                * @property
-                */
-               libs: {},
-
                /**
                 * Store for messages.
                 *
                                        }
 
                                        if ( !hasOwn.call( scriptFiles, fileName ) ) {
-                                               throw new Error( 'Cannot require() undefined file ' + fileName );
+                                               throw new Error( 'Cannot require undefined file ' + fileName );
                                        }
                                        if ( hasOwn.call( moduleObj.packageExports, fileName ) ) {
                                                // File has already been executed, return the cached result
 
                                dependencies.forEach( function ( module ) {
                                        // Only queue modules that are still in the initial 'registered' state
-                                       // (not ones already loading, ready or error).
+                                       // (e.g. not ones already loading or loaded etc.).
                                        if ( registry[ module ].state === 'registered' && queue.indexOf( module ) === -1 ) {
-                                               // Private modules must be embedded in the page. Don't bother queuing
-                                               // these as the server will deny them anyway (T101806).
-                                               if ( registry[ module ].group === 'private' ) {
-                                                       setAndPropagate( module, 'error' );
-                                               } else {
-                                                       queue.push( module );
-                                               }
+                                               queue.push( module );
                                        }
                                } );
 
                                        cssPending = 0;
 
                                if ( registry[ module ].state !== 'loaded' ) {
-                                       throw new Error( 'Module in state "' + registry[ module ].state + '" may not be executed: ' + module );
+                                       throw new Error( 'Module in state "' + registry[ module ].state + '" may not execute: ' + module );
                                }
 
                                registry[ module ].state = 'executing';
                                                        } else {
                                                                mainScript = script.files[ script.main ];
                                                                if ( typeof mainScript !== 'function' ) {
-                                                                       throw new Error( 'Main file ' + script.main + ' in module ' + module +
-                                                                               ' must be of type function, found ' + typeof mainScript );
+                                                                       throw new Error( 'Main file in module ' + module + ' must be a function' );
                                                                }
                                                                // jQuery parameters are not passed for multi-file modules
                                                                mainScript(
                                 * @param {string} [type='text/javascript'] MIME type to use if calling with a URL of an
                                 *  external script or style; acceptable values are "text/css" and
                                 *  "text/javascript"; if no type is provided, text/javascript is assumed.
+                                * @throws {Error} If type is invalid
                                 */
                                load: function ( modules, type ) {
                                        if ( typeof modules === 'string' && /^(https?:)?\/?\//.test( modules ) ) {
                                                        addScript( modules );
                                                } else {
                                                        // Unknown type
-                                                       throw new Error( 'type must be text/css or text/javascript, found ' + type );
+                                                       throw new Error( 'Invalid type ' + type );
                                                }
                                        } else {
                                                // One or more modules
                                         * @return {Object} Module store contents.
                                         */
                                        toJSON: function () {
-                                               return { items: mw.loader.store.items, vary: mw.loader.store.getVary() };
+                                               return { items: mw.loader.store.items, vary: mw.loader.store.vary };
                                        },
 
                                        /**
-                                        * Get the localStorage key for the entire module store. The key references
+                                        * The localStorage key for the entire module store. The key references
                                         * $wgDBname to prevent clashes between wikis which share a common host.
                                         *
-                                        * @return {string} localStorage item key
+                                        * @property {string}
                                         */
-                                       getStoreKey: function () {
-                                               return $VARS.storeKey;
-                                       },
+                                       key: $VARS.storeKey,
 
                                        /**
-                                        * Get a key on which to vary the module cache.
+                                        * A string containing various factors on which to the module cache should vary.
                                         *
-                                        * @return {string} String of concatenated vary conditions.
+                                        * @property {string}
                                         */
-                                       getVary: function () {
-                                               return $VARS.storeVary;
-                                       },
+                                       vary: $VARS.storeVary,
 
                                        /**
                                         * Initialize the store.
 
                                                try {
                                                        // This a string we stored, or `null` if the key does not (yet) exist.
-                                                       raw = localStorage.getItem( this.getStoreKey() );
+                                                       raw = localStorage.getItem( this.key );
                                                        // If we get here, localStorage is available; mark enabled
                                                        this.enabled = true;
                                                        // If null, JSON.parse() will cast to string and re-parse, still null.
                                                        data = JSON.parse( raw );
-                                                       if ( data && typeof data.items === 'object' && data.vary === this.getVary() ) {
+                                                       if ( data && typeof data.items === 'object' && data.vary === this.vary ) {
                                                                this.items = data.items;
                                                                return;
                                                        }
                                                //    The store was enabled, and `items` starts fresh.
                                                //
                                                // 2. localStorage contained parseable data under our store key,
-                                               //    but it's not applicable to our current context (see getVary).
+                                               //    but it's not applicable to our current context (see #vary).
                                                //    The store was enabled, and `items` starts fresh.
                                                //
                                                // 3. JSON.parse threw (localStorage contained corrupt data).
                                        clear: function () {
                                                this.items = {};
                                                try {
-                                                       localStorage.removeItem( this.getStoreKey() );
+                                                       localStorage.removeItem( this.key );
                                                } catch ( e ) {}
                                        },
 
                                                                mw.loader.store.set( mw.loader.store.queue.shift() );
                                                        }
 
-                                                       key = mw.loader.store.getStoreKey();
+                                                       key = mw.loader.store.key;
                                                        try {
                                                                // Replacing the content of the module store might fail if the new
                                                                // contents would exceed the browser's localStorage size limit. To
                         * @property {mw.Map}
                         */
                        tokens: new Map()
-               },
-
-               // OOUI widgets specific to MediaWiki
-               widgets: {}
+               }
 
        };