ContactTest.php 13 KB

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