Merge "(bug 43467) Add invert selection for ns in Special:Newpages"
[lhc/web/wiklou.git] / tests / qunit / data / generateJqueryMsgData.php
1 <?php
2 /**
3 * This PHP script defines the spec that the mediawiki.jqueryMsg module should conform to.
4 *
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).
9 *
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).
14 */
15
16 /*
17 * @example QUnit
18 * <code>
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 ) {
22 QUnit.stop();
23 getMwLanguage( test.lang, function ( langClass ) {
24 var parser = new mw.jqueryMsg.parser( { language: langClass } );
25 assert.equal(
26 parser.parse( test.key, test.args ).html(),
27 test.result,
28 test.name
29 );
30 QUnit.start();
31 } );
32 } );
33 });
34 * </code>
35 *
36 * @example Jasmine
37 * <code>
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 () {
42 var langClass;
43 runs( function () {
44 getMwLanguage( test.lang, function ( gotIt ) {
45 langClass = gotIt;
46 });
47 });
48 waitsFor( function () {
49 return langClass !== undefined;
50 }, 'Language class should be loaded', 1000 );
51 runs( function () {
52 console.log( test.lang, 'running tests' );
53 var parser = new mw.jqueryMsg.parser( { language: langClass } );
54 expect(
55 parser.parse( test.key, test.args ).html()
56 ).toEqual( test.result );
57 } );
58 } );
59 } );
60 } );
61 * </code>
62 */
63
64 $maintenanceDir = dirname( dirname( dirname( __DIR__ ) ) ) . '/maintenance';
65
66 require( "$maintenanceDir/Maintenance.php" );
67
68 class GenerateJqueryMsgData extends Maintenance {
69
70 static $keyToTestArgs = array(
71 'undelete_short' => array(
72 array( 0 ),
73 array( 1 ),
74 array( 2 ),
75 array( 5 ),
76 array( 21 ),
77 array( 101 )
78 ),
79 'category-subcat-count' => array(
80 array( 0, 10 ),
81 array( 1, 1 ),
82 array( 1, 2 ),
83 array( 3, 30 )
84 )
85 );
86
87 public function __construct() {
88 parent::__construct();
89 $this->mDescription = 'Create a specification for message parsing ini JSON format';
90 // add any other options here
91 }
92
93 public function execute() {
94 list( $messages, $tests ) = $this->getMessagesAndTests();
95 $this->writeJavascriptFile( $messages, $tests, __DIR__ . '/mediawiki.jqueryMsg.data.js' );
96 }
97
98 private function getMessagesAndTests() {
99 $messages = array();
100 $tests = array();
101 foreach ( array( 'en', 'fr', 'ar', 'jp', 'zh' ) as $languageCode ) {
102 foreach ( self::$keyToTestArgs as $key => $testArgs ) {
103 foreach ( $testArgs as $args ) {
104 // Get the raw message, without any transformations.
105 $template = wfMessage( $key )->inLanguage( $languageCode )->plain();
106
107 // Get the magic-parsed version with args.
108 $result = wfMessage( $key, $args )->inLanguage( $languageCode )->text();
109
110 // Record the template, args, language, and expected result
111 // fake multiple languages by flattening them together.
112 $langKey = $languageCode . '_' . $key;
113 $messages[$langKey] = $template;
114 $tests[] = array(
115 'name' => $languageCode . ' ' . $key . ' ' . join( ',', $args ),
116 'key' => $langKey,
117 'args' => $args,
118 'result' => $result,
119 'lang' => $languageCode
120 );
121 }
122 }
123 }
124 return array( $messages, $tests );
125 }
126
127 private function writeJavascriptFile( $messages, $tests, $dataSpecFile ) {
128 $phpParserData = array(
129 'messages' => $messages,
130 'tests' => $tests,
131 );
132
133 $output =
134 "// This file stores the output from the PHP parser for various messages, arguments,\n"
135 . "// languages, and parser modes. Intended for use by a unit test framework by looping\n"
136 . "// through the object and comparing its parser return value with the 'result' property.\n"
137 . '// Last generated with ' . basename( __FILE__ ) . ' at ' . gmdate( 'r' ) . "\n"
138 // This file will contain unquoted JSON strings as javascript native object literals,
139 // flip the quotemark convention for this file.
140 . "/*jshint quotmark: double */\n"
141 . "\n"
142 . 'mediaWiki.libs.phpParserData = ' . FormatJson::encode( $phpParserData, true ) . ";\n";
143
144 $fp = file_put_contents( $dataSpecFile, $output );
145 if ( $fp === false ) {
146 die( "Couldn't write to $dataSpecFile." );
147 }
148 }
149 }
150
151 $maintClass = "GenerateJqueryMsgData";
152 require_once( "$maintenanceDir/doMaintenance.php" );