FunctionsLibTest.php 34 KB

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