3 class SpecialJavaScriptTest
extends SpecialPage
{
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.
10 static $frameworks = array(
11 'qunit' => 'initQUnitTesting',
14 public function __construct() {
15 parent
::__construct( 'JavaScriptTest' );
18 public function execute( $par ) {
19 global $wgEnableJavaScriptTest;
21 $out = $this->getOutput();
24 $out->disallowUserJs();
26 // Abort early if we're disabled
27 if ( $wgEnableJavaScriptTest !== true ) {
28 $out->addWikiMsg( 'javascripttest-disabled' );
32 $out->addModules( 'mediawiki.special.javaScriptTest' );
34 // Determine framework
35 $pars = explode( '/', $par );
36 $framework = strtolower( $pars[0] );
38 // No framework specified
40 $out->setPagetitle( wfMsg( 'javascripttest' ) );
41 $summary = $this->wrapSummaryHtml(
42 wfMsg( 'javascripttest-pagetext-noframework' ) . $this->getFrameworkListHtml(),
45 $out->addHtml( $summary );
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" ) ) );
51 wfMessage( 'javascripttest-backlink' )->rawParams( Linker
::linkKnown( $this->getTitle() ) )->escaped()
53 $this->{self
::$frameworks[$framework]}();
55 // Framework not found, display error
57 $out->setPagetitle( wfMsg( 'javascripttest' ) );
58 $summary = $this->wrapSummaryHtml( '<p class="error">'
59 . wfMsg( 'javascripttest-pagetext-unknownframework', $par )
61 . $this->getFrameworkListHtml() );
62 $out->addHtml( $summary, 'unknownframework' );
67 * Get a list of frameworks (including introduction paragraph and links to the framework run pages)
68 * @return String: HTML
70 private function getFrameworkListHtml() {
72 foreach( self
::$frameworks as $framework => $initFn ) {
73 $list .= Html
::rawElement(
76 Linker
::link( $this->getTitle( $framework ), wfMsg( "javascripttest-$framework-name" ) )
80 $msg = wfMessage( 'javascripttest-pagetext-frameworks' )->rawParams( $list )->parseAsBlock();
86 * Function to wrap the summary.
87 * @param $html String: The raw HTML.
88 * @param $state String: State, one of 'noframework', 'unknownframework' or 'frameworkfound'
90 private function wrapSummaryHtml( $html = '', $state ) {
91 return "<div id=\"mw-javascripttest-summary\" class=\"mw-javascripttest-$state\">$html</div>";
95 * Initialize the page for QUnit.
97 private function initQUnitTesting() {
98 global $wgJavaScriptTestConfig;
100 $out = $this->getOutput();
102 $out->addModules( 'mediawiki.tests.qunit.testrunner' );
103 $qunitTestModules = $out->getResourceLoader()->getTestModuleNames( 'qunit' );
104 $out->addModules( $qunitTestModules );
106 $summary = wfMessage( 'javascripttest-qunit-intro' )
107 ->params( $wgJavaScriptTestConfig['qunit']['documentation'] )
109 $header = wfMessage( 'javascripttest-qunit-heading' )->escaped();
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>
118 $out->addHtml( $this->wrapSummaryHtml( $summary, 'frameworkfound' ) . $baseHtml );
122 public function isListed(){
123 global $wgEnableJavaScriptTest;
124 return $wgEnableJavaScriptTest === true;