AdherentTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. <?php
  2. /* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2013 Marcos García <marcosgdf@gmail.com>
  4. * Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. * or see https://www.gnu.org/
  19. */
  20. /**
  21. * \file test/phpunit/AdherentTest.php
  22. * \ingroup test
  23. * \brief PHPUnit test
  24. * \remarks To run this script as CLI: phpunit filename.php
  25. */
  26. global $conf,$user,$langs,$db;
  27. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  28. //require_once 'PHPUnit/Autoload.php';
  29. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  30. require_once dirname(__FILE__).'/../../htdocs/adherents/class/adherent.class.php';
  31. require_once dirname(__FILE__).'/../../htdocs/adherents/class/adherent_type.class.php';
  32. if (empty($user->id)) {
  33. print "Load permissions for admin user nb 1\n";
  34. $user->fetch(1);
  35. $user->getrights();
  36. }
  37. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  38. /**
  39. * Class for PHPUnit tests
  40. *
  41. * @backupGlobals disabled
  42. * @backupStaticAttributes enabled
  43. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  44. */
  45. class AdherentTest extends PHPUnit\Framework\TestCase
  46. {
  47. protected $savconf;
  48. protected $savuser;
  49. protected $savlangs;
  50. protected $savdb;
  51. /**
  52. * Constructor
  53. * We save global variables into local variables
  54. *
  55. * @param string $name Name
  56. * @return AdherentTest
  57. */
  58. public function __construct($name = '')
  59. {
  60. parent::__construct($name);
  61. //$this->sharedFixture
  62. global $conf,$user,$langs,$db;
  63. $this->savconf=$conf;
  64. $this->savuser=$user;
  65. $this->savlangs=$langs;
  66. $this->savdb=$db;
  67. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  68. //print " - db ".$db->db;
  69. print "\n";
  70. }
  71. /**
  72. * setUpBeforeClass
  73. *
  74. * @return void
  75. */
  76. public static function setUpBeforeClass(): void
  77. {
  78. global $conf,$user,$langs,$db;
  79. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  80. if (getDolGlobalString('MAIN_FIRSTNAME_NAME_POSITION')) {
  81. print "\n".__METHOD__." Company must be setup to have name-firstname in order 'Firstname Lastname'\n";
  82. die(1);
  83. }
  84. if (getDolGlobalString('MAIN_MODULE_LDAP')) {
  85. print "\n".__METHOD__." module LDAP must be disabled.\n";
  86. die(1);
  87. }
  88. if (getDolGlobalString('MAIN_MODULE_MAILMANSPIP')) {
  89. print "\n".__METHOD__." module MailmanSpip must be disabled.\n";
  90. die(1);
  91. }
  92. print __METHOD__."\n";
  93. }
  94. /**
  95. * tearDownAfterClass
  96. *
  97. * @return void
  98. */
  99. public static function tearDownAfterClass(): void
  100. {
  101. global $conf,$user,$langs,$db;
  102. $db->rollback();
  103. print __METHOD__."\n";
  104. }
  105. /**
  106. * Init phpunit tests
  107. *
  108. * @return void
  109. */
  110. protected function setUp(): void
  111. {
  112. global $conf,$user,$langs,$db;
  113. $conf=$this->savconf;
  114. $user=$this->savuser;
  115. $langs=$this->savlangs;
  116. $db=$this->savdb;
  117. print __METHOD__."\n";
  118. }
  119. /**
  120. * End phpunit tests
  121. *
  122. * @return void
  123. */
  124. protected function tearDown(): void
  125. {
  126. print __METHOD__."\n";
  127. }
  128. /**
  129. * testAdherentTypeCreate
  130. *
  131. * @return void
  132. */
  133. public function testAdherentTypeCreate()
  134. {
  135. global $conf,$user,$langs,$db;
  136. $conf=$this->savconf;
  137. $user=$this->savuser;
  138. $langs=$this->savlangs;
  139. $db=$this->savdb;
  140. $localobject=new AdherentType($db);
  141. $localobject->statut=1;
  142. $localobject->label='Adherent type test';
  143. $localobject->subscription=1;
  144. $localobject->amount=0;
  145. $localobject->vote=1;
  146. $localobject->company='Old company label';
  147. $result=$localobject->create($user);
  148. print __METHOD__." result=".$result."\n";
  149. $this->assertLessThan($result, 0);
  150. return $localobject->id;
  151. }
  152. /**
  153. * testAdherentCreate
  154. *
  155. * @param int $fk_adherent_type Id type of member
  156. * @return int
  157. *
  158. * @depends testAdherentTypeCreate
  159. * The depends says test is run only if previous is ok
  160. */
  161. public function testAdherentCreate($fk_adherent_type)
  162. {
  163. global $conf,$user,$langs,$db;
  164. $conf=$this->savconf;
  165. $user=$this->savuser;
  166. $langs=$this->savlangs;
  167. $db=$this->savdb;
  168. $localobject=new Adherent($db);
  169. $localobject->initAsSpecimen();
  170. $localobject->typeid=$fk_adherent_type;
  171. $result=$localobject->create($user);
  172. print __METHOD__." result=".$result."\n";
  173. if ($result < 0) {
  174. print $localobject->error;
  175. }
  176. $this->assertLessThan($result, 0);
  177. return $result;
  178. }
  179. /**
  180. * testAdherentFetch
  181. *
  182. * @param int $id Id of object to fetch
  183. * @return object Fetched object
  184. *
  185. * @depends testAdherentCreate
  186. * The depends says test is run only if previous is ok
  187. */
  188. public function testAdherentFetch($id)
  189. {
  190. global $conf,$user,$langs,$db;
  191. $conf=$this->savconf;
  192. $user=$this->savuser;
  193. $langs=$this->savlangs;
  194. $db=$this->savdb;
  195. $localobject=new Adherent($db);
  196. $result=$localobject->fetch($id);
  197. print __METHOD__." id=".$id." result=".$result."\n";
  198. $this->assertLessThan($result, 0);
  199. return $localobject;
  200. }
  201. /**
  202. * testAdherentFetchLogin
  203. *
  204. * @param Adherent $localobject Member instance
  205. * @return Adherent
  206. *
  207. * @depends testAdherentFetch
  208. * The depends says test is run only if previous is ok
  209. */
  210. public function testAdherentFetchLogin(Adherent $localobject)
  211. {
  212. global $conf,$user,$langs,$db;
  213. $conf=$this->savconf;
  214. $user=$this->savuser;
  215. $langs=$this->savlangs;
  216. $db=$this->savdb;
  217. $newobject = new Adherent($db);
  218. $result = $newobject->fetch_login($localobject->login);
  219. $this->assertEquals($newobject, $localobject);
  220. return $localobject;
  221. }
  222. /**
  223. * testAdherentUpdate
  224. *
  225. * @param Adherent $localobject Member instance
  226. * @return Adherent
  227. *
  228. * @depends testAdherentFetchLogin
  229. * The depends says test is run only if previous is ok
  230. */
  231. public function testAdherentUpdate(Adherent $localobject)
  232. {
  233. global $conf,$user,$langs,$db;
  234. $conf=$this->savconf;
  235. $user=$this->savuser;
  236. $langs=$this->savlangs;
  237. $db=$this->savdb;
  238. $timestamp = dol_now();
  239. $localobject->civility_id = 0;
  240. $localobject->login='newlogin';
  241. $localobject->company='New company label';
  242. $localobject->note_public='New note public after update';
  243. $localobject->note_private='New note private after update';
  244. $localobject->lastname='New name';
  245. $localobject->firstname='New firstname';
  246. $localobject->gender='man';
  247. $localobject->address='New address';
  248. $localobject->zip='New zip';
  249. $localobject->town='New town';
  250. $localobject->country_id=2;
  251. $localobject->statut=0;
  252. $localobject->morphy=0;
  253. $localobject->phone='New tel pro';
  254. $localobject->phone_perso='New tel perso';
  255. $localobject->phone_mobile='New tel mobile';
  256. $localobject->email='newemail@newemail.com';
  257. $localobject->birth=$timestamp;
  258. $result=$localobject->update($user);
  259. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  260. $this->assertLessThan($result, 0);
  261. $result=$localobject->update_note($localobject->note_private, '_private');
  262. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  263. $this->assertLessThan($result, 0);
  264. $result=$localobject->update_note($localobject->note_public, '_public');
  265. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  266. $this->assertLessThan($result, 0);
  267. $newobject=new Adherent($db);
  268. $result=$newobject->fetch($localobject->id);
  269. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  270. $this->assertLessThan($result, 0);
  271. $this->assertEquals($localobject->civility_id, $newobject->civility_id);
  272. $this->assertEquals($localobject->login, $newobject->login);
  273. $this->assertEquals($localobject->company, $newobject->company);
  274. $this->assertEquals($localobject->note_public, $newobject->note_public);
  275. $this->assertEquals($localobject->note_private, $newobject->note_private);
  276. $this->assertEquals($localobject->lastname, $newobject->lastname);
  277. $this->assertEquals($localobject->firstname, $newobject->firstname);
  278. $this->assertEquals($localobject->gender, $newobject->gender);
  279. $this->assertEquals($localobject->address, $newobject->address);
  280. $this->assertEquals($localobject->zip, $newobject->zip);
  281. $this->assertEquals($localobject->town, $newobject->town);
  282. $this->assertEquals($localobject->country_id, $newobject->country_id);
  283. $this->assertEquals('BE', $newobject->country_code);
  284. $this->assertEquals('Belgium', $newobject->country);
  285. $this->assertEquals($localobject->statut, $newobject->statut);
  286. $this->assertEquals($localobject->phone, $newobject->phone);
  287. $this->assertEquals($localobject->phone_perso, $newobject->phone_perso);
  288. $this->assertEquals($localobject->phone_mobile, $newobject->phone_mobile);
  289. $this->assertEquals($localobject->email, $newobject->email);
  290. $this->assertEquals($localobject->birth, $timestamp);
  291. $this->assertEquals($localobject->morphy, $newobject->morphy);
  292. //We return newobject because of new values
  293. return $newobject;
  294. }
  295. /**
  296. * testAdherentMakeSubstitution
  297. *
  298. * @param Adherent $localobject Member instance
  299. * @return Adherent
  300. *
  301. * @depends testAdherentUpdate
  302. * The depends says test is run only if previous is ok
  303. */
  304. public function testAdherentMakeSubstitution(Adherent $localobject)
  305. {
  306. global $conf,$user,$langs,$db;
  307. $conf=$this->savconf;
  308. $user=$this->savuser;
  309. $langs=$this->savlangs;
  310. $db=$this->savdb;
  311. $conf->global->MAIN_FIRSTNAME_NAME_POSITION = 0; // Force setup for firstname+lastname
  312. $template = '__CIVILITY__,__FIRSTNAME__,__LASTNAME__,__FULLNAME__,__COMPANY__,';
  313. $template .= '__ADDRESS__,__ZIP__,__TOWN__,__COUNTRY__,__EMAIL__,__BIRTH__,__PHOTO__,__LOGIN__';
  314. // If option to store clear password has been set, we get 'dolibspec' into PASSWORD field.
  315. $expected = ',New firstname,New name,New firstname New name,';
  316. $expected .= 'New company label,New address,New zip,New town,Belgium,newemail@newemail.com,'.dol_print_date($localobject->birth, 'day').',,';
  317. $expected .= 'newlogin';
  318. $result = $localobject->makeSubstitution($template);
  319. print __METHOD__." result=".$result."\n";
  320. $this->assertEquals($expected, $result);
  321. return $localobject;
  322. }
  323. /**
  324. * testAdherentSetUserId
  325. *
  326. * @param Adherent $localobject Member instance
  327. * @return Adherent
  328. *
  329. * @depends testAdherentMakeSubstitution
  330. * The depends says test is run only if previous is ok
  331. */
  332. public function testAdherentSetUserId(Adherent $localobject)
  333. {
  334. global $conf,$user,$langs,$db;
  335. $conf=$this->savconf;
  336. $user=$this->savuser;
  337. $langs=$this->savlangs;
  338. $db=$this->savdb;
  339. //We associate member with user
  340. $result = $localobject->setUserId($user->id);
  341. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  342. $this->assertEquals($result, 1);
  343. //We update user object
  344. $user->fetch($user->id);
  345. print __METHOD__." user id=".$user->id." fk_member=".$user->fk_member."\n";
  346. $this->assertEquals($user->fk_member, $localobject->id);
  347. //We remove association with user
  348. $result = $localobject->setUserId(0);
  349. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  350. $this->assertEquals($result, 1);
  351. //We update user object
  352. $user->fetch($user->id);
  353. print __METHOD__." user id=".$user->id." fk_member=".$user->fk_member."\n";
  354. $this->assertNull($user->fk_member);
  355. return $localobject;
  356. }
  357. /**
  358. * testAdherentSetThirdPartyId
  359. *
  360. * @param Adherent $localobject Member instance
  361. * @return Adherent
  362. *
  363. * @depends testAdherentSetUserId
  364. * The depends says test is run only if previous is ok
  365. */
  366. public function testAdherentSetThirdPartyId(Adherent $localobject)
  367. {
  368. global $conf,$user,$langs,$db;
  369. $conf=$this->savconf;
  370. $user=$this->savuser;
  371. $langs=$this->savlangs;
  372. $db=$this->savdb;
  373. //Create a Third Party
  374. $thirdparty = new Societe($db);
  375. $thirdparty->initAsSpecimen();
  376. $result = $thirdparty->create($user);
  377. print __METHOD__." third party id=".$thirdparty->id." result=".$result."\n";
  378. $this->assertTrue($result > 0, 'Test to create a thirdparty specimen to use it to set as thirdparty of a member');
  379. //Set Third Party ID
  380. $result = $localobject->setThirdPartyId($thirdparty->id);
  381. $this->assertEquals($result, 1, 'Set thirdparty');
  382. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  383. //Adherent is updated with new data
  384. $localobject->fetch($localobject->id);
  385. $this->assertEquals($localobject->fk_soc, $thirdparty->id, 'Fetch member');
  386. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  387. //We remove the third party association
  388. $result = $localobject->setThirdPartyId(0);
  389. $this->assertEquals($result, 1, 'Removed the link with thirdparty');
  390. //And check if it has been updated
  391. $localobject->fetch($localobject->id);
  392. $this->assertNull($localobject->fk_soc, 'Check field is null');
  393. //Now we remove the third party
  394. $result = $thirdparty->delete($thirdparty->id, $user);
  395. $this->assertEquals($result, 1, 'Delete thirdparty');
  396. return $localobject;
  397. }
  398. /**
  399. * testAdherentValid
  400. *
  401. * @param Adherent $localobject Member instance
  402. * @return Adherent
  403. *
  404. * @depends testAdherentSetThirdPartyId
  405. * The depends says test is run only if previous is ok
  406. */
  407. public function testAdherentValidate(Adherent $localobject)
  408. {
  409. global $conf,$user,$langs,$db;
  410. $conf=$this->savconf;
  411. $user=$this->savuser;
  412. $langs=$this->savlangs;
  413. $db=$this->savdb;
  414. $result=$localobject->validate($user);
  415. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  416. $this->assertLessThan($result, 0);
  417. return $localobject;
  418. }
  419. /**
  420. * testAdherentOther
  421. *
  422. * @param Adherent $localobject Member instance
  423. * @return int Id of object
  424. *
  425. * @depends testAdherentValidate
  426. * The depends says test is run only if previous is ok
  427. */
  428. public function testAdherentOther(Adherent $localobject)
  429. {
  430. global $conf,$user,$langs,$db;
  431. $conf=$this->savconf;
  432. $user=$this->savuser;
  433. $langs=$this->savlangs;
  434. $db=$this->savdb;
  435. /*$result=$localobject->setstatus(0);
  436. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  437. $this->assertLessThan($result, 0);
  438. */
  439. $localobject->info($localobject->id);
  440. print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
  441. $this->assertNotEquals($localobject->date_creation, '');
  442. return $localobject;
  443. }
  444. /**
  445. * testAdherentResiliate
  446. *
  447. * @param Adherent $localobject Member instance
  448. * @return Adherent
  449. *
  450. * @depends testAdherentOther
  451. * The depends says test is run only if previous is ok
  452. */
  453. public function testAdherentResiliate(Adherent $localobject)
  454. {
  455. global $conf,$user,$langs,$db;
  456. $conf=$this->savconf;
  457. $user=$this->savuser;
  458. $langs=$this->savlangs;
  459. $db=$this->savdb;
  460. //Let's resilie un adherent
  461. $result = $localobject->resiliate($user);
  462. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  463. $this->assertEquals($result, 1);
  464. //Is statut updated?
  465. $this->assertEquals($localobject->statut, 0);
  466. //We update the object and let's check if it was updated on DB
  467. $localobject->fetch($localobject->id);
  468. $this->assertEquals($localobject->statut, 0);
  469. //Now that status=0, resiliate should return 0
  470. $result = $localobject->resiliate($user);
  471. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  472. $this->assertEquals($result, 0);
  473. return $localobject;
  474. }
  475. /**
  476. * testAdherentDelete
  477. *
  478. * @param Adherent $localobject Member instance
  479. * @return void
  480. *
  481. * @depends testAdherentResiliate
  482. * The depends says test is run only if previous is ok
  483. */
  484. public function testAdherentDelete($localobject)
  485. {
  486. global $conf,$user,$langs,$db;
  487. $conf=$this->savconf;
  488. $user=$this->savuser;
  489. $langs=$this->savlangs;
  490. $db=$this->savdb;
  491. $result=$localobject->delete($localobject->id, $user);
  492. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  493. $this->assertLessThan($result, 0);
  494. return $localobject;
  495. }
  496. /**
  497. * testAdherentTypeDelete
  498. *
  499. * @param Adherent $localobject Member instance
  500. * @return void
  501. *
  502. * @depends testAdherentDelete
  503. * The depends says test is run only if previous is ok
  504. */
  505. public function testAdherentTypeDelete($localobject)
  506. {
  507. global $conf,$user,$langs,$db;
  508. $conf=$this->savconf;
  509. $user=$this->savuser;
  510. $langs=$this->savlangs;
  511. $db=$this->savdb;
  512. $localobjectat=new AdherentType($db);
  513. $result=$localobjectat->fetch($localobject->typeid);
  514. $result=$localobjectat->delete($user);
  515. print __METHOD__." result=".$result."\n";
  516. $this->assertLessThan($result, 0);
  517. return $localobject->id;
  518. }
  519. }