ExtractTaskTest.php 15 KB

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