Add php unit tests on expense reports

This commit is contained in:
Laurent Destailleur 2017-04-14 16:50:30 +02:00
parent db73eeba5c
commit 8c07f514c8
7 changed files with 394 additions and 76 deletions

View File

@ -90,7 +90,6 @@ class DolibarrApi
// Remove linkedObjects. We should already have linkedObjectIds that avoid huge responses
unset($object->linkedObjects);
unset($object->lignes); // should be ->lines
unset($object->oldline);
unset($object->error);

View File

@ -186,8 +186,6 @@ class pdf_standard extends ModeleExpenseReport
}
}
if (isset($object->lignes) && ! isset($object->lines)) $object->lines=$object->lignes;
if (file_exists($dir))
{
// Add pdfgeneration hook

View File

@ -2141,7 +2141,7 @@ if ($action != 'create' && $action != 'edit')
print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=edit&id='.$object->id.'">'.$langs->trans('Modify').'</a></div>';
// Validate
if (count($object->lines) > 0 || count($object->lignes) > 0)
if (count($object->lines) > 0)
{
print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=save&id='.$object->id.'">'.$langs->trans('ValidateAndSubmit').'</a></div>';
}

View File

@ -182,10 +182,10 @@ class ExpenseReport extends CommonObject
$resql=$this->db->query($sql);
if (!$resql) $error++;
foreach ($this->lignes as $i => $val)
foreach ($this->lines as $i => $val)
{
$newndfline=new ExpenseReportLine($this->db);
$newndfline=$this->lignes[$i];
$newndfline=$this->lines[$i];
$newndfline->fk_expensereport=$this->id;
if ($result >= 0)
{
@ -241,11 +241,10 @@ class ExpenseReport extends CommonObject
}
else
{
$this->error=$this->db->error()." sql=".$sql;
$this->error=$this->db->lasterror()." sql=".$sql;
$this->db->rollback();
return -1;
}
}
@ -266,57 +265,58 @@ class ExpenseReport extends CommonObject
$this->db->begin();
// get extrafields so they will be clone
foreach($this->lines as $line)
//foreach($this->lines as $line)
//$line->fetch_optionals($line->rowid);
// Load source object
$objFrom = clone $this;
$this->id=0;
$this->ref = '';
$this->statut=0;
// Clear fields
$this->fk_user_author = $user->id; // Note fk_user_author is not the 'author' but the guy the expense report is for.
$this->fk_user_valid = '';
$this->date_create = '';
$this->date_creation = '';
$this->date_validation = '';
// Create clone
$result=$this->create($user);
// Load source object
$objFrom = clone $this;
$this->id=0;
$this->ref = '';
$this->status=0;
$this->fk_statut=0;
// Clear fields
$this->fk_user_author = $user->id; // Note fk_user_author is not the 'author' but the guy the expense report is for.
$this->fk_user_valid = '';
$this->date_create = '';
$this->date_creation = '';
$this->date_validation = '';
// Create clone
$result=$this->create($user);
if ($result < 0) $error++;
if (! $error)
{
// Hook of thirdparty module
if (is_object($hookmanager))
{
$parameters=array('objFrom'=>$objFrom);
$action='';
$reshook=$hookmanager->executeHooks('createFrom',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) $error++;
}
// Call trigger
$result=$this->call_trigger('EXPENSEREPORT_CLONE',$user);
if ($result < 0) $error++;
if (! $error)
{
// Hook of thirdparty module
if (is_object($hookmanager))
{
$parameters=array('objFrom'=>$objFrom);
$action='';
$reshook=$hookmanager->executeHooks('createFrom',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) $error++;
}
// Call trigger
$result=$this->call_trigger('EXPENSEREPORT_CLONE',$user);
if ($result < 0) $error++;
// End call triggers
}
unset($this->context['createfromclone']);
// End
if (! $error)
{
$this->db->commit();
return $this->id;
}
else
{
$this->db->rollback();
return -1;
}
// End call triggers
}
unset($this->context['createfromclone']);
// End
if (! $error)
{
$this->db->commit();
return $this->id;
}
else
{
$this->db->rollback();
return -1;
}
}
@ -475,7 +475,6 @@ class ExpenseReport extends CommonObject
$this->code_statut = $obj->code_statut;
$this->code_paiement = $obj->code_paiement;
$this->lignes = array(); // deprecated
$this->lines = array();
$result=$this->fetch_lines();
@ -698,6 +697,8 @@ class ExpenseReport extends CommonObject
$this->date_fin = $now;
$this->date_approve = $now;
$type_fees_id = 2; // TF_TRIP
$this->status = 5;
$this->fk_statut = 5;
@ -723,6 +724,7 @@ class ExpenseReport extends CommonObject
$line->value_unit=120;
$line->fk_expensereport=0;
$line->type_fees_code='TRA';
$line->fk_c_type_fees=$type_fees_id;
$line->projet_ref = 'ABC';
@ -748,7 +750,7 @@ class ExpenseReport extends CommonObject
$langs->load('trips');
if($user->rights->expensereport->lire) {
if ($user->rights->expensereport->lire) {
$sql = "SELECT de.fk_expensereport, de.date, de.comments, de.total_ht, de.total_ttc";
$sql.= " FROM ".MAIN_DB_PREFIX."expensereport_det as de";
@ -834,7 +836,7 @@ class ExpenseReport extends CommonObject
}
else
{
$this->error=$db->error();
$this->error=$db->lasterror();
return -1;
}
}
@ -856,8 +858,6 @@ class ExpenseReport extends CommonObject
$total_ht = 0; $total_tva = 0; $total_ttc = 0;
dol_syslog('ExpenseReport::recalculer sql='.$sql,LOG_DEBUG);
$result = $this->db->query($sql);
if($result)
{
@ -881,15 +881,15 @@ class ExpenseReport extends CommonObject
$this->db->free($result);
return 1;
else:
$this->error=$this->db->error();
dol_syslog('ExpenseReport::recalculer: Error '.$this->error,LOG_ERR);
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::recalculer: Error ".$this->error,LOG_ERR);
return -3;
endif;
}
else
{
$this->error=$this->db->error();
dol_syslog('ExpenseReport::recalculer: Error '.$this->error,LOG_ERR);
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::recalculer: Error ".$this->error,LOG_ERR);
return -3;
}
}
@ -914,7 +914,6 @@ class ExpenseReport extends CommonObject
$sql.= ' WHERE de.'.$this->fk_element.' = '.$this->id;
$sql.= ' ORDER BY de.date ASC';
dol_syslog('ExpenseReport::fetch_lines sql='.$sql, LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
@ -947,7 +946,6 @@ class ExpenseReport extends CommonObject
$deplig->projet_ref = $objp->ref_projet;
$deplig->projet_title = $objp->title_projet;
$this->lignes[$i] = $deplig;
$this->lines[$i] = $deplig;
$i++;
@ -958,7 +956,7 @@ class ExpenseReport extends CommonObject
else
{
$this->error=$this->db->lasterror();
dol_syslog('ExpenseReport::fetch_lines: Error '.$this->error, LOG_ERR);
dol_syslog(get_class($this)."::fetch_lines: Error ".$this->error, LOG_ERR);
return -3;
}
}
@ -989,7 +987,7 @@ class ExpenseReport extends CommonObject
else
{
$this->error=$this->db->error()." sql=".$sql;
dol_syslog("ExpenseReport.class::delete ".$this->error, LOG_ERR);
dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
$this->db->rollback();
return -6;
}
@ -997,7 +995,7 @@ class ExpenseReport extends CommonObject
else
{
$this->error=$this->db->error()." sql=".$sql;
dol_syslog("ExpenseReport.class::delete ".$this->error, LOG_ERR);
dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
$this->db->rollback();
return -4;
}
@ -1008,7 +1006,7 @@ class ExpenseReport extends CommonObject
*
* @param User $fuser User
* @param int $notrigger Disable triggers
* @return int <0 if KO, >0 if OK
* @return int <0 if KO, 0 if nothing done, >0 if OK
*/
function setValidate($fuser, $notrigger=0)
{
@ -1051,7 +1049,7 @@ class ExpenseReport extends CommonObject
$dirdest = $conf->expensereport->dir_output.'/'.$newref;
if (file_exists($dirsource))
{
dol_syslog(get_class($this)."::valid() rename dir ".$dirsource." into ".$dirdest);
dol_syslog(get_class($this)."::setValidate() rename dir ".$dirsource." into ".$dirdest);
if (@rename($dirsource, $dirdest))
{
@ -1116,8 +1114,10 @@ class ExpenseReport extends CommonObject
}
else
{
dol_syslog(get_class($this)."::set_save expensereport already with save status", LOG_WARNING);
dol_syslog(get_class($this)."::setValidate expensereport already with validated status", LOG_WARNING);
}
return 0;
}
/**
@ -1170,7 +1170,7 @@ class ExpenseReport extends CommonObject
*
* @param User $fuser User
* @param int $notrigger Disable triggers
* @return int <0 if KO, >0 if OK
* @return int <0 if KO, 0 if nothing done, >0 if OK
*/
function setApproved($fuser, $notrigger=0)
{
@ -1221,8 +1221,10 @@ class ExpenseReport extends CommonObject
}
else
{
dol_syslog(get_class($this)."::set_valide expensereport already with valide status", LOG_WARNING);
dol_syslog(get_class($this)."::setApproved expensereport already with approve status", LOG_WARNING);
}
return 0;
}
/**

View File

@ -176,6 +176,9 @@ class AllTests
$suite->addTestSuite('ChargeSocialesTest');
require_once dirname(__FILE__).'/HolidayTest.php';
$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';

View File

@ -264,7 +264,7 @@ class CommandeFournisseurTest extends PHPUnit_Framework_TestCase
* @param Object $localobject Supplier order
* @return void
*
* @depends testCommandeFournisseurValid
* @depends testCommandeFournisseurApprove
* The depends says test is run only if previous is ok
*/
public function testCommandeFournisseurCancel($localobject)

View File

@ -0,0 +1,316 @@
<?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 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/ExpenseReportTest.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/expensereport/class/expensereport.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 ExpenseReportTest extends PHPUnit_Framework_TestCase
{
protected $savconf;
protected $savuser;
protected $savlangs;
protected $savdb;
/**
* Constructor
* We save global variables into local variables
*
* @return ExpenseReportTest
*/
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";
//print $db->getVersion()."\n";
}
/**
* End phpunit tests
*
* @return void
*/
protected function tearDown()
{
print __METHOD__."\n";
}
/**
* testExpenseReportCreate
*
* @return void
*/
public function testExpenseReportCreate()
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
// Set supplier and product to use
$socid=1;
// Create supplier order with a too low quantity
$localobject=new ExpenseReport($db);
$localobject->initAsSpecimen(); // Init a speciment with lines
$localobject->status = 0;
$localobject->fk_statut = 0;
$localobject->date_fin = null; // Force bad value
$result=$localobject->create($user);
print __METHOD__." result=".$result."\n";
$this->assertEquals(-1, $result); // must be -1 because of missing mandatory fields
$sql="DELETE FROM ".MAIN_DB_PREFIX."expensereport where ref=''";
$db->query($sql);
// Create supplier order
$localobject2=new ExpenseReport($db);
$localobject2->initAsSpecimen(); // Init a speciment with lines
$localobject2->status = 0;
$localobject2->fk_statut = 0;
$result=$localobject2->create($user);
print __METHOD__." result=".$result."\n";
$this->assertGreaterThanOrEqual(0, $result);
return $result;
}
/**
* testExpenseReportFetch
*
* @param int $id Id of supplier order
* @return void
*
* @depends testExpenseReportCreate
* The depends says test is run only if previous is ok
*/
public function testExpenseReportFetch($id)
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new ExpenseReport($this->savdb);
$result=$localobject->fetch($id);
print __METHOD__." id=".$id." result=".$result."\n";
$this->assertLessThan($result, 0);
return $localobject;
}
/**
* testExpenseReportValid
*
* @param Object $localobject Supplier order
* @return void
*
* @depends testExpenseReportFetch
* The depends says test is run only if previous is ok
*/
public function testExpenseReportValid($localobject)
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$result=$localobject->setValidate($user);
print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0);
return $localobject;
}
/**
* testExpenseReportApprove
*
* @param Object $localobject Supplier order
* @return void
*
* @depends testExpenseReportValid
* The depends says test is run only if previous is ok
*/
public function testExpenseReportApprove($localobject)
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$result=$localobject->setApproved($user);
print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0);
return $localobject;
}
/**
* testExpenseReportCancel
*
* @param Object $localobject Supplier order
* @return void
*
* @depends testExpenseReportApprove
* The depends says test is run only if previous is ok
*/
public function testExpenseReportCancel($localobject)
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$result=$localobject->set_cancel($user, 'Because...');
print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0);
return $localobject;
}
/**
* testExpenseReportOther
*
* @param Object $localobject Supplier order
* @return void
*
* @depends testExpenseReportCancel
* The depends says test is run only if previous is ok
*/
public function testExpenseReportOther($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->id;
}
/**
* testExpenseReportDelete
*
* @param int $id Id of order
* @return void
*
* @depends testExpenseReportOther
* The depends says test is run only if previous is ok
*/
public function testExpenseReportDelete($id)
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new ExpenseReport($this->savdb);
$result=$localobject->fetch($id);
$result=$localobject->delete($user);
print __METHOD__." id=".$id." result=".$result."\n";
$this->assertLessThan($result, 0);
return $result;
}
}