MediaWiki  1.23.14
SpecialJavaScriptTest.php
Go to the documentation of this file.
1 <?php
28 
32  private static $frameworks = array(
33  'qunit',
34  );
35 
36  public function __construct() {
37  parent::__construct( 'JavaScriptTest' );
38  }
39 
40  public function execute( $par ) {
41  $out = $this->getOutput();
42 
43  $this->setHeaders();
44  $out->disallowUserJs();
45 
46  if ( $par === null ) {
47  // No framework specified
48  $out->setStatusCode( 404 );
49  $out->setPageTitle( $this->msg( 'javascripttest' ) );
50  $out->addHTML(
51  $this->msg( 'javascripttest-pagetext-noframework' )->parseAsBlock()
52  . $this->getFrameworkListHtml()
53  );
54  return;
55  }
56 
57  // Determine framework and mode
58  $pars = explode( '/', $par, 2 );
59 
60  $framework = $pars[0];
61  if ( !in_array( $framework, self::$frameworks ) ) {
62  // Framework not found
63  $out->setStatusCode( 404 );
64  $out->addHTML(
65  '<div class="error">'
66  . $this->msg( 'javascripttest-pagetext-unknownframework' )
67  ->plaintextParams( $par )->parseAsBlock()
68  . '</div>'
69  . $this->getFrameworkListHtml()
70  );
71  return;
72  }
73 
74  // This special page is disabled by default ($wgEnableJavaScriptTest), and contains
75  // no sensitive data. In order to allow TestSwarm to embed it into a test client window,
76  // we need to allow iframing of this page.
77  $out->allowClickjacking();
78  $out->setSubtitle(
79  $this->msg( 'javascripttest-backlink' )
80  ->rawParams( Linker::linkKnown( $this->getPageTitle() ) )
81  );
82 
83  // Custom actions
84  if ( isset( $pars[1] ) ) {
85  $action = $pars[1];
86  if ( !in_array( $action, array( 'export', 'plain' ) ) ) {
87  $out->setStatusCode( 404 );
88  $out->addHTML(
89  '<div class="error">'
90  . $this->msg( 'javascripttest-pagetext-unknownaction' )
91  ->plaintextParams( $action )->parseAsBlock()
92  . '</div>'
93  );
94  return;
95  }
96  $method = $action . ucfirst( $framework );
97  $this->$method();
98  return;
99  }
100 
101  $out->addModules( 'mediawiki.special.javaScriptTest' );
102 
103  $method = 'view' . ucfirst( $framework );
104  $this->$method();
105  $out->setPageTitle( $this->msg(
106  'javascripttest-title',
107  // Messages: javascripttest-qunit-name
108  $this->msg( "javascripttest-$framework-name" )->plain()
109  ) );
110  }
111 
118  private function getFrameworkListHtml() {
119  $list = '<ul>';
120  foreach ( self::$frameworks as $framework ) {
121  $list .= Html::rawElement(
122  'li',
123  array(),
124  Linker::link(
125  $this->getPageTitle( $framework ),
126  // Message: javascripttest-qunit-name
127  $this->msg( "javascripttest-$framework-name" )->escaped()
128  )
129  );
130  }
131  $list .= '</ul>';
132 
133  return $this->msg( 'javascripttest-pagetext-frameworks' )->rawParams( $list )
134  ->parseAsBlock();
135  }
136 
143  private function wrapSummaryHtml( $html ) {
144  return "<div id=\"mw-javascripttest-summary\">$html</div>";
145  }
146 
152  private function viewQUnit() {
153  global $wgJavaScriptTestConfig;
154 
155  $out = $this->getOutput();
156  $testConfig = $wgJavaScriptTestConfig;
157 
158  $modules = $out->getResourceLoader()->getTestModuleNames( 'qunit' );
159 
160  $summary = $this->msg( 'javascripttest-qunit-intro' )
161  ->params( $wgJavaScriptTestConfig['qunit']['documentation'] )
162  ->parseAsBlock();
163 
164  $baseHtml = <<<HTML
165 <div class="mw-content-ltr">
166 <div id="qunit"></div>
167 </div>
168 HTML;
169 
170  // Used in ./tests/qunit/data/testrunner.js, see also documentation of
171  // $wgJavaScriptTestConfig in DefaultSettings.php
172  $out->addJsConfigVars(
173  'QUnitTestSwarmInjectJSPath',
174  $testConfig['qunit']['testswarm-injectjs']
175  );
176 
177  $out->addHtml( $this->wrapSummaryHtml( $summary ) . $baseHtml );
178 
179  // The testrunner configures QUnit and essentially depends on it. However, test suites
180  // are reusable in environments that preload QUnit (or a compatibility interface to
181  // another framework). Therefore we have to load it ourselves.
182  $out->addHtml( Html::inlineScript(
184  Xml::encodeJsCall( 'mw.loader.using', array(
185  array( 'jquery.qunit', 'jquery.qunit.completenessTest' ),
186  new XmlJsCode(
187  'function () {' . Xml::encodeJsCall( 'mw.loader.load', array( $modules ) ) . '}'
188  )
189  ) )
190  )
191  ) );
192  }
193 
204  private function exportQUnit() {
205  $out = $this->getOutput();
206 
207  $out->disable();
208 
209  $rl = $out->getResourceLoader();
210 
211  $query = array(
212  'lang' => $this->getLanguage()->getCode(),
213  'skin' => $this->getSkin()->getSkinName(),
214  'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false',
215  );
216  $embedContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) );
217  $query['only'] = 'scripts';
218  $startupContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) );
219 
220  $modules = $rl->getTestModuleNames( 'qunit' );
221 
222  // The below is essentially a pure-javascript version of OutputPage::getHeadScripts.
223  $startup = $rl->makeModuleResponse( $startupContext, array(
224  'startup' => $rl->getModule( 'startup' ),
225  ) );
226  // Embed page-specific mw.config variables.
227  // The current Special page shouldn't be relevant to tests, but various modules (which
228  // are loaded before the test suites), reference mw.config while initialising.
229  $code = ResourceLoader::makeConfigSetScript( $out->getJSVars() );
230  // Embed private modules as they're not allowed to be loaded dynamically
231  $code .= $rl->makeModuleResponse( $embedContext, array(
232  'user.options' => $rl->getModule( 'user.options' ),
233  'user.tokens' => $rl->getModule( 'user.tokens' ),
234  ) );
235  $code .= Xml::encodeJsCall( 'mw.loader.load', array( $modules ) );
236 
237  header( 'Content-Type: text/javascript; charset=utf-8' );
238  header( 'Cache-Control: private, no-cache, must-revalidate' );
239  header( 'Pragma: no-cache' );
240  echo $startup;
241  echo "\n";
242  // Note: The following has to be wrapped in a script tag because the startup module also
243  // writes a script tag (the one loading mediawiki.js). Script tags are synchronous, block
244  // each other, and run in order. But they don't nest. The code appended after the startup
245  // module runs before the added script tag is parsed and executed.
246  echo Xml::encodeJsCall( 'document.write', array( Html::inlineScript( $code ) ) );
247  }
248 
249  private function plainQUnit() {
250  $out = $this->getOutput();
251  $out->disable();
252 
253  $url = $this->getPageTitle( 'qunit/export' )->getFullURL( array(
254  'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false',
255  ) );
256 
257  $styles = $out->makeResourceLoaderLink( 'jquery.qunit', ResourceLoaderModule::TYPE_STYLES, false );
258  // Use 'raw' since this is a plain HTML page without ResourceLoader
259  $scripts = $out->makeResourceLoaderLink( 'jquery.qunit', ResourceLoaderModule::TYPE_SCRIPTS, false, array( 'raw' => 'true' ) );
260 
261  $head = trim( $styles['html'] . $scripts['html'] );
262  $html = <<<HTML
263 <!DOCTYPE html>
264 <title>QUnit</title>
265 $head
266 <div id="qunit"></div>
267 HTML;
268  $html .= "\n" . Html::linkedScript( $url );
269 
270  header( 'Content-Type: text/html; charset=utf-8' );
271  echo $html;
272  }
273 
274  protected function getGroupName() {
275  return 'other';
276  }
277 }
ResourceLoader\makeLoaderConditionalScript
static makeLoaderConditionalScript( $script)
Returns JS code which runs given JS code if the client-side framework is present.
Definition: ResourceLoader.php:1138
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:488
ResourceLoaderContext
Object passed around to modules which contains information about the state of a specific loader reque...
Definition: ResourceLoaderContext.php:29
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: WebRequest.php:1275
SpecialJavaScriptTest\execute
execute( $par)
Default execute method Checks user permissions, calls the function given in mFunction.
Definition: SpecialJavaScriptTest.php:40
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ResourceLoader\makeConfigSetScript
static makeConfigSetScript(array $configuration)
Returns JS code which will set the MediaWiki configuration array to the given value.
Definition: ResourceLoader.php:1149
$html
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition: hooks.txt:1530
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:535
SpecialJavaScriptTest\$frameworks
static $frameworks
Definition: SpecialJavaScriptTest.php:32
SpecialJavaScriptTest\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialJavaScriptTest.php:274
SpecialPage\getSkin
getSkin()
Shortcut to get the skin being used for this instance.
Definition: SpecialPage.php:555
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:578
Html\inlineScript
static inlineScript( $contents)
Output a "<script>" tag with the given contents.
Definition: Html.php:573
Linker\linkKnown
static linkKnown( $target, $html=null, $customAttribs=array(), $query=array(), $options=array( 'known', 'noclasses'))
Identical to link(), except $options defaults to 'known'.
Definition: Linker.php:264
SpecialJavaScriptTest\wrapSummaryHtml
wrapSummaryHtml( $html)
Wrap HTML contents in a summary container.
Definition: SpecialJavaScriptTest.php:143
Linker\link
static link( $target, $html=null, $customAttribs=array(), $query=array(), $options=array())
This function returns an HTML link to the given target.
Definition: Linker.php:192
XmlJsCode
A wrapper class which causes Xml::encodeJsVar() and Xml::encodeJsCall() to interpret a given string a...
Definition: Xml.php:967
title
to move a page</td >< td > &*You are moving the page across *A non empty talk page already exists under the new or *You uncheck the box below In those you will have to move or merge the page manually if desired</td >< td > be sure to &You are responsible for making sure that links continue to point where they are supposed to go Note that the page will &a page at the new title
Definition: All_system_messages.txt:2703
Xml\encodeJsCall
static encodeJsCall( $name, $args, $pretty=false)
Create a call to a JavaScript function.
Definition: Xml.php:665
$out
$out
Definition: UtfNormalGenerate.php:167
SpecialJavaScriptTest\__construct
__construct()
Definition: SpecialJavaScriptTest.php:36
SpecialJavaScriptTest\plainQUnit
plainQUnit()
Definition: SpecialJavaScriptTest.php:249
Html\linkedScript
static linkedScript( $url)
Output a "<script>" tag linking to the given URL, e.g., "<script src=foo.js></script>".
Definition: Html.php:592
ResourceLoaderModule\TYPE_SCRIPTS
const TYPE_SCRIPTS
Definition: ResourceLoaderModule.php:31
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:352
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
SpecialPage\msg
msg()
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:609
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:33
SpecialJavaScriptTest
Definition: SpecialJavaScriptTest.php:27
$summary
$summary
Definition: importImages.php:120
SpecialJavaScriptTest\exportQUnit
exportQUnit()
Generate self-sufficient JavaScript payload to run the tests elsewhere.
Definition: SpecialJavaScriptTest.php:204
ResourceLoader\inDebugMode
static inDebugMode()
Determine whether debug mode was requested Order of priority is 1) request param, 2) cookie,...
Definition: ResourceLoader.php:1188
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ResourceLoaderModule\TYPE_STYLES
const TYPE_STYLES
Definition: ResourceLoaderModule.php:32
SpecialJavaScriptTest\viewQUnit
viewQUnit()
Run the test suite on the Special page.
Definition: SpecialJavaScriptTest.php:152
Html\rawElement
static rawElement( $element, $attribs=array(), $contents='')
Returns an HTML element in a string.
Definition: Html.php:124
$query
return true to allow those checks to and false if checking is done use this to change the tables headers temp or archived zone change it to an object instance and return false override the list derivative used the name of the old file when set the default code will be skipped add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1105
SpecialJavaScriptTest\getFrameworkListHtml
getFrameworkListHtml()
Get a list of frameworks (including introduction paragraph and links to the framework run pages)
Definition: SpecialJavaScriptTest.php:118