ExtractTaskTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * CakePHP : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Shell\Task;
  16. use Cake\Core\App;
  17. use Cake\Core\Configure;
  18. use Cake\Core\Plugin;
  19. use Cake\Filesystem\Folder;
  20. use Cake\Shell\Task\ExtractTask;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * ExtractTaskTest class
  24. *
  25. */
  26. class ExtractTaskTest extends TestCase
  27. {
  28. /**
  29. * setUp method
  30. *
  31. * @return void
  32. */
  33. public function setUp()
  34. {
  35. parent::setUp();
  36. $this->io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  37. $this->Task = $this->getMock(
  38. 'Cake\Shell\Task\ExtractTask',
  39. ['in', 'out', 'err', '_stop'],
  40. [$this->io]
  41. );
  42. $this->path = TMP . 'tests/extract_task_test';
  43. new Folder($this->path . DS . 'locale', true);
  44. }
  45. /**
  46. * tearDown method
  47. *
  48. * @return void
  49. */
  50. public function tearDown()
  51. {
  52. parent::tearDown();
  53. unset($this->Task);
  54. $Folder = new Folder($this->path);
  55. $Folder->delete();
  56. Plugin::unload();
  57. }
  58. /**
  59. * testExecute method
  60. *
  61. * @return void
  62. */
  63. public function testExecute()
  64. {
  65. $this->Task->interactive = false;
  66. $this->Task->params['paths'] = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages';
  67. $this->Task->params['output'] = $this->path . DS;
  68. $this->Task->params['extract-core'] = 'no';
  69. $this->Task->expects($this->never())->method('err');
  70. $this->Task->expects($this->any())->method('in')
  71. ->will($this->returnValue('y'));
  72. $this->Task->expects($this->never())->method('_stop');
  73. $this->Task->main();
  74. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  75. $result = file_get_contents($this->path . DS . 'default.pot');
  76. $this->assertFalse(file_exists($this->path . DS . 'cake.pot'));
  77. // extract.ctp
  78. $pattern = '/\#: (\\\\|\/)extract\.ctp:\d+;\d+\n';
  79. $pattern .= 'msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  80. $this->assertRegExp($pattern, $result);
  81. $pattern = '/msgid "You have %d new message."\nmsgstr ""/';
  82. $this->assertNotRegExp($pattern, $result, 'No duplicate msgid');
  83. $pattern = '/\#: (\\\\|\/)extract\.ctp:\d+\n';
  84. $pattern .= 'msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  85. $this->assertRegExp($pattern, $result);
  86. $pattern = '/\#: (\\\\|\/)extract\.ctp:\d+\nmsgid "';
  87. $pattern .= 'Hot features!';
  88. $pattern .= '\\\n - No Configuration: Set-up the database and let the magic begin';
  89. $pattern .= '\\\n - Extremely Simple: Just look at the name...It\'s Cake';
  90. $pattern .= '\\\n - Active, Friendly Community: Join us #cakephp on IRC. We\'d love to help you get started';
  91. $pattern .= '"\nmsgstr ""/';
  92. $this->assertRegExp($pattern, $result);
  93. $this->assertContains('msgid "double \\"quoted\\""', $result, 'Strings with quotes not handled correctly');
  94. $this->assertContains("msgid \"single 'quoted'\"", $result, 'Strings with quotes not handled correctly');
  95. $pattern = '/\#: (\\\\|\/)extract\.ctp:\d+\n';
  96. $pattern .= 'msgctxt "mail"\n';
  97. $pattern .= 'msgid "letter"/';
  98. $this->assertRegExp($pattern, $result);
  99. $pattern = '/\#: (\\\\|\/)extract\.ctp:\d+\n';
  100. $pattern .= 'msgctxt "alphabet"\n';
  101. $pattern .= 'msgid "letter"/';
  102. $this->assertRegExp($pattern, $result);
  103. // extract.ctp - reading the domain.pot
  104. $result = file_get_contents($this->path . DS . 'domain.pot');
  105. $pattern = '/msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  106. $this->assertNotRegExp($pattern, $result);
  107. $pattern = '/msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  108. $this->assertNotRegExp($pattern, $result);
  109. $pattern = '/msgid "You have %d new message \(domain\)."\nmsgid_plural "You have %d new messages \(domain\)."/';
  110. $this->assertRegExp($pattern, $result);
  111. $pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
  112. $this->assertRegExp($pattern, $result);
  113. }
  114. /**
  115. * test exclusions
  116. *
  117. * @return void
  118. */
  119. public function testExtractWithExclude()
  120. {
  121. $this->Task->interactive = false;
  122. $this->Task->params['paths'] = TEST_APP . 'TestApp/Template';
  123. $this->Task->params['output'] = $this->path . DS;
  124. $this->Task->params['exclude'] = 'Pages,Layout';
  125. $this->Task->params['extract-core'] = 'no';
  126. $this->Task->expects($this->any())->method('in')
  127. ->will($this->returnValue('y'));
  128. $this->Task->main();
  129. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  130. $result = file_get_contents($this->path . DS . 'default.pot');
  131. $pattern = '/\#: .*extract\.ctp:\d+\n/';
  132. $this->assertNotRegExp($pattern, $result);
  133. $pattern = '/\#: .*default\.ctp:\d+\n/';
  134. $this->assertNotRegExp($pattern, $result);
  135. }
  136. /**
  137. * testExtractWithoutLocations method
  138. *
  139. * @return void
  140. */
  141. public function testExtractWithoutLocations()
  142. {
  143. $this->Task->params['paths'] = TEST_APP . 'TestApp/Template';
  144. $this->Task->params['output'] = $this->path . DS;
  145. $this->Task->params['exclude'] = 'Pages,Layout';
  146. $this->Task->params['extract-core'] = 'no';
  147. $this->Task->params['no-location'] = true;
  148. $this->Task->expects($this->never())->method('err');
  149. $this->Task->expects($this->any())->method('in')
  150. ->will($this->returnValue('y'));
  151. $this->Task->main();
  152. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  153. $result = file_get_contents($this->path . DS . 'default.pot');
  154. $pattern = '/\n\#: .*\n/';
  155. $this->assertNotRegExp($pattern, $result);
  156. }
  157. /**
  158. * test extract can read more than one path.
  159. *
  160. * @return void
  161. */
  162. public function testExtractMultiplePaths()
  163. {
  164. $this->Task->interactive = false;
  165. $this->Task->params['paths'] =
  166. TEST_APP . 'TestApp/Template/Pages,' .
  167. TEST_APP . 'TestApp/Template/Posts';
  168. $this->Task->params['output'] = $this->path . DS;
  169. $this->Task->params['extract-core'] = 'no';
  170. $this->Task->expects($this->never())->method('err');
  171. $this->Task->expects($this->never())->method('_stop');
  172. $this->Task->main();
  173. $result = file_get_contents($this->path . DS . 'default.pot');
  174. $pattern = '/msgid "Add User"/';
  175. $this->assertRegExp($pattern, $result);
  176. }
  177. /**
  178. * Tests that it is possible to exclude plugin paths by enabling the param option for the ExtractTask
  179. *
  180. * @return void
  181. */
  182. public function testExtractExcludePlugins()
  183. {
  184. Configure::write('App.namespace', 'TestApp');
  185. $this->Task = $this->getMock(
  186. 'Cake\Shell\Task\ExtractTask',
  187. ['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'],
  188. [$this->io]
  189. );
  190. $this->Task->expects($this->exactly(1))
  191. ->method('_isExtractingApp')
  192. ->will($this->returnValue(true));
  193. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  194. $this->Task->params['output'] = $this->path . DS;
  195. $this->Task->params['exclude-plugins'] = true;
  196. $this->Task->main();
  197. $result = file_get_contents($this->path . DS . 'default.pot');
  198. $this->assertNotRegExp('#TestPlugin#', $result);
  199. }
  200. /**
  201. * Test that is possible to extract messages form a single plugin
  202. *
  203. * @return void
  204. */
  205. public function testExtractPlugin()
  206. {
  207. Configure::write('App.namespace', 'TestApp');
  208. $this->Task = $this->getMock(
  209. 'Cake\Shell\Task\ExtractTask',
  210. ['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'],
  211. [$this->io]
  212. );
  213. $this->Task->params['output'] = $this->path . DS;
  214. $this->Task->params['plugin'] = 'TestPlugin';
  215. $this->Task->main();
  216. $result = file_get_contents($this->path . DS . 'default.pot');
  217. $this->assertNotRegExp('#Pages#', $result);
  218. $this->assertRegExp('/translate\.ctp:\d+/', $result);
  219. $this->assertContains('This is a translatable string', $result);
  220. }
  221. /**
  222. * Test that the extract shell overwrites existing files with the overwrite parameter
  223. *
  224. * @return void
  225. */
  226. public function testExtractOverwrite()
  227. {
  228. Configure::write('App.namespace', 'TestApp');
  229. $this->Task->interactive = false;
  230. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  231. $this->Task->params['output'] = $this->path . DS;
  232. $this->Task->params['extract-core'] = 'no';
  233. $this->Task->params['overwrite'] = true;
  234. file_put_contents($this->path . DS . 'default.pot', 'will be overwritten');
  235. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  236. $original = file_get_contents($this->path . DS . 'default.pot');
  237. $this->Task->main();
  238. $result = file_get_contents($this->path . DS . 'default.pot');
  239. $this->assertNotEquals($original, $result);
  240. }
  241. /**
  242. * Test that the extract shell scans the core libs
  243. *
  244. * @return void
  245. */
  246. public function testExtractCore()
  247. {
  248. Configure::write('App.namespace', 'TestApp');
  249. $this->Task->interactive = false;
  250. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  251. $this->Task->params['output'] = $this->path . DS;
  252. $this->Task->params['extract-core'] = 'yes';
  253. $this->Task->main();
  254. $this->assertTrue(file_exists($this->path . DS . 'cake.pot'));
  255. $result = file_get_contents($this->path . DS . 'cake.pot');
  256. $pattern = '/#: Console\/Templates\//';
  257. $this->assertNotRegExp($pattern, $result);
  258. $pattern = '/#: Test\//';
  259. $this->assertNotRegExp($pattern, $result);
  260. }
  261. }