1ac8dcba56756201f8bf92ee8a9d7d719d3dd6d0
[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 = wfMsgGetKey( $key, /* useDb */ true, $languageCode, /* transform */ false );
61
62 // get the magic-parsed version with args
63 $wfMsgExtArgs = array_merge( array( $key, $wfMsgExtOptions ), $args );
64 $result = call_user_func_array( 'wfMsgExt', $wfMsgExtArgs );
65
66 // record the template, args, language, and expected result
67 // fake multiple languages by flattening them together
68 $langKey = $languageCode . '_' . $key;
69 $messages[ $langKey ] = $template;
70 $tests[] = array(
71 'name' => $languageCode . " " . $key . " " . join( ",", $args ),
72 'key' => $langKey,
73 'args' => $args,
74 'result' => $result,
75 'lang' => $languageCode
76 );
77 }
78 }
79 }
80 return array( $messages, $tests );
81 }
82
83 private function writeJavascriptFile( $messages, $tests, $dataSpecFile ) {
84 global $argv;
85 $arguments = count($argv) ? $argv : $_SERVER[ 'argv' ];
86
87 $json = new Services_JSON;
88 $json->pretty = true;
89 $javascriptPrologue = "// This file stores the results from the PHP parser for certain messages and arguments,\n"
90 . "// so we can test the equivalent Javascript libraries.\n"
91 . '// Last generated with ' . join(' ', $arguments) . ' at ' . gmdate('c') . "\n\n";
92 $javascriptMessages = "mediaWiki.messages.set( " . $json->encode( $messages, true ) . " );\n";
93 $javascriptTests = 'var jasmineMsgSpec = ' . $json->encode( $tests, true ) . ";\n";
94
95 $fp = fopen( $dataSpecFile, 'w' );
96 if ( !$fp ) {
97 die( "couldn't open $dataSpecFile for writing" );
98 }
99 $success = fwrite( $fp, $javascriptPrologue . $javascriptMessages . $javascriptTests );
100 if ( !$success ) {
101 die( "couldn't write to $dataSpecFile" );
102 }
103 $success = fclose( $fp );
104 if ( !$success ) {
105 die( "couldn't close $dataSpecFile" );
106 }
107 }
108 }
109
110 $maintClass = "MakeLanguageSpec";
111 require_once( "$maintenanceDir/doMaintenance.php" );
112
113
114