ExtractTaskTest.php 16 KB

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