SocieteTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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/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. public 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. /**
  69. * setUpBeforeClass
  70. *
  71. * @return void
  72. */
  73. public static function setUpBeforeClass()
  74. {
  75. global $conf,$user,$langs,$db;
  76. if ($conf->global->SOCIETE_CODECLIENT_ADDON != 'mod_codeclient_monkey') {
  77. print "\n".__METHOD__." third party ref checker must be setup to 'mod_codeclient_monkey' not to '".$conf->global->SOCIETE_CODECLIENT_ADDON."'.\n"; die(1);
  78. }
  79. if (! empty($conf->global->MAIN_DISABLEPROFIDRULES)) {
  80. print "\n".__METHOD__." constant MAIN_DISABLEPROFIDRULES must be empty (if a module set it, disable module).\n"; die(1);
  81. }
  82. if ($langs->defaultlang != 'en_US') {
  83. print "\n".__METHOD__." default language of company must be set to autodetect.\n"; die(1);
  84. }
  85. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  86. print __METHOD__."\n";
  87. }
  88. /**
  89. * tearDownAfterClass
  90. *
  91. * @return void
  92. */
  93. public static function tearDownAfterClass()
  94. {
  95. global $conf,$user,$langs,$db;
  96. $db->rollback();
  97. print __METHOD__."\n";
  98. }
  99. /**
  100. * Init phpunit tests
  101. *
  102. * @return void
  103. */
  104. protected function setUp()
  105. {
  106. global $conf,$user,$langs,$db;
  107. $conf=$this->savconf;
  108. $user=$this->savuser;
  109. $langs=$this->savlangs;
  110. $db=$this->savdb;
  111. print __METHOD__."\n";
  112. }
  113. /**
  114. * End phpunit tests
  115. *
  116. * @return void
  117. */
  118. protected function tearDown()
  119. {
  120. print __METHOD__."\n";
  121. }
  122. /**
  123. * testSocieteCreate
  124. *
  125. * @return int
  126. */
  127. public function testSocieteCreate()
  128. {
  129. global $conf,$user,$langs,$db;
  130. $conf=$this->savconf;
  131. $user=$this->savuser;
  132. $langs=$this->savlangs;
  133. $db=$this->savdb;
  134. $localobject=new Societe($this->savdb);
  135. $localobject->initAsSpecimen();
  136. $result=$localobject->create($user);
  137. print __METHOD__." result=".$result."\n";
  138. $this->assertLessThanOrEqual($result, 0);
  139. return $result;
  140. }
  141. /**
  142. * testSocieteFetch
  143. *
  144. * @param int $id Company id
  145. * @return Societe $localobject Company
  146. *
  147. * @depends testSocieteCreate
  148. * The depends says test is run only if previous is ok
  149. */
  150. public function testSocieteFetch($id)
  151. {
  152. global $conf,$user,$langs,$db;
  153. $conf=$this->savconf;
  154. $user=$this->savuser;
  155. $langs=$this->savlangs;
  156. $db=$this->savdb;
  157. $localobject=new Societe($this->savdb);
  158. $result=$localobject->fetch($id);
  159. print __METHOD__." id=".$id." result=".$result."\n";
  160. $this->assertLessThan($result, 0);
  161. $result=$localobject->verify();
  162. print __METHOD__." id=".$id." result=".$result."\n";
  163. $this->assertEquals($result, 0);
  164. return $localobject;
  165. }
  166. /**
  167. * testSocieteUpdate
  168. *
  169. * @param Societe $localobject Company
  170. * @return Societe $localobject Company
  171. *
  172. * @depends testSocieteFetch
  173. * The depends says test is run only if previous is ok
  174. */
  175. public function testSocieteUpdate($localobject)
  176. {
  177. global $conf,$user,$langs,$db;
  178. $conf=$this->savconf;
  179. $user=$this->savuser;
  180. $langs=$this->savlangs;
  181. $db=$this->savdb;
  182. $localobject->note_private='New private note after update';
  183. $localobject->note_public='New public note after update';
  184. $localobject->name='New name';
  185. $localobject->address='New address';
  186. $localobject->zip='New zip';
  187. $localobject->town='New town';
  188. $localobject->country_id=2;
  189. $localobject->status=0;
  190. $localobject->phone='New tel';
  191. $localobject->fax='New fax';
  192. $localobject->email='newemail@newemail.com';
  193. $localobject->url='New url';
  194. $localobject->idprof1='new idprof1';
  195. $localobject->idprof2='new idprof2';
  196. $localobject->idprof3='new idprof3';
  197. $localobject->idprof4='new idprof4';
  198. $result=$localobject->update($localobject->id, $user);
  199. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  200. $this->assertLessThan($result, 0);
  201. $result=$localobject->update_note($localobject->note_private, '_private');
  202. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  203. $this->assertLessThan($result, 0, 'Holiday::update_note (private) error');
  204. $result=$localobject->update_note($localobject->note_public, '_public');
  205. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  206. $this->assertLessThan($result, 0, 'Holiday::update_note (public) error');
  207. $newobject=new Societe($this->savdb);
  208. $result=$newobject->fetch($localobject->id);
  209. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  210. $this->assertLessThan($result, 0);
  211. $this->assertEquals($localobject->note_private, $newobject->note_private);
  212. //$this->assertEquals($localobject->note_public, $newobject->note_public);
  213. $this->assertEquals($localobject->name, $newobject->name);
  214. $this->assertEquals($localobject->address, $newobject->address);
  215. $this->assertEquals($localobject->zip, $newobject->zip);
  216. $this->assertEquals($localobject->town, $newobject->town);
  217. $this->assertEquals($localobject->country_id, $newobject->country_id);
  218. $this->assertEquals('BE', $newobject->country_code);
  219. $this->assertEquals($localobject->status, $newobject->status);
  220. $this->assertEquals($localobject->phone, $newobject->phone);
  221. $this->assertEquals($localobject->fax, $newobject->fax);
  222. $this->assertEquals($localobject->email, $newobject->email);
  223. $this->assertEquals($localobject->url, $newobject->url);
  224. $this->assertEquals($localobject->idprof1, $newobject->idprof1);
  225. $this->assertEquals($localobject->idprof2, $newobject->idprof2);
  226. $this->assertEquals($localobject->idprof3, $newobject->idprof3);
  227. $this->assertEquals($localobject->idprof4, $newobject->idprof4);
  228. return $localobject;
  229. }
  230. /**
  231. * testIdProfCheck
  232. *
  233. * @param Societe $localobject Company
  234. * @return Societe $localobject Company
  235. *
  236. * @depends testSocieteUpdate
  237. * The depends says test is run only if previous is ok
  238. */
  239. public function testIdProfCheck($localobject)
  240. {
  241. // OK FR
  242. $localobject->country_code='FR';
  243. $localobject->idprof1=493861496;
  244. $localobject->idprof2=49386149600021;
  245. $result=$localobject->id_prof_check(1, $localobject); // Must be > 0
  246. print __METHOD__." OK FR idprof1 result=".$result."\n";
  247. $this->assertGreaterThanOrEqual(1, $result);
  248. $result=$localobject->id_prof_check(2, $localobject); // Must be > 0
  249. print __METHOD__." OK FR idprof2 result=".$result."\n";
  250. $this->assertGreaterThanOrEqual(1, $result);
  251. // KO FR
  252. $localobject->country_code='FR';
  253. $localobject->idprof1='id1ko';
  254. $localobject->idprof2='id2ko';
  255. $result=$localobject->id_prof_check(1, $localobject); // Must be <= 0
  256. print __METHOD__." KO FR idprof1 result=".$result."\n";
  257. $this->assertLessThan(1, $result);
  258. $result=$localobject->id_prof_check(2, $localobject); // Must be <= 0
  259. print __METHOD__." KO FR idprof2 result=".$result."\n";
  260. $this->assertLessThan(1, $result);
  261. // KO ES
  262. $localobject->country_code='ES';
  263. $localobject->idprof1='id1ko';
  264. $result=$localobject->id_prof_check(1, $localobject); // Must be <= 0
  265. print __METHOD__." KO ES idprof1 result=".$result."\n";
  266. $this->assertLessThan(1, $result);
  267. // OK AR
  268. $localobject->country_code='AR';
  269. $localobject->idprof1='id1ko';
  270. $localobject->idprof2='id2ko';
  271. $result=$localobject->id_prof_check(1, $localobject); // Must be > 0
  272. print __METHOD__." OK AR idprof1 result=".$result."\n";
  273. $this->assertGreaterThanOrEqual(0, $result);
  274. $result=$localobject->id_prof_check(2, $localobject); // Must be > 0
  275. print __METHOD__." OK AR idprof2 result=".$result."\n";
  276. $this->assertGreaterThanOrEqual(1, $result);
  277. return $localobject;
  278. }
  279. /**
  280. * testSocieteOther
  281. *
  282. * @param Societe $localobject Company
  283. * @return int $id Id of company
  284. *
  285. * @depends testIdProfCheck
  286. * The depends says test is run only if previous is ok
  287. */
  288. public function testSocieteOther($localobject)
  289. {
  290. global $conf,$user,$langs,$db;
  291. $conf=$this->savconf;
  292. $user=$this->savuser;
  293. $langs=$this->savlangs;
  294. $db=$this->savdb;
  295. $result=$localobject->set_as_client();
  296. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  297. $this->assertLessThan($result, 0);
  298. $result=$localobject->setPriceLevel(1, $user);
  299. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  300. $this->assertLessThan($result, 0);
  301. $result=$localobject->set_remise_client(10, 'Gift', $user);
  302. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  303. $this->assertLessThan($result, 0);
  304. $result=$localobject->getNomUrl(1);
  305. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  306. $this->assertNotEquals($result, '');
  307. $localobject->country_code = 'FR';
  308. $result=$localobject->isInEEC();
  309. print __METHOD__." id=".$localobject->id." country_code=".$localobject->country_code." result=".$result."\n";
  310. $this->assertTrue($result);
  311. $localobject->country_code = 'US';
  312. $result=$localobject->isInEEC();
  313. print __METHOD__." id=".$localobject->id." country_code=".$localobject->country_code." result=".$result."\n";
  314. $this->assertFalse($result);
  315. /*$localobject->country_code = 'GB';
  316. $result=$localobject->isInEEC();
  317. print __METHOD__." id=".$localobject->id." country_code=".$localobject->country_code." result=".$result."\n";
  318. $this->assertTrue($result);
  319. */
  320. $localobject->info($localobject->id);
  321. print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
  322. $this->assertNotEquals($localobject->date_creation, '');
  323. return $localobject->id;
  324. }
  325. /**
  326. * testGetOutstandingBills
  327. *
  328. * @param int $id Id of company
  329. * @return int
  330. *
  331. * @depends testSocieteOther
  332. * The depends says test is run only if previous is ok
  333. */
  334. public function testGetOutstandingBills($id)
  335. {
  336. global $conf,$user,$langs,$db;
  337. $conf=$this->savconf;
  338. $user=$this->savuser;
  339. $langs=$this->savlangs;
  340. $db=$this->savdb;
  341. $localobject=new Societe($this->savdb);
  342. $localobject->fetch($id);
  343. $result=$localobject->getOutstandingBills();
  344. print __METHOD__." id=".$id." result=".var_export($result, true)."\n";
  345. $this->assertTrue(array_key_exists('opened', $result), 'Result of getOutstandingBills failed');
  346. return $id;
  347. }
  348. /**
  349. * testSocieteDelete
  350. *
  351. * @param int $id Id of company
  352. * @return int
  353. *
  354. * @depends testGetOutstandingBills
  355. * The depends says test is run only if previous is ok
  356. */
  357. public function testSocieteDelete($id)
  358. {
  359. global $conf,$user,$langs,$db;
  360. $conf=$this->savconf;
  361. $user=$this->savuser;
  362. $langs=$this->savlangs;
  363. $db=$this->savdb;
  364. $localobject=new Societe($this->savdb);
  365. $result=$localobject->fetch($id);
  366. $result=$localobject->delete($id, $user);
  367. print __METHOD__." id=".$id." result=".$result."\n";
  368. $this->assertLessThan($result, 0);
  369. return $result;
  370. }
  371. /**
  372. * testSocieteGetFullAddress
  373. *
  374. * @return int $id Id of company
  375. */
  376. public function testSocieteGetFullAddress()
  377. {
  378. global $conf,$user,$langs,$db;
  379. $conf=$this->savconf;
  380. $user=$this->savuser;
  381. $langs=$this->savlangs;
  382. $db=$this->savdb;
  383. $localobjectadd=new Societe($db);
  384. $localobjectadd->initAsSpecimen();
  385. // France
  386. unset($localobjectadd->country_code);
  387. $localobjectadd->country_id=1;
  388. $localobjectadd->name='New name';
  389. $localobjectadd->address='New address';
  390. $localobjectadd->zip='New zip';
  391. $localobjectadd->town='New town';
  392. $result=$localobjectadd->getFullAddress(1);
  393. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  394. $this->assertContains("New address\nNew zip New town\nFrance", $result);
  395. // Belgium
  396. unset($localobjectadd->country_code);
  397. $localobjectadd->country_id=2;
  398. $localobjectadd->name='New name';
  399. $localobjectadd->address='New address';
  400. $localobjectadd->zip='New zip';
  401. $localobjectadd->town='New town';
  402. $result=$localobjectadd->getFullAddress(1);
  403. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  404. $this->assertContains("New address\nNew zip New town\nBelgium", $result);
  405. // Switzerland
  406. unset($localobjectadd->country_code);
  407. $localobjectadd->country_id=6;
  408. $localobjectadd->name='New name';
  409. $localobjectadd->address='New address';
  410. $localobjectadd->zip='New zip';
  411. $localobjectadd->town='New town';
  412. $result=$localobjectadd->getFullAddress(1);
  413. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  414. $this->assertContains("New address\nNew zip New town\nSwitzerland", $result);
  415. // USA
  416. unset($localobjectadd->country_code);
  417. $localobjectadd->country_id=11;
  418. $localobjectadd->name='New name';
  419. $localobjectadd->address='New address';
  420. $localobjectadd->zip='New zip';
  421. $localobjectadd->town='New town';
  422. $localobjectadd->state='New state';
  423. $result=$localobjectadd->getFullAddress(1);
  424. print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
  425. $this->assertContains("New address\nNew town, New state, New zip\nUnited States", $result);
  426. return $localobjectadd->id;
  427. }
  428. }