AdherentTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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 (!empty($conf->global->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 (!empty($conf->global->MAIN_MODULE_LDAP)) {
  85. print "\n".__METHOD__." module LDAP must be disabled.\n"; die(1);
  86. }
  87. if (!empty($conf->global->MAIN_MODULE_MAILMANSPIP)) {
  88. print "\n".__METHOD__." module MailmanSpip must be disabled.\n"; die(1);
  89. }
  90. print __METHOD__."\n";
  91. }
  92. /**
  93. * tearDownAfterClass
  94. *
  95. * @return void
  96. */
  97. public static function tearDownAfterClass(): void
  98. {
  99. global $conf,$user,$langs,$db;
  100. $db->rollback();
  101. print __METHOD__."\n";
  102. }
  103. /**
  104. * Init phpunit tests
  105. *
  106. * @return void
  107. */
  108. protected function setUp(): void
  109. {
  110. global $conf,$user,$langs,$db;
  111. $conf=$this->savconf;
  112. $user=$this->savuser;
  113. $langs=$this->savlangs;
  114. $db=$this->savdb;
  115. print __METHOD__."\n";
  116. }
  117. /**
  118. * End phpunit tests
  119. *
  120. * @return void
  121. */
  122. protected function tearDown(): void
  123. {
  124. print __METHOD__."\n";
  125. }
  126. /**
  127. * testAdherentTypeCreate
  128. *
  129. * @return void
  130. */
  131. public function testAdherentTypeCreate()
  132. {
  133. global $conf,$user,$langs,$db;
  134. $conf=$this->savconf;
  135. $user=$this->savuser;
  136. $langs=$this->savlangs;
  137. $db=$this->savdb;
  138. $localobject=new AdherentType($db);
  139. $localobject->statut=1;
  140. $localobject->label='Adherent type test';
  141. $localobject->subscription=1;
  142. $localobject->amount=0;
  143. $localobject->vote=1;
  144. $localobject->company='Old company label';
  145. $result=$localobject->create($user);
  146. print __METHOD__." result=".$result."\n";
  147. $this->assertLessThan($result, 0);
  148. return $localobject->id;
  149. }
  150. /**
  151. * testAdherentCreate
  152. *
  153. * @param int $fk_adherent_type Id type of member
  154. * @return int
  155. *
  156. * @depends testAdherentTypeCreate
  157. * The depends says test is run only if previous is ok
  158. */
  159. public function testAdherentCreate($fk_adherent_type)
  160. {
  161. global $conf,$user,$langs,$db;
  162. $conf=$this->savconf;
  163. $user=$this->savuser;
  164. $langs=$this->savlangs;
  165. $db=$this->savdb;
  166. $localobject=new Adherent($db);
  167. $localobject->initAsSpecimen();
  168. $localobject->typeid=$fk_adherent_type;
  169. $result=$localobject->create($user);
  170. print __METHOD__." result=".$result."\n";
  171. if ($result < 0) {
  172. print $localobject->error;
  173. }
  174. $this->assertLessThan($result, 0);
  175. return $result;
  176. }
  177. /**
  178. * testAdherentFetch
  179. *
  180. * @param int $id Id of object to fetch
  181. * @return object Fetched object
  182. *
  183. * @depends testAdherentCreate
  184. * The depends says test is run only if previous is ok
  185. */
  186. public function testAdherentFetch($id)
  187. {
  188. global $conf,$user,$langs,$db;
  189. $conf=$this->savconf;
  190. $user=$this->savuser;
  191. $langs=$this->savlangs;
  192. $db=$this->savdb;
  193. $localobject=new Adherent($db);
  194. $result=$localobject->fetch($id);
  195. print __METHOD__." id=".$id." result=".$result."\n";
  196. $this->assertLessThan($result, 0);
  197. return $localobject;
  198. }
  199. /**
  200. * testAdherentFetchLogin
  201. *
  202. * @param Adherent $localobject Member instance
  203. * @return Adherent
  204. *
  205. * @depends testAdherentFetch
  206. * The depends says test is run only if previous is ok
  207. */
  208. public function testAdherentFetchLogin(Adherent $localobject)
  209. {
  210. global $conf,$user,$langs,$db;
  211. $conf=$this->savconf;
  212. $user=$this->savuser;
  213. $langs=$this->savlangs;
  214. $db=$this->savdb;
  215. $newobject = new Adherent($db);
  216. $result = $newobject->fetch_login($localobject->login);
  217. $this->assertEquals($newobject, $localobject);
  218. return $localobject;
  219. }
  220. /**
  221. * testAdherentUpdate
  222. *
  223. * @param Adherent $localobject Member instance
  224. * @return Adherent
  225. *
  226. * @depends testAdherentFetchLogin
  227. * The depends says test is run only if previous is ok
  228. */
  229. public function testAdherentUpdate(Adherent $localobject)
  230. {
  231. global $conf,$user,$langs,$db;
  232. $conf=$this->savconf;
  233. $user=$this->savuser;
  234. $langs=$this->savlangs;
  235. $db=$this->savdb;
  236. $timestamp = dol_now();
  237. $localobject->civility_id = 0;
  238. $localobject->login='newlogin';
  239. $localobject->company='New company label';
  240. $localobject->note_public='New note public after update';
  241. $localobject->note_private='New note private after update';
  242. $localobject->lastname='New name';
  243. $localobject->firstname='New firstname';
  244. $localobject->gender='man';
  245. $localobject->address='New address';
  246. $localobject->zip='New zip';
  247. $localobject->town='New town';
  248. $localobject->country_id=2;
  249. $localobject->statut=0;
  250. $localobject->morphy=0;
  251. $localobject->phone='New tel pro';
  252. $localobject->phone_perso='New tel perso';
  253. $localobject->phone_mobile='New tel mobile';
  254. $localobject->email='newemail@newemail.com';
  255. $localobject->birth=$timestamp;
  256. $result=$localobject->update($user);
  257. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  258. $this->assertLessThan($result, 0);
  259. $result=$localobject->update_note($localobject->note_private, '_private');
  260. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  261. $this->assertLessThan($result, 0);
  262. $result=$localobject->update_note($localobject->note_public, '_public');
  263. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  264. $this->assertLessThan($result, 0);
  265. $newobject=new Adherent($db);
  266. $result=$newobject->fetch($localobject->id);
  267. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  268. $this->assertLessThan($result, 0);
  269. $this->assertEquals($localobject->civility_id, $newobject->civility_id);
  270. $this->assertEquals($localobject->login, $newobject->login);
  271. $this->assertEquals($localobject->company, $newobject->company);
  272. $this->assertEquals($localobject->note_public, $newobject->note_public);
  273. $this->assertEquals($localobject->note_private, $newobject->note_private);
  274. $this->assertEquals($localobject->lastname, $newobject->lastname);
  275. $this->assertEquals($localobject->firstname, $newobject->firstname);
  276. $this->assertEquals($localobject->gender, $newobject->gender);
  277. $this->assertEquals($localobject->address, $newobject->address);
  278. $this->assertEquals($localobject->zip, $newobject->zip);
  279. $this->assertEquals($localobject->town, $newobject->town);
  280. $this->assertEquals($localobject->country_id, $newobject->country_id);
  281. $this->assertEquals('BE', $newobject->country_code);
  282. $this->assertEquals('Belgium', $newobject->country);
  283. $this->assertEquals($localobject->statut, $newobject->statut);
  284. $this->assertEquals($localobject->phone, $newobject->phone);
  285. $this->assertEquals($localobject->phone_perso, $newobject->phone_perso);
  286. $this->assertEquals($localobject->phone_mobile, $newobject->phone_mobile);
  287. $this->assertEquals($localobject->email, $newobject->email);
  288. $this->assertEquals($localobject->birth, $timestamp);
  289. $this->assertEquals($localobject->morphy, $newobject->morphy);
  290. //We return newobject because of new values
  291. return $newobject;
  292. }
  293. /**
  294. * testAdherentMakeSubstitution
  295. *
  296. * @param Adherent $localobject Member instance
  297. * @return Adherent
  298. *
  299. * @depends testAdherentUpdate
  300. * The depends says test is run only if previous is ok
  301. */
  302. public function testAdherentMakeSubstitution(Adherent $localobject)
  303. {
  304. global $conf,$user,$langs,$db;
  305. $conf=$this->savconf;
  306. $user=$this->savuser;
  307. $langs=$this->savlangs;
  308. $db=$this->savdb;
  309. $conf->global->MAIN_FIRSTNAME_NAME_POSITION = 0; // Force setup for firstname+lastname
  310. $template = '__CIVILITY__,__FIRSTNAME__,__LASTNAME__,__FULLNAME__,__COMPANY__,';
  311. $template .= '__ADDRESS__,__ZIP__,__TOWN__,__COUNTRY__,__EMAIL__,__BIRTH__,__PHOTO__,__LOGIN__';
  312. // If option to store clear password has been set, we get 'dolibspec' into PASSWORD field.
  313. $expected = ',New firstname,New name,New firstname New name,';
  314. $expected .= 'New company label,New address,New zip,New town,Belgium,newemail@newemail.com,'.dol_print_date($localobject->birth, 'day').',,';
  315. $expected .= 'newlogin';
  316. $result = $localobject->makeSubstitution($template);
  317. print __METHOD__." result=".$result."\n";
  318. $this->assertEquals($expected, $result);
  319. return $localobject;
  320. }
  321. /**
  322. * testAdherentSetUserId
  323. *
  324. * @param Adherent $localobject Member instance
  325. * @return Adherent
  326. *
  327. * @depends testAdherentMakeSubstitution
  328. * The depends says test is run only if previous is ok
  329. */
  330. public function testAdherentSetUserId(Adherent $localobject)
  331. {
  332. global $conf,$user,$langs,$db;
  333. $conf=$this->savconf;
  334. $user=$this->savuser;
  335. $langs=$this->savlangs;
  336. $db=$this->savdb;
  337. //We associate member with user
  338. $result = $localobject->setUserId($user->id);
  339. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  340. $this->assertEquals($result, 1);
  341. //We update user object
  342. $user->fetch($user->id);
  343. print __METHOD__." user id=".$user->id." fk_member=".$user->fk_member."\n";
  344. $this->assertEquals($user->fk_member, $localobject->id);
  345. //We remove association with user
  346. $result = $localobject->setUserId(0);
  347. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  348. $this->assertEquals($result, 1);
  349. //We update user object
  350. $user->fetch($user->id);
  351. print __METHOD__." user id=".$user->id." fk_member=".$user->fk_member."\n";
  352. $this->assertNull($user->fk_member);
  353. return $localobject;
  354. }
  355. /**
  356. * testAdherentSetThirdPartyId
  357. *
  358. * @param Adherent $localobject Member instance
  359. * @return Adherent
  360. *
  361. * @depends testAdherentSetUserId
  362. * The depends says test is run only if previous is ok
  363. */
  364. public function testAdherentSetThirdPartyId(Adherent $localobject)
  365. {
  366. global $conf,$user,$langs,$db;
  367. $conf=$this->savconf;
  368. $user=$this->savuser;
  369. $langs=$this->savlangs;
  370. $db=$this->savdb;
  371. //Create a Third Party
  372. $thirdparty = new Societe($db);
  373. $thirdparty->initAsSpecimen();
  374. $result = $thirdparty->create($user);
  375. print __METHOD__." third party id=".$thirdparty->id." result=".$result."\n";
  376. $this->assertTrue($result > 0, 'Test to create a thirdparty specimen to use it to set as thirdparty of a member');
  377. //Set Third Party ID
  378. $result = $localobject->setThirdPartyId($thirdparty->id);
  379. $this->assertEquals($result, 1, 'Set thirdparty');
  380. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  381. //Adherent is updated with new data
  382. $localobject->fetch($localobject->id);
  383. $this->assertEquals($localobject->fk_soc, $thirdparty->id, 'Fetch member');
  384. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  385. //We remove the third party association
  386. $result = $localobject->setThirdPartyId(0);
  387. $this->assertEquals($result, 1, 'Removed the link with thirdparty');
  388. //And check if it has been updated
  389. $localobject->fetch($localobject->id);
  390. $this->assertNull($localobject->fk_soc, 'Check field is null');
  391. //Now we remove the third party
  392. $result = $thirdparty->delete($thirdparty->id, $user);
  393. $this->assertEquals($result, 1, 'Delete thirdparty');
  394. return $localobject;
  395. }
  396. /**
  397. * testAdherentValid
  398. *
  399. * @param Adherent $localobject Member instance
  400. * @return Adherent
  401. *
  402. * @depends testAdherentSetThirdPartyId
  403. * The depends says test is run only if previous is ok
  404. */
  405. public function testAdherentValidate(Adherent $localobject)
  406. {
  407. global $conf,$user,$langs,$db;
  408. $conf=$this->savconf;
  409. $user=$this->savuser;
  410. $langs=$this->savlangs;
  411. $db=$this->savdb;
  412. $result=$localobject->validate($user);
  413. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  414. $this->assertLessThan($result, 0);
  415. return $localobject;
  416. }
  417. /**
  418. * testAdherentOther
  419. *
  420. * @param Adherent $localobject Member instance
  421. * @return int Id of object
  422. *
  423. * @depends testAdherentValidate
  424. * The depends says test is run only if previous is ok
  425. */
  426. public function testAdherentOther(Adherent $localobject)
  427. {
  428. global $conf,$user,$langs,$db;
  429. $conf=$this->savconf;
  430. $user=$this->savuser;
  431. $langs=$this->savlangs;
  432. $db=$this->savdb;
  433. /*$result=$localobject->setstatus(0);
  434. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  435. $this->assertLessThan($result, 0);
  436. */
  437. $localobject->info($localobject->id);
  438. print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
  439. $this->assertNotEquals($localobject->date_creation, '');
  440. return $localobject;
  441. }
  442. /**
  443. * testAdherentResiliate
  444. *
  445. * @param Adherent $localobject Member instance
  446. * @return Adherent
  447. *
  448. * @depends testAdherentOther
  449. * The depends says test is run only if previous is ok
  450. */
  451. public function testAdherentResiliate(Adherent $localobject)
  452. {
  453. global $conf,$user,$langs,$db;
  454. $conf=$this->savconf;
  455. $user=$this->savuser;
  456. $langs=$this->savlangs;
  457. $db=$this->savdb;
  458. //Let's resilie un adherent
  459. $result = $localobject->resiliate($user);
  460. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  461. $this->assertEquals($result, 1);
  462. //Is statut updated?
  463. $this->assertEquals($localobject->statut, 0);
  464. //We update the object and let's check if it was updated on DB
  465. $localobject->fetch($localobject->id);
  466. $this->assertEquals($localobject->statut, 0);
  467. //Now that status=0, resiliate should return 0
  468. $result = $localobject->resiliate($user);
  469. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  470. $this->assertEquals($result, 0);
  471. return $localobject;
  472. }
  473. /**
  474. * testAdherentDelete
  475. *
  476. * @param Adherent $localobject Member instance
  477. * @return void
  478. *
  479. * @depends testAdherentResiliate
  480. * The depends says test is run only if previous is ok
  481. */
  482. public function testAdherentDelete($localobject)
  483. {
  484. global $conf,$user,$langs,$db;
  485. $conf=$this->savconf;
  486. $user=$this->savuser;
  487. $langs=$this->savlangs;
  488. $db=$this->savdb;
  489. $result=$localobject->delete($localobject->id, $user);
  490. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  491. $this->assertLessThan($result, 0);
  492. return $localobject;
  493. }
  494. /**
  495. * testAdherentTypeDelete
  496. *
  497. * @param Adherent $localobject Member instance
  498. * @return void
  499. *
  500. * @depends testAdherentDelete
  501. * The depends says test is run only if previous is ok
  502. */
  503. public function testAdherentTypeDelete($localobject)
  504. {
  505. global $conf,$user,$langs,$db;
  506. $conf=$this->savconf;
  507. $user=$this->savuser;
  508. $langs=$this->savlangs;
  509. $db=$this->savdb;
  510. $localobjectat=new AdherentType($db);
  511. $result=$localobjectat->fetch($localobject->typeid);
  512. $result=$localobjectat->delete();
  513. print __METHOD__." result=".$result."\n";
  514. $this->assertLessThan($result, 0);
  515. return $localobject->id;
  516. }
  517. }