AdherentTest.php 17 KB

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