ExtractTaskTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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.tests.cases.console.libs.tasks
  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.tests.cases.console.libs.tasks
  29. */
  30. class ExtractTaskTest extends CakeTestCase {
  31. /**
  32. * setUp method
  33. *
  34. * @return void
  35. */
  36. public function setUp() {
  37. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  38. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  39. $this->Task = $this->getMock(
  40. 'ExtractTask',
  41. array('in', 'out', 'err', '_stop'),
  42. array($out, $out, $in)
  43. );
  44. $this->path = TMP . 'tests' . DS . 'extract_task_test';
  45. $Folder = new Folder($this->path . DS . 'locale', true);
  46. }
  47. /**
  48. * tearDown method
  49. *
  50. * @return void
  51. */
  52. public function tearDown() {
  53. parent::tearDown();
  54. unset($this->Task);
  55. $Folder = new Folder($this->path);
  56. $Folder->delete();
  57. App::build();
  58. }
  59. /**
  60. * testExecute method
  61. *
  62. * @return void
  63. */
  64. public function testExecute() {
  65. $this->Task->interactive = false;
  66. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages';
  67. $this->Task->params['output'] = $this->path . DS;
  68. $this->Task->expects($this->never())->method('err');
  69. $this->Task->expects($this->any())->method('in')
  70. ->will($this->returnValue('y'));
  71. $this->Task->expects($this->never())->method('_stop');
  72. $this->Task->execute();
  73. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  74. $result = file_get_contents($this->path . DS . 'default.pot');
  75. $pattern = '/"Content-Type\: text\/plain; charset\=utf-8/';
  76. $this->assertPattern($pattern, $result);
  77. $pattern = '/"Content-Transfer-Encoding\: 8bit/';
  78. $this->assertPattern($pattern, $result);
  79. $pattern = '/"Plural-Forms\: nplurals\=INTEGER; plural\=EXPRESSION;/';
  80. $this->assertPattern($pattern, $result);
  81. // home.ctp
  82. $pattern = '/msgid "Your tmp directory is writable."\nmsgstr ""\n/';
  83. $this->assertPattern($pattern, $result);
  84. $pattern = '/msgid "Your tmp directory is NOT writable."\nmsgstr ""\n/';
  85. $this->assertPattern($pattern, $result);
  86. $pattern = '/msgid "The %s is being used for caching. To change the config edit ';
  87. $pattern .= 'APP\/config\/core.php "\nmsgstr ""\n/';
  88. $this->assertPattern($pattern, $result);
  89. $pattern = '/msgid "Your cache is NOT working. Please check ';
  90. $pattern .= 'the settings in APP\/config\/core.php"\nmsgstr ""\n/';
  91. $this->assertPattern($pattern, $result);
  92. $pattern = '/msgid "Your database configuration file is present."\nmsgstr ""\n/';
  93. $this->assertPattern($pattern, $result);
  94. $pattern = '/msgid "Your database configuration file is NOT present."\nmsgstr ""\n/';
  95. $this->assertPattern($pattern, $result);
  96. $pattern = '/msgid "Rename config\/database.php.default to ';
  97. $pattern .= 'config\/database.php"\nmsgstr ""\n/';
  98. $this->assertPattern($pattern, $result);
  99. $pattern = '/msgid "Cake is able to connect to the database."\nmsgstr ""\n/';
  100. $this->assertPattern($pattern, $result);
  101. $pattern = '/msgid "Cake is NOT able to connect to the database."\nmsgstr ""\n/';
  102. $this->assertPattern($pattern, $result);
  103. $pattern = '/msgid "Editing this Page"\nmsgstr ""\n/';
  104. $this->assertPattern($pattern, $result);
  105. $pattern = '/msgid "To change the content of this page, create: APP\/views\/pages\/home\.ctp/';
  106. $this->assertPattern($pattern, $result);
  107. $pattern = '/To change its layout, create: APP\/views\/layouts\/default\.ctp\./s';
  108. $this->assertPattern($pattern, $result);
  109. // extract.ctp
  110. $pattern = '/\#: (\\\\|\/)extract\.ctp:6\n';
  111. $pattern .= 'msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  112. $this->assertPattern($pattern, $result);
  113. $pattern = '/\#: (\\\\|\/)extract\.ctp:7\n';
  114. $pattern .= 'msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  115. $this->assertPattern($pattern, $result);
  116. $pattern = '/\#: (\\\\|\/)extract\.ctp:14\n';
  117. $pattern .= '\#: (\\\\|\/)home\.ctp:99\n';
  118. $pattern .= 'msgid "Editing this Page"\nmsgstr ""/';
  119. $this->assertPattern($pattern, $result);
  120. $pattern = '/\#: (\\\\|\/)extract\.ctp:17\nmsgid "';
  121. $pattern .= 'Hot features!';
  122. $pattern .= '\\\n - No Configuration: Set-up the database and let the magic begin';
  123. $pattern .= '\\\n - Extremely Simple: Just look at the name...It\'s Cake';
  124. $pattern .= '\\\n - Active, Friendly Community: Join us #cakephp on IRC. We\'d love to help you get started';
  125. $pattern .= '"\nmsgstr ""/';
  126. $this->assertPattern($pattern, $result);
  127. // extract.ctp - reading the domain.pot
  128. $result = file_get_contents($this->path . DS . 'domain.pot');
  129. $pattern = '/msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  130. $this->assertNoPattern($pattern, $result);
  131. $pattern = '/msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  132. $this->assertNoPattern($pattern, $result);
  133. $pattern = '/msgid "You have %d new message \(domain\)."\nmsgid_plural "You have %d new messages \(domain\)."/';
  134. $this->assertPattern($pattern, $result);
  135. $pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
  136. $this->assertPattern($pattern, $result);
  137. }
  138. /**
  139. * test exclusions
  140. *
  141. * @return void
  142. */
  143. public function testExtractWithExclude() {
  144. $this->Task->interactive = false;
  145. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS . 'View';
  146. $this->Task->params['output'] = $this->path . DS;
  147. $this->Task->params['exclude'] = 'Pages,Layouts';
  148. $this->Task->expects($this->any())->method('in')
  149. ->will($this->returnValue('y'));
  150. $this->Task->execute();
  151. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  152. $result = file_get_contents($this->path . DS . 'default.pot');
  153. $pattern = '/\#: .*extract\.ctp:6\n/';
  154. $this->assertNotRegExp($pattern, $result);
  155. $pattern = '/\#: .*default\.ctp:26\n/';
  156. $this->assertNotRegExp($pattern, $result);
  157. }
  158. /**
  159. * test extract can read more than one path.
  160. *
  161. * @return void
  162. */
  163. public function testExtractMultiplePaths() {
  164. $this->Task->interactive = false;
  165. $this->Task->params['paths'] =
  166. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages,' .
  167. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts';
  168. $this->Task->params['output'] = $this->path . DS;
  169. $this->Task->expects($this->never())->method('err');
  170. $this->Task->expects($this->never())->method('_stop');
  171. $this->Task->execute();
  172. $result = file_get_contents($this->path . DS . 'default.pot');
  173. $pattern = '/msgid "Add User"/';
  174. $this->assertPattern($pattern, $result);
  175. }
  176. /**
  177. * Tests that it is possible to exclude plugin paths by enabling the param option for the ExtractTask
  178. *
  179. * @return void
  180. */
  181. public function testExtractExcludePlugins() {
  182. App::build(array(
  183. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  184. ));
  185. $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  186. $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
  187. $this->Task = $this->getMock('ExtractTask',
  188. array('_isExtractingApp', '_extractValidationMessages', 'in', 'out', 'err', 'clear', '_stop'),
  189. array($this->out, $this->out, $this->in)
  190. );
  191. $this->Task->expects($this->exactly(2))->method('_isExtractingApp')->will($this->returnValue(true));
  192. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
  193. $this->Task->params['output'] = $this->path . DS;
  194. $this->Task->params['exclude-plugins'] = true;
  195. $this->Task->execute();
  196. $result = file_get_contents($this->path . DS . 'default.pot');
  197. $this->assertNoPattern('#TesPlugin#', $result);
  198. }
  199. /**
  200. * Tests that the task will inspect application models and extract the validation messages from them
  201. *
  202. * @return void
  203. */
  204. public function testExtractModelValidation() {
  205. App::build(array(
  206. 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
  207. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  208. ), App::RESET);
  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', 'in', 'out', 'err', 'clear', '_stop'),
  213. array($this->out, $this->out, $this->in)
  214. );
  215. $this->Task->expects($this->exactly(2))->method('_isExtractingApp')->will($this->returnValue(true));
  216. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
  217. $this->Task->params['output'] = $this->path . DS;
  218. $this->Task->params['exclude-plugins'] = true;
  219. $this->Task->params['ignore-model-validation'] = false;
  220. $this->Task->execute();
  221. $result = file_get_contents($this->path . DS . 'default.pot');
  222. $pattern = '#Model/PersisterOne.php:validation for field title#';
  223. $this->assertPattern($pattern, $result);
  224. $pattern = '#Model/PersisterOne.php:validation for field body#';
  225. $this->assertPattern($pattern, $result);
  226. $pattern = '#msgid "Post title is required"#';
  227. $this->assertPattern($pattern, $result);
  228. $pattern = '#msgid "Post body is required"#';
  229. $this->assertPattern($pattern, $result);
  230. $pattern = '#msgid "Post body is super required"#';
  231. $this->assertPattern($pattern, $result);
  232. }
  233. /**
  234. * Tests that the task will inspect application models and extract the validation messages from them
  235. * while using a custom validation domain for the messages set on the model itself
  236. *
  237. * @return void
  238. */
  239. public function testExtractModelValidationWithDomainInModel() {
  240. App::build(array(
  241. 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Model' . DS)
  242. ));
  243. $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  244. $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
  245. $this->Task = $this->getMock('ExtractTask',
  246. array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
  247. array($this->out, $this->out, $this->in)
  248. );
  249. $this->Task->expects($this->exactly(2))->method('_isExtractingApp')->will($this->returnValue(true));
  250. $this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
  251. $this->Task->params['output'] = $this->path . DS;
  252. $this->Task->params['exclude-plugins'] = true;
  253. $this->Task->params['ignore-model-validation'] = false;
  254. $this->Task->execute();
  255. $result = file_get_contents($this->path . DS . 'test_plugin.pot');
  256. $pattern = '#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field title#';
  257. $this->assertPattern($pattern, $result);
  258. $pattern = '#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field body#';
  259. $this->assertPattern($pattern, $result);
  260. $pattern = '#msgid "Post title is required"#';
  261. $this->assertPattern($pattern, $result);
  262. $pattern = '#msgid "Post body is required"#';
  263. $this->assertPattern($pattern, $result);
  264. $pattern = '#msgid "Post body is super required"#';
  265. $this->assertPattern($pattern, $result);
  266. }
  267. }