ExtractTaskTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * test extract can read more than one path.
  138. *
  139. * @return void
  140. */
  141. public function testExtractMultiplePaths()
  142. {
  143. $this->Task->interactive = false;
  144. $this->Task->params['paths'] =
  145. TEST_APP . 'TestApp/Template/Pages,' .
  146. TEST_APP . 'TestApp/Template/Posts';
  147. $this->Task->params['output'] = $this->path . DS;
  148. $this->Task->params['extract-core'] = 'no';
  149. $this->Task->expects($this->never())->method('err');
  150. $this->Task->expects($this->never())->method('_stop');
  151. $this->Task->main();
  152. $result = file_get_contents($this->path . DS . 'default.pot');
  153. $pattern = '/msgid "Add User"/';
  154. $this->assertRegExp($pattern, $result);
  155. }
  156. /**
  157. * Tests that it is possible to exclude plugin paths by enabling the param option for the ExtractTask
  158. *
  159. * @return void
  160. */
  161. public function testExtractExcludePlugins()
  162. {
  163. Configure::write('App.namespace', 'TestApp');
  164. $this->Task = $this->getMock(
  165. 'Cake\Shell\Task\ExtractTask',
  166. ['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'],
  167. [$this->io]
  168. );
  169. $this->Task->expects($this->exactly(1))
  170. ->method('_isExtractingApp')
  171. ->will($this->returnValue(true));
  172. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  173. $this->Task->params['output'] = $this->path . DS;
  174. $this->Task->params['exclude-plugins'] = true;
  175. $this->Task->main();
  176. $result = file_get_contents($this->path . DS . 'default.pot');
  177. $this->assertNotRegExp('#TestPlugin#', $result);
  178. }
  179. /**
  180. * Test that is possible to extract messages form a single plugin
  181. *
  182. * @return void
  183. */
  184. public function testExtractPlugin()
  185. {
  186. Configure::write('App.namespace', 'TestApp');
  187. $this->Task = $this->getMock(
  188. 'Cake\Shell\Task\ExtractTask',
  189. ['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'],
  190. [$this->io]
  191. );
  192. $this->Task->params['output'] = $this->path . DS;
  193. $this->Task->params['plugin'] = 'TestPlugin';
  194. $this->Task->main();
  195. $result = file_get_contents($this->path . DS . 'default.pot');
  196. $this->assertNotRegExp('#Pages#', $result);
  197. $this->assertRegExp('/translate\.ctp:\d+/', $result);
  198. $this->assertContains('This is a translatable string', $result);
  199. }
  200. /**
  201. * Test that the extract shell overwrites existing files with the overwrite parameter
  202. *
  203. * @return void
  204. */
  205. public function testExtractOverwrite()
  206. {
  207. Configure::write('App.namespace', 'TestApp');
  208. $this->Task->interactive = false;
  209. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  210. $this->Task->params['output'] = $this->path . DS;
  211. $this->Task->params['extract-core'] = 'no';
  212. $this->Task->params['overwrite'] = true;
  213. file_put_contents($this->path . DS . 'default.pot', 'will be overwritten');
  214. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  215. $original = file_get_contents($this->path . DS . 'default.pot');
  216. $this->Task->main();
  217. $result = file_get_contents($this->path . DS . 'default.pot');
  218. $this->assertNotEquals($original, $result);
  219. }
  220. /**
  221. * Test that the extract shell scans the core libs
  222. *
  223. * @return void
  224. */
  225. public function testExtractCore()
  226. {
  227. Configure::write('App.namespace', 'TestApp');
  228. $this->Task->interactive = false;
  229. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  230. $this->Task->params['output'] = $this->path . DS;
  231. $this->Task->params['extract-core'] = 'yes';
  232. $this->Task->main();
  233. $this->assertTrue(file_exists($this->path . DS . 'cake.pot'));
  234. $result = file_get_contents($this->path . DS . 'cake.pot');
  235. $pattern = '/#: Console\/Templates\//';
  236. $this->assertNotRegExp($pattern, $result);
  237. $pattern = '/#: Test\//';
  238. $this->assertNotRegExp($pattern, $result);
  239. }
  240. }