c658f0d0c89379ff4cf89fca0c5c4f9772955cbc
[lhc/web/wiklou.git] / includes / specials / SpecialJavaScriptTest.php
1 <?php
2
3 class SpecialJavaScriptTest extends SpecialPage {
4
5 /**
6 * @var $frameworks Array: Mapping of framework ids and their initilizer methods
7 * in this class. If a framework is requested but not in this array,
8 * the 'unknownframework' error is served.
9 */
10 static $frameworks = array(
11 'qunit' => 'initQUnitTesting',
12 );
13
14 public function __construct() {
15 parent::__construct( 'JavaScriptTest' );
16 }
17
18 public function execute( $par ) {
19 global $wgEnableJavaScriptTest;
20
21 $out = $this->getOutput();
22
23 $this->setHeaders();
24 $out->disallowUserJs();
25
26 // Abort early if we're disabled
27 if ( $wgEnableJavaScriptTest !== true ) {
28 $out->addWikiMsg( 'javascripttest-disabled' );
29 return;
30 }
31
32 $out->addModules( 'mediawiki.special.javaScriptTest' );
33
34 // Determine framework
35 $pars = explode( '/', $par );
36 $framework = strtolower( $pars[0] );
37
38 // No framework specified
39 if ( $par == '' ) {
40 $out->setPagetitle( wfMsgHtml( 'javascripttest' ) );
41 $summary = $this->wrapSummaryHtml(
42 wfMsgHtml( 'javascripttest-pagetext-noframework' ) . $this->getFrameworkListHtml(),
43 'noframework'
44 );
45 $out->addHtml( $summary );
46
47 // Matched! Display proper title and initialize the framework
48 } elseif ( isset( self::$frameworks[$framework] ) ) {
49 $out->setPagetitle( wfMsgHtml( 'javascripttest-title', wfMsgHtml( "javascripttest-$framework-name" ) ) );
50 $out->setSubtitle(
51 wfMessage( 'javascripttest-backlink' )->rawParams( Linker::linkKnown( $this->getTitle() ) )->escaped()
52 );
53 $this->{self::$frameworks[$framework]}();
54
55 // Framework not found, display error
56 } else {
57 $out->setPagetitle( wfMsgHtml( 'javascripttest' ) );
58 $summary = $this->wrapSummaryHtml( '<p class="error">'
59 . wfMsgHtml( 'javascripttest-pagetext-unknownframework', $par )
60 . '</p>'
61 . $this->getFrameworkListHtml(),
62 'unknownframework'
63 );
64 $out->addHtml( $summary );
65 }
66 }
67
68 /**
69 * Get a list of frameworks (including introduction paragraph and links to the framework run pages)
70 * @return String: HTML
71 */
72 private function getFrameworkListHtml() {
73 $list = '<ul>';
74 foreach( self::$frameworks as $framework => $initFn ) {
75 $list .= Html::rawElement(
76 'li',
77 array(),
78 Linker::link( $this->getTitle( $framework ), wfMsgHtml( "javascripttest-$framework-name" ) )
79 );
80 }
81 $list .= '</ul>';
82 $msg = wfMessage( 'javascripttest-pagetext-frameworks' )->rawParams( $list )->parseAsBlock();
83
84 return $msg;
85 }
86
87 /**
88 * Function to wrap the summary.
89 * It must be given a valid state as a second parameter or an exception will
90 * be thrown.
91 * @param $html String: The raw HTML.
92 * @param $state String: State, one of 'noframework', 'unknownframework' or 'frameworkfound'
93 */
94 private function wrapSummaryHtml( $html, $state ) {
95 $validStates = array( 'noframework', 'unknownframework', 'frameworkfound' );
96 if( !in_array( $state, $validStates ) ) {
97 throw new MWException( __METHOD__
98 . ' given an invalid state. Must be one of "'
99 . join( '", "', $validStates) . '".'
100 );
101 }
102 return "<div id=\"mw-javascripttest-summary\" class=\"mw-javascripttest-$state\">$html</div>";
103 }
104
105 /**
106 * Initialize the page for QUnit.
107 */
108 private function initQUnitTesting() {
109 global $wgJavaScriptTestConfig, $wgLang;
110
111 $out = $this->getOutput();
112
113 $out->addModules( 'mediawiki.tests.qunit.testrunner' );
114 $qunitTestModules = $out->getResourceLoader()->getTestModuleNames( 'qunit' );
115 $out->addModules( $qunitTestModules );
116
117 $summary = wfMessage( 'javascripttest-qunit-intro' )
118 ->params( $wgJavaScriptTestConfig['qunit']['documentation'] )
119 ->parseAsBlock();
120 $header = wfMessage( 'javascripttest-qunit-heading' )->escaped();
121 $userDir = $wgLang->getDir();
122
123 $baseHtml = <<<HTML
124 <div class="mw-content-ltr">
125 <div id="qunit-header"><span dir="$userDir">$header</span></div>
126 <div id="qunit-banner"></div>
127 <div id="qunit-testrunner-toolbar"></div>
128 <div id="qunit-userAgent"></div>
129 <ol id="qunit-tests"></ol>
130 </div>
131 HTML;
132 $out->addHtml( $this->wrapSummaryHtml( $summary, 'frameworkfound' ) . $baseHtml );
133
134 }
135
136 public function isListed(){
137 global $wgEnableJavaScriptTest;
138 return $wgEnableJavaScriptTest === true;
139 }
140
141 }