ExtractTaskTest.php 9.8 KB

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