ScriptsTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /* Copyright (C) 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/ScriptsTest.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/security.lib.php';
  30. require_once dirname(__FILE__).'/../../htdocs/core/lib/security2.lib.php';
  31. if (! defined('NOREQUIREUSER')) {
  32. define('NOREQUIREUSER', '1');
  33. }
  34. if (! defined('NOREQUIREDB')) {
  35. define('NOREQUIREDB', '1');
  36. }
  37. if (! defined('NOREQUIRESOC')) {
  38. define('NOREQUIRESOC', '1');
  39. }
  40. if (! defined('NOREQUIRETRAN')) {
  41. define('NOREQUIRETRAN', '1');
  42. }
  43. if (! defined('NOCSRFCHECK')) {
  44. define('NOCSRFCHECK', '1');
  45. }
  46. if (! defined('NOTOKENRENEWAL')) {
  47. define('NOTOKENRENEWAL', '1');
  48. }
  49. if (! defined('NOREQUIREMENU')) {
  50. define('NOREQUIREMENU', '1'); // If there is no menu to show
  51. }
  52. if (! defined('NOREQUIREHTML')) {
  53. define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  54. }
  55. if (! defined('NOREQUIREAJAX')) {
  56. define('NOREQUIREAJAX', '1');
  57. }
  58. if (! defined("NOLOGIN")) {
  59. define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
  60. }
  61. if (empty($user->id)) {
  62. print "Load permissions for admin user nb 1\n";
  63. $user->fetch(1);
  64. $user->getrights();
  65. }
  66. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  67. /**
  68. * Class for PHPUnit tests
  69. *
  70. * @backupGlobals disabled
  71. * @backupStaticAttributes enabled
  72. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  73. */
  74. class ScriptsTest extends PHPUnit\Framework\TestCase
  75. {
  76. protected $savconf;
  77. protected $savuser;
  78. protected $savlangs;
  79. protected $savdb;
  80. /**
  81. * Constructor
  82. * We save global variables into local variables
  83. *
  84. * @param string $name Name
  85. * @return ScriptsTest
  86. */
  87. public function __construct($name = '')
  88. {
  89. parent::__construct($name);
  90. //$this->sharedFixture
  91. global $conf,$user,$langs,$db;
  92. $this->savconf=$conf;
  93. $this->savuser=$user;
  94. $this->savlangs=$langs;
  95. $this->savdb=$db;
  96. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  97. //print " - db ".$db->db;
  98. print "\n";
  99. }
  100. /**
  101. * setUpBeforeClass
  102. *
  103. * @return void
  104. */
  105. public static function setUpBeforeClass(): void
  106. {
  107. global $conf,$user,$langs,$db;
  108. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  109. print __METHOD__."\n";
  110. }
  111. /**
  112. * tearDownAfterClass
  113. *
  114. * @return void
  115. */
  116. public static function tearDownAfterClass(): void
  117. {
  118. global $conf,$user,$langs,$db;
  119. $db->rollback();
  120. print __METHOD__."\n";
  121. }
  122. /**
  123. * Init phpunit tests
  124. *
  125. * @return void
  126. */
  127. protected function setUp(): void
  128. {
  129. global $conf,$user,$langs,$db;
  130. $conf=$this->savconf;
  131. $user=$this->savuser;
  132. $langs=$this->savlangs;
  133. $db=$this->savdb;
  134. print __METHOD__."\n";
  135. }
  136. /**
  137. * End phpunit tests
  138. *
  139. * @return void
  140. */
  141. protected function tearDown(): void
  142. {
  143. print __METHOD__."\n";
  144. }
  145. /**
  146. * testBank
  147. *
  148. * @return string
  149. */
  150. public function testBank()
  151. {
  152. global $conf,$user,$langs,$db;
  153. $conf=$this->savconf;
  154. $user=$this->savuser;
  155. $langs=$this->savlangs;
  156. $db=$this->savdb;
  157. $script=dirname(__FILE__).'/../../scripts/bank/export-bank-receipts.php BANKDUMMY RECEIPTDUMMY excel2007 lang=fr_FR';
  158. $returnvar = 0;
  159. $output = array();
  160. $result=exec($script, $output, $returnvar);
  161. print __METHOD__." result=".$result."\n";
  162. print __METHOD__." output=".join("\n", $output)."\n";
  163. print __METHOD__." returnvar=".$returnvar."\n";
  164. $this->assertEquals($result, 'Failed to find bank account with ref BANKDUMMY.');
  165. $this->assertEquals($returnvar, 255);
  166. return $result;
  167. }
  168. /**
  169. * testCompany
  170. *
  171. * @depends testBank
  172. * @return string
  173. */
  174. public function testCompany()
  175. {
  176. global $conf,$user,$langs,$db;
  177. $conf=$this->savconf;
  178. $user=$this->savuser;
  179. $langs=$this->savlangs;
  180. $db=$this->savdb;
  181. /*
  182. $script=dirname(__FILE__).'/../../scripts/company/sync_contacts_dolibarr_2ldap now';
  183. $result=exec($script, $output, $returnvar);
  184. print __METHOD__." result=".$result."\n";
  185. print __METHOD__." output=".join("\n",$output)."\n";
  186. print __METHOD__." returnvar=".$returnvar."\n";
  187. $this->assertEquals($result,'Failed to find bank account with ref BANKDUMMY.');
  188. $this->assertEquals($returnvar,255);
  189. */
  190. $this->assertEquals(0, 0);
  191. return '';
  192. }
  193. /**
  194. * testContracts
  195. *
  196. * @depends testCompany
  197. * @return string
  198. */
  199. public function testContracts()
  200. {
  201. global $conf,$user,$langs,$db;
  202. $conf=$this->savconf;
  203. $user=$this->savuser;
  204. $langs=$this->savlangs;
  205. $db=$this->savdb;
  206. $returnvar = 0;
  207. $output = array();
  208. $script=dirname(__FILE__).'/../../scripts/contracts/email_expire_services_to_customers.php test thirdparties';
  209. $result=exec($script, $output, $returnvar);
  210. print __METHOD__." result=".$result."\n";
  211. print __METHOD__." output=".join("\n", $output)."\n";
  212. print __METHOD__." returnvar=".$returnvar."\n";
  213. $this->assertEquals($returnvar, 0, 'email_expire_services_to_customers.php thirdparties');
  214. $script=dirname(__FILE__).'/../../scripts/contracts/email_expire_services_to_customers.php test contacts -30';
  215. $result=exec($script, $output, $returnvar);
  216. print __METHOD__." result=".$result."\n";
  217. print __METHOD__." output=".join("\n", $output)."\n";
  218. print __METHOD__." returnvar=".$returnvar."\n";
  219. $this->assertEquals($returnvar, 0, 'email_expire_services_to_customers.php contacts');
  220. $script=dirname(__FILE__).'/../../scripts/contracts/email_expire_services_to_representatives.php test -30';
  221. $result=exec($script, $output, $returnvar);
  222. print __METHOD__." result=".$result."\n";
  223. print __METHOD__." output=".join("\n", $output)."\n";
  224. print __METHOD__." returnvar=".$returnvar."\n";
  225. $this->assertEquals($returnvar, 0, 'email_expire_services_to_representatives.php');
  226. return $result;
  227. }
  228. /**
  229. * testInvoices
  230. *
  231. * @depends testContracts
  232. * @return string
  233. */
  234. public function testInvoices()
  235. {
  236. global $conf,$user,$langs,$db;
  237. $conf=$this->savconf;
  238. $user=$this->savuser;
  239. $langs=$this->savlangs;
  240. $db=$this->savdb;
  241. $returnvar = 0;
  242. $output = array();
  243. $script=dirname(__FILE__).'/../../scripts/invoices/email_unpaid_invoices_to_customers.php test thirdparties';
  244. $result=exec($script, $output, $returnvar);
  245. print __METHOD__." result=".$result."\n";
  246. print __METHOD__." output=".join("\n", $output)."\n";
  247. print __METHOD__." returnvar=".$returnvar."\n";
  248. $this->assertEquals($returnvar, 0, 'email_unpaid_invoices_to_customers.php thirdparties');
  249. $script=dirname(__FILE__).'/../../scripts/invoices/email_unpaid_invoices_to_customers.php test contacts -30';
  250. $result=exec($script, $output, $returnvar);
  251. print __METHOD__." result=".$result."\n";
  252. print __METHOD__." output=".join("\n", $output)."\n";
  253. print __METHOD__." returnvar=".$returnvar."\n";
  254. $this->assertEquals($returnvar, 0, 'email_unpaid_invoices_to_customers.php contacts');
  255. $script=dirname(__FILE__).'/../../scripts/invoices/email_unpaid_invoices_to_representatives.php test thirdparties';
  256. $result=exec($script, $output, $returnvar);
  257. print __METHOD__." result=".$result."\n";
  258. print __METHOD__." output=".join("\n", $output)."\n";
  259. print __METHOD__." returnvar=".$returnvar."\n";
  260. $this->assertEquals($returnvar, 0, 'email_unpaid_invoices_to_customers.php thirdparties');
  261. return $result;
  262. }
  263. }