MediaWiki  REL1_20
SpecialJavaScriptTest.php
Go to the documentation of this file.
00001 <?php
00027 class SpecialJavaScriptTest extends SpecialPage {
00028 
00034         static $frameworks = array(
00035                 'qunit' => 'initQUnitTesting',
00036         );
00037 
00038         public function __construct() {
00039                 parent::__construct( 'JavaScriptTest' );
00040         }
00041 
00042         public function execute( $par ) {
00043                 global $wgEnableJavaScriptTest;
00044 
00045                 $out = $this->getOutput();
00046 
00047                 $this->setHeaders();
00048                 $out->disallowUserJs();
00049 
00050                 // Abort early if we're disabled
00051                 if ( $wgEnableJavaScriptTest !== true ) {
00052                         $out->addWikiMsg( 'javascripttest-disabled' );
00053                         return;
00054                 }
00055 
00056                 $out->addModules( 'mediawiki.special.javaScriptTest' );
00057 
00058                 // Determine framework
00059                 $pars = explode( '/', $par );
00060                 $framework = strtolower( $pars[0] );
00061 
00062                 // No framework specified
00063                 if ( $par == '' ) {
00064                         $out->setPageTitle( $this->msg( 'javascripttest' ) );
00065                         $summary = $this->wrapSummaryHtml(
00066                                 $this->msg( 'javascripttest-pagetext-noframework' )->escaped() . $this->getFrameworkListHtml(),
00067                                 'noframework'
00068                         );
00069                         $out->addHtml( $summary );
00070 
00071                 // Matched! Display proper title and initialize the framework
00072                 } elseif ( isset( self::$frameworks[$framework] ) ) {
00073                         $out->setPageTitle( $this->msg( 'javascripttest-title', $this->msg( "javascripttest-$framework-name" )->plain() ) );
00074                         $out->setSubtitle( $this->msg( 'javascripttest-backlink' )->rawParams( Linker::linkKnown( $this->getTitle() ) ) );
00075                         $this->{self::$frameworks[$framework]}();
00076 
00077                 // Framework not found, display error
00078                 } else {
00079                         $out->setPageTitle( $this->msg( 'javascripttest' ) );
00080                         $summary = $this->wrapSummaryHtml( '<p class="error">'
00081                                 . $this->msg( 'javascripttest-pagetext-unknownframework', $par )->escaped()
00082                                 . '</p>'
00083                                 . $this->getFrameworkListHtml(),
00084                                 'unknownframework'
00085                         );
00086                         $out->addHtml( $summary );
00087                 }
00088         }
00089 
00094         private function getFrameworkListHtml() {
00095                 $list = '<ul>';
00096                 foreach( self::$frameworks as $framework => $initFn ) {
00097                         $list .= Html::rawElement(
00098                                 'li',
00099                                 array(),
00100                                 Linker::link( $this->getTitle( $framework ), $this->msg( "javascripttest-$framework-name" )->escaped() )
00101                         );
00102                 }
00103                 $list .= '</ul>';
00104                 $msg = $this->msg( 'javascripttest-pagetext-frameworks' )->rawParams( $list )->parseAsBlock();
00105 
00106                 return $msg;
00107         }
00108 
00117         private function wrapSummaryHtml( $html, $state ) {
00118                 $validStates = array( 'noframework', 'unknownframework', 'frameworkfound' );
00119                 if( !in_array( $state, $validStates ) ) {
00120                         throw new MWException( __METHOD__
00121                                 . ' given an invalid state. Must be one of "'
00122                                 . join( '", "', $validStates) . '".'
00123                         );
00124                 }
00125                 return "<div id=\"mw-javascripttest-summary\" class=\"mw-javascripttest-$state\">$html</div>";
00126         }
00127 
00131         private function initQUnitTesting() {
00132                 global $wgJavaScriptTestConfig;
00133 
00134                 $out = $this->getOutput();
00135 
00136                 $out->addModules( 'mediawiki.tests.qunit.testrunner' );
00137                 $qunitTestModules = $out->getResourceLoader()->getTestModuleNames( 'qunit' );
00138                 $out->addModules( $qunitTestModules );
00139 
00140                 $summary = $this->msg( 'javascripttest-qunit-intro' )
00141                         ->params( $wgJavaScriptTestConfig['qunit']['documentation'] )
00142                         ->parseAsBlock();
00143                 $header = $this->msg( 'javascripttest-qunit-heading' )->escaped();
00144                 $userDir = $this->getLanguage()->getDir();
00145 
00146                 $baseHtml = <<<HTML
00147 <div class="mw-content-ltr">
00148 <div id="qunit-header"><span dir="$userDir">$header</span></div>
00149 <div id="qunit-banner"></div>
00150 <div id="qunit-testrunner-toolbar"></div>
00151 <div id="qunit-userAgent"></div>
00152 <ol id="qunit-tests"></ol>
00153 <div id="qunit-fixture">test markup, will be hidden</div>
00154 </div>
00155 HTML;
00156                 $out->addHtml( $this->wrapSummaryHtml( $summary, 'frameworkfound' ) . $baseHtml );
00157 
00158                 // This special page is disabled by default ($wgEnableJavaScriptTest), and contains
00159                 // no sensitive data. In order to allow TestSwarm to embed it into a test client window,
00160                 // we need to allow iframing of this page.
00161                 $out->allowClickjacking();
00162 
00163                 // Used in ./tests/qunit/data/testrunner.js, see also documentation of
00164                 // $wgJavaScriptTestConfig in DefaultSettings.php
00165                 $out->addJsConfigVars( 'QUnitTestSwarmInjectJSPath', $wgJavaScriptTestConfig['qunit']['testswarm-injectjs'] );
00166         }
00167 
00168         public function isListed(){
00169                 global $wgEnableJavaScriptTest;
00170                 return $wgEnableJavaScriptTest === true;
00171         }
00172 
00173 }