ExtractTaskTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Shell\Task;
  16. use Cake\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\Filesystem\Folder;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * ExtractTaskTest class
  22. *
  23. */
  24. class ExtractTaskTest extends TestCase
  25. {
  26. /**
  27. * setUp method
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. parent::setUp();
  34. $this->io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  35. $progress = $this->getMock('Cake\Shell\Helper\ProgressHelper', [], [$this->io]);
  36. $this->io->method('helper')
  37. ->will($this->returnValue($progress));
  38. $this->Task = $this->getMock(
  39. 'Cake\Shell\Task\ExtractTask',
  40. ['in', 'out', 'err', '_stop'],
  41. [$this->io]
  42. );
  43. $this->path = TMP . 'tests/extract_task_test';
  44. new Folder($this->path . DS . 'locale', true);
  45. }
  46. /**
  47. * tearDown method
  48. *
  49. * @return void
  50. */
  51. public function tearDown()
  52. {
  53. parent::tearDown();
  54. unset($this->Task);
  55. $Folder = new Folder($this->path);
  56. $Folder->delete();
  57. Plugin::unload();
  58. }
  59. /**
  60. * testExecute method
  61. *
  62. * @return void
  63. */
  64. public function testExecute()
  65. {
  66. $this->Task->params['paths'] = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages';
  67. $this->Task->params['output'] = $this->path . DS;
  68. $this->Task->params['extract-core'] = 'no';
  69. $this->Task->params['merge'] = '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->main();
  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 = '/\#: Template[\/\\\\]Pages[\/\\\\]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 = '/\#: Template[\/\\\\]Pages[\/\\\\]extract\.ctp:\d+\n';
  85. $pattern .= 'msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  86. $this->assertRegExp($pattern, $result);
  87. $pattern = '/\#: Template[\/\\\\]Pages[\/\\\\]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. $pattern = '/\#: Template[\/\\\\]Pages[\/\\\\]extract\.ctp:\d+\n';
  97. $pattern .= 'msgctxt "mail"\n';
  98. $pattern .= 'msgid "letter"/';
  99. $this->assertRegExp($pattern, $result);
  100. $pattern = '/\#: Template[\/\\\\]Pages[\/\\\\]extract\.ctp:\d+\n';
  101. $pattern .= 'msgctxt "alphabet"\n';
  102. $pattern .= 'msgid "letter"/';
  103. $this->assertRegExp($pattern, $result);
  104. // extract.ctp - reading the domain.pot
  105. $result = file_get_contents($this->path . DS . 'domain.pot');
  106. $pattern = '/msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  107. $this->assertNotRegExp($pattern, $result);
  108. $pattern = '/msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  109. $this->assertNotRegExp($pattern, $result);
  110. $pattern = '/msgid "You have %d new message \(domain\)."\nmsgid_plural "You have %d new messages \(domain\)."/';
  111. $this->assertRegExp($pattern, $result);
  112. $pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
  113. $this->assertRegExp($pattern, $result);
  114. }
  115. /**
  116. * testExecute with merging on method
  117. *
  118. * @return void
  119. */
  120. public function testExecuteMerge()
  121. {
  122. $this->Task->params['paths'] = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages';
  123. $this->Task->params['output'] = $this->path . DS;
  124. $this->Task->params['extract-core'] = 'no';
  125. $this->Task->params['merge'] = 'yes';
  126. $this->Task->expects($this->never())->method('err');
  127. $this->Task->expects($this->any())->method('in')
  128. ->will($this->returnValue('y'));
  129. $this->Task->expects($this->never())->method('_stop');
  130. $this->Task->main();
  131. $this->assertFileExists($this->path . DS . 'default.pot');
  132. $this->assertFileNotExists($this->path . DS . 'cake.pot');
  133. $this->assertFileNotExists($this->path . DS . 'domain.pot');
  134. }
  135. /**
  136. * test exclusions
  137. *
  138. * @return void
  139. */
  140. public function testExtractWithExclude()
  141. {
  142. $this->Task->interactive = false;
  143. $this->Task->params['paths'] = TEST_APP . 'TestApp/Template';
  144. $this->Task->params['output'] = $this->path . DS;
  145. $this->Task->params['exclude'] = 'Pages,Layout';
  146. $this->Task->params['extract-core'] = 'no';
  147. $this->Task->expects($this->any())->method('in')
  148. ->will($this->returnValue('y'));
  149. $this->Task->main();
  150. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  151. $result = file_get_contents($this->path . DS . 'default.pot');
  152. $pattern = '/\#: .*extract\.ctp:\d+\n/';
  153. $this->assertNotRegExp($pattern, $result);
  154. $pattern = '/\#: .*default\.ctp:\d+\n/';
  155. $this->assertNotRegExp($pattern, $result);
  156. }
  157. /**
  158. * testExtractWithoutLocations method
  159. *
  160. * @return void
  161. */
  162. public function testExtractWithoutLocations()
  163. {
  164. $this->Task->params['paths'] = TEST_APP . 'TestApp/Template';
  165. $this->Task->params['output'] = $this->path . DS;
  166. $this->Task->params['exclude'] = 'Pages,Layout';
  167. $this->Task->params['extract-core'] = 'no';
  168. $this->Task->params['no-location'] = true;
  169. $this->Task->expects($this->never())->method('err');
  170. $this->Task->expects($this->any())->method('in')
  171. ->will($this->returnValue('y'));
  172. $this->Task->main();
  173. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  174. $result = file_get_contents($this->path . DS . 'default.pot');
  175. $pattern = '/\n\#: .*\n/';
  176. $this->assertNotRegExp($pattern, $result);
  177. }
  178. /**
  179. * test extract can read more than one path.
  180. *
  181. * @return void
  182. */
  183. public function testExtractMultiplePaths()
  184. {
  185. $this->Task->interactive = false;
  186. $this->Task->params['paths'] =
  187. TEST_APP . 'TestApp/Template/Pages,' .
  188. TEST_APP . 'TestApp/Template/Posts';
  189. $this->Task->params['output'] = $this->path . DS;
  190. $this->Task->params['extract-core'] = 'no';
  191. $this->Task->expects($this->never())->method('err');
  192. $this->Task->expects($this->never())->method('_stop');
  193. $this->Task->main();
  194. $result = file_get_contents($this->path . DS . 'default.pot');
  195. $pattern = '/msgid "Add User"/';
  196. $this->assertRegExp($pattern, $result);
  197. }
  198. /**
  199. * Tests that it is possible to exclude plugin paths by enabling the param option for the ExtractTask
  200. *
  201. * @return void
  202. */
  203. public function testExtractExcludePlugins()
  204. {
  205. Configure::write('App.namespace', 'TestApp');
  206. $this->Task = $this->getMock(
  207. 'Cake\Shell\Task\ExtractTask',
  208. ['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'],
  209. [$this->io]
  210. );
  211. $this->Task->expects($this->exactly(1))
  212. ->method('_isExtractingApp')
  213. ->will($this->returnValue(true));
  214. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  215. $this->Task->params['output'] = $this->path . DS;
  216. $this->Task->params['exclude-plugins'] = true;
  217. $this->Task->main();
  218. $result = file_get_contents($this->path . DS . 'default.pot');
  219. $this->assertNotRegExp('#TestPlugin#', $result);
  220. }
  221. /**
  222. * Test that is possible to extract messages from a single plugin
  223. *
  224. * @return void
  225. */
  226. public function testExtractPlugin()
  227. {
  228. Configure::write('App.namespace', 'TestApp');
  229. $this->Task = $this->getMock(
  230. 'Cake\Shell\Task\ExtractTask',
  231. ['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'],
  232. [$this->io]
  233. );
  234. $this->Task->params['output'] = $this->path . DS;
  235. $this->Task->params['plugin'] = 'TestPlugin';
  236. $this->Task->main();
  237. $result = file_get_contents($this->path . DS . 'default.pot');
  238. $this->assertNotRegExp('#Pages#', $result);
  239. $this->assertRegExp('/translate\.ctp:\d+/', $result);
  240. $this->assertContains('This is a translatable string', $result);
  241. }
  242. /**
  243. * Test that is possible to extract messages from a vendored plugin.
  244. *
  245. * @return void
  246. */
  247. public function testExtractVendoredPlugin()
  248. {
  249. Configure::write('App.namespace', 'TestApp');
  250. $this->Task = $this->getMock(
  251. 'Cake\Shell\Task\ExtractTask',
  252. ['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'],
  253. [$this->io]
  254. );
  255. $this->Task->params['output'] = $this->path . DS;
  256. $this->Task->params['plugin'] = 'Company/TestPluginThree';
  257. $this->Task->main();
  258. $result = file_get_contents($this->path . DS . 'test_plugin_three.pot');
  259. $this->assertNotRegExp('#Pages#', $result);
  260. $this->assertRegExp('/default\.ctp:\d+/', $result);
  261. $this->assertContains('A vendor message', $result);
  262. }
  263. /**
  264. * Test that the extract shell overwrites existing files with the overwrite parameter
  265. *
  266. * @return void
  267. */
  268. public function testExtractOverwrite()
  269. {
  270. Configure::write('App.namespace', 'TestApp');
  271. $this->Task->interactive = false;
  272. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  273. $this->Task->params['output'] = $this->path . DS;
  274. $this->Task->params['extract-core'] = 'no';
  275. $this->Task->params['overwrite'] = true;
  276. file_put_contents($this->path . DS . 'default.pot', 'will be overwritten');
  277. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  278. $original = file_get_contents($this->path . DS . 'default.pot');
  279. $this->Task->main();
  280. $result = file_get_contents($this->path . DS . 'default.pot');
  281. $this->assertNotEquals($original, $result);
  282. }
  283. /**
  284. * Test that the extract shell scans the core libs
  285. *
  286. * @return void
  287. */
  288. public function testExtractCore()
  289. {
  290. Configure::write('App.namespace', 'TestApp');
  291. $this->Task->interactive = false;
  292. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  293. $this->Task->params['output'] = $this->path . DS;
  294. $this->Task->params['extract-core'] = 'yes';
  295. $this->Task->main();
  296. $this->assertTrue(file_exists($this->path . DS . 'cake.pot'));
  297. $result = file_get_contents($this->path . DS . 'cake.pot');
  298. $pattern = '/#: Console\/Templates\//';
  299. $this->assertNotRegExp($pattern, $result);
  300. $pattern = '/#: Test\//';
  301. $this->assertNotRegExp($pattern, $result);
  302. }
  303. }