ExtractTaskTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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:31\n';
  96. $pattern .= 'msgctxt "mail"/';
  97. $this->assertRegExp($pattern, $result);
  98. // extract.ctp - 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->assertNotRegExp($pattern, $result);
  102. $pattern = '/msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  103. $this->assertNotRegExp($pattern, $result);
  104. $pattern = '/msgid "You have %d new message \(domain\)."\nmsgid_plural "You have %d new messages \(domain\)."/';
  105. $this->assertRegExp($pattern, $result);
  106. $pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
  107. $this->assertRegExp($pattern, $result);
  108. }
  109. /**
  110. * test exclusions
  111. *
  112. * @return void
  113. */
  114. public function testExtractWithExclude()
  115. {
  116. $this->Task->interactive = false;
  117. $this->Task->params['paths'] = TEST_APP . 'TestApp/Template';
  118. $this->Task->params['output'] = $this->path . DS;
  119. $this->Task->params['exclude'] = 'Pages,Layout';
  120. $this->Task->params['extract-core'] = 'no';
  121. $this->Task->expects($this->any())->method('in')
  122. ->will($this->returnValue('y'));
  123. $this->Task->main();
  124. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  125. $result = file_get_contents($this->path . DS . 'default.pot');
  126. $pattern = '/\#: .*extract\.ctp:\d+\n/';
  127. $this->assertNotRegExp($pattern, $result);
  128. $pattern = '/\#: .*default\.ctp:\d+\n/';
  129. $this->assertNotRegExp($pattern, $result);
  130. }
  131. /**
  132. * test extract can read more than one path.
  133. *
  134. * @return void
  135. */
  136. public function testExtractMultiplePaths()
  137. {
  138. $this->Task->interactive = false;
  139. $this->Task->params['paths'] =
  140. TEST_APP . 'TestApp/Template/Pages,' .
  141. TEST_APP . 'TestApp/Template/Posts';
  142. $this->Task->params['output'] = $this->path . DS;
  143. $this->Task->params['extract-core'] = 'no';
  144. $this->Task->expects($this->never())->method('err');
  145. $this->Task->expects($this->never())->method('_stop');
  146. $this->Task->main();
  147. $result = file_get_contents($this->path . DS . 'default.pot');
  148. $pattern = '/msgid "Add User"/';
  149. $this->assertRegExp($pattern, $result);
  150. }
  151. /**
  152. * Tests that it is possible to exclude plugin paths by enabling the param option for the ExtractTask
  153. *
  154. * @return void
  155. */
  156. public function testExtractExcludePlugins()
  157. {
  158. Configure::write('App.namespace', 'TestApp');
  159. $this->Task = $this->getMock(
  160. 'Cake\Shell\Task\ExtractTask',
  161. ['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'],
  162. [$this->io]
  163. );
  164. $this->Task->expects($this->exactly(1))
  165. ->method('_isExtractingApp')
  166. ->will($this->returnValue(true));
  167. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  168. $this->Task->params['output'] = $this->path . DS;
  169. $this->Task->params['exclude-plugins'] = true;
  170. $this->Task->main();
  171. $result = file_get_contents($this->path . DS . 'default.pot');
  172. $this->assertNotRegExp('#TestPlugin#', $result);
  173. }
  174. /**
  175. * Test that is possible to extract messages form a single plugin
  176. *
  177. * @return void
  178. */
  179. public function testExtractPlugin()
  180. {
  181. Configure::write('App.namespace', 'TestApp');
  182. $this->Task = $this->getMock(
  183. 'Cake\Shell\Task\ExtractTask',
  184. ['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'],
  185. [$this->io]
  186. );
  187. $this->Task->params['output'] = $this->path . DS;
  188. $this->Task->params['plugin'] = 'TestPlugin';
  189. $this->Task->main();
  190. $result = file_get_contents($this->path . DS . 'default.pot');
  191. $this->assertNotRegExp('#Pages#', $result);
  192. $this->assertRegExp('/translate\.ctp:\d+/', $result);
  193. $this->assertContains('This is a translatable string', $result);
  194. }
  195. /**
  196. * Test that the extract shell overwrites existing files with the overwrite parameter
  197. *
  198. * @return void
  199. */
  200. public function testExtractOverwrite()
  201. {
  202. Configure::write('App.namespace', 'TestApp');
  203. $this->Task->interactive = false;
  204. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  205. $this->Task->params['output'] = $this->path . DS;
  206. $this->Task->params['extract-core'] = 'no';
  207. $this->Task->params['overwrite'] = true;
  208. file_put_contents($this->path . DS . 'default.pot', 'will be overwritten');
  209. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  210. $original = file_get_contents($this->path . DS . 'default.pot');
  211. $this->Task->main();
  212. $result = file_get_contents($this->path . DS . 'default.pot');
  213. $this->assertNotEquals($original, $result);
  214. }
  215. /**
  216. * Test that the extract shell scans the core libs
  217. *
  218. * @return void
  219. */
  220. public function testExtractCore()
  221. {
  222. Configure::write('App.namespace', 'TestApp');
  223. $this->Task->interactive = false;
  224. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  225. $this->Task->params['output'] = $this->path . DS;
  226. $this->Task->params['extract-core'] = 'yes';
  227. $this->Task->main();
  228. $this->assertTrue(file_exists($this->path . DS . 'cake.pot'));
  229. $result = file_get_contents($this->path . DS . 'cake.pot');
  230. $pattern = '/#: Console\/Templates\//';
  231. $this->assertNotRegExp($pattern, $result);
  232. $pattern = '/#: Test\//';
  233. $this->assertNotRegExp($pattern, $result);
  234. }
  235. }