LangTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /* Copyright (C) 2013 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file test/phpunit/LangTest.php
  20. * \ingroup test
  21. * \brief PHPUnit test
  22. * \remarks To run this script as CLI: phpunit filename.php
  23. */
  24. global $conf,$user,$langs,$db;
  25. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  26. //require_once 'PHPUnit/Autoload.php';
  27. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  28. require_once dirname(__FILE__).'/../../htdocs/core/lib/security.lib.php';
  29. require_once dirname(__FILE__).'/../../htdocs/core/lib/security2.lib.php';
  30. if (! defined('NOREQUIREUSER')) {
  31. define('NOREQUIREUSER', '1');
  32. }
  33. if (! defined('NOREQUIREDB')) {
  34. define('NOREQUIREDB', '1');
  35. }
  36. if (! defined('NOREQUIRESOC')) {
  37. define('NOREQUIRESOC', '1');
  38. }
  39. if (! defined('NOREQUIRETRAN')) {
  40. define('NOREQUIRETRAN', '1');
  41. }
  42. if (! defined('NOCSRFCHECK')) {
  43. define('NOCSRFCHECK', '1');
  44. }
  45. if (! defined('NOTOKENRENEWAL')) {
  46. define('NOTOKENRENEWAL', '1');
  47. }
  48. if (! defined('NOREQUIREMENU')) {
  49. define('NOREQUIREMENU', '1'); // If there is no menu to show
  50. }
  51. if (! defined('NOREQUIREHTML')) {
  52. define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  53. }
  54. if (! defined('NOREQUIREAJAX')) {
  55. define('NOREQUIREAJAX', '1');
  56. }
  57. if (! defined("NOLOGIN")) {
  58. define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
  59. }
  60. if (empty($user->id)) {
  61. print "Load permissions for admin user nb 1\n";
  62. $user->fetch(1);
  63. $user->getrights();
  64. }
  65. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  66. /**
  67. * Class for PHPUnit tests
  68. *
  69. * @backupGlobals disabled
  70. * @backupStaticAttributes enabled
  71. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  72. */
  73. class LangTest extends PHPUnit\Framework\TestCase
  74. {
  75. protected $savconf;
  76. protected $savuser;
  77. protected $savlangs;
  78. protected $savdb;
  79. /**
  80. * Constructor
  81. * We save global variables into local variables
  82. *
  83. * @return SecurityTest
  84. */
  85. public function __construct()
  86. {
  87. parent::__construct();
  88. //$this->sharedFixture
  89. global $conf,$user,$langs,$db;
  90. $this->savconf=$conf;
  91. $this->savuser=$user;
  92. $this->savlangs=$langs;
  93. $this->savdb=$db;
  94. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  95. //print " - db ".$db->db;
  96. print "\n";
  97. }
  98. /**
  99. * setUpBeforeClass
  100. *
  101. * @return void
  102. */
  103. public static function setUpBeforeClass()
  104. {
  105. global $conf,$user,$langs,$db;
  106. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  107. print __METHOD__."\n";
  108. }
  109. /**
  110. * tearDownAfterClass
  111. *
  112. * @return void
  113. */
  114. public static function tearDownAfterClass()
  115. {
  116. global $conf,$user,$langs,$db;
  117. $db->rollback();
  118. print __METHOD__."\n";
  119. }
  120. /**
  121. * Init phpunit tests
  122. *
  123. * @return void
  124. */
  125. protected function setUp()
  126. {
  127. global $conf,$user,$langs,$db;
  128. $conf=$this->savconf;
  129. $user=$this->savuser;
  130. $langs=$this->savlangs;
  131. $db=$this->savdb;
  132. print __METHOD__."\n";
  133. }
  134. /**
  135. * End phpunit tests
  136. *
  137. * @return void
  138. */
  139. protected function tearDown()
  140. {
  141. print __METHOD__."\n";
  142. }
  143. /**
  144. * testLang
  145. *
  146. * @return string
  147. */
  148. public function testLang()
  149. {
  150. global $conf,$user,$langs,$db;
  151. $conf=$this->savconf;
  152. $user=$this->savuser;
  153. $langs=$this->savlangs;
  154. $db=$this->savdb;
  155. include_once DOL_DOCUMENT_ROOT.'/core/class/translate.class.php';
  156. $filesarray = scandir(DOL_DOCUMENT_ROOT.'/langs');
  157. foreach ($filesarray as $key => $code) {
  158. if (! preg_match('/^[a-z]+_[A-Z]+$/', $code)) {
  159. continue;
  160. }
  161. print 'Check language file for lang code='.$code."\n";
  162. $tmplangs=new Translate('', $conf);
  163. $langcode=$code;
  164. $tmplangs->setDefaultLang($langcode);
  165. $tmplangs->load("main");
  166. $result=$tmplangs->transnoentitiesnoconv("FONTFORPDF");
  167. print __METHOD__." FONTFORPDF=".$result."\n";
  168. $this->assertTrue(in_array($result, array('msungstdlight', 'stsongstdlight', 'helvetica', 'DejaVuSans', 'cid0jp', 'cid0kr', 'freemono')), 'Error bad value '.$result.' for FONTFORPDF in main.lang file '.$code);
  169. $result=$tmplangs->transnoentitiesnoconv("DIRECTION");
  170. print __METHOD__." DIRECTION=".$result."\n";
  171. $this->assertTrue(in_array($result, array('rtl', 'ltr')), 'Error bad value for DIRECTION in main.lang file '.$code);
  172. $result=$tmplangs->transnoentitiesnoconv("SeparatorDecimal");
  173. print __METHOD__." SeparatorDecimal=".$result."\n";
  174. $this->assertContains($result, array('.',',','/',' ','','None'), 'Error on decimal separator for lang code '.$code); // Note that ، that is coma for RTL languages is not supported
  175. $result=$tmplangs->transnoentitiesnoconv("SeparatorThousand");
  176. print __METHOD__." SeparatorThousand=".$result."\n";
  177. $this->assertContains($result, array('.',',','/',' ','','\'','None','Space'), 'Error on thousand separator for lang code '.$code); // Note that ، that is coma for RTL languages is not supported
  178. // Test java string contains only d,M,y,/,-,. and not m,...
  179. $result=$tmplangs->transnoentitiesnoconv("FormatDateShortJava");
  180. print __METHOD__." FormatDateShortJava=".$result."\n";
  181. $this->assertRegExp('/^[dMy\/\-\.]+$/', $result, 'FormatDateShortJava KO for lang code '.$code);
  182. $result=$tmplangs->trans("FormatDateShortJavaInput");
  183. print __METHOD__." FormatDateShortJavaInput=".$result."\n";
  184. $this->assertRegExp('/^[dMy\/\-\.]+$/', $result, 'FormatDateShortJavaInput KO for lang code '.$code);
  185. unset($tmplangs);
  186. $filesarray2 = scandir(DOL_DOCUMENT_ROOT.'/langs/'.$code);
  187. foreach ($filesarray2 as $key => $file) {
  188. if (! preg_match('/\.lang$/', $file)) {
  189. continue;
  190. }
  191. print 'Check lang file '.$file."\n";
  192. $filecontent=file_get_contents(DOL_DOCUMENT_ROOT.'/langs/'.$code.'/'.$file);
  193. $result=preg_match('/=--$/m', $filecontent); // A special % char we don't want. We want the common one.
  194. //print __METHOD__." Result for checking we don't have bad percent char = ".$result."\n";
  195. $this->assertTrue($result == 0, 'Found a translation KEY=-- into file '.$code.'/'.$file.'. We probably want Key=- instead.');
  196. $result=strpos($filecontent, '%'); // A special % char we don't want. We want the common one.
  197. //print __METHOD__." Result for checking we don't have bad percent char = ".$result."\n";
  198. $this->assertTrue($result === false, 'Found a bad percent char % instead of % into file '.$code.'/'.$file);
  199. }
  200. }
  201. return;
  202. }
  203. /**
  204. * testTrans
  205. *
  206. * @return string
  207. */
  208. public function testTrans()
  209. {
  210. global $conf,$user,$langs,$db;
  211. $conf=$this->savconf;
  212. $user=$this->savuser;
  213. $langs=$this->savlangs;
  214. $db=$this->savdb;
  215. $tmplangs=new Translate('', $conf);
  216. $langcode='en_US';
  217. $tmplangs->setDefaultLang($langcode);
  218. $tmplangs->load("main");
  219. $result = $tmplangs->trans("FilterOnInto", "<input autofocus onfocus='alert(1337)' <--!");
  220. print __METHOD__." result trans FilterOnInto = ".$result."\n";
  221. $this->assertEquals($result, "Search criteria '<b>&lt;input autofocus onfocus='alert(1337)' &lt;--!</b>' into fields ", 'Result of lang->trans must have original translation string with its original HTML tag, but inserted values must be fully encoded.');
  222. }
  223. }