DifferenceEngine cleanup
[mediawiki.git] / includes / specials / SpecialJavaScriptTest.php
blob7069d5270e89ad5d2b6cdfb5ecf2250b18c40a31
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * @ingroup SpecialPage
27 class SpecialJavaScriptTest extends SpecialPage {
29 /**
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();
45 $this->setHeaders();
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
55 if ( $par == '' ) {
56 $out->setPageTitle( $this->msg( 'javascripttest' ) );
57 $summary = $this->wrapSummaryHtml(
58 $this->msg( 'javascripttest-pagetext-noframework' )->escaped() .
59 $this->getFrameworkListHtml(),
60 'noframework'
62 $out->addHtml( $summary );
63 } elseif ( isset( self::$frameworks[$framework] ) ) {
64 // Matched! Display proper title and initialize the framework
65 $out->setPageTitle( $this->msg(
66 'javascripttest-title',
67 // Messages: javascripttest-qunit-name
68 $this->msg( "javascripttest-$framework-name" )->plain()
69 ) );
70 $out->setSubtitle( $this->msg( 'javascripttest-backlink' )
71 ->rawParams( Linker::linkKnown( $this->getTitle() ) ) );
72 $this->{self::$frameworks[$framework]}();
73 } else {
74 // Framework not found, display error
75 $out->setPageTitle( $this->msg( 'javascripttest' ) );
76 $summary = $this->wrapSummaryHtml(
77 '<p class="error">' .
78 $this->msg( 'javascripttest-pagetext-unknownframework', $par )->escaped() .
79 '</p>' .
80 $this->getFrameworkListHtml(),
81 'unknownframework'
83 $out->addHtml( $summary );
87 /**
88 * Get a list of frameworks (including introduction paragraph and links
89 * to the framework run pages)
91 * @return string HTML
93 private function getFrameworkListHtml() {
94 $list = '<ul>';
95 foreach ( self::$frameworks as $framework => $initFn ) {
96 $list .= Html::rawElement(
97 'li',
98 array(),
99 Linker::link(
100 $this->getTitle( $framework ),
101 // Message: javascripttest-qunit-name
102 $this->msg( "javascripttest-$framework-name" )->escaped()
106 $list .= '</ul>';
108 return $this->msg( 'javascripttest-pagetext-frameworks' )->rawParams( $list )
109 ->parseAsBlock();
113 * Function to wrap the summary.
114 * It must be given a valid state as a second parameter or an exception will
115 * be thrown.
116 * @param string $html The raw HTML.
117 * @param string $state State, one of 'noframework', 'unknownframework' or 'frameworkfound'
118 * @throws MWException
119 * @return string
121 private function wrapSummaryHtml( $html, $state ) {
122 $validStates = array( 'noframework', 'unknownframework', 'frameworkfound' );
124 if ( !in_array( $state, $validStates ) ) {
125 throw new MWException( __METHOD__
126 . ' given an invalid state. Must be one of "'
127 . join( '", "', $validStates ) . '".'
131 return "<div id=\"mw-javascripttest-summary\" class=\"mw-javascripttest-$state\">$html</div>";
135 * Initialize the page for QUnit.
137 private function initQUnitTesting() {
138 global $wgJavaScriptTestConfig;
140 $out = $this->getOutput();
142 $out->addModules( 'mediawiki.tests.qunit.testrunner' );
143 $qunitTestModules = $out->getResourceLoader()->getTestModuleNames( 'qunit' );
144 $out->addModules( $qunitTestModules );
146 $summary = $this->msg( 'javascripttest-qunit-intro' )
147 ->params( $wgJavaScriptTestConfig['qunit']['documentation'] )
148 ->parseAsBlock();
149 $header = $this->msg( 'javascripttest-qunit-heading' )->escaped();
150 $userDir = $this->getLanguage()->getDir();
152 $baseHtml = <<<HTML
153 <div class="mw-content-ltr">
154 <div id="qunit-header"><span dir="$userDir">$header</span></div>
155 <div id="qunit-banner"></div>
156 <div id="qunit-testrunner-toolbar"></div>
157 <div id="qunit-userAgent"></div>
158 <ol id="qunit-tests"></ol>
159 <div id="qunit-fixture">test markup, will be hidden</div>
160 </div>
161 HTML;
162 $out->addHtml( $this->wrapSummaryHtml( $summary, 'frameworkfound' ) . $baseHtml );
164 // This special page is disabled by default ($wgEnableJavaScriptTest), and contains
165 // no sensitive data. In order to allow TestSwarm to embed it into a test client window,
166 // we need to allow iframing of this page.
167 $out->allowClickjacking();
169 // Used in ./tests/qunit/data/testrunner.js, see also documentation of
170 // $wgJavaScriptTestConfig in DefaultSettings.php
171 $out->addJsConfigVars(
172 'QUnitTestSwarmInjectJSPath',
173 $wgJavaScriptTestConfig['qunit']['testswarm-injectjs']
177 protected function getGroupName() {
178 return 'other';