3 * This PHP script defines the spec that the mediawiki.jqueryMsg module should conform to.
5 * It does this by looking up the results of various kinds of string parsing, with various
6 * languages, in the current installation of MediaWiki. It then outputs a static specification,
7 * mapping expected inputs to outputs, which can be used fed into a unit test framework.
8 * (QUnit, Jasmine, anything, it just outputs an object with key/value pairs).
10 * This is similar to Michael Dale (mdale@mediawiki.org)'s parser tests, except that it doesn't
11 * look up the API results while doing the test, so the test run is much faster (at the cost
12 * of being out of date in rare circumstances. But mostly the parsing that we are doing in
13 * Javascript doesn't change much).
19 QUnit.test( 'Output matches PHP parser', mw.libs.phpParserData.tests.length, function ( assert ) {
20 mw.messages.set( mw.libs.phpParserData.messages );
21 $.each( mw.libs.phpParserData.tests, function ( i, test ) {
23 getMwLanguage( test.lang, function ( langClass ) {
24 var parser = new mw.jqueryMsg.parser( { language: langClass } );
26 parser.parse( test.key, test.args ).html(),
38 describe( 'match output to output from PHP parser', function () {
39 mw.messages.set( mw.libs.phpParserData.messages );
40 $.each( mw.libs.phpParserData.tests, function ( i, test ) {
41 it( 'should parse ' + test.name, function () {
44 getMwLanguage( test.lang, function ( gotIt ) {
48 waitsFor( function () {
49 return langClass !== undefined;
50 }, 'Language class should be loaded', 1000 );
52 console.log( test.lang, 'running tests' );
53 var parser = new mw.jqueryMsg.parser( { language: langClass } );
55 parser.parse( test.key, test.args ).html()
56 ).toEqual( test.result );
64 require __DIR__
. '/../../../maintenance/Maintenance.php';
66 class GenerateJqueryMsgData
extends Maintenance
{
68 public static $keyToTestArgs = array(
69 'undelete_short' => array(
77 'category-subcat-count' => array(
85 public function __construct() {
86 parent
::__construct();
87 $this->mDescription
= 'Create a specification for message parsing ini JSON format';
88 // add any other options here
91 public function execute() {
92 list( $messages, $tests ) = $this->getMessagesAndTests();
93 $this->writeJavascriptFile( $messages, $tests, __DIR__
. '/mediawiki.jqueryMsg.data.js' );
96 private function getMessagesAndTests() {
99 foreach ( array( 'en', 'fr', 'ar', 'jp', 'zh' ) as $languageCode ) {
100 foreach ( self
::$keyToTestArgs as $key => $testArgs ) {
101 foreach ( $testArgs as $args ) {
102 // Get the raw message, without any transformations.
103 $template = wfMessage( $key )->inLanguage( $languageCode )->plain();
105 // Get the magic-parsed version with args.
106 $result = wfMessage( $key, $args )->inLanguage( $languageCode )->text();
108 // Record the template, args, language, and expected result
109 // fake multiple languages by flattening them together.
110 $langKey = $languageCode . '_' . $key;
111 $messages[$langKey] = $template;
113 'name' => $languageCode . ' ' . $key . ' ' . join( ',', $args ),
117 'lang' => $languageCode
122 return array( $messages, $tests );
125 private function writeJavascriptFile( $messages, $tests, $dataSpecFile ) {
126 $phpParserData = array(
127 'messages' => $messages,
132 "// This file stores the output from the PHP parser for various messages, arguments,\n"
133 . "// languages, and parser modes. Intended for use by a unit test framework by looping\n"
134 . "// through the object and comparing its parser return value with the 'result' property.\n"
135 . '// Last generated with ' . basename( __FILE__
) . ' at ' . gmdate( 'r' ) . "\n"
136 // This file will contain unquoted JSON strings as javascript native object literals,
137 // flip the quotemark convention for this file.
139 . 'mediaWiki.libs.phpParserData = ' . FormatJson
::encode( $phpParserData, true ) . ";\n";
141 $fp = file_put_contents( $dataSpecFile, $output );
142 if ( $fp === false ) {
143 die( "Couldn't write to $dataSpecFile." );
148 $maintClass = "GenerateJqueryMsgData";
149 require_once RUN_MAINTENANCE_IF_MAIN
;