I18nExtractCommandTest.php 12 KB

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