SocieteTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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/SocieteTest.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/societe/class/societe.class.php';
  29. $langs->load("dict");
  30. if (empty($user->id)) {
  31. print "Load permissions for admin user nb 1\n";
  32. $user->fetch(1);
  33. $user->getrights();
  34. }
  35. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  36. /**
  37. * Class for PHPUnit tests
  38. *
  39. * @backupGlobals disabled
  40. * @backupStaticAttributes enabled
  41. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  42. */
  43. class SocieteTest extends PHPUnit_Framework_TestCase
  44. {
  45. protected $savconf;
  46. protected $savuser;
  47. protected $savlangs;
  48. protected $savdb;
  49. /**
  50. * Constructor
  51. * We save global variables into local variables
  52. *
  53. * @return SocieteTest
  54. */
  55. function __construct()
  56. {
  57. parent::__construct();
  58. //$this->sharedFixture
  59. global $conf,$user,$langs,$db;
  60. $this->savconf=$conf;
  61. $this->savuser=$user;
  62. $this->savlangs=$langs;
  63. $this->savdb=$db;
  64. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  65. //print " - db ".$db->db;
  66. print "\n";
  67. }
  68. // Static methods
  69. public static function setUpBeforeClass()
  70. {
  71. global $conf,$user,$langs,$db;
  72. if ($conf->global->SOCIETE_CODECLIENT_ADDON != 'mod_codeclient_monkey') { print "\n".__METHOD__." third party ref checker must be setup to 'mod_codeclient_monkey' not to '".$conf->global->SOCIETE_CODECLIENT_ADDON."'.\n"; die(); }
  73. if (! empty($conf->global->MAIN_DISABLEPROFIDRULES)) { print "\n".__METHOD__." constant MAIN_DISABLEPROFIDRULES must be empty (if a module set it, disable module).\n"; die(); }
  74. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  75. print __METHOD__."\n";
  76. }
  77. // tear down after class
  78. public static function tearDownAfterClass()
  79. {
  80. global $conf,$user,$langs,$db;
  81. $db->rollback();
  82. print __METHOD__."\n";
  83. }
  84. /**
  85. * Init phpunit tests
  86. *
  87. * @return void
  88. */
  89. protected function setUp()
  90. {
  91. global $conf,$user,$langs,$db;
  92. $conf=$this->savconf;
  93. $user=$this->savuser;
  94. $langs=$this->savlangs;
  95. $db=$this->savdb;
  96. print __METHOD__."\n";
  97. }
  98. /**
  99. * End phpunit tests
  100. *
  101. * @return void
  102. */
  103. protected function tearDown()
  104. {
  105. print __METHOD__."\n";
  106. }
  107. /**
  108. * testSocieteCreate
  109. *
  110. * @return int
  111. */
  112. public function testSocieteCreate()
  113. {
  114. global $conf,$user,$langs,$db;
  115. $conf=$this->savconf;
  116. $user=$this->savuser;
  117. $langs=$this->savlangs;
  118. $db=$this->savdb;
  119. $localobject=new Societe($this->savdb);
  120. $localobject->initAsSpecimen();
  121. $result=$localobject->create($user);
  122. print __METHOD__." result=".$result."\n";
  123. $this->assertLessThanOrEqual($result, 0);
  124. return $result;
  125. }
  126. /**
  127. * testSocieteFetch
  128. *
  129. * @param int $id Company id
  130. * @return Societe $localobject Company
  131. *
  132. * @depends testSocieteCreate
  133. * The depends says test is run only if previous is ok
  134. */
  135. public function testSocieteFetch($id)
  136. {
  137. global $conf,$user,$langs,$db;
  138. $conf=$this->savconf;
  139. $user=$this->savuser;
  140. $langs=$this->savlangs;
  141. $db=$this->savdb;
  142. $localobject=new Societe($this->savdb);
  143. $result=$localobject->fetch($id);
  144. print __METHOD__." id=".$id." result=".$result."\n";
  145. $this->assertLessThan($result, 0);
  146. $result=$localobject->verify();
  147. print __METHOD__." id=".$id." result=".$result."\n";
  148. $this->assertEquals($result, 0);
  149. return $localobject;
  150. }
  151. /**
  152. * testSocieteUpdate
  153. *
  154. * @param Societe $localobject Company
  155. * @return Societe $localobject Company
  156. *
  157. * @depends testSocieteFetch
  158. * The depends says test is run only if previous is ok
  159. */
  160. public function testSocieteUpdate($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->note_private='New private note after update';
  168. $localobject->note_public='New public note after update';
  169. $localobject->name='New name';
  170. $localobject->address='New address';
  171. $localobject->zip='New zip';
  172. $localobject->town='New town';
  173. $localobject->country_id=2;
  174. $localobject->status=0;
  175. $localobject->phone='New tel';
  176. $localobject->fax='New fax';
  177. $localobject->email='newemail@newemail.com';
  178. $localobject->url='New url';
  179. $localobject->idprof1='new idprof1';
  180. $localobject->idprof2='new idprof2';
  181. $localobject->idprof3='new idprof3';
  182. $localobject->idprof4='new idprof4';
  183. $result=$localobject->update($localobject->id,$user);
  184. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  185. $this->assertLessThan($result, 0);
  186. $result=$localobject->update_note($localobject->note_private,'_private');
  187. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  188. $this->assertLessThan($result, 0, 'Holiday::update_note (private) error');
  189. $result=$localobject->update_note($localobject->note_public, '_public');
  190. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  191. $this->assertLessThan($result, 0, 'Holiday::update_note (public) error');
  192. $newobject=new Societe($this->savdb);
  193. $result=$newobject->fetch($localobject->id);
  194. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  195. $this->assertLessThan($result, 0);
  196. $this->assertEquals($localobject->note_private, $newobject->note_private);
  197. //$this->assertEquals($localobject->note_public, $newobject->note_public);
  198. $this->assertEquals($localobject->name, $newobject->name);
  199. $this->assertEquals($localobject->address, $newobject->address);
  200. $this->assertEquals($localobject->zip, $newobject->zip);
  201. $this->assertEquals($localobject->town, $newobject->town);
  202. $this->assertEquals($localobject->country_id, $newobject->country_id);
  203. $this->assertEquals('BE', $newobject->country_code);
  204. $this->assertEquals($localobject->status, $newobject->status);
  205. $this->assertEquals($localobject->phone, $newobject->phone);
  206. $this->assertEquals($localobject->fax, $newobject->fax);
  207. $this->assertEquals($localobject->email, $newobject->email);
  208. $this->assertEquals($localobject->url, $newobject->url);
  209. $this->assertEquals($localobject->idprof1, $newobject->idprof1);
  210. $this->assertEquals($localobject->idprof2, $newobject->idprof2);
  211. $this->assertEquals($localobject->idprof3, $newobject->idprof3);
  212. $this->assertEquals($localobject->idprof4, $newobject->idprof4);
  213. return $localobject;
  214. }
  215. /**
  216. * testIdProfCheck
  217. *
  218. * @param Societe $localobject Company
  219. * @return Societe $localobject Company
  220. *
  221. * @depends testSocieteUpdate
  222. * The depends says test is run only if previous is ok
  223. */
  224. public function testIdProfCheck($localobject)
  225. {
  226. // OK FR
  227. $localobject->country_code='FR';
  228. $localobject->idprof1=493861496;
  229. $localobject->idprof2=49386149600021;
  230. $result=$localobject->id_prof_check(1,$localobject); // Must be > 0
  231. print __METHOD__." OK FR idprof1 result=".$result."\n";
  232. $this->assertGreaterThanOrEqual(1, $result);
  233. $result=$localobject->id_prof_check(2,$localobject); // Must be > 0
  234. print __METHOD__." OK FR idprof2 result=".$result."\n";
  235. $this->assertGreaterThanOrEqual(1, $result);
  236. // KO FR
  237. $localobject->country_code='FR';
  238. $localobject->idprof1='id1ko';
  239. $localobject->idprof2='id2ko';
  240. $result=$localobject->id_prof_check(1,$localobject); // Must be <= 0
  241. print __METHOD__." KO FR idprof1 result=".$result."\n";
  242. $this->assertLessThan(1, $result);
  243. $result=$localobject->id_prof_check(2,$localobject); // Must be <= 0
  244. print __METHOD__." KO FR idprof2 result=".$result."\n";
  245. $this->assertLessThan(1, $result);
  246. // KO ES
  247. $localobject->country_code='ES';
  248. $localobject->idprof1='id1ko';
  249. $result=$localobject->id_prof_check(1,$localobject); // Must be <= 0
  250. print __METHOD__." KO ES idprof1 result=".$result."\n";
  251. $this->assertLessThan(1, $result);
  252. // OK AR
  253. $localobject->country_code='AR';
  254. $localobject->idprof1='id1ko';
  255. $localobject->idprof2='id2ko';
  256. $result=$localobject->id_prof_check(1,$localobject); // Must be > 0
  257. print __METHOD__." OK AR idprof1 result=".$result."\n";
  258. $this->assertGreaterThanOrEqual(0, $result);
  259. $result=$localobject->id_prof_check(2,$localobject); // Must be > 0
  260. print __METHOD__." OK AR idprof2 result=".$result."\n";
  261. $this->assertGreaterThanOrEqual(1, $result);
  262. return $localobject;
  263. }
  264. /**
  265. * testSocieteOther
  266. *
  267. * @param Societe $localobject Company
  268. * @return int $id Id of company
  269. *
  270. * @depends testIdProfCheck
  271. * The depends says test is run only if previous is ok
  272. */
  273. public function testSocieteOther($localobject)
  274. {
  275. global $conf,$user,$langs,$db;
  276. $conf=$this->savconf;
  277. $user=$this->savuser;
  278. $langs=$this->savlangs;
  279. $db=$this->savdb;
  280. $result=$localobject->set_as_client();
  281. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  282. $this->assertLessThan($result, 0);
  283. $result=$localobject->set_price_level(1,$user);
  284. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  285. $this->assertLessThan($result, 0);
  286. $result=$localobject->set_remise_client(10,'Gift',$user);
  287. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  288. $this->assertLessThan($result, 0);
  289. $result=$localobject->getNomUrl(1);
  290. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  291. $this->assertNotEquals($result, '');
  292. $result=$localobject->isInEEC();
  293. print __METHOD__." id=".$localobject->id." country_code=".$localobject->country_code." result=".$result."\n";
  294. $this->assertTrue(true, $result);
  295. $localobject->info($localobject->id);
  296. print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
  297. $this->assertNotEquals($localobject->date_creation, '');
  298. return $localobject->id;
  299. }
  300. /**
  301. * testSocieteDelete
  302. *
  303. * @param int $id Id of company
  304. * @return int
  305. *
  306. * @depends testSocieteOther
  307. * The depends says test is run only if previous is ok
  308. */
  309. public function testSocieteDelete($id)
  310. {
  311. global $conf,$user,$langs,$db;
  312. $conf=$this->savconf;
  313. $user=$this->savuser;
  314. $langs=$this->savlangs;
  315. $db=$this->savdb;
  316. $localobject=new Societe($this->savdb);
  317. $result=$localobject->fetch($id);
  318. $result=$localobject->delete($id, $user);
  319. print __METHOD__." id=".$id." result=".$result."\n";
  320. $this->assertLessThan($result, 0);
  321. return $result;
  322. }
  323. /**
  324. * testSocieteStatic
  325. *
  326. * @return void
  327. */
  328. public function testSocieteStatic()
  329. {
  330. global $conf,$user,$langs,$db;
  331. $conf=$this->savconf;
  332. $user=$this->savuser;
  333. $langs=$this->savlangs;
  334. $db=$this->savdb;
  335. $localobject=new Societe($db);
  336. return;
  337. }
  338. /**
  339. * testSocieteGetFullAddress
  340. *
  341. * @return int $id Id of company
  342. */
  343. public function testSocieteGetFullAddress()
  344. {
  345. global $conf,$user,$langs,$db;
  346. $conf=$this->savconf;
  347. $user=$this->savuser;
  348. $langs=$this->savlangs;
  349. $db=$this->savdb;
  350. $localobjectadd=new Societe($db);
  351. $localobjectadd->initAsSpecimen();
  352. // France
  353. unset($localobjectadd->country_code);
  354. $localobjectadd->country_id=1;
  355. $localobjectadd->name='New name';
  356. $localobjectadd->address='New address';
  357. $localobjectadd->zip='New zip';
  358. $localobjectadd->town='New town';
  359. $result=$localobjectadd->getFullAddress(1);
  360. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  361. $this->assertContains("New address\nNew zip New town\nFrance", $result);
  362. // Belgium
  363. unset($localobjectadd->country_code);
  364. $localobjectadd->country_id=2;
  365. $localobjectadd->name='New name';
  366. $localobjectadd->address='New address';
  367. $localobjectadd->zip='New zip';
  368. $localobjectadd->town='New town';
  369. $result=$localobjectadd->getFullAddress(1);
  370. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  371. $this->assertContains("New address\nNew zip New town\nBelgium", $result);
  372. // Switzerland
  373. unset($localobjectadd->country_code);
  374. $localobjectadd->country_id=6;
  375. $localobjectadd->name='New name';
  376. $localobjectadd->address='New address';
  377. $localobjectadd->zip='New zip';
  378. $localobjectadd->town='New town';
  379. $result=$localobjectadd->getFullAddress(1);
  380. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  381. $this->assertContains("New address\nNew zip New town\nSwitzerland", $result);
  382. // USA
  383. unset($localobjectadd->country_code);
  384. $localobjectadd->country_id=11;
  385. $localobjectadd->name='New name';
  386. $localobjectadd->address='New address';
  387. $localobjectadd->zip='New zip';
  388. $localobjectadd->town='New town';
  389. $localobjectadd->state='New state';
  390. $result=$localobjectadd->getFullAddress(1);
  391. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  392. $this->assertContains("New address\nNew town, New state, New zip\nUnited States", $result);
  393. return $localobjectadd->id;
  394. }
  395. }