ExtractTaskTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Shell\Task;
  17. use Cake\Filesystem\Folder;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * ExtractTaskTest class
  21. *
  22. * @property \Cake\Shell\Task\ExtractTask|MockObject $Task
  23. * @property \Cake\Console\ConsoleIo|MockObject $io
  24. * @property string $path
  25. */
  26. class ExtractTaskTest extends TestCase
  27. {
  28. /**
  29. * setUp method
  30. *
  31. * @return void
  32. */
  33. public function setUp()
  34. {
  35. parent::setUp();
  36. $this->io = $this->getMockBuilder('Cake\Console\ConsoleIo')
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $progress = $this->getMockBuilder('Cake\Shell\Helper\ProgressHelper')
  40. ->setConstructorArgs([$this->io])
  41. ->getMock();
  42. $this->io->method('helper')
  43. ->will($this->returnValue($progress));
  44. $this->Task = $this->getMockBuilder('Cake\Shell\Task\ExtractTask')
  45. ->setMethods(['in', 'out', 'err', '_stop'])
  46. ->setConstructorArgs([$this->io])
  47. ->getMock();
  48. $this->path = TMP . 'tests/extract_task_test';
  49. new Folder($this->path . DS . 'locale', true);
  50. }
  51. /**
  52. * tearDown method
  53. *
  54. * @return void
  55. */
  56. public function tearDown()
  57. {
  58. parent::tearDown();
  59. unset($this->Task);
  60. $Folder = new Folder($this->path);
  61. $Folder->delete();
  62. $this->clearPlugins();
  63. }
  64. /**
  65. * testExecute method
  66. *
  67. * @return void
  68. */
  69. public function testExecute()
  70. {
  71. $this->Task->params['paths'] = TEST_APP . 'templates' . DS . 'Pages';
  72. $this->Task->params['output'] = $this->path . DS;
  73. $this->Task->params['extract-core'] = 'no';
  74. $this->Task->params['merge'] = 'no';
  75. $this->Task->expects($this->never())->method('err');
  76. $this->Task->expects($this->any())->method('in')
  77. ->will($this->returnValue('y'));
  78. $this->Task->expects($this->never())->method('_stop');
  79. $this->Task->main();
  80. $this->assertFileExists($this->path . DS . 'default.pot');
  81. $result = file_get_contents($this->path . DS . 'default.pot');
  82. $this->assertFileNotExists($this->path . DS . 'cake.pot');
  83. // extract.php
  84. $pattern = '/\#: [\/\\\\]extract\.php:\d+;\d+\n';
  85. $pattern .= 'msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  86. $this->assertRegExp($pattern, $result);
  87. $pattern = '/msgid "You have %d new message."\nmsgstr ""/';
  88. $this->assertNotRegExp($pattern, $result, 'No duplicate msgid');
  89. $pattern = '/\#: [\/\\\\]extract\.php:\d+\n';
  90. $pattern .= 'msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  91. $this->assertRegExp($pattern, $result);
  92. $pattern = '/\#: [\/\\\\]extract\.php:\d+\nmsgid "';
  93. $pattern .= 'Hot features!';
  94. $pattern .= '\\\n - No Configuration: Set-up the database and let the magic begin';
  95. $pattern .= '\\\n - Extremely Simple: Just look at the name...It\'s Cake';
  96. $pattern .= '\\\n - Active, Friendly Community: Join us #cakephp on IRC. We\'d love to help you get started';
  97. $pattern .= '"\nmsgstr ""/';
  98. $this->assertRegExp($pattern, $result);
  99. $this->assertContains('msgid "double \\"quoted\\""', $result, 'Strings with quotes not handled correctly');
  100. $this->assertContains("msgid \"single 'quoted'\"", $result, 'Strings with quotes not handled correctly');
  101. $pattern = '/\#: [\/\\\\]extract\.php:\d+\n';
  102. $pattern .= 'msgctxt "mail"\n';
  103. $pattern .= 'msgid "letter"/';
  104. $this->assertRegExp($pattern, $result);
  105. $pattern = '/\#: [\/\\\\]extract\.php:\d+\n';
  106. $pattern .= 'msgctxt "alphabet"\n';
  107. $pattern .= 'msgid "letter"/';
  108. $this->assertRegExp($pattern, $result);
  109. // extract.php - reading the domain.pot
  110. $result = file_get_contents($this->path . DS . 'domain.pot');
  111. $pattern = '/msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  112. $this->assertNotRegExp($pattern, $result);
  113. $pattern = '/msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  114. $this->assertNotRegExp($pattern, $result);
  115. $pattern = '/msgid "You have %d new message \(domain\)."\nmsgid_plural "You have %d new messages \(domain\)."/';
  116. $this->assertRegExp($pattern, $result);
  117. $pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
  118. $this->assertRegExp($pattern, $result);
  119. }
  120. /**
  121. * testExecute with merging on method
  122. *
  123. * @return void
  124. */
  125. public function testExecuteMerge()
  126. {
  127. $this->Task->params['paths'] = TEST_APP . 'templates' . DS . 'Pages';
  128. $this->Task->params['output'] = $this->path . DS;
  129. $this->Task->params['extract-core'] = 'no';
  130. $this->Task->params['merge'] = 'yes';
  131. $this->Task->expects($this->never())->method('err');
  132. $this->Task->expects($this->any())->method('in')
  133. ->will($this->returnValue('y'));
  134. $this->Task->expects($this->never())->method('_stop');
  135. $this->Task->main();
  136. $this->assertFileExists($this->path . DS . 'default.pot');
  137. $this->assertFileNotExists($this->path . DS . 'cake.pot');
  138. $this->assertFileNotExists($this->path . DS . 'domain.pot');
  139. }
  140. /**
  141. * test exclusions
  142. *
  143. * @return void
  144. */
  145. public function testExtractWithExclude()
  146. {
  147. $this->Task->interactive = false;
  148. $this->Task->params['paths'] = TEST_APP . 'templates';
  149. $this->Task->params['output'] = $this->path . DS;
  150. $this->Task->params['exclude'] = 'Pages,Layout';
  151. $this->Task->params['extract-core'] = 'no';
  152. $this->Task->expects($this->any())->method('in')
  153. ->will($this->returnValue('y'));
  154. $this->Task->main();
  155. $this->assertFileExists($this->path . DS . 'default.pot');
  156. $result = file_get_contents($this->path . DS . 'default.pot');
  157. $pattern = '/\#: .*extract\.php:\d+\n/';
  158. $this->assertNotRegExp($pattern, $result);
  159. $pattern = '/\#: .*default\.php:\d+\n/';
  160. $this->assertNotRegExp($pattern, $result);
  161. }
  162. /**
  163. * testExtractWithoutLocations method
  164. *
  165. * @return void
  166. */
  167. public function testExtractWithoutLocations()
  168. {
  169. $this->Task->params['paths'] = TEST_APP . 'templates';
  170. $this->Task->params['output'] = $this->path . DS;
  171. $this->Task->params['exclude'] = 'Pages,Layout';
  172. $this->Task->params['extract-core'] = 'no';
  173. $this->Task->params['no-location'] = true;
  174. $this->Task->expects($this->never())->method('err');
  175. $this->Task->expects($this->any())->method('in')
  176. ->will($this->returnValue('y'));
  177. $this->Task->main();
  178. $this->assertFileExists($this->path . DS . 'default.pot');
  179. $result = file_get_contents($this->path . DS . 'default.pot');
  180. $pattern = '/\n\#: .*\n/';
  181. $this->assertNotRegExp($pattern, $result);
  182. }
  183. /**
  184. * test extract can read more than one path.
  185. *
  186. * @return void
  187. */
  188. public function testExtractMultiplePaths()
  189. {
  190. $this->Task->interactive = false;
  191. $this->Task->params['paths'] =
  192. TEST_APP . 'templates/Pages,' .
  193. TEST_APP . 'templates/Posts';
  194. $this->Task->params['output'] = $this->path . DS;
  195. $this->Task->params['extract-core'] = 'no';
  196. $this->Task->expects($this->never())->method('err');
  197. $this->Task->expects($this->never())->method('_stop');
  198. $this->Task->main();
  199. $result = file_get_contents($this->path . DS . 'default.pot');
  200. $pattern = '/msgid "Add User"/';
  201. $this->assertRegExp($pattern, $result);
  202. }
  203. /**
  204. * Tests that it is possible to exclude plugin paths by enabling the param option for the ExtractTask
  205. *
  206. * @return void
  207. */
  208. public function testExtractExcludePlugins()
  209. {
  210. static::setAppNamespace();
  211. $this->Task = $this->getMockBuilder('Cake\Shell\Task\ExtractTask')
  212. ->setMethods(['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'])
  213. ->setConstructorArgs([$this->io])
  214. ->getMock();
  215. $this->Task->expects($this->exactly(1))
  216. ->method('_isExtractingApp')
  217. ->will($this->returnValue(true));
  218. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  219. $this->Task->params['output'] = $this->path . DS;
  220. $this->Task->params['exclude-plugins'] = true;
  221. $this->Task->main();
  222. $result = file_get_contents($this->path . DS . 'default.pot');
  223. $this->assertNotRegExp('#TestPlugin#', $result);
  224. }
  225. /**
  226. * Test that is possible to extract messages from a single plugin
  227. *
  228. * @return void
  229. */
  230. public function testExtractPlugin()
  231. {
  232. static::setAppNamespace();
  233. $this->loadPlugins(['TestPlugin']);
  234. $this->Task = $this->getMockBuilder('Cake\Shell\Task\ExtractTask')
  235. ->setMethods(['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'])
  236. ->setConstructorArgs([$this->io])
  237. ->getMock();
  238. $this->Task->params['output'] = $this->path . DS;
  239. $this->Task->params['plugin'] = 'TestPlugin';
  240. $this->Task->main();
  241. $result = file_get_contents($this->path . DS . 'default.pot');
  242. $this->assertNotRegExp('#Pages#', $result);
  243. $this->assertRegExp('/translate\.php:\d+/', $result);
  244. $this->assertContains('This is a translatable string', $result);
  245. }
  246. /**
  247. * Test that is possible to extract messages from a vendored plugin.
  248. *
  249. * @return void
  250. */
  251. public function testExtractVendoredPlugin()
  252. {
  253. static::setAppNamespace();
  254. $this->loadPlugins(['Company/TestPluginThree']);
  255. $this->Task = $this->getMockBuilder('Cake\Shell\Task\ExtractTask')
  256. ->setMethods(['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'])
  257. ->setConstructorArgs([$this->io])
  258. ->getMock();
  259. $this->Task->params['output'] = $this->path . DS;
  260. $this->Task->params['plugin'] = 'Company/TestPluginThree';
  261. $this->Task->main();
  262. $result = file_get_contents($this->path . DS . 'test_plugin_three.pot');
  263. $this->assertNotRegExp('#Pages#', $result);
  264. $this->assertRegExp('/default\.php:\d+/', $result);
  265. $this->assertContains('A vendor message', $result);
  266. }
  267. /**
  268. * Test that the extract shell overwrites existing files with the overwrite parameter
  269. *
  270. * @return void
  271. */
  272. public function testExtractOverwrite()
  273. {
  274. static::setAppNamespace();
  275. $this->Task->interactive = false;
  276. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  277. $this->Task->params['output'] = $this->path . DS;
  278. $this->Task->params['extract-core'] = 'no';
  279. $this->Task->params['overwrite'] = true;
  280. file_put_contents($this->path . DS . 'default.pot', 'will be overwritten');
  281. $this->assertFileExists($this->path . DS . 'default.pot');
  282. $original = file_get_contents($this->path . DS . 'default.pot');
  283. $this->Task->main();
  284. $result = file_get_contents($this->path . DS . 'default.pot');
  285. $this->assertNotEquals($original, $result);
  286. }
  287. /**
  288. * Test that the extract shell scans the core libs
  289. *
  290. * @return void
  291. */
  292. public function testExtractCore()
  293. {
  294. static::setAppNamespace();
  295. $this->Task->interactive = false;
  296. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  297. $this->Task->params['output'] = $this->path . DS;
  298. $this->Task->params['extract-core'] = 'yes';
  299. $this->Task->main();
  300. $this->assertFileExists($this->path . DS . 'cake.pot');
  301. $result = file_get_contents($this->path . DS . 'cake.pot');
  302. $pattern = '/#: Console\/Templates\//';
  303. $this->assertNotRegExp($pattern, $result);
  304. $pattern = '/#: Test\//';
  305. $this->assertNotRegExp($pattern, $result);
  306. }
  307. /**
  308. * Test when marker-error option is set
  309. * When marker-error is unset, it's already test
  310. * with other functions like testExecute that not detects error because err never called
  311. */
  312. public function testMarkerErrorSets()
  313. {
  314. $this->Task->method('err')
  315. ->will($this->returnCallback([$this, 'echoTest']));
  316. $this->Task->params['paths'] = TEST_APP . 'templates' . DS . 'Pages';
  317. $this->Task->params['output'] = $this->path . DS;
  318. $this->Task->params['extract-core'] = 'no';
  319. $this->Task->params['merge'] = 'no';
  320. $this->Task->params['marker-error'] = true;
  321. $this->expectOutputRegex('/.*Invalid marker content in .*extract\.php.*/');
  322. $this->Task->main();
  323. }
  324. /**
  325. * A useful function to mock/replace err or out function that allows to use expectOutput
  326. * @param string $val
  327. * @param int $nbLines
  328. */
  329. public function echoTest($val = '', $nbLines = 1)
  330. {
  331. echo $val . str_repeat(PHP_EOL, $nbLines);
  332. }
  333. /**
  334. * test relative-paths option
  335. *
  336. * @return void
  337. */
  338. public function testExtractWithRelativePaths()
  339. {
  340. $this->Task->interactive = false;
  341. $this->Task->params['paths'] = TEST_APP . 'templates';
  342. $this->Task->params['output'] = $this->path . DS;
  343. $this->Task->params['extract-core'] = 'no';
  344. $this->Task->params['relative-paths'] = true;
  345. $this->Task->method('in')
  346. ->will($this->returnValue('y'));
  347. $this->Task->main();
  348. $this->assertFileExists($this->path . DS . 'default.pot');
  349. $result = file_get_contents($this->path . DS . 'default.pot');
  350. $expected = '#: ./tests/test_app/templates/Pages/extract.php:';
  351. $this->assertContains($expected, $result);
  352. }
  353. }