FunctionsLibTest.php 52 KB

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