MediaWiki
REL1_21
|
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 $out = $this->getOutput(); 00044 00045 $this->setHeaders(); 00046 $out->disallowUserJs(); 00047 00048 $out->addModules( 'mediawiki.special.javaScriptTest' ); 00049 00050 // Determine framework 00051 $pars = explode( '/', $par ); 00052 $framework = strtolower( $pars[0] ); 00053 00054 // No framework specified 00055 if ( $par == '' ) { 00056 $out->setPageTitle( $this->msg( 'javascripttest' ) ); 00057 $summary = $this->wrapSummaryHtml( 00058 $this->msg( 'javascripttest-pagetext-noframework' )->escaped() . $this->getFrameworkListHtml(), 00059 'noframework' 00060 ); 00061 $out->addHtml( $summary ); 00062 00063 // Matched! Display proper title and initialize the framework 00064 } elseif ( isset( self::$frameworks[$framework] ) ) { 00065 $out->setPageTitle( $this->msg( 'javascripttest-title', $this->msg( "javascripttest-$framework-name" )->plain() ) ); 00066 $out->setSubtitle( $this->msg( 'javascripttest-backlink' )->rawParams( Linker::linkKnown( $this->getTitle() ) ) ); 00067 $this->{self::$frameworks[$framework]}(); 00068 00069 // Framework not found, display error 00070 } else { 00071 $out->setPageTitle( $this->msg( 'javascripttest' ) ); 00072 $summary = $this->wrapSummaryHtml( '<p class="error">' 00073 . $this->msg( 'javascripttest-pagetext-unknownframework', $par )->escaped() 00074 . '</p>' 00075 . $this->getFrameworkListHtml(), 00076 'unknownframework' 00077 ); 00078 $out->addHtml( $summary ); 00079 } 00080 } 00081 00086 private function getFrameworkListHtml() { 00087 $list = '<ul>'; 00088 foreach( self::$frameworks as $framework => $initFn ) { 00089 $list .= Html::rawElement( 00090 'li', 00091 array(), 00092 Linker::link( $this->getTitle( $framework ), $this->msg( "javascripttest-$framework-name" )->escaped() ) 00093 ); 00094 } 00095 $list .= '</ul>'; 00096 $msg = $this->msg( 'javascripttest-pagetext-frameworks' )->rawParams( $list )->parseAsBlock(); 00097 00098 return $msg; 00099 } 00100 00110 private function wrapSummaryHtml( $html, $state ) { 00111 $validStates = array( 'noframework', 'unknownframework', 'frameworkfound' ); 00112 if( !in_array( $state, $validStates ) ) { 00113 throw new MWException( __METHOD__ 00114 . ' given an invalid state. Must be one of "' 00115 . join( '", "', $validStates) . '".' 00116 ); 00117 } 00118 return "<div id=\"mw-javascripttest-summary\" class=\"mw-javascripttest-$state\">$html</div>"; 00119 } 00120 00124 private function initQUnitTesting() { 00125 global $wgJavaScriptTestConfig; 00126 00127 $out = $this->getOutput(); 00128 00129 $out->addModules( 'mediawiki.tests.qunit.testrunner' ); 00130 $qunitTestModules = $out->getResourceLoader()->getTestModuleNames( 'qunit' ); 00131 $out->addModules( $qunitTestModules ); 00132 00133 $summary = $this->msg( 'javascripttest-qunit-intro' ) 00134 ->params( $wgJavaScriptTestConfig['qunit']['documentation'] ) 00135 ->parseAsBlock(); 00136 $header = $this->msg( 'javascripttest-qunit-heading' )->escaped(); 00137 $userDir = $this->getLanguage()->getDir(); 00138 00139 $baseHtml = <<<HTML 00140 <div class="mw-content-ltr"> 00141 <div id="qunit-header"><span dir="$userDir">$header</span></div> 00142 <div id="qunit-banner"></div> 00143 <div id="qunit-testrunner-toolbar"></div> 00144 <div id="qunit-userAgent"></div> 00145 <ol id="qunit-tests"></ol> 00146 <div id="qunit-fixture">test markup, will be hidden</div> 00147 </div> 00148 HTML; 00149 $out->addHtml( $this->wrapSummaryHtml( $summary, 'frameworkfound' ) . $baseHtml ); 00150 00151 // This special page is disabled by default ($wgEnableJavaScriptTest), and contains 00152 // no sensitive data. In order to allow TestSwarm to embed it into a test client window, 00153 // we need to allow iframing of this page. 00154 $out->allowClickjacking(); 00155 00156 // Used in ./tests/qunit/data/testrunner.js, see also documentation of 00157 // $wgJavaScriptTestConfig in DefaultSettings.php 00158 $out->addJsConfigVars( 'QUnitTestSwarmInjectJSPath', $wgJavaScriptTestConfig['qunit']['testswarm-injectjs'] ); 00159 } 00160 00161 protected function getGroupName() { 00162 return 'other'; 00163 } 00164 }