From 2be848025f231922a5ef5061fe91e190dde2061d Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Mon, 26 Jun 2017 10:09:22 +0200
Subject: [PATCH] Fix parameter withcode of currency_name function. Add PHPUnit
 tests

---
 htdocs/core/lib/company.lib.php | 105 ++++++++++++-----------
 test/phpunit/AdminLibTest.php   |   2 +-
 test/phpunit/AllTests.php       |  10 ++-
 test/phpunit/CompanyLibTest.php | 144 ++++++++++++++++++++++++++++++++
 4 files changed, 205 insertions(+), 56 deletions(-)
 create mode 100644 test/phpunit/CompanyLibTest.php

diff --git a/htdocs/core/lib/company.lib.php b/htdocs/core/lib/company.lib.php
index 0a41969afd3..d1e0b9020b7 100644
--- a/htdocs/core/lib/company.lib.php
+++ b/htdocs/core/lib/company.lib.php
@@ -162,7 +162,7 @@ function societe_prepare_head(Societe $object)
         $head[$h][2] = 'rib';
         $h++;
     }
-        
+
 	// Show more tabs from modules
     // Entries must be declared in modules descriptor with line
     // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__');   to add new tab
@@ -217,7 +217,7 @@ function societe_prepare_head(Societe $object)
         $upload_dir = $conf->societe->multidir_output[$object->entity] . "/" . $object->id ;
         $nbFiles = count(dol_dir_list($upload_dir,'files',0,'','(\.meta|_preview.*\.png)$'));
         $nbLinks=Link::count($db, $object->element, $object->id);
-        
+
         $head[$h][0] = DOL_URL_ROOT.'/societe/document.php?socid='.$object->id;
         $head[$h][1] = $langs->trans("Documents");
 		if (($nbFiles+$nbLinks) > 0) $head[$h][1].= ' <span class="badge">'.($nbFiles+$nbLinks).'</span>';
@@ -234,7 +234,7 @@ function societe_prepare_head(Societe $object)
     }
     $head[$h][2] = 'agenda';
     $h++;
-    
+
     // Log
     /*$head[$h][0] = DOL_URL_ROOT.'/societe/info.php?socid='.$object->id;
     $head[$h][1] = $langs->trans("Info");
@@ -334,7 +334,7 @@ function getCountry($searchkey,$withcode='',$dbtouse=0,$outputlangs='',$entconv=
     global $db,$langs;
 
     $result='';
-    
+
     // Check parameters
     if (empty($searchkey) && empty($searchlabel))
     {
@@ -421,23 +421,26 @@ function getState($id,$withcode='',$dbtouse=0)
 }
 
 /**
- *    Retourne le nom traduit ou code+nom d'une devise
+ *    Return label of currency or code+label
  *
- *    @param      string	$code_iso       Code iso de la devise
- *    @param      int		$withcode       '1'=affiche code + nom
- *    @return     string     			    Nom traduit de la devise
+ *    @param      string	$code_iso       Code iso of currency
+ *    @param      int		$withcode       '1'=show code + label
+ *    @param      Translate $outputlangs    Output language
+ *    @return     string     			    Label translated of currency
  */
-function currency_name($code_iso,$withcode='')
+function currency_name($code_iso, $withcode='', $outputlangs=null)
 {
     global $langs,$db;
 
-    // Si il existe une traduction, on peut renvoyer de suite le libelle
-    if ($langs->trans("Currency".$code_iso)!="Currency".$code_iso)
+    if (empty($outputlangs)) $outputlangs=$langs;
+
+    // If there is a translation, we can send immediatly the label
+    if ($outputlangs->trans("Currency".$code_iso)!="Currency".$code_iso)
     {
-        return $langs->trans("Currency".$code_iso);
+        return ($withcode?$code_iso.' - ':'').$outputlangs->trans("Currency".$code_iso);
     }
 
-    // Si pas de traduction, on consulte le libelle par defaut en table
+    // If no translation, we read table to get label by default
     $sql = "SELECT label FROM ".MAIN_DB_PREFIX."c_currencies";
     $sql.= " WHERE code_iso='".$code_iso."'";
 
@@ -457,8 +460,8 @@ function currency_name($code_iso,$withcode='')
         {
             return $code_iso;
         }
-
     }
+    return 'ErrorWhenReadingCurrencyLabel';
 }
 
 /**
@@ -499,7 +502,7 @@ function getFormeJuridiqueLabel($code)
 /**
  *  Return if a country is inside the EEC (European Economic Community)
  *  TODO Add a field into country dictionary.
- *  
+ *
  *  @param      Object      $object    Object
  *  @return     boolean		           true = country inside EEC, false = country outside EEC
  */
@@ -649,7 +652,7 @@ function show_projects($conf, $langs, $db, $object, $backtopage='', $nocreatelin
 			            // Opp percent
             			print '<td align="right">';
             			if ($obj->opp_percent) print price($obj->opp_percent, 1, '', 1, 0).'%';
-            			print '</td>';			            
+            			print '</td>';
                         // Status
                         print '<td align="right">'.$projecttmp->getLibStatut(5).'</td>';
 
@@ -671,7 +674,7 @@ function show_projects($conf, $langs, $db, $object, $backtopage='', $nocreatelin
         }
         print "</table>";
         print '</div>';
-        
+
         print "<br>\n";
     }
 
@@ -735,7 +738,7 @@ function show_contacts($conf,$langs,$db,$object,$backtopage='')
     print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
     print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
     print '<input type="hidden" name="page" value="'.$page.'">';
-    
+
     print "\n".'<table class="noborder" width="100%">'."\n";
 
     $param="socid=".$object->id;
@@ -777,44 +780,44 @@ function show_contacts($conf,$langs,$db,$object,$backtopage='')
 	if ($num || (GETPOST('button_search') || GETPOST('button_search.x') || GETPOST('button_search_x')))
     {
         print '<tr class="liste_titre">';
-        
+
         // Photo - Name
         print '<td class="liste_titre">';
         print '<input type="text" class="flat" name="search_name" size="20" value="'.$search_name.'">';
         print '</td>';
-    
+
         // Position
         print '<td class="liste_titre">';
         print '</td>';
-    
+
         // Address - Phone - Email
         print '<td class="liste_titre">&nbsp;</td>';
-    
+
         // Status
         print '<td class="liste_titre maxwidthonsmartphone">';
         print $form->selectarray('search_status', array('-1'=>'','0'=>$contactstatic->LibStatut(0,1),'1'=>$contactstatic->LibStatut(1,1)),$search_status);
         print '</td>';
-    
+
         // Add to agenda
         if (! empty($conf->agenda->enabled) && $user->rights->agenda->myactions->create)
         {
         	$colspan++;
             print '<td class="liste_titre">&nbsp;</td>';
         }
-    
+
     	// Edit
         print '<td class="liste_titre" align="right">';
         print '<input type="image" class="liste_titre" name="button_search" src="'.img_picto($langs->trans("Search"),'search.png','','',1).'" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">';
         print '</td>';
-    
+
         print "</tr>";
-    
+
         $i=0;
 
         while ($i < $num)
         {
             $obj = $db->fetch_object($result);
-            
+
             $contactstatic->id = $obj->rowid;
             $contactstatic->ref = $obj->ref;
             $contactstatic->statut = $obj->statut;
@@ -838,7 +841,7 @@ function show_contacts($conf,$langs,$db,$object,$backtopage='')
             $contactstatic->country_code = $country_code;
 
             $contactstatic->setGenderFromCivility();
-            
+
             print "<tr>";
 
 			// Photo - Name
@@ -846,7 +849,7 @@ function show_contacts($conf,$langs,$db,$object,$backtopage='')
             print $form->showphoto('contact',$contactstatic,0,0,0,'photorefnoborder valignmiddle marginrightonly','small',1,0,1);
 			print $contactstatic->getNomUrl(0,'',0,'&backtopage='.urlencode($backtopage));
 			print '</td>';
-			
+
 			// Job position
 			print '<td>';
             if ($obj->poste) print $obj->poste;
@@ -1036,7 +1039,7 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon='', $noprint=
     global $form;
 
     global $param;
-    
+
     // Check parameters
     if (! is_object($filterobj)) dol_print_error('','BadParameter');
 
@@ -1068,7 +1071,7 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon='', $noprint=
         $sql.= " AND a.entity IN (".getEntity('agenda').")";
         if (get_class($filterobj) == 'Societe'  && $filterobj->id) $sql.= " AND a.fk_soc = ".$filterobj->id;
         if (get_class($filterobj) == 'Project' && $filterobj->id) $sql.= " AND a.fk_project = ".$filterobj->id;
-        if (get_class($filterobj) == 'Adherent') 
+        if (get_class($filterobj) == 'Adherent')
         {
             $sql.= " AND a.fk_element = m.rowid AND a.elementtype = 'member'";
             if ($filterobj->id) $sql.= " AND a.fk_element = ".$filterobj->id;
@@ -1086,7 +1089,7 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon='', $noprint=
             {
                 if ($actioncode == 'AC_NON_AUTO') $sql.= " AND c.type != 'systemauto'";
                 elseif ($actioncode == 'AC_ALL_AUTO') $sql.= " AND c.type = 'systemauto'";
-                else 
+                else
                 {
                     if ($actioncode == 'AC_OTH') $sql.= " AND c.type != 'systemauto'";
                     if ($actioncode == 'AC_OTH_AUTO') $sql.= " AND c.type = 'systemauto'";
@@ -1114,7 +1117,7 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon='', $noprint=
             while ($i < $num)
             {
                 $obj = $db->fetch_object($resql);
-                
+
                 //if ($donetodo == 'todo') $sql.= " AND ((a.percent >= 0 AND a.percent < 100) OR (a.percent = -1 AND a.datep > '".$db->idate($now)."'))";
                 //if ($donetodo == 'done') $sql.= " AND (a.percent = 100 OR (a.percent = -1 AND a.datep <= '".$db->idate($now)."'))";
                 $tododone='';
@@ -1128,7 +1131,7 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon='', $noprint=
             		'dateend'=>$db->jdate($obj->dp2),
             		'note'=>$obj->label,
             		'percent'=>$obj->percent,
-                    
+
                     'userid'=>$obj->user_id,
                     'login'=>$obj->user_login,
                     'userfirstname'=>$obj->user_firstname,
@@ -1146,7 +1149,7 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon='', $noprint=
                     'libelle'=>$obj->alabel,    // deprecated
                     'apicto'=>$obj->apicto
                 );
-                
+
                 $numaction++;
                 $i++;
             }
@@ -1191,7 +1194,7 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon='', $noprint=
                 		'note'=>$obj->note,
                 		'percent'=>$obj->percentage,
                 		'acode'=>$obj->acode,
-                    
+
                         'userid'=>$obj->user_id,
                         'login'=>$obj->user_login,
                         'userfirstname'=>$obj->user_firstname,
@@ -1213,7 +1216,7 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon='', $noprint=
     if (! empty($conf->agenda->enabled) || (! empty($conf->mailing->enabled) && ! empty($objcon->email)))
     {
         $delay_warning=$conf->global->MAIN_DELAY_ACTIONS_TODO*24*60*60;
-        
+
         require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
         require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
         require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
@@ -1222,7 +1225,7 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon='', $noprint=
 	    require_once DOL_DOCUMENT_ROOT.'/core/class/html.formactions.class.php';
 
 	    $formactions=new FormActions($db);
-        
+
 	    $actionstatic=new ActionComm($db);
         $userstatic=new User($db);
         $contactstatic = new Contact($db);
@@ -1243,9 +1246,9 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon='', $noprint=
             $out.='<input type="hidden" name="id" value="'.$filterobj->id.'" />';
         }
         if (get_class($filterobj) == 'Societe') $out.='<input type="hidden" name="socid" value="'.$filterobj->id.'" />';
-        
+
         $out.="\n";
-        
+
         $out.='<div class="div-table-responsive-no-min">';
         $out.='<table class="noborder" width="100%">';
 
@@ -1270,7 +1273,7 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon='', $noprint=
         $out.=$searchpicto;
         $out.='</td>';
         $out.='</tr>';
-        
+
         $out.='<tr class="liste_titre">';
 		if ($donetodo)
 		{
@@ -1293,26 +1296,26 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon='', $noprint=
 		$out.=getTitleFieldOfList($langs->trans("Status"), 0, $_SERVER["PHP_SELF"], 'a.percent', '', $param, 'align="center"', $sortfield, $sortorder);
 		$out.=getTitleFieldOfList('', 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'maxwidthsearch ');
 		$out.='</tr>';
-		
+
         foreach ($histo as $key=>$value)
         {
-            
+
 			$actionstatic->fetch($histo[$key]['id']);    // TODO Do we need this, we already have a lot of data of line into $histo
 
             $out.="<tr ".$bc[$var].">";
-			
+
             // Done or todo
             if ($donetodo)
             {
                 $out.='<td class="nowrap">';
                 $out.='</td>';
             }
-            
+
             // Ref
             $out.='<td class="nowrap">';
             $out.=$actionstatic->getNomUrl(1, -1);
             $out.='</td>';
-            
+
             // Author of event
             $out.='<td>';
             //$userstatic->id=$histo[$key]['userid'];
@@ -1321,7 +1324,7 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon='', $noprint=
             $userstatic->fetch($histo[$key]['userid']);
             $out.=$userstatic->getNomUrl(-1);
             $out.='</td>';
-            
+
             // Title
             $out.='<td>';
             if (isset($histo[$key]['type']) && $histo[$key]['type']=='action')
@@ -1360,7 +1363,7 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon='', $noprint=
             if ($histo[$key]['percent'] > 0 && $histo[$key]['percent'] < 100 && ! $histo[$key]['dateend'] && $histo[$key]['datestart'] && $db->jdate($histo[$key]['datestart']) < ($now - $delay_warning)) $late=1;
             if ($late) $out.=img_warning($langs->trans("Late")).' ';
             $out.="</td>\n";
-            
+
             // Type
 			$out.='<td>';
 			if (! empty($conf->global->AGENDA_USE_EVENT_TYPE))
@@ -1375,7 +1378,7 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon='', $noprint=
 			}
 			else {
 			    $typelabel = $actionstatic->type;
-			    if ($histo[$key]['acode'] != 'AC_OTH_AUTO') $typelabel = $langs->trans("ActionAC_MANUAL"); 
+			    if ($histo[$key]['acode'] != 'AC_OTH_AUTO') $typelabel = $langs->trans("ActionAC_MANUAL");
 			    $out.=$typelabel;
 			}
 			$out.='</td>';
@@ -1445,7 +1448,7 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon='', $noprint=
 
             // Actions
             $out.='<td></td>';
-            
+
             $out.="</tr>\n";
             $i++;
         }
@@ -1454,7 +1457,7 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon='', $noprint=
     }
 
     $out.='</form>';
-    
+
     if ($noprint) return $out;
     else print $out;
 }
diff --git a/test/phpunit/AdminLibTest.php b/test/phpunit/AdminLibTest.php
index 7c9033fe2a6..5cf34e969b1 100644
--- a/test/phpunit/AdminLibTest.php
+++ b/test/phpunit/AdminLibTest.php
@@ -115,7 +115,7 @@ class AdminLibTest extends PHPUnit_Framework_TestCase
     }
 
     /**
-     * testConvertTime2Seconds
+     * testVersionCompare
      *
      * @return	void
      */
diff --git a/test/phpunit/AllTests.php b/test/phpunit/AllTests.php
index a417824867b..56c5541d0bb 100644
--- a/test/phpunit/AllTests.php
+++ b/test/phpunit/AllTests.php
@@ -81,6 +81,8 @@ class AllTests
         //$suite->addTestSuite('CoreTest');
         require_once dirname(__FILE__).'/AdminLibTest.php';
         $suite->addTestSuite('AdminLibTest');
+        require_once dirname(__FILE__).'/CompanyLibTest.php';
+        $suite->addTestSuite('CompanyLibTest');
         require_once dirname(__FILE__).'/DateLibTest.php';
         $suite->addTestSuite('DateLibTest');
         //require_once dirname(__FILE__).'/DateLibTzFranceTest.php';
@@ -107,7 +109,7 @@ class AllTests
         $suite->addTestSuite('CodingSqlTest');
         require_once dirname(__FILE__).'/CodingPhpTest.php';
         $suite->addTestSuite('CodingPhpTest');
-        
+
         require_once dirname(__FILE__).'/SecurityTest.php';
         $suite->addTestSuite('SecurityTest');
 
@@ -121,7 +123,7 @@ class AllTests
         $suite->addTestSuite('BuildDocTest');
         require_once dirname(__FILE__).'/CMailFileTest.php';
         $suite->addTestSuite('CMailFileTest');
-        
+
         require_once dirname(__FILE__).'/CommonObjectTest.php';
         $suite->addTestSuite('CommonObjectTest');
 
@@ -181,7 +183,7 @@ class AllTests
         $suite->addTestSuite('HolidayTest');
         require_once dirname(__FILE__).'/ExpenseReportTest.php';
         $suite->addTestSuite('ExpenseReportTest');
-        
+
         require_once dirname(__FILE__).'/EntrepotTest.php';
         $suite->addTestSuite('EntrepotTest');
         require_once dirname(__FILE__).'/MouvementStockTest.php';
@@ -192,7 +194,7 @@ class AllTests
 
         require_once dirname(__FILE__).'/RestAPIUserTest.php';
         $suite->addTestSuite('RestAPIUserTest');
- 
+
         require_once dirname(__FILE__).'/WebservicesProductsTest.php';
         $suite->addTestSuite('WebservicesProductsTest');
         require_once dirname(__FILE__).'/WebservicesInvoicesTest.php';
diff --git a/test/phpunit/CompanyLibTest.php b/test/phpunit/CompanyLibTest.php
new file mode 100644
index 00000000000..1d61d5925e3
--- /dev/null
+++ b/test/phpunit/CompanyLibTest.php
@@ -0,0 +1,144 @@
+<?php
+/* Copyright (C) 2017 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 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 <http://www.gnu.org/licenses/>.
+ * or see http://www.gnu.org/
+ */
+
+/**
+ *      \file       test/phpunit/CompanyLibTest.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/core/lib/company.lib.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 CompanyLibTest extends PHPUnit_Framework_TestCase
+{
+    protected $savconf;
+    protected $savuser;
+    protected $savlangs;
+    protected $savdb;
+
+    /**
+     * Constructor
+     * We save global variables into local variables
+     *
+     * @return AdminLibTest
+     */
+    function __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";
+    }
+
+    // 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";
+    }
+
+    // tear down after class
+    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";
+    }
+
+    /**
+     * testNameCurrency
+     *
+     * @return	void
+     */
+    public function testNameCurrency()
+    {
+        global $conf,$user,$langs,$db;
+        $conf=$this->savconf;
+        $user=$this->savuser;
+        $langs=$this->savlangs;
+        $db=$this->savdb;
+
+        $result=currency_name('USD');
+        print __METHOD__." result=".$result."\n";
+        $this->assertEquals('United States Dollar',$result);
+
+        $outputlangs=new Translate('', $conf);
+        $outputlangs->setDefaultLang('fr_FR');
+        $outputlangs->load("dict");
+
+        $result=currency_name('USD', 1, $outputlangs);
+        print __METHOD__." result=".$result."\n";
+        $this->assertEquals('USD - Dollars US',$result);
+
+        return $result;
+    }
+}
-- 
GitLab