FunctionsLibTest.php 47 KB

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