ScriptsTest.php 8.0 KB

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