LanguagesTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace Shaarli;
  3. use Shaarli\Config\ConfigManager;
  4. /**
  5. * Class LanguagesTest.
  6. */
  7. class LanguagesTest extends \PHPUnit_Framework_TestCase
  8. {
  9. /**
  10. * @var string Config file path (without extension).
  11. */
  12. protected static $configFile = 'tests/utils/config/configJson';
  13. /**
  14. * @var ConfigManager
  15. */
  16. protected $conf;
  17. /**
  18. *
  19. */
  20. public function setUp()
  21. {
  22. $this->conf = new ConfigManager(self::$configFile);
  23. }
  24. /**
  25. * Test t() with a simple non identified value.
  26. */
  27. public function testTranslateSingleNotIDGettext()
  28. {
  29. $this->conf->set('translation.mode', 'gettext');
  30. new Languages('en', $this->conf);
  31. $text = 'abcdé 564 fgK';
  32. $this->assertEquals($text, t($text));
  33. }
  34. /**
  35. * Test t() with a simple identified value in gettext mode.
  36. */
  37. public function testTranslateSingleIDGettext()
  38. {
  39. $this->conf->set('translation.mode', 'gettext');
  40. new Languages('en', $this->conf);
  41. $text = 'permalink';
  42. $this->assertEquals($text, t($text));
  43. }
  44. /**
  45. * Test t() with a non identified plural form in gettext mode.
  46. */
  47. public function testTranslatePluralNotIDGettext()
  48. {
  49. $this->conf->set('translation.mode', 'gettext');
  50. new Languages('en', $this->conf);
  51. $text = 'sandwich';
  52. $nText = 'sandwiches';
  53. $this->assertEquals('sandwiches', t($text, $nText, 0));
  54. $this->assertEquals('sandwich', t($text, $nText, 1));
  55. $this->assertEquals('sandwiches', t($text, $nText, 2));
  56. }
  57. /**
  58. * Test t() with an identified plural form in gettext mode.
  59. */
  60. public function testTranslatePluralIDGettext()
  61. {
  62. $this->conf->set('translation.mode', 'gettext');
  63. new Languages('en', $this->conf);
  64. $text = 'shaare';
  65. $nText = 'shaares';
  66. // In english, zero is followed by plural form
  67. $this->assertEquals('shaares', t($text, $nText, 0));
  68. $this->assertEquals('shaare', t($text, $nText, 1));
  69. $this->assertEquals('shaares', t($text, $nText, 2));
  70. }
  71. /**
  72. * Test t() with a simple non identified value.
  73. */
  74. public function testTranslateSingleNotIDPhp()
  75. {
  76. $this->conf->set('translation.mode', 'php');
  77. new Languages('en', $this->conf);
  78. $text = 'abcdé 564 fgK';
  79. $this->assertEquals($text, t($text));
  80. }
  81. /**
  82. * Test t() with a simple identified value in PHP mode.
  83. */
  84. public function testTranslateSingleIDPhp()
  85. {
  86. $this->conf->set('translation.mode', 'php');
  87. new Languages('en', $this->conf);
  88. $text = 'permalink';
  89. $this->assertEquals($text, t($text));
  90. }
  91. /**
  92. * Test t() with a non identified plural form in PHP mode.
  93. */
  94. public function testTranslatePluralNotIDPhp()
  95. {
  96. $this->conf->set('translation.mode', 'php');
  97. new Languages('en', $this->conf);
  98. $text = 'sandwich';
  99. $nText = 'sandwiches';
  100. $this->assertEquals('sandwiches', t($text, $nText, 0));
  101. $this->assertEquals('sandwich', t($text, $nText, 1));
  102. $this->assertEquals('sandwiches', t($text, $nText, 2));
  103. }
  104. /**
  105. * Test t() with an identified plural form in PHP mode.
  106. */
  107. public function testTranslatePluralIDPhp()
  108. {
  109. $this->conf->set('translation.mode', 'php');
  110. new Languages('en', $this->conf);
  111. $text = 'shaare';
  112. $nText = 'shaares';
  113. // In english, zero is followed by plural form
  114. $this->assertEquals('shaares', t($text, $nText, 0));
  115. $this->assertEquals('shaare', t($text, $nText, 1));
  116. $this->assertEquals('shaares', t($text, $nText, 2));
  117. }
  118. /**
  119. * Test t() with an invalid language set in the configuration in gettext mode.
  120. */
  121. public function testTranslateWithInvalidConfLanguageGettext()
  122. {
  123. $this->conf->set('translation.mode', 'gettext');
  124. $this->conf->set('translation.language', 'nope');
  125. new Languages('fr', $this->conf);
  126. $text = 'grumble';
  127. $this->assertEquals($text, t($text));
  128. }
  129. /**
  130. * Test t() with an invalid language set in the configuration in PHP mode.
  131. */
  132. public function testTranslateWithInvalidConfLanguagePhp()
  133. {
  134. $this->conf->set('translation.mode', 'php');
  135. $this->conf->set('translation.language', 'nope');
  136. new Languages('fr', $this->conf);
  137. $text = 'grumble';
  138. $this->assertEquals($text, t($text));
  139. }
  140. /**
  141. * Test t() with an invalid language set with auto language in gettext mode.
  142. */
  143. public function testTranslateWithInvalidAutoLanguageGettext()
  144. {
  145. $this->conf->set('translation.mode', 'gettext');
  146. new Languages('nope', $this->conf);
  147. $text = 'grumble';
  148. $this->assertEquals($text, t($text));
  149. }
  150. /**
  151. * Test t() with an invalid language set with auto language in PHP mode.
  152. */
  153. public function testTranslateWithInvalidAutoLanguagePhp()
  154. {
  155. $this->conf->set('translation.mode', 'php');
  156. new Languages('nope', $this->conf);
  157. $text = 'grumble';
  158. $this->assertEquals($text, t($text));
  159. }
  160. /**
  161. * Test t() with an extension language file coming from the theme in gettext mode
  162. */
  163. public function testTranslationThemeExtensionGettext()
  164. {
  165. $this->conf->set('translation.mode', 'gettext');
  166. $this->conf->set('raintpl_tpl', 'tests/utils/customtpl/');
  167. $this->conf->set('theme', 'dummy');
  168. new Languages('en', $this->conf);
  169. $txt = 'rooster'; // ignore me poedit
  170. $this->assertEquals('rooster', t($txt, $txt, 1, 'dummy'));
  171. }
  172. /**
  173. * Test t() with an extension language file coming from the theme in PHP mode
  174. */
  175. public function testTranslationThemeExtensionPhp()
  176. {
  177. $this->conf->set('translation.mode', 'php');
  178. $this->conf->set('raintpl_tpl', 'tests/utils/customtpl/');
  179. $this->conf->set('theme', 'dummy');
  180. new Languages('en', $this->conf);
  181. $txt = 'rooster'; // ignore me poedit
  182. $this->assertEquals('rooster', t($txt, $txt, 1, 'dummy'));
  183. }
  184. /**
  185. * Test t() with an extension language file in gettext mode
  186. */
  187. public function testTranslationExtensionGettext()
  188. {
  189. $this->conf->set('translation.mode', 'gettext');
  190. $this->conf->set('translation.extensions.test', 'tests/utils/languages/');
  191. new Languages('en', $this->conf);
  192. $txt = 'car'; // ignore me poedit
  193. $this->assertEquals('car', t($txt, $txt, 1, 'test'));
  194. $this->assertEquals('Search', t('Search', 'Search', 1, 'test'));
  195. }
  196. /**
  197. * Test t() with an extension language file in PHP mode
  198. */
  199. public function testTranslationExtensionPhp()
  200. {
  201. $this->conf->set('translation.mode', 'php');
  202. $this->conf->set('translation.extensions.test', 'tests/utils/languages/');
  203. new Languages('en', $this->conf);
  204. $txt = 'car'; // ignore me poedit
  205. $this->assertEquals('car', t($txt, $txt, 1, 'test'));
  206. $this->assertEquals('Search', t('Search', 'Search', 1, 'test'));
  207. }
  208. }