123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590 |
- <?php
- /* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
- * Copyright (C) 2013 Marcos García <marcosgdf@gmail.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- * or see https://www.gnu.org/
- */
- /**
- * \file test/phpunit/AdherentTest.php
- * \ingroup test
- * \brief PHPUnit test
- * \remarks To run this script as CLI: phpunit filename.php
- */
- global $conf,$user,$langs,$db;
- //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
- //require_once 'PHPUnit/Autoload.php';
- require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
- require_once dirname(__FILE__).'/../../htdocs/adherents/class/adherent.class.php';
- require_once dirname(__FILE__).'/../../htdocs/adherents/class/adherent_type.class.php';
- if (empty($user->id)) {
- print "Load permissions for admin user nb 1\n";
- $user->fetch(1);
- $user->getrights();
- }
- $conf->global->MAIN_DISABLE_ALL_MAILS=1;
- /**
- * Class for PHPUnit tests
- *
- * @backupGlobals disabled
- * @backupStaticAttributes enabled
- * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
- */
- class AdherentTest extends PHPUnit\Framework\TestCase
- {
- protected $savconf;
- protected $savuser;
- protected $savlangs;
- protected $savdb;
- /**
- * Constructor
- * We save global variables into local variables
- *
- * @return AdherentTest
- */
- public function __construct()
- {
- parent::__construct();
- //$this->sharedFixture
- global $conf,$user,$langs,$db;
- $this->savconf=$conf;
- $this->savuser=$user;
- $this->savlangs=$langs;
- $this->savdb=$db;
- print __METHOD__." db->type=".$db->type." user->id=".$user->id;
- //print " - db ".$db->db;
- print "\n";
- }
- /**
- * setUpBeforeClass
- *
- * @return void
- */
- public static function setUpBeforeClass()
- {
- global $conf,$user,$langs,$db;
- $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
- if (! empty($conf->global->MAIN_FIRSTNAME_NAME_POSITION)) {
- print "\n".__METHOD__." Company must be setup to have name-firstname in order 'Firstname Lastname'\n";
- die(1);
- }
- if (! empty($conf->global->MAIN_MODULE_LDAP)) {
- print "\n".__METHOD__." module LDAP must be disabled.\n"; die(1);
- }
- if (! empty($conf->global->MAIN_MODULE_MAILMANSPIP)) {
- print "\n".__METHOD__." module MailmanSpip must be disabled.\n"; die(1);
- }
- print __METHOD__."\n";
- }
- /**
- * tearDownAfterClass
- *
- * @return void
- */
- public static function tearDownAfterClass()
- {
- global $conf,$user,$langs,$db;
- $db->rollback();
- print __METHOD__."\n";
- }
- /**
- * Init phpunit tests
- *
- * @return void
- */
- protected function setUp()
- {
- global $conf,$user,$langs,$db;
- $conf=$this->savconf;
- $user=$this->savuser;
- $langs=$this->savlangs;
- $db=$this->savdb;
- print __METHOD__."\n";
- }
- /**
- * End phpunit tests
- *
- * @return void
- */
- protected function tearDown()
- {
- print __METHOD__."\n";
- }
- /**
- * testAdherentTypeCreate
- *
- * @return void
- */
- public function testAdherentTypeCreate()
- {
- global $conf,$user,$langs,$db;
- $conf=$this->savconf;
- $user=$this->savuser;
- $langs=$this->savlangs;
- $db=$this->savdb;
- $localobject=new AdherentType($this->savdb);
- $localobject->statut=1;
- $localobject->label='Adherent type test';
- $localobject->subscription=1;
- $localobject->amount=0;
- $localobject->vote=1;
- $localobject->company='Old company label';
- $result=$localobject->create($user);
- print __METHOD__." result=".$result."\n";
- $this->assertLessThan($result, 0);
- return $localobject->id;
- }
- /**
- * testAdherentCreate
- *
- * @param int $fk_adherent_type Id type of member
- * @return int
- *
- * @depends testAdherentTypeCreate
- * The depends says test is run only if previous is ok
- */
- public function testAdherentCreate($fk_adherent_type)
- {
- global $conf,$user,$langs,$db;
- $conf=$this->savconf;
- $user=$this->savuser;
- $langs=$this->savlangs;
- $db=$this->savdb;
- $localobject=new Adherent($this->savdb);
- $localobject->initAsSpecimen();
- $localobject->typeid=$fk_adherent_type;
- $result=$localobject->create($user);
- print __METHOD__." result=".$result."\n";
- if ($result < 0) {
- print $localobject->error;
- }
- $this->assertLessThan($result, 0);
- return $result;
- }
- /**
- * testAdherentFetch
- *
- * @param int $id Id of object to fetch
- * @return object Fetched object
- *
- * @depends testAdherentCreate
- * The depends says test is run only if previous is ok
- */
- public function testAdherentFetch($id)
- {
- global $conf,$user,$langs,$db;
- $conf=$this->savconf;
- $user=$this->savuser;
- $langs=$this->savlangs;
- $db=$this->savdb;
- $localobject=new Adherent($this->savdb);
- $result=$localobject->fetch($id);
- print __METHOD__." id=".$id." result=".$result."\n";
- $this->assertLessThan($result, 0);
- return $localobject;
- }
- /**
- * testAdherentFetchLogin
- *
- * @param Adherent $localobject Member instance
- * @return Adherent
- *
- * @depends testAdherentFetch
- * The depends says test is run only if previous is ok
- */
- public function testAdherentFetchLogin(Adherent $localobject)
- {
- global $conf,$user,$langs,$db;
- $conf=$this->savconf;
- $user=$this->savuser;
- $langs=$this->savlangs;
- $db=$this->savdb;
- $newobject = new Adherent($this->savdb);
- $result = $newobject->fetch_login($localobject->login);
- $this->assertEquals($newobject, $localobject);
- return $localobject;
- }
- /**
- * testAdherentUpdate
- *
- * @param Adherent $localobject Member instance
- * @return Adherent
- *
- * @depends testAdherentFetchLogin
- * The depends says test is run only if previous is ok
- */
- public function testAdherentUpdate(Adherent $localobject)
- {
- global $conf,$user,$langs,$db;
- $conf=$this->savconf;
- $user=$this->savuser;
- $langs=$this->savlangs;
- $db=$this->savdb;
- $timestamp = dol_now();
- $localobject->civility_id = 0;
- $localobject->login='newlogin';
- $localobject->company='New company label';
- $localobject->note_public='New note public after update';
- $localobject->note_private='New note private after update';
- $localobject->lastname='New name';
- $localobject->firstname='New firstname';
- $localobject->gender='man';
- $localobject->address='New address';
- $localobject->zip='New zip';
- $localobject->town='New town';
- $localobject->country_id=2;
- $localobject->statut=0;
- $localobject->morphy=0;
- $localobject->phone='New tel pro';
- $localobject->phone_perso='New tel perso';
- $localobject->phone_mobile='New tel mobile';
- $localobject->email='newemail@newemail.com';
- $localobject->birth=$timestamp;
- $result=$localobject->update($user);
- print __METHOD__." id=".$localobject->id." result=".$result."\n";
- $this->assertLessThan($result, 0);
- $result=$localobject->update_note($localobject->note_private, '_private');
- print __METHOD__." id=".$localobject->id." result=".$result."\n";
- $this->assertLessThan($result, 0);
- $result=$localobject->update_note($localobject->note_public, '_public');
- print __METHOD__." id=".$localobject->id." result=".$result."\n";
- $this->assertLessThan($result, 0);
- $newobject=new Adherent($this->savdb);
- $result=$newobject->fetch($localobject->id);
- print __METHOD__." id=".$localobject->id." result=".$result."\n";
- $this->assertLessThan($result, 0);
- $this->assertEquals($localobject->civility_id, $newobject->civility_id);
- $this->assertEquals($localobject->login, $newobject->login);
- $this->assertEquals($localobject->company, $newobject->company);
- $this->assertEquals($localobject->note_public, $newobject->note_public);
- $this->assertEquals($localobject->note_private, $newobject->note_private);
- $this->assertEquals($localobject->lastname, $newobject->lastname);
- $this->assertEquals($localobject->firstname, $newobject->firstname);
- $this->assertEquals($localobject->gender, $newobject->gender);
- $this->assertEquals($localobject->address, $newobject->address);
- $this->assertEquals($localobject->zip, $newobject->zip);
- $this->assertEquals($localobject->town, $newobject->town);
- $this->assertEquals($localobject->country_id, $newobject->country_id);
- $this->assertEquals('BE', $newobject->country_code);
- $this->assertEquals('Belgium', $newobject->country);
- $this->assertEquals($localobject->statut, $newobject->statut);
- $this->assertEquals($localobject->phone, $newobject->phone);
- $this->assertEquals($localobject->phone_perso, $newobject->phone_perso);
- $this->assertEquals($localobject->phone_mobile, $newobject->phone_mobile);
- $this->assertEquals($localobject->email, $newobject->email);
- $this->assertEquals($localobject->birth, $timestamp);
- $this->assertEquals($localobject->morphy, $newobject->morphy);
- //We return newobject because of new values
- return $newobject;
- }
- /**
- * testAdherentMakeSubstitution
- *
- * @param Adherent $localobject Member instance
- * @return Adherent
- *
- * @depends testAdherentUpdate
- * The depends says test is run only if previous is ok
- */
- public function testAdherentMakeSubstitution(Adherent $localobject)
- {
- global $conf,$user,$langs,$db;
- $conf=$this->savconf;
- $user=$this->savuser;
- $langs=$this->savlangs;
- $db=$this->savdb;
- $conf->global->MAIN_FIRSTNAME_NAME_POSITION = 0; // Force setup for firstname+lastname
- $template = '__CIVILITY__,__FIRSTNAME__,__LASTNAME__,__FULLNAME__,__COMPANY__,';
- $template .= '__ADDRESS__,__ZIP__,__TOWN__,__COUNTRY__,__EMAIL__,__BIRTH__,__PHOTO__,__LOGIN__';
- // If option to store clear password has been set, we get 'dolibspec' into PASSWORD field.
- $expected = ',New firstname,New name,New firstname New name,';
- $expected .= 'New company label,New address,New zip,New town,Belgium,newemail@newemail.com,'.dol_print_date($localobject->birth, 'day').',,';
- $expected .= 'newlogin';
- $result = $localobject->makeSubstitution($template);
- print __METHOD__." result=".$result."\n";
- $this->assertEquals($expected, $result);
- return $localobject;
- }
- /**
- * testAdherentSetUserId
- *
- * @param Adherent $localobject Member instance
- * @return Adherent
- *
- * @depends testAdherentMakeSubstitution
- * The depends says test is run only if previous is ok
- */
- public function testAdherentSetUserId(Adherent $localobject)
- {
- global $conf,$user,$langs,$db;
- $conf=$this->savconf;
- $user=$this->savuser;
- $langs=$this->savlangs;
- $db=$this->savdb;
- //We associate member with user
- $result = $localobject->setUserId($user->id);
- print __METHOD__." id=".$localobject->id." result=".$result."\n";
- $this->assertEquals($result, 1);
- //We update user object
- $user->fetch($user->id);
- print __METHOD__." user id=".$user->id." fk_member=".$user->fk_member."\n";
- $this->assertEquals($user->fk_member, $localobject->id);
- //We remove association with user
- $result = $localobject->setUserId(0);
- print __METHOD__." id=".$localobject->id." result=".$result."\n";
- $this->assertEquals($result, 1);
- //We update user object
- $user->fetch($user->id);
- print __METHOD__." user id=".$user->id." fk_member=".$user->fk_member."\n";
- $this->assertNull($user->fk_member);
- return $localobject;
- }
- /**
- * testAdherentSetThirdPartyId
- *
- * @param Adherent $localobject Member instance
- * @return Adherent
- *
- * @depends testAdherentSetUserId
- * The depends says test is run only if previous is ok
- */
- public function testAdherentSetThirdPartyId(Adherent $localobject)
- {
- global $conf,$user,$langs,$db;
- $conf=$this->savconf;
- $user=$this->savuser;
- $langs=$this->savlangs;
- $db=$this->savdb;
- //Create a Third Party
- $thirdparty = new Societe($db);
- $thirdparty->initAsSpecimen();
- $result = $thirdparty->create($user);
- print __METHOD__." third party id=".$thirdparty->id." result=".$result."\n";
- $this->assertTrue($result > 0, 'Test to create a thirdparty specimen to use it to set as thirdparty of a member');
- //Set Third Party ID
- $result = $localobject->setThirdPartyId($thirdparty->id);
- $this->assertEquals($result, 1, 'Set thirdparty');
- print __METHOD__." id=".$localobject->id." result=".$result."\n";
- //Adherent is updated with new data
- $localobject->fetch($localobject->id);
- $this->assertEquals($localobject->fk_soc, $thirdparty->id, 'Fetch member');
- print __METHOD__." id=".$localobject->id." result=".$result."\n";
- //We remove the third party association
- $result = $localobject->setThirdPartyId(0);
- $this->assertEquals($result, 1, 'Removed the link with thirdparty');
- //And check if it has been updated
- $localobject->fetch($localobject->id);
- $this->assertNull($localobject->fk_soc, 'Check field is null');
- //Now we remove the third party
- $result = $thirdparty->delete($thirdparty->id, $user);
- $this->assertEquals($result, 1, 'Delete thirdparty');
- return $localobject;
- }
- /**
- * testAdherentValid
- *
- * @param Adherent $localobject Member instance
- * @return Adherent
- *
- * @depends testAdherentSetThirdPartyId
- * The depends says test is run only if previous is ok
- */
- public function testAdherentValidate(Adherent $localobject)
- {
- global $conf,$user,$langs,$db;
- $conf=$this->savconf;
- $user=$this->savuser;
- $langs=$this->savlangs;
- $db=$this->savdb;
- $result=$localobject->validate($user);
- print __METHOD__." id=".$localobject->id." result=".$result."\n";
- $this->assertLessThan($result, 0);
- return $localobject;
- }
- /**
- * testAdherentOther
- *
- * @param Adherent $localobject Member instance
- * @return int Id of object
- *
- * @depends testAdherentValidate
- * The depends says test is run only if previous is ok
- */
- public function testAdherentOther(Adherent $localobject)
- {
- global $conf,$user,$langs,$db;
- $conf=$this->savconf;
- $user=$this->savuser;
- $langs=$this->savlangs;
- $db=$this->savdb;
- /*$result=$localobject->setstatus(0);
- print __METHOD__." id=".$localobject->id." result=".$result."\n";
- $this->assertLessThan($result, 0);
- */
- $localobject->info($localobject->id);
- print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
- $this->assertNotEquals($localobject->date_creation, '');
- return $localobject;
- }
- /**
- * testAdherentResiliate
- *
- * @param Adherent $localobject Member instance
- * @return Adherent
- *
- * @depends testAdherentOther
- * The depends says test is run only if previous is ok
- */
- public function testAdherentResiliate(Adherent $localobject)
- {
- global $conf,$user,$langs,$db;
- $conf=$this->savconf;
- $user=$this->savuser;
- $langs=$this->savlangs;
- $db=$this->savdb;
- //Let's resilie un adherent
- $result = $localobject->resiliate($user);
- print __METHOD__." id=".$localobject->id." result=".$result."\n";
- $this->assertEquals($result, 1);
- //Is statut updated?
- $this->assertEquals($localobject->statut, 0);
- //We update the object and let's check if it was updated on DB
- $localobject->fetch($localobject->id);
- $this->assertEquals($localobject->statut, 0);
- //Now that status=0, resiliate should return 0
- $result = $localobject->resiliate($user);
- print __METHOD__." id=".$localobject->id." result=".$result."\n";
- $this->assertEquals($result, 0);
- return $localobject;
- }
- /**
- * testAdherentDelete
- *
- * @param Adherent $localobject Member instance
- * @return void
- *
- * @depends testAdherentResiliate
- * The depends says test is run only if previous is ok
- */
- public function testAdherentDelete($localobject)
- {
- global $conf,$user,$langs,$db;
- $conf=$this->savconf;
- $user=$this->savuser;
- $langs=$this->savlangs;
- $db=$this->savdb;
- $result=$localobject->delete($localobject->id, $user);
- print __METHOD__." id=".$localobject->id." result=".$result."\n";
- $this->assertLessThan($result, 0);
- return $localobject;
- }
- /**
- * testAdherentTypeDelete
- *
- * @param Adherent $localobject Member instance
- * @return void
- *
- * @depends testAdherentDelete
- * The depends says test is run only if previous is ok
- */
- public function testAdherentTypeDelete($localobject)
- {
- global $conf,$user,$langs,$db;
- $conf=$this->savconf;
- $user=$this->savuser;
- $langs=$this->savlangs;
- $db=$this->savdb;
- $localobjectat=new AdherentType($this->savdb);
- $result=$localobjectat->fetch($localobject->typeid);
- $result=$localobjectat->delete();
- print __METHOD__." result=".$result."\n";
- $this->assertLessThan($result, 0);
- return $localobject->id;
- }
- }
|