ExtractTaskTest.php 15 KB

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