FunctionsLibTest.php 42 KB

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