ExtractTaskTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /**
  3. * CakePHP : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Shell\Task;
  16. use Cake\Core\Plugin;
  17. use Cake\Filesystem\Folder;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * ExtractTaskTest class
  21. */
  22. class ExtractTaskTest extends TestCase
  23. {
  24. /**
  25. * setUp method
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. $this->io = $this->getMockBuilder('Cake\Console\ConsoleIo')
  33. ->disableOriginalConstructor()
  34. ->getMock();
  35. $progress = $this->getMockBuilder('Cake\Shell\Helper\ProgressHelper')
  36. ->setConstructorArgs([$this->io])
  37. ->getMock();
  38. $this->io->method('helper')
  39. ->will($this->returnValue($progress));
  40. $this->Task = $this->getMockBuilder('Cake\Shell\Task\ExtractTask')
  41. ->setMethods(['in', 'out', 'err', '_stop'])
  42. ->setConstructorArgs([$this->io])
  43. ->getMock();
  44. $this->path = TMP . 'tests/extract_task_test';
  45. new Folder($this->path . DS . 'locale', true);
  46. }
  47. /**
  48. * tearDown method
  49. *
  50. * @return void
  51. */
  52. public function tearDown()
  53. {
  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. {
  67. $this->Task->params['paths'] = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages';
  68. $this->Task->params['output'] = $this->path . DS;
  69. $this->Task->params['extract-core'] = 'no';
  70. $this->Task->params['merge'] = 'no';
  71. $this->Task->expects($this->never())->method('err');
  72. $this->Task->expects($this->any())->method('in')
  73. ->will($this->returnValue('y'));
  74. $this->Task->expects($this->never())->method('_stop');
  75. $this->Task->main();
  76. $this->assertFileExists($this->path . DS . 'default.pot');
  77. $result = file_get_contents($this->path . DS . 'default.pot');
  78. $this->assertFileNotExists($this->path . DS . 'cake.pot');
  79. // extract.ctp
  80. $pattern = '/\#: Template[\/\\\\]Pages[\/\\\\]extract\.ctp:\d+;\d+\n';
  81. $pattern .= 'msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  82. $this->assertRegExp($pattern, $result);
  83. $pattern = '/msgid "You have %d new message."\nmsgstr ""/';
  84. $this->assertNotRegExp($pattern, $result, 'No duplicate msgid');
  85. $pattern = '/\#: Template[\/\\\\]Pages[\/\\\\]extract\.ctp:\d+\n';
  86. $pattern .= 'msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  87. $this->assertRegExp($pattern, $result);
  88. $pattern = '/\#: Template[\/\\\\]Pages[\/\\\\]extract\.ctp:\d+\nmsgid "';
  89. $pattern .= 'Hot features!';
  90. $pattern .= '\\\n - No Configuration: Set-up the database and let the magic begin';
  91. $pattern .= '\\\n - Extremely Simple: Just look at the name...It\'s Cake';
  92. $pattern .= '\\\n - Active, Friendly Community: Join us #cakephp on IRC. We\'d love to help you get started';
  93. $pattern .= '"\nmsgstr ""/';
  94. $this->assertRegExp($pattern, $result);
  95. $this->assertContains('msgid "double \\"quoted\\""', $result, 'Strings with quotes not handled correctly');
  96. $this->assertContains("msgid \"single 'quoted'\"", $result, 'Strings with quotes not handled correctly');
  97. $pattern = '/\#: Template[\/\\\\]Pages[\/\\\\]extract\.ctp:\d+\n';
  98. $pattern .= 'msgctxt "mail"\n';
  99. $pattern .= 'msgid "letter"/';
  100. $this->assertRegExp($pattern, $result);
  101. $pattern = '/\#: Template[\/\\\\]Pages[\/\\\\]extract\.ctp:\d+\n';
  102. $pattern .= 'msgctxt "alphabet"\n';
  103. $pattern .= 'msgid "letter"/';
  104. $this->assertRegExp($pattern, $result);
  105. // extract.ctp - reading the domain.pot
  106. $result = file_get_contents($this->path . DS . 'domain.pot');
  107. $pattern = '/msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  108. $this->assertNotRegExp($pattern, $result);
  109. $pattern = '/msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  110. $this->assertNotRegExp($pattern, $result);
  111. $pattern = '/msgid "You have %d new message \(domain\)."\nmsgid_plural "You have %d new messages \(domain\)."/';
  112. $this->assertRegExp($pattern, $result);
  113. $pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
  114. $this->assertRegExp($pattern, $result);
  115. }
  116. /**
  117. * testExecute with merging on method
  118. *
  119. * @return void
  120. */
  121. public function testExecuteMerge()
  122. {
  123. $this->Task->params['paths'] = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages';
  124. $this->Task->params['output'] = $this->path . DS;
  125. $this->Task->params['extract-core'] = 'no';
  126. $this->Task->params['merge'] = 'yes';
  127. $this->Task->expects($this->never())->method('err');
  128. $this->Task->expects($this->any())->method('in')
  129. ->will($this->returnValue('y'));
  130. $this->Task->expects($this->never())->method('_stop');
  131. $this->Task->main();
  132. $this->assertFileExists($this->path . DS . 'default.pot');
  133. $this->assertFileNotExists($this->path . DS . 'cake.pot');
  134. $this->assertFileNotExists($this->path . DS . 'domain.pot');
  135. }
  136. /**
  137. * test exclusions
  138. *
  139. * @return void
  140. */
  141. public function testExtractWithExclude()
  142. {
  143. $this->Task->interactive = false;
  144. $this->Task->params['paths'] = TEST_APP . 'TestApp/Template';
  145. $this->Task->params['output'] = $this->path . DS;
  146. $this->Task->params['exclude'] = 'Pages,Layout';
  147. $this->Task->params['extract-core'] = 'no';
  148. $this->Task->expects($this->any())->method('in')
  149. ->will($this->returnValue('y'));
  150. $this->Task->main();
  151. $this->assertFileExists($this->path . DS . 'default.pot');
  152. $result = file_get_contents($this->path . DS . 'default.pot');
  153. $pattern = '/\#: .*extract\.ctp:\d+\n/';
  154. $this->assertNotRegExp($pattern, $result);
  155. $pattern = '/\#: .*default\.ctp:\d+\n/';
  156. $this->assertNotRegExp($pattern, $result);
  157. }
  158. /**
  159. * testExtractWithoutLocations method
  160. *
  161. * @return void
  162. */
  163. public function testExtractWithoutLocations()
  164. {
  165. $this->Task->params['paths'] = TEST_APP . 'TestApp/Template';
  166. $this->Task->params['output'] = $this->path . DS;
  167. $this->Task->params['exclude'] = 'Pages,Layout';
  168. $this->Task->params['extract-core'] = 'no';
  169. $this->Task->params['no-location'] = true;
  170. $this->Task->expects($this->never())->method('err');
  171. $this->Task->expects($this->any())->method('in')
  172. ->will($this->returnValue('y'));
  173. $this->Task->main();
  174. $this->assertFileExists($this->path . DS . 'default.pot');
  175. $result = file_get_contents($this->path . DS . 'default.pot');
  176. $pattern = '/\n\#: .*\n/';
  177. $this->assertNotRegExp($pattern, $result);
  178. }
  179. /**
  180. * test extract can read more than one path.
  181. *
  182. * @return void
  183. */
  184. public function testExtractMultiplePaths()
  185. {
  186. $this->Task->interactive = false;
  187. $this->Task->params['paths'] =
  188. TEST_APP . 'TestApp/Template/Pages,' .
  189. TEST_APP . 'TestApp/Template/Posts';
  190. $this->Task->params['output'] = $this->path . DS;
  191. $this->Task->params['extract-core'] = 'no';
  192. $this->Task->expects($this->never())->method('err');
  193. $this->Task->expects($this->never())->method('_stop');
  194. $this->Task->main();
  195. $result = file_get_contents($this->path . DS . 'default.pot');
  196. $pattern = '/msgid "Add User"/';
  197. $this->assertRegExp($pattern, $result);
  198. }
  199. /**
  200. * Tests that it is possible to exclude plugin paths by enabling the param option for the ExtractTask
  201. *
  202. * @return void
  203. */
  204. public function testExtractExcludePlugins()
  205. {
  206. static::setAppNamespace();
  207. $this->Task = $this->getMockBuilder('Cake\Shell\Task\ExtractTask')
  208. ->setMethods(['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'])
  209. ->setConstructorArgs([$this->io])
  210. ->getMock();
  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. static::setAppNamespace();
  229. $this->Task = $this->getMockBuilder('Cake\Shell\Task\ExtractTask')
  230. ->setMethods(['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'])
  231. ->setConstructorArgs([$this->io])
  232. ->getMock();
  233. $this->Task->params['output'] = $this->path . DS;
  234. $this->Task->params['plugin'] = 'TestPlugin';
  235. $this->Task->main();
  236. $result = file_get_contents($this->path . DS . 'default.pot');
  237. $this->assertNotRegExp('#Pages#', $result);
  238. $this->assertRegExp('/translate\.ctp:\d+/', $result);
  239. $this->assertContains('This is a translatable string', $result);
  240. }
  241. /**
  242. * Test that is possible to extract messages from a vendored plugin.
  243. *
  244. * @return void
  245. */
  246. public function testExtractVendoredPlugin()
  247. {
  248. static::setAppNamespace();
  249. $this->Task = $this->getMockBuilder('Cake\Shell\Task\ExtractTask')
  250. ->setMethods(['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'])
  251. ->setConstructorArgs([$this->io])
  252. ->getMock();
  253. $this->Task->params['output'] = $this->path . DS;
  254. $this->Task->params['plugin'] = 'Company/TestPluginThree';
  255. $this->Task->main();
  256. $result = file_get_contents($this->path . DS . 'test_plugin_three.pot');
  257. $this->assertNotRegExp('#Pages#', $result);
  258. $this->assertRegExp('/default\.ctp:\d+/', $result);
  259. $this->assertContains('A vendor message', $result);
  260. }
  261. /**
  262. * Test that the extract shell overwrites existing files with the overwrite parameter
  263. *
  264. * @return void
  265. */
  266. public function testExtractOverwrite()
  267. {
  268. static::setAppNamespace();
  269. $this->Task->interactive = false;
  270. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  271. $this->Task->params['output'] = $this->path . DS;
  272. $this->Task->params['extract-core'] = 'no';
  273. $this->Task->params['overwrite'] = true;
  274. file_put_contents($this->path . DS . 'default.pot', 'will be overwritten');
  275. $this->assertFileExists($this->path . DS . 'default.pot');
  276. $original = file_get_contents($this->path . DS . 'default.pot');
  277. $this->Task->main();
  278. $result = file_get_contents($this->path . DS . 'default.pot');
  279. $this->assertNotEquals($original, $result);
  280. }
  281. /**
  282. * Test that the extract shell scans the core libs
  283. *
  284. * @return void
  285. */
  286. public function testExtractCore()
  287. {
  288. static::setAppNamespace();
  289. $this->Task->interactive = false;
  290. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  291. $this->Task->params['output'] = $this->path . DS;
  292. $this->Task->params['extract-core'] = 'yes';
  293. $this->Task->main();
  294. $this->assertFileExists($this->path . DS . 'cake.pot');
  295. $result = file_get_contents($this->path . DS . 'cake.pot');
  296. $pattern = '/#: Console\/Templates\//';
  297. $this->assertNotRegExp($pattern, $result);
  298. $pattern = '/#: Test\//';
  299. $this->assertNotRegExp($pattern, $result);
  300. }
  301. }