Merge "Use preview content when it transcludes itself"
[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 * @var array Mapping of framework ids and their initilizer methods
30 * in this class. If a framework is requested but not in this array,
31 * the 'unknownframework' error is served.
32 */
33 private static $frameworks = array(
34 'qunit' => 'initQUnitTesting',
35 );
36
37 public function __construct() {
38 parent::__construct( 'JavaScriptTest' );
39 }
40
41 public function execute( $par ) {
42 $out = $this->getOutput();
43
44 $this->setHeaders();
45 $out->disallowUserJs();
46
47 $out->addModules( 'mediawiki.special.javaScriptTest' );
48
49 // Determine framework
50 $pars = explode( '/', $par );
51 $framework = strtolower( $pars[0] );
52
53 // No framework specified
54 if ( $par == '' ) {
55 $out->setPageTitle( $this->msg( 'javascripttest' ) );
56 $summary = $this->wrapSummaryHtml(
57 $this->msg( 'javascripttest-pagetext-noframework' )->escaped() .
58 $this->getFrameworkListHtml(),
59 'noframework'
60 );
61 $out->addHtml( $summary );
62 } elseif ( isset( self::$frameworks[$framework] ) ) {
63 // Matched! Display proper title and initialize the framework
64 $out->setPageTitle( $this->msg(
65 'javascripttest-title',
66 // Messages: javascripttest-qunit-name
67 $this->msg( "javascripttest-$framework-name" )->plain()
68 ) );
69 $out->setSubtitle( $this->msg( 'javascripttest-backlink' )
70 ->rawParams( Linker::linkKnown( $this->getPageTitle() ) ) );
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->getPageTitle( $framework ),
100 // Message: javascripttest-qunit-name
101 $this->msg( "javascripttest-$framework-name" )->escaped()
102 )
103 );
104 }
105 $list .= '</ul>';
106
107 return $this->msg( 'javascripttest-pagetext-frameworks' )->rawParams( $list )
108 ->parseAsBlock();
109 }
110
111 /**
112 * Function to wrap the summary.
113 * It must be given a valid state as a second parameter or an exception will
114 * be thrown.
115 * @param string $html The raw HTML.
116 * @param string $state State, one of 'noframework', 'unknownframework' or 'frameworkfound'
117 * @throws MWException
118 * @return string
119 */
120 private function wrapSummaryHtml( $html, $state ) {
121 $validStates = array( 'noframework', 'unknownframework', 'frameworkfound' );
122
123 if ( !in_array( $state, $validStates ) ) {
124 throw new MWException( __METHOD__
125 . ' given an invalid state. Must be one of "'
126 . join( '", "', $validStates ) . '".'
127 );
128 }
129
130 return "<div id=\"mw-javascripttest-summary\" class=\"mw-javascripttest-$state\">$html</div>";
131 }
132
133 /**
134 * Initialize the page for QUnit.
135 */
136 private function initQUnitTesting() {
137 $out = $this->getOutput();
138
139 $out->addModules( 'test.mediawiki.qunit.testrunner' );
140 $qunitTestModules = $out->getResourceLoader()->getTestModuleNames( 'qunit' );
141 $out->addModules( $qunitTestModules );
142
143 $summary = $this->msg( 'javascripttest-qunit-intro' )
144 ->params( 'https://www.mediawiki.org/wiki/Manual:JavaScript_unit_testing' )
145 ->parseAsBlock();
146 $header = $this->msg( 'javascripttest-qunit-heading' )->escaped();
147 $userDir = $this->getLanguage()->getDir();
148
149 $baseHtml = <<<HTML
150 <div class="mw-content-ltr">
151 <div id="qunit-header"><span dir="$userDir">$header</span></div>
152 <div id="qunit-banner"></div>
153 <div id="qunit-testrunner-toolbar"></div>
154 <div id="qunit-userAgent"></div>
155 <ol id="qunit-tests"></ol>
156 <div id="qunit-fixture">test markup, will be hidden</div>
157 </div>
158 HTML;
159 $out->addHtml( $this->wrapSummaryHtml( $summary, 'frameworkfound' ) . $baseHtml );
160
161 // This special page is disabled by default ($wgEnableJavaScriptTest), and contains
162 // no sensitive data. In order to allow test frameworks to embed it into a test client window,
163 // we need to allow iframing of this page.
164 $out->allowClickjacking();
165 }
166
167 /**
168 * Return an array of subpages that this special page will accept.
169 *
170 * @return string[] subpages
171 */
172 public function getSubpagesForPrefixSearch() {
173 return array_keys( self::$frameworks );
174 }
175
176 protected function getGroupName() {
177 return 'other';
178 }
179 }