MediaWiki  REL1_22
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         $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() .
00059                     $this->getFrameworkListHtml(),
00060                 'noframework'
00061             );
00062             $out->addHtml( $summary );
00063         } elseif ( isset( self::$frameworks[$framework] ) ) {
00064             // Matched! Display proper title and initialize the framework
00065             $out->setPageTitle( $this->msg(
00066                 'javascripttest-title',
00067                 // Messages: javascripttest-qunit-name
00068                 $this->msg( "javascripttest-$framework-name" )->plain()
00069             ) );
00070             $out->setSubtitle( $this->msg( 'javascripttest-backlink' )
00071                 ->rawParams( Linker::linkKnown( $this->getTitle() ) ) );
00072             $this->{self::$frameworks[$framework]}();
00073         } else {
00074             // Framework not found, display error
00075             $out->setPageTitle( $this->msg( 'javascripttest' ) );
00076             $summary = $this->wrapSummaryHtml(
00077                 '<p class="error">' .
00078                     $this->msg( 'javascripttest-pagetext-unknownframework', $par )->escaped() .
00079                     '</p>' .
00080                     $this->getFrameworkListHtml(),
00081                 'unknownframework'
00082             );
00083             $out->addHtml( $summary );
00084         }
00085     }
00086 
00093     private function getFrameworkListHtml() {
00094         $list = '<ul>';
00095         foreach ( self::$frameworks as $framework => $initFn ) {
00096             $list .= Html::rawElement(
00097                 'li',
00098                 array(),
00099                 Linker::link(
00100                     $this->getTitle( $framework ),
00101                     // Message: javascripttest-qunit-name
00102                     $this->msg( "javascripttest-$framework-name" )->escaped()
00103                 )
00104             );
00105         }
00106         $list .= '</ul>';
00107 
00108         return $this->msg( 'javascripttest-pagetext-frameworks' )->rawParams( $list )
00109             ->parseAsBlock();
00110     }
00111 
00121     private function wrapSummaryHtml( $html, $state ) {
00122         $validStates = array( 'noframework', 'unknownframework', 'frameworkfound' );
00123 
00124         if ( !in_array( $state, $validStates ) ) {
00125             throw new MWException( __METHOD__
00126                 . ' given an invalid state. Must be one of "'
00127                 . join( '", "', $validStates ) . '".'
00128             );
00129         }
00130 
00131         return "<div id=\"mw-javascripttest-summary\" class=\"mw-javascripttest-$state\">$html</div>";
00132     }
00133 
00137     private function initQUnitTesting() {
00138         global $wgJavaScriptTestConfig;
00139 
00140         $out = $this->getOutput();
00141 
00142         $out->addModules( 'mediawiki.tests.qunit.testrunner' );
00143         $qunitTestModules = $out->getResourceLoader()->getTestModuleNames( 'qunit' );
00144         $out->addModules( $qunitTestModules );
00145 
00146         $summary = $this->msg( 'javascripttest-qunit-intro' )
00147             ->params( $wgJavaScriptTestConfig['qunit']['documentation'] )
00148             ->parseAsBlock();
00149         $header = $this->msg( 'javascripttest-qunit-heading' )->escaped();
00150         $userDir = $this->getLanguage()->getDir();
00151 
00152         $baseHtml = <<<HTML
00153 <div class="mw-content-ltr">
00154 <div id="qunit-header"><span dir="$userDir">$header</span></div>
00155 <div id="qunit-banner"></div>
00156 <div id="qunit-testrunner-toolbar"></div>
00157 <div id="qunit-userAgent"></div>
00158 <ol id="qunit-tests"></ol>
00159 <div id="qunit-fixture">test markup, will be hidden</div>
00160 </div>
00161 HTML;
00162         $out->addHtml( $this->wrapSummaryHtml( $summary, 'frameworkfound' ) . $baseHtml );
00163 
00164         // This special page is disabled by default ($wgEnableJavaScriptTest), and contains
00165         // no sensitive data. In order to allow TestSwarm to embed it into a test client window,
00166         // we need to allow iframing of this page.
00167         $out->allowClickjacking();
00168 
00169         // Used in ./tests/qunit/data/testrunner.js, see also documentation of
00170         // $wgJavaScriptTestConfig in DefaultSettings.php
00171         $out->addJsConfigVars(
00172             'QUnitTestSwarmInjectJSPath',
00173             $wgJavaScriptTestConfig['qunit']['testswarm-injectjs']
00174         );
00175     }
00176 
00177     protected function getGroupName() {
00178         return 'other';
00179     }
00180 }