Merge "Revert "merged master"" into Wikidata
[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( dirname( dirname( __FILE__ ) ) ) ) ) . '/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 $wfMsgExtOptions = array( 'parsemag' );
55 foreach ( array( 'en', 'fr', 'ar', 'jp', 'zh' ) as $languageCode ) {
56 $wfMsgExtOptions['language'] = $languageCode;
57 foreach ( self::$keyToTestArgs as $key => $testArgs ) {
58 foreach ($testArgs as $args) {
59 // get the raw template, without any transformations
60 $template = wfMessage( $key )->inLanguage( $languageCode )->plain();
61
62 // get the magic-parsed version with args
63 $wfMsgExtArgs = array_merge( array( $key, $wfMsgExtOptions ), $args );
64 // @todo FIXME: Use Message class.
65 $result = call_user_func_array( 'wfMsgExt', $wfMsgExtArgs );
66
67 // record the template, args, language, and expected result
68 // fake multiple languages by flattening them together
69 $langKey = $languageCode . '_' . $key;
70 $messages[ $langKey ] = $template;
71 $tests[] = array(
72 'name' => $languageCode . " " . $key . " " . join( ",", $args ),
73 'key' => $langKey,
74 'args' => $args,
75 'result' => $result,
76 'lang' => $languageCode
77 );
78 }
79 }
80 }
81 return array( $messages, $tests );
82 }
83
84 private function writeJavascriptFile( $messages, $tests, $dataSpecFile ) {
85 global $argv;
86 $arguments = count($argv) ? $argv : $_SERVER[ 'argv' ];
87
88 $json = new Services_JSON;
89 $json->pretty = true;
90 $javascriptPrologue = "// This file stores the results from the PHP parser for certain messages and arguments,\n"
91 . "// so we can test the equivalent Javascript libraries.\n"
92 . '// Last generated with ' . join(' ', $arguments) . ' at ' . gmdate('c') . "\n\n";
93 $javascriptMessages = "mediaWiki.messages.set( " . $json->encode( $messages, true ) . " );\n";
94 $javascriptTests = 'var jasmineMsgSpec = ' . $json->encode( $tests, true ) . ";\n";
95
96 $fp = fopen( $dataSpecFile, 'w' );
97 if ( !$fp ) {
98 die( "couldn't open $dataSpecFile for writing" );
99 }
100 $success = fwrite( $fp, $javascriptPrologue . $javascriptMessages . $javascriptTests );
101 if ( !$success ) {
102 die( "couldn't write to $dataSpecFile" );
103 }
104 $success = fclose( $fp );
105 if ( !$success ) {
106 die( "couldn't close $dataSpecFile" );
107 }
108 }
109 }
110
111 $maintClass = "MakeLanguageSpec";
112 require_once( "$maintenanceDir/doMaintenance.php" );
113
114
115