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