ContactTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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/ContactTest.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/contact/class/contact.class.php';
  30. $langs->load("dict");
  31. if ($langs->defaultlang != 'en_US') {
  32. print "Error: Default language for company to run tests must be set to en_US or auto. Current is ".$langs->defaultlang."\n";
  33. exit(1);
  34. }
  35. if (empty($user->id)) {
  36. print "Load permissions for admin user nb 1\n";
  37. $user->fetch(1);
  38. $user->getrights();
  39. }
  40. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  41. /**
  42. * Class for PHPUnit tests
  43. *
  44. * @backupGlobals disabled
  45. * @backupStaticAttributes enabled
  46. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  47. */
  48. class ContactTest extends PHPUnit\Framework\TestCase
  49. {
  50. protected $savconf;
  51. protected $savuser;
  52. protected $savlangs;
  53. protected $savdb;
  54. /**
  55. * Constructor
  56. * We save global variables into local variables
  57. *
  58. * @param string $name Name
  59. * @return ContactTest
  60. */
  61. public function __construct($name = '')
  62. {
  63. parent::__construct($name);
  64. //$this->sharedFixture
  65. global $conf,$user,$langs,$db;
  66. $this->savconf=$conf;
  67. $this->savuser=$user;
  68. $this->savlangs=$langs;
  69. $this->savdb=$db;
  70. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  71. //print " - db ".$db->db;
  72. print "\n";
  73. }
  74. /**
  75. * setUpBeforeClass
  76. *
  77. * @return void
  78. */
  79. public static function setUpBeforeClass(): void
  80. {
  81. global $conf,$user,$langs,$db;
  82. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  83. print __METHOD__."\n";
  84. }
  85. /**
  86. * tearDownAfterClass
  87. *
  88. * @return void
  89. */
  90. public static function tearDownAfterClass(): void
  91. {
  92. global $conf,$user,$langs,$db;
  93. $db->rollback();
  94. print __METHOD__."\n";
  95. }
  96. /**
  97. * Init phpunit tests
  98. *
  99. * @return void
  100. */
  101. protected function setUp(): void
  102. {
  103. global $conf,$user,$langs,$db;
  104. $conf=$this->savconf;
  105. $user=$this->savuser;
  106. $langs=$this->savlangs;
  107. $db=$this->savdb;
  108. print __METHOD__."\n";
  109. }
  110. /**
  111. * End phpunit tests
  112. *
  113. * @return void
  114. */
  115. protected function tearDown(): void
  116. {
  117. print __METHOD__."\n";
  118. }
  119. /**
  120. * testContactCreate
  121. *
  122. * @return int
  123. */
  124. public function testContactCreate()
  125. {
  126. global $conf,$user,$langs,$db;
  127. $conf=$this->savconf;
  128. $user=$this->savuser;
  129. $langs=$this->savlangs;
  130. $db=$this->savdb;
  131. $localobject=new Contact($db);
  132. $localobject->initAsSpecimen();
  133. $result=$localobject->create($user);
  134. print __METHOD__." result=".$result."\n";
  135. $this->assertLessThan($result, 0);
  136. return $result;
  137. }
  138. /**
  139. * testContactFetch
  140. *
  141. * @param int $id Id of contact
  142. * @return int
  143. * @depends testContactCreate
  144. * The depends says test is run only if previous is ok
  145. */
  146. public function testContactFetch($id)
  147. {
  148. global $conf,$user,$langs,$db;
  149. $conf=$this->savconf;
  150. $user=$this->savuser;
  151. $langs=$this->savlangs;
  152. $db=$this->savdb;
  153. $localobject=new Contact($db);
  154. $result=$localobject->fetch($id);
  155. print __METHOD__." id=".$id." result=".$result."\n";
  156. $this->assertLessThan($result, 0);
  157. return $localobject;
  158. }
  159. /**
  160. * testContactUpdate
  161. *
  162. * @param Contact $localobject Contact
  163. * @return int
  164. *
  165. * @depends testContactFetch
  166. * The depends says test is run only if previous is ok
  167. */
  168. public function testContactUpdate($localobject)
  169. {
  170. global $conf,$user,$langs,$db;
  171. $conf=$this->savconf;
  172. $user=$this->savuser;
  173. $langs=$this->savlangs;
  174. $db=$this->savdb;
  175. $localobject->oldcopy = clone $localobject;
  176. $localobject->note_private='New private note after update';
  177. $localobject->note_public='New public note after update';
  178. $localobject->lastname='New name';
  179. $localobject->firstname='New firstname';
  180. $localobject->address='New address';
  181. $localobject->zip='New zip';
  182. $localobject->town='New town';
  183. $localobject->country_id=2;
  184. //$localobject->status=0;
  185. $localobject->phone_pro='New tel pro';
  186. $localobject->phone_perso='New tel perso';
  187. $localobject->phone_mobile='New tel mobile';
  188. $localobject->fax='New fax';
  189. $localobject->email='newemail@newemail.com';
  190. $localobject->socialnetworks['jabber']='New im id';
  191. $localobject->default_lang='es_ES';
  192. $result=$localobject->update($localobject->id, $user);
  193. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  194. $this->assertLessThan($result, 0, 'Contact::update error');
  195. $result=$localobject->update_note($localobject->note_private, '_private');
  196. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  197. $this->assertLessThan($result, 0, 'Contact::update_note (private) error');
  198. $result=$localobject->update_note($localobject->note_public, '_public');
  199. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  200. $this->assertLessThan($result, 0, 'Contact::update_note (public) error');
  201. $newobject=new Contact($db);
  202. $result=$newobject->fetch($localobject->id);
  203. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  204. $this->assertLessThan($result, 0, 'Contact::fetch error');
  205. print __METHOD__." old=".$localobject->note_private." new=".$newobject->note_private."\n";
  206. $this->assertEquals($localobject->note_private, $newobject->note_private);
  207. //print __METHOD__." old=".$localobject->note_public." new=".$newobject->note_public."\n";
  208. //$this->assertEquals($localobject->note_public, $newobject->note_public);
  209. print __METHOD__." old=".$localobject->lastname." new=".$newobject->lastname."\n";
  210. $this->assertEquals($localobject->lastname, $newobject->lastname);
  211. print __METHOD__." old=".$localobject->firstname." new=".$newobject->firstname."\n";
  212. $this->assertEquals($localobject->firstname, $newobject->firstname);
  213. print __METHOD__." old=".$localobject->address." new=".$newobject->address."\n";
  214. $this->assertEquals($localobject->address, $newobject->address);
  215. print __METHOD__." old=".$localobject->zip." new=".$newobject->zip."\n";
  216. $this->assertEquals($localobject->zip, $newobject->zip);
  217. print __METHOD__." old=".$localobject->town." new=".$newobject->town."\n";
  218. $this->assertEquals($localobject->town, $newobject->town);
  219. print __METHOD__." old=".$localobject->country_id." new=".$newobject->country_id."\n";
  220. $this->assertEquals($localobject->country_id, $newobject->country_id);
  221. print __METHOD__." old=BE new=".$newobject->country_code."\n";
  222. $this->assertEquals('BE', $newobject->country_code);
  223. //print __METHOD__." old=".$localobject->status." new=".$newobject->status."\n";
  224. //$this->assertEquals($localobject->status, $newobject->status);
  225. print __METHOD__." old=".$localobject->phone_pro." new=".$newobject->phone_pro."\n";
  226. $this->assertEquals($localobject->phone_pro, $newobject->phone_pro);
  227. print __METHOD__." old=".$localobject->phone_pro." new=".$newobject->phone_pro."\n";
  228. $this->assertEquals($localobject->phone_perso, $newobject->phone_perso);
  229. print __METHOD__." old=".$localobject->phone_mobile." new=".$newobject->phone_mobile."\n";
  230. $this->assertEquals($localobject->phone_mobile, $newobject->phone_mobile);
  231. print __METHOD__." old=".$localobject->fax." new=".$newobject->fax."\n";
  232. $this->assertEquals($localobject->fax, $newobject->fax);
  233. print __METHOD__." old=".$localobject->email." new=".$newobject->email."\n";
  234. $this->assertEquals($localobject->email, $newobject->email);
  235. print __METHOD__." old=".$localobject->socialnetworks['jabber']." new=".$newobject->socialnetworks['jabber']."\n";
  236. $this->assertEquals($localobject->socialnetworks['jabber'], $newobject->socialnetworks['jabber']);
  237. print __METHOD__." old=".$localobject->default_lang." new=".$newobject->default_lang."\n";
  238. $this->assertEquals($localobject->default_lang, $newobject->default_lang);
  239. return $localobject;
  240. }
  241. /**
  242. * testContactOther
  243. *
  244. * @param Contact $localobject Contact
  245. * @return void
  246. *
  247. * @depends testContactUpdate
  248. * The depends says test is run only if previous is ok
  249. */
  250. public function testContactOther($localobject)
  251. {
  252. global $conf,$user,$langs,$db;
  253. $conf=$this->savconf;
  254. $user=$this->savuser;
  255. $langs=$this->savlangs;
  256. $db=$this->savdb;
  257. //$localobject->fetch($localobject->id);
  258. $result=$localobject->getNomUrl(1);
  259. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  260. $this->assertNotEquals($result, '');
  261. $result=$localobject->getFullAddress(1);
  262. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  263. $this->assertStringContainsString("New address\nNew zip New town\nBelgium", $result);
  264. $localobject->info($localobject->id);
  265. print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
  266. $this->assertNotEquals($localobject->date_creation, '');
  267. return $localobject->id;
  268. }
  269. /**
  270. * testContactDelete
  271. *
  272. * @param int $id Id of contact
  273. * @return void
  274. *
  275. * @depends testContactOther
  276. * The depends says test is run only if previous is ok
  277. */
  278. public function testContactDelete($id)
  279. {
  280. global $conf,$user,$langs,$db;
  281. $conf=$this->savconf;
  282. $user=$this->savuser;
  283. $langs=$this->savlangs;
  284. $db=$this->savdb;
  285. $localobject=new Contact($db);
  286. $result=$localobject->fetch($id);
  287. $result=$localobject->delete($user);
  288. print __METHOD__." id=".$id." result=".$result."\n";
  289. $this->assertLessThan($result, 0);
  290. return $result;
  291. }
  292. /**
  293. * testContactGetFullAddress
  294. *
  295. * @return int $id Id of company
  296. */
  297. public function testContactGetFullAddress()
  298. {
  299. global $conf,$user,$langs,$db;
  300. $conf=$this->savconf;
  301. $user=$this->savuser;
  302. $langs=$this->savlangs;
  303. $db=$this->savdb;
  304. $localobjectadd=new Contact($db);
  305. $localobjectadd->initAsSpecimen();
  306. // France
  307. unset($localobjectadd->country_code);
  308. $localobjectadd->country_id=1;
  309. $localobjectadd->name='New name';
  310. $localobjectadd->address='New address';
  311. $localobjectadd->zip='New zip';
  312. $localobjectadd->town='New town';
  313. $result=$localobjectadd->getFullAddress(1);
  314. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  315. $this->assertStringContainsString("New address\nNew zip New town\nFrance", $result);
  316. // Belgium
  317. unset($localobjectadd->country_code);
  318. $localobjectadd->country_id=2;
  319. $localobjectadd->name='New name';
  320. $localobjectadd->address='New address';
  321. $localobjectadd->zip='New zip';
  322. $localobjectadd->town='New town';
  323. $result=$localobjectadd->getFullAddress(1);
  324. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  325. $this->assertStringContainsString("New address\nNew zip New town\nBelgium", $result);
  326. // Switzerland
  327. unset($localobjectadd->country_code);
  328. $localobjectadd->country_id=6;
  329. $localobjectadd->name='New name';
  330. $localobjectadd->address='New address';
  331. $localobjectadd->zip='New zip';
  332. $localobjectadd->town='New town';
  333. $result=$localobjectadd->getFullAddress(1);
  334. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  335. $this->assertStringContainsString("New address\nNew zip New town\nSwitzerland", $result);
  336. // USA
  337. unset($localobjectadd->country_code);
  338. $localobjectadd->country_id=11;
  339. $localobjectadd->name='New name';
  340. $localobjectadd->address='New address';
  341. $localobjectadd->zip='New zip';
  342. $localobjectadd->town='New town';
  343. $result=$localobjectadd->getFullAddress(1);
  344. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  345. $this->assertStringContainsString("New address\nNew town, New zip\nUnited States", $result);
  346. return $localobjectadd->id;
  347. }
  348. }