Update formatting
[lhc/web/wiklou.git] / includes / specials / SpecialJavaScriptTest.php
1 <?php
2 /**
3 * Implements Special:JavaScriptTest
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * @ingroup SpecialPage
26 */
27 class SpecialJavaScriptTest extends SpecialPage {
28
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.
33 */
34 static $frameworks = array(
35 'qunit' => 'initQUnitTesting',
36 );
37
38 public function __construct() {
39 parent::__construct( 'JavaScriptTest' );
40 }
41
42 public function execute( $par ) {
43 $out = $this->getOutput();
44
45 $this->setHeaders();
46 $out->disallowUserJs();
47
48 $out->addModules( 'mediawiki.special.javaScriptTest' );
49
50 // Determine framework
51 $pars = explode( '/', $par );
52 $framework = strtolower( $pars[0] );
53
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'
61 );
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 $this->msg( "javascripttest-$framework-name" )->plain()
68 ) );
69 $out->setSubtitle( $this->msg( 'javascripttest-backlink' )
70 ->rawParams( Linker::linkKnown( $this->getTitle() ) ) );
71 $this->{self::$frameworks[$framework]}();
72 } else {
73 // Framework not found, display error
74 $out->setPageTitle( $this->msg( 'javascripttest' ) );
75 $summary = $this->wrapSummaryHtml(
76 '<p class="error">' .
77 $this->msg( 'javascripttest-pagetext-unknownframework', $par )->escaped() .
78 '</p>' .
79 $this->getFrameworkListHtml(),
80 'unknownframework'
81 );
82 $out->addHtml( $summary );
83 }
84 }
85
86 /**
87 * Get a list of frameworks (including introduction paragraph and links
88 * to the framework run pages)
89 *
90 * @return string HTML
91 */
92 private function getFrameworkListHtml() {
93 $list = '<ul>';
94 foreach ( self::$frameworks as $framework => $initFn ) {
95 $list .= Html::rawElement(
96 'li',
97 array(),
98 Linker::link(
99 $this->getTitle( $framework ),
100 $this->msg( "javascripttest-$framework-name" )->escaped()
101 )
102 );
103 }
104 $list .= '</ul>';
105
106 return $this->msg( 'javascripttest-pagetext-frameworks' )->rawParams( $list )
107 ->parseAsBlock();
108 }
109
110 /**
111 * Function to wrap the summary.
112 * It must be given a valid state as a second parameter or an exception will
113 * be thrown.
114 * @param string $html The raw HTML.
115 * @param string $state State, one of 'noframework', 'unknownframework' or 'frameworkfound'
116 * @throws MWException
117 * @return string
118 */
119 private function wrapSummaryHtml( $html, $state ) {
120 $validStates = array( 'noframework', 'unknownframework', 'frameworkfound' );
121
122 if ( !in_array( $state, $validStates ) ) {
123 throw new MWException( __METHOD__
124 . ' given an invalid state. Must be one of "'
125 . join( '", "', $validStates ) . '".'
126 );
127 }
128
129 return "<div id=\"mw-javascripttest-summary\" class=\"mw-javascripttest-$state\">$html</div>";
130 }
131
132 /**
133 * Initialize the page for QUnit.
134 */
135 private function initQUnitTesting() {
136 global $wgJavaScriptTestConfig;
137
138 $out = $this->getOutput();
139
140 $out->addModules( 'mediawiki.tests.qunit.testrunner' );
141 $qunitTestModules = $out->getResourceLoader()->getTestModuleNames( 'qunit' );
142 $out->addModules( $qunitTestModules );
143
144 $summary = $this->msg( 'javascripttest-qunit-intro' )
145 ->params( $wgJavaScriptTestConfig['qunit']['documentation'] )
146 ->parseAsBlock();
147 $header = $this->msg( 'javascripttest-qunit-heading' )->escaped();
148 $userDir = $this->getLanguage()->getDir();
149
150 $baseHtml = <<<HTML
151 <div class="mw-content-ltr">
152 <div id="qunit-header"><span dir="$userDir">$header</span></div>
153 <div id="qunit-banner"></div>
154 <div id="qunit-testrunner-toolbar"></div>
155 <div id="qunit-userAgent"></div>
156 <ol id="qunit-tests"></ol>
157 <div id="qunit-fixture">test markup, will be hidden</div>
158 </div>
159 HTML;
160 $out->addHtml( $this->wrapSummaryHtml( $summary, 'frameworkfound' ) . $baseHtml );
161
162 // This special page is disabled by default ($wgEnableJavaScriptTest), and contains
163 // no sensitive data. In order to allow TestSwarm to embed it into a test client window,
164 // we need to allow iframing of this page.
165 $out->allowClickjacking();
166
167 // Used in ./tests/qunit/data/testrunner.js, see also documentation of
168 // $wgJavaScriptTestConfig in DefaultSettings.php
169 $out->addJsConfigVars(
170 'QUnitTestSwarmInjectJSPath',
171 $wgJavaScriptTestConfig['qunit']['testswarm-injectjs']
172 );
173 }
174
175 protected function getGroupName() {
176 return 'other';
177 }
178 }