PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/TestCase/Shell/Task/ExtractTaskTest.php

http://github.com/cakephp/cakephp
PHP | 360 lines | 224 code | 58 blank | 78 comment | 0 complexity | af5ab3ac4d288dbccc5167fb2d2e1d77 MD5 | raw file
Possible License(s): JSON
  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\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\Filesystem\Folder;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * ExtractTaskTest class
  22. */
  23. class ExtractTaskTest extends TestCase
  24. {
  25. /**
  26. * setUp method
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. $this->io = $this->getMockBuilder('Cake\Console\ConsoleIo')
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $progress = $this->getMockBuilder('Cake\Shell\Helper\ProgressHelper')
  37. ->setConstructorArgs([$this->io])
  38. ->getMock();
  39. $this->io->method('helper')
  40. ->will($this->returnValue($progress));
  41. $this->Task = $this->getMockBuilder('Cake\Shell\Task\ExtractTask')
  42. ->setMethods(['in', 'out', 'err', '_stop'])
  43. ->setConstructorArgs([$this->io])
  44. ->getMock();
  45. $this->path = TMP . 'tests/extract_task_test';
  46. new Folder($this->path . DS . 'locale', true);
  47. }
  48. /**
  49. * tearDown method
  50. *
  51. * @return void
  52. */
  53. public function tearDown()
  54. {
  55. parent::tearDown();
  56. unset($this->Task);
  57. $Folder = new Folder($this->path);
  58. $Folder->delete();
  59. Plugin::unload();
  60. }
  61. /**
  62. * testExecute method
  63. *
  64. * @return void
  65. */
  66. public function testExecute()
  67. {
  68. $this->Task->params['paths'] = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages';
  69. $this->Task->params['output'] = $this->path . DS;
  70. $this->Task->params['extract-core'] = 'no';
  71. $this->Task->params['merge'] = 'no';
  72. $this->Task->expects($this->never())->method('err');
  73. $this->Task->expects($this->any())->method('in')
  74. ->will($this->returnValue('y'));
  75. $this->Task->expects($this->never())->method('_stop');
  76. $this->Task->main();
  77. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  78. $result = file_get_contents($this->path . DS . 'default.pot');
  79. $this->assertFalse(file_exists($this->path . DS . 'cake.pot'));
  80. // extract.ctp
  81. $pattern = '/\#: Template[\/\\\\]Pages[\/\\\\]extract\.ctp:\d+;\d+\n';
  82. $pattern .= 'msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  83. $this->assertRegExp($pattern, $result);
  84. $pattern = '/msgid "You have %d new message."\nmsgstr ""/';
  85. $this->assertNotRegExp($pattern, $result, 'No duplicate msgid');
  86. $pattern = '/\#: Template[\/\\\\]Pages[\/\\\\]extract\.ctp:\d+\n';
  87. $pattern .= 'msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  88. $this->assertRegExp($pattern, $result);
  89. $pattern = '/\#: Template[\/\\\\]Pages[\/\\\\]extract\.ctp:\d+\nmsgid "';
  90. $pattern .= 'Hot features!';
  91. $pattern .= '\\\n - No Configuration: Set-up the database and let the magic begin';
  92. $pattern .= '\\\n - Extremely Simple: Just look at the name...It\'s Cake';
  93. $pattern .= '\\\n - Active, Friendly Community: Join us #cakephp on IRC. We\'d love to help you get started';
  94. $pattern .= '"\nmsgstr ""/';
  95. $this->assertRegExp($pattern, $result);
  96. $this->assertContains('msgid "double \\"quoted\\""', $result, 'Strings with quotes not handled correctly');
  97. $this->assertContains("msgid \"single 'quoted'\"", $result, 'Strings with quotes not handled correctly');
  98. $pattern = '/\#: Template[\/\\\\]Pages[\/\\\\]extract\.ctp:\d+\n';
  99. $pattern .= 'msgctxt "mail"\n';
  100. $pattern .= 'msgid "letter"/';
  101. $this->assertRegExp($pattern, $result);
  102. $pattern = '/\#: Template[\/\\\\]Pages[\/\\\\]extract\.ctp:\d+\n';
  103. $pattern .= 'msgctxt "alphabet"\n';
  104. $pattern .= 'msgid "letter"/';
  105. $this->assertRegExp($pattern, $result);
  106. // extract.ctp - reading the domain.pot
  107. $result = file_get_contents($this->path . DS . 'domain.pot');
  108. $pattern = '/msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  109. $this->assertNotRegExp($pattern, $result);
  110. $pattern = '/msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  111. $this->assertNotRegExp($pattern, $result);
  112. $pattern = '/msgid "You have %d new message \(domain\)."\nmsgid_plural "You have %d new messages \(domain\)."/';
  113. $this->assertRegExp($pattern, $result);
  114. $pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
  115. $this->assertRegExp($pattern, $result);
  116. }
  117. /**
  118. * testExecute with merging on method
  119. *
  120. * @return void
  121. */
  122. public function testExecuteMerge()
  123. {
  124. $this->Task->params['paths'] = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages';
  125. $this->Task->params['output'] = $this->path . DS;
  126. $this->Task->params['extract-core'] = 'no';
  127. $this->Task->params['merge'] = 'yes';
  128. $this->Task->expects($this->never())->method('err');
  129. $this->Task->expects($this->any())->method('in')
  130. ->will($this->returnValue('y'));
  131. $this->Task->expects($this->never())->method('_stop');
  132. $this->Task->main();
  133. $this->assertFileExists($this->path . DS . 'default.pot');
  134. $this->assertFileNotExists($this->path . DS . 'cake.pot');
  135. $this->assertFileNotExists($this->path . DS . 'domain.pot');
  136. }
  137. /**
  138. * test exclusions
  139. *
  140. * @return void
  141. */
  142. public function testExtractWithExclude()
  143. {
  144. $this->Task->interactive = false;
  145. $this->Task->params['paths'] = TEST_APP . 'TestApp/Template';
  146. $this->Task->params['output'] = $this->path . DS;
  147. $this->Task->params['exclude'] = 'Pages,Layout';
  148. $this->Task->params['extract-core'] = 'no';
  149. $this->Task->expects($this->any())->method('in')
  150. ->will($this->returnValue('y'));
  151. $this->Task->main();
  152. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  153. $result = file_get_contents($this->path . DS . 'default.pot');
  154. $pattern = '/\#: .*extract\.ctp:\d+\n/';
  155. $this->assertNotRegExp($pattern, $result);
  156. $pattern = '/\#: .*default\.ctp:\d+\n/';
  157. $this->assertNotRegExp($pattern, $result);
  158. }
  159. /**
  160. * testExtractWithoutLocations method
  161. *
  162. * @return void
  163. */
  164. public function testExtractWithoutLocations()
  165. {
  166. $this->Task->params['paths'] = TEST_APP . 'TestApp/Template';
  167. $this->Task->params['output'] = $this->path . DS;
  168. $this->Task->params['exclude'] = 'Pages,Layout';
  169. $this->Task->params['extract-core'] = 'no';
  170. $this->Task->params['no-location'] = true;
  171. $this->Task->expects($this->never())->method('err');
  172. $this->Task->expects($this->any())->method('in')
  173. ->will($this->returnValue('y'));
  174. $this->Task->main();
  175. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  176. $result = file_get_contents($this->path . DS . 'default.pot');
  177. $pattern = '/\n\#: .*\n/';
  178. $this->assertNotRegExp($pattern, $result);
  179. }
  180. /**
  181. * test extract can read more than one path.
  182. *
  183. * @return void
  184. */
  185. public function testExtractMultiplePaths()
  186. {
  187. $this->Task->interactive = false;
  188. $this->Task->params['paths'] =
  189. TEST_APP . 'TestApp/Template/Pages,' .
  190. TEST_APP . 'TestApp/Template/Posts';
  191. $this->Task->params['output'] = $this->path . DS;
  192. $this->Task->params['extract-core'] = 'no';
  193. $this->Task->expects($this->never())->method('err');
  194. $this->Task->expects($this->never())->method('_stop');
  195. $this->Task->main();
  196. $result = file_get_contents($this->path . DS . 'default.pot');
  197. $pattern = '/msgid "Add User"/';
  198. $this->assertRegExp($pattern, $result);
  199. }
  200. /**
  201. * Tests that it is possible to exclude plugin paths by enabling the param option for the ExtractTask
  202. *
  203. * @return void
  204. */
  205. public function testExtractExcludePlugins()
  206. {
  207. Configure::write('App.namespace', 'TestApp');
  208. $this->Task = $this->getMockBuilder('Cake\Shell\Task\ExtractTask')
  209. ->setMethods(['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'])
  210. ->setConstructorArgs([$this->io])
  211. ->getMock();
  212. $this->Task->expects($this->exactly(1))
  213. ->method('_isExtractingApp')
  214. ->will($this->returnValue(true));
  215. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  216. $this->Task->params['output'] = $this->path . DS;
  217. $this->Task->params['exclude-plugins'] = true;
  218. $this->Task->main();
  219. $result = file_get_contents($this->path . DS . 'default.pot');
  220. $this->assertNotRegExp('#TestPlugin#', $result);
  221. }
  222. /**
  223. * Test that is possible to extract messages from a single plugin
  224. *
  225. * @return void
  226. */
  227. public function testExtractPlugin()
  228. {
  229. Configure::write('App.namespace', 'TestApp');
  230. $this->Task = $this->getMockBuilder('Cake\Shell\Task\ExtractTask')
  231. ->setMethods(['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'])
  232. ->setConstructorArgs([$this->io])
  233. ->getMock();
  234. $this->Task->params['output'] = $this->path . DS;
  235. $this->Task->params['plugin'] = 'TestPlugin';
  236. $this->Task->main();
  237. $result = file_get_contents($this->path . DS . 'default.pot');
  238. $this->assertNotRegExp('#Pages#', $result);
  239. $this->assertRegExp('/translate\.ctp:\d+/', $result);
  240. $this->assertContains('This is a translatable string', $result);
  241. }
  242. /**
  243. * Test that is possible to extract messages from a vendored plugin.
  244. *
  245. * @return void
  246. */
  247. public function testExtractVendoredPlugin()
  248. {
  249. Configure::write('App.namespace', 'TestApp');
  250. $this->Task = $this->getMockBuilder('Cake\Shell\Task\ExtractTask')
  251. ->setMethods(['_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'])
  252. ->setConstructorArgs([$this->io])
  253. ->getMock();
  254. $this->Task->params['output'] = $this->path . DS;
  255. $this->Task->params['plugin'] = 'Company/TestPluginThree';
  256. $this->Task->main();
  257. $result = file_get_contents($this->path . DS . 'test_plugin_three.pot');
  258. $this->assertNotRegExp('#Pages#', $result);
  259. $this->assertRegExp('/default\.ctp:\d+/', $result);
  260. $this->assertContains('A vendor message', $result);
  261. }
  262. /**
  263. * Test that the extract shell overwrites existing files with the overwrite parameter
  264. *
  265. * @return void
  266. */
  267. public function testExtractOverwrite()
  268. {
  269. Configure::write('App.namespace', 'TestApp');
  270. $this->Task->interactive = false;
  271. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  272. $this->Task->params['output'] = $this->path . DS;
  273. $this->Task->params['extract-core'] = 'no';
  274. $this->Task->params['overwrite'] = true;
  275. file_put_contents($this->path . DS . 'default.pot', 'will be overwritten');
  276. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  277. $original = file_get_contents($this->path . DS . 'default.pot');
  278. $this->Task->main();
  279. $result = file_get_contents($this->path . DS . 'default.pot');
  280. $this->assertNotEquals($original, $result);
  281. }
  282. /**
  283. * Test that the extract shell scans the core libs
  284. *
  285. * @return void
  286. */
  287. public function testExtractCore()
  288. {
  289. Configure::write('App.namespace', 'TestApp');
  290. $this->Task->interactive = false;
  291. $this->Task->params['paths'] = TEST_APP . 'TestApp/';
  292. $this->Task->params['output'] = $this->path . DS;
  293. $this->Task->params['extract-core'] = 'yes';
  294. $this->Task->main();
  295. $this->assertTrue(file_exists($this->path . DS . 'cake.pot'));
  296. $result = file_get_contents($this->path . DS . 'cake.pot');
  297. $pattern = '/#: Console\/Templates\//';
  298. $this->assertNotRegExp($pattern, $result);
  299. $pattern = '/#: Test\//';
  300. $this->assertNotRegExp($pattern, $result);
  301. }
  302. }