From 55156edd0d28b0899d116606e6f49a76c795273b Mon Sep 17 00:00:00 2001 From: Laurent Destailleur <eldy@destailleur.fr> Date: Fri, 30 Dec 2011 14:18:19 +0100 Subject: [PATCH] Enhance PHPUnits --- htdocs/adherents/class/adherent.class.php | 62 +++-- htdocs/contact/class/contact.class.php | 131 +++++----- htdocs/societe/class/societe.class.php | 51 ++-- htdocs/societe/soc.php | 6 +- test/phpunit/AdherentTest.php | 48 +++- test/phpunit/AllTests.php | 2 + test/phpunit/ContactTest.php | 283 ++++++++++++++++++++++ test/phpunit/SocieteTest.php | 10 +- 8 files changed, 484 insertions(+), 109 deletions(-) create mode 100755 test/phpunit/ContactTest.php diff --git a/htdocs/adherents/class/adherent.class.php b/htdocs/adherents/class/adherent.class.php index d80b862bd5b..30505903f1d 100644 --- a/htdocs/adherents/class/adherent.class.php +++ b/htdocs/adherents/class/adherent.class.php @@ -80,7 +80,7 @@ class Adherent extends CommonObject var $morphy; var $public; - var $note; // Note + var $note; // Private note var $statut; // -1:brouillon, 0:resilie, >=1:valide,paye var $photo; @@ -115,11 +115,11 @@ class Adherent extends CommonObject /** * Constructor * - * @param DoliDB $DB Database handler + * @param DoliDB $db Database handler */ - function Adherent($DB) + function Adherent($db) { - $this->db = $DB ; + $this->db = $db; $this->statut = -1; // l'adherent n'est pas public par defaut $this->public = 0; @@ -388,8 +388,15 @@ class Adherent extends CommonObject dol_syslog(get_class($this)."::update notrigger=".$notrigger.", nosyncuser=".$nosyncuser.", nosyncuserpass=".$nosyncuserpass.", email=".$this->email); // Clean parameters - if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->nom=ucwords(trim($this->nom)); - if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->prenom=ucwords(trim($this->prenom)); + $this->lastname=trim($this->lastname)?trim($this->lastname):trim($this->nom); + $this->firstname=trim($this->firstname)?trim($this->firstname):trim($this->prenom); + $this->address=($this->address?$this->address:$this->adresse); + $this->zip=($this->zip?$this->zip:$this->cp); + $this->town=($this->town?$this->town:$this->ville); + $this->country_id=($this->country_id > 0?$this->country_id:$this->fk_pays); + $this->state_id=($this->state_id > 0?$this->state_id:$this->fk_departement); + if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->lastname=ucwords(trim($this->lastname)); + if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->firstname=ucwords(trim($this->firstname)); // Check parameters if (! empty($conf->global->ADHERENT_MAIL_REQUIRED) && ! isValidEMail($this->email)) @@ -403,16 +410,16 @@ class Adherent extends CommonObject $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET"; $sql.= " civilite = ".($this->civilite_id?"'".$this->civilite_id."'":"null"); - $sql.= ", prenom = ".($this->prenom?"'".$this->db->escape($this->prenom)."'":"null"); - $sql.= ", nom=" .($this->nom?"'".$this->db->escape($this->nom)."'":"null"); + $sql.= ", prenom = ".($this->firstname?"'".$this->db->escape($this->firstname)."'":"null"); + $sql.= ", nom=" .($this->lastname?"'".$this->db->escape($this->lastname)."'":"null"); $sql.= ", login=" .($this->login?"'".$this->db->escape($this->login)."'":"null"); $sql.= ", societe=" .($this->societe?"'".$this->db->escape($this->societe)."'":"null"); $sql.= ", fk_soc=" .($this->fk_soc > 0?"'".$this->fk_soc."'":"null"); - $sql.= ", adresse=" .($this->adresse?"'".$this->db->escape($this->adresse)."'":"null"); - $sql.= ", cp=" .($this->cp?"'".$this->db->escape($this->cp)."'":"null"); - $sql.= ", ville=" .($this->ville?"'".$this->db->escape($this->ville)."'":"null"); + $sql.= ", adresse=" .($this->address?"'".$this->db->escape($this->address)."'":"null"); + $sql.= ", cp=" .($this->zip?"'".$this->db->escape($this->zip)."'":"null"); + $sql.= ", ville=" .($this->town?"'".$this->db->escape($this->town)."'":"null"); $sql.= ", pays=" .($this->country_id>0?"'".$this->country_id."'":"null"); - $sql.= ", fk_departement=".($this->fk_departement>0?"'".$this->fk_departement."'":"null"); + $sql.= ", fk_departement=".($this->state_id>0?"'".$this->state_id."'":"null"); $sql.= ", email='".$this->email."'"; $sql.= ", phone=" .($this->phone?"'".$this->db->escape($this->phone)."'":"null"); $sql.= ", phone_perso=" .($this->phone_perso?"'".$this->db->escape($this->phone_perso)."'":"null"); @@ -433,7 +440,12 @@ class Adherent extends CommonObject $resql = $this->db->query($sql); if ($resql) { - $nbrowsaffected+=$this->db->affected_rows($resql); + unset($this->country_code); + unset($this->country); + unset($this->state_code); + unset($this->state); + + $nbrowsaffected+=$this->db->affected_rows($resql); // Actions on extra fields (by external module) include_once(DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'); @@ -953,7 +965,7 @@ class Adherent extends CommonObject { global $conf, $langs; - $sql = "SELECT d.rowid, d.civilite, d.prenom as firstname, d.nom as name, d.societe, d.fk_soc, d.statut, d.public, d.adresse as address, d.cp as zip, d.ville as town, d.note,"; + $sql = "SELECT d.rowid, d.civilite, d.prenom as firstname, d.nom as lastname, d.societe, d.fk_soc, d.statut, d.public, d.adresse as address, d.cp as zip, d.ville as town, d.note,"; $sql.= " d.email, d.phone, d.phone_perso, d.phone_mobile, d.login, d.pass,"; $sql.= " d.photo, d.fk_adherent_type, d.morphy,"; $sql.= " d.datec as datec,"; @@ -990,8 +1002,8 @@ class Adherent extends CommonObject $this->civilite_id = $obj->civilite; $this->prenom = $obj->firstname; $this->firstname = $obj->firstname; - $this->nom = $obj->name; - $this->lastname = $obj->name; + $this->nom = $obj->lastname; + $this->lastname = $obj->lastname; $this->login = $obj->login; $this->pass = $obj->pass; $this->societe = $obj->societe; @@ -1755,14 +1767,23 @@ class Adherent extends CommonObject /** * Return full address of member * - * @param int $withcountry 1=Add country into address string - * @param string $sep Separator to use to build string - * @return string Full address string + * + * @param int $withcountry 1=Add country into address string + * @param string $sep Separator to use to build string + * @return string Full address string */ function getFullAddress($withcountry=0,$sep="\n") { $ret=''; - if (in_array($this->country,array('us'))) + if ($withcountry && $this->country_id && (empty($this->country_code) || empty($this->country))) + { + require_once(DOL_DOCUMENT_ROOT ."/core/lib/company.lib.php"); + $tmparray=getCountry($this->country_id,'all'); + $this->country_code=$tmparray['code']; + $this->country =$tmparray['label']; + } + + if (in_array($this->country_code,array('US'))) { $ret.=($this->address?$this->address.$sep:''); $ret.=trim($this->zip.' '.$this->town); @@ -1777,7 +1798,6 @@ class Adherent extends CommonObject return trim($ret); } - /** * Retourne le libelle du statut d'un adherent (brouillon, valide, resilie) * diff --git a/htdocs/contact/class/contact.class.php b/htdocs/contact/class/contact.class.php index fd857f0431d..4b481e93902 100644 --- a/htdocs/contact/class/contact.class.php +++ b/htdocs/contact/class/contact.class.php @@ -53,6 +53,9 @@ class Contact extends CommonObject var $fk_departement; // Id of department var $departement_code; // Code of department var $departement; // Label of department + var $state_id; // Id of department + var $state_code; // Code of department + var $state; // Label of department var $fk_pays; // Id of country var $pays_code; // Code of country @@ -68,6 +71,7 @@ class Contact extends CommonObject var $email; var $birthday; var $default_lang; + var $note; // Private note var $ref_facturation; // Nb de reference facture pour lequel il est contact var $ref_contrat; // Nb de reference contrat pour lequel il est contact @@ -83,11 +87,11 @@ class Contact extends CommonObject /** * Constructor * - * @param DoliDB $DB Database handler + * @param DoliDB $db Database handler */ - function Contact($DB) + function Contact($db) { - $this->db = $DB; + $this->db = $db; } /** @@ -106,9 +110,9 @@ class Contact extends CommonObject $this->db->begin(); // Clean parameters - $this->name=trim($this->name); + $this->lastname=$this->lastname?trim($this->lastname):$this->name; $this->firstname=trim($this->firstname); - if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->name=ucwords($this->name); + if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->lastname=ucwords($this->lastname); if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->firstname=ucwords($this->firstname); if (! $this->socid) $this->socid = 0; if (! $this->priv) $this->priv = 0; @@ -134,7 +138,7 @@ class Contact extends CommonObject $sql.= " ".$conf->entity; $sql.= ")"; - dol_syslog("Contact::create sql=".$sql); + dol_syslog(get_class($this)."::create sql=".$sql); $resql=$this->db->query($sql); if ($resql) { @@ -178,7 +182,7 @@ class Contact extends CommonObject else { $this->db->rollback(); - dol_syslog("Contact::create ".$this->error, LOG_ERR); + dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR); return -2; } } @@ -187,7 +191,7 @@ class Contact extends CommonObject $this->error=$this->db->lasterror(); $this->db->rollback(); - dol_syslog("Contact::create ".$this->error, LOG_ERR); + dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR); return -1; } } @@ -208,16 +212,18 @@ class Contact extends CommonObject $this->id = $id; - // Nettoyage parametres - $this->name=trim($this->name); + // Clean parameters + $this->lastname=trim($this->lastname)?trim($this->lastname):trim($this->name); $this->firstname=trim($this->firstname); - $this->email=trim($this->email); $this->phone_pro=trim($this->phone_pro); $this->phone_perso=trim($this->phone_perso); $this->phone_mobile=trim($this->phone_mobile); $this->fax=trim($this->fax); + $this->zip=($this->zip?$this->zip:$this->cp); + $this->town=($this->town?$this->town:$this->ville); $this->country_id=($this->country_id > 0?$this->country_id:$this->fk_pays); + $this->state_id=($this->state_id > 0?$this->state_id:$this->fk_departement); $this->db->begin(); @@ -225,13 +231,13 @@ class Contact extends CommonObject if ($this->socid > 0) $sql .= " fk_soc='".$this->db->escape($this->socid)."',"; if ($this->socid == -1) $sql .= " fk_soc=null,"; $sql .= " civilite='".$this->db->escape($this->civilite_id)."'"; - $sql .= ", name='".$this->db->escape($this->name)."'"; + $sql .= ", name='".$this->db->escape($this->lastname)."'"; $sql .= ", firstname='".$this->db->escape($this->firstname)."'"; $sql .= ", address='".$this->db->escape($this->address)."'"; $sql .= ", cp='".$this->db->escape($this->zip)."'"; $sql .= ", ville='".$this->db->escape($this->town)."'"; $sql .= ", fk_pays=".($this->country_id>0?$this->country_id:'NULL'); - $sql .= ", fk_departement=".($this->fk_departement>0?$this->fk_departement:'NULL'); + $sql .= ", fk_departement=".($this->state_id>0?$this->state_id:'NULL'); $sql .= ", poste='".$this->db->escape($this->poste)."'"; $sql .= ", fax='".$this->db->escape($this->fax)."'"; $sql .= ", email='".$this->db->escape($this->email)."'"; @@ -245,10 +251,15 @@ class Contact extends CommonObject $sql .= ", default_lang=".($this->default_lang?"'".$this->default_lang."'":"null"); $sql .= " WHERE rowid=".$id; - dol_syslog("Contact::update sql=".$sql,LOG_DEBUG); + dol_syslog(get_class($this)."::update sql=".$sql,LOG_DEBUG); $result = $this->db->query($sql); if ($result) { + unset($this->country_code); + unset($this->country); + unset($this->state_code); + unset($this->state); + if (! $error && ! $notrigger) { // Appel des triggers @@ -267,7 +278,7 @@ class Contact extends CommonObject else { $this->error=join(',',$this->errors); - dol_syslog("Contact::update Error ".$this->error,LOG_ERR); + dol_syslog(get_class($this)."::update Error ".$this->error,LOG_ERR); $this->db->rollback(); return -$error; } @@ -275,7 +286,7 @@ class Contact extends CommonObject else { $this->error=$this->db->lasterror().' sql='.$sql; - dol_syslog("Contact::update Error ".$this->error,LOG_ERR); + dol_syslog(get_class($this)."::update Error ".$this->error,LOG_ERR); $this->db->rollback(); return -1; } @@ -390,7 +401,7 @@ class Contact extends CommonObject if ($user) $sql .= ", fk_user_modif=".$user->id; $sql .= " WHERE rowid=".$id; //print "update_perso: ".$this->birthday.'-'.$this->db->idate($this->birthday); - dol_syslog("Contact::update_perso this->birthday=".$this->birthday." - sql=".$sql); + dol_syslog(get_class($this)."::update_perso this->birthday=".$this->birthday." - sql=".$sql); $resql = $this->db->query($sql); if (! $resql) { @@ -450,14 +461,14 @@ class Contact extends CommonObject $langs->load("companies"); - $sql = "SELECT c.rowid, c.fk_soc, c.civilite as civilite_id, c.name, c.firstname,"; - $sql.= " c.address, c.cp, c.ville,"; - $sql.= " c.fk_pays,"; + $sql = "SELECT c.rowid, c.fk_soc, c.civilite as civilite_id, c.name as lastname, c.firstname,"; + $sql.= " c.address, c.cp as zip, c.ville as town,"; + $sql.= " c.fk_pays as country_id,"; $sql.= " c.fk_departement,"; $sql.= " c.birthday,"; $sql.= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email, c.jabberid,"; $sql.= " c.priv, c.note, c.default_lang, c.canvas,"; - $sql.= " p.libelle as pays, p.code as pays_code,"; + $sql.= " p.libelle as country, p.code as country_code,"; $sql.= " d.nom as departement, d.code_departement as departement_code,"; $sql.= " u.rowid as user_id, u.login as user_login,"; $sql.= " s.nom as socname, s.address as socaddress, s.cp as soccp, s.ville as soccity, s.default_lang as socdefault_lang"; @@ -468,7 +479,7 @@ class Contact extends CommonObject $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid"; $sql.= " WHERE c.rowid = ". $id; - dol_syslog("Contact::fetch sql=".$sql); + dol_syslog(get_class($this)."::fetch sql=".$sql); $resql=$this->db->query($sql); if ($resql) { @@ -479,31 +490,32 @@ class Contact extends CommonObject $this->id = $obj->rowid; $this->ref = $obj->rowid; $this->civilite_id = $obj->civilite_id; - $this->name = $obj->name; + $this->lastname = $obj->lastname; + $this->name = $obj->lastname; // TODO deprecated $this->firstname = $obj->firstname; - $this->nom = $obj->name; // TODO deprecated + $this->nom = $obj->lastname; // TODO deprecated $this->prenom = $obj->firstname; // TODO deprecated $this->address = $obj->address; $this->adresse = $obj->address; // TODO deprecated - $this->cp = $obj->cp; // TODO deprecated - $this->zip = $obj->cp; - $this->ville = $obj->ville; // TODO deprecated - $this->town = $obj->ville; + $this->cp = $obj->zip; // TODO deprecated + $this->zip = $obj->zip; + $this->ville = $obj->town; // TODO deprecated + $this->town = $obj->town; $this->fk_departement = $obj->fk_departement; $this->state_id = $obj->fk_departement; $this->departement_code = $obj->departement_code; // TODO deprecated $this->state_code = $obj->departement_code; - $this->departement = $obj->departement; // TODO deprecated + $this->departement = $obj->departement; // TODO deprecated $this->state = $obj->departement; - $this->fk_pays = $obj->fk_pays; - $this->country_id = $obj->fk_pays; - $this->pays_code = $obj->fk_pays?$obj->pays_code:''; - $this->country_code = $obj->fk_pays?$obj->pays_code:''; - $this->pays = ($obj->fk_pays > 0)?$langs->transnoentitiesnoconv("Country".$obj->pays_code):''; - $this->country = ($obj->fk_pays > 0)?$langs->transnoentitiesnoconv("Country".$obj->pays_code):''; + $this->fk_pays = $obj->country_id; + $this->country_id = $obj->country_id; + $this->pays_code = $obj->country_id?$obj->country_code:''; + $this->country_code = $obj->country_id?$obj->country_code:''; + $this->pays = ($obj->country_id > 0)?$langs->transnoentitiesnoconv("Country".$obj->country_code):''; + $this->country = ($obj->country_id > 0)?$langs->transnoentitiesnoconv("Country".$obj->country_code):''; $this->socid = $obj->fk_soc; $this->socname = $obj->socname; @@ -547,7 +559,7 @@ class Contact extends CommonObject else { $this->error=$this->db->error(); - dol_syslog("Contact::fetch ".$this->error, LOG_ERR); + dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR); return -1; } @@ -572,7 +584,7 @@ class Contact extends CommonObject else { $this->error=$this->db->error(); - dol_syslog("Contact::fetch ".$this->error, LOG_ERR); + dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR); return -1; } } @@ -588,7 +600,7 @@ class Contact extends CommonObject else { $this->error=$this->db->error(); - dol_syslog("Contact::fetch ".$this->error, LOG_ERR); + dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR); return -1; } } @@ -612,7 +624,7 @@ class Contact extends CommonObject $sql.=" AND fk_socpeople = ". $this->id; $sql.=" GROUP BY tc.element"; - dol_syslog("Contact::load_ref_elements sql=".$sql); + dol_syslog(get_class($this)."::load_ref_elements sql=".$sql); $resql=$this->db->query($sql); if ($resql) @@ -633,7 +645,7 @@ class Contact extends CommonObject else { $this->error=$this->db->error()." - ".$sql; - dol_syslog("Contact::load_ref_elements Error ".$this->error, LOG_ERR); + dol_syslog(get_class($this)."::load_ref_elements Error ".$this->error, LOG_ERR); return -1; } } @@ -664,7 +676,7 @@ class Contact extends CommonObject $sql.= " WHERE ec.fk_socpeople=".$this->id; $sql.= " AND ec.fk_c_type_contact=tc.rowid"; $sql.= " AND tc.source='external'"; - dol_syslog("Contact::delete sql=".$sql); + dol_syslog(get_class($this)."::delete sql=".$sql); $resql = $this->db->query($sql); if ($resql) { @@ -677,7 +689,7 @@ class Contact extends CommonObject $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact"; $sqldel.=" WHERE rowid = ".$obj->rowid; - dol_syslog("Contact::delete sql=".$sqldel); + dol_syslog(get_class($this)."::delete sql=".$sqldel); $result = $this->db->query($sqldel); if (! $result) { @@ -699,7 +711,7 @@ class Contact extends CommonObject { $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople"; $sql .= " WHERE rowid=".$this->id; - dol_syslog("Contact::delete sql=".$sql); + dol_syslog(get_class($this)."::delete sql=".$sql); $result = $this->db->query($sql); if (! $result) { @@ -847,7 +859,15 @@ class Contact extends CommonObject function getFullAddress($withcountry=0,$sep="\n") { $ret=''; - if (in_array($this->country,array('us'))) + if ($withcountry && $this->country_id && (empty($this->country_code) || empty($this->country))) + { + require_once(DOL_DOCUMENT_ROOT ."/core/lib/company.lib.php"); + $tmparray=getCountry($this->country_id,'all'); + $this->country_code=$tmparray['code']; + $this->country =$tmparray['label']; + } + + if (in_array($this->country_code,array('US'))) { $ret.=($this->address?$this->address.$sep:''); $ret.=trim($this->zip.' '.$this->town); @@ -875,13 +895,6 @@ class Contact extends CommonObject $code=$this->civilite_id; return $langs->trans("Civility".$code)!="Civility".$code ? $langs->trans("Civility".$code) : ''; - /*if (empty($ret)) - { - $ret=$code; - $langs->getLabelFromKey($this->db,$reg[1],'c_civilite','code','civilite'); - //$ret=dol_getIdFromCode($this->db,$code,'c_civilite', - } - return $ret;*/ } @@ -1035,16 +1048,14 @@ class Contact extends CommonObject // Initialise parameters $this->id=0; $this->specimen=1; - $this->nom = 'DOLIBARR'; - $this->name = $this->nom; - $this->prenom = 'SPECIMEN'; - $this->firstname = $this->prenom; + $this->name = 'DOLIBARR'; + $this->firstname = 'SPECIMEN'; $this->address = '61 jump street'; - $this->cp = '75000'; - $this->ville = 'Paris'; - $this->fk_pays = 1; - $this->pays_code = 'FR'; - $this->pays = 'France'; + $this->zip = '75000'; + $this->town = 'Paris'; + $this->country_id = 1; + $this->country_code = 'FR'; + $this->country = 'France'; $this->email = 'specimen@specimen.com'; $socid = rand(1, $num_socs); $this->socid = $socids[$socid]; diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index f7538120dc1..abcc5937a76 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -377,7 +377,7 @@ class Societe extends CommonObject $error=0; - dol_syslog("Societe::Update id=".$id." call_trigger=".$call_trigger." allowmodcodeclient=".$allowmodcodeclient." allowmodcodefournisseur=".$allowmodcodefournisseur); + dol_syslog(get_class($this)."::Update id=".$id." call_trigger=".$call_trigger." allowmodcodeclient=".$allowmodcodeclient." allowmodcodefournisseur=".$allowmodcodefournisseur); // For triggers if ($call_trigger) @@ -401,7 +401,7 @@ class Societe extends CommonObject $this->ville=$this->town; // TODO obsolete $this->state_id=trim($this->state_id); $this->country_id = ($this->country_id > 0)?$this->country_id:$this->pays_id; - $this->pays_id = $this->country_id; + $this->pays_id = $this->country_id; $this->tel = trim($this->tel); $this->fax = trim($this->fax); $this->tel = preg_replace("/\s/","",$this->tel); @@ -456,7 +456,7 @@ class Societe extends CommonObject if ($result >= 0) { - dol_syslog("Societe::Update verify ok"); + dol_syslog(get_class($this)."::Update verify ok"); $sql = "UPDATE ".MAIN_DB_PREFIX."societe"; $sql.= " SET nom = '" . $this->db->escape($this->name) ."'"; // Champ obligatoire @@ -534,6 +534,11 @@ class Societe extends CommonObject $resql=$this->db->query($sql); if ($resql) { + unset($this->country_code); + unset($this->country); + unset($this->state_code); + unset($this->state); + // Si le fournisseur est classe on l'ajoute $this->AddFournisseurInCategory($this->fournisseur_categorie); @@ -698,7 +703,7 @@ class Societe extends CommonObject $this->country_code = $obj->country_id?$obj->country_code:''; $this->pays = $obj->country_id?($langs->trans('Country'.$obj->country_code)!='Country'.$obj->country_code?$langs->trans('Country'.$obj->country_code):$obj->country):''; // TODO obsolete $this->country = $obj->country_id?($langs->trans('Country'.$obj->country_code)!='Country'.$obj->country_code?$langs->trans('Country'.$obj->country_code):$obj->country):''; - + $this->state_id = $obj->fk_departement; $this->state_code = $obj->departement_code; $this->state = $obj->departement; @@ -1426,24 +1431,33 @@ class Societe extends CommonObject /** * Return full address of third party - * @param withcountry 1=Add country into address string - * @param sep Separator to use to build string - * @return string Full address string + * + * @param int $withcountry 1=Add country into address string + * @param string $sep Separator to use to build string + * @return string Full address string */ function getFullAddress($withcountry=0,$sep="\n") { $ret=''; - if (in_array($this->country,array('us'))) + if ($withcountry && $this->country_id && (empty($this->country_code) || empty($this->country))) { - $ret.=($this->address?$this->address.$sep:''); - $ret.=trim($this->zip.' '.$this->town); - if ($withcountry) $ret.=($this->country?$sep.$this->country:''); + require_once(DOL_DOCUMENT_ROOT ."/core/lib/company.lib.php"); + $tmparray=getCountry($this->country_id,'all'); + $this->country_code=$tmparray['code']; + $this->country =$tmparray['label']; + } + + if (in_array($this->country_code,array('US'))) + { + $ret.=($this->address?$this->address.$sep:''); + $ret.=trim($this->zip.' '.$this->town); + if ($withcountry) $ret.=($this->country?$sep.$this->country:''); } else { - $ret.=($this->address?$this->address.$sep:''); - $ret.=trim($this->zip.' '.$this->town); - if ($withcountry) $ret.=($this->country?$sep.$this->country:''); + $ret.=($this->address?$this->address.$sep:''); + $ret.=trim($this->zip.' '.$this->town); + if ($withcountry) $ret.=($this->country?$sep.$this->country:''); } return trim($ret); } @@ -1913,10 +1927,11 @@ class Societe extends CommonObject /** * Verify if a profid exists into database for others thirds - * @param idprof 1,2,3,4 (Exemple: 1=siren,2=siret,3=naf,4=rcs/rm) - * @param value value of profid - * @param socid id of society if update - * @return boolean true if exists, false if not + * + * @param int $idprof 1,2,3,4 (Example: 1=siren,2=siret,3=naf,4=rcs/rm) + * @param string $value Value of profid + * @param int $socid Id of society if update + * @return boolean true if exists, false if not */ function id_prof_exists($idprof,$value,$socid=0) { diff --git a/htdocs/societe/soc.php b/htdocs/societe/soc.php index d205cfefb3d..a30531e35a9 100644 --- a/htdocs/societe/soc.php +++ b/htdocs/societe/soc.php @@ -234,11 +234,9 @@ if (empty($reshook)) $contact->firstname = $object->firstname; $contact->address = $object->address; $contact->zip = $object->zip; - $contact->cp = $object->cp; // TODO obsolete $contact->town = $object->town; - $contact->ville = $object->ville; // TODO obsolete - $contact->fk_departement = $object->state_id; - $contact->fk_pays = $object->country_id; + $contact->state_id = $object->state_id; + $contact->country_id = $object->country_id; $contact->socid = $object->id; // fk_soc $contact->status = 1; $contact->email = $object->email; diff --git a/test/phpunit/AdherentTest.php b/test/phpunit/AdherentTest.php index ef473d7d5c1..d0e68c1ba7e 100644 --- a/test/phpunit/AdherentTest.php +++ b/test/phpunit/AdherentTest.php @@ -158,10 +158,52 @@ class AdherentTest extends PHPUnit_Framework_TestCase $langs=$this->savlangs; $db=$this->savdb; + $localobject->login='newlogin'; + $localobject->societe='New company'; $localobject->note='New note after update'; - $result=$localobject->update($user); - print __METHOD__." id=".$localobject->id." result=".$result."\n"; - $this->assertLessThan($result, 0); + //$localobject->note_public='New note public after update'; + $localobject->lastname='New name'; + $localobject->firstname='New firstname'; + $localobject->address='New address'; + $localobject->zip='New zip'; + $localobject->town='New town'; + $localobject->country_id=2; + $localobject->statut=0; + $localobject->phone='New tel pro'; + $localobject->phone_perso='New tel perso'; + $localobject->phone_mobile='New tel mobile'; + $localobject->email='newemail@newemail.com'; + $result=$localobject->update($user); + print __METHOD__." id=".$localobject->id." result=".$result."\n"; + $this->assertLessThan($result, 0); + $result=$localobject->update_note($localobject->note); + print __METHOD__." id=".$localobject->id." result=".$result."\n"; + $this->assertLessThan($result, 0); + //$result=$localobject->update_note_public($localobject->note_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->login, $newobject->login); + $this->assertEquals($localobject->societe, $newobject->societe); + $this->assertEquals($localobject->note, $newobject->note); + //$this->assertEquals($localobject->note_public, $newobject->note_public); + $this->assertEquals($localobject->lastname, $newobject->lastname); + $this->assertEquals($localobject->firstname, $newobject->firstname); + $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($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); return $localobject; } diff --git a/test/phpunit/AllTests.php b/test/phpunit/AllTests.php index 83d8427396d..cb11567eac5 100644 --- a/test/phpunit/AllTests.php +++ b/test/phpunit/AllTests.php @@ -74,6 +74,8 @@ class AllTests require_once dirname(__FILE__).'/SocieteTest.php'; $suite->addTestSuite('SocieteTest'); + require_once dirname(__FILE__).'/ContactTest.php'; + $suite->addTestSuite('ContactTest'); require_once dirname(__FILE__).'/AdherentTest.php'; $suite->addTestSuite('AdherentTest'); diff --git a/test/phpunit/ContactTest.php b/test/phpunit/ContactTest.php new file mode 100755 index 00000000000..dcb989e8359 --- /dev/null +++ b/test/phpunit/ContactTest.php @@ -0,0 +1,283 @@ +<?php +/* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net> + * + * 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 2 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 <http://www.gnu.org/licenses/>. + * or see http://www.gnu.org/ + */ + +/** + * \file test/phpunit/ContactTest.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/contact/class/contact.class.php'; +$langs->load("dict"); + +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 ContactTest extends PHPUnit_Framework_TestCase +{ + protected $savconf; + protected $savuser; + protected $savlangs; + protected $savdb; + + /** + * Constructor + * We save global variables into local variables + * + * @return ContactTest + */ + function ContactTest() + { + //$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"; + } + + // Static methods + 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. + + print __METHOD__."\n"; + } + public static function tearDownAfterClass() + { + global $conf,$user,$langs,$db; + $db->rollback(); + + print __METHOD__."\n"; + } + + /** + */ + protected function setUp() + { + global $conf,$user,$langs,$db; + $conf=$this->savconf; + $user=$this->savuser; + $langs=$this->savlangs; + $db=$this->savdb; + + print __METHOD__."\n"; + } + /** + */ + protected function tearDown() + { + print __METHOD__."\n"; + } + + /** + */ + public function testContactCreate() + { + global $conf,$user,$langs,$db; + $conf=$this->savconf; + $user=$this->savuser; + $langs=$this->savlangs; + $db=$this->savdb; + + $localobject=new Contact($this->savdb); + $localobject->initAsSpecimen(); + $result=$localobject->create($user); + + print __METHOD__." result=".$result."\n"; + $this->assertLessThanOrEqual($result, 0); + + return $result; + } + + /** + * @depends testContactCreate + * The depends says test is run only if previous is ok + */ + public function testContactFetch($id) + { + global $conf,$user,$langs,$db; + $conf=$this->savconf; + $user=$this->savuser; + $langs=$this->savlangs; + $db=$this->savdb; + + $localobject=new Contact($this->savdb); + $result=$localobject->fetch($id); + print __METHOD__." id=".$id." result=".$result."\n"; + $this->assertLessThan($result, 0); + + return $localobject; + } + + /** + * @depends testContactFetch + * The depends says test is run only if previous is ok + */ + public function testContactUpdate($localobject) + { + global $conf,$user,$langs,$db; + $conf=$this->savconf; + $user=$this->savuser; + $langs=$this->savlangs; + $db=$this->savdb; + + $localobject->note='New note after update'; + //$localobject->note_public='New note public after update'; + $localobject->lastname='New name'; + $localobject->firstname='New firstname'; + $localobject->address='New address'; + $localobject->zip='New zip'; + $localobject->town='New town'; + $localobject->country_id=2; + $localobject->status=0; + $localobject->phone_pro='New tel pro'; + $localobject->phone_perso='New tel perso'; + $localobject->phone_mobile='New tel mobile'; + $localobject->fax='New fax'; + $localobject->email='newemail@newemail.com'; + $localobject->jabberid='New im id'; + $localobject->default_lang='es_ES'; + $result=$localobject->update($localobject->id,$user); + print __METHOD__." id=".$localobject->id." result=".$result."\n"; + $this->assertLessThan($result, 0); + $result=$localobject->update_note($localobject->note); + print __METHOD__." id=".$localobject->id." result=".$result."\n"; + $this->assertLessThan($result, 0); + //$result=$localobject->update_note_public($localobject->note_public); + //print __METHOD__." id=".$localobject->id." result=".$result."\n"; + //$this->assertLessThan($result, 0); + + $newobject=new Contact($this->savdb); + $result=$newobject->fetch($localobject->id); + print __METHOD__." id=".$localobject->id." result=".$result."\n"; + $this->assertLessThan($result, 0); + + $this->assertEquals($localobject->note, $newobject->note); + //$this->assertEquals($localobject->note_public, $newobject->note_public); + $this->assertEquals($localobject->lastname, $newobject->lastname); + $this->assertEquals($localobject->firstname, $newobject->firstname); + $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($localobject->status, $newobject->status); + $this->assertEquals($localobject->phone_pro, $newobject->phone_pro); + $this->assertEquals($localobject->phone_perso, $newobject->phone_perso); + $this->assertEquals($localobject->phone_mobile, $newobject->phone_mobile); + $this->assertEquals($localobject->fax, $newobject->fax); + $this->assertEquals($localobject->email, $newobject->email); + $this->assertEquals($localobject->jabberid, $newobject->jabberid); + $this->assertEquals($localobject->default_lang, $newobject->default_lang); + + return $localobject; + } + + /** + * @depends testContactUpdate + * The depends says test is run only if previous is ok + */ + public function testContactOther($localobject) + { + global $conf,$user,$langs,$db; + $conf=$this->savconf; + $user=$this->savuser; + $langs=$this->savlangs; + $db=$this->savdb; + + //$localobject->fetch($localobject->id); + + $result=$localobject->getNomUrl(1); + print __METHOD__." id=".$localobject->id." result=".$result."\n"; + $this->assertNotEquals($result, ''); + + $result=$localobject->getFullAddress(1); + print __METHOD__." id=".$localobject->id." result=".$result."\n"; + $this->assertContains("New address\nNew zip New town\nBelgium", $result); + + $localobject->info($localobject->id); + print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n"; + $this->assertNotEquals($localobject->date_creation, ''); + + return $localobject->id; + } + + /** + * @depends testContactOther + * The depends says test is run only if previous is ok + */ + public function testContactDelete($id) + { + global $conf,$user,$langs,$db; + $conf=$this->savconf; + $user=$this->savuser; + $langs=$this->savlangs; + $db=$this->savdb; + + $localobject=new Contact($this->savdb); + $result=$localobject->fetch($id); + + $result=$localobject->delete($id); + print __METHOD__." id=".$id." result=".$result."\n"; + $this->assertLessThan($result, 0); + + return $result; + } + + /** + */ + public function testContactStatic() + { + global $conf,$user,$langs,$db; + $conf=$this->savconf; + $user=$this->savuser; + $langs=$this->savlangs; + $db=$this->savdb; + + $localobject=new Contact($db); + + + return; + } +} +?> \ No newline at end of file diff --git a/test/phpunit/SocieteTest.php b/test/phpunit/SocieteTest.php index d43ad86471d..53fd04d90cb 100755 --- a/test/phpunit/SocieteTest.php +++ b/test/phpunit/SocieteTest.php @@ -28,6 +28,7 @@ global $conf,$user,$langs,$db; require_once 'PHPUnit/Autoload.php'; require_once dirname(__FILE__).'/../../htdocs/master.inc.php'; require_once dirname(__FILE__).'/../../htdocs/societe/class/societe.class.php'; +$langs->load("dict"); if (empty($user->id)) { @@ -172,10 +173,11 @@ class SocieteTest extends PHPUnit_Framework_TestCase $localobject->address='New address'; $localobject->zip='New zip'; $localobject->town='New town'; + $localobject->country_id=2; $localobject->status=0; $localobject->tel='New tel'; $localobject->fax='New fax'; - $localobject->email='New email'; + $localobject->email='newemail@newemail.com'; $localobject->url='New url'; $localobject->idprof1='new idprof1'; $localobject->idprof2='new idprof2'; @@ -202,6 +204,8 @@ class SocieteTest extends PHPUnit_Framework_TestCase $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($localobject->status, $newobject->status); $this->assertEquals($localobject->tel, $newobject->tel); $this->assertEquals($localobject->fax, $newobject->fax); @@ -243,9 +247,9 @@ class SocieteTest extends PHPUnit_Framework_TestCase print __METHOD__." id=".$localobject->id." result=".$result."\n"; $this->assertNotEquals($result, ''); - $result=$localobject->getFullAddress(); + $result=$localobject->getFullAddress(1); print __METHOD__." id=".$localobject->id." result=".$result."\n"; - $this->assertContains("New address\nNew zip New town", $result); + $this->assertContains("New address\nNew zip New town\nBelgium", $result); $result=$localobject->isInEEC(); print __METHOD__." id=".$localobject->id." country_code=".$this->country_code." result=".$result."\n"; -- GitLab