LangTest.php 8.5 KB

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