ExtractTaskTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <?php
  2. /**
  3. * ExtractTaskTest file
  4. *
  5. * Test Case for i18n extraction shell task
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2012, Cake Software Foundation, Inc.
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  16. * @link http://cakephp.org CakePHP Project
  17. * @package Cake.Test.Case.Console.Command.Task
  18. * @since CakePHP v 1.2.0.7726
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('Folder', 'Utility');
  22. App::uses('ConsoleOutput', 'Console');
  23. App::uses('ConsoleInput', 'Console');
  24. App::uses('ShellDispatcher', 'Console');
  25. App::uses('Shell', 'Console');
  26. App::uses('ExtractTask', 'Console/Command/Task');
  27. /**
  28. * ExtractTaskTest class
  29. *
  30. * @package Cake.Test.Case.Console.Command.Task
  31. */
  32. class ExtractTaskTest extends CakeTestCase {
  33. /**
  34. * setUp method
  35. *
  36. * @return void
  37. */
  38. public function setUp() {
  39. parent::setUp();
  40. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  41. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  42. $this->Task = $this->getMock(
  43. 'ExtractTask',
  44. array('in', 'out', 'err', '_stop'),
  45. array($out, $out, $in)
  46. );
  47. $this->path = TMP . 'tests' . DS . 'extract_task_test';
  48. new Folder($this->path . DS . 'locale', true);
  49. }
  50. /**
  51. * tearDown method
  52. *
  53. * @return void
  54. */
  55. public function tearDown() {
  56. parent::tearDown();
  57. unset($this->Task);
  58. $Folder = new Folder($this->path);
  59. $Folder->delete();
  60. CakePlugin::unload();
  61. }
  62. /**
  63. * testExecute method
  64. *
  65. * @return void
  66. */
  67. public function testExecute() {
  68. $this->Task->interactive = false;
  69. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages';
  70. $this->Task->params['output'] = $this->path . DS;
  71. $this->Task->params['extract-core'] = 'no';
  72. $this->Task->expects($this->never())->method('err');
  73. $this->Task->expects($this->any())->method('in')
  74. ->will($this->returnValue('y'));
  75. $this->Task->expects($this->never())->method('_stop');
  76. $this->Task->execute();
  77. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  78. $result = file_get_contents($this->path . DS . 'default.pot');
  79. $this->assertFalse(file_exists($this->path . DS . 'cake.pot'));
  80. $pattern = '/"Content-Type\: text\/plain; charset\=utf-8/';
  81. $this->assertRegExp($pattern, $result);
  82. $pattern = '/"Content-Transfer-Encoding\: 8bit/';
  83. $this->assertRegExp($pattern, $result);
  84. $pattern = '/"Plural-Forms\: nplurals\=INTEGER; plural\=EXPRESSION;/';
  85. $this->assertRegExp($pattern, $result);
  86. // home.ctp
  87. $pattern = '/msgid "Your tmp directory is writable."\nmsgstr ""\n/';
  88. $this->assertRegExp($pattern, $result);
  89. $pattern = '/msgid "Your tmp directory is NOT writable."\nmsgstr ""\n/';
  90. $this->assertRegExp($pattern, $result);
  91. $pattern = '/msgid "The %s is being used for caching. To change the config edit ';
  92. $pattern .= 'APP\/config\/core.php "\nmsgstr ""\n/';
  93. $this->assertRegExp($pattern, $result);
  94. $pattern = '/msgid "Your cache is NOT working. Please check ';
  95. $pattern .= 'the settings in APP\/config\/core.php"\nmsgstr ""\n/';
  96. $this->assertRegExp($pattern, $result);
  97. $pattern = '/msgid "Your database configuration file is present."\nmsgstr ""\n/';
  98. $this->assertRegExp($pattern, $result);
  99. $pattern = '/msgid "Your database configuration file is NOT present."\nmsgstr ""\n/';
  100. $this->assertRegExp($pattern, $result);
  101. $pattern = '/msgid "Rename config\/database.php.default to ';
  102. $pattern .= 'config\/database.php"\nmsgstr ""\n/';
  103. $this->assertRegExp($pattern, $result);
  104. $pattern = '/msgid "Cake is able to connect to the database."\nmsgstr ""\n/';
  105. $this->assertRegExp($pattern, $result);
  106. $pattern = '/msgid "Cake is NOT able to connect to the database."\nmsgstr ""\n/';
  107. $this->assertRegExp($pattern, $result);
  108. $pattern = '/msgid "Editing this Page"\nmsgstr ""\n/';
  109. $this->assertRegExp($pattern, $result);
  110. $pattern = '/msgid "To change the content of this page, create: APP\/views\/pages\/home\.ctp/';
  111. $this->assertRegExp($pattern, $result);
  112. $pattern = '/To change its layout, create: APP\/views\/layouts\/default\.ctp\./s';
  113. $this->assertRegExp($pattern, $result);
  114. // extract.ctp
  115. $pattern = '/\#: (\\\\|\/)extract\.ctp:15;6\n';
  116. $pattern .= 'msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  117. $this->assertRegExp($pattern, $result);
  118. $pattern = '/msgid "You have %d new message."\nmsgstr ""/';
  119. $this->assertNotRegExp($pattern, $result, 'No duplicate msgid');
  120. $pattern = '/\#: (\\\\|\/)extract\.ctp:7\n';
  121. $pattern .= 'msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  122. $this->assertRegExp($pattern, $result);
  123. $pattern = '/\#: (\\\\|\/)extract\.ctp:14\n';
  124. $pattern .= '\#: (\\\\|\/)home\.ctp:99\n';
  125. $pattern .= 'msgid "Editing this Page"\nmsgstr ""/';
  126. $this->assertRegExp($pattern, $result);
  127. $pattern = '/\#: (\\\\|\/)extract\.ctp:22\nmsgid "';
  128. $pattern .= 'Hot features!';
  129. $pattern .= '\\\n - No Configuration: Set-up the database and let the magic begin';
  130. $pattern .= '\\\n - Extremely Simple: Just look at the name...It\'s Cake';
  131. $pattern .= '\\\n - Active, Friendly Community: Join us #cakephp on IRC. We\'d love to help you get started';
  132. $pattern .= '"\nmsgstr ""/';
  133. $this->assertRegExp($pattern, $result);
  134. $this->assertContains('msgid "double \\"quoted\\""', $result, 'Strings with quotes not handled correctly');
  135. $this->assertContains("msgid \"single 'quoted'\"", $result, 'Strings with quotes not handled correctly');
  136. // extract.ctp - reading the domain.pot
  137. $result = file_get_contents($this->path . DS . 'domain.pot');
  138. $pattern = '/msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  139. $this->assertNotRegExp($pattern, $result);
  140. $pattern = '/msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  141. $this->assertNotRegExp($pattern, $result);
  142. $pattern = '/msgid "You have %d new message \(domain\)."\nmsgid_plural "You have %d new messages \(domain\)."/';
  143. $this->assertRegExp($pattern, $result);
  144. $pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
  145. $this->assertRegExp($pattern, $result);
  146. }
  147. /**
  148. * test exclusions
  149. *
  150. * @return void
  151. */
  152. public function testExtractWithExclude() {
  153. $this->Task->interactive = false;
  154. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS . 'View';
  155. $this->Task->params['output'] = $this->path . DS;
  156. $this->Task->params['exclude'] = 'Pages,Layouts';
  157. $this->Task->params['extract-core'] = 'no';
  158. $this->Task->expects($this->any())->method('in')
  159. ->will($this->returnValue('y'));
  160. $this->Task->execute();
  161. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  162. $result = file_get_contents($this->path . DS . 'default.pot');
  163. $pattern = '/\#: .*extract\.ctp:6\n/';
  164. $this->assertNotRegExp($pattern, $result);
  165. $pattern = '/\#: .*default\.ctp:26\n/';
  166. $this->assertNotRegExp($pattern, $result);
  167. }
  168. /**
  169. * test extract can read more than one path.
  170. *
  171. * @return void
  172. */
  173. public function testExtractMultiplePaths() {
  174. $this->Task->interactive = false;
  175. $this->Task->params['paths'] =
  176. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages,' .
  177. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts';
  178. $this->Task->params['output'] = $this->path . DS;
  179. $this->Task->params['extract-core'] = 'no';
  180. $this->Task->expects($this->never())->method('err');
  181. $this->Task->expects($this->never())->method('_stop');
  182. $this->Task->execute();
  183. $result = file_get_contents($this->path . DS . 'default.pot');
  184. $pattern = '/msgid "Add User"/';
  185. $this->assertRegExp($pattern, $result);
  186. }
  187. /**
  188. * Tests that it is possible to exclude plugin paths by enabling the param option for the ExtractTask
  189. *
  190. * @return void
  191. */
  192. public function testExtractExcludePlugins() {
  193. App::build(array(
  194. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  195. ));
  196. $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  197. $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
  198. $this->Task = $this->getMock('ExtractTask',
  199. array('_isExtractingApp', '_extractValidationMessages', 'in', 'out', 'err', 'clear', '_stop'),
  200. array($this->out, $this->out, $this->in)
  201. );
  202. $this->Task->expects($this->exactly(2))->method('_isExtractingApp')->will($this->returnValue(true));
  203. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
  204. $this->Task->params['output'] = $this->path . DS;
  205. $this->Task->params['exclude-plugins'] = true;
  206. $this->Task->execute();
  207. $result = file_get_contents($this->path . DS . 'default.pot');
  208. $this->assertNotRegExp('#TestPlugin#', $result);
  209. }
  210. /**
  211. * Test that is possible to extract messages form a single plugin
  212. *
  213. * @return void
  214. */
  215. public function testExtractPlugin() {
  216. App::build(array(
  217. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  218. ));
  219. $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  220. $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
  221. $this->Task = $this->getMock('ExtractTask',
  222. array('_isExtractingApp', '_extractValidationMessages', 'in', 'out', 'err', 'clear', '_stop'),
  223. array($this->out, $this->out, $this->in)
  224. );
  225. $this->Task->params['output'] = $this->path . DS;
  226. $this->Task->params['plugin'] = 'TestPlugin';
  227. $this->Task->execute();
  228. $result = file_get_contents($this->path . DS . 'default.pot');
  229. $this->assertNotRegExp('#Pages#', $result);
  230. $this->assertContains('translate.ctp:1', $result);
  231. $this->assertContains('This is a translatable string', $result);
  232. }
  233. /**
  234. * Tests that the task will inspect application models and extract the validation messages from them
  235. *
  236. * @return void
  237. */
  238. public function testExtractModelValidation() {
  239. App::build(array(
  240. 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
  241. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  242. ), App::RESET);
  243. $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  244. $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
  245. $this->Task = $this->getMock('ExtractTask',
  246. array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
  247. array($this->out, $this->out, $this->in)
  248. );
  249. $this->Task->expects($this->exactly(2))->method('_isExtractingApp')->will($this->returnValue(true));
  250. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
  251. $this->Task->params['output'] = $this->path . DS;
  252. $this->Task->params['extract-core'] = 'no';
  253. $this->Task->params['exclude-plugins'] = true;
  254. $this->Task->params['ignore-model-validation'] = false;
  255. $this->Task->execute();
  256. $result = file_get_contents($this->path . DS . 'default.pot');
  257. $pattern = preg_quote('#Model/PersisterOne.php:validation for field title#', '\\');
  258. $this->assertRegExp($pattern, $result);
  259. $pattern = preg_quote('#Model/PersisterOne.php:validation for field body#', '\\');
  260. $this->assertRegExp($pattern, $result);
  261. $pattern = '#msgid "Post title is required"#';
  262. $this->assertRegExp($pattern, $result);
  263. $pattern = '#msgid "You may enter up to %s chars \(minimum is %s chars\)"#';
  264. $this->assertRegExp($pattern, $result);
  265. $pattern = '#msgid "Post body is required"#';
  266. $this->assertRegExp($pattern, $result);
  267. $pattern = '#msgid "Post body is super required"#';
  268. $this->assertRegExp($pattern, $result);
  269. }
  270. /**
  271. * Tests that the task will inspect application models and extract the validation messages from them
  272. * while using a custom validation domain for the messages set on the model itself
  273. *
  274. * @return void
  275. */
  276. public function testExtractModelValidationWithDomainInModel() {
  277. App::build(array(
  278. 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Model' . DS)
  279. ));
  280. $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  281. $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
  282. $this->Task = $this->getMock('ExtractTask',
  283. array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
  284. array($this->out, $this->out, $this->in)
  285. );
  286. $this->Task->expects($this->exactly(2))->method('_isExtractingApp')->will($this->returnValue(true));
  287. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
  288. $this->Task->params['output'] = $this->path . DS;
  289. $this->Task->params['extract-core'] = 'no';
  290. $this->Task->params['exclude-plugins'] = true;
  291. $this->Task->params['ignore-model-validation'] = false;
  292. $this->Task->execute();
  293. $result = file_get_contents($this->path . DS . 'test_plugin.pot');
  294. $pattern = preg_quote('#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field title#', '\\');
  295. $this->assertRegExp($pattern, $result);
  296. $pattern = preg_quote('#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field body#', '\\');
  297. $this->assertRegExp($pattern, $result);
  298. $pattern = '#msgid "Post title is required"#';
  299. $this->assertRegExp($pattern, $result);
  300. $pattern = '#msgid "Post body is required"#';
  301. $this->assertRegExp($pattern, $result);
  302. $pattern = '#msgid "Post body is super required"#';
  303. $this->assertRegExp($pattern, $result);
  304. }
  305. /**
  306. * Test that the extract shell can obtain validation messages from models inside a specific plugin
  307. *
  308. * @return void
  309. */
  310. public function testExtractModelValidationInPlugin() {
  311. App::build(array(
  312. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  313. ));
  314. $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  315. $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
  316. $this->Task = $this->getMock('ExtractTask',
  317. array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
  318. array($this->out, $this->out, $this->in)
  319. );
  320. $this->Task->params['output'] = $this->path . DS;
  321. $this->Task->params['ignore-model-validation'] = false;
  322. $this->Task->params['plugin'] = 'TestPlugin';
  323. $this->Task->execute();
  324. $result = file_get_contents($this->path . DS . 'test_plugin.pot');
  325. $pattern = preg_quote('#Model/TestPluginPost.php:validation for field title#', '\\');
  326. $this->assertRegExp($pattern, $result);
  327. $pattern = preg_quote('#Model/TestPluginPost.php:validation for field body#', '\\');
  328. $this->assertRegExp($pattern, $result);
  329. $pattern = '#msgid "Post title is required"#';
  330. $this->assertRegExp($pattern, $result);
  331. $pattern = '#msgid "Post body is required"#';
  332. $this->assertRegExp($pattern, $result);
  333. $pattern = '#msgid "Post body is super required"#';
  334. $this->assertRegExp($pattern, $result);
  335. $pattern = '#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field title#';
  336. $this->assertNotRegExp($pattern, $result);
  337. }
  338. /**
  339. * Test that the extract shell overwrites existing files with the overwrite parameter
  340. *
  341. * @return void
  342. */
  343. public function testExtractOverwrite() {
  344. $this->Task->interactive = false;
  345. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
  346. $this->Task->params['output'] = $this->path . DS;
  347. $this->Task->params['extract-core'] = 'no';
  348. $this->Task->params['overwrite'] = true;
  349. file_put_contents($this->path . DS . 'default.pot', 'will be overwritten');
  350. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  351. $original = file_get_contents($this->path . DS . 'default.pot');
  352. $this->Task->execute();
  353. $result = file_get_contents($this->path . DS . 'default.pot');
  354. $this->assertNotEquals($original, $result);
  355. }
  356. /**
  357. * Test that the extract shell scans the core libs
  358. *
  359. * @return void
  360. */
  361. public function testExtractCore() {
  362. $this->Task->interactive = false;
  363. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
  364. $this->Task->params['output'] = $this->path . DS;
  365. $this->Task->params['extract-core'] = 'yes';
  366. $this->Task->execute();
  367. $this->assertTrue(file_exists($this->path . DS . 'cake.pot'));
  368. $result = file_get_contents($this->path . DS . 'cake.pot');
  369. $pattern = '/msgid "Yesterday, %s"\nmsgstr ""\n/';
  370. $this->assertRegExp($pattern, $result);
  371. $this->assertTrue(file_exists($this->path . DS . 'cake_dev.pot'));
  372. $result = file_get_contents($this->path . DS . 'cake_dev.pot');
  373. $pattern = '/#: Console\/Templates\//';
  374. $this->assertNotRegExp($pattern, $result);
  375. $pattern = '/#: Test\//';
  376. $this->assertNotRegExp($pattern, $result);
  377. }
  378. }