I18nExtractCommandTest.php 13 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\Console\TestSuite\ConsoleIntegrationTestTrait;
  18. use Cake\Core\Configure;
  19. use Cake\Filesystem\Filesystem;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * I18nExtractCommandTest
  23. */
  24. class I18nExtractCommandTest extends TestCase
  25. {
  26. use ConsoleIntegrationTestTrait;
  27. /**
  28. * @var string
  29. */
  30. protected $path;
  31. /**
  32. * setUp method
  33. */
  34. public function setUp(): void
  35. {
  36. parent::setUp();
  37. $this->useCommandRunner();
  38. $this->setAppNamespace();
  39. $this->path = TMP . 'tests/extract_task_test';
  40. $fs = new Filesystem();
  41. $fs->deleteDir($this->path);
  42. $fs->mkdir($this->path . DS . 'locale');
  43. }
  44. /**
  45. * tearDown method
  46. */
  47. public function tearDown(): void
  48. {
  49. parent::tearDown();
  50. $fs = new Filesystem();
  51. $fs->deleteDir($this->path);
  52. $this->clearPlugins();
  53. }
  54. /**
  55. * testExecute method
  56. */
  57. public function testExecute(): void
  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->assertFileDoesNotExist($this->path . DS . 'cake.pot');
  70. // The additional "./tests/test_app" is just due to the wonky folder structure of the test app.
  71. // In a regular app the path would start with "./templates".
  72. $pattern = '@\#: \./tests/test_app/templates/Pages/extract\.php:\d+\n';
  73. $pattern .= '\#: \./tests/test_app/templates/Pages/extract\.php:\d+\n';
  74. $pattern .= 'msgid "You have %d new message."\nmsgid_plural "You have %d new messages."@';
  75. $this->assertMatchesRegularExpression($pattern, $result);
  76. $pattern = '/msgid "You have %d new message."\nmsgstr ""/';
  77. $this->assertDoesNotMatchRegularExpression($pattern, $result, 'No duplicate msgid');
  78. $pattern = '@\#: \./tests/test_app/templates/Pages/extract\.php:\d+\n';
  79. $pattern .= 'msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."@';
  80. $this->assertMatchesRegularExpression($pattern, $result);
  81. $pattern = '@\#: \./tests/test_app/templates/Pages/extract\.php:\d+\nmsgid "';
  82. $pattern .= 'Hot features!';
  83. $pattern .= '\\\n - No Configuration: Set-up the database and let the magic begin';
  84. $pattern .= '\\\n - Extremely Simple: Just look at the name...It\'s Cake';
  85. $pattern .= '\\\n - Active, Friendly Community: Join us #cakephp on IRC. We\'d love to help you get started';
  86. $pattern .= '"\nmsgstr ""@';
  87. $this->assertMatchesRegularExpression($pattern, $result);
  88. $this->assertStringContainsString('msgid "double \\"quoted\\""', $result, 'Strings with quotes not handled correctly');
  89. $this->assertStringContainsString("msgid \"single 'quoted'\"", $result, 'Strings with quotes not handled correctly');
  90. $pattern = '@\#: \./tests/test_app/templates/Pages/extract\.php:\d+\n';
  91. $pattern .= 'msgctxt "mail"\n';
  92. $pattern .= 'msgid "letter"@';
  93. $this->assertMatchesRegularExpression($pattern, $result);
  94. $pattern = '@\#: \./tests/test_app/templates/Pages/extract\.php:\d+\n';
  95. $pattern .= 'msgctxt "alphabet"\n';
  96. $pattern .= 'msgid "letter"@';
  97. $this->assertMatchesRegularExpression($pattern, $result);
  98. // extract.php - reading the domain.pot
  99. $result = file_get_contents($this->path . DS . 'domain.pot');
  100. $pattern = '/msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  101. $this->assertDoesNotMatchRegularExpression($pattern, $result);
  102. $pattern = '/msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  103. $this->assertDoesNotMatchRegularExpression($pattern, $result);
  104. $pattern = '/msgid "You have %d new message \(domain\)."\nmsgid_plural "You have %d new messages \(domain\)."/';
  105. $this->assertMatchesRegularExpression($pattern, $result);
  106. $pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
  107. $this->assertMatchesRegularExpression($pattern, $result);
  108. }
  109. /**
  110. * testExecute with no paths
  111. */
  112. public function testExecuteNoPathOption(): void
  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. public function testExecuteMerge(): void
  131. {
  132. $this->exec(
  133. 'i18n extract ' .
  134. '--merge=yes ' .
  135. '--extract-core=no ' .
  136. '--paths=' . TEST_APP . 'templates' . DS . 'Pages ' .
  137. '--output=' . $this->path . DS
  138. );
  139. $this->assertExitSuccess();
  140. $this->assertFileExists($this->path . DS . 'default.pot');
  141. $this->assertFileDoesNotExist($this->path . DS . 'cake.pot');
  142. $this->assertFileDoesNotExist($this->path . DS . 'domain.pot');
  143. }
  144. /**
  145. * test exclusions
  146. */
  147. public function testExtractWithExclude(): void
  148. {
  149. $this->exec(
  150. 'i18n extract ' .
  151. '--extract-core=no ' .
  152. '--exclude=Pages,Layout ' .
  153. '--paths=' . TEST_APP . 'templates' . DS . ' ' .
  154. '--output=' . $this->path . DS
  155. );
  156. $this->assertExitSuccess();
  157. $this->assertFileExists($this->path . DS . 'default.pot');
  158. $result = file_get_contents($this->path . DS . 'default.pot');
  159. $pattern = '/\#: .*extract\.php:\d+\n/';
  160. $this->assertDoesNotMatchRegularExpression($pattern, $result);
  161. $pattern = '/\#: .*default\.php:\d+\n/';
  162. $this->assertDoesNotMatchRegularExpression($pattern, $result);
  163. }
  164. /**
  165. * testExtractWithoutLocations method
  166. */
  167. public function testExtractWithoutLocations(): void
  168. {
  169. $this->exec(
  170. 'i18n extract ' .
  171. '--extract-core=no ' .
  172. '--no-location=true ' .
  173. '--exclude=Pages,Layout ' .
  174. '--paths=' . TEST_APP . 'templates' . DS . ' ' .
  175. '--output=' . $this->path . DS
  176. );
  177. $this->assertExitSuccess();
  178. $this->assertFileExists($this->path . DS . 'default.pot');
  179. $result = file_get_contents($this->path . DS . 'default.pot');
  180. $pattern = '/\n\#: .*\n/';
  181. $this->assertDoesNotMatchRegularExpression($pattern, $result);
  182. }
  183. /**
  184. * test extract can read more than one path.
  185. */
  186. public function testExtractMultiplePaths(): void
  187. {
  188. $this->exec(
  189. 'i18n extract ' .
  190. '--extract-core=no ' .
  191. '--exclude=Pages,Layout ' .
  192. '--paths=' . TEST_APP . 'templates/Pages,' .
  193. TEST_APP . 'templates/Posts ' .
  194. '--output=' . $this->path . DS
  195. );
  196. $this->assertExitSuccess();
  197. $result = file_get_contents($this->path . DS . 'default.pot');
  198. $pattern = '/msgid "Add User"/';
  199. $this->assertMatchesRegularExpression($pattern, $result);
  200. }
  201. /**
  202. * Tests that it is possible to exclude plugin paths by enabling the param option for the ExtractTask
  203. */
  204. public function testExtractExcludePlugins(): void
  205. {
  206. static::setAppNamespace();
  207. $this->exec(
  208. 'i18n extract ' .
  209. '--extract-core=no ' .
  210. '--exclude-plugins=true ' .
  211. '--paths=' . TEST_APP . 'TestApp/ ' .
  212. '--output=' . $this->path . DS
  213. );
  214. $this->assertExitSuccess();
  215. $result = file_get_contents($this->path . DS . 'default.pot');
  216. $this->assertDoesNotMatchRegularExpression('#TestPlugin#', $result);
  217. }
  218. /**
  219. * Test that is possible to extract messages from a single plugin
  220. */
  221. public function testExtractPlugin(): void
  222. {
  223. Configure::write('Plugins.autoload', ['TestPlugin']);
  224. $this->exec(
  225. 'i18n extract ' .
  226. '--extract-core=no ' .
  227. '--plugin=TestPlugin ' .
  228. '--output=' . $this->path . DS
  229. );
  230. $this->assertExitSuccess();
  231. $result = file_get_contents($this->path . DS . 'default.pot');
  232. $this->assertDoesNotMatchRegularExpression('#Pages#', $result);
  233. $this->assertMatchesRegularExpression('/translate\.php:\d+/', $result);
  234. $this->assertStringContainsString('This is a translatable string', $result);
  235. }
  236. /**
  237. * Test that is possible to extract messages from a vendored plugin.
  238. */
  239. public function testExtractVendoredPlugin(): void
  240. {
  241. $this->loadPlugins(['Company/TestPluginThree']);
  242. $this->exec(
  243. 'i18n extract ' .
  244. '--extract-core=no ' .
  245. '--plugin=Company/TestPluginThree ' .
  246. '--output=' . $this->path . DS
  247. );
  248. $this->assertExitSuccess();
  249. $result = file_get_contents($this->path . DS . 'test_plugin_three.pot');
  250. $this->assertDoesNotMatchRegularExpression('#Pages#', $result);
  251. $this->assertMatchesRegularExpression('/default\.php:\d+/', $result);
  252. $this->assertStringContainsString('A vendor message', $result);
  253. }
  254. /**
  255. * Test that the extract shell overwrites existing files with the overwrite parameter
  256. */
  257. public function testExtractOverwrite(): void
  258. {
  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. public function testExtractCore(): void
  277. {
  278. $this->exec(
  279. 'i18n extract ' .
  280. '--extract-core=yes ' .
  281. '--paths=' . TEST_APP . 'TestApp/ ' .
  282. '--output=' . $this->path . DS
  283. );
  284. $this->assertExitSuccess();
  285. $this->assertFileExists($this->path . DS . 'cake.pot');
  286. $result = file_get_contents($this->path . DS . 'cake.pot');
  287. $pattern = '/#: Console\/Templates\//';
  288. $this->assertDoesNotMatchRegularExpression($pattern, $result);
  289. $pattern = '/#: Test\//';
  290. $this->assertDoesNotMatchRegularExpression($pattern, $result);
  291. }
  292. /**
  293. * Test when marker-error option is set
  294. * When marker-error is unset, it's already test
  295. * with other functions like testExecute that not detects error because err never called
  296. */
  297. public function testMarkerErrorSets(): void
  298. {
  299. $this->exec(
  300. 'i18n extract ' .
  301. '--marker-error ' .
  302. '--merge=no ' .
  303. '--extract-core=no ' .
  304. '--paths=' . TEST_APP . 'templates/Pages ' .
  305. '--output=' . $this->path . DS
  306. );
  307. $this->assertExitSuccess();
  308. $this->assertErrorContains('Invalid marker content in');
  309. $this->assertErrorContains('extract.php');
  310. }
  311. /**
  312. * test relative-paths option
  313. */
  314. public function testExtractWithRelativePaths(): void
  315. {
  316. $this->exec(
  317. 'i18n extract ' .
  318. '--extract-core=no ' .
  319. '--paths=' . TEST_APP . 'templates ' .
  320. '--output=' . $this->path . DS
  321. );
  322. $this->assertExitSuccess();
  323. $this->assertFileExists($this->path . DS . 'default.pot');
  324. $result = file_get_contents($this->path . DS . 'default.pot');
  325. $expected = '#: ./tests/test_app/templates/Pages/extract.php:';
  326. $this->assertStringContainsString($expected, $result);
  327. }
  328. /**
  329. * test invalid path options
  330. */
  331. public function testExtractWithInvalidPaths(): void
  332. {
  333. $this->exec(
  334. 'i18n extract ' .
  335. '--extract-core=no ' .
  336. '--paths=' . TEST_APP . 'templates,' . TEST_APP . 'unknown ' .
  337. '--output=' . $this->path . DS
  338. );
  339. $this->assertExitSuccess();
  340. $this->assertFileExists($this->path . DS . 'default.pot');
  341. $result = file_get_contents($this->path . DS . 'default.pot');
  342. $expected = '#: ./tests/test_app/templates/Pages/extract.php:';
  343. $this->assertStringContainsString($expected, $result);
  344. }
  345. }