PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/qunit/data/generateJqueryMsgData.php

https://gitlab.com/link233/bootmw
PHP | 148 lines | 71 code | 12 blank | 65 comment | 1 complexity | 2d96c1787378eaf3653dd84ce198780e MD5 | raw file
  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. * @example QUnit
  17. * <code>
  18. QUnit.test( 'Output matches PHP parser', mw.libs.phpParserData.tests.length, function ( assert ) {
  19. mw.messages.set( mw.libs.phpParserData.messages );
  20. $.each( mw.libs.phpParserData.tests, function ( i, test ) {
  21. QUnit.stop();
  22. getMwLanguage( test.lang, function ( langClass ) {
  23. var parser = new mw.jqueryMsg.parser( { language: langClass } );
  24. assert.equal(
  25. parser.parse( test.key, test.args ).html(),
  26. test.result,
  27. test.name
  28. );
  29. QUnit.start();
  30. } );
  31. } );
  32. });
  33. * </code>
  34. *
  35. * @example Jasmine
  36. * <code>
  37. describe( 'match output to output from PHP parser', function () {
  38. mw.messages.set( mw.libs.phpParserData.messages );
  39. $.each( mw.libs.phpParserData.tests, function ( i, test ) {
  40. it( 'should parse ' + test.name, function () {
  41. var langClass;
  42. runs( function () {
  43. getMwLanguage( test.lang, function ( gotIt ) {
  44. langClass = gotIt;
  45. });
  46. });
  47. waitsFor( function () {
  48. return langClass !== undefined;
  49. }, 'Language class should be loaded', 1000 );
  50. runs( function () {
  51. console.log( test.lang, 'running tests' );
  52. var parser = new mw.jqueryMsg.parser( { language: langClass } );
  53. expect(
  54. parser.parse( test.key, test.args ).html()
  55. ).toEqual( test.result );
  56. } );
  57. } );
  58. } );
  59. } );
  60. * </code>
  61. */
  62. require __DIR__ . '/../../../maintenance/Maintenance.php';
  63. class GenerateJqueryMsgData extends Maintenance {
  64. public static $keyToTestArgs = [
  65. 'undelete_short' => [
  66. [ 0 ],
  67. [ 1 ],
  68. [ 2 ],
  69. [ 5 ],
  70. [ 21 ],
  71. [ 101 ]
  72. ],
  73. 'category-subcat-count' => [
  74. [ 0, 10 ],
  75. [ 1, 1 ],
  76. [ 1, 2 ],
  77. [ 3, 30 ]
  78. ]
  79. ];
  80. public function __construct() {
  81. parent::__construct();
  82. $this->mDescription = 'Create a specification for message parsing ini JSON format';
  83. // add any other options here
  84. }
  85. public function execute() {
  86. list( $messages, $tests ) = $this->getMessagesAndTests();
  87. $this->writeJavascriptFile( $messages, $tests, __DIR__ . '/mediawiki.jqueryMsg.data.js' );
  88. }
  89. private function getMessagesAndTests() {
  90. $messages = [];
  91. $tests = [];
  92. foreach ( [ 'en', 'fr', 'ar', 'jp', 'zh' ] as $languageCode ) {
  93. foreach ( self::$keyToTestArgs as $key => $testArgs ) {
  94. foreach ( $testArgs as $args ) {
  95. // Get the raw message, without any transformations.
  96. $template = wfMessage( $key )->inLanguage( $languageCode )->plain();
  97. // Get the magic-parsed version with args.
  98. $result = wfMessage( $key, $args )->inLanguage( $languageCode )->text();
  99. // Record the template, args, language, and expected result
  100. // fake multiple languages by flattening them together.
  101. $langKey = $languageCode . '_' . $key;
  102. $messages[$langKey] = $template;
  103. $tests[] = [
  104. 'name' => $languageCode . ' ' . $key . ' ' . implode( ',', $args ),
  105. 'key' => $langKey,
  106. 'args' => $args,
  107. 'result' => $result,
  108. 'lang' => $languageCode
  109. ];
  110. }
  111. }
  112. }
  113. return [ $messages, $tests ];
  114. }
  115. private function writeJavascriptFile( $messages, $tests, $dataSpecFile ) {
  116. $phpParserData = [
  117. 'messages' => $messages,
  118. 'tests' => $tests,
  119. ];
  120. $output =
  121. "// This file stores the output from the PHP parser for various messages, arguments,\n"
  122. . "// languages, and parser modes. Intended for use by a unit test framework by looping\n"
  123. . "// through the object and comparing its parser return value with the 'result' property.\n"
  124. . '// Last generated with ' . basename( __FILE__ ) . ' at ' . gmdate( 'r' ) . "\n"
  125. . "//jscs:disable\n"
  126. . "\n"
  127. . 'mediaWiki.libs.phpParserData = ' . FormatJson::encode( $phpParserData, true ) . ";\n";
  128. $fp = file_put_contents( $dataSpecFile, $output );
  129. if ( $fp === false ) {
  130. die( "Couldn't write to $dataSpecFile." );
  131. }
  132. }
  133. }
  134. $maintClass = "GenerateJqueryMsgData";
  135. require_once RUN_MAINTENANCE_IF_MAIN;