ExtractTaskTest.php 12 KB

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