ExtractTaskTest.php 15 KB

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