FunctionsLibTest.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  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. }
  800. /**
  801. * testDolFormatAddress
  802. *
  803. * @return void
  804. */
  805. public function testDolPrintPhone()
  806. {
  807. global $conf,$user,$langs,$db;
  808. $conf=$this->savconf;
  809. $user=$this->savuser;
  810. $langs=$this->savlangs;
  811. $db=$this->savdb;
  812. $object=new Societe($db);
  813. $object->initAsSpecimen();
  814. $object->country_code='FR';
  815. $phone=dol_print_phone('1234567890', $object->country_code);
  816. $this->assertEquals('<span style="margin-right: 10px;">12&nbsp;34&nbsp;56&nbsp;78&nbsp;90</span>', $phone, 'Phone for FR 1');
  817. $object->country_code='FR';
  818. $phone=dol_print_phone('1234567890', $object->country_code, 0, 0, 0, '');
  819. $this->assertEquals('<span style="margin-right: 10px;">1234567890</span>', $phone, 'Phone for FR 2');
  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;">12 34 56 78 90</span>', $phone, 'Phone for FR 3');
  823. $object->country_code='CA';
  824. $phone=dol_print_phone('1234567890', $object->country_code, 0, 0, 0, ' ');
  825. $this->assertEquals('<span style="margin-right: 10px;">(123) 456-7890</span>', $phone, 'Phone for CA 1');
  826. }
  827. /**
  828. * testImgPicto
  829. *
  830. * @return void
  831. */
  832. public function testImgPicto()
  833. {
  834. $s=img_picto('title', 'user');
  835. print __METHOD__." s=".$s."\n";
  836. $this->assertContains('fa-user', $s, 'testImgPicto1');
  837. $s=img_picto('title', 'img.png', 'style="float: right"', 0);
  838. print __METHOD__." s=".$s."\n";
  839. $this->assertContains('theme', $s, 'testImgPicto2');
  840. $this->assertContains('style="float: right"', $s, 'testImgPicto2');
  841. $s=img_picto('title', '/fullpath/img.png', '', 1);
  842. print __METHOD__." s=".$s."\n";
  843. $this->assertEquals('<img src="/fullpath/img.png" alt="" title="title" class="inline-block">', $s, 'testImgPicto3');
  844. $s=img_picto('title', '/fullpath/img.png', '', true);
  845. print __METHOD__." s=".$s."\n";
  846. $this->assertEquals('<img src="/fullpath/img.png" alt="" title="title" class="inline-block">', $s, 'testImgPicto4');
  847. $s=img_picto('title', 'delete', '', 0, 1);
  848. print __METHOD__." s=".$s."\n";
  849. $this->assertEquals(DOL_URL_ROOT.'/theme/eldy/img/delete.png', $s, 'testImgPicto5');
  850. }
  851. /**
  852. * testDolNow
  853. *
  854. * @return void
  855. */
  856. public function testDolNow()
  857. {
  858. $now=dol_now('gmt');
  859. $nowtzserver=dol_now('tzserver');
  860. print __METHOD__." getServerTimeZoneInt=".(getServerTimeZoneInt('now')*3600)."\n";
  861. $this->assertEquals(getServerTimeZoneInt('now')*3600, ($nowtzserver-$now));
  862. }
  863. /**
  864. * testVerifCond
  865. *
  866. * @return void
  867. */
  868. public function testVerifCond()
  869. {
  870. $verifcond=verifCond('1==1');
  871. $this->assertTrue($verifcond, 'Test a true comparison');
  872. $verifcond=verifCond('1==2');
  873. $this->assertFalse($verifcond, 'Test a false comparison');
  874. $verifcond=verifCond('$conf->facture->enabled');
  875. $this->assertTrue($verifcond, 'Test that conf property of a module report true when enabled');
  876. $verifcond=verifCond('$conf->moduledummy->enabled');
  877. $this->assertFalse($verifcond, 'Test that conf property of a module report false when disabled');
  878. $verifcond=verifCond('');
  879. $this->assertTrue($verifcond);
  880. }
  881. /**
  882. * testGetDefaultTva
  883. *
  884. * @return void
  885. */
  886. public function testGetDefaultTva()
  887. {
  888. global $conf,$user,$langs,$db;
  889. $this->savconf=$conf;
  890. $this->savuser=$user;
  891. $this->savlangs=$langs;
  892. $this->savdb=$db;
  893. // Sellers
  894. $companyfrnovat=new Societe($db);
  895. $companyfrnovat->country_code='FR';
  896. $companyfrnovat->tva_assuj=0;
  897. $companyfr=new Societe($db);
  898. $companyfr->country_code='FR';
  899. $companyfr->tva_assuj=1;
  900. $companyfr->tva_intra='FR9999';
  901. // Buyers
  902. $companymc=new Societe($db);
  903. $companymc->country_code='MC';
  904. $companymc->tva_assuj=1;
  905. $companyfr->tva_intra='MC9999';
  906. $companyit=new Societe($db);
  907. $companyit->country_code='IT';
  908. $companyit->tva_assuj=1;
  909. $companyit->tva_intra='IT99999';
  910. $companyde=new Societe($db);
  911. $companyde->country_code='DE';
  912. $companyde->tva_assuj=1;
  913. $companyde->tva_intra='DE99999';
  914. $notcompanyde=new Societe($db);
  915. $notcompanyde->country_code='DE';
  916. $notcompanyde->tva_assuj=0;
  917. $notcompanyde->tva_intra='';
  918. $notcompanyde->typent_code='TE_PRIVATE';
  919. $companyus=new Societe($db);
  920. $companyus->country_code='US';
  921. $companyus->tva_assuj=1;
  922. $companyus->tva_intra='';
  923. // Test RULE 0 (FR-DE)
  924. // Not tested
  925. // Test RULE 1
  926. $vat=get_default_tva($companyfrnovat, $companymc, 0);
  927. $this->assertEquals(0, $vat, 'RULE 1');
  928. // Test RULE 2 (FR-FR)
  929. $vat=get_default_tva($companyfr, $companyfr, 0);
  930. $this->assertEquals(20, $vat, 'RULE 2');
  931. // Test RULE 2 (FR-MC)
  932. $vat=get_default_tva($companyfr, $companymc, 0);
  933. $this->assertEquals(20, $vat, 'RULE 2');
  934. // Test RULE 3 (FR-DE company)
  935. $vat=get_default_tva($companyfr, $companyit, 0);
  936. $this->assertEquals(0, $vat, 'RULE 3');
  937. // Test RULE 4 (FR-DE not a company)
  938. $vat=get_default_tva($companyfr, $notcompanyde, 0);
  939. $this->assertEquals(20, $vat, 'RULE 4');
  940. // Test RULE 5 (FR-US)
  941. $vat=get_default_tva($companyfr, $companyus, 0);
  942. $this->assertEquals(0, $vat, 'RULE 5');
  943. // We do same tests but with option SERVICE_ARE_ECOMMERCE_200238EC on.
  944. $conf->global->SERVICE_ARE_ECOMMERCE_200238EC = 1;
  945. // Test RULE 1 (FR-US)
  946. $vat=get_default_tva($companyfr, $companyus, 0);
  947. $this->assertEquals(0, $vat, 'RULE 1 ECOMMERCE_200238EC');
  948. // Test RULE 2 (FR-FR)
  949. $vat=get_default_tva($companyfr, $companyfr, 0);
  950. $this->assertEquals(20, $vat, 'RULE 2 ECOMMERCE_200238EC');
  951. // Test RULE 3 (FR-DE company)
  952. $vat=get_default_tva($companyfr, $companyde, 0);
  953. $this->assertEquals(0, $vat, 'RULE 3 ECOMMERCE_200238EC');
  954. // Test RULE 4 (FR-DE not a company)
  955. $vat=get_default_tva($companyfr, $notcompanyde, 0);
  956. $this->assertEquals(19, $vat, 'RULE 4 ECOMMERCE_200238EC');
  957. // Test RULE 5 (FR-US)
  958. $vat=get_default_tva($companyfr, $companyus, 0);
  959. $this->assertEquals(0, $vat, 'RULE 5 ECOMMERCE_200238EC');
  960. }
  961. /**
  962. * testGetDefaultTva
  963. *
  964. * @return void
  965. */
  966. public function testGetDefaultLocalTax()
  967. {
  968. global $conf,$user,$langs,$db;
  969. $this->savconf=$conf;
  970. $this->savuser=$user;
  971. $this->savlangs=$langs;
  972. $this->savdb=$db;
  973. $companyfrnovat=new Societe($db);
  974. $companyfrnovat->country_code='FR';
  975. $companyfrnovat->tva_assuj=0;
  976. $companyfrnovat->localtax1_assuj=0;
  977. $companyfrnovat->localtax2_assuj=0;
  978. $companyes=new Societe($db);
  979. $companyes->country_code='ES';
  980. $companyes->tva_assuj=1;
  981. $companyes->localtax1_assuj=1;
  982. $companyes->localtax2_assuj=1;
  983. $companymc=new Societe($db);
  984. $companymc->country_code='MC';
  985. $companymc->tva_assuj=1;
  986. $companymc->localtax1_assuj=0;
  987. $companymc->localtax2_assuj=0;
  988. $companyit=new Societe($db);
  989. $companyit->country_code='IT';
  990. $companyit->tva_assuj=1;
  991. $companyit->tva_intra='IT99999';
  992. $companyit->localtax1_assuj=0;
  993. $companyit->localtax2_assuj=0;
  994. $notcompanyit=new Societe($db);
  995. $notcompanyit->country_code='IT';
  996. $notcompanyit->tva_assuj=1;
  997. $notcompanyit->tva_intra='';
  998. $notcompanyit->typent_code='TE_PRIVATE';
  999. $notcompanyit->localtax1_assuj=0;
  1000. $notcompanyit->localtax2_assuj=0;
  1001. $companyus=new Societe($db);
  1002. $companyus->country_code='US';
  1003. $companyus->tva_assuj=1;
  1004. $companyus->tva_intra='';
  1005. $companyus->localtax1_assuj=0;
  1006. $companyus->localtax2_assuj=0;
  1007. // Test RULE FR-MC
  1008. $vat1=get_default_localtax($companyfrnovat, $companymc, 1, 0);
  1009. $vat2=get_default_localtax($companyfrnovat, $companymc, 2, 0);
  1010. $this->assertEquals(0, $vat1);
  1011. $this->assertEquals(0, $vat2);
  1012. // Test RULE ES-ES
  1013. $vat1=get_default_localtax($companyes, $companyes, 1, 0);
  1014. $vat2=get_default_localtax($companyes, $companyes, 2, 0);
  1015. $this->assertEquals($vat1, 5.2);
  1016. $this->assertStringStartsWith((string) $vat2, '-19:-15:-9'); // Can be -19 (old version) or '-19:-15:-9' (new setup)
  1017. // Test RULE ES-IT
  1018. $vat1=get_default_localtax($companyes, $companyit, 1, 0);
  1019. $vat2=get_default_localtax($companyes, $companyit, 2, 0);
  1020. $this->assertEquals(0, $vat1);
  1021. $this->assertEquals(0, $vat2);
  1022. // Test RULE ES-IT
  1023. $vat1=get_default_localtax($companyes, $notcompanyit, 1, 0);
  1024. $vat2=get_default_localtax($companyes, $notcompanyit, 2, 0);
  1025. $this->assertEquals(0, $vat1);
  1026. $this->assertEquals(0, $vat2);
  1027. // Test RULE FR-IT
  1028. // Not tested
  1029. // Test RULE ES-US
  1030. $vat1=get_default_localtax($companyes, $companyus, 1, 0);
  1031. $vat2=get_default_localtax($companyes, $companyus, 2, 0);
  1032. $this->assertEquals(0, $vat1);
  1033. $this->assertEquals(0, $vat2);
  1034. }
  1035. /**
  1036. * testDolExplodeIntoArray
  1037. *
  1038. * @return void
  1039. */
  1040. public function testDolExplodeIntoArray()
  1041. {
  1042. $stringtoexplode='AA=B/B.CC=.EE=FF.HH=GG;.';
  1043. $tmp=dolExplodeIntoArray($stringtoexplode, '.', '=');
  1044. print __METHOD__." tmp=".json_encode($tmp)."\n";
  1045. $this->assertEquals('{"AA":"B\/B","CC":"","EE":"FF","HH":"GG;"}', json_encode($tmp));
  1046. }
  1047. /**
  1048. * dol_nl2br
  1049. *
  1050. * @return void
  1051. */
  1052. public function testDolNl2Br()
  1053. {
  1054. //String to encode
  1055. $string = "a\na";
  1056. $this->assertEquals(dol_nl2br($string), "a<br>\na");
  1057. //With $forxml parameter
  1058. $this->assertEquals(dol_nl2br($string, 0, 1), "a<br />\na");
  1059. //Replacing \n by br
  1060. $this->assertEquals(dol_nl2br($string, 1), "a<br>a");
  1061. //With $forxml parameter
  1062. $this->assertEquals(dol_nl2br($string, 1, 1), "a<br />a");
  1063. }
  1064. /**
  1065. * testDolPrice2Num
  1066. *
  1067. * @return boolean
  1068. */
  1069. public function testDolPrice2Num()
  1070. {
  1071. global $langs, $conf;
  1072. $oldlangs = $langs;
  1073. $newlangs = new Translate('', $conf);
  1074. $newlangs->setDefaultLang('en_US');
  1075. $newlangs->load("main");
  1076. $langs = $newlangs;
  1077. $this->assertEquals(150, price2num('(SELECT/**/CASE/**/WHEN/**/(0<1)/**/THEN/**/SLEEP(5)/**/ELSE/**/SLEEP(0)/**/END)'));
  1078. $this->assertEquals(1000, price2num('1 000.0'));
  1079. $this->assertEquals(1000, price2num('1 000', 'MT'));
  1080. $this->assertEquals(1000, price2num('1 000', 'MU'));
  1081. $this->assertEquals(1000.123456, price2num('1 000.123456'));
  1082. // Round down
  1083. $this->assertEquals(1000.12, price2num('1 000.123452', 'MT'), 'Error in round down with MT');
  1084. $this->assertEquals(1000.12345, price2num('1 000.123452', 'MU'), "Test MU");
  1085. // Round up
  1086. $this->assertEquals(1000.13, price2num('1 000.125456', 'MT'));
  1087. $this->assertEquals(1000.12546, price2num('1 000.125456', 'MU'), "Test MU");
  1088. $this->assertEquals(1, price2num('1.000'), 'Test 1.000 give 1 with english language');
  1089. // Text can't be converted
  1090. $this->assertEquals('12.4$', price2num('12.4$'));
  1091. $this->assertEquals('12.4$', price2num('12r.4$'));
  1092. // For spanish language
  1093. $newlangs2 = new Translate('', $conf);
  1094. $newlangs2->setDefaultLang('es_ES');
  1095. $newlangs2->load("main");
  1096. $langs = $newlangs2;
  1097. // Test with 3 chars after . or ,
  1098. // If a . is used and there is 3 digits after, it is a thousand separator
  1099. $this->assertEquals(1234, price2num('1.234', '', 2), 'Test 1.234 give 1234 with spanish language if user input');
  1100. $this->assertEquals(1.234, price2num('1,234', '', 2), 'Test 1,234 give 1234 with spanish language if user input');
  1101. $this->assertEquals(1234, price2num('1 234', '', 2), 'Test 1 234 give 1234 with spanish language if user input');
  1102. $this->assertEquals(-1.234, price2num('-1.234'), 'Test 1.234 give 1.234 with spanish language');
  1103. $this->assertEquals(-1.234, price2num('-1,234'), 'Test 1,234 give 1234 with spanish language');
  1104. $this->assertEquals(-1234, price2num('-1 234'), 'Test 1 234 give 1234 with spanish language');
  1105. $this->assertEquals(21500123, price2num('21.500.123'), 'Test 21.500.123 give 21500123 with spanish language');
  1106. $this->assertEquals(21500123, price2num('21500.123', 0, 2), 'Test 21500.123 give 21500123 with spanish language if user input');
  1107. $this->assertEquals(21500.123, price2num('21500.123'), 'Test 21500.123 give 21500123 with spanish language');
  1108. $this->assertEquals(21500.123, price2num('21500,123'), 'Test 21500,123 give 21500.123 with spanish language');
  1109. // Test with 2 digits
  1110. $this->assertEquals(21500.12, price2num('21500.12'), 'Test 21500.12 give 21500.12 with spanish language');
  1111. $this->assertEquals(21500.12, price2num('21500,12'), 'Test 21500,12 give 21500.12 with spanish language');
  1112. // Test with 3 digits
  1113. $this->assertEquals(12123, price2num('12.123', '', 2), 'Test 12.123 give 12123 with spanish language if user input');
  1114. $this->assertEquals(12.123, price2num('12,123', '', 2), 'Test 12,123 give 12.123 with spanish language if user input');
  1115. $this->assertEquals(12.123, price2num('12.123'), 'Test 12.123 give 12.123 with spanish language');
  1116. $this->assertEquals(12.123, price2num('12,123'), 'Test 12,123 give 12.123 with spanish language');
  1117. // For french language
  1118. $newlangs3 = new Translate('', $conf);
  1119. $newlangs3->setDefaultLang('fr_FR');
  1120. $newlangs3->load("main");
  1121. $langs = $newlangs3;
  1122. $this->assertEquals(1, price2num('1.000', '', 2), 'Test 1.000 give 1 with french language if user input');
  1123. $this->assertEquals(1, price2num('1.000'), 'Test 1.000 give 1 with french language');
  1124. $this->assertEquals(1000, price2num('1 000'), 'Test 1.000 give 1 with french language');
  1125. $this->assertEquals(1.234, price2num('1.234', '', 2), 'Test 1.234 give 1.234 with french language if user input');
  1126. $this->assertEquals(1.234, price2num('1.234'), 'Test 1.234 give 1.234 with french language');
  1127. $this->assertEquals(1.234, price2num('1,234', '', 2), 'Test 1,234 give 1.234 with french language if user input');
  1128. $this->assertEquals(1.234, price2num('1,234'), 'Test 1,234 give 1.234 with french language');
  1129. $this->assertEquals(21500000, price2num('21500 000'), 'Test 21500 000 give 21500000 with french language');
  1130. $this->assertEquals(21500000, price2num('21 500 000'), 'Test 21 500 000 give 21500000 with french language');
  1131. $this->assertEquals(21500, price2num('21500.00'), 'Test 21500.00 give 21500 with french language');
  1132. $this->assertEquals(21500, price2num('21500,00'), 'Test 21500,00 give 21500 with french language');
  1133. $langs = $oldlangs;
  1134. return true;
  1135. }
  1136. /**
  1137. * testDolGetDate
  1138. *
  1139. * @return boolean
  1140. */
  1141. public function testDolGetDate()
  1142. {
  1143. global $conf;
  1144. $conf->global->MAIN_START_WEEK = 0;
  1145. $tmp=dol_getdate(24*60*60+1, false, 'UTC'); // 2/1/1970 and 1 second = friday
  1146. $this->assertEquals(5, $tmp['wday'], 'Bad value of day in week');
  1147. $conf->global->MAIN_START_WEEK = 1;
  1148. $tmp=dol_getdate(1, false, 'UTC'); // 1/1/1970 and 1 second = thirday
  1149. $this->assertEquals(4, $tmp['wday'], 'Bad value of day in week');
  1150. $tmp=dol_getdate(24*60*60+1, false, 'UTC'); // 2/1/1970 and 1 second = friday
  1151. $this->assertEquals(5, $tmp['wday'], 'Bad value of day in week');
  1152. $tmp=dol_getdate(1, false, "Europe/Paris"); // 1/1/1970 and 1 second = thirday
  1153. $this->assertEquals(1970, $tmp['year']);
  1154. $this->assertEquals(1, $tmp['mon']);
  1155. $this->assertEquals(1, $tmp['mday']);
  1156. $this->assertEquals(4, $tmp['wday']);
  1157. $this->assertEquals(0, $tmp['yday']);
  1158. $this->assertEquals(1, $tmp['hours']); // We are winter, so we are GMT+1 even during summer
  1159. $this->assertEquals(0, $tmp['minutes']);
  1160. $this->assertEquals(1, $tmp['seconds']);
  1161. $tmp=dol_getdate(15638401, false, "Europe/Paris"); // 1/7/1970 and 1 second = wednesday
  1162. $this->assertEquals(1970, $tmp['year']);
  1163. $this->assertEquals(7, $tmp['mon']);
  1164. $this->assertEquals(1, $tmp['mday']);
  1165. $this->assertEquals(3, $tmp['wday']);
  1166. $this->assertEquals(181, $tmp['yday']);
  1167. $this->assertEquals(1, $tmp['hours']); // There is no daylight in 1970, so we are GMT+1 even during summer
  1168. $this->assertEquals(0, $tmp['minutes']);
  1169. $this->assertEquals(1, $tmp['seconds']);
  1170. $tmp=dol_getdate(1593561601, false, "Europe/Paris"); // 1/7/2020 and 1 second = wednesday
  1171. $this->assertEquals(2020, $tmp['year']);
  1172. $this->assertEquals(7, $tmp['mon']);
  1173. $this->assertEquals(1, $tmp['mday']);
  1174. $this->assertEquals(3, $tmp['wday']);
  1175. $this->assertEquals(182, $tmp['yday']); // 182 and not 181, due to the 29th february
  1176. $this->assertEquals(2, $tmp['hours']); // There is a daylight, so we are GMT+2
  1177. $this->assertEquals(0, $tmp['minutes']);
  1178. $this->assertEquals(1, $tmp['seconds']);
  1179. $tmp=dol_getdate(1, false, 'UTC'); // 1/1/1970 and 1 second = thirday
  1180. $this->assertEquals(1970, $tmp['year']);
  1181. $this->assertEquals(1, $tmp['mon']);
  1182. $this->assertEquals(1, $tmp['mday']);
  1183. $this->assertEquals(4, $tmp['wday']);
  1184. $this->assertEquals(0, $tmp['yday']);
  1185. // We must disable this because on CI, timezone is may be UTC or something else
  1186. //$this->assertEquals(1, $tmp['hours']); // We are winter, so we are GMT+1 even during summer
  1187. $this->assertEquals(0, $tmp['minutes']);
  1188. $this->assertEquals(1, $tmp['seconds']);
  1189. $tmp=dol_getdate(15638401, false, 'UTC'); // 1/7/1970 and 1 second = wednesday
  1190. $this->assertEquals(1970, $tmp['year']);
  1191. $this->assertEquals(7, $tmp['mon']);
  1192. $this->assertEquals(1, $tmp['mday']);
  1193. $this->assertEquals(3, $tmp['wday']);
  1194. $this->assertEquals(181, $tmp['yday']);
  1195. // We must disable this because on CI, timezone is may be UTC or something else
  1196. //$this->assertEquals(1, $tmp['hours']); // There is no daylight in 1970, so we are GMT+1 even during summer
  1197. $this->assertEquals(0, $tmp['minutes']);
  1198. $this->assertEquals(1, $tmp['seconds']);
  1199. $tmp=dol_getdate(1593561601, false, 'UTC'); // 1/7/2020 and 1 second = wednesday
  1200. $this->assertEquals(2020, $tmp['year']);
  1201. $this->assertEquals(7, $tmp['mon']);
  1202. $this->assertEquals(1, $tmp['mday']);
  1203. $this->assertEquals(3, $tmp['wday']);
  1204. $this->assertEquals(182, $tmp['yday']); // 182 and not 181, due to the 29th february
  1205. // We must disable this because on CI, timezone is may be UTC or something else
  1206. //$this->assertEquals(2, $tmp['hours']); // There is a daylight, so we are GMT+2
  1207. $this->assertEquals(0, $tmp['minutes']);
  1208. $this->assertEquals(1, $tmp['seconds']);
  1209. return true;
  1210. }
  1211. /**
  1212. * testMakeSubstitutions
  1213. *
  1214. * @return boolean
  1215. */
  1216. public function testMakeSubstitutions()
  1217. {
  1218. global $conf, $langs, $mysoc;
  1219. $langs->load("main");
  1220. // Try simple replacement
  1221. $substit = array("__AAA__"=>'Not used', "__BBB__"=>'Not used', "__CCC__"=>"C replaced", "DDD"=>"D replaced");
  1222. $substit += getCommonSubstitutionArray($langs);
  1223. $chaine = 'This is a string with theme constant __[MAIN_THEME]__ and __(DIRECTION)__ and __CCC__ and DDD and __MYCOMPANY_NAME__ and __YEAR__';
  1224. $newstring = make_substitutions($chaine, $substit);
  1225. print __METHOD__." ".$newstring."\n";
  1226. $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'));
  1227. // Try mix HTML not HTML, no change on initial text
  1228. $substit = array("__NOHTML__"=>'No html', "__HTML__"=>'<b>HTML</b>');
  1229. $chaine = "This is a text with\nNew line\nThen\n__NOHTML__\nThen\n__HTML__";
  1230. $newstring = make_substitutions($chaine, $substit, $langs);
  1231. print __METHOD__." ".$newstring."\n";
  1232. $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');
  1233. // Try mix HTML not HTML, accept to change initial text
  1234. $substit = array("__NOHTML__"=>'No html', "__HTML__"=>'<b>HTML</b>');
  1235. $chaine = "This is a text with\nNew line\nThen\n__NOHTML__\nThen\n__HTML__";
  1236. $newstring = make_substitutions($chaine, $substit, $langs, 1);
  1237. print __METHOD__." ".$newstring."\n";
  1238. $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');
  1239. return true;
  1240. }
  1241. /**
  1242. * testDolStringIsGoodIso
  1243. *
  1244. * @return boolean
  1245. */
  1246. public function testDolStringIsGoodIso()
  1247. {
  1248. global $conf, $langs;
  1249. $chaine='This is an ISO string';
  1250. $result = dol_string_is_good_iso($chaine);
  1251. $this->assertEquals($result, 1);
  1252. $chaine='This is a not ISO string '.chr(0);
  1253. $result = dol_string_is_good_iso($chaine);
  1254. $this->assertEquals($result, 0);
  1255. return true;
  1256. }
  1257. /**
  1258. * testGetUserRemoteIP
  1259. *
  1260. * @return boolean
  1261. */
  1262. public function testGetUserRemoteIP()
  1263. {
  1264. global $conf, $langs;
  1265. $_SERVER['HTTP_X_FORWARDED_FOR']='1.2.3.4';
  1266. $_SERVER['HTTP_CLIENT_IP']='5.6.7.8';
  1267. $result = getUserRemoteIP();
  1268. $this->assertEquals($result, '1.2.3.4');
  1269. $_SERVER['HTTP_X_FORWARDED_FOR']='1.2.3.4<corrupted>';
  1270. $_SERVER['HTTP_CLIENT_IP']='5.6.7.8';
  1271. $result = getUserRemoteIP();
  1272. $this->assertEquals($result, '5.6.7.8');
  1273. $_SERVER['HTTP_X_FORWARDED_FOR']='[1:2:3:4]';
  1274. $_SERVER['HTTP_CLIENT_IP']='5.6.7.8';
  1275. $result = getUserRemoteIP();
  1276. $this->assertEquals($result, '[1:2:3:4]');
  1277. return true;
  1278. }
  1279. }