FunctionsLibTest.php 53 KB

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