CoreTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /* Copyright (C) 2010-2013 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/CoreTest.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. 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 to test core functions
  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 CoreTest 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. * @param string $name Name
  77. * @return CoreTest
  78. */
  79. public function __construct($name = '')
  80. {
  81. parent::__construct($name);
  82. //$this->sharedFixture
  83. global $conf,$user,$langs,$db;
  84. $this->savconf=$conf;
  85. $this->savuser=$user;
  86. $this->savlangs=$langs;
  87. $this->savdb=$db;
  88. //print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  89. //print " - db ".$db->db;
  90. print "\n";
  91. }
  92. /**
  93. * setUpBeforeClass
  94. *
  95. * @return void
  96. */
  97. public static function setUpBeforeClass(): void
  98. {
  99. global $conf,$user,$langs,$db;
  100. //$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  101. print __METHOD__."\n";
  102. }
  103. /**
  104. * tearDownAfterClass
  105. *
  106. * @return void
  107. */
  108. public static function tearDownAfterClass(): void
  109. {
  110. global $conf,$user,$langs,$db;
  111. //$db->rollback();
  112. print __METHOD__."\n";
  113. }
  114. /**
  115. * Init phpunit tests
  116. *
  117. * @return void
  118. */
  119. protected function setUp(): void
  120. {
  121. global $conf,$user,$langs,$db;
  122. $conf=$this->savconf;
  123. $user=$this->savuser;
  124. $langs=$this->savlangs;
  125. $db=$this->savdb;
  126. print __METHOD__."\n";
  127. }
  128. /**
  129. * End phpunit tests
  130. *
  131. * @return void
  132. */
  133. protected function tearDown(): void
  134. {
  135. print __METHOD__."\n";
  136. }
  137. /**
  138. * testDetectURLROOT
  139. *
  140. * @return void
  141. */
  142. public function testDetectURLROOT()
  143. {
  144. global $dolibarr_main_prod;
  145. global $dolibarr_main_url_root;
  146. global $dolibarr_main_data_root;
  147. global $dolibarr_main_document_root;
  148. global $dolibarr_main_data_root_alt;
  149. global $dolibarr_main_document_root_alt;
  150. global $dolibarr_main_db_host;
  151. global $dolibarr_main_db_port;
  152. global $dolibarr_main_db_type;
  153. global $dolibarr_main_db_prefix;
  154. $testtodo=0;
  155. // Case 1:
  156. // Test for subdir dolibarrnew (that point to htdocs) in root directory /var/www
  157. // URL: http://localhost/dolibarrnew/admin/system/phpinfo.php
  158. // To prepare this test:
  159. // - Create link from htdocs to /var/www/dolibarrnew
  160. // - Put into conf.php $dolibarr_main_document_root='/var/www/dolibarrnew';
  161. if ($testtodo == 1) {
  162. $_SERVER["HTTPS"]='';
  163. $_SERVER["SERVER_NAME"]='localhost';
  164. $_SERVER["SERVER_PORT"]='80';
  165. $_SERVER["DOCUMENT_ROOT"]='/var/www';
  166. $_SERVER["SCRIPT_NAME"]='/dolibarrnew/admin/system/phpinfo.php';
  167. $expectedresult='/dolibarrnew';
  168. }
  169. // Case 2:
  170. // Test for subdir aaa (that point to dolibarr) in root directory /var/www
  171. // URL: http://localhost/aaa/htdocs/admin/system/phpinfo.php
  172. // To prepare this test:
  173. // - Create link from dolibarr to /var/www/aaa
  174. // - Put into conf.php $dolibarr_main_document_root='/var/www/aaa/htdocs';
  175. if ($testtodo == 2) {
  176. $_SERVER["HTTPS"]='';
  177. $_SERVER["SERVER_NAME"]='localhost';
  178. $_SERVER["SERVER_PORT"]='80';
  179. $_SERVER["DOCUMENT_ROOT"]='/var/www';
  180. $_SERVER["SCRIPT_NAME"]='/aaa/htdocs/admin/system/phpinfo.php';
  181. $expectedresult='/aaa/htdocs';
  182. }
  183. // Case 3:
  184. // Test for virtual host localhostdolibarrnew that point to htdocs directory with
  185. // a direct document root
  186. // URL: http://localhostdolibarrnew/admin/system/phpinfo.php
  187. // To prepare this test:
  188. // - Create virtual host localhostdolibarrnew that point to /home/ldestailleur/git/dolibarr/htdocs
  189. // - Put into conf.php $dolibarr_main_document_root='/home/ldestailleur/git/dolibarr/htdocs';
  190. if ($testtodo == 3) {
  191. $_SERVER["HTTPS"]='';
  192. $_SERVER["SERVER_NAME"]='localhostdolibarrnew';
  193. $_SERVER["SERVER_PORT"]='80';
  194. $_SERVER["DOCUMENT_ROOT"]='/home/ldestailleur/git/dolibarr/htdocs';
  195. $_SERVER["SCRIPT_NAME"]='/admin/system/phpinfo.php';
  196. $expectedresult='';
  197. }
  198. // Case 4:
  199. // Test for virtual host localhostdolibarrnew that point to htdocs directory with
  200. // a symbolic link
  201. // URL: http://localhostdolibarrnew/admin/system/phpinfo.php
  202. if ($testtodo == 4) {
  203. $_SERVER["HTTPS"]='';
  204. $_SERVER["SERVER_NAME"]='localhostdolibarrnew';
  205. $_SERVER["SERVER_PORT"]='80';
  206. $_SERVER["DOCUMENT_ROOT"]='/var/www/dolibarr'; // This is a link that point to /home/ldestail/workspace/dolibarr/htdocs
  207. $_SERVER["SCRIPT_NAME"]='/admin/system/phpinfo.php';
  208. $expectedresult='';
  209. }
  210. // Case 5:
  211. // Test for alias /dolibarralias, Test when using nginx, Test when using lighttpd
  212. // URL: http://localhost/dolibarralias/admin/system/phpinfo.php
  213. // To prepare this test:
  214. // - Copy content of dolibarr project into /var/www/dolibarr
  215. // - Put into conf.php $dolibarr_main_document_root='/var/www/dolibarr/htdocs';
  216. // - Put into conf.php $dolibarr_main_url_root='http://localhost/dolibarralias'; (because autodetect will fails in this case)
  217. if ($testtodo == 5) {
  218. $_SERVER["HTTPS"]='';
  219. $_SERVER["SERVER_NAME"]='localhost';
  220. $_SERVER["SERVER_PORT"]='80';
  221. $_SERVER["DOCUMENT_ROOT"]='/var/www';
  222. $_SERVER["SCRIPT_NAME"]='/dolibarralias/admin/system/phpinfo.php';
  223. $expectedresult='/dolibarralias';
  224. }
  225. // Force to rerun filefunc.inc.php
  226. include dirname(__FILE__).'/../../htdocs/filefunc.inc.php';
  227. if ($testtodo != 0) {
  228. print __METHOD__." DOL_MAIN_URL_ROOT=".DOL_MAIN_URL_ROOT."\n";
  229. print __METHOD__." DOL_URL_ROOT=".DOL_URL_ROOT."\n";
  230. $this->assertEquals($expectedresult, DOL_URL_ROOT);
  231. }
  232. return true;
  233. }
  234. }