MediaWiki  REL1_19
SpecialJavaScriptTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class SpecialJavaScriptTest extends SpecialPage {
00004 
00010         static $frameworks = array(
00011                 'qunit' => 'initQUnitTesting',
00012         );
00013 
00014         public function __construct() {
00015                 parent::__construct( 'JavaScriptTest' );
00016         }
00017 
00018         public function execute( $par ) {
00019                 global $wgEnableJavaScriptTest;
00020 
00021                 $out = $this->getOutput();
00022 
00023                 $this->setHeaders();
00024                 $out->disallowUserJs();
00025 
00026                 // Abort early if we're disabled
00027                 if ( $wgEnableJavaScriptTest !== true ) {
00028                         $out->addWikiMsg( 'javascripttest-disabled' );
00029                         return;
00030                 }
00031 
00032                 $out->addModules( 'mediawiki.special.javaScriptTest' );
00033 
00034                 // Determine framework
00035                 $pars = explode( '/', $par );
00036                 $framework = strtolower( $pars[0] );
00037 
00038                 // No framework specified
00039                 if ( $par == '' ) {
00040                         $out->setPagetitle( wfMsgHtml( 'javascripttest' ) );
00041                         $summary = $this->wrapSummaryHtml(
00042                                 wfMsgHtml( 'javascripttest-pagetext-noframework' ) . $this->getFrameworkListHtml(),
00043                                 'noframework'
00044                         );
00045                         $out->addHtml( $summary );
00046 
00047                 // Matched! Display proper title and initialize the framework
00048                 } elseif ( isset( self::$frameworks[$framework] ) ) {
00049                         $out->setPagetitle( wfMsgHtml( 'javascripttest-title', wfMsgHtml( "javascripttest-$framework-name" ) ) );
00050                         $out->setSubtitle(
00051                                 wfMessage( 'javascripttest-backlink' )->rawParams( Linker::linkKnown( $this->getTitle() ) )->escaped()
00052                         );
00053                         $this->{self::$frameworks[$framework]}();
00054 
00055                 // Framework not found, display error
00056                 } else {
00057                         $out->setPagetitle( wfMsgHtml( 'javascripttest' ) );
00058                         $summary = $this->wrapSummaryHtml( '<p class="error">'
00059                                 . wfMsgHtml( 'javascripttest-pagetext-unknownframework', $par )
00060                                 . '</p>'
00061                                 . $this->getFrameworkListHtml(),
00062                                 'unknownframework'
00063                         );
00064                         $out->addHtml( $summary );
00065                 }
00066         }
00067 
00072         private function getFrameworkListHtml() {
00073                 $list = '<ul>';
00074                 foreach( self::$frameworks as $framework => $initFn ) {
00075                         $list .= Html::rawElement(
00076                                 'li',
00077                                 array(),
00078                                 Linker::link( $this->getTitle( $framework ), wfMsgHtml( "javascripttest-$framework-name" ) )
00079                         );
00080                 }
00081                 $list .= '</ul>';
00082                 $msg = wfMessage( 'javascripttest-pagetext-frameworks' )->rawParams( $list )->parseAsBlock();
00083 
00084                 return $msg;
00085         }
00086 
00094         private function wrapSummaryHtml( $html, $state ) {
00095                 $validStates = array( 'noframework', 'unknownframework', 'frameworkfound' );
00096                 if( !in_array( $state, $validStates ) ) {
00097                         throw new MWException( __METHOD__
00098                                 . ' given an invalid state. Must be one of "'
00099                                 . join( '", "', $validStates) . '".'
00100                         );
00101                 }
00102                 return "<div id=\"mw-javascripttest-summary\" class=\"mw-javascripttest-$state\">$html</div>";
00103         }
00104 
00108         private function initQUnitTesting() {
00109                 global $wgJavaScriptTestConfig, $wgLang;
00110 
00111                 $out = $this->getOutput();
00112 
00113                 $out->addModules( 'mediawiki.tests.qunit.testrunner' );
00114                 $qunitTestModules = $out->getResourceLoader()->getTestModuleNames( 'qunit' );
00115                 $out->addModules( $qunitTestModules );
00116 
00117                 $summary = wfMessage( 'javascripttest-qunit-intro' )
00118                         ->params( $wgJavaScriptTestConfig['qunit']['documentation'] )
00119                         ->parseAsBlock();
00120                 $header = wfMessage( 'javascripttest-qunit-heading' )->escaped();
00121                 $userDir = $wgLang->getDir();
00122 
00123                 $baseHtml = <<<HTML
00124 <div class="mw-content-ltr">
00125 <div id="qunit-header"><span dir="$userDir">$header</span></div>
00126 <div id="qunit-banner"></div>
00127 <div id="qunit-testrunner-toolbar"></div>
00128 <div id="qunit-userAgent"></div>
00129 <ol id="qunit-tests"></ol>
00130 <div id="qunit-fixture">test markup, will be hidden</div>
00131 </div>
00132 HTML;
00133                 $out->addHtml( $this->wrapSummaryHtml( $summary, 'frameworkfound' ) . $baseHtml );
00134 
00135         }
00136 
00137         public function isListed(){
00138                 global $wgEnableJavaScriptTest;
00139                 return $wgEnableJavaScriptTest === true;
00140         }
00141 
00142 }