FunctionsLibTest.php 51 KB

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