ExtractTaskTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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->assertPattern($pattern, $result);
  78. $pattern = '/"Content-Transfer-Encoding\: 8bit/';
  79. $this->assertPattern($pattern, $result);
  80. $pattern = '/"Plural-Forms\: nplurals\=INTEGER; plural\=EXPRESSION;/';
  81. $this->assertPattern($pattern, $result);
  82. // home.ctp
  83. $pattern = '/msgid "Your tmp directory is writable."\nmsgstr ""\n/';
  84. $this->assertPattern($pattern, $result);
  85. $pattern = '/msgid "Your tmp directory is NOT writable."\nmsgstr ""\n/';
  86. $this->assertPattern($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->assertPattern($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->assertPattern($pattern, $result);
  93. $pattern = '/msgid "Your database configuration file is present."\nmsgstr ""\n/';
  94. $this->assertPattern($pattern, $result);
  95. $pattern = '/msgid "Your database configuration file is NOT present."\nmsgstr ""\n/';
  96. $this->assertPattern($pattern, $result);
  97. $pattern = '/msgid "Rename config\/database.php.default to ';
  98. $pattern .= 'config\/database.php"\nmsgstr ""\n/';
  99. $this->assertPattern($pattern, $result);
  100. $pattern = '/msgid "Cake is able to connect to the database."\nmsgstr ""\n/';
  101. $this->assertPattern($pattern, $result);
  102. $pattern = '/msgid "Cake is NOT able to connect to the database."\nmsgstr ""\n/';
  103. $this->assertPattern($pattern, $result);
  104. $pattern = '/msgid "Editing this Page"\nmsgstr ""\n/';
  105. $this->assertPattern($pattern, $result);
  106. $pattern = '/msgid "To change the content of this page, create: APP\/views\/pages\/home\.ctp/';
  107. $this->assertPattern($pattern, $result);
  108. $pattern = '/To change its layout, create: APP\/views\/layouts\/default\.ctp\./s';
  109. $this->assertPattern($pattern, $result);
  110. // extract.ctp
  111. $pattern = '/\#: (\\\\|\/)extract\.ctp:6\n';
  112. $pattern .= 'msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  113. $this->assertPattern($pattern, $result);
  114. $pattern = '/\#: (\\\\|\/)extract\.ctp:7\n';
  115. $pattern .= 'msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  116. $this->assertPattern($pattern, $result);
  117. $pattern = '/\#: (\\\\|\/)extract\.ctp:14\n';
  118. $pattern .= '\#: (\\\\|\/)home\.ctp:99\n';
  119. $pattern .= 'msgid "Editing this Page"\nmsgstr ""/';
  120. $this->assertPattern($pattern, $result);
  121. $pattern = '/\#: (\\\\|\/)extract\.ctp:17\nmsgid "';
  122. $pattern .= 'Hot features!';
  123. $pattern .= '\\\n - No Configuration: Set-up the database and let the magic begin';
  124. $pattern .= '\\\n - Extremely Simple: Just look at the name...It\'s Cake';
  125. $pattern .= '\\\n - Active, Friendly Community: Join us #cakephp on IRC. We\'d love to help you get started';
  126. $pattern .= '"\nmsgstr ""/';
  127. $this->assertPattern($pattern, $result);
  128. // extract.ctp - reading the domain.pot
  129. $result = file_get_contents($this->path . DS . 'domain.pot');
  130. $pattern = '/msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  131. $this->assertNoPattern($pattern, $result);
  132. $pattern = '/msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  133. $this->assertNoPattern($pattern, $result);
  134. $pattern = '/msgid "You have %d new message \(domain\)."\nmsgid_plural "You have %d new messages \(domain\)."/';
  135. $this->assertPattern($pattern, $result);
  136. $pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
  137. $this->assertPattern($pattern, $result);
  138. }
  139. /**
  140. * test exclusions
  141. *
  142. * @return void
  143. */
  144. public function testExtractWithExclude() {
  145. $this->Task->interactive = false;
  146. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS . 'View';
  147. $this->Task->params['output'] = $this->path . DS;
  148. $this->Task->params['exclude'] = 'Pages,Layouts';
  149. $this->Task->expects($this->any())->method('in')
  150. ->will($this->returnValue('y'));
  151. $this->Task->execute();
  152. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  153. $result = file_get_contents($this->path . DS . 'default.pot');
  154. $pattern = '/\#: .*extract\.ctp:6\n/';
  155. $this->assertNotRegExp($pattern, $result);
  156. $pattern = '/\#: .*default\.ctp:26\n/';
  157. $this->assertNotRegExp($pattern, $result);
  158. }
  159. /**
  160. * test extract can read more than one path.
  161. *
  162. * @return void
  163. */
  164. public function testExtractMultiplePaths() {
  165. $this->Task->interactive = false;
  166. $this->Task->params['paths'] =
  167. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages,' .
  168. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts';
  169. $this->Task->params['output'] = $this->path . DS;
  170. $this->Task->expects($this->never())->method('err');
  171. $this->Task->expects($this->never())->method('_stop');
  172. $this->Task->execute();
  173. $result = file_get_contents($this->path . DS . 'default.pot');
  174. $pattern = '/msgid "Add User"/';
  175. $this->assertPattern($pattern, $result);
  176. }
  177. /**
  178. * Tests that it is possible to exclude plugin paths by enabling the param option for the ExtractTask
  179. *
  180. * @return void
  181. */
  182. public function testExtractExcludePlugins() {
  183. App::build(array(
  184. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  185. ));
  186. $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  187. $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
  188. $this->Task = $this->getMock('ExtractTask',
  189. array('_isExtractingApp', '_extractValidationMessages', 'in', 'out', 'err', 'clear', '_stop'),
  190. array($this->out, $this->out, $this->in)
  191. );
  192. $this->Task->expects($this->exactly(2))->method('_isExtractingApp')->will($this->returnValue(true));
  193. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
  194. $this->Task->params['output'] = $this->path . DS;
  195. $this->Task->params['exclude-plugins'] = true;
  196. $this->Task->execute();
  197. $result = file_get_contents($this->path . DS . 'default.pot');
  198. $this->assertNoPattern('#TestPlugin#', $result);
  199. }
  200. /**
  201. * Test that is possible to extract messages form a single plugin
  202. *
  203. * @return void
  204. */
  205. public function testExtractPlugin() {
  206. App::build(array(
  207. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  208. ));
  209. $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  210. $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
  211. $this->Task = $this->getMock('ExtractTask',
  212. array('_isExtractingApp', '_extractValidationMessages', 'in', 'out', 'err', 'clear', '_stop'),
  213. array($this->out, $this->out, $this->in)
  214. );
  215. $this->Task->params['output'] = $this->path . DS;
  216. $this->Task->params['plugin'] = 'TestPlugin';
  217. $this->Task->execute();
  218. $result = file_get_contents($this->path . DS . 'default.pot');
  219. $this->assertNoPattern('#Pages#', $result);
  220. $this->assertContains('translate.ctp:1', $result);
  221. $this->assertContains('This is a translatable string', $result);
  222. }
  223. /**
  224. * Tests that the task will inspect application models and extract the validation messages from them
  225. *
  226. * @return void
  227. */
  228. public function testExtractModelValidation() {
  229. App::build(array(
  230. 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
  231. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  232. ), App::RESET);
  233. $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  234. $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
  235. $this->Task = $this->getMock('ExtractTask',
  236. array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
  237. array($this->out, $this->out, $this->in)
  238. );
  239. $this->Task->expects($this->exactly(2))->method('_isExtractingApp')->will($this->returnValue(true));
  240. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
  241. $this->Task->params['output'] = $this->path . DS;
  242. $this->Task->params['exclude-plugins'] = true;
  243. $this->Task->params['ignore-model-validation'] = false;
  244. $this->Task->execute();
  245. $result = file_get_contents($this->path . DS . 'default.pot');
  246. $pattern = preg_quote('#Model' . DS . 'PersisterOne.php:validation for field title#', '\\');
  247. $this->assertPattern($pattern, $result);
  248. $pattern = preg_quote('#Model' . DS . 'PersisterOne.php:validation for field body#', '\\');
  249. $this->assertPattern($pattern, $result);
  250. $pattern = '#msgid "Post title is required"#';
  251. $this->assertPattern($pattern, $result);
  252. $pattern = '#msgid "You may enter up to %s chars \(minimum is %s chars\)"#';
  253. $this->assertPattern($pattern, $result);
  254. $pattern = '#msgid "Post body is required"#';
  255. $this->assertPattern($pattern, $result);
  256. $pattern = '#msgid "Post body is super required"#';
  257. $this->assertPattern($pattern, $result);
  258. }
  259. /**
  260. * Tests that the task will inspect application models and extract the validation messages from them
  261. * while using a custom validation domain for the messages set on the model itself
  262. *
  263. * @return void
  264. */
  265. public function testExtractModelValidationWithDomainInModel() {
  266. App::build(array(
  267. 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Model' . DS)
  268. ));
  269. $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  270. $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
  271. $this->Task = $this->getMock('ExtractTask',
  272. array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
  273. array($this->out, $this->out, $this->in)
  274. );
  275. $this->Task->expects($this->exactly(2))->method('_isExtractingApp')->will($this->returnValue(true));
  276. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
  277. $this->Task->params['output'] = $this->path . DS;
  278. $this->Task->params['exclude-plugins'] = true;
  279. $this->Task->params['ignore-model-validation'] = false;
  280. $this->Task->execute();
  281. $result = file_get_contents($this->path . DS . 'test_plugin.pot');
  282. $pattern = preg_quote('#Plugin' . DS. 'TestPlugin' . DS. 'Model' . DS. 'TestPluginPost.php:validation for field title#', '\\');
  283. $this->assertPattern($pattern, $result);
  284. $pattern = preg_quote('#Plugin' . DS. 'TestPlugin' . DS. 'Model' . DS. 'TestPluginPost.php:validation for field body#', '\\');
  285. $this->assertPattern($pattern, $result);
  286. $pattern = '#msgid "Post title is required"#';
  287. $this->assertPattern($pattern, $result);
  288. $pattern = '#msgid "Post body is required"#';
  289. $this->assertPattern($pattern, $result);
  290. $pattern = '#msgid "Post body is super required"#';
  291. $this->assertPattern($pattern, $result);
  292. }
  293. /**
  294. * Test that the extract shell can obtain validation messages from models inside a specific plugin
  295. *
  296. * @return void
  297. */
  298. public function testExtractModelValidationInPlugin() {
  299. App::build(array(
  300. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  301. ));
  302. $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  303. $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
  304. $this->Task = $this->getMock('ExtractTask',
  305. array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
  306. array($this->out, $this->out, $this->in)
  307. );
  308. $this->Task->params['output'] = $this->path . DS;
  309. $this->Task->params['ignore-model-validation'] = false;
  310. $this->Task->params['plugin'] = 'TestPlugin';
  311. $this->Task->execute();
  312. $result = file_get_contents($this->path . DS . 'test_plugin.pot');
  313. $pattern = preg_quote('#Model' . DS. 'TestPluginPost.php:validation for field title#', '\\');
  314. $this->assertPattern($pattern, $result);
  315. $pattern = preg_quote('#Model' . DS. 'TestPluginPost.php:validation for field body#', '\\');
  316. $this->assertPattern($pattern, $result);
  317. $pattern = '#msgid "Post title is required"#';
  318. $this->assertPattern($pattern, $result);
  319. $pattern = '#msgid "Post body is required"#';
  320. $this->assertPattern($pattern, $result);
  321. $pattern = '#msgid "Post body is super required"#';
  322. $this->assertPattern($pattern, $result);
  323. $pattern = '#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field title#';
  324. $this->assertNoPattern($pattern, $result);
  325. }
  326. }