FunctionsLibTest.php 50 KB

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