]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - tests/qunit/data/generateJqueryMsgData.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.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', 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 require __DIR__ . '/../../../maintenance/Maintenance.php';
65
66 class GenerateJqueryMsgData extends Maintenance {
67
68         public static $keyToTestArgs = [
69                 'undelete_short' => [
70                         [ 0 ],
71                         [ 1 ],
72                         [ 2 ],
73                         [ 5 ],
74                         [ 21 ],
75                         [ 101 ]
76                 ],
77                 'category-subcat-count' => [
78                         [ 0, 10 ],
79                         [ 1, 1 ],
80                         [ 1, 2 ],
81                         [ 3, 30 ]
82                 ]
83         ];
84
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
89         }
90
91         public function execute() {
92                 list( $messages, $tests ) = $this->getMessagesAndTests();
93                 $this->writeJavascriptFile( $messages, $tests, __DIR__ . '/mediawiki.jqueryMsg.data.js' );
94         }
95
96         private function getMessagesAndTests() {
97                 $messages = [];
98                 $tests = [];
99                 foreach ( [ '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();
104
105                                         // Get the magic-parsed version with args.
106                                         $result = wfMessage( $key, $args )->inLanguage( $languageCode )->text();
107
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;
112                                         $tests[] = [
113                                                 'name' => $languageCode . ' ' . $key . ' ' . implode( ',', $args ),
114                                                 'key' => $langKey,
115                                                 'args' => $args,
116                                                 'result' => $result,
117                                                 'lang' => $languageCode
118                                         ];
119                                 }
120                         }
121                 }
122                 return [ $messages, $tests ];
123         }
124
125         private function writeJavascriptFile( $messages, $tests, $dataSpecFile ) {
126                 $phpParserData = [
127                         'messages' => $messages,
128                         'tests' => $tests,
129                 ];
130
131                 $output =
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                                 . "/* eslint-disable */\n"
137                                 . "\n"
138                                 . 'mediaWiki.libs.phpParserData = ' . FormatJson::encode( $phpParserData, true ) . ";\n";
139
140                 $fp = file_put_contents( $dataSpecFile, $output );
141                 if ( $fp === false ) {
142                         die( "Couldn't write to $dataSpecFile." );
143                 }
144         }
145 }
146
147 $maintClass = "GenerateJqueryMsgData";
148 require_once RUN_MAINTENANCE_IF_MAIN;