ExtractTaskTest.php 9.2 KB

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