ExtractTaskTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 2006-2010, 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 2006-2010, 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. }
  58. /**
  59. * testExecute method
  60. *
  61. * @return void
  62. */
  63. public function testExecute() {
  64. $this->Task->interactive = false;
  65. $this->Task->params['paths'] = LIBS . 'tests' . DS . 'test_app' . DS . 'View' . DS . 'pages';
  66. $this->Task->params['output'] = $this->path . DS;
  67. $this->Task->expects($this->never())->method('err');
  68. $this->Task->expects($this->any())->method('in')
  69. ->will($this->returnValue('y'));
  70. $this->Task->expects($this->never())->method('_stop');
  71. $this->Task->execute();
  72. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  73. $result = file_get_contents($this->path . DS . 'default.pot');
  74. $pattern = '/"Content-Type\: text\/plain; charset\=utf-8/';
  75. $this->assertPattern($pattern, $result);
  76. $pattern = '/"Content-Transfer-Encoding\: 8bit/';
  77. $this->assertPattern($pattern, $result);
  78. $pattern = '/"Plural-Forms\: nplurals\=INTEGER; plural\=EXPRESSION;/';
  79. $this->assertPattern($pattern, $result);
  80. // home.ctp
  81. $pattern = '/msgid "Your tmp directory is writable."\nmsgstr ""\n/';
  82. $this->assertPattern($pattern, $result);
  83. $pattern = '/msgid "Your tmp directory is NOT writable."\nmsgstr ""\n/';
  84. $this->assertPattern($pattern, $result);
  85. $pattern = '/msgid "The %s is being used for caching. To change the config edit ';
  86. $pattern .= 'APP\/config\/core.php "\nmsgstr ""\n/';
  87. $this->assertPattern($pattern, $result);
  88. $pattern = '/msgid "Your cache is NOT working. Please check ';
  89. $pattern .= 'the settings in APP\/config\/core.php"\nmsgstr ""\n/';
  90. $this->assertPattern($pattern, $result);
  91. $pattern = '/msgid "Your database configuration file is present."\nmsgstr ""\n/';
  92. $this->assertPattern($pattern, $result);
  93. $pattern = '/msgid "Your database configuration file is NOT present."\nmsgstr ""\n/';
  94. $this->assertPattern($pattern, $result);
  95. $pattern = '/msgid "Rename config\/database.php.default to ';
  96. $pattern .= 'config\/database.php"\nmsgstr ""\n/';
  97. $this->assertPattern($pattern, $result);
  98. $pattern = '/msgid "Cake is able to connect to the database."\nmsgstr ""\n/';
  99. $this->assertPattern($pattern, $result);
  100. $pattern = '/msgid "Cake is NOT able to connect to the database."\nmsgstr ""\n/';
  101. $this->assertPattern($pattern, $result);
  102. $pattern = '/msgid "Editing this Page"\nmsgstr ""\n/';
  103. $this->assertPattern($pattern, $result);
  104. $pattern = '/msgid "To change the content of this page, create: APP\/views\/pages\/home\.ctp/';
  105. $this->assertPattern($pattern, $result);
  106. $pattern = '/To change its layout, create: APP\/views\/layouts\/default\.ctp\./s';
  107. $this->assertPattern($pattern, $result);
  108. // extract.ctp
  109. $pattern = '/\#: (\\\\|\/)extract\.ctp:6\n';
  110. $pattern .= 'msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  111. $this->assertPattern($pattern, $result);
  112. $pattern = '/\#: (\\\\|\/)extract\.ctp:7\n';
  113. $pattern .= 'msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  114. $this->assertPattern($pattern, $result);
  115. $pattern = '/\#: (\\\\|\/)extract\.ctp:14\n';
  116. $pattern .= '\#: (\\\\|\/)home\.ctp:99\n';
  117. $pattern .= 'msgid "Editing this Page"\nmsgstr ""/';
  118. $this->assertPattern($pattern, $result);
  119. $pattern = '/\#: (\\\\|\/)extract\.ctp:17\nmsgid "';
  120. $pattern .= 'Hot features!';
  121. $pattern .= '\\\n - No Configuration: Set-up the database and let the magic begin';
  122. $pattern .= '\\\n - Extremely Simple: Just look at the name...It\'s Cake';
  123. $pattern .= '\\\n - Active, Friendly Community: Join us #cakephp on IRC. We\'d love to help you get started';
  124. $pattern .= '"\nmsgstr ""/';
  125. $this->assertPattern($pattern, $result);
  126. // extract.ctp - reading the domain.pot
  127. $result = file_get_contents($this->path . DS . 'domain.pot');
  128. $pattern = '/msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  129. $this->assertNoPattern($pattern, $result);
  130. $pattern = '/msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  131. $this->assertNoPattern($pattern, $result);
  132. $pattern = '/msgid "You have %d new message \(domain\)."\nmsgid_plural "You have %d new messages \(domain\)."/';
  133. $this->assertPattern($pattern, $result);
  134. $pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
  135. $this->assertPattern($pattern, $result);
  136. }
  137. /**
  138. * test exclusions
  139. *
  140. * @return void
  141. */
  142. function testExtractWithExclude() {
  143. $this->Task->interactive = false;
  144. $this->Task->params['paths'] = LIBS . 'tests' . DS . 'test_app' . DS . 'View';
  145. $this->Task->params['output'] = $this->path . DS;
  146. $this->Task->params['exclude'] = 'pages,layouts';
  147. $this->Task->expects($this->any())->method('in')
  148. ->will($this->returnValue('y'));
  149. $this->Task->execute();
  150. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  151. $result = file_get_contents($this->path . DS . 'default.pot');
  152. $pattern = '/\#: .*extract\.ctp:6\n/';
  153. $this->assertNotRegExp($pattern, $result);
  154. $pattern = '/\#: .*default\.ctp:26\n/';
  155. $this->assertNotRegExp($pattern, $result);
  156. }
  157. /**
  158. * test extract can read more than one path.
  159. *
  160. * @return void
  161. */
  162. function testExtractMultiplePaths() {
  163. $this->Task->interactive = false;
  164. $this->Task->params['paths'] =
  165. LIBS . 'tests' . DS . 'test_app' . DS . 'View' . DS . 'pages,' .
  166. LIBS . 'tests' . DS . 'test_app' . DS . 'View' . DS . 'posts';
  167. $this->Task->params['output'] = $this->path . DS;
  168. $this->Task->expects($this->never())->method('err');
  169. $this->Task->expects($this->never())->method('_stop');
  170. $this->Task->execute();
  171. $result = file_get_contents($this->path . DS . 'default.pot');
  172. $pattern = '/msgid "Add User"/';
  173. $this->assertPattern($pattern, $result);
  174. }
  175. }