ExtractTaskTest.php 17 KB

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