ExtractTaskTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. // extract.ctp - reading the domain.pot
  120. $result = file_get_contents($this->path . DS . 'domain.pot');
  121. $pattern = '/msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
  122. $this->assertNoPattern($pattern, $result);
  123. $pattern = '/msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
  124. $this->assertNoPattern($pattern, $result);
  125. $pattern = '/msgid "You have %d new message \(domain\)."\nmsgid_plural "You have %d new messages \(domain\)."/';
  126. $this->assertPattern($pattern, $result);
  127. $pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
  128. $this->assertPattern($pattern, $result);
  129. }
  130. /**
  131. * test exclusions
  132. *
  133. * @return void
  134. */
  135. function testExtractWithExclude() {
  136. $this->Task->interactive = false;
  137. $this->Task->params['paths'] = LIBS . 'tests' . DS . 'test_app' . DS . 'View';
  138. $this->Task->params['output'] = $this->path . DS;
  139. $this->Task->params['exclude'] = 'pages,layouts';
  140. $this->Task->expects($this->any())->method('in')
  141. ->will($this->returnValue('y'));
  142. $this->Task->execute();
  143. $this->assertTrue(file_exists($this->path . DS . 'default.pot'));
  144. $result = file_get_contents($this->path . DS . 'default.pot');
  145. $pattern = '/\#: .*extract\.ctp:6\n/';
  146. $this->assertNotRegExp($pattern, $result);
  147. $pattern = '/\#: .*default\.ctp:26\n/';
  148. $this->assertNotRegExp($pattern, $result);
  149. }
  150. /**
  151. * test extract can read more than one path.
  152. *
  153. * @return void
  154. */
  155. function testExtractMultiplePaths() {
  156. $this->Task->interactive = false;
  157. $this->Task->params['paths'] =
  158. LIBS . 'tests' . DS . 'test_app' . DS . 'View' . DS . 'pages,' .
  159. LIBS . 'tests' . DS . 'test_app' . DS . 'View' . DS . 'posts';
  160. $this->Task->params['output'] = $this->path . DS;
  161. $this->Task->expects($this->never())->method('err');
  162. $this->Task->expects($this->never())->method('_stop');
  163. $this->Task->execute();
  164. $result = file_get_contents($this->path . DS . 'default.pot');
  165. $pattern = '/msgid "Add User"/';
  166. $this->assertPattern($pattern, $result);
  167. }
  168. }