SocieteTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <?php
  2. /* Copyright (C) 2010 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/SocieteTest.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/societe/class/societe.class.php';
  30. $langs->load("dict");
  31. if (empty($user->id)) {
  32. print "Load permissions for admin user nb 1\n";
  33. $user->fetch(1);
  34. $user->getrights();
  35. }
  36. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  37. /**
  38. * Class for PHPUnit tests
  39. *
  40. * @backupGlobals disabled
  41. * @backupStaticAttributes enabled
  42. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  43. */
  44. class SocieteTest extends PHPUnit\Framework\TestCase
  45. {
  46. protected $savconf;
  47. protected $savuser;
  48. protected $savlangs;
  49. protected $savdb;
  50. /**
  51. * Constructor
  52. * We save global variables into local variables
  53. *
  54. * @param string $name Name
  55. * @return SocieteTest
  56. */
  57. public function __construct($name = '')
  58. {
  59. parent::__construct($name);
  60. //$this->sharedFixture
  61. global $conf,$user,$langs,$db;
  62. $this->savconf=$conf;
  63. $this->savuser=$user;
  64. $this->savlangs=$langs;
  65. $this->savdb=$db;
  66. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  67. //print " - db ".$db->db;
  68. print "\n";
  69. }
  70. /**
  71. * setUpBeforeClass
  72. *
  73. * @return void
  74. */
  75. public static function setUpBeforeClass(): void
  76. {
  77. global $conf,$user,$langs,$db;
  78. if ($conf->global->SOCIETE_CODECLIENT_ADDON != 'mod_codeclient_monkey') {
  79. print "\n".__METHOD__." third party ref checker must be setup to 'mod_codeclient_monkey' not to '".$conf->global->SOCIETE_CODECLIENT_ADDON."'.\n"; die(1);
  80. }
  81. if (!empty($conf->global->MAIN_DISABLEPROFIDRULES)) {
  82. print "\n".__METHOD__." constant MAIN_DISABLEPROFIDRULES must be empty (if a module set it, disable module).\n"; die(1);
  83. }
  84. if ($langs->defaultlang != 'en_US') {
  85. print "\n".__METHOD__." default language of company must be set to autodetect.\n"; die(1);
  86. }
  87. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  88. print __METHOD__."\n";
  89. }
  90. /**
  91. * tearDownAfterClass
  92. *
  93. * @return void
  94. */
  95. public static function tearDownAfterClass(): void
  96. {
  97. global $conf,$user,$langs,$db;
  98. $db->rollback();
  99. print __METHOD__."\n";
  100. }
  101. /**
  102. * Init phpunit tests
  103. *
  104. * @return void
  105. */
  106. protected function setUp(): void
  107. {
  108. global $conf,$user,$langs,$db;
  109. $conf=$this->savconf;
  110. $user=$this->savuser;
  111. $langs=$this->savlangs;
  112. $db=$this->savdb;
  113. print __METHOD__."\n";
  114. }
  115. /**
  116. * End phpunit tests
  117. *
  118. * @return void
  119. */
  120. protected function tearDown(): void
  121. {
  122. print __METHOD__."\n";
  123. }
  124. /**
  125. * testSocieteCreate
  126. *
  127. * @return int
  128. */
  129. public function testSocieteCreate()
  130. {
  131. global $conf,$user,$langs,$db;
  132. $conf=$this->savconf;
  133. $user=$this->savuser;
  134. $langs=$this->savlangs;
  135. $db=$this->savdb;
  136. $localobject=new Societe($db);
  137. $localobject->initAsSpecimen();
  138. $result=$localobject->create($user);
  139. print __METHOD__." result=".$result."\n";
  140. $this->assertLessThanOrEqual($result, 0);
  141. return $result;
  142. }
  143. /**
  144. * testSocieteFetch
  145. *
  146. * @param int $id Company id
  147. * @return Societe $localobject Company
  148. *
  149. * @depends testSocieteCreate
  150. * The depends says test is run only if previous is ok
  151. */
  152. public function testSocieteFetch($id)
  153. {
  154. global $conf,$user,$langs,$db;
  155. $conf=$this->savconf;
  156. $user=$this->savuser;
  157. $langs=$this->savlangs;
  158. $db=$this->savdb;
  159. $localobject=new Societe($db);
  160. $result=$localobject->fetch($id);
  161. print __METHOD__." id=".$id." result=".$result."\n";
  162. $this->assertLessThan($result, 0);
  163. $result=$localobject->verify();
  164. print __METHOD__." id=".$id." result=".$result."\n";
  165. $this->assertEquals($result, 0);
  166. return $localobject;
  167. }
  168. /**
  169. * testSocieteUpdate
  170. *
  171. * @param Societe $localobject Company
  172. * @return Societe $localobject Company
  173. *
  174. * @depends testSocieteFetch
  175. * The depends says test is run only if previous is ok
  176. */
  177. public function testSocieteUpdate($localobject)
  178. {
  179. global $conf,$user,$langs,$db;
  180. $conf=$this->savconf;
  181. $user=$this->savuser;
  182. $langs=$this->savlangs;
  183. $db=$this->savdb;
  184. $localobject->note_private='New private note after update';
  185. $localobject->note_public='New public note after update';
  186. $localobject->name='New name';
  187. $localobject->address='New address';
  188. $localobject->zip='New zip';
  189. $localobject->town='New town';
  190. $localobject->country_id=2;
  191. $localobject->status=0;
  192. $localobject->phone='New tel';
  193. $localobject->fax='New fax';
  194. $localobject->email='newemail@newemail.com';
  195. $localobject->url='New url';
  196. $localobject->idprof1='new idprof1';
  197. $localobject->idprof2='new idprof2';
  198. $localobject->idprof3='new idprof3';
  199. $localobject->idprof4='new idprof4';
  200. $result=$localobject->update($localobject->id, $user);
  201. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  202. $this->assertLessThan($result, 0);
  203. $result=$localobject->update_note($localobject->note_private, '_private');
  204. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  205. $this->assertLessThan($result, 0, 'Holiday::update_note (private) error');
  206. $result=$localobject->update_note($localobject->note_public, '_public');
  207. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  208. $this->assertLessThan($result, 0, 'Holiday::update_note (public) error');
  209. $newobject=new Societe($db);
  210. $result=$newobject->fetch($localobject->id);
  211. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  212. $this->assertLessThan($result, 0);
  213. $this->assertEquals($localobject->note_private, $newobject->note_private);
  214. //$this->assertEquals($localobject->note_public, $newobject->note_public);
  215. $this->assertEquals($localobject->name, $newobject->name);
  216. $this->assertEquals($localobject->address, $newobject->address);
  217. $this->assertEquals($localobject->zip, $newobject->zip);
  218. $this->assertEquals($localobject->town, $newobject->town);
  219. $this->assertEquals($localobject->country_id, $newobject->country_id);
  220. $this->assertEquals('BE', $newobject->country_code);
  221. $this->assertEquals($localobject->status, $newobject->status);
  222. $this->assertEquals($localobject->phone, $newobject->phone);
  223. $this->assertEquals($localobject->fax, $newobject->fax);
  224. $this->assertEquals($localobject->email, $newobject->email);
  225. $this->assertEquals($localobject->url, $newobject->url);
  226. $this->assertEquals($localobject->idprof1, $newobject->idprof1);
  227. $this->assertEquals($localobject->idprof2, $newobject->idprof2);
  228. $this->assertEquals($localobject->idprof3, $newobject->idprof3);
  229. $this->assertEquals($localobject->idprof4, $newobject->idprof4);
  230. return $localobject;
  231. }
  232. /**
  233. * testIdProfCheck
  234. *
  235. * @param Societe $localobject Company
  236. * @return Societe $localobject Company
  237. *
  238. * @depends testSocieteUpdate
  239. * The depends says test is run only if previous is ok
  240. */
  241. public function testIdProfCheck($localobject)
  242. {
  243. // OK FR
  244. $localobject->country_code='FR';
  245. $localobject->idprof1=493861496;
  246. $localobject->idprof2=49386149600021;
  247. $result=$localobject->id_prof_check(1, $localobject); // Must be > 0
  248. print __METHOD__." OK FR idprof1 result=".$result."\n";
  249. $this->assertGreaterThanOrEqual(1, $result);
  250. $result=$localobject->id_prof_check(2, $localobject); // Must be > 0
  251. print __METHOD__." OK FR idprof2 result=".$result."\n";
  252. $this->assertGreaterThanOrEqual(1, $result);
  253. // KO FR
  254. $localobject->country_code='FR';
  255. $localobject->idprof1='id1ko';
  256. $localobject->idprof2='id2ko';
  257. $result=$localobject->id_prof_check(1, $localobject); // Must be <= 0
  258. print __METHOD__." KO FR idprof1 result=".$result."\n";
  259. $this->assertLessThan(1, $result);
  260. $result=$localobject->id_prof_check(2, $localobject); // Must be <= 0
  261. print __METHOD__." KO FR idprof2 result=".$result."\n";
  262. $this->assertLessThan(1, $result);
  263. // KO ES
  264. $localobject->country_code='ES';
  265. $localobject->idprof1='id1ko';
  266. $result=$localobject->id_prof_check(1, $localobject); // Must be <= 0
  267. print __METHOD__." KO ES idprof1 result=".$result."\n";
  268. $this->assertLessThan(1, $result);
  269. // OK AR
  270. $localobject->country_code='AR';
  271. $localobject->idprof1='id1ko';
  272. $localobject->idprof2='id2ko';
  273. $result=$localobject->id_prof_check(1, $localobject); // Must be > 0
  274. print __METHOD__." OK AR idprof1 result=".$result."\n";
  275. $this->assertGreaterThanOrEqual(0, $result);
  276. $result=$localobject->id_prof_check(2, $localobject); // Must be > 0
  277. print __METHOD__." OK AR idprof2 result=".$result."\n";
  278. $this->assertGreaterThanOrEqual(1, $result);
  279. return $localobject;
  280. }
  281. /**
  282. * testSocieteOther
  283. *
  284. * @param Societe $localobject Company
  285. * @return int $id Id of company
  286. *
  287. * @depends testIdProfCheck
  288. * The depends says test is run only if previous is ok
  289. */
  290. public function testSocieteOther($localobject)
  291. {
  292. global $conf,$user,$langs,$db;
  293. $conf=$this->savconf;
  294. $user=$this->savuser;
  295. $langs=$this->savlangs;
  296. $db=$this->savdb;
  297. $result=$localobject->set_as_client();
  298. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  299. $this->assertLessThan($result, 0);
  300. $result=$localobject->setPriceLevel(1, $user);
  301. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  302. $this->assertLessThan($result, 0);
  303. $result=$localobject->set_remise_client(10, 'Gift', $user);
  304. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  305. $this->assertLessThan($result, 0);
  306. $result=$localobject->getNomUrl(1);
  307. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  308. $this->assertNotEquals($result, '');
  309. $localobject->country_code = 'FR';
  310. $result=$localobject->isInEEC();
  311. print __METHOD__." id=".$localobject->id." country_code=".$localobject->country_code." result=".$result."\n";
  312. $this->assertTrue($result);
  313. $localobject->country_code = 'US';
  314. $result=$localobject->isInEEC();
  315. print __METHOD__." id=".$localobject->id." country_code=".$localobject->country_code." result=".$result."\n";
  316. $this->assertFalse($result);
  317. /*$localobject->country_code = 'GB';
  318. $result=$localobject->isInEEC();
  319. print __METHOD__." id=".$localobject->id." country_code=".$localobject->country_code." result=".$result."\n";
  320. $this->assertTrue($result);
  321. */
  322. $localobject->info($localobject->id);
  323. print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
  324. $this->assertNotEquals($localobject->date_creation, '');
  325. return $localobject->id;
  326. }
  327. /**
  328. * testGetOutstandingBills
  329. *
  330. * @param int $id Id of company
  331. * @return int
  332. *
  333. * @depends testSocieteOther
  334. * The depends says test is run only if previous is ok
  335. */
  336. public function testGetOutstandingBills($id)
  337. {
  338. global $conf,$user,$langs,$db;
  339. $conf=$this->savconf;
  340. $user=$this->savuser;
  341. $langs=$this->savlangs;
  342. $db=$this->savdb;
  343. $localobject=new Societe($db);
  344. $localobject->fetch($id);
  345. $result=$localobject->getOutstandingBills();
  346. print __METHOD__." id=".$id." result=".var_export($result, true)."\n";
  347. $this->assertTrue(array_key_exists('opened', $result), 'Result of getOutstandingBills failed');
  348. return $id;
  349. }
  350. /**
  351. * testSocieteDelete
  352. *
  353. * @param int $id Id of company
  354. * @return int
  355. *
  356. * @depends testGetOutstandingBills
  357. * The depends says test is run only if previous is ok
  358. */
  359. public function testSocieteDelete($id)
  360. {
  361. global $conf,$user,$langs,$db;
  362. $conf=$this->savconf;
  363. $user=$this->savuser;
  364. $langs=$this->savlangs;
  365. $db=$this->savdb;
  366. $localobject=new Societe($db);
  367. $result=$localobject->fetch($id);
  368. $result=$localobject->delete($id, $user);
  369. print __METHOD__." id=".$id." result=".$result."\n";
  370. $this->assertLessThan($result, 0);
  371. return $result;
  372. }
  373. /**
  374. * testSocieteGetFullAddress
  375. *
  376. * @return int $id Id of company
  377. */
  378. public function testSocieteGetFullAddress()
  379. {
  380. global $conf,$user,$langs,$db;
  381. $conf=$this->savconf;
  382. $user=$this->savuser;
  383. $langs=$this->savlangs;
  384. $db=$this->savdb;
  385. $localobjectadd=new Societe($db);
  386. $localobjectadd->initAsSpecimen();
  387. // France
  388. unset($localobjectadd->country_code);
  389. $localobjectadd->country_id=1;
  390. $localobjectadd->name='New name';
  391. $localobjectadd->address='New address';
  392. $localobjectadd->zip='New zip';
  393. $localobjectadd->town='New town';
  394. $result=$localobjectadd->getFullAddress(1);
  395. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  396. $this->assertContains("New address\nNew zip New town\nFrance", $result);
  397. // Belgium
  398. unset($localobjectadd->country_code);
  399. $localobjectadd->country_id=2;
  400. $localobjectadd->name='New name';
  401. $localobjectadd->address='New address';
  402. $localobjectadd->zip='New zip';
  403. $localobjectadd->town='New town';
  404. $result=$localobjectadd->getFullAddress(1);
  405. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  406. $this->assertContains("New address\nNew zip New town\nBelgium", $result);
  407. // Switzerland
  408. unset($localobjectadd->country_code);
  409. $localobjectadd->country_id=6;
  410. $localobjectadd->name='New name';
  411. $localobjectadd->address='New address';
  412. $localobjectadd->zip='New zip';
  413. $localobjectadd->town='New town';
  414. $result=$localobjectadd->getFullAddress(1);
  415. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  416. $this->assertContains("New address\nNew zip New town\nSwitzerland", $result);
  417. // USA
  418. unset($localobjectadd->country_code);
  419. $localobjectadd->country_id=11;
  420. $localobjectadd->name='New name';
  421. $localobjectadd->address='New address';
  422. $localobjectadd->zip='New zip';
  423. $localobjectadd->town='New town';
  424. $localobjectadd->state='New state';
  425. $result=$localobjectadd->getFullAddress(1);
  426. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  427. $this->assertContains("New address\nNew town, New state, New zip\nUnited States", $result);
  428. return $localobjectadd->id;
  429. }
  430. }