FunctionsLibTest.php 55 KB

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