phpcs: More require/include is not a function
[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 require __DIR__ . '/../../../maintenance/Maintenance.php';
65
66 class GenerateJqueryMsgData extends Maintenance {
67
68 static $keyToTestArgs = array(
69 'undelete_short' => array(
70 array( 0 ),
71 array( 1 ),
72 array( 2 ),
73 array( 5 ),
74 array( 21 ),
75 array( 101 )
76 ),
77 'category-subcat-count' => array(
78 array( 0, 10 ),
79 array( 1, 1 ),
80 array( 1, 2 ),
81 array( 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 = array();
98 $tests = array();
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();
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[] = array(
113 'name' => $languageCode . ' ' . $key . ' ' . join( ',', $args ),
114 'key' => $langKey,
115 'args' => $args,
116 'result' => $result,
117 'lang' => $languageCode
118 );
119 }
120 }
121 }
122 return array( $messages, $tests );
123 }
124
125 private function writeJavascriptFile( $messages, $tests, $dataSpecFile ) {
126 $phpParserData = array(
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 // This file will contain unquoted JSON strings as javascript native object literals,
137 // flip the quotemark convention for this file.
138 . "/*jshint quotmark: double */\n"
139 . "\n"
140 . 'mediaWiki.libs.phpParserData = ' . FormatJson::encode( $phpParserData, true ) . ";\n";
141
142 $fp = file_put_contents( $dataSpecFile, $output );
143 if ( $fp === false ) {
144 die( "Couldn't write to $dataSpecFile." );
145 }
146 }
147 }
148
149 $maintClass = "GenerateJqueryMsgData";
150 require_once RUN_MAINTENANCE_IF_MAIN;