ExtractTaskTest.php 13 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->getMockBuilder('Cake\Console\ConsoleIo')
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. $progress = $this->getMockBuilder('Cake\Shell\Helper\ProgressHelper')
  38. ->setConstructorArgs([$this->io])
  39. ->getMock();
  40. $this->io->method('helper')
  41. ->will($this->returnValue($progress));
  42. $this->Task = $this->getMockBuilder('Cake\Shell\Task\ExtractTask')
  43. ->setMethods(['in', 'out', 'err', '_stop'])
  44. ->setConstructorArgs([$this->io])
  45. ->getMock();
  46. $this->path = TMP . 'tests/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. {
  56. parent::tearDown();
  57. unset($this->Task);
  58. $Folder = new Folder($this->path);
  59. $Folder->delete();
  60. Plugin::unload();
  61. }
  62. /**
  63. * testExecute method
  64. *
  65. * @return void
  66. */
  67. public function testExecute()
  68. {
  69. $this->Task->params['paths'] = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages';
  70. $this->Task->params['output'] = $this->path . DS;
  71. $this->Task->params['extract-core'] = 'no';
  72. $this->Task->params['merge'] = '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->main();
  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. // extract.ctp
  82. $pattern = '/\#: Template[\/\\\\]Pages[\/\\\\]extract\.ctp:\d+;\d+\n';
  83. $pattern .= 'msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  84. $this->assertRegExp($pattern, $result);
  85. $pattern = '/msgid "You have %d new message."\nmsgstr ""/';
  86. $this->assertNotRegExp($pattern, $result, 'No duplicate msgid');
  87. $pattern = '/\#: Template[\/\\\\]Pages[\/\\\\]extract\.ctp:\d+\n';
  88. $pattern .= 'msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  89. $this->assertRegExp($pattern, $result);
  90. $pattern = '/\#: Template[\/\\\\]Pages[\/\\\\]extract\.ctp:\d+\nmsgid "';
  91. $pattern .= 'Hot features!';
  92. $pattern .= '\\\n - No Configuration: Set-up the database and let the magic begin';
  93. $pattern .= '\\\n - Extremely Simple: Just look at the name...It\'s Cake';
  94. $pattern .= '\\\n - Active, Friendly Community: Join us #cakephp on IRC. We\'d love to help you get started';
  95. $pattern .= '"\nmsgstr ""/';
  96. $this->assertRegExp($pattern, $result);
  97. $this->assertContains('msgid "double \\"quoted\\""', $result, 'Strings with quotes not handled correctly');
  98. $this->assertContains("msgid \"single 'quoted'\"", $result, 'Strings with quotes not handled correctly');
  99. $pattern = '/\#: Template[\/\\\\]Pages[\/\\\\]extract\.ctp:\d+\n';
  100. $pattern .= 'msgctxt "mail"\n';
  101. $pattern .= 'msgid "letter"/';
  102. $this->assertRegExp($pattern, $result);
  103. $pattern = '/\#: Template[\/\\\\]Pages[\/\\\\]extract\.ctp:\d+\n';
  104. $pattern .= 'msgctxt "alphabet"\n';
  105. $pattern .= 'msgid "letter"/';
  106. $this->assertRegExp($pattern, $result);
  107. // extract.ctp - reading the domain.pot
  108. $result = file_get_contents($this->path . DS . 'domain.pot');
  109. $pattern = '/msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  110. $this->assertNotRegExp($pattern, $result);
  111. $pattern = '/msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  112. $this->assertNotRegExp($pattern, $result);
  113. $pattern = '/msgid "You have %d new message \(domain\)."\nmsgid_plural "You have %d new messages \(domain\)."/';
  114. $this->assertRegExp($pattern, $result);
  115. $pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
  116. $this->assertRegExp($pattern, $result);
  117. }
  118. /**
  119. * testExecute with merging on method
  120. *
  121. * @return void
  122. */
  123. public function testExecuteMerge()
  124. {
  125. $this->Task->params['paths'] = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages';
  126. $this->Task->params['output'] = $this->path . DS;
  127. $this->Task->params['extract-core'] = 'no';
  128. $this->Task->params['merge'] = 'yes';
  129. $this->Task->expects($this->never())->method('err');
  130. $this->Task->expects($this->any())->method('in')
  131. ->will($this->returnValue('y'));
  132. $this->Task->expects($this->never())->method('_stop');
  133. $this->Task->main();
  134. $this->assertFileExists($this->path . DS . 'default.pot');
  135. $this->assertFileNotExists($this->path . DS . 'cake.pot');
  136. $this->assertFileNotExists($this->path . DS . 'domain.pot');
  137. }
  138. /**
  139. * test exclusions
  140. *
  141. * @return void
  142. */
  143. public function testExtractWithExclude()
  144. {
  145. $this->Task->interactive = false;
  146. $this->Task->params['paths'] = TEST_APP . 'TestApp/Template';
  147. $this->Task->params['output'] = $this->path . DS;
  148. $this->Task->params['exclude'] = 'Pages,Layout';
  149. $this->Task->params['extract-core'] = 'no';
  150. $this->Task->expects($this->any())->method('in')
  151. ->will($this->returnValue('y'));
  152. $this->Task->main();
  153. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  154. $result = file_get_contents($this->path . DS . 'default.pot');
  155. $pattern = '/\#: .*extract\.ctp:\d+\n/';
  156. $this->assertNotRegExp($pattern, $result);
  157. $pattern = '/\#: .*default\.ctp:\d+\n/';
  158. $this->assertNotRegExp($pattern, $result);
  159. }
  160. /**
  161. * testExtractWithoutLocations method
  162. *
  163. * @return void
  164. */
  165. public function testExtractWithoutLocations()
  166. {
  167. $this->Task->params['paths'] = TEST_APP . 'TestApp/Template';
  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->params['no-location'] = true;
  172. $this->Task->expects($this->never())->method('err');
  173. $this->Task->expects($this->any())->method('in')
  174. ->will($this->returnValue('y'));
  175. $this->Task->main();
  176. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  177. $result = file_get_contents($this->path . DS . 'default.pot');
  178. $pattern = '/\n\#: .*\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. {
  188. $this->Task->interactive = false;
  189. $this->Task->params['paths'] =
  190. TEST_APP . 'TestApp/Template/Pages,' .
  191. TEST_APP . 'TestApp/Template/Posts';
  192. $this->Task->params['output'] = $this->path . DS;
  193. $this->Task->params['extract-core'] = 'no';
  194. $this->Task->expects($this->never())->method('err');
  195. $this->Task->expects($this->never())->method('_stop');
  196. $this->Task->main();
  197. $result = file_get_contents($this->path . DS . 'default.pot');
  198. $pattern = '/msgid "Add User"/';
  199. $this->assertRegExp($pattern, $result);
  200. }
  201. /**
  202. * Tests that it is possible to exclude plugin paths by enabling the param option for the ExtractTask
  203. *
  204. * @return void
  205. */
  206. public function testExtractExcludePlugins()
  207. {
  208. Configure::write('App.namespace', 'TestApp');
  209. $this->Task = $this->getMockBuilder('Cake\Shell\Task\ExtractTask')
  210. ->setMethods(['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'])
  211. ->setConstructorArgs([$this->io])
  212. ->getMock();
  213. $this->Task->expects($this->exactly(1))
  214. ->method('_isExtractingApp')
  215. ->will($this->returnValue(true));
  216. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  217. $this->Task->params['output'] = $this->path . DS;
  218. $this->Task->params['exclude-plugins'] = true;
  219. $this->Task->main();
  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 from a single plugin
  225. *
  226. * @return void
  227. */
  228. public function testExtractPlugin()
  229. {
  230. Configure::write('App.namespace', 'TestApp');
  231. $this->Task = $this->getMockBuilder('Cake\Shell\Task\ExtractTask')
  232. ->setMethods(['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'])
  233. ->setConstructorArgs([$this->io])
  234. ->getMock();
  235. $this->Task->params['output'] = $this->path . DS;
  236. $this->Task->params['plugin'] = 'TestPlugin';
  237. $this->Task->main();
  238. $result = file_get_contents($this->path . DS . 'default.pot');
  239. $this->assertNotRegExp('#Pages#', $result);
  240. $this->assertRegExp('/translate\.ctp:\d+/', $result);
  241. $this->assertContains('This is a translatable string', $result);
  242. }
  243. /**
  244. * Test that is possible to extract messages from a vendored plugin.
  245. *
  246. * @return void
  247. */
  248. public function testExtractVendoredPlugin()
  249. {
  250. Configure::write('App.namespace', 'TestApp');
  251. $this->Task = $this->getMockBuilder('Cake\Shell\Task\ExtractTask')
  252. ->setMethods(['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'])
  253. ->setConstructorArgs([$this->io])
  254. ->getMock();
  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. }