SocieteTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 '" . getDolGlobalString('SOCIETE_CODECLIENT_ADDON')."'.\n";
  80. die(1);
  81. }
  82. if (getDolGlobalString('MAIN_DISABLEPROFIDRULES')) {
  83. print "\n".__METHOD__." constant MAIN_DISABLEPROFIDRULES must be empty (if a module set it, disable module).\n";
  84. die(1);
  85. }
  86. if ($langs->defaultlang != 'en_US') {
  87. print "\n".__METHOD__." default language of company must be set to autodetect.\n";
  88. die(1);
  89. }
  90. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  91. print __METHOD__."\n";
  92. }
  93. /**
  94. * tearDownAfterClass
  95. *
  96. * @return void
  97. */
  98. public static function tearDownAfterClass(): void
  99. {
  100. global $conf,$user,$langs,$db;
  101. $db->rollback();
  102. print __METHOD__."\n";
  103. }
  104. /**
  105. * Init phpunit tests
  106. *
  107. * @return void
  108. */
  109. protected function setUp(): void
  110. {
  111. global $conf,$user,$langs,$db;
  112. $conf=$this->savconf;
  113. $user=$this->savuser;
  114. $langs=$this->savlangs;
  115. $db=$this->savdb;
  116. print __METHOD__."\n";
  117. }
  118. /**
  119. * End phpunit tests
  120. *
  121. * @return void
  122. */
  123. protected function tearDown(): void
  124. {
  125. print __METHOD__."\n";
  126. }
  127. /**
  128. * testSocieteCreate
  129. *
  130. * @return int
  131. */
  132. public function testSocieteCreate()
  133. {
  134. global $conf,$user,$langs,$db;
  135. $conf=$this->savconf;
  136. $user=$this->savuser;
  137. $langs=$this->savlangs;
  138. $db=$this->savdb;
  139. $localobject=new Societe($db);
  140. $localobject->initAsSpecimen();
  141. $result=$localobject->create($user);
  142. print __METHOD__." result=".$result."\n";
  143. $this->assertLessThanOrEqual($result, 0);
  144. return $result;
  145. }
  146. /**
  147. * testSocieteFetch
  148. *
  149. * @param int $id Company id
  150. * @return Societe $localobject Company
  151. *
  152. * @depends testSocieteCreate
  153. * The depends says test is run only if previous is ok
  154. */
  155. public function testSocieteFetch($id)
  156. {
  157. global $conf,$user,$langs,$db;
  158. $conf=$this->savconf;
  159. $user=$this->savuser;
  160. $langs=$this->savlangs;
  161. $db=$this->savdb;
  162. $localobject=new Societe($db);
  163. $result=$localobject->fetch($id);
  164. print __METHOD__." id=".$id." result=".$result."\n";
  165. $this->assertLessThan($result, 0);
  166. $result=$localobject->verify();
  167. print __METHOD__." id=".$id." result=".$result."\n";
  168. $this->assertEquals($result, 0);
  169. return $localobject;
  170. }
  171. /**
  172. * testSocieteUpdate
  173. *
  174. * @param Societe $localobject Company
  175. * @return Societe $localobject Company
  176. *
  177. * @depends testSocieteFetch
  178. * The depends says test is run only if previous is ok
  179. */
  180. public function testSocieteUpdate($localobject)
  181. {
  182. global $conf,$user,$langs,$db;
  183. $conf=$this->savconf;
  184. $user=$this->savuser;
  185. $langs=$this->savlangs;
  186. $db=$this->savdb;
  187. $localobject->note_private='New private note after update';
  188. $localobject->note_public='New public note after update';
  189. $localobject->name='New name';
  190. $localobject->address='New address';
  191. $localobject->zip='New zip';
  192. $localobject->town='New town';
  193. $localobject->country_id=2;
  194. $localobject->status=0;
  195. $localobject->phone='New tel';
  196. $localobject->fax='New fax';
  197. $localobject->email='newemail@newemail.com';
  198. $localobject->url='New url';
  199. $localobject->idprof1='new idprof1';
  200. $localobject->idprof2='new idprof2';
  201. $localobject->idprof3='new idprof3';
  202. $localobject->idprof4='new idprof4';
  203. $result=$localobject->update($localobject->id, $user);
  204. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  205. $this->assertLessThan($result, 0);
  206. $result=$localobject->update_note($localobject->note_private, '_private');
  207. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  208. $this->assertLessThan($result, 0, 'Holiday::update_note (private) error');
  209. $result=$localobject->update_note($localobject->note_public, '_public');
  210. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  211. $this->assertLessThan($result, 0, 'Holiday::update_note (public) error');
  212. $newobject=new Societe($db);
  213. $result=$newobject->fetch($localobject->id);
  214. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  215. $this->assertLessThan($result, 0);
  216. $this->assertEquals($localobject->note_private, $newobject->note_private);
  217. //$this->assertEquals($localobject->note_public, $newobject->note_public);
  218. $this->assertEquals($localobject->name, $newobject->name);
  219. $this->assertEquals($localobject->address, $newobject->address);
  220. $this->assertEquals($localobject->zip, $newobject->zip);
  221. $this->assertEquals($localobject->town, $newobject->town);
  222. $this->assertEquals($localobject->country_id, $newobject->country_id);
  223. $this->assertEquals('BE', $newobject->country_code);
  224. $this->assertEquals($localobject->status, $newobject->status);
  225. $this->assertEquals($localobject->phone, $newobject->phone);
  226. $this->assertEquals($localobject->fax, $newobject->fax);
  227. $this->assertEquals($localobject->email, $newobject->email);
  228. $this->assertEquals($localobject->url, $newobject->url);
  229. $this->assertEquals($localobject->idprof1, $newobject->idprof1);
  230. $this->assertEquals($localobject->idprof2, $newobject->idprof2);
  231. $this->assertEquals($localobject->idprof3, $newobject->idprof3);
  232. $this->assertEquals($localobject->idprof4, $newobject->idprof4);
  233. return $localobject;
  234. }
  235. /**
  236. * testIdProfCheck
  237. *
  238. * @param Societe $localobject Company
  239. * @return Societe $localobject Company
  240. *
  241. * @depends testSocieteUpdate
  242. * The depends says test is run only if previous is ok
  243. */
  244. public function testIdProfCheck($localobject)
  245. {
  246. // OK FR
  247. $localobject->country_code='FR';
  248. $localobject->idprof1=493861496;
  249. $localobject->idprof2=49386149600021;
  250. $result=$localobject->id_prof_check(1, $localobject); // Must be > 0
  251. print __METHOD__." OK FR idprof1 result=".$result."\n";
  252. $this->assertGreaterThanOrEqual(1, $result);
  253. $result=$localobject->id_prof_check(2, $localobject); // Must be > 0
  254. print __METHOD__." OK FR idprof2 result=".$result."\n";
  255. $this->assertGreaterThanOrEqual(1, $result);
  256. // KO FR
  257. $localobject->country_code='FR';
  258. $localobject->idprof1='id1ko';
  259. $localobject->idprof2='id2ko';
  260. $result=$localobject->id_prof_check(1, $localobject); // Must be <= 0
  261. print __METHOD__." KO FR idprof1 result=".$result."\n";
  262. $this->assertLessThan(1, $result);
  263. $result=$localobject->id_prof_check(2, $localobject); // Must be <= 0
  264. print __METHOD__." KO FR idprof2 result=".$result."\n";
  265. $this->assertLessThan(1, $result);
  266. // KO ES
  267. $localobject->country_code='ES';
  268. $localobject->idprof1='id1ko';
  269. $result=$localobject->id_prof_check(1, $localobject); // Must be <= 0
  270. print __METHOD__." KO ES idprof1 result=".$result."\n";
  271. $this->assertLessThan(1, $result);
  272. // OK AR
  273. $localobject->country_code='AR';
  274. $localobject->idprof1='id1ko';
  275. $localobject->idprof2='id2ko';
  276. $result=$localobject->id_prof_check(1, $localobject); // Must be > 0
  277. print __METHOD__." OK AR idprof1 result=".$result."\n";
  278. $this->assertGreaterThanOrEqual(0, $result);
  279. $result=$localobject->id_prof_check(2, $localobject); // Must be > 0
  280. print __METHOD__." OK AR idprof2 result=".$result."\n";
  281. $this->assertGreaterThanOrEqual(1, $result);
  282. return $localobject;
  283. }
  284. /**
  285. * testSocieteOther
  286. *
  287. * @param Societe $localobject Company
  288. * @return int $id Id of company
  289. *
  290. * @depends testIdProfCheck
  291. * The depends says test is run only if previous is ok
  292. */
  293. public function testSocieteOther($localobject)
  294. {
  295. global $conf,$user,$langs,$db;
  296. $conf=$this->savconf;
  297. $user=$this->savuser;
  298. $langs=$this->savlangs;
  299. $db=$this->savdb;
  300. $result=$localobject->setAsCustomer();
  301. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  302. $this->assertLessThan($result, 0);
  303. $result=$localobject->setPriceLevel(1, $user);
  304. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  305. $this->assertLessThan($result, 0);
  306. $result=$localobject->set_remise_client(10, 'Gift', $user);
  307. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  308. $this->assertLessThan($result, 0);
  309. $result=$localobject->getNomUrl(1);
  310. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  311. $this->assertNotEquals($result, '');
  312. $localobject->country_code = 'FR';
  313. $result=$localobject->isInEEC();
  314. print __METHOD__." id=".$localobject->id." country_code=".$localobject->country_code." result=".$result."\n";
  315. $this->assertTrue($result);
  316. $localobject->country_code = 'US';
  317. $result=$localobject->isInEEC();
  318. print __METHOD__." id=".$localobject->id." country_code=".$localobject->country_code." result=".$result."\n";
  319. $this->assertFalse($result);
  320. /*$localobject->country_code = 'GB';
  321. $result=$localobject->isInEEC();
  322. print __METHOD__." id=".$localobject->id." country_code=".$localobject->country_code." result=".$result."\n";
  323. $this->assertTrue($result);
  324. */
  325. $localobject->info($localobject->id);
  326. print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
  327. $this->assertNotEquals($localobject->date_creation, '');
  328. return $localobject->id;
  329. }
  330. /**
  331. * testGetOutstandingBills
  332. *
  333. * @param int $id Id of company
  334. * @return int
  335. *
  336. * @depends testSocieteOther
  337. * The depends says test is run only if previous is ok
  338. */
  339. public function testGetOutstandingBills($id)
  340. {
  341. global $conf,$user,$langs,$db;
  342. $conf=$this->savconf;
  343. $user=$this->savuser;
  344. $langs=$this->savlangs;
  345. $db=$this->savdb;
  346. $localobject=new Societe($db);
  347. $localobject->fetch($id);
  348. $result=$localobject->getOutstandingBills();
  349. print __METHOD__." id=".$id." result=".var_export($result, true)."\n";
  350. $this->assertTrue(array_key_exists('opened', $result), 'Result of getOutstandingBills failed');
  351. return $id;
  352. }
  353. /**
  354. * testSocieteDelete
  355. *
  356. * @param int $id Id of company
  357. * @return int
  358. *
  359. * @depends testGetOutstandingBills
  360. * The depends says test is run only if previous is ok
  361. */
  362. public function testSocieteDelete($id)
  363. {
  364. global $conf,$user,$langs,$db;
  365. $conf=$this->savconf;
  366. $user=$this->savuser;
  367. $langs=$this->savlangs;
  368. $db=$this->savdb;
  369. $localobject=new Societe($db);
  370. $result=$localobject->fetch($id);
  371. $result=$localobject->delete($id, $user);
  372. print __METHOD__." id=".$id." result=".$result."\n";
  373. $this->assertLessThan($result, 0);
  374. return $result;
  375. }
  376. /**
  377. * testSocieteGetFullAddress
  378. *
  379. * @return int $id Id of company
  380. */
  381. public function testSocieteGetFullAddress()
  382. {
  383. global $conf,$user,$langs,$db;
  384. $conf=$this->savconf;
  385. $user=$this->savuser;
  386. $langs=$this->savlangs;
  387. $db=$this->savdb;
  388. $localobjectadd=new Societe($db);
  389. $localobjectadd->initAsSpecimen();
  390. // France
  391. unset($localobjectadd->country_code);
  392. $localobjectadd->country_id=1;
  393. $localobjectadd->name='New name';
  394. $localobjectadd->address='New address';
  395. $localobjectadd->zip='New zip';
  396. $localobjectadd->town='New town';
  397. $result=$localobjectadd->getFullAddress(1);
  398. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  399. $this->assertStringContainsString("New address\nNew zip New town\nFrance", $result);
  400. // Belgium
  401. unset($localobjectadd->country_code);
  402. $localobjectadd->country_id=2;
  403. $localobjectadd->name='New name';
  404. $localobjectadd->address='New address';
  405. $localobjectadd->zip='New zip';
  406. $localobjectadd->town='New town';
  407. $result=$localobjectadd->getFullAddress(1);
  408. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  409. $this->assertStringContainsString("New address\nNew zip New town\nBelgium", $result);
  410. // Switzerland
  411. unset($localobjectadd->country_code);
  412. $localobjectadd->country_id=6;
  413. $localobjectadd->name='New name';
  414. $localobjectadd->address='New address';
  415. $localobjectadd->zip='New zip';
  416. $localobjectadd->town='New town';
  417. $result=$localobjectadd->getFullAddress(1);
  418. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  419. $this->assertStringContainsString("New address\nNew zip New town\nSwitzerland", $result);
  420. // USA
  421. unset($localobjectadd->country_code);
  422. $localobjectadd->country_id=11;
  423. $localobjectadd->name='New name';
  424. $localobjectadd->address='New address';
  425. $localobjectadd->zip='New zip';
  426. $localobjectadd->town='New town';
  427. $localobjectadd->state='New state';
  428. $result=$localobjectadd->getFullAddress(1);
  429. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  430. $this->assertStringContainsString("New address\nNew town, New state, New zip\nUnited States", $result);
  431. return $localobjectadd->id;
  432. }
  433. /**
  434. * testSocieteMerge
  435. *
  436. * Check that we can merge two companies together. In this test,
  437. * no other object is referencing the companies.
  438. *
  439. * @return int the result of the merge and fetch operation
  440. */
  441. public function testSocieteMerge()
  442. {
  443. global $user, $db;
  444. $soc1 = new Societe($db);
  445. $soc1->initAsSpecimen();
  446. $soc1_id = $soc1->create($user);
  447. $this->assertLessThanOrEqual($soc1_id, 0);
  448. $soc2 = new Societe($db);
  449. $soc2->entity = 1;
  450. $soc2->name = "Copy of ".$soc1->name;
  451. $soc2->code_client = 'CC-0002';
  452. $soc2->code_fournisseur = 'SC-0002';
  453. $soc2_id = $soc2->create($user);
  454. $this->assertLessThanOrEqual($soc2_id, 0, implode('\n', $soc2->errors));
  455. $result = $soc1->mergeCompany($soc2_id);
  456. $this->assertLessThanOrEqual($result, 0, implode('\n', $soc1->errors));
  457. $result = $soc1->fetch($soc1_id);
  458. $this->assertLessThanOrEqual($result, 0);
  459. print __METHOD__." result=".$result."\n";
  460. return $result;
  461. }
  462. }