Merge "Normalise documentation in tests/selenium"
[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 string $html The raw HTML.
106 * @param string $state State, one of 'noframework', 'unknownframework' or 'frameworkfound'
107 * @throws MWException
108 * @return string
109 */
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) . '".'
116 );
117 }
118 return "<div id=\"mw-javascripttest-summary\" class=\"mw-javascripttest-$state\">$html</div>";
119 }
120
121 /**
122 * Initialize the page for QUnit.
123 */
124 private function initQUnitTesting() {
125 global $wgJavaScriptTestConfig;
126
127 $out = $this->getOutput();
128
129 $out->addModules( 'mediawiki.tests.qunit.testrunner' );
130 $qunitTestModules = $out->getResourceLoader()->getTestModuleNames( 'qunit' );
131 $out->addModules( $qunitTestModules );
132
133 $summary = $this->msg( 'javascripttest-qunit-intro' )
134 ->params( $wgJavaScriptTestConfig['qunit']['documentation'] )
135 ->parseAsBlock();
136 $header = $this->msg( 'javascripttest-qunit-heading' )->escaped();
137 $userDir = $this->getLanguage()->getDir();
138
139 $baseHtml = <<<HTML
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>
147 </div>
148 HTML;
149 $out->addHtml( $this->wrapSummaryHtml( $summary, 'frameworkfound' ) . $baseHtml );
150
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();
155
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'] );
159 }
160
161 protected function getGroupName() {
162 return 'other';
163 }
164 }