Improve HttpInput::extract , the array is set and not passed anymore

This commit is contained in:
sparkyx 2021-09-30 20:45:29 +02:00
parent 5d70f21dfa
commit 0142c9cb93
9 changed files with 353 additions and 55 deletions

View file

@ -56,7 +56,7 @@ switch ($op)
catch (Exception $e)
{
record_log($e);
echo span($e->getMessage(), ' class="notice"');
echo span($e->getMessage(), ' class="warning"');
Anc_Key::display_list();
Anc_Key::key_add();
}
@ -70,7 +70,7 @@ switch ($op)
}
catch (Exception $e)
{
echo span($e->getMessage(), ' class="notice"');
echo span($e->getMessage(), ' class="warning"');
}
Anc_Key::display_list();
Anc_Key::key_add();

View file

@ -755,8 +755,9 @@ class Acc_Ledger extends jrn_def_sql
$ret.="</tr>";
}
$currency_code=$http->extract($p_array,"p_currency_code","number");
$currency_rate=$http->extract($p_array,"p_currency_rate","number");
$http->set_array($p_array);
$currency_code=$http->extract("p_currency_code","number");
$currency_rate=$http->extract("p_currency_rate","number");
$currency=new Acc_Currency($this->db,$currency_code);
$msg_currency= ($currency_code != 0 )?sprintf(_("Totaux %s (%s)"),$currency->get_code(),$currency_rate):_("Totaux");
@ -765,7 +766,7 @@ class Acc_Ledger extends jrn_def_sql
// Currency
if ( $currency_code != 0)
{
$currency_rate=$http->extract($p_array,"p_currency_rate","number");
$currency_rate=$http->extract("p_currency_rate","number");
$default_currency=new Acc_Currency($this->db,0);
$ret.=tr(td('').
@ -1102,13 +1103,14 @@ class Acc_Ledger extends jrn_def_sql
$tot_cred=0;
$tot_deb=0;
$msg=array();
$http->set_array($p_array);
/* Check currency : rate cannot be equal to 0 */
$currency_rate=$http->extract($p_array,"p_currency_rate","number");
$currency_rate=$http->extract("p_currency_rate","number");
if ( $currency_rate <=0 ) {
throw new Exception(_("Taux de conversion doit être supérieur à 0"),3);
}
/* Check currency : Does the currency parameter exist */
$currency_code=$http->extract($p_array,"p_currency_code","number");
$currency_code=$http->extract("p_currency_code","number");
$currency=new Acc_Currency($this->db,$currency_code);
$this->check_currency_setting($currency_code);
@ -1321,8 +1323,9 @@ class Acc_Ledger extends jrn_def_sql
$count=0;
// currency
$currency_code=$http->extract($p_array, "p_currency_code","number",0);
$currency_rate=$http->extract($p_array, "p_currency_rate","number",1);
$http->set_array($p_array);
$currency_code=$http->extract( "p_currency_code","number",0);
$currency_rate=$http->extract( "p_currency_rate","number",1);
$currency_rate_ref=new Acc_Currency($this->db, $currency_code);
for ($i=0; $i<$nb_item; $i++)

View file

@ -40,14 +40,14 @@ class Anc_Key
private $key; /*! the distribution key */
/**
* Return the number of keys available.
*@brief Return the number of keys available.
* Return the number of keys available for the ledger given in parameter
*
* @global $cn database connection
* @param $p_jrn number of the ledger (jrn_def.jrn_def_id
* @return number of available keys
*/
static function key_avaiable($p_jrn)
static function key_available($p_jrn)
{
global $cn;
$count=$cn->get_value (' select count(*)
@ -65,8 +65,18 @@ class Anc_Key
$this->a_activity=null;
$this->a_row=null;
}
public function get_key()
{
return $this->key;
}
/**
public function set_key($key)
{
$this->key=$key;
return $this;
}
/**
* @brief display list of available keys
* @param $p_amount amount to distribute
* @param $p_target target to update
@ -142,7 +152,11 @@ class Anc_Key
*/
function verify($p_array)
{
$a_percent=$p_array['percent'];
$http=new HttpInput();
$http->set_array($p_array);
$a_percent=$http->extract('percent',"array");
if (count($a_percent)==0)
{
throw new Exception(_('Aucune répartition'));
@ -157,7 +171,8 @@ class Anc_Key
{
throw new Exception(_('Le total ne vaut pas 100, total calculé = ').$tot_percent);
}
if ($p_array['name_key']=='') {
if (trim($http->extract('name_key'))=='') {
throw new Exception (_('Le nom ne peut être vide'));
}
}
@ -179,6 +194,8 @@ class Anc_Key
* @verbatim
'key_id' => string '1' (length=1)
"name_key" => "name"
"description_key" => "description"
'row' =>
array
0 => string '1' (length=1)
@ -220,16 +237,18 @@ class Anc_Key
$this->verify($p_array);
$cn->start();
// for each row
$a_row=$p_array['row'];
$http=new HttpInput();
$a_ledger=$http->extract($p_array,"jrn","string",array());
$a_percent=$p_array['percent'];
$a_po_id=$p_array['po_id'];
$a_plan=$p_array['pa'];
$http->set_array($p_array);
$a_row=$http->extract('row',"array");
$a_ledger=$http->extract("jrn","string",array());
$a_percent=$http->extract('percent',"array",[]);
$a_po_id=$http->extract('po_id',"array",[]);
$a_plan=$http->extract('pa',"array",[]);
try
{
$this->key->setp('name',$p_array['name_key']);
$this->key->setp('description',$p_array['description_key']);
$this->key->setp('name',$http->extract('name_key'));
$this->key->setp('description',$http->extract('description_key'));
$this->key->save();
for ($i=0; $i<count($a_row); $i++)
{

View file

@ -200,11 +200,12 @@ $order
* Check needed info
*/
$http=new HttpInput();
$p_nom_mod = $http->extract($array,'nom_mod',"string","");
$p_fd_description = $http->extract($array,'fd_description',"string", "");
$p_class_base=$http->extract($array,'class_base',"string", "");
$p_fiche_def= $http->extract($array,'FICHE_REF',"string", "");
$p_create= $http->extract($array,'create',"string", "off");
$http->set_array($array);
$p_nom_mod = $http->extract('nom_mod',"string","");
$p_fd_description = $http->extract('fd_description',"string", "");
$p_class_base=$http->extract('class_base',"string", "");
$p_fiche_def= $http->extract('FICHE_REF',"string", "");
$p_create= $http->extract('create',"string", "off");
// If there is no description then add a empty one
if ( ! isset ($p_fd_description)) {

View file

@ -955,23 +955,24 @@ class Follow_Up
{
global $g_user;
$http=new HttpInput();
$this->ag_id=$http->extract($p_array,"ag_id","number",0);
$this->ag_ref=$http->extract($p_array,"ag_ref","string","");
$this->qcode_dest=$http->extract($p_array,"qcode_dest","string","");
$this->f_id_dest=$http->extract($p_array,"f_id_dest","string",null);
$this->ag_timestamp=$http->extract($p_array,"ag_timestamp","string",date('d.m.Y'));
$this->dt_id=$http->extract($p_array,"dt_id","string","");
$this->ag_state=$http->extract($p_array,"ag_state","number",2);
$this->ag_title=$http->extract($p_array,"ag_title","string","");
$this->ag_hour=$http->extract($p_array,"ag_hour","string","");
$this->ag_dest=$http->extract($p_array,"ag_dest","string",$g_user->get_profile());
$this->ag_priority=$http->extract($p_array,"ag_priority","string","2");
$this->ag_contact=$http->extract($p_array,"ag_contact","string","");
$this->ag_comment=$http->extract($p_array,"ag_comment","string","");
$this->ag_description=$http->extract($p_array,"ag_description","string","");
$this->ag_remind_date=$http->extract($p_array,"ag_remind_date","string",null);
$this->operation=$http->extract($p_array,"operation","string",null);
$this->action=$http->extract($p_array,"action","string",null);
$http->set_array($p_array);
$this->ag_id=$http->extract("ag_id","number",0);
$this->ag_ref=$http->extract("ag_ref","string","");
$this->qcode_dest=$http->extract("qcode_dest","string","");
$this->f_id_dest=$http->extract("f_id_dest","string",null);
$this->ag_timestamp=$http->extract("ag_timestamp","string",date('d.m.Y'));
$this->dt_id=$http->extract("dt_id","string","");
$this->ag_state=$http->extract("ag_state","number",2);
$this->ag_title=$http->extract("ag_title","string","");
$this->ag_hour=$http->extract("ag_hour","string","");
$this->ag_dest=$http->extract("ag_dest","string",$g_user->get_profile());
$this->ag_priority=$http->extract("ag_priority","string","2");
$this->ag_contact=$http->extract("ag_contact","string","");
$this->ag_comment=$http->extract("ag_comment","string","");
$this->ag_description=$http->extract("ag_description","string","");
$this->ag_remind_date=$http->extract("ag_remind_date","string",null);
$this->operation=$http->extract("operation","string",null);
$this->action=$http->extract("action","string",null);
}
/**

View file

@ -252,23 +252,24 @@ class HttpInput
}
}
/**
* @brief Retrieve from $p_array
* @param $p_array source
* @brief Retrieve from $p_array,
* @param $p_name name of the variable
* @param $p_type type of the variable , opt. default string
* @param $p_default default value is variable is not set
* @throws Exception if invalid
*/
function extract($p_array, $p_name, $p_type="string", $p_default="")
function extract( $p_name, $p_type="string", $p_default="")
{
try
{
$this->array=$p_array;
if (func_num_args()==2)
if ( $this->array === null ) {
throw new Exception( _("HTTP266:array not set")) ;
}
if (func_num_args()==1)
return $this->get_value($p_name);
if (func_num_args()==3)
if (func_num_args()==2)
return $this->get_value($p_name, $p_type);
if (func_num_args()==4)
if (func_num_args()==3)
return $this->get_value($p_name, $p_type, $p_default);
}
catch (Exception $exc)

View file

@ -18,7 +18,7 @@ class Anc_KeyTest extends TestCase
*/
protected function setUp()
{
$this->object=new Anc_Key;
}
/**
@ -29,6 +29,73 @@ class Anc_KeyTest extends TestCase
{
}
static function setUpBeforeClass()
{
// require_once 'global.php';
//
// global $g_connection;
// $g_connection=Dossier::connect();
}
static function tearDownAfterClass()
{
}
/**
* @brief test Key Available and save
* @global type $g_connection
* @global type $cn
* @covers Anc_Key::key_available Anc_Key::save
*/
function testKeyAvailable()
{
require_once 'global.php';
global $g_connection,$cn;
$cn=Dossier::connect();
// create several key
$key1=new Anc_Key(-1);
$key2=new Anc_Key(-1);
$key1->save(array(
"key"=>-1,
'name_key'=>'Test.Key1',
"description_key"=>"Description de la clef",
"row"=>[-1,-1,-1,-1],
"pa"=>[1],
"po_id"=>array(
[1],
[2],
[3],
[4],
),
"percent"=>[10,20,30,40],
"jrn"=>[1,152,83]
));
$key2->save(array(
"key"=>-1,
'name_key'=>'Test.Key2',
"description_key"=>"Description de la clef",
"row"=>[-1,-1,-1,-1],
"pa"=>[1],
"po_id"=>array(
[1],
[2],
[3],
[4],
),
"percent"=>[10,20,30,40],
"jrn"=>[152]
));
$this->assertGreaterThan(-1,$key1->get_key()->getp("id")," Key not created");
$this->assertEquals(1,$key1::key_available(1),"Ledger 1 incorrect number of key");
$this->assertEquals(2,$key1::key_available(152),"Ledger 152 incorrect number of key");
$this->assertEquals(1,$key1::key_available(83),"Ledger 83 incorrect number of key");
$this->assertEquals(0,$key1::key_available(2),"Ledger 2 incorrect number of key");
$key1->delete();
$key2->delete();
$this->assertEquals(1,$cn->get_value("select count(*) from key_distribution"),"ANC_KEY Cannot delete keys");
}
}

View file

@ -0,0 +1,201 @@
<?php
use PHPUnit\Framework\TestCase;
/*
* This file is part of NOALYSS.
*
* PhpCompta 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.
*
* PhpCompta 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 PhpCompta; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// Copyright (2002-2021) Author Dany De Bontridder <danydb@noalyss.eu>
/**
* @file
* @brief PHPUNIT test the class fiche_def
*/
/**
* @covers Fiche_Def
* @backupGlobals enabled
*/
class Fiche_DefTest extends Testcase
{
protected $fiche_def;
static function setUpBeforeClass()
{
require_once 'global.php';
global $g_connection;
$g_connection=Dossier::connect();
$fd_id=$g_connection->get_value("select fd_id from fiche_def where fd_label=$1", ["Test.card"]);
if ($g_connection->count()>0)
{
// clean
$g_connection->exec_sql("delete from fiche_detail
where f_id in (select f_id from fiche where fd_id=$1)", [$fd_id]);
$g_connection->exec_sql("delete from fiche where fd_id=$1", [$fd_id]);
$g_connection->exec_sql("delete from jnt_fic_attr where fd_id=$1", [$fd_id]);
$g_connection->exec_sql("delete from fiche_def where fd_id=$1", [$fd_id]);
}
// add to this new categorie all the possible attributes
}
/**
* @brief clean after testing it
* @global type $g_connection
*/
static function tearDownAfterClass()
{
require_once 'global.php';
global $g_connection;
$g_connection=Dossier::connect();
$fd_id=$g_connection->get_value("select fd_id from fiche_def where fd_label=$1", ["Test.card"]);
if ($g_connection->count()>0)
{
// clean
$g_connection->exec_sql("delete from fiche_detail
where f_id in (select f_id from fiche where fd_id=$1)", [$fd_id]);
$g_connection->exec_sql("delete from fiche where fd_id=$1", [$fd_id]);
$g_connection->exec_sql("delete from jnt_fic_attr where fd_id=$1", [$fd_id]);
$g_connection->exec_sql("delete from fiche_def where fd_id=$1", [$fd_id]);
}
}
protected function setUp()
{
include 'global.php';
}
function testCreateNewCategory()
{
global $g_connection;
// create a category of card, type Charges
$fiche_def=new Fiche_Def($g_connection);
$aParam=["nom_mod"=>"Test.card", "fd_description"=>'PHPUNIT test', 'class_base'=>'600', 'FICHE_REF'=>3,
'create'=>'on'];
$this->assertEquals($fiche_def->id, 0, 'Before created');
$fiche_def->add($aParam);
$this->assertLessThan($fiche_def->id, 0, 'After created');
$this->assertEquals($g_connection->get_value("select count(*) from fiche_def where fd_id=$1", [$fiche_def->id]),
1, "Category created");
$this->fiche_def=$fiche_def;
}
function getFicheDef()
{
global $g_connection;
$fd_id=$g_connection->get_value("select fd_id from fiche_def where fd_label=$1", ["Test.card"]);
$this->assertNotEquals($g_connection->count(),0,"Card category NOT found") ;
$fiche_def=new Fiche_Def($g_connection,$fd_id);
$this->fiche_def=$fiche_def;
return $fiche_def;
}
/**
* @covers hasAttribute
* @testdox Test the Fiche_Def->hasAttribute function
*/
function testHasAttribute()
{
global $g_connection;
$fiche_def=$this->getFicheDef();
$aProperty=$g_connection->get_array("select ad_id from jnt_fic_attr where fd_id=$1", [$fiche_def->id]);
$this->assertFalse(empty($aProperty),"default attribute created");
$this->assertGreaterThan(0, count($aProperty));
foreach ($aProperty as $row)
{
$this->assertTrue($this->fiche_def->HasAttribute($row['ad_id']),
$row['ad_id'] ."this attribute is supposed to exist");
}
$aProperty=$g_connection->get_array("select distinct ad_id from jnt_fic_attr where
ad_id not in ( select ad_id from jnt_fic_attr where
fd_id=$1)",
[$this->fiche_def->id]);
foreach ($aProperty as $row)
{
$this->assertFalse($this->fiche_def->HasAttribute($row['ad_id']),
$row['ad_id'] ." this attribute does not exist");
}
}
/**
*
* @testdox Add new Attributes and remove them
*
*/
function testAddNewAttribute()
{
global $g_connection;
$fiche_def=$this->getFicheDef();
$aProperty=$g_connection->get_array("select distinct ad_id from jnt_fic_attr where
ad_id not in ( select ad_id from jnt_fic_attr where
fd_id=$1)",
[$this->fiche_def->id]);
foreach ($aProperty as $row)
{
$this->assertFalse($this->fiche_def->HasAttribute($row['ad_id']), $row['ad_id'] ." not existing attribute");
$fiche_def->insertAttribut($row['ad_id']);
$this->assertTrue($this->fiche_def->HasAttribute($row['ad_id']), $row['ad_id'] ." is now an existing attribute");
}
foreach($aProperty as $row) {
$fiche_def->removeAttribut([ $row['ad_id'] ]);
$this->assertFalse($this->fiche_def->HasAttribute($row['ad_id']), $row['ad_id'] ." cannot be removed ");
}
}
/**
* @testdox getAttribut
*/
function testGetAttribut()
{
global $g_connection;
$fiche_def=$this->getFicheDef();
$aProperty=$g_connection->get_array("select ad_id from jnt_fic_attr where fd_id=$1", [$fiche_def->id]);
$this->assertEquals(count($fiche_def->getAttribut()),count($aProperty)," number of property different in db and function getAttribut");
}
/**
* @testdox if we insert a attribut ; all the cards from this category will have these attributes
*/
function testCardAttribute()
{
global $g_connection;
$fiche_def=new Fiche_Def($g_connection,5);
$fiche_def->RemoveAttribut([20, 21, 22, 51, 52, 53]);
$nbProperty=$g_connection->get_value("select count(ad_id) from jnt_fic_attr where fd_id=$1", [$fiche_def->id]);
$nbCard=$g_connection->get_value("select count(f_id) from fiche where fd_id=$1", [$fiche_def->id]);
// percent deductible
$fiche_def->InsertAttribut(20);
$fiche_def->InsertAttribut(21);
$fiche_def->InsertAttribut(22);
// accouting for not deductible
$fiche_def->InsertAttribut(51);
$fiche_def->InsertAttribut(52);
$fiche_def->InsertAttribut(53);
// check that all card has these attributes
$nbAttribut=$nbProperty*$nbCard+$nbCard*6;
$this->assertEquals($nbAttribut,
$g_connection->get_value("select count(*) from fiche_detail join fiche using (f_id)
where fd_id=$1",[$fiche_def->id]), "must have 6 new attributes");
$fiche_def->RemoveAttribut([20, 21, 22, 51, 52, 53]);
}
}

View file

@ -5,6 +5,7 @@ help(){
echo " -d folder to test "
echo " -i function , optional filter to a function"
echo " -c in the folder coverage , show the coverage"
echo " -x load php.ini file"
}
cd `dirname $0`
@ -15,8 +16,9 @@ FILETOTEST=""
FUNCTION=""
COVERAGE=""
FOLDERTEST=""
PHPINI="php "
while getopts "f:i:cd:" opt; do
while getopts "f:i:cd:x" opt; do
case $opt in
d)
FOLDERTEST=$OPTARG
@ -29,6 +31,9 @@ while getopts "f:i:cd:" opt; do
;;
c) COVERAGE="--whitelist=../include --coverage-html=coverage"
;;
x)
PHPINI=$PHPINI" -c php.ini "
;;
*)
help
exit 1
@ -49,7 +54,7 @@ fi
if [ ! -z "$FOLDERTEST" -a -d "$FOLDERTEST" ] ; then
$PHPUNIT --bootstrap bootstrap.php $COVERAGE --testdox --color $FOLDERTEST
$PHPINI $PHPUNIT --bootstrap bootstrap.php $COVERAGE --testdox --color $FOLDERTEST
exit $?
fi
@ -60,12 +65,12 @@ fi
if [ ! -z "$FUNCTION" ] ; then
echo "testing $FILETOTEST $FUNCTION"
$PHPUNIT --bootstrap $CUR_DIR/bootstrap.php --verbose --color --filter $FUNCTION $FILETOTEST
$PHPINI $PHPUNIT --bootstrap $CUR_DIR/bootstrap.php --verbose --color --filter $FUNCTION $FILETOTEST
else
# $PHPUNIT --bootstrap bootstrap.php --whitelist $FILETOTEST --coverage-text=${FILETOTEST%.php}.txt --color $FILETOTEST
# $PHPUNIT --bootstrap $CUR_DIR/bootstrap.php --whitelist=$CUR_DIR/../include/class --coverage-html=coverage --color $FILETOTEST
$PHPUNIT --bootstrap bootstrap.php $COVERAGE $FILETOTEST --testdox --color $FILETOTEST
$PHPINI $PHPUNIT --bootstrap bootstrap.php $COVERAGE $FILETOTEST --testdox --color $FILETOTEST
#$PHPUNIT --bootstrap bootstrap.php --whitelist=../include --coverage-html=coverage $FILETOTEST --testdox-html ${FILETOTEST%.php}-testdox.html --color $FILETOTEST
fi