Merge "Convert last usages of wfMsg*() to wfMessage()."
[lhc/web/wiklou.git] / tests / jasmine / spec_makers / makeJqueryMsgSpec.php
1 <?php
2
3 /**
4 * This PHP script defines the spec that the Javascript message parser should conform to.
5 *
6 * It does this by looking up the results of various string kinds of string parsing, with various languages,
7 * in the current installation of MediaWiki. It then outputs a static specification, mapping expected inputs to outputs,
8 * which can be used with the JasmineBDD framework. This specification can then be used by simply including it into
9 * the SpecRunner.html file.
10 *
11 * This is similar to Michael Dale (mdale@mediawiki.org)'s parser tests, except that it doesn't look up the
12 * API results while doing the test, so the Jasmine run is much faster(at the cost of being out of date in rare
13 * circumstances. But mostly the parsing that we are doing in Javascript doesn't change much.)
14 *
15 */
16
17 $maintenanceDir = dirname( dirname( dirname( __DIR__ ) ) ) . '/maintenance';
18
19 require( "$maintenanceDir/Maintenance.php" );
20
21 class MakeLanguageSpec extends Maintenance {
22
23 static $keyToTestArgs = array(
24 'undelete_short' => array(
25 array( 0 ),
26 array( 1 ),
27 array( 2 ),
28 array( 5 ),
29 array( 21 ),
30 array( 101 )
31 ),
32 'category-subcat-count' => array(
33 array( 0, 10 ),
34 array( 1, 1 ),
35 array( 1, 2 ),
36 array( 3, 30 )
37 )
38 );
39
40 public function __construct() {
41 parent::__construct();
42 $this->mDescription = "Create a JasmineBDD-compatible specification for message parsing";
43 // add any other options here
44 }
45
46 public function execute() {
47 list( $messages, $tests ) = $this->getMessagesAndTests();
48 $this->writeJavascriptFile( $messages, $tests, "spec/mediawiki.language.parser.spec.data.js" );
49 }
50
51 private function getMessagesAndTests() {
52 $messages = array();
53 $tests = array();
54 foreach ( array( 'en', 'fr', 'ar', 'jp', 'zh' ) as $languageCode ) {
55 foreach ( self::$keyToTestArgs as $key => $testArgs ) {
56 foreach ($testArgs as $args) {
57 // get the raw template, without any transformations
58 $template = wfMessage( $key )->inLanguage( $languageCode )->plain();
59
60 $result = wfMessage( $key, $args )->inLanguage( $languageCode )->text();
61
62 // record the template, args, language, and expected result
63 // fake multiple languages by flattening them together
64 $langKey = $languageCode . '_' . $key;
65 $messages[ $langKey ] = $template;
66 $tests[] = array(
67 'name' => $languageCode . " " . $key . " " . join( ",", $args ),
68 'key' => $langKey,
69 'args' => $args,
70 'result' => $result,
71 'lang' => $languageCode
72 );
73 }
74 }
75 }
76 return array( $messages, $tests );
77 }
78
79 private function writeJavascriptFile( $messages, $tests, $dataSpecFile ) {
80 global $argv;
81 $arguments = count($argv) ? $argv : $_SERVER[ 'argv' ];
82
83 $json = new Services_JSON;
84 $json->pretty = true;
85 $javascriptPrologue = "// This file stores the results from the PHP parser for certain messages and arguments,\n"
86 . "// so we can test the equivalent Javascript libraries.\n"
87 . '// Last generated with ' . join(' ', $arguments) . ' at ' . gmdate('c') . "\n\n";
88 $javascriptMessages = "mediaWiki.messages.set( " . $json->encode( $messages, true ) . " );\n";
89 $javascriptTests = 'var jasmineMsgSpec = ' . $json->encode( $tests, true ) . ";\n";
90
91 $fp = fopen( $dataSpecFile, 'w' );
92 if ( !$fp ) {
93 die( "couldn't open $dataSpecFile for writing" );
94 }
95 $success = fwrite( $fp, $javascriptPrologue . $javascriptMessages . $javascriptTests );
96 if ( !$success ) {
97 die( "couldn't write to $dataSpecFile" );
98 }
99 $success = fclose( $fp );
100 if ( !$success ) {
101 die( "couldn't close $dataSpecFile" );
102 }
103 }
104 }
105
106 $maintClass = "MakeLanguageSpec";
107 require_once( "$maintenanceDir/doMaintenance.php" );
108
109
110