ExtractTaskTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. * @since 1.2.0
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  18. */
  19. namespace Cake\Test\TestCase\Console\Command\Task;
  20. use Cake\Console\Command\Task\ExtractTask;
  21. use Cake\Core\App;
  22. use Cake\Core\Configure;
  23. use Cake\Core\Plugin;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\Folder;
  26. /**
  27. * ExtractTaskTest class
  28. *
  29. */
  30. class ExtractTaskTest extends TestCase {
  31. /**
  32. * setUp method
  33. *
  34. * @return void
  35. */
  36. public function setUp() {
  37. parent::setUp();
  38. $out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
  39. $in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
  40. $this->Task = $this->getMock(
  41. 'Cake\Console\Command\Task\ExtractTask',
  42. array('in', 'out', 'err', '_stop'),
  43. array($out, $out, $in)
  44. );
  45. $this->path = TMP . 'tests/extract_task_test';
  46. new Folder($this->path . DS . 'locale', true);
  47. }
  48. /**
  49. * tearDown method
  50. *
  51. * @return void
  52. */
  53. public function tearDown() {
  54. parent::tearDown();
  55. unset($this->Task);
  56. $Folder = new Folder($this->path);
  57. $Folder->delete();
  58. Plugin::unload();
  59. }
  60. /**
  61. * testExecute method
  62. *
  63. * @return void
  64. */
  65. public function testExecute() {
  66. $this->Task->interactive = false;
  67. $this->Task->params['paths'] = TEST_APP . 'TestApp/Template/Pages';
  68. $this->Task->params['output'] = $this->path . DS;
  69. $this->Task->params['extract-core'] = 'no';
  70. $this->Task->expects($this->never())->method('err');
  71. $this->Task->expects($this->any())->method('in')
  72. ->will($this->returnValue('y'));
  73. $this->Task->expects($this->never())->method('_stop');
  74. $this->Task->execute();
  75. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  76. $result = file_get_contents($this->path . DS . 'default.pot');
  77. $this->assertFalse(file_exists($this->path . DS . 'cake.pot'));
  78. // extract.ctp
  79. $pattern = '/\#: (\\\\|\/)extract\.ctp:\d+;\d+\n';
  80. $pattern .= 'msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  81. $this->assertRegExp($pattern, $result);
  82. $pattern = '/msgid "You have %d new message."\nmsgstr ""/';
  83. $this->assertNotRegExp($pattern, $result, 'No duplicate msgid');
  84. $pattern = '/\#: (\\\\|\/)extract\.ctp:\d+\n';
  85. $pattern .= 'msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  86. $this->assertRegExp($pattern, $result);
  87. $pattern = '/\#: (\\\\|\/)extract\.ctp:\d+\nmsgid "';
  88. $pattern .= 'Hot features!';
  89. $pattern .= '\\\n - No Configuration: Set-up the database and let the magic begin';
  90. $pattern .= '\\\n - Extremely Simple: Just look at the name...It\'s Cake';
  91. $pattern .= '\\\n - Active, Friendly Community: Join us #cakephp on IRC. We\'d love to help you get started';
  92. $pattern .= '"\nmsgstr ""/';
  93. $this->assertRegExp($pattern, $result);
  94. $this->assertContains('msgid "double \\"quoted\\""', $result, 'Strings with quotes not handled correctly');
  95. $this->assertContains("msgid \"single 'quoted'\"", $result, 'Strings with quotes not handled correctly');
  96. // extract.ctp - reading the domain.pot
  97. $result = file_get_contents($this->path . DS . 'domain.pot');
  98. $pattern = '/msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  99. $this->assertNotRegExp($pattern, $result);
  100. $pattern = '/msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  101. $this->assertNotRegExp($pattern, $result);
  102. $pattern = '/msgid "You have %d new message \(domain\)."\nmsgid_plural "You have %d new messages \(domain\)."/';
  103. $this->assertRegExp($pattern, $result);
  104. $pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
  105. $this->assertRegExp($pattern, $result);
  106. }
  107. /**
  108. * testExtractCategory method
  109. *
  110. * @return void
  111. */
  112. public function testExtractCategory() {
  113. $this->Task->interactive = false;
  114. $this->Task->params['paths'] = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages';
  115. $this->Task->params['output'] = $this->path . DS;
  116. $this->Task->params['extract-core'] = 'no';
  117. $this->Task->params['merge'] = 'no';
  118. $this->Task->expects($this->never())->method('err');
  119. $this->Task->expects($this->any())->method('in')
  120. ->will($this->returnValue('y'));
  121. $this->Task->expects($this->never())->method('_stop');
  122. $this->Task->execute();
  123. $this->assertTrue(file_exists($this->path . DS . 'LC_TIME' . DS . 'default.pot'));
  124. $result = file_get_contents($this->path . DS . 'default.pot');
  125. $this->assertNotContains('You have a new message (category: LC_TIME).', $result);
  126. }
  127. /**
  128. * test exclusions
  129. *
  130. * @return void
  131. */
  132. public function testExtractWithExclude() {
  133. $this->Task->interactive = false;
  134. $this->Task->params['paths'] = TEST_APP . 'TestApp/Template';
  135. $this->Task->params['output'] = $this->path . DS;
  136. $this->Task->params['exclude'] = 'Pages,Layout';
  137. $this->Task->params['extract-core'] = 'no';
  138. $this->Task->expects($this->any())->method('in')
  139. ->will($this->returnValue('y'));
  140. $this->Task->execute();
  141. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  142. $result = file_get_contents($this->path . DS . 'default.pot');
  143. $pattern = '/\#: .*extract\.ctp:\d+\n/';
  144. $this->assertNotRegExp($pattern, $result);
  145. $pattern = '/\#: .*default\.ctp:\d+\n/';
  146. $this->assertNotRegExp($pattern, $result);
  147. }
  148. /**
  149. * test extract can read more than one path.
  150. *
  151. * @return void
  152. */
  153. public function testExtractMultiplePaths() {
  154. $this->Task->interactive = false;
  155. $this->Task->params['paths'] =
  156. TEST_APP . 'TestApp/Template/Pages,' .
  157. TEST_APP . 'TestApp/Template/Posts';
  158. $this->Task->params['output'] = $this->path . DS;
  159. $this->Task->params['extract-core'] = 'no';
  160. $this->Task->expects($this->never())->method('err');
  161. $this->Task->expects($this->never())->method('_stop');
  162. $this->Task->execute();
  163. $result = file_get_contents($this->path . DS . 'default.pot');
  164. $pattern = '/msgid "Add User"/';
  165. $this->assertRegExp($pattern, $result);
  166. }
  167. /**
  168. * Tests that it is possible to exclude plugin paths by enabling the param option for the ExtractTask
  169. *
  170. * @return void
  171. */
  172. public function testExtractExcludePlugins() {
  173. Configure::write('App.namespace', 'TestApp');
  174. $this->out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
  175. $this->in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
  176. $this->Task = $this->getMock('Cake\Console\Command\Task\ExtractTask',
  177. array('_isExtractingApp', '_extractValidationMessages', 'in', 'out', 'err', 'clear', '_stop'),
  178. array($this->out, $this->out, $this->in)
  179. );
  180. $this->Task->expects($this->exactly(2))
  181. ->method('_isExtractingApp')
  182. ->will($this->returnValue(true));
  183. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  184. $this->Task->params['output'] = $this->path . DS;
  185. $this->Task->params['exclude-plugins'] = true;
  186. $this->Task->execute();
  187. $result = file_get_contents($this->path . DS . 'default.pot');
  188. $this->assertNotRegExp('#TestPlugin#', $result);
  189. }
  190. /**
  191. * Test that is possible to extract messages form a single plugin
  192. *
  193. * @return void
  194. */
  195. public function testExtractPlugin() {
  196. Configure::write('App.namespace', 'TestApp');
  197. $this->out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
  198. $this->in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
  199. $this->Task = $this->getMock('Cake\Console\Command\Task\ExtractTask',
  200. array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
  201. array($this->out, $this->out, $this->in)
  202. );
  203. $this->Task->params['output'] = $this->path . DS;
  204. $this->Task->params['plugin'] = 'TestPlugin';
  205. $this->markTestIncomplete('Extracting validation messages from plugin models is not working.');
  206. $this->Task->execute();
  207. $result = file_get_contents($this->path . DS . 'default.pot');
  208. $this->assertNotRegExp('#Pages#', $result);
  209. $this->assertRegExp('/translate\.ctp:\d+/', $result);
  210. $this->assertContains('This is a translatable string', $result);
  211. $this->assertContains('I can haz plugin model validation message', $result);
  212. }
  213. /**
  214. * Tests that the task will inspect application models and extract the validation messages from them
  215. *
  216. * @return void
  217. */
  218. public function testExtractModelValidation() {
  219. $this->markTestIncomplete('Extracting validation messages is not working right now.');
  220. Configure::write('App.namespace', 'TestApp');
  221. Plugin::load('TestPlugin');
  222. $this->out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
  223. $this->in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
  224. $this->Task = $this->getMock('Cake\Console\Command\Task\ExtractTask',
  225. array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
  226. array($this->out, $this->out, $this->in)
  227. );
  228. $this->Task->expects($this->exactly(2))
  229. ->method('_isExtractingApp')
  230. ->will($this->returnValue(true));
  231. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  232. $this->Task->params['output'] = $this->path . DS;
  233. $this->Task->params['extract-core'] = 'no';
  234. $this->Task->params['exclude-plugins'] = true;
  235. $this->Task->params['ignore-model-validation'] = false;
  236. $this->Task->execute();
  237. $result = file_get_contents($this->path . DS . 'default.pot');
  238. $pattern = preg_quote('#Model/PersisterOne.php:validation for field title#', '\\');
  239. $this->assertRegExp($pattern, $result);
  240. $pattern = preg_quote('#Model/PersisterOne.php:validation for field body#', '\\');
  241. $this->assertRegExp($pattern, $result);
  242. $pattern = '#msgid "Post title is required"#';
  243. $this->assertRegExp($pattern, $result);
  244. $pattern = '#msgid "You may enter up to %s chars \(minimum is %s chars\)"#';
  245. $this->assertRegExp($pattern, $result);
  246. $pattern = '#msgid "Post body is required"#';
  247. $this->assertRegExp($pattern, $result);
  248. $pattern = '#msgid "Post body is super required"#';
  249. $this->assertRegExp($pattern, $result);
  250. $this->assertContains('msgid "double \\"quoted\\" validation"', $result, 'Strings with quotes not handled correctly');
  251. $this->assertContains("msgid \"single 'quoted' validation\"", $result, 'Strings with quotes not handled correctly');
  252. }
  253. /**
  254. * Test that the extract shell can obtain validation messages from models inside a specific plugin
  255. *
  256. * @return void
  257. */
  258. public function testExtractModelValidationInPlugin() {
  259. $this->markTestIncomplete('Extracting validation messages is not working right now.');
  260. Configure::write('App.namespace', 'TestApp');
  261. Plugin::load('TestPlugin');
  262. $this->out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
  263. $this->in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
  264. $this->Task = $this->getMock('Cake\Console\Command\Task\ExtractTask',
  265. array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
  266. array($this->out, $this->out, $this->in)
  267. );
  268. $this->Task->params['output'] = $this->path . DS;
  269. $this->Task->params['ignore-model-validation'] = false;
  270. $this->Task->params['plugin'] = 'TestPlugin';
  271. $this->Task->execute();
  272. $result = file_get_contents($this->path . DS . 'test_plugin.pot');
  273. $pattern = preg_quote('#Model/TestPluginPost.php:validation for field title#', '\\');
  274. $this->assertRegExp($pattern, $result);
  275. $pattern = preg_quote('#Model/TestPluginPost.php:validation for field body#', '\\');
  276. $this->assertRegExp($pattern, $result);
  277. $pattern = '#msgid "Post title is required"#';
  278. $this->assertRegExp($pattern, $result);
  279. $pattern = '#msgid "Post body is required"#';
  280. $this->assertRegExp($pattern, $result);
  281. $pattern = '#msgid "Post body is super required"#';
  282. $this->assertRegExp($pattern, $result);
  283. $pattern = '#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field title#';
  284. $this->assertNotRegExp($pattern, $result);
  285. }
  286. /**
  287. * Test that the extract shell overwrites existing files with the overwrite parameter
  288. *
  289. * @return void
  290. */
  291. public function testExtractOverwrite() {
  292. Configure::write('App.namespace', 'TestApp');
  293. $this->Task->interactive = false;
  294. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  295. $this->Task->params['output'] = $this->path . DS;
  296. $this->Task->params['extract-core'] = 'no';
  297. $this->Task->params['overwrite'] = true;
  298. file_put_contents($this->path . DS . 'default.pot', 'will be overwritten');
  299. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  300. $original = file_get_contents($this->path . DS . 'default.pot');
  301. $this->Task->execute();
  302. $result = file_get_contents($this->path . DS . 'default.pot');
  303. $this->assertNotEquals($original, $result);
  304. }
  305. /**
  306. * Test that the extract shell scans the core libs
  307. *
  308. * @return void
  309. */
  310. public function testExtractCore() {
  311. Configure::write('App.namespace', 'TestApp');
  312. $this->Task->interactive = false;
  313. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  314. $this->Task->params['output'] = $this->path . DS;
  315. $this->Task->params['extract-core'] = 'yes';
  316. $this->Task->execute();
  317. $this->assertTrue(file_exists($this->path . DS . 'cake.pot'));
  318. $result = file_get_contents($this->path . DS . 'cake.pot');
  319. $pattern = '/#: Console\/Templates\//';
  320. $this->assertNotRegExp($pattern, $result);
  321. $pattern = '/#: Test\//';
  322. $this->assertNotRegExp($pattern, $result);
  323. }
  324. }