Merge "Support explicit 0 and 1 forms for plural in PHP"
[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() . $this->getFrameworkListHtml(),
59 'noframework'
60 );
61 $out->addHtml( $summary );
62
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]}();
68
69 // Framework not found, display error
70 } else {
71 $out->setPageTitle( $this->msg( 'javascripttest' ) );
72 $summary = $this->wrapSummaryHtml( '<p class="error">'
73 . $this->msg( 'javascripttest-pagetext-unknownframework', $par )->escaped()
74 . '</p>'
75 . $this->getFrameworkListHtml(),
76 'unknownframework'
77 );
78 $out->addHtml( $summary );
79 }
80 }
81
82 /**
83 * Get a list of frameworks (including introduction paragraph and links to the framework run pages)
84 * @return String: HTML
85 */
86 private function getFrameworkListHtml() {
87 $list = '<ul>';
88 foreach( self::$frameworks as $framework => $initFn ) {
89 $list .= Html::rawElement(
90 'li',
91 array(),
92 Linker::link( $this->getTitle( $framework ), $this->msg( "javascripttest-$framework-name" )->escaped() )
93 );
94 }
95 $list .= '</ul>';
96 $msg = $this->msg( 'javascripttest-pagetext-frameworks' )->rawParams( $list )->parseAsBlock();
97
98 return $msg;
99 }
100
101 /**
102 * Function to wrap the summary.
103 * It must be given a valid state as a second parameter or an exception will
104 * be thrown.
105 * @param $html String: The raw HTML.
106 * @param $state String: State, one of 'noframework', 'unknownframework' or 'frameworkfound'
107 * @return string
108 */
109 private function wrapSummaryHtml( $html, $state ) {
110 $validStates = array( 'noframework', 'unknownframework', 'frameworkfound' );
111 if( !in_array( $state, $validStates ) ) {
112 throw new MWException( __METHOD__
113 . ' given an invalid state. Must be one of "'
114 . join( '", "', $validStates) . '".'
115 );
116 }
117 return "<div id=\"mw-javascripttest-summary\" class=\"mw-javascripttest-$state\">$html</div>";
118 }
119
120 /**
121 * Initialize the page for QUnit.
122 */
123 private function initQUnitTesting() {
124 global $wgJavaScriptTestConfig;
125
126 $out = $this->getOutput();
127
128 $out->addModules( 'mediawiki.tests.qunit.testrunner' );
129 $qunitTestModules = $out->getResourceLoader()->getTestModuleNames( 'qunit' );
130 $out->addModules( $qunitTestModules );
131
132 $summary = $this->msg( 'javascripttest-qunit-intro' )
133 ->params( $wgJavaScriptTestConfig['qunit']['documentation'] )
134 ->parseAsBlock();
135 $header = $this->msg( 'javascripttest-qunit-heading' )->escaped();
136 $userDir = $this->getLanguage()->getDir();
137
138 $baseHtml = <<<HTML
139 <div class="mw-content-ltr">
140 <div id="qunit-header"><span dir="$userDir">$header</span></div>
141 <div id="qunit-banner"></div>
142 <div id="qunit-testrunner-toolbar"></div>
143 <div id="qunit-userAgent"></div>
144 <ol id="qunit-tests"></ol>
145 <div id="qunit-fixture">test markup, will be hidden</div>
146 </div>
147 HTML;
148 $out->addHtml( $this->wrapSummaryHtml( $summary, 'frameworkfound' ) . $baseHtml );
149
150 // This special page is disabled by default ($wgEnableJavaScriptTest), and contains
151 // no sensitive data. In order to allow TestSwarm to embed it into a test client window,
152 // we need to allow iframing of this page.
153 $out->allowClickjacking();
154
155 // Used in ./tests/qunit/data/testrunner.js, see also documentation of
156 // $wgJavaScriptTestConfig in DefaultSettings.php
157 $out->addJsConfigVars( 'QUnitTestSwarmInjectJSPath', $wgJavaScriptTestConfig['qunit']['testswarm-injectjs'] );
158 }
159 }