Functions2LibTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /* Copyright (C) 2010-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file test/phpunit/Functions2LibTest.php
  20. * \ingroup test
  21. * \brief PHPUnit test
  22. * \remarks To run this script as CLI: phpunit filename.php
  23. */
  24. global $conf,$user,$langs,$db;
  25. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  26. //require_once 'PHPUnit/Autoload.php';
  27. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  28. require_once dirname(__FILE__).'/../../htdocs/core/lib/functions2.lib.php';
  29. if (! defined('NOREQUIREUSER')) {
  30. define('NOREQUIREUSER', '1');
  31. }
  32. if (! defined('NOREQUIREDB')) {
  33. define('NOREQUIREDB', '1');
  34. }
  35. if (! defined('NOREQUIRESOC')) {
  36. define('NOREQUIRESOC', '1');
  37. }
  38. if (! defined('NOREQUIRETRAN')) {
  39. define('NOREQUIRETRAN', '1');
  40. }
  41. if (! defined('NOCSRFCHECK')) {
  42. define('NOCSRFCHECK', '1');
  43. }
  44. if (! defined('NOTOKENRENEWAL')) {
  45. define('NOTOKENRENEWAL', '1');
  46. }
  47. if (! defined('NOREQUIREMENU')) {
  48. define('NOREQUIREMENU', '1'); // If there is no menu to show
  49. }
  50. if (! defined('NOREQUIREHTML')) {
  51. define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  52. }
  53. if (! defined('NOREQUIREAJAX')) {
  54. define('NOREQUIREAJAX', '1');
  55. }
  56. if (! defined("NOLOGIN")) {
  57. define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
  58. }
  59. /**
  60. * Class for PHPUnit tests
  61. *
  62. * @backupGlobals disabled
  63. * @backupStaticAttributes enabled
  64. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  65. */
  66. class Functions2LibTest extends PHPUnit\Framework\TestCase
  67. {
  68. protected $savconf;
  69. protected $savuser;
  70. protected $savlangs;
  71. protected $savdb;
  72. /**
  73. * Constructor
  74. * We save global variables into local variables
  75. *
  76. * @return CoreTest
  77. */
  78. public function __construct()
  79. {
  80. parent::__construct();
  81. //$this->sharedFixture
  82. global $conf,$user,$langs,$db;
  83. $this->savconf=$conf;
  84. $this->savuser=$user;
  85. $this->savlangs=$langs;
  86. $this->savdb=$db;
  87. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  88. //print " - db ".$db->db;
  89. print "\n";
  90. }
  91. /**
  92. * setUpBeforeClass
  93. *
  94. * @return void
  95. */
  96. public static function setUpBeforeClass()
  97. {
  98. global $conf,$user,$langs,$db;
  99. //$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  100. print __METHOD__."\n";
  101. }
  102. /**
  103. * tearDownAfterClass
  104. *
  105. * @return void
  106. */
  107. public static function tearDownAfterClass()
  108. {
  109. global $conf,$user,$langs,$db;
  110. //$db->rollback();
  111. print __METHOD__."\n";
  112. }
  113. /**
  114. * Init phpunit tests
  115. *
  116. * @return void
  117. */
  118. protected function setUp()
  119. {
  120. global $conf,$user,$langs,$db;
  121. $conf=$this->savconf;
  122. $user=$this->savuser;
  123. $langs=$this->savlangs;
  124. $db=$this->savdb;
  125. print __METHOD__."\n";
  126. }
  127. /**
  128. * End phpunit tests
  129. *
  130. * @return void
  131. */
  132. protected function tearDown()
  133. {
  134. print __METHOD__."\n";
  135. }
  136. /**
  137. * testJsUnEscape
  138. *
  139. * @return void
  140. */
  141. public function testJsUnEscape()
  142. {
  143. $result=jsUnEscape('%u03BD%u03B5%u03BF');
  144. print __METHOD__." result=".$result."\n";
  145. $this->assertEquals('νεο', $result);
  146. }
  147. /**
  148. * testIsValidMailDomain
  149. *
  150. * @return void
  151. */
  152. public function testIsValidMailDomain()
  153. {
  154. $mail = 'bidon@unvalid.unvalid';
  155. $result = isValidMailDomain($mail);
  156. $this->assertEquals(0, $result, 'Email isValidMailDomain('.$mail.') should return 0 (not valid) but returned '.$result);
  157. $mail = 'bidon@dolibarr.org';
  158. $result = isValidMailDomain($mail);
  159. $this->assertEquals(1, $result, 'Email isValidMailDomain('.$mail.') should return 1 (valid) but returned '.$result);
  160. }
  161. /**
  162. * testIsValidURL
  163. *
  164. * @return void
  165. */
  166. public function testIsValidUrl()
  167. {
  168. //Simple check
  169. $result = isValidUrl('http://google.com');
  170. $this->assertEquals(1, $result);
  171. $result = isValidUrl('goo=gle'); // This is good, it might be an alias of hostname
  172. $this->assertEquals(1, $result);
  173. //With scheme check
  174. $result = isValidUrl('http://www.google.com', 1);
  175. $this->assertEquals(1, $result);
  176. $result = isValidUrl('ftp://www.google.com', 1);
  177. $this->assertEquals(0, $result);
  178. //With password check invalid. This test should be ko but currently it is not
  179. //$result = isValidUrl('http://user:password@http://www.google.com', 1, 1);
  180. //$this->assertEquals(0, $result);
  181. //With password check valid
  182. $result = isValidUrl('http://user:password@www.google.com', 1, 1);
  183. $this->assertEquals(1, $result);
  184. $result = isValidUrl('http://www.google.com', 1, 1);
  185. $this->assertEquals(0, $result);
  186. //With port check
  187. $result = isValidUrl('http://google.com:8080', 0, 0, 1);
  188. $this->assertEquals(1, $result);
  189. $result = isValidUrl('http://google.com', 0, 0, 1);
  190. $this->assertEquals(0, $result);
  191. //With path check
  192. $result = isValidUrl('http://google.com/search', 0, 0, 0, 1);
  193. $this->assertEquals(1, $result);
  194. $result = isValidUrl('http://google.com', 0, 0, 0, 0);
  195. $this->assertEquals(1, $result);
  196. //With query check
  197. $result = isValidUrl('http://google.com/search?test=test', 0, 0, 0, 0, 1);
  198. $this->assertEquals(1, $result);
  199. //With query check
  200. $result = isValidUrl('http://google.com?test=test', 0, 0, 0, 0, 1);
  201. $this->assertEquals(1, $result);
  202. $result = isValidUrl('http://google.com', 0, 0, 0, 0, 1);
  203. $this->assertEquals(0, $result);
  204. //With anchor check
  205. $result = isValidUrl('http://google.com/search#done', 0, 0, 0, 0, 0, 1);
  206. $this->assertEquals(1, $result);
  207. $result = isValidUrl('http://google.com/search', 0, 0, 0, 0, 0, 1);
  208. $this->assertEquals(0, $result);
  209. }
  210. /**
  211. * testIsIP
  212. *
  213. * @return void
  214. */
  215. public function testIsIP()
  216. {
  217. // Not valid
  218. $ip='a299.299.299.299';
  219. $result=is_ip($ip);
  220. print __METHOD__." for ".$ip." result=".$result."\n";
  221. $this->assertEquals(0, $result, $ip);
  222. // Reserved IP range (not checked by is_ip function)
  223. $ip='169.254.0.0';
  224. $result=is_ip($ip);
  225. print __METHOD__." for ".$ip." result=".$result."\n";
  226. //$this->assertEquals(2,$result,$ip); // Assertion disabled because returned value differs between PHP patch version
  227. $ip='1.2.3.4';
  228. $result=is_ip($ip);
  229. print __METHOD__." for ".$ip." result=".$result."\n";
  230. $this->assertEquals(1, $result, $ip);
  231. // Private IP ranges
  232. $ip='10.0.0.0';
  233. $result=is_ip($ip);
  234. print __METHOD__." for ".$ip." result=".$result."\n";
  235. $this->assertEquals(2, $result, $ip);
  236. $ip='172.16.0.0';
  237. $result=is_ip($ip);
  238. print __METHOD__." for ".$ip." result=".$result."\n";
  239. $this->assertEquals(2, $result, $ip);
  240. $ip='192.168.0.0';
  241. $result=is_ip($ip);
  242. print __METHOD__." for ".$ip." result=".$result."\n";
  243. $this->assertEquals(2, $result, $ip);
  244. }
  245. }