AdherentTest.php 18 KB

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