FunctionsLibTest.php 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  1. <?php
  2. /* Copyright (C) 2010-2014 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2015 Juanjo Menent <jmenent@2byte.es>
  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/FunctionsLibTest.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/date.lib.php';
  30. require_once dirname(__FILE__).'/../../htdocs/product/class/product.class.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. print "\n".$langs->trans("CurrentTimeZone").' : '.getServerTimeZoneString();
  62. print "\n".$langs->trans("CurrentHour").' : '.dol_print_date(dol_now('gmt'), 'dayhour', 'tzserver');
  63. print "\n";
  64. /**
  65. * Class for PHPUnit tests
  66. *
  67. * @backupGlobals disabled
  68. * @backupStaticAttributes enabled
  69. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  70. */
  71. class FunctionsLibTest extends PHPUnit\Framework\TestCase
  72. {
  73. protected $savconf;
  74. protected $savuser;
  75. protected $savlangs;
  76. protected $savdb;
  77. /**
  78. * Constructor
  79. * We save global variables into local variables
  80. *
  81. * @return CoreTest
  82. */
  83. public function __construct()
  84. {
  85. parent::__construct();
  86. //$this->sharedFixture
  87. global $conf,$user,$langs,$db;
  88. $this->savconf=$conf;
  89. $this->savuser=$user;
  90. $this->savlangs=$langs;
  91. $this->savdb=$db;
  92. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  93. //print " - db ".$db->db;
  94. print "\n";
  95. }
  96. /**
  97. * setUpBeforeClass
  98. *
  99. * @return void
  100. */
  101. public static function setUpBeforeClass(): void
  102. {
  103. global $conf,$user,$langs,$db;
  104. //$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  105. if (! function_exists('mb_substr')) {
  106. print "\n".__METHOD__." function mb_substr must be enabled.\n"; die(1);
  107. }
  108. if ($conf->global->MAIN_MAX_DECIMALS_UNIT != 5) {
  109. print "\n".__METHOD__." bad setup for number of digits for unit amount. Must be 5 for this test.\n"; die(1);
  110. }
  111. if ($conf->global->MAIN_MAX_DECIMALS_TOT != 2) {
  112. print "\n".__METHOD__." bad setup for number of digits for unit amount. Must be 2 for this test.\n"; die(1);
  113. }
  114. print __METHOD__."\n";
  115. }
  116. /**
  117. * tearDownAfterClass
  118. *
  119. * @return void
  120. */
  121. public static function tearDownAfterClass(): void
  122. {
  123. global $conf,$user,$langs,$db;
  124. //$db->rollback();
  125. print __METHOD__."\n";
  126. }
  127. /**
  128. * Init phpunit tests
  129. *
  130. * @return void
  131. */
  132. protected function setUp(): void
  133. {
  134. global $conf,$user,$langs,$db;
  135. $conf=$this->savconf;
  136. $user=$this->savuser;
  137. $langs=$this->savlangs;
  138. $db=$this->savdb;
  139. print __METHOD__."\n";
  140. }
  141. /**
  142. * End phpunit tests
  143. *
  144. * @return void
  145. */
  146. protected function tearDown(): void
  147. {
  148. print __METHOD__."\n";
  149. }
  150. /**
  151. * testDolClone
  152. *
  153. * @return void
  154. */
  155. public function testDolClone()
  156. {
  157. $newproduct1 = new Product($this->savdb);
  158. print __METHOD__." this->savdb has type ".(is_resource($this->savdb->db) ? get_resource_type($this->savdb->db) : (is_object($this->savdb->db) ? 'object' : 'unknown'))."\n";
  159. print __METHOD__." newproduct1->db->db has type ".(is_resource($newproduct1->db->db) ? get_resource_type($newproduct1->db->db) : (is_object($newproduct1->db->db) ? 'object' : 'unknown'))."\n";
  160. $this->assertEquals($this->savdb->connected, 1, 'Savdb is connected');
  161. $this->assertNotNull($newproduct1->db->db, 'newproduct1->db is not null');
  162. $newproductcloned1 = dol_clone($newproduct1);
  163. print __METHOD__." this->savdb has type ".(is_resource($this->savdb->db) ? get_resource_type($this->savdb->db) : (is_object($this->savdb->db) ? 'object' : 'unknown'))."\n";
  164. print __METHOD__." newproduct1->db->db has type ".(is_resource($newproduct1->db->db) ? get_resource_type($newproduct1->db->db) : (is_object($newproduct1->db->db) ? 'object' : 'unknown'))."\n";
  165. $this->assertEquals($this->savdb->connected, 1, 'Savdb is connected');
  166. $this->assertNotNull($newproduct1->db->db, 'newproduct1->db is not null');
  167. $newproductcloned2 = dol_clone($newproduct1, 2);
  168. var_dump($newproductcloned2);
  169. //print __METHOD__." newproductcloned1->db must be null\n";
  170. //$this->assertNull($newproductcloned1->db, 'newproductcloned1->db is null');
  171. }
  172. /**
  173. * testNum2Alpha
  174. *
  175. * @return void
  176. */
  177. public function testNum2Alpha()
  178. {
  179. $result = num2Alpha(0);
  180. print __METHOD__." result=".$result."\n";
  181. $this->assertEquals($result, 'A', 'Check num2Alpha 0');
  182. $result = num2Alpha(5);
  183. print __METHOD__." result=".$result."\n";
  184. $this->assertEquals($result, 'F', 'Check num2Alpha 5');
  185. $result = num2Alpha(26);
  186. print __METHOD__." result=".$result."\n";
  187. $this->assertEquals($result, 'AA', 'Check num2Alpha 26');
  188. }
  189. /**
  190. * testIsValidEmail
  191. *
  192. * @return void
  193. */
  194. public function testIsValidEmail()
  195. {
  196. // Nb of line is same than entry text
  197. $input="bidon@bademail";
  198. $result=isValidEmail($input);
  199. print __METHOD__." result=".$result."\n";
  200. $this->assertFalse($result, 'Check isValidEmail '.$input);
  201. $input="test@yahoo.com";
  202. $result=isValidEmail($input);
  203. print __METHOD__." result=".$result."\n";
  204. $this->assertTrue($result, 'Check isValidEmail '.$input);
  205. $input="The name of sender <test@yahoo.com>";
  206. $result=isValidEmail($input);
  207. print __METHOD__." result=".$result."\n";
  208. $this->assertFalse($result, 'Check isValidEmail '.$input);
  209. $input="1234.abcdefg@domainame.com.br";
  210. $result=isValidEmail($input);
  211. print __METHOD__." result=".$result."\n";
  212. $this->assertTrue($result, 'Check isValidEmail '.$input);
  213. }
  214. /**
  215. * testIsValidMXRecord
  216. *
  217. * @return void
  218. */
  219. public function testIsValidMXRecord()
  220. {
  221. // Nb of line is same than entry text
  222. $input="yahoo.com";
  223. $result=isValidMXRecord($input);
  224. print __METHOD__." result=".$result."\n";
  225. $this->assertEquals(1, $result);
  226. $input="yhaoo.com";
  227. $result=isValidMXRecord($input);
  228. print __METHOD__." result=".$result."\n";
  229. $this->assertEquals(0, $result);
  230. $input="dolibarr.fr";
  231. $result=isValidMXRecord($input);
  232. print __METHOD__." result=".$result."\n";
  233. $this->assertEquals(0, $result);
  234. }
  235. /**
  236. * testDolGetFirstLineOfText
  237. *
  238. * @return void
  239. */
  240. public function testDolGetFirstLineOfText()
  241. {
  242. // Nb of line is same than entry text
  243. $input="aaaa";
  244. $result=dolGetFirstLineOfText($input);
  245. print __METHOD__." result=".$result."\n";
  246. $this->assertEquals("aaaa", $result);
  247. $input="aaaa\nbbbbbbbbbbbb\n";
  248. $result=dolGetFirstLineOfText($input, 2);
  249. print __METHOD__." result=".$result."\n";
  250. $this->assertEquals("aaaa\nbbbbbbbbbbbb", $result);
  251. $input="aaaa<br>bbbbbbbbbbbb<br>";
  252. $result=dolGetFirstLineOfText($input, 2);
  253. print __METHOD__." result=".$result."\n";
  254. $this->assertEquals("aaaa<br>\nbbbbbbbbbbbb", $result);
  255. // Nb of line is lower
  256. $input="aaaa\nbbbbbbbbbbbb\ncccccc\n";
  257. $result=dolGetFirstLineOfText($input);
  258. print __METHOD__." result=".$result."\n";
  259. $this->assertEquals("aaaa...", $result);
  260. $input="aaaa<br>bbbbbbbbbbbb<br>cccccc<br>";
  261. $result=dolGetFirstLineOfText($input);
  262. print __METHOD__." result=".$result."\n";
  263. $this->assertEquals("aaaa...", $result);
  264. $input="aaaa\nbbbbbbbbbbbb\ncccccc\n";
  265. $result=dolGetFirstLineOfText($input, 2);
  266. print __METHOD__." result=".$result."\n";
  267. $this->assertEquals("aaaa\nbbbbbbbbbbbb...", $result);
  268. $input="aaaa<br>bbbbbbbbbbbb<br>cccccc<br>";
  269. $result=dolGetFirstLineOfText($input, 2);
  270. print __METHOD__." result=".$result."\n";
  271. $this->assertEquals("aaaa<br>\nbbbbbbbbbbbb...", $result);
  272. // Nb of line is higher
  273. $input="aaaa<br>bbbbbbbbbbbb<br>cccccc";
  274. $result=dolGetFirstLineOfText($input, 100);
  275. print __METHOD__." result=".$result."\n";
  276. $this->assertEquals("aaaa<br>\nbbbbbbbbbbbb<br>\ncccccc", $result, 'dolGetFirstLineOfText with nb 100 a');
  277. $input="aaaa<br>bbbbbbbbbbbb<br>cccccc<br>";
  278. $result=dolGetFirstLineOfText($input, 100);
  279. print __METHOD__." result=".$result."\n";
  280. $this->assertEquals("aaaa<br>\nbbbbbbbbbbbb<br>\ncccccc", $result, 'dolGetFirstLineOfText with nb 100 b');
  281. $input="aaaa<br>bbbbbbbbbbbb<br>cccccc<br>\n";
  282. $result=dolGetFirstLineOfText($input, 100);
  283. print __METHOD__." result=".$result."\n";
  284. $this->assertEquals("aaaa<br>\nbbbbbbbbbbbb<br>\ncccccc", $result, 'dolGetFirstLineOfText with nb 100 c');
  285. }
  286. /**
  287. * testDolBuildPath
  288. *
  289. * @return void
  290. */
  291. public function testDolBuildPath()
  292. {
  293. /*$tmp=dol_buildpath('/google/oauth2callback.php', 0);
  294. var_dump($tmp);
  295. */
  296. /*$tmp=dol_buildpath('/google/oauth2callback.php', 1);
  297. var_dump($tmp);
  298. */
  299. $result=dol_buildpath('/google/oauth2callback.php', 2);
  300. print __METHOD__." result=".$result."\n";
  301. $this->assertStringStartsWith('http', $result);
  302. $result=dol_buildpath('/google/oauth2callback.php', 3);
  303. print __METHOD__." result=".$result."\n";
  304. $this->assertStringStartsWith('http', $result);
  305. }
  306. /**
  307. * testGetBrowserInfo
  308. *
  309. * @return void
  310. */
  311. public function testGetBrowserInfo()
  312. {
  313. // MSIE 5.0
  314. $user_agent ='Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt; KITV4 Wanadoo; KITV5 Wanadoo)';
  315. $tmp=getBrowserInfo($user_agent);
  316. $this->assertEquals('ie', $tmp['browsername']);
  317. $this->assertEquals('5.0', $tmp['browserversion']);
  318. $this->assertEmpty($tmp['phone']);
  319. $this->assertFalse($tmp['tablet']);
  320. $this->assertEquals('classic', $tmp['layout']);
  321. // Firefox 0.9.1
  322. $user_agent ='Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.5a) Gecko/20030728 Mozilla Firefox/0.9.1';
  323. $tmp=getBrowserInfo($user_agent);
  324. $this->assertEquals('firefox', $tmp['browsername']);
  325. $this->assertEquals('0.9.1', $tmp['browserversion']);
  326. $this->assertEmpty($tmp['phone']);
  327. $this->assertFalse($tmp['tablet']);
  328. $this->assertEquals('classic', $tmp['layout']);
  329. $user_agent ='Mozilla/3.0 (Windows 98; U) Opera 6.03 [en]';
  330. $tmp=getBrowserInfo($user_agent);
  331. $this->assertEquals('opera', $tmp['browsername']);
  332. $this->assertEquals('6.03', $tmp['browserversion']);
  333. $this->assertEmpty($tmp['phone']);
  334. $this->assertFalse($tmp['tablet']);
  335. $this->assertEquals('classic', $tmp['layout']);
  336. $user_agent ='Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21';
  337. $tmp=getBrowserInfo($user_agent);
  338. $this->assertEquals('chrome', $tmp['browsername']);
  339. $this->assertEquals('19.0.1042.0', $tmp['browserversion']);
  340. $this->assertEmpty($tmp['phone']);
  341. $this->assertFalse($tmp['tablet']);
  342. $this->assertEquals('classic', $tmp['layout']);
  343. $user_agent ='chrome (Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11)';
  344. $tmp=getBrowserInfo($user_agent);
  345. $this->assertEquals('chrome', $tmp['browsername']);
  346. $this->assertEquals('17.0.963.56', $tmp['browserversion']);
  347. $this->assertEmpty($tmp['phone']);
  348. $this->assertFalse($tmp['tablet']);
  349. $this->assertEquals('classic', $tmp['layout']);
  350. $user_agent ='Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1';
  351. $tmp=getBrowserInfo($user_agent);
  352. $this->assertEquals('safari', $tmp['browsername']);
  353. $this->assertEquals('533.21.1', $tmp['browserversion']);
  354. $this->assertEmpty($tmp['phone']);
  355. $this->assertFalse($tmp['tablet']);
  356. $this->assertEquals('classic', $tmp['layout']);
  357. //Internet Explorer 11
  358. $user_agent = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko';
  359. $tmp=getBrowserInfo($user_agent);
  360. $this->assertEquals('ie', $tmp['browsername']);
  361. $this->assertEquals('11.0', $tmp['browserversion']);
  362. $this->assertEmpty($tmp['phone']);
  363. $this->assertFalse($tmp['tablet']);
  364. $this->assertEquals('classic', $tmp['layout']);
  365. //Internet Explorer 11 bis
  366. $user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; NP06; rv:11.0) like Gecko';
  367. $tmp=getBrowserInfo($user_agent);
  368. $this->assertEquals('ie', $tmp['browsername']);
  369. $this->assertEquals('11.0', $tmp['browserversion']);
  370. $this->assertEmpty($tmp['phone']);
  371. $this->assertFalse($tmp['tablet']);
  372. $this->assertEquals('classic', $tmp['layout']);
  373. //iPad
  374. $user_agent = 'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25';
  375. $tmp=getBrowserInfo($user_agent);
  376. $this->assertEquals('safari', $tmp['browsername']);
  377. $this->assertEquals('8536.25', $tmp['browserversion']);
  378. $this->assertEquals('ios', $tmp['browseros']);
  379. $this->assertEquals('tablet', $tmp['layout']);
  380. $this->assertEquals('iphone', $tmp['phone']);
  381. //Lynx
  382. $user_agent = 'Lynx/2.8.8dev.3 libwww‑FM/2.14 SSL‑MM/1.4.1';
  383. $tmp=getBrowserInfo($user_agent);
  384. $this->assertEquals('lynxlinks', $tmp['browsername']);
  385. $this->assertEquals('2.8.8', $tmp['browserversion']);
  386. $this->assertEquals('unknown', $tmp['browseros']);
  387. $this->assertEquals('classic', $tmp['layout']);
  388. }
  389. /**
  390. * testGetLanguageCodeFromCountryCode
  391. *
  392. * @return void
  393. */
  394. public function testGetLanguageCodeFromCountryCode()
  395. {
  396. global $mysoc;
  397. $language = getLanguageCodeFromCountryCode('US');
  398. $this->assertEquals('en_US', $language, 'US');
  399. $language = getLanguageCodeFromCountryCode('ES');
  400. $this->assertEquals('es_ES', $language, 'ES');
  401. $language = getLanguageCodeFromCountryCode('CL');
  402. $this->assertEquals('es_CL', $language, 'CL');
  403. $language = getLanguageCodeFromCountryCode('CA');
  404. $this->assertEquals('en_CA', $language, 'CA');
  405. $language = getLanguageCodeFromCountryCode('MQ');
  406. $this->assertEquals('fr_CA', $language);
  407. $language = getLanguageCodeFromCountryCode('FR');
  408. $this->assertEquals('fr_FR', $language);
  409. $language = getLanguageCodeFromCountryCode('BE');
  410. $this->assertEquals('fr_BE', $language);
  411. $mysoc->country_code = 'FR';
  412. $language = getLanguageCodeFromCountryCode('CH');
  413. $this->assertEquals('fr_CH', $language);
  414. $mysoc->country_code = 'DE';
  415. $language = getLanguageCodeFromCountryCode('CH');
  416. $this->assertEquals('de_CH', $language);
  417. $language = getLanguageCodeFromCountryCode('DE');
  418. $this->assertEquals('de_DE', $language);
  419. $language = getLanguageCodeFromCountryCode('SA');
  420. $this->assertEquals('ar_SA', $language);
  421. $language = getLanguageCodeFromCountryCode('SE');
  422. $this->assertEquals('sv_SE', $language);
  423. $language = getLanguageCodeFromCountryCode('DK');
  424. $this->assertEquals('da_DK', $language);
  425. }
  426. /**
  427. * testDolTextIsHtml
  428. *
  429. * @return void
  430. */
  431. public function testDolTextIsHtml()
  432. {
  433. // True
  434. $input='<html>xxx</html>';
  435. $after=dol_textishtml($input);
  436. $this->assertTrue($after, 'Test with html tag');
  437. $input='<body>xxx</body>';
  438. $after=dol_textishtml($input);
  439. $this->assertTrue($after, 'Test with body tag');
  440. $input='xxx <b>yyy</b> zzz';
  441. $after=dol_textishtml($input);
  442. $this->assertTrue($after, 'Test with b tag');
  443. $input='xxx <u>yyy</u> zzz';
  444. $after=dol_textishtml($input);
  445. $this->assertTrue($after, 'Test with u tag');
  446. $input='text with <div>some div</div>';
  447. $after=dol_textishtml($input);
  448. $this->assertTrue($after, 'Test with div tag');
  449. $input='text with HTML &nbsp; entities';
  450. $after=dol_textishtml($input);
  451. $this->assertTrue($after, 'Test with entities tag');
  452. $input='xxx<br>';
  453. $after=dol_textishtml($input);
  454. $this->assertTrue($after, 'Test with entities br');
  455. $input='xxx<br >';
  456. $after=dol_textishtml($input);
  457. $this->assertTrue($after, 'Test with entities br');
  458. $input='xxx<br style="eee">';
  459. $after=dol_textishtml($input);
  460. $this->assertTrue($after, 'Test with entities br and attributes');
  461. $input='xxx<br style="eee" >';
  462. $after=dol_textishtml($input);
  463. $this->assertTrue($after, 'Test with entities br and attributes bis');
  464. $input='<h2>abc</h2>';
  465. $after=dol_textishtml($input);
  466. $this->assertTrue($after, 'Test with entities h2');
  467. $input='<img id="abc" src="https://xxx.com/aaa/image.png" />';
  468. $after=dol_textishtml($input);
  469. $this->assertTrue($after, 'Test with img tag');
  470. $input='<a class="azerty" href="https://xxx.com/aaa/image.png" />';
  471. $after=dol_textishtml($input);
  472. $this->assertTrue($after, 'Test with a tag');
  473. $input='This is a text with&nbsp;html spaces';
  474. $after=dol_textishtml($input);
  475. $this->assertTrue($after, 'Test with a &nbsp;');
  476. $input='This is a text with accent &eacute;';
  477. $after=dol_textishtml($input);
  478. $this->assertTrue($after, 'Test with a &eacute;');
  479. // False
  480. $input='xxx < br>';
  481. $after=dol_textishtml($input);
  482. $this->assertFalse($after);
  483. $input='xxx <email@email.com>'; // <em> is html, <em... is not
  484. $after=dol_textishtml($input);
  485. $this->assertFalse($after);
  486. $input='xxx <brstyle="ee">';
  487. $after=dol_textishtml($input);
  488. $this->assertFalse($after);
  489. $input='This is a text with html comments <!-- comment -->'; // we suppose this is not enough to be html content
  490. $after=dol_textishtml($input);
  491. $this->assertFalse($after);
  492. $input="A text\nwith a link https://aaa?param=abc&amp;param2=def";
  493. $after=dol_textishtml($input);
  494. $this->assertFalse($after);
  495. }
  496. /**
  497. * testDolHtmlCleanLastBr
  498. *
  499. * @return boolean
  500. */
  501. public function testDolHtmlCleanLastBr()
  502. {
  503. $input="A string\n";
  504. $after=dol_htmlcleanlastbr($input);
  505. $this->assertEquals("A string", $after);
  506. $input="A string first\nA string second\n";
  507. $after=dol_htmlcleanlastbr($input);
  508. $this->assertEquals("A string first\nA string second", $after);
  509. $input="A string\n\n\n";
  510. $after=dol_htmlcleanlastbr($input);
  511. $this->assertEquals("A string", $after);
  512. $input="A string<br>";
  513. $after=dol_htmlcleanlastbr($input);
  514. $this->assertEquals("A string", $after);
  515. $input="A string first<br>\nA string second<br>";
  516. $after=dol_htmlcleanlastbr($input);
  517. $this->assertEquals("A string first<br>\nA string second", $after);
  518. $input="A string\n<br type=\"_moz\" />\n";
  519. $after=dol_htmlcleanlastbr($input);
  520. $this->assertEquals("A string", $after);
  521. $input="A string\n<br><br />\n\n";
  522. $after=dol_htmlcleanlastbr($input);
  523. $this->assertEquals("A string", $after);
  524. return true;
  525. }
  526. /**
  527. * testDolConcat
  528. *
  529. * @return boolean
  530. */
  531. public function testDolConcat()
  532. {
  533. $text1="A string 1"; $text2="A string 2"; // text 1 and 2 are text, concat need only \n
  534. $after=dol_concatdesc($text1, $text2);
  535. $this->assertEquals("A string 1\nA string 2", $after);
  536. $text1="A<br>string 1"; $text2="A string 2"; // text 1 is html, concat need <br>\n
  537. $after=dol_concatdesc($text1, $text2);
  538. $this->assertEquals("A<br>string 1<br>\nA string 2", $after);
  539. $text1="A string 1"; $text2="A <b>string</b> 2"; // text 2 is html, concat need <br>\n
  540. $after=dol_concatdesc($text1, $text2);
  541. $this->assertEquals("A string 1<br>\nA <b>string</b> 2", $after);
  542. return true;
  543. }
  544. /**
  545. * testDolStringNohtmltag
  546. *
  547. * @return boolean
  548. */
  549. public function testDolStringNohtmltag()
  550. {
  551. $text="A\nstring\n\nand more\n";
  552. $after=dol_string_nohtmltag($text, 0);
  553. $this->assertEquals("A\nstring\n\nand more", $after, "test1a");
  554. $text="A <b>string<b><br>\n<br>\n\nwith html tag<br>\n";
  555. $after=dol_string_nohtmltag($text, 0);
  556. $this->assertEquals("A string\n\n\n\n\nwith html tag", $after, 'test2a 2 br and 3 \n give 5 \n');
  557. $text="A <b>string<b><br>\n<br>\n\nwith html tag<br>\n";
  558. $after=dol_string_nohtmltag($text, 1);
  559. $this->assertEquals("A string with html tag", $after, 'test2b 2 br and 3 \n give 1 space');
  560. $text="A <b>string<b><br>\n<br>\n\nwith html tag<br>\n";
  561. $after=dol_string_nohtmltag($text, 2);
  562. $this->assertEquals("A string\n\nwith html tag", $after, 'test2c 2 br and 3 \n give 2 \n');
  563. $text="A <b>string<b><br>\r\n<br>\r\n\r\nwith html tag<br>\n";
  564. $after=dol_string_nohtmltag($text, 2);
  565. $this->assertEquals("A string\n\nwith html tag", $after, 'test2c 2 br and 3 \r\n give 2 \n');
  566. $text="A string<br>Another string";
  567. $after=dol_string_nohtmltag($text, 0);
  568. $this->assertEquals("A string\nAnother string", $after, "test4");
  569. $text="A string<br>Another string";
  570. $after=dol_string_nohtmltag($text, 1);
  571. $this->assertEquals("A string Another string", $after, "test5");
  572. $text='<a href="/myurl" title="<u>Afficher projet</u>">ABC</a>';
  573. $after=dol_string_nohtmltag($text, 1);
  574. $this->assertEquals("ABC", $after, "test6");
  575. $text='<a href="/myurl" title="&lt;u&gt;Afficher projet&lt;/u&gt;">DEF</a>';
  576. $after=dol_string_nohtmltag($text, 1);
  577. $this->assertEquals("DEF", $after, "test7");
  578. $text='<a href="/myurl" title="<u>A title</u>">HIJ</a>';
  579. $after=dol_string_nohtmltag($text, 0);
  580. $this->assertEquals("HIJ", $after, "test8");
  581. $text="A <b>string<b>\n\nwith html tag and '<' chars<br>\n";
  582. $after=dol_string_nohtmltag($text, 0);
  583. $this->assertEquals("A string\n\nwith html tag and '<' chars", $after, "test9");
  584. $text="A <b>string<b>\n\nwith tag with < chars<br>\n";
  585. $after=dol_string_nohtmltag($text, 1);
  586. $this->assertEquals("A string with tag with < chars", $after, "test10");
  587. return true;
  588. }
  589. /**
  590. * testDolHtmlEntitiesBr
  591. *
  592. * @return boolean
  593. */
  594. public function testDolHtmlEntitiesBr()
  595. {
  596. // Text not already HTML
  597. $input="A string\nwith a é, &, < and >.";
  598. $after=dol_htmlentitiesbr($input, 0); // Add <br> before \n
  599. $this->assertEquals("A string<br>\nwith a &eacute;, &amp;, &lt; and &gt;.", $after);
  600. $input="A string\nwith a é, &, < and >.";
  601. $after=dol_htmlentitiesbr($input, 1); // Replace \n with <br>
  602. $this->assertEquals("A string<br>with a &eacute;, &amp;, &lt; and &gt;.", $after);
  603. $input="A string\nwith a é, &, < and >.\n\n"; // With some \n at end that should be cleaned
  604. $after=dol_htmlentitiesbr($input, 0); // Add <br> before \n
  605. $this->assertEquals("A string<br>\nwith a &eacute;, &amp;, &lt; and &gt;.", $after);
  606. $input="A string\nwith a é, &, < and >.\n\n"; // With some \n at end that should be cleaned
  607. $after=dol_htmlentitiesbr($input, 1); // Replace \n with <br>
  608. $this->assertEquals("A string<br>with a &eacute;, &amp;, &lt; and &gt;.", $after);
  609. // Text already HTML, so &,<,> should not be converted
  610. $input="A string<br>\nwith a é, &, < and >.";
  611. $after=dol_htmlentitiesbr($input);
  612. $this->assertEquals("A string<br>\nwith a &eacute;, &, < and >.", $after);
  613. $input="<li>\nA string with a é, &, < and >.</li>\nAnother string";
  614. $after=dol_htmlentitiesbr($input);
  615. $this->assertEquals("<li>\nA string with a &eacute;, &, < and >.</li>\nAnother string", $after);
  616. $input="A string<br>\nwith a é, &, < and >.<br>"; // With some <br> at end that should be cleaned
  617. $after=dol_htmlentitiesbr($input);
  618. $this->assertEquals("A string<br>\nwith a &eacute;, &, < and >.", $after);
  619. $input="<li>\nA string with a é, &, < and >.</li>\nAnother string<br>"; // With some <br> at end that should be cleaned
  620. $after=dol_htmlentitiesbr($input);
  621. $this->assertEquals("<li>\nA string with a &eacute;, &, < and >.</li>\nAnother string", $after);
  622. // TODO Add test with param $removelasteolbr = 0
  623. return true;
  624. }
  625. /**
  626. * testDolNbOfLinesBis
  627. *
  628. * @return boolean
  629. */
  630. public function testDolNbOfLinesBis()
  631. {
  632. // This is not a html string so nb of lines depends on \n
  633. $input="A string\nwith a é, &, < and > and bold tag.\nThird line";
  634. $after=dol_nboflines_bis($input, 0);
  635. $this->assertEquals($after, 3);
  636. // This is a html string so nb of lines depends on <br>
  637. $input="A string\nwith a é, &, < and > and <b>bold</b> tag.\nThird line";
  638. $after=dol_nboflines_bis($input, 0);
  639. $this->assertEquals($after, 1);
  640. // This is a html string so nb of lines depends on <br>
  641. $input="A string<br>with a é, &, < and > and <b>bold</b> tag.<br>Third line";
  642. $after=dol_nboflines_bis($input, 0);
  643. $this->assertEquals($after, 3);
  644. return true;
  645. }
  646. /**
  647. * testDolUnaccent
  648. *
  649. * @return boolean
  650. */
  651. public function testDolUnaccent()
  652. {
  653. // Text not already HTML
  654. $input="A string\nwith a à ä é è ë ï ü ö ÿ, &, < and >.";
  655. $after=dol_string_unaccent($input);
  656. $this->assertEquals("A string\nwith a a a e e e i u o y, &, < and >.", $after);
  657. }
  658. /**
  659. * testDolUtf8Check
  660. *
  661. * @return void
  662. */
  663. public function testDolUtf8Check()
  664. {
  665. // True
  666. $result=utf8_check('azerty');
  667. $this->assertTrue($result);
  668. $file=dirname(__FILE__).'/textutf8.txt';
  669. $filecontent=file_get_contents($file);
  670. $result=utf8_check($filecontent);
  671. $this->assertTrue($result);
  672. $file=dirname(__FILE__).'/textiso.txt';
  673. $filecontent=file_get_contents($file);
  674. $result=utf8_check($filecontent);
  675. $this->assertFalse($result);
  676. }
  677. /**
  678. * testDolAsciiCheck
  679. *
  680. * @return void
  681. */
  682. public function testDolAsciiCheck()
  683. {
  684. // True
  685. $result=ascii_check('azerty');
  686. $this->assertTrue($result);
  687. $result=ascii_check('é');
  688. $this->assertFalse($result);
  689. $file=dirname(__FILE__).'/textutf8.txt';
  690. $filecontent=file_get_contents($file);
  691. $result=ascii_check($filecontent);
  692. $this->assertFalse($result);
  693. }
  694. /**
  695. * testDolTrunc
  696. *
  697. * @return boolean
  698. */
  699. public function testDolTrunc()
  700. {
  701. // Default trunc (will add … if truncation truncation or keep last char if only one char)
  702. $input="éeéeéeàa";
  703. $after=dol_trunc($input, 3);
  704. $this->assertEquals("éeé…", $after, 'Test A1');
  705. $after=dol_trunc($input, 2);
  706. $this->assertEquals("ée…", $after, 'Test A2');
  707. $after=dol_trunc($input, 1);
  708. $this->assertEquals("é…", $after, 'Test A3');
  709. $input="éeée";
  710. $after=dol_trunc($input, 3);
  711. $this->assertEquals("éeée", $after, 'Test B1');
  712. $after=dol_trunc($input, 2);
  713. $this->assertEquals("ée…", $after, 'Test B2');
  714. $after=dol_trunc($input, 1);
  715. $this->assertEquals("é…", $after, 'Test B3');
  716. $input="éeée";
  717. $after=dol_trunc($input, 3);
  718. $this->assertEquals("éeée", $after, 'Test C1');
  719. $after=dol_trunc($input, 2);
  720. $this->assertEquals("ée…", $after, 'Test C2');
  721. $after=dol_trunc($input, 1);
  722. $this->assertEquals("é…", $after, 'Test C3');
  723. $input="éeé";
  724. $after=dol_trunc($input, 3);
  725. $this->assertEquals("éeé", $after, 'Test C');
  726. $after=dol_trunc($input, 2);
  727. $this->assertEquals("éeé", $after, 'Test D');
  728. $after=dol_trunc($input, 1);
  729. $this->assertEquals("é…", $after, 'Test E');
  730. // Trunc with no …
  731. $input="éeéeéeàa";
  732. $after=dol_trunc($input, 3, 'right', 'UTF-8', 1);
  733. $this->assertEquals("éeé", $after, 'Test F');
  734. $after=dol_trunc($input, 2, 'right', 'UTF-8', 1);
  735. $this->assertEquals("ée", $after, 'Test G');
  736. $input="éeé";
  737. $after=dol_trunc($input, 3, 'right', 'UTF-8', 1);
  738. $this->assertEquals("éeé", $after, 'Test H');
  739. $after=dol_trunc($input, 2, 'right', 'UTF-8', 1);
  740. $this->assertEquals("ée", $after, 'Test I');
  741. $after=dol_trunc($input, 1, 'right', 'UTF-8', 1);
  742. $this->assertEquals("é", $after, 'Test J');
  743. $input="éeéeéeàa";
  744. $after=dol_trunc($input, 4, 'middle');
  745. $this->assertEquals("ée…àa", $after, 'Test K');
  746. return true;
  747. }
  748. /**
  749. * testDolMkTime
  750. *
  751. * @return void
  752. */
  753. public function testDolMkTime()
  754. {
  755. global $conf;
  756. $savtz=date_default_timezone_get();
  757. // Some test for UTC TZ
  758. date_default_timezone_set('UTC');
  759. // Check bad hours
  760. $result=dol_mktime(25, 0, 0, 1, 1, 1970, 1, 1); // Error (25 hours)
  761. print __METHOD__." result=".$result."\n";
  762. $this->assertEquals('', $result);
  763. $result=dol_mktime(2, 61, 0, 1, 1, 1970, 1, 1); // Error (61 minutes)
  764. print __METHOD__." result=".$result."\n";
  765. $this->assertEquals('', $result);
  766. $result=dol_mktime(2, 1, 61, 1, 1, 1970, 1, 1); // Error (61 seconds)
  767. print __METHOD__." result=".$result."\n";
  768. $this->assertEquals('', $result);
  769. $result=dol_mktime(2, 1, 1, 1, 32, 1970, 1, 1); // Error (day 32)
  770. print __METHOD__." result=".$result."\n";
  771. $this->assertEquals('', $result);
  772. $result=dol_mktime(2, 1, 1, 13, 1, 1970, 1, 1); // Error (month 13)
  773. print __METHOD__." result=".$result."\n";
  774. $this->assertEquals('', $result);
  775. $result=dol_mktime(2, 1, 1, 1, 1, 1970, 1); // 1970-01-01 02:01:01 in GMT area -> 7261
  776. print __METHOD__." result=".$result."\n";
  777. $this->assertEquals(7261, $result);
  778. $result=dol_mktime(2, 0, 0, 1, 1, 1970, 0); // 1970-01-01 02:00:00 = 7200 in local area Europe/Paris = 3600 GMT
  779. print __METHOD__." result=".$result."\n";
  780. $tz=getServerTimeZoneInt('winter'); // +1 in Europe/Paris at this time (this time is winter)
  781. $this->assertEquals(7200-($tz*3600), $result); // 7200 if we are at greenwich winter, 7200-($tz*3600) at local winter
  782. // Some test for local TZ Europe/Paris
  783. date_default_timezone_set('Europe/Paris');
  784. // Check that tz for paris in winter is used
  785. $result=dol_mktime(2, 0, 0, 1, 1, 1970, 'server'); // 1970-01-01 02:00:00 = 7200 in local area Europe/Paris = 3600 GMT
  786. print __METHOD__." result=".$result."\n";
  787. $this->assertEquals(3600, $result); // 7200 if we are at greenwich winter, 3600 at Europe/Paris
  788. // Check that daylight saving time is used
  789. $result=dol_mktime(2, 0, 0, 6, 1, 2014, 0); // 2014-06-01 02:00:00 = 1401588000-3600(location)-3600(daylight) in local area Europe/Paris = 1401588000 GMT
  790. print __METHOD__." result=".$result."\n";
  791. $this->assertEquals(1401588000-3600-3600, $result); // 1401588000 are at greenwich summer, 1401588000-3600(location)-3600(daylight) at Europe/Paris summer
  792. date_default_timezone_set($savtz);
  793. }
  794. /**
  795. * testDolEscapeJs
  796. *
  797. * @return void
  798. */
  799. public function testDolEscapeJs()
  800. {
  801. $input="x&<b>#</b>,\"'"; // " will be converted into '
  802. $result=dol_escape_js($input);
  803. $this->assertEquals("x&<b>#</b>,\'\'", $result, "Test mode=0");
  804. $result=dol_escape_js($input, 1);
  805. $this->assertEquals("x&<b>#</b>,\"\'", $result, "Test mode=1");
  806. $result=dol_escape_js($input, 2);
  807. $this->assertEquals("x&<b>#</b>,\\\"'", $result, "Test mode=2");
  808. }
  809. /**
  810. * testDolEscapeHtmlTag
  811. *
  812. * @return void
  813. */
  814. public function testDolEscapeHtmlTag()
  815. {
  816. $input='x&<b>#</b>,"'; // & and " are converted into html entities, <b> are removed
  817. $result=dol_escape_htmltag($input);
  818. $this->assertEquals('x&amp;#,&quot;', $result);
  819. $input='x&<b>#</b>,"'; // & and " are converted into html entities, <b> are not removed
  820. $result=dol_escape_htmltag($input, 1);
  821. $this->assertEquals('x&amp;&lt;b&gt;#&lt;/b&gt;,&quot;', $result);
  822. }
  823. /**
  824. * testDolFormatAddress
  825. *
  826. * @return void
  827. */
  828. public function testDolFormatAddress()
  829. {
  830. global $conf,$user,$langs,$db;
  831. $conf=$this->savconf;
  832. $user=$this->savuser;
  833. $langs=$this->savlangs;
  834. $db=$this->savdb;
  835. $object=new Societe($db);
  836. $object->initAsSpecimen();
  837. $object->country_code='FR';
  838. $address=dol_format_address($object);
  839. $this->assertEquals("21 jump street\n99999 MyTown", $address);
  840. $object->country_code='GB';
  841. $address=dol_format_address($object);
  842. $this->assertEquals("21 jump street\nMyTown, MyState\n99999", $address);
  843. $object->country_code='US';
  844. $address=dol_format_address($object);
  845. $this->assertEquals("21 jump street\nMyTown, MyState, 99999", $address);
  846. $object->country_code='AU';
  847. $address=dol_format_address($object);
  848. $this->assertEquals("21 jump street\nMyTown, MyState, 99999", $address);
  849. $object->country_code='JP';
  850. $address=dol_format_address($object);
  851. $this->assertEquals("21 jump street\nMyState, MyTown 99999", $address);
  852. }
  853. /**
  854. * testDolFormatAddress
  855. *
  856. * @return void
  857. */
  858. public function testDolPrintPhone()
  859. {
  860. global $conf,$user,$langs,$db;
  861. $conf=$this->savconf;
  862. $user=$this->savuser;
  863. $langs=$this->savlangs;
  864. $db=$this->savdb;
  865. $object=new Societe($db);
  866. $object->initAsSpecimen();
  867. $object->country_code='FR';
  868. $phone=dol_print_phone('1234567890', $object->country_code);
  869. $this->assertEquals('<span style="margin-right: 10px;">12&nbsp;34&nbsp;56&nbsp;78&nbsp;90</span>', $phone, 'Phone for FR 1');
  870. $object->country_code='FR';
  871. $phone=dol_print_phone('1234567890', $object->country_code, 0, 0, 0, '');
  872. $this->assertEquals('<span style="margin-right: 10px;">1234567890</span>', $phone, 'Phone for FR 2');
  873. $object->country_code='FR';
  874. $phone=dol_print_phone('1234567890', $object->country_code, 0, 0, 0, ' ');
  875. $this->assertEquals('<span style="margin-right: 10px;">12 34 56 78 90</span>', $phone, 'Phone for FR 3');
  876. $object->country_code='CA';
  877. $phone=dol_print_phone('1234567890', $object->country_code, 0, 0, 0, ' ');
  878. $this->assertEquals('<span style="margin-right: 10px;">(123) 456-7890</span>', $phone, 'Phone for CA 1');
  879. }
  880. /**
  881. * testImgPicto
  882. *
  883. * @return void
  884. */
  885. public function testImgPicto()
  886. {
  887. $s=img_picto('title', 'user');
  888. print __METHOD__." s=".$s."\n";
  889. $this->assertContains('fa-user', $s, 'testImgPicto1');
  890. $s=img_picto('title', 'img.png', 'style="float: right"', 0);
  891. print __METHOD__." s=".$s."\n";
  892. $this->assertContains('theme', $s, 'testImgPicto2');
  893. $this->assertContains('style="float: right"', $s, 'testImgPicto2');
  894. $s=img_picto('title', '/fullpath/img.png', '', 1);
  895. print __METHOD__." s=".$s."\n";
  896. $this->assertEquals('<img src="/fullpath/img.png" alt="" title="title" class="inline-block">', $s, 'testImgPicto3');
  897. $s=img_picto('title', '/fullpath/img.png', '', true);
  898. print __METHOD__." s=".$s."\n";
  899. $this->assertEquals('<img src="/fullpath/img.png" alt="" title="title" class="inline-block">', $s, 'testImgPicto4');
  900. $s=img_picto('title', 'delete', '', 0, 1);
  901. print __METHOD__." s=".$s."\n";
  902. $this->assertEquals(DOL_URL_ROOT.'/theme/eldy/img/delete.png', $s, 'testImgPicto5');
  903. }
  904. /**
  905. * testDolNow
  906. *
  907. * @return void
  908. */
  909. public function testDolNow()
  910. {
  911. $now=dol_now('gmt');
  912. $nowtzserver=dol_now('tzserver');
  913. print __METHOD__." getServerTimeZoneInt=".(getServerTimeZoneInt('now')*3600)."\n";
  914. $this->assertEquals(getServerTimeZoneInt('now')*3600, ($nowtzserver-$now));
  915. }
  916. /**
  917. * testVerifCond
  918. *
  919. * @return void
  920. */
  921. public function testVerifCond()
  922. {
  923. $verifcond=verifCond('1==1');
  924. $this->assertTrue($verifcond, 'Test a true comparison');
  925. $verifcond=verifCond('1==2');
  926. $this->assertFalse($verifcond, 'Test a false comparison');
  927. $verifcond=verifCond('$conf->facture->enabled');
  928. $this->assertTrue($verifcond, 'Test that the conf property of a module reports true when enabled');
  929. $verifcond=verifCond('$conf->moduledummy->enabled');
  930. $this->assertFalse($verifcond, 'Test that the conf property of a module reports false when disabled');
  931. $verifcond=verifCond(0);
  932. $this->assertFalse($verifcond, 'Test that verifConf(0) return False');
  933. $verifcond=verifCond("0");
  934. $this->assertFalse($verifcond, 'Test that verifConf("0") return False');
  935. $verifcond=verifCond('');
  936. $this->assertTrue($verifcond, 'Test that verifConf("") return False (special case)');
  937. }
  938. /**
  939. * testGetDefaultTva
  940. *
  941. * @return void
  942. */
  943. public function testGetDefaultTva()
  944. {
  945. global $conf,$user,$langs,$db;
  946. $this->savconf=$conf;
  947. $this->savuser=$user;
  948. $this->savlangs=$langs;
  949. $this->savdb=$db;
  950. // Sellers
  951. $companyfrnovat=new Societe($db);
  952. $companyfrnovat->country_code='FR';
  953. $companyfrnovat->tva_assuj=0;
  954. $companyfr=new Societe($db);
  955. $companyfr->country_code='FR';
  956. $companyfr->tva_assuj=1;
  957. $companyfr->tva_intra='FR9999';
  958. // Buyers
  959. $companymc=new Societe($db);
  960. $companymc->country_code='MC';
  961. $companymc->tva_assuj=1;
  962. $companyfr->tva_intra='MC9999';
  963. $companyit=new Societe($db);
  964. $companyit->country_code='IT';
  965. $companyit->tva_assuj=1;
  966. $companyit->tva_intra='IT99999';
  967. $companyde=new Societe($db);
  968. $companyde->country_code='DE';
  969. $companyde->tva_assuj=1;
  970. $companyde->tva_intra='DE99999';
  971. $notcompanyde=new Societe($db);
  972. $notcompanyde->country_code='DE';
  973. $notcompanyde->tva_assuj=0;
  974. $notcompanyde->tva_intra='';
  975. $notcompanyde->typent_code='TE_PRIVATE';
  976. $companyus=new Societe($db);
  977. $companyus->country_code='US';
  978. $companyus->tva_assuj=1;
  979. $companyus->tva_intra='';
  980. // Test RULE 0 (FR-DE)
  981. // Not tested
  982. // Test RULE 1
  983. $vat=get_default_tva($companyfrnovat, $companymc, 0);
  984. $this->assertEquals(0, $vat, 'RULE 1');
  985. // Test RULE 2 (FR-FR)
  986. $vat=get_default_tva($companyfr, $companyfr, 0);
  987. $this->assertEquals(20, $vat, 'RULE 2');
  988. // Test RULE 2 (FR-MC)
  989. $vat=get_default_tva($companyfr, $companymc, 0);
  990. $this->assertEquals(20, $vat, 'RULE 2');
  991. // Test RULE 3 (FR-DE company)
  992. $vat=get_default_tva($companyfr, $companyit, 0);
  993. $this->assertEquals(0, $vat, 'RULE 3');
  994. // Test RULE 4 (FR-DE not a company)
  995. $vat=get_default_tva($companyfr, $notcompanyde, 0);
  996. $this->assertEquals(20, $vat, 'RULE 4');
  997. // Test RULE 5 (FR-US)
  998. $vat=get_default_tva($companyfr, $companyus, 0);
  999. $this->assertEquals(0, $vat, 'RULE 5');
  1000. // We do same tests but with option SERVICE_ARE_ECOMMERCE_200238EC on.
  1001. $conf->global->SERVICE_ARE_ECOMMERCE_200238EC = 1;
  1002. // Test RULE 1 (FR-US)
  1003. $vat=get_default_tva($companyfr, $companyus, 0);
  1004. $this->assertEquals(0, $vat, 'RULE 1 ECOMMERCE_200238EC');
  1005. // Test RULE 2 (FR-FR)
  1006. $vat=get_default_tva($companyfr, $companyfr, 0);
  1007. $this->assertEquals(20, $vat, 'RULE 2 ECOMMERCE_200238EC');
  1008. // Test RULE 3 (FR-DE company)
  1009. $vat=get_default_tva($companyfr, $companyde, 0);
  1010. $this->assertEquals(0, $vat, 'RULE 3 ECOMMERCE_200238EC');
  1011. // Test RULE 4 (FR-DE not a company)
  1012. $vat=get_default_tva($companyfr, $notcompanyde, 0);
  1013. $this->assertEquals(19, $vat, 'RULE 4 ECOMMERCE_200238EC');
  1014. // Test RULE 5 (FR-US)
  1015. $vat=get_default_tva($companyfr, $companyus, 0);
  1016. $this->assertEquals(0, $vat, 'RULE 5 ECOMMERCE_200238EC');
  1017. }
  1018. /**
  1019. * testGetDefaultTva
  1020. *
  1021. * @return void
  1022. */
  1023. public function testGetDefaultLocalTax()
  1024. {
  1025. global $conf,$user,$langs,$db;
  1026. $this->savconf=$conf;
  1027. $this->savuser=$user;
  1028. $this->savlangs=$langs;
  1029. $this->savdb=$db;
  1030. $companyfrnovat=new Societe($db);
  1031. $companyfrnovat->country_code='FR';
  1032. $companyfrnovat->tva_assuj=0;
  1033. $companyfrnovat->localtax1_assuj=0;
  1034. $companyfrnovat->localtax2_assuj=0;
  1035. $companyes=new Societe($db);
  1036. $companyes->country_code='ES';
  1037. $companyes->tva_assuj=1;
  1038. $companyes->localtax1_assuj=1;
  1039. $companyes->localtax2_assuj=1;
  1040. $companymc=new Societe($db);
  1041. $companymc->country_code='MC';
  1042. $companymc->tva_assuj=1;
  1043. $companymc->localtax1_assuj=0;
  1044. $companymc->localtax2_assuj=0;
  1045. $companyit=new Societe($db);
  1046. $companyit->country_code='IT';
  1047. $companyit->tva_assuj=1;
  1048. $companyit->tva_intra='IT99999';
  1049. $companyit->localtax1_assuj=0;
  1050. $companyit->localtax2_assuj=0;
  1051. $notcompanyit=new Societe($db);
  1052. $notcompanyit->country_code='IT';
  1053. $notcompanyit->tva_assuj=1;
  1054. $notcompanyit->tva_intra='';
  1055. $notcompanyit->typent_code='TE_PRIVATE';
  1056. $notcompanyit->localtax1_assuj=0;
  1057. $notcompanyit->localtax2_assuj=0;
  1058. $companyus=new Societe($db);
  1059. $companyus->country_code='US';
  1060. $companyus->tva_assuj=1;
  1061. $companyus->tva_intra='';
  1062. $companyus->localtax1_assuj=0;
  1063. $companyus->localtax2_assuj=0;
  1064. // Test RULE FR-MC
  1065. $vat1=get_default_localtax($companyfrnovat, $companymc, 1, 0);
  1066. $vat2=get_default_localtax($companyfrnovat, $companymc, 2, 0);
  1067. $this->assertEquals(0, $vat1);
  1068. $this->assertEquals(0, $vat2);
  1069. // Test RULE ES-ES
  1070. $vat1=get_default_localtax($companyes, $companyes, 1, 0);
  1071. $vat2=get_default_localtax($companyes, $companyes, 2, 0);
  1072. $this->assertEquals($vat1, 5.2);
  1073. $this->assertStringStartsWith((string) $vat2, '-19:-15:-9'); // Can be -19 (old version) or '-19:-15:-9' (new setup)
  1074. // Test RULE ES-IT
  1075. $vat1=get_default_localtax($companyes, $companyit, 1, 0);
  1076. $vat2=get_default_localtax($companyes, $companyit, 2, 0);
  1077. $this->assertEquals(0, $vat1);
  1078. $this->assertEquals(0, $vat2);
  1079. // Test RULE ES-IT
  1080. $vat1=get_default_localtax($companyes, $notcompanyit, 1, 0);
  1081. $vat2=get_default_localtax($companyes, $notcompanyit, 2, 0);
  1082. $this->assertEquals(0, $vat1);
  1083. $this->assertEquals(0, $vat2);
  1084. // Test RULE FR-IT
  1085. // Not tested
  1086. // Test RULE ES-US
  1087. $vat1=get_default_localtax($companyes, $companyus, 1, 0);
  1088. $vat2=get_default_localtax($companyes, $companyus, 2, 0);
  1089. $this->assertEquals(0, $vat1);
  1090. $this->assertEquals(0, $vat2);
  1091. }
  1092. /**
  1093. * testDolExplodeIntoArray
  1094. *
  1095. * @return void
  1096. */
  1097. public function testDolExplodeIntoArray()
  1098. {
  1099. $stringtoexplode='AA=B/B.CC=.EE=FF.HH=GG;.';
  1100. $tmp=dolExplodeIntoArray($stringtoexplode, '.', '=');
  1101. print __METHOD__." tmp=".json_encode($tmp)."\n";
  1102. $this->assertEquals('{"AA":"B\/B","CC":"","EE":"FF","HH":"GG;"}', json_encode($tmp));
  1103. }
  1104. /**
  1105. * dol_nl2br
  1106. *
  1107. * @return void
  1108. */
  1109. public function testDolNl2Br()
  1110. {
  1111. //String to encode
  1112. $string = "a\na";
  1113. $this->assertEquals(dol_nl2br($string), "a<br>\na");
  1114. //With $forxml parameter
  1115. $this->assertEquals(dol_nl2br($string, 0, 1), "a<br />\na");
  1116. //Replacing \n by br
  1117. $this->assertEquals(dol_nl2br($string, 1), "a<br>a");
  1118. //With $forxml parameter
  1119. $this->assertEquals(dol_nl2br($string, 1, 1), "a<br />a");
  1120. }
  1121. /**
  1122. * testDolPrice2Num
  1123. *
  1124. * @return boolean
  1125. */
  1126. public function testDolPrice2Num()
  1127. {
  1128. global $langs, $conf;
  1129. $oldlangs = $langs;
  1130. $newlangs = new Translate('', $conf);
  1131. $newlangs->setDefaultLang('en_US');
  1132. $newlangs->load("main");
  1133. $langs = $newlangs;
  1134. $this->assertEquals(150, price2num('(SELECT/**/CASE/**/WHEN/**/(0<1)/**/THEN/**/SLEEP(5)/**/ELSE/**/SLEEP(0)/**/END)'));
  1135. $this->assertEquals(1000, price2num('1 000.0'));
  1136. $this->assertEquals(1000, price2num('1 000', 'MT'));
  1137. $this->assertEquals(1000, price2num('1 000', 'MU'));
  1138. $this->assertEquals(1000.123456, price2num('1 000.123456'));
  1139. // Round down
  1140. $this->assertEquals(1000.12, price2num('1 000.123452', 'MT'), 'Error in round down with MT');
  1141. $this->assertEquals(1000.12345, price2num('1 000.123452', 'MU'), "Test MU");
  1142. // Round up
  1143. $this->assertEquals(1000.13, price2num('1 000.125456', 'MT'));
  1144. $this->assertEquals(1000.12546, price2num('1 000.125456', 'MU'), "Test MU");
  1145. $this->assertEquals(1, price2num('1.000'), 'Test 1.000 give 1 with english language');
  1146. // Text can't be converted
  1147. $this->assertEquals('12.4', price2num('12.4$'));
  1148. $this->assertEquals('12.4', price2num('12r.4$'));
  1149. // For spanish language
  1150. $newlangs2 = new Translate('', $conf);
  1151. $newlangs2->setDefaultLang('es_ES');
  1152. $newlangs2->load("main");
  1153. $langs = $newlangs2;
  1154. // Test with 3 chars after . or ,
  1155. // If a . is used and there is 3 digits after, it is a thousand separator
  1156. $this->assertEquals(1234, price2num('1.234', '', 2), 'Test 1.234 give 1234 with spanish language if user input');
  1157. $this->assertEquals(1.234, price2num('1,234', '', 2), 'Test 1,234 give 1234 with spanish language if user input');
  1158. $this->assertEquals(1234, price2num('1 234', '', 2), 'Test 1 234 give 1234 with spanish language if user input');
  1159. $this->assertEquals(-1.234, price2num('-1.234'), 'Test 1.234 give 1.234 with spanish language');
  1160. $this->assertEquals(-1.234, price2num('-1,234'), 'Test 1,234 give 1234 with spanish language');
  1161. $this->assertEquals(-1234, price2num('-1 234'), 'Test 1 234 give 1234 with spanish language');
  1162. $this->assertEquals(21500123, price2num('21.500.123'), 'Test 21.500.123 give 21500123 with spanish language');
  1163. $this->assertEquals(21500123, price2num('21500.123', 0, 2), 'Test 21500.123 give 21500123 with spanish language if user input');
  1164. $this->assertEquals(21500.123, price2num('21500.123'), 'Test 21500.123 give 21500123 with spanish language');
  1165. $this->assertEquals(21500.123, price2num('21500,123'), 'Test 21500,123 give 21500.123 with spanish language');
  1166. // Test with 2 digits
  1167. $this->assertEquals(21500.12, price2num('21500.12'), 'Test 21500.12 give 21500.12 with spanish language');
  1168. $this->assertEquals(21500.12, price2num('21500,12'), 'Test 21500,12 give 21500.12 with spanish language');
  1169. // Test with 3 digits
  1170. $this->assertEquals(12123, price2num('12.123', '', 2), 'Test 12.123 give 12123 with spanish language if user input');
  1171. $this->assertEquals(12.123, price2num('12,123', '', 2), 'Test 12,123 give 12.123 with spanish language if user input');
  1172. $this->assertEquals(12.123, price2num('12.123'), 'Test 12.123 give 12.123 with spanish language');
  1173. $this->assertEquals(12.123, price2num('12,123'), 'Test 12,123 give 12.123 with spanish language');
  1174. // For french language
  1175. $newlangs3 = new Translate('', $conf);
  1176. $newlangs3->setDefaultLang('fr_FR');
  1177. $newlangs3->load("main");
  1178. $langs = $newlangs3;
  1179. $this->assertEquals(1, price2num('1.000', '', 2), 'Test 1.000 give 1 with french language if user input');
  1180. $this->assertEquals(1, price2num('1.000'), 'Test 1.000 give 1 with french language');
  1181. $this->assertEquals(1000, price2num('1 000'), 'Test 1.000 give 1 with french language');
  1182. $this->assertEquals(1.234, price2num('1.234', '', 2), 'Test 1.234 give 1.234 with french language if user input');
  1183. $this->assertEquals(1.234, price2num('1.234'), 'Test 1.234 give 1.234 with french language');
  1184. $this->assertEquals(1.234, price2num('1,234', '', 2), 'Test 1,234 give 1.234 with french language if user input');
  1185. $this->assertEquals(1.234, price2num('1,234'), 'Test 1,234 give 1.234 with french language');
  1186. $this->assertEquals(21500000, price2num('21500 000'), 'Test 21500 000 give 21500000 with french language');
  1187. $this->assertEquals(21500000, price2num('21 500 000'), 'Test 21 500 000 give 21500000 with french language');
  1188. $this->assertEquals(21500, price2num('21500.00'), 'Test 21500.00 give 21500 with french language');
  1189. $this->assertEquals(21500, price2num('21500,00'), 'Test 21500,00 give 21500 with french language');
  1190. $langs = $oldlangs;
  1191. return true;
  1192. }
  1193. /**
  1194. * testDolGetDate
  1195. *
  1196. * @return boolean
  1197. */
  1198. public function testDolGetDate()
  1199. {
  1200. global $conf;
  1201. $conf->global->MAIN_START_WEEK = 0;
  1202. $tmp=dol_getdate(24*60*60+1, false, 'UTC'); // 2/1/1970 and 1 second = friday
  1203. $this->assertEquals(5, $tmp['wday'], 'Bad value of day in week');
  1204. $conf->global->MAIN_START_WEEK = 1;
  1205. $tmp=dol_getdate(1, false, 'UTC'); // 1/1/1970 and 1 second = thirday
  1206. $this->assertEquals(4, $tmp['wday'], 'Bad value of day in week');
  1207. $tmp=dol_getdate(24*60*60+1, false, 'UTC'); // 2/1/1970 and 1 second = friday
  1208. $this->assertEquals(5, $tmp['wday'], 'Bad value of day in week');
  1209. $tmp=dol_getdate(1, false, "Europe/Paris"); // 1/1/1970 and 1 second = thirday
  1210. $this->assertEquals(1970, $tmp['year']);
  1211. $this->assertEquals(1, $tmp['mon']);
  1212. $this->assertEquals(1, $tmp['mday']);
  1213. $this->assertEquals(4, $tmp['wday']);
  1214. $this->assertEquals(0, $tmp['yday']);
  1215. $this->assertEquals(1, $tmp['hours']); // We are winter, so we are GMT+1 even during summer
  1216. $this->assertEquals(0, $tmp['minutes']);
  1217. $this->assertEquals(1, $tmp['seconds']);
  1218. $tmp=dol_getdate(15638401, false, "Europe/Paris"); // 1/7/1970 and 1 second = wednesday
  1219. $this->assertEquals(1970, $tmp['year']);
  1220. $this->assertEquals(7, $tmp['mon']);
  1221. $this->assertEquals(1, $tmp['mday']);
  1222. $this->assertEquals(3, $tmp['wday']);
  1223. $this->assertEquals(181, $tmp['yday']);
  1224. $this->assertEquals(1, $tmp['hours']); // There is no daylight in 1970, so we are GMT+1 even during summer
  1225. $this->assertEquals(0, $tmp['minutes']);
  1226. $this->assertEquals(1, $tmp['seconds']);
  1227. $tmp=dol_getdate(1593561601, false, "Europe/Paris"); // 1/7/2020 and 1 second = wednesday
  1228. $this->assertEquals(2020, $tmp['year']);
  1229. $this->assertEquals(7, $tmp['mon']);
  1230. $this->assertEquals(1, $tmp['mday']);
  1231. $this->assertEquals(3, $tmp['wday']);
  1232. $this->assertEquals(182, $tmp['yday']); // 182 and not 181, due to the 29th february
  1233. $this->assertEquals(2, $tmp['hours']); // There is a daylight, so we are GMT+2
  1234. $this->assertEquals(0, $tmp['minutes']);
  1235. $this->assertEquals(1, $tmp['seconds']);
  1236. $tmp=dol_getdate(1, false, 'UTC'); // 1/1/1970 and 1 second = thirday
  1237. $this->assertEquals(1970, $tmp['year']);
  1238. $this->assertEquals(1, $tmp['mon']);
  1239. $this->assertEquals(1, $tmp['mday']);
  1240. $this->assertEquals(4, $tmp['wday']);
  1241. $this->assertEquals(0, $tmp['yday']);
  1242. // We must disable this because on CI, timezone is may be UTC or something else
  1243. //$this->assertEquals(1, $tmp['hours']); // We are winter, so we are GMT+1 even during summer
  1244. $this->assertEquals(0, $tmp['minutes']);
  1245. $this->assertEquals(1, $tmp['seconds']);
  1246. $tmp=dol_getdate(15638401, false, 'UTC'); // 1/7/1970 and 1 second = wednesday
  1247. $this->assertEquals(1970, $tmp['year']);
  1248. $this->assertEquals(7, $tmp['mon']);
  1249. $this->assertEquals(1, $tmp['mday']);
  1250. $this->assertEquals(3, $tmp['wday']);
  1251. $this->assertEquals(181, $tmp['yday']);
  1252. // We must disable this because on CI, timezone is may be UTC or something else
  1253. //$this->assertEquals(1, $tmp['hours']); // There is no daylight in 1970, so we are GMT+1 even during summer
  1254. $this->assertEquals(0, $tmp['minutes']);
  1255. $this->assertEquals(1, $tmp['seconds']);
  1256. $tmp=dol_getdate(1593561601, false, 'UTC'); // 1/7/2020 and 1 second = wednesday
  1257. $this->assertEquals(2020, $tmp['year']);
  1258. $this->assertEquals(7, $tmp['mon']);
  1259. $this->assertEquals(1, $tmp['mday']);
  1260. $this->assertEquals(3, $tmp['wday']);
  1261. $this->assertEquals(182, $tmp['yday']); // 182 and not 181, due to the 29th february
  1262. // We must disable this because on CI, timezone is may be UTC or something else
  1263. //$this->assertEquals(2, $tmp['hours']); // There is a daylight, so we are GMT+2
  1264. $this->assertEquals(0, $tmp['minutes']);
  1265. $this->assertEquals(1, $tmp['seconds']);
  1266. return true;
  1267. }
  1268. /**
  1269. * testMakeSubstitutions
  1270. *
  1271. * @return boolean
  1272. */
  1273. public function testMakeSubstitutions()
  1274. {
  1275. global $conf, $langs, $mysoc;
  1276. $langs->load("main");
  1277. // Try simple replacement
  1278. $substit = array("__AAA__"=>'Not used', "__BBB__"=>'Not used', "__CCC__"=>"C replaced", "DDD"=>"D replaced");
  1279. $substit += getCommonSubstitutionArray($langs);
  1280. $chaine = 'This is a string with theme constant __[MAIN_THEME]__ and __(DIRECTION)__ and __CCC__ and DDD and __MYCOMPANY_NAME__ and __YEAR__';
  1281. $newstring = make_substitutions($chaine, $substit);
  1282. print __METHOD__." ".$newstring."\n";
  1283. $this->assertEquals($newstring, 'This is a string with theme constant eldy and ltr and C replaced and D replaced and '.$mysoc->name.' and '.dol_print_date(dol_now(), '%Y', 'gmt'));
  1284. // Try mix HTML not HTML, no change on initial text
  1285. $substit = array("__NOHTML__"=>'No html', "__HTML__"=>'<b>HTML</b>');
  1286. $chaine = "This is a text with\nNew line\nThen\n__NOHTML__\nThen\n__HTML__";
  1287. $newstring = make_substitutions($chaine, $substit, $langs);
  1288. print __METHOD__." ".$newstring."\n";
  1289. $this->assertEquals($newstring, "This is a text with\nNew line\nThen\nNo html\nThen\n<b>HTML</b>", 'Test on make_substitutions with conversion of inserted values only');
  1290. // Try mix HTML not HTML, accept to change initial text
  1291. $substit = array("__NOHTML__"=>'No html', "__HTML__"=>'<b>HTML</b>');
  1292. $chaine = "This is a text with\nNew line\nThen\n__NOHTML__\nThen\n__HTML__";
  1293. $newstring = make_substitutions($chaine, $substit, $langs, 1);
  1294. print __METHOD__." ".$newstring."\n";
  1295. $this->assertEquals($newstring, "This is a text with<br>\nNew line<br>\nThen<br>\nNo html<br>\nThen<br>\n<b>HTML</b>", 'Test on make_substitutions with full conversion of text accepted');
  1296. return true;
  1297. }
  1298. /**
  1299. * testDolStringIsGoodIso
  1300. *
  1301. * @return boolean
  1302. */
  1303. public function testDolStringIsGoodIso()
  1304. {
  1305. global $conf, $langs;
  1306. $chaine='This is an ISO string';
  1307. $result = dol_string_is_good_iso($chaine);
  1308. $this->assertEquals($result, 1);
  1309. $chaine='This is a not ISO string '.chr(0);
  1310. $result = dol_string_is_good_iso($chaine);
  1311. $this->assertEquals($result, 0);
  1312. return true;
  1313. }
  1314. /**
  1315. * testGetUserRemoteIP
  1316. *
  1317. * @return boolean
  1318. */
  1319. public function testGetUserRemoteIP()
  1320. {
  1321. global $conf, $langs;
  1322. $_SERVER['HTTP_X_FORWARDED_FOR']='1.2.3.4';
  1323. $_SERVER['HTTP_CLIENT_IP']='5.6.7.8';
  1324. $result = getUserRemoteIP();
  1325. $this->assertEquals($result, '1.2.3.4');
  1326. $_SERVER['HTTP_X_FORWARDED_FOR']='1.2.3.4<corrupted>';
  1327. $_SERVER['HTTP_CLIENT_IP']='5.6.7.8';
  1328. $result = getUserRemoteIP();
  1329. $this->assertEquals($result, '5.6.7.8');
  1330. $_SERVER['HTTP_X_FORWARDED_FOR']='[1:2:3:4]';
  1331. $_SERVER['HTTP_CLIENT_IP']='5.6.7.8';
  1332. $result = getUserRemoteIP();
  1333. $this->assertEquals($result, '[1:2:3:4]');
  1334. return true;
  1335. }
  1336. }