ExtractTaskTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. /**
  3. * ExtractTaskTest file
  4. *
  5. * Test Case for i18n extraction shell task
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2011, Cake Software Foundation, Inc.
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc.
  16. * @link http://cakephp.org CakePHP Project
  17. * @package Cake.Test.Case.Console.Command.Task
  18. * @since CakePHP v 1.2.0.7726
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('Folder', 'Utility');
  22. App::uses('ShellDispatcher', 'Console');
  23. App::uses('Shell', 'Console');
  24. App::uses('ExtractTask', 'Console/Command/Task');
  25. /**
  26. * ExtractTaskTest class
  27. *
  28. * @package Cake.Test.Case.Console.Command.Task
  29. */
  30. class ExtractTaskTest extends CakeTestCase {
  31. /**
  32. * setUp method
  33. *
  34. * @return void
  35. */
  36. public function setUp() {
  37. parent::setUp();
  38. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  39. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  40. $this->Task = $this->getMock(
  41. 'ExtractTask',
  42. array('in', 'out', 'err', '_stop'),
  43. array($out, $out, $in)
  44. );
  45. $this->path = TMP . 'tests' . DS . 'extract_task_test';
  46. $Folder = new Folder($this->path . DS . 'locale', true);
  47. }
  48. /**
  49. * tearDown method
  50. *
  51. * @return void
  52. */
  53. public function tearDown() {
  54. parent::tearDown();
  55. unset($this->Task);
  56. $Folder = new Folder($this->path);
  57. $Folder->delete();
  58. CakePlugin::unload();
  59. }
  60. /**
  61. * testExecute method
  62. *
  63. * @return void
  64. */
  65. public function testExecute() {
  66. $this->Task->interactive = false;
  67. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages';
  68. $this->Task->params['output'] = $this->path . DS;
  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->execute();
  74. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  75. $result = file_get_contents($this->path . DS . 'default.pot');
  76. $pattern = '/"Content-Type\: text\/plain; charset\=utf-8/';
  77. $this->assertRegExp($pattern, $result);
  78. $pattern = '/"Content-Transfer-Encoding\: 8bit/';
  79. $this->assertRegExp($pattern, $result);
  80. $pattern = '/"Plural-Forms\: nplurals\=INTEGER; plural\=EXPRESSION;/';
  81. $this->assertRegExp($pattern, $result);
  82. // home.ctp
  83. $pattern = '/msgid "Your tmp directory is writable."\nmsgstr ""\n/';
  84. $this->assertRegExp($pattern, $result);
  85. $pattern = '/msgid "Your tmp directory is NOT writable."\nmsgstr ""\n/';
  86. $this->assertRegExp($pattern, $result);
  87. $pattern = '/msgid "The %s is being used for caching. To change the config edit ';
  88. $pattern .= 'APP\/config\/core.php "\nmsgstr ""\n/';
  89. $this->assertRegExp($pattern, $result);
  90. $pattern = '/msgid "Your cache is NOT working. Please check ';
  91. $pattern .= 'the settings in APP\/config\/core.php"\nmsgstr ""\n/';
  92. $this->assertRegExp($pattern, $result);
  93. $pattern = '/msgid "Your database configuration file is present."\nmsgstr ""\n/';
  94. $this->assertRegExp($pattern, $result);
  95. $pattern = '/msgid "Your database configuration file is NOT present."\nmsgstr ""\n/';
  96. $this->assertRegExp($pattern, $result);
  97. $pattern = '/msgid "Rename config\/database.php.default to ';
  98. $pattern .= 'config\/database.php"\nmsgstr ""\n/';
  99. $this->assertRegExp($pattern, $result);
  100. $pattern = '/msgid "Cake is able to connect to the database."\nmsgstr ""\n/';
  101. $this->assertRegExp($pattern, $result);
  102. $pattern = '/msgid "Cake is NOT able to connect to the database."\nmsgstr ""\n/';
  103. $this->assertRegExp($pattern, $result);
  104. $pattern = '/msgid "Editing this Page"\nmsgstr ""\n/';
  105. $this->assertRegExp($pattern, $result);
  106. $pattern = '/msgid "To change the content of this page, create: APP\/views\/pages\/home\.ctp/';
  107. $this->assertRegExp($pattern, $result);
  108. $pattern = '/To change its layout, create: APP\/views\/layouts\/default\.ctp\./s';
  109. $this->assertRegExp($pattern, $result);
  110. // extract.ctp
  111. $pattern = '/\#: (\\\\|\/)extract\.ctp:15;6\n';
  112. $pattern .= 'msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  113. $this->assertRegExp($pattern, $result);
  114. $pattern = '/msgid "You have %d new message."\nmsgstr ""/';
  115. $this->assertNotRegExp($pattern, $result, 'No duplicate msgid');
  116. $pattern = '/\#: (\\\\|\/)extract\.ctp:7\n';
  117. $pattern .= 'msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  118. $this->assertRegExp($pattern, $result);
  119. $pattern = '/\#: (\\\\|\/)extract\.ctp:14\n';
  120. $pattern .= '\#: (\\\\|\/)home\.ctp:99\n';
  121. $pattern .= 'msgid "Editing this Page"\nmsgstr ""/';
  122. $this->assertRegExp($pattern, $result);
  123. $pattern = '/\#: (\\\\|\/)extract\.ctp:18\nmsgid "';
  124. $pattern .= 'Hot features!';
  125. $pattern .= '\\\n - No Configuration: Set-up the database and let the magic begin';
  126. $pattern .= '\\\n - Extremely Simple: Just look at the name...It\'s Cake';
  127. $pattern .= '\\\n - Active, Friendly Community: Join us #cakephp on IRC. We\'d love to help you get started';
  128. $pattern .= '"\nmsgstr ""/';
  129. $this->assertRegExp($pattern, $result);
  130. // extract.ctp - reading the domain.pot
  131. $result = file_get_contents($this->path . DS . 'domain.pot');
  132. $pattern = '/msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  133. $this->assertNotRegExp($pattern, $result);
  134. $pattern = '/msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  135. $this->assertNotRegExp($pattern, $result);
  136. $pattern = '/msgid "You have %d new message \(domain\)."\nmsgid_plural "You have %d new messages \(domain\)."/';
  137. $this->assertRegExp($pattern, $result);
  138. $pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
  139. $this->assertRegExp($pattern, $result);
  140. }
  141. /**
  142. * test exclusions
  143. *
  144. * @return void
  145. */
  146. public function testExtractWithExclude() {
  147. $this->Task->interactive = false;
  148. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS . 'View';
  149. $this->Task->params['output'] = $this->path . DS;
  150. $this->Task->params['exclude'] = 'Pages,Layouts';
  151. $this->Task->expects($this->any())->method('in')
  152. ->will($this->returnValue('y'));
  153. $this->Task->execute();
  154. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  155. $result = file_get_contents($this->path . DS . 'default.pot');
  156. $pattern = '/\#: .*extract\.ctp:6\n/';
  157. $this->assertNotRegExp($pattern, $result);
  158. $pattern = '/\#: .*default\.ctp:26\n/';
  159. $this->assertNotRegExp($pattern, $result);
  160. }
  161. /**
  162. * test extract can read more than one path.
  163. *
  164. * @return void
  165. */
  166. public function testExtractMultiplePaths() {
  167. $this->Task->interactive = false;
  168. $this->Task->params['paths'] =
  169. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages,' .
  170. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts';
  171. $this->Task->params['output'] = $this->path . DS;
  172. $this->Task->expects($this->never())->method('err');
  173. $this->Task->expects($this->never())->method('_stop');
  174. $this->Task->execute();
  175. $result = file_get_contents($this->path . DS . 'default.pot');
  176. $pattern = '/msgid "Add User"/';
  177. $this->assertRegExp($pattern, $result);
  178. }
  179. /**
  180. * Tests that it is possible to exclude plugin paths by enabling the param option for the ExtractTask
  181. *
  182. * @return void
  183. */
  184. public function testExtractExcludePlugins() {
  185. App::build(array(
  186. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  187. ));
  188. $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  189. $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
  190. $this->Task = $this->getMock('ExtractTask',
  191. array('_isExtractingApp', '_extractValidationMessages', 'in', 'out', 'err', 'clear', '_stop'),
  192. array($this->out, $this->out, $this->in)
  193. );
  194. $this->Task->expects($this->exactly(2))->method('_isExtractingApp')->will($this->returnValue(true));
  195. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
  196. $this->Task->params['output'] = $this->path . DS;
  197. $this->Task->params['exclude-plugins'] = true;
  198. $this->Task->execute();
  199. $result = file_get_contents($this->path . DS . 'default.pot');
  200. $this->assertNotRegExp('#TestPlugin#', $result);
  201. }
  202. /**
  203. * Test that is possible to extract messages form a single plugin
  204. *
  205. * @return void
  206. */
  207. public function testExtractPlugin() {
  208. App::build(array(
  209. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  210. ));
  211. $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  212. $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
  213. $this->Task = $this->getMock('ExtractTask',
  214. array('_isExtractingApp', '_extractValidationMessages', 'in', 'out', 'err', 'clear', '_stop'),
  215. array($this->out, $this->out, $this->in)
  216. );
  217. $this->Task->params['output'] = $this->path . DS;
  218. $this->Task->params['plugin'] = 'TestPlugin';
  219. $this->Task->execute();
  220. $result = file_get_contents($this->path . DS . 'default.pot');
  221. $this->assertNotRegExp('#Pages#', $result);
  222. $this->assertContains('translate.ctp:1', $result);
  223. $this->assertContains('This is a translatable string', $result);
  224. }
  225. /**
  226. * Tests that the task will inspect application models and extract the validation messages from them
  227. *
  228. * @return void
  229. */
  230. public function testExtractModelValidation() {
  231. App::build(array(
  232. 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
  233. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  234. ), App::RESET);
  235. $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  236. $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
  237. $this->Task = $this->getMock('ExtractTask',
  238. array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
  239. array($this->out, $this->out, $this->in)
  240. );
  241. $this->Task->expects($this->exactly(2))->method('_isExtractingApp')->will($this->returnValue(true));
  242. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
  243. $this->Task->params['output'] = $this->path . DS;
  244. $this->Task->params['exclude-plugins'] = true;
  245. $this->Task->params['ignore-model-validation'] = false;
  246. $this->Task->execute();
  247. $result = file_get_contents($this->path . DS . 'default.pot');
  248. $pattern = preg_quote('#Model' . DS . 'PersisterOne.php:validation for field title#', '\\');
  249. $this->assertRegExp($pattern, $result);
  250. $pattern = preg_quote('#Model' . DS . 'PersisterOne.php:validation for field body#', '\\');
  251. $this->assertRegExp($pattern, $result);
  252. $pattern = '#msgid "Post title is required"#';
  253. $this->assertRegExp($pattern, $result);
  254. $pattern = '#msgid "You may enter up to %s chars \(minimum is %s chars\)"#';
  255. $this->assertRegExp($pattern, $result);
  256. $pattern = '#msgid "Post body is required"#';
  257. $this->assertRegExp($pattern, $result);
  258. $pattern = '#msgid "Post body is super required"#';
  259. $this->assertRegExp($pattern, $result);
  260. }
  261. /**
  262. * Tests that the task will inspect application models and extract the validation messages from them
  263. * while using a custom validation domain for the messages set on the model itself
  264. *
  265. * @return void
  266. */
  267. public function testExtractModelValidationWithDomainInModel() {
  268. App::build(array(
  269. 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Model' . DS)
  270. ));
  271. $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  272. $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
  273. $this->Task = $this->getMock('ExtractTask',
  274. array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
  275. array($this->out, $this->out, $this->in)
  276. );
  277. $this->Task->expects($this->exactly(2))->method('_isExtractingApp')->will($this->returnValue(true));
  278. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
  279. $this->Task->params['output'] = $this->path . DS;
  280. $this->Task->params['exclude-plugins'] = true;
  281. $this->Task->params['ignore-model-validation'] = false;
  282. $this->Task->execute();
  283. $result = file_get_contents($this->path . DS . 'test_plugin.pot');
  284. $pattern = preg_quote('#Plugin' . DS . 'TestPlugin' . DS . 'Model' . DS . 'TestPluginPost.php:validation for field title#', '\\');
  285. $this->assertRegExp($pattern, $result);
  286. $pattern = preg_quote('#Plugin' . DS . 'TestPlugin' . DS . 'Model' . DS . 'TestPluginPost.php:validation for field body#', '\\');
  287. $this->assertRegExp($pattern, $result);
  288. $pattern = '#msgid "Post title is required"#';
  289. $this->assertRegExp($pattern, $result);
  290. $pattern = '#msgid "Post body is required"#';
  291. $this->assertRegExp($pattern, $result);
  292. $pattern = '#msgid "Post body is super required"#';
  293. $this->assertRegExp($pattern, $result);
  294. }
  295. /**
  296. * Test that the extract shell can obtain validation messages from models inside a specific plugin
  297. *
  298. * @return void
  299. */
  300. public function testExtractModelValidationInPlugin() {
  301. App::build(array(
  302. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  303. ));
  304. $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  305. $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
  306. $this->Task = $this->getMock('ExtractTask',
  307. array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
  308. array($this->out, $this->out, $this->in)
  309. );
  310. $this->Task->params['output'] = $this->path . DS;
  311. $this->Task->params['ignore-model-validation'] = false;
  312. $this->Task->params['plugin'] = 'TestPlugin';
  313. $this->Task->execute();
  314. $result = file_get_contents($this->path . DS . 'test_plugin.pot');
  315. $pattern = preg_quote('#Model' . DS . 'TestPluginPost.php:validation for field title#', '\\');
  316. $this->assertRegExp($pattern, $result);
  317. $pattern = preg_quote('#Model' . DS . 'TestPluginPost.php:validation for field body#', '\\');
  318. $this->assertRegExp($pattern, $result);
  319. $pattern = '#msgid "Post title is required"#';
  320. $this->assertRegExp($pattern, $result);
  321. $pattern = '#msgid "Post body is required"#';
  322. $this->assertRegExp($pattern, $result);
  323. $pattern = '#msgid "Post body is super required"#';
  324. $this->assertRegExp($pattern, $result);
  325. $pattern = '#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field title#';
  326. $this->assertNotRegExp($pattern, $result);
  327. }
  328. }