Functions2LibTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /* Copyright (C) 2010-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
  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/Functions2LibTest.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/functions2.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. /**
  61. * Class for PHPUnit tests
  62. *
  63. * @backupGlobals disabled
  64. * @backupStaticAttributes enabled
  65. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  66. */
  67. class Functions2LibTest extends PHPUnit\Framework\TestCase
  68. {
  69. protected $savconf;
  70. protected $savuser;
  71. protected $savlangs;
  72. protected $savdb;
  73. /**
  74. * Constructor
  75. * We save global variables into local variables
  76. *
  77. * @param string $name Name
  78. * @return CoreTest
  79. */
  80. public function __construct($name = '')
  81. {
  82. parent::__construct($name);
  83. //$this->sharedFixture
  84. global $conf,$user,$langs,$db;
  85. $this->savconf=$conf;
  86. $this->savuser=$user;
  87. $this->savlangs=$langs;
  88. $this->savdb=$db;
  89. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  90. //print " - db ".$db->db;
  91. print "\n";
  92. }
  93. /**
  94. * setUpBeforeClass
  95. *
  96. * @return void
  97. */
  98. public static function setUpBeforeClass(): void
  99. {
  100. global $conf,$user,$langs,$db;
  101. //$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  102. print __METHOD__."\n";
  103. }
  104. /**
  105. * tearDownAfterClass
  106. *
  107. * @return void
  108. */
  109. public static function tearDownAfterClass(): void
  110. {
  111. global $conf,$user,$langs,$db;
  112. //$db->rollback();
  113. print __METHOD__."\n";
  114. }
  115. /**
  116. * Init phpunit tests
  117. *
  118. * @return void
  119. */
  120. protected function setUp(): void
  121. {
  122. global $conf,$user,$langs,$db;
  123. $conf=$this->savconf;
  124. $user=$this->savuser;
  125. $langs=$this->savlangs;
  126. $db=$this->savdb;
  127. print __METHOD__."\n";
  128. }
  129. /**
  130. * End phpunit tests
  131. *
  132. * @return void
  133. */
  134. protected function tearDown(): void
  135. {
  136. print __METHOD__."\n";
  137. }
  138. /**
  139. * testJsUnEscape
  140. *
  141. * @return void
  142. */
  143. public function testJsUnEscape()
  144. {
  145. $result=jsUnEscape('%u03BD%u03B5%u03BF');
  146. print __METHOD__." result=".$result."\n";
  147. $this->assertEquals('νεο', $result);
  148. }
  149. /**
  150. * testIsValidMailDomain
  151. *
  152. * @return void
  153. */
  154. public function testIsValidMailDomain()
  155. {
  156. $mail = 'bidon@unvalid.unvalid';
  157. $result = isValidMailDomain($mail);
  158. $this->assertEquals(0, $result, 'Email isValidMailDomain('.$mail.') should return 0 (not valid) but returned '.$result);
  159. $mail = 'bidon@dolibarr.org';
  160. $result = isValidMailDomain($mail);
  161. $this->assertEquals(1, $result, 'Email isValidMailDomain('.$mail.') should return 1 (valid) but returned '.$result);
  162. }
  163. /**
  164. * testIsValidURL
  165. *
  166. * @return void
  167. */
  168. public function testIsValidUrl()
  169. {
  170. //Simple check
  171. $result = isValidUrl('http://google.com');
  172. $this->assertEquals(1, $result);
  173. $result = isValidUrl('goo=gle'); // This is good, it might be an alias of hostname
  174. $this->assertEquals(1, $result);
  175. //With scheme check
  176. $result = isValidUrl('http://www.google.com', 1);
  177. $this->assertEquals(1, $result);
  178. $result = isValidUrl('ftp://www.google.com', 1);
  179. $this->assertEquals(0, $result);
  180. //With password check invalid. This test should be ko but currently it is not
  181. //$result = isValidUrl('http://user:password@http://www.google.com', 1, 1);
  182. //$this->assertEquals(0, $result);
  183. //With password check valid
  184. $result = isValidUrl('http://user:password@www.google.com', 1, 1);
  185. $this->assertEquals(1, $result);
  186. $result = isValidUrl('http://www.google.com', 1, 1);
  187. $this->assertEquals(0, $result);
  188. //With port check
  189. $result = isValidUrl('http://google.com:8080', 0, 0, 1);
  190. $this->assertEquals(1, $result);
  191. $result = isValidUrl('http://google.com', 0, 0, 1);
  192. $this->assertEquals(0, $result);
  193. //With path check
  194. $result = isValidUrl('http://google.com/search', 0, 0, 0, 1);
  195. $this->assertEquals(1, $result);
  196. $result = isValidUrl('http://google.com', 0, 0, 0, 0);
  197. $this->assertEquals(1, $result);
  198. //With query check
  199. $result = isValidUrl('http://google.com/search?test=test', 0, 0, 0, 0, 1);
  200. $this->assertEquals(1, $result);
  201. //With query check
  202. $result = isValidUrl('http://google.com?test=test', 0, 0, 0, 0, 1);
  203. $this->assertEquals(1, $result);
  204. $result = isValidUrl('http://google.com', 0, 0, 0, 0, 1);
  205. $this->assertEquals(0, $result);
  206. //With anchor check
  207. $result = isValidUrl('http://google.com/search#done', 0, 0, 0, 0, 0, 1);
  208. $this->assertEquals(1, $result);
  209. $result = isValidUrl('http://google.com/search', 0, 0, 0, 0, 0, 1);
  210. $this->assertEquals(0, $result);
  211. }
  212. /**
  213. * testIsIP
  214. *
  215. * @return void
  216. */
  217. public function testIsIP()
  218. {
  219. // Not valid
  220. $ip='a299.299.299.299';
  221. $result=is_ip($ip);
  222. print __METHOD__." for ".$ip." result=".$result."\n";
  223. $this->assertEquals(0, $result, $ip);
  224. // Reserved IP range (not checked by is_ip function)
  225. $ip='169.254.0.0';
  226. $result=is_ip($ip);
  227. print __METHOD__." for ".$ip." result=".$result."\n";
  228. //$this->assertEquals(2,$result,$ip); // Assertion disabled because returned value differs between PHP patch version
  229. $ip='1.2.3.4';
  230. $result=is_ip($ip);
  231. print __METHOD__." for ".$ip." result=".$result."\n";
  232. $this->assertEquals(1, $result, $ip);
  233. // Private IP ranges
  234. $ip='10.0.0.0';
  235. $result=is_ip($ip);
  236. print __METHOD__." for ".$ip." result=".$result."\n";
  237. $this->assertEquals(2, $result, $ip);
  238. $ip='172.16.0.0';
  239. $result=is_ip($ip);
  240. print __METHOD__." for ".$ip." result=".$result."\n";
  241. $this->assertEquals(2, $result, $ip);
  242. $ip='192.168.0.0';
  243. $result=is_ip($ip);
  244. print __METHOD__." for ".$ip." result=".$result."\n";
  245. $this->assertEquals(2, $result, $ip);
  246. }
  247. }