3 * Implements Special:JavaScriptTest
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * @ingroup SpecialPage
27 class SpecialJavaScriptTest
extends SpecialPage
{
30 * @var $frameworks Array: Mapping of framework ids and their initilizer methods
31 * in this class. If a framework is requested but not in this array,
32 * the 'unknownframework' error is served.
34 static $frameworks = array(
35 'qunit' => 'initQUnitTesting',
38 public function __construct() {
39 parent
::__construct( 'JavaScriptTest' );
42 public function execute( $par ) {
43 $out = $this->getOutput();
46 $out->disallowUserJs();
48 $out->addModules( 'mediawiki.special.javaScriptTest' );
50 // Determine framework
51 $pars = explode( '/', $par );
52 $framework = strtolower( $pars[0] );
54 // No framework specified
56 $out->setPageTitle( $this->msg( 'javascripttest' ) );
57 $summary = $this->wrapSummaryHtml(
58 $this->msg( 'javascripttest-pagetext-noframework' )->escaped() . $this->getFrameworkListHtml(),
61 $out->addHtml( $summary );
63 // Matched! Display proper title and initialize the framework
64 } elseif ( isset( self
::$frameworks[$framework] ) ) {
65 $out->setPageTitle( $this->msg( 'javascripttest-title', $this->msg( "javascripttest-$framework-name" )->plain() ) );
66 $out->setSubtitle( $this->msg( 'javascripttest-backlink' )->rawParams( Linker
::linkKnown( $this->getTitle() ) ) );
67 $this->{self
::$frameworks[$framework]}();
69 // Framework not found, display error
71 $out->setPageTitle( $this->msg( 'javascripttest' ) );
72 $summary = $this->wrapSummaryHtml( '<p class="error">'
73 . $this->msg( 'javascripttest-pagetext-unknownframework', $par )->escaped()
75 . $this->getFrameworkListHtml(),
78 $out->addHtml( $summary );
83 * Get a list of frameworks (including introduction paragraph and links to the framework run pages)
84 * @return String: HTML
86 private function getFrameworkListHtml() {
88 foreach ( self
::$frameworks as $framework => $initFn ) {
89 $list .= Html
::rawElement(
92 Linker
::link( $this->getTitle( $framework ), $this->msg( "javascripttest-$framework-name" )->escaped() )
96 $msg = $this->msg( 'javascripttest-pagetext-frameworks' )->rawParams( $list )->parseAsBlock();
102 * Function to wrap the summary.
103 * It must be given a valid state as a second parameter or an exception will
105 * @param string $html The raw HTML.
106 * @param string $state State, one of 'noframework', 'unknownframework' or 'frameworkfound'
107 * @throws MWException
110 private function wrapSummaryHtml( $html, $state ) {
111 $validStates = array( 'noframework', 'unknownframework', 'frameworkfound' );
112 if ( !in_array( $state, $validStates ) ) {
113 throw new MWException( __METHOD__
114 . ' given an invalid state. Must be one of "'
115 . join( '", "', $validStates ) . '".'
118 return "<div id=\"mw-javascripttest-summary\" class=\"mw-javascripttest-$state\">$html</div>";
122 * Initialize the page for QUnit.
124 private function initQUnitTesting() {
125 global $wgJavaScriptTestConfig;
127 $out = $this->getOutput();
129 $out->addModules( 'mediawiki.tests.qunit.testrunner' );
130 $qunitTestModules = $out->getResourceLoader()->getTestModuleNames( 'qunit' );
131 $out->addModules( $qunitTestModules );
133 $summary = $this->msg( 'javascripttest-qunit-intro' )
134 ->params( $wgJavaScriptTestConfig['qunit']['documentation'] )
136 $header = $this->msg( 'javascripttest-qunit-heading' )->escaped();
137 $userDir = $this->getLanguage()->getDir();
140 <div class="mw-content-ltr">
141 <div id="qunit-header"><span dir="$userDir">$header</span></div>
142 <div id="qunit-banner"></div>
143 <div id="qunit-testrunner-toolbar"></div>
144 <div id="qunit-userAgent"></div>
145 <ol id="qunit-tests"></ol>
146 <div id="qunit-fixture">test markup, will be hidden</div>
149 $out->addHtml( $this->wrapSummaryHtml( $summary, 'frameworkfound' ) . $baseHtml );
151 // This special page is disabled by default ($wgEnableJavaScriptTest), and contains
152 // no sensitive data. In order to allow TestSwarm to embed it into a test client window,
153 // we need to allow iframing of this page.
154 $out->allowClickjacking();
156 // Used in ./tests/qunit/data/testrunner.js, see also documentation of
157 // $wgJavaScriptTestConfig in DefaultSettings.php
158 $out->addJsConfigVars( 'QUnitTestSwarmInjectJSPath', $wgJavaScriptTestConfig['qunit']['testswarm-injectjs'] );
161 protected function getGroupName() {