CoreTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /* Copyright (C) 2010-2013 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/CoreTest.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. if (! defined('NOREQUIREUSER')) {
  29. define('NOREQUIREUSER', '1');
  30. }
  31. if (! defined('NOREQUIREDB')) {
  32. define('NOREQUIREDB', '1');
  33. }
  34. if (! defined('NOREQUIRESOC')) {
  35. define('NOREQUIRESOC', '1');
  36. }
  37. if (! defined('NOREQUIRETRAN')) {
  38. define('NOREQUIRETRAN', '1');
  39. }
  40. if (! defined('NOCSRFCHECK')) {
  41. define('NOCSRFCHECK', '1');
  42. }
  43. if (! defined('NOTOKENRENEWAL')) {
  44. define('NOTOKENRENEWAL', '1');
  45. }
  46. if (! defined('NOREQUIREMENU')) {
  47. define('NOREQUIREMENU', '1'); // If there is no menu to show
  48. }
  49. if (! defined('NOREQUIREHTML')) {
  50. define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  51. }
  52. if (! defined('NOREQUIREAJAX')) {
  53. define('NOREQUIREAJAX', '1');
  54. }
  55. if (! defined("NOLOGIN")) {
  56. define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
  57. }
  58. /**
  59. * Class to test core functions
  60. *
  61. * @backupGlobals disabled
  62. * @backupStaticAttributes enabled
  63. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  64. */
  65. class CoreTest extends PHPUnit\Framework\TestCase
  66. {
  67. protected $savconf;
  68. protected $savuser;
  69. protected $savlangs;
  70. protected $savdb;
  71. /**
  72. * Constructor
  73. * We save global variables into local variables
  74. *
  75. * @return CoreTest
  76. */
  77. public function __construct()
  78. {
  79. parent::__construct();
  80. //$this->sharedFixture
  81. global $conf,$user,$langs,$db;
  82. $this->savconf=$conf;
  83. $this->savuser=$user;
  84. $this->savlangs=$langs;
  85. $this->savdb=$db;
  86. //print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  87. //print " - db ".$db->db;
  88. print "\n";
  89. }
  90. /**
  91. * setUpBeforeClass
  92. *
  93. * @return void
  94. */
  95. public static function setUpBeforeClass()
  96. {
  97. global $conf,$user,$langs,$db;
  98. //$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  99. print __METHOD__."\n";
  100. }
  101. /**
  102. * tearDownAfterClass
  103. *
  104. * @return void
  105. */
  106. public static function tearDownAfterClass()
  107. {
  108. global $conf,$user,$langs,$db;
  109. //$db->rollback();
  110. print __METHOD__."\n";
  111. }
  112. /**
  113. * Init phpunit tests
  114. *
  115. * @return void
  116. */
  117. protected function setUp()
  118. {
  119. global $conf,$user,$langs,$db;
  120. $conf=$this->savconf;
  121. $user=$this->savuser;
  122. $langs=$this->savlangs;
  123. $db=$this->savdb;
  124. print __METHOD__."\n";
  125. }
  126. /**
  127. * End phpunit tests
  128. *
  129. * @return void
  130. */
  131. protected function tearDown()
  132. {
  133. print __METHOD__."\n";
  134. }
  135. /**
  136. * testDetectURLROOT
  137. *
  138. * @return void
  139. */
  140. public function testDetectURLROOT()
  141. {
  142. global $dolibarr_main_prod;
  143. global $dolibarr_main_url_root;
  144. global $dolibarr_main_data_root;
  145. global $dolibarr_main_document_root;
  146. global $dolibarr_main_data_root_alt;
  147. global $dolibarr_main_document_root_alt;
  148. global $dolibarr_main_db_host;
  149. global $dolibarr_main_db_port;
  150. global $dolibarr_main_db_type;
  151. global $dolibarr_main_db_prefix;
  152. $testtodo=0;
  153. // Case 1:
  154. // Test for subdir dolibarrnew (that point to htdocs) in root directory /var/www
  155. // URL: http://localhost/dolibarrnew/admin/system/phpinfo.php
  156. // To prepare this test:
  157. // - Create link from htdocs to /var/www/dolibarrnew
  158. // - Put into conf.php $dolibarr_main_document_root='/var/www/dolibarrnew';
  159. if ($testtodo == 1) {
  160. $_SERVER["HTTPS"]='';
  161. $_SERVER["SERVER_NAME"]='localhost';
  162. $_SERVER["SERVER_PORT"]='80';
  163. $_SERVER["DOCUMENT_ROOT"]='/var/www';
  164. $_SERVER["SCRIPT_NAME"]='/dolibarrnew/admin/system/phpinfo.php';
  165. $expectedresult='/dolibarrnew';
  166. }
  167. // Case 2:
  168. // Test for subdir aaa (that point to dolibarr) in root directory /var/www
  169. // URL: http://localhost/aaa/htdocs/admin/system/phpinfo.php
  170. // To prepare this test:
  171. // - Create link from dolibarr to /var/www/aaa
  172. // - Put into conf.php $dolibarr_main_document_root='/var/www/aaa/htdocs';
  173. if ($testtodo == 2) {
  174. $_SERVER["HTTPS"]='';
  175. $_SERVER["SERVER_NAME"]='localhost';
  176. $_SERVER["SERVER_PORT"]='80';
  177. $_SERVER["DOCUMENT_ROOT"]='/var/www';
  178. $_SERVER["SCRIPT_NAME"]='/aaa/htdocs/admin/system/phpinfo.php';
  179. $expectedresult='/aaa/htdocs';
  180. }
  181. // Case 3:
  182. // Test for virtual host localhostdolibarrnew that point to htdocs directory with
  183. // a direct document root
  184. // URL: http://localhostdolibarrnew/admin/system/phpinfo.php
  185. // To prepare this test:
  186. // - Create virtual host localhostdolibarrnew that point to /home/ldestailleur/git/dolibarr/htdocs
  187. // - Put into conf.php $dolibarr_main_document_root='/home/ldestailleur/git/dolibarr/htdocs';
  188. if ($testtodo == 3) {
  189. $_SERVER["HTTPS"]='';
  190. $_SERVER["SERVER_NAME"]='localhostdolibarrnew';
  191. $_SERVER["SERVER_PORT"]='80';
  192. $_SERVER["DOCUMENT_ROOT"]='/home/ldestailleur/git/dolibarr/htdocs';
  193. $_SERVER["SCRIPT_NAME"]='/admin/system/phpinfo.php';
  194. $expectedresult='';
  195. }
  196. // Case 4:
  197. // Test for virtual host localhostdolibarrnew that point to htdocs directory with
  198. // a symbolic link
  199. // URL: http://localhostdolibarrnew/admin/system/phpinfo.php
  200. if ($testtodo == 4) {
  201. $_SERVER["HTTPS"]='';
  202. $_SERVER["SERVER_NAME"]='localhostdolibarrnew';
  203. $_SERVER["SERVER_PORT"]='80';
  204. $_SERVER["DOCUMENT_ROOT"]='/var/www/dolibarr'; // This is a link that point to /home/ldestail/workspace/dolibarr/htdocs
  205. $_SERVER["SCRIPT_NAME"]='/admin/system/phpinfo.php';
  206. $expectedresult='';
  207. }
  208. // Case 5:
  209. // Test for alias /dolibarralias, Test when using nginx, Test when using lighttpd
  210. // URL: http://localhost/dolibarralias/admin/system/phpinfo.php
  211. // To prepare this test:
  212. // - Copy content of dolibarr project into /var/www/dolibarr
  213. // - Put into conf.php $dolibarr_main_document_root='/var/www/dolibarr/htdocs';
  214. // - Put into conf.php $dolibarr_main_url_root='http://localhost/dolibarralias'; (because autodetect will fails in this case)
  215. if ($testtodo == 5) {
  216. $_SERVER["HTTPS"]='';
  217. $_SERVER["SERVER_NAME"]='localhost';
  218. $_SERVER["SERVER_PORT"]='80';
  219. $_SERVER["DOCUMENT_ROOT"]='/var/www';
  220. $_SERVER["SCRIPT_NAME"]='/dolibarralias/admin/system/phpinfo.php';
  221. $expectedresult='/dolibarralias';
  222. }
  223. // Force to rerun filefunc.inc.php
  224. include dirname(__FILE__).'/../../htdocs/filefunc.inc.php';
  225. if ($testtodo != 0) {
  226. print __METHOD__." DOL_MAIN_URL_ROOT=".DOL_MAIN_URL_ROOT."\n";
  227. print __METHOD__." DOL_URL_ROOT=".DOL_URL_ROOT."\n";
  228. $this->assertEquals($expectedresult, DOL_URL_ROOT);
  229. }
  230. return true;
  231. }
  232. }