merge JSTesting branch into trunk
[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( wfMsg( 'javascripttest' ) );
41 $summary = $this->wrapSummaryHtml(
42 wfMsg( '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( wfMsg( 'javascripttest-title', wfMsg( "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( wfMsg( 'javascripttest' ) );
58 $summary = $this->wrapSummaryHtml( '<p class="error">'
59 . wfMsg( 'javascripttest-pagetext-unknownframework', $par )
60 . '</p>'
61 . $this->getFrameworkListHtml() );
62 $out->addHtml( $summary, 'unknownframework' );
63 }
64 }
65
66 /**
67 * Get a list of frameworks (including introduction paragraph and links to the framework run pages)
68 * @return String: HTML
69 */
70 private function getFrameworkListHtml() {
71 $list = '<ul>';
72 foreach( self::$frameworks as $framework => $initFn ) {
73 $list .= Html::rawElement(
74 'li',
75 array(),
76 Linker::link( $this->getTitle( $framework ), wfMsg( "javascripttest-$framework-name" ) )
77 );
78 }
79 $list .= '</ul>';
80 $msg = wfMessage( 'javascripttest-pagetext-frameworks' )->rawParams( $list )->parseAsBlock();
81
82 return $msg;
83 }
84
85 /**
86 * Function to wrap the summary.
87 * @param $html String: The raw HTML.
88 * @param $state String: State, one of 'noframework', 'unknownframework' or 'frameworkfound'
89 */
90 private function wrapSummaryHtml( $html = '', $state ) {
91 return "<div id=\"mw-javascripttest-summary\" class=\"mw-javascripttest-$state\">$html</div>";
92 }
93
94 /**
95 * Initialize the page for QUnit.
96 */
97 private function initQUnitTesting() {
98 global $wgJavaScriptTestConfig;
99
100 $out = $this->getOutput();
101
102 $out->addModules( 'mediawiki.tests.qunit.testrunner' );
103 $qunitTestModules = $out->getResourceLoader()->getTestModuleNames( 'qunit' );
104 $out->addModules( $qunitTestModules );
105
106 $summary = wfMessage( 'javascripttest-qunit-intro' )
107 ->params( $wgJavaScriptTestConfig['qunit']['documentation'] )
108 ->parseAsBlock();
109 $header = wfMessage( 'javascripttest-qunit-heading' )->escaped();
110
111 $baseHtml = <<<HTML
112 <div id="qunit-header">$header</div>
113 <div id="qunit-banner"></div>
114 <div id="qunit-testrunner-toolbar"></div>
115 <div id="qunit-userAgent"></div>
116 <ol id="qunit-tests"></ol>
117 HTML;
118 $out->addHtml( $this->wrapSummaryHtml( $summary, 'frameworkfound' ) . $baseHtml );
119
120 }
121
122 public function isListed(){
123 global $wgEnableJavaScriptTest;
124 return $wgEnableJavaScriptTest === true;
125 }
126
127 }