ContactTest.php 12 KB

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