noalyss-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Noalyss-commit] [noalyss] 01/02: Improve PHPUNIT tests for Fiche + impr


From: Dany De Bontridder
Subject: [Noalyss-commit] [noalyss] 01/02: Improve PHPUNIT tests for Fiche + improve code and fix potential bug
Date: Thu, 19 Aug 2021 05:53:03 -0400 (EDT)

sparkyx pushed a commit to branch master
in repository noalyss.

commit 6e96c8a9d5978364f9a90394c8c53368a501fe7d
Author: Dany wm De Bontridder <danydb@noalyss.eu>
AuthorDate: Wed Aug 18 17:04:27 2021 +0200

    Improve PHPUNIT tests for Fiche + improve code and fix potential bug
---
 include/class/card_property.class.php  |   1 +
 include/class/fiche.class.php          |  15 +-
 include/constant.php                   |   2 +-
 include/sql/patch/upgrade165.sql       | 167 +++++++++++++++
 unit-test/include/class/fiche.Test.php | 376 +++++++++++++++++++++++++--------
 5 files changed, 474 insertions(+), 87 deletions(-)

diff --git a/include/class/card_property.class.php 
b/include/class/card_property.class.php
index ae37d74..69bc864 100644
--- a/include/class/card_property.class.php
+++ b/include/class/card_property.class.php
@@ -435,6 +435,7 @@ class Card_Property
                     array($p_fiche->get_f_enable(), $p_fiche->id));
 
             $name = $p_fiche->strAttribut(ATTR_DEF_NAME);
+
             // parse the attribute
             foreach ($p_fiche->attribut as $value)
             {
diff --git a/include/class/fiche.class.php b/include/class/fiche.class.php
index 53a7958..a5033a9 100644
--- a/include/class/fiche.class.php
+++ b/include/class/fiche.class.php
@@ -163,8 +163,8 @@ class Fiche
     /**
      *@brief set an attribute by a value, if the attribut array is empty
      * a call to getAttribut is performed
-     *@param int  AD_ID
-     *@param int value
+     *@param int  AD_ID attr_def.ad_id
+     *@param int value value of this attribute
      *@see constant.php table: attr_def
      */
     function setAttribut($p_ad_id,$p_value)
@@ -442,6 +442,15 @@ class Fiche
             }
             $Ret=$this->cn->exec_sql("insert into fiche(f_id,f_enable,fd_id) 
values ($1,$2,$3)",
                     array($fiche_id, $p_array['f_enable'], $p_fiche_def));
+            // first we need to save the name , to compute properly the 
quickcode
+            if ( empty ($p_array['av_text'.ATTR_DEF_NAME])) {
+
+                $p_array['av_text'.ATTR_DEF_NAME]=_("Nom vide");
+            }
+            // the name must be saved first
+            $this->cn->exec_sql("insert into fiche_detail 
(f_id,ad_id,ad_value) values ($1,$2,$3)",
+                array($fiche_id,1,$p_array['av_text'.ATTR_DEF_NAME]));
+
             // compute a quick_code
             if (!isset($p_array["av_text".ATTR_DEF_QUICKCODE]))
             {
@@ -449,7 +458,7 @@ class Fiche
             }
             $sql=sprintf("select insert_quick_code(%d,'%s')", $fiche_id,
                     sql_string($p_array['av_text'.ATTR_DEF_QUICKCODE]));
-            
+            $this->cn->exec_sql($sql);
             // get the card properties for this card category
             $fiche_def=new Fiche_Def($this->cn, $p_fiche_def);
             
diff --git a/include/constant.php b/include/constant.php
index b43ff47..ab35454 100644
--- a/include/constant.php
+++ b/include/constant.php
@@ -116,7 +116,7 @@ if ( !defined ("NOALYSS_PACKAGE_REPOSITORY")) {
 if ( ! defined ("SYSINFO_DISPLAY")) {
     define ("SYSINFO_DISPLAY",TRUE);
 }
-define ("DBVERSION",165);
+define ("DBVERSION",166);
 define ("MONO_DATABASE",25);
 define ("DBVERSIONREPO",20);
 define ('NOTFOUND','--not found--');
diff --git a/include/sql/patch/upgrade165.sql b/include/sql/patch/upgrade165.sql
new file mode 100644
index 0000000..ac60229
--- /dev/null
+++ b/include/sql/patch/upgrade165.sql
@@ -0,0 +1,167 @@
+-- noinspection SqlNoDataSourceInspectionForFile
+
+begin;
+
+CREATE OR REPLACE FUNCTION comptaproc.format_quickcode(p_qcode text)
+    RETURNS text
+AS $function$
+declare
+    tText text;
+BEGIN
+    tText := lower(trim(p_qcode));
+    tText := replace(tText,' ','');
+    tText:= translate(tText,E' $€µ£%+/\\!(){}(),;&|"#''^<>*','');
+    tText := translate(tText,E'éèêëàâäïîüûùöôç','eeeeaaaiiuuuooc');
+
+    return upper(tText);
+END;
+$function$
+LANGUAGE plpgsql;
+
+COMMENT ON FUNCTION comptaproc.format_quickcode(text) IS 'Put in upper case 
and remove invalid char';
+
+CREATE OR REPLACE FUNCTION comptaproc.insert_quick_code(nf_id integer, 
tav_text text)
+    RETURNS integer
+AS $function$
+declare
+    ns integer;
+    nExist integer;
+    tText text;
+    tBase text;
+    tName text;
+    nCount Integer;
+    nDuplicate Integer;
+begin
+    tText := comptaproc.format_quickcode(tav_text);
+    nDuplicate := 0;
+    tBase := tText;
+    -- take the next sequence
+    select nextval('s_jnt_fic_att_value') into ns;
+    loop
+        if length (tText) = 0 or tText is null then
+            select count(*) into nCount from fiche_detail where f_id=nf_id and 
ad_id=1;
+            if nCount = 0 then
+                tBase := 'CRD';
+            else
+                select ad_value into tName from fiche_detail where f_id=nf_id 
and ad_id=1;
+                tName := comptaproc.format_quickcode(tName);
+                tName := substr(tName,1,6);
+                tBase := tName;
+                if nDuplicate = 0 then
+                    tText := tName;
+                else
+                    tText := tBase||nDuplicate::text;
+                end if;
+            end if;
+        end if;
+        if coalesce(tText,'') = '' then
+            tText := 'CRD';
+        end if;
+        -- av_text already used ?
+        select count(*) into nExist
+        from fiche_detail
+        where
+                ad_id=23 and  ad_value=tText;
+
+        if nExist = 0 then
+            exit;
+        end if;
+        nDuplicate := nDuplicate + 1 ;
+        tText := tBase || nDuplicate::text;
+
+        if nDuplicate > 99999 then
+            raise Exception 'too many duplicate % duplicate# 
%',tText,nDuplicate;
+        end if;
+    end loop;
+
+
+    insert into fiche_detail(jft_id,f_id,ad_id,ad_value) values 
(ns,nf_id,23,upper(tText));
+    return ns;
+end;
+$function$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION comptaproc.update_quick_code(njft_id integer, 
tav_text text)
+    RETURNS integer
+AS $function$
+declare
+    ns integer;
+    nExist integer;
+    tText text;
+    tBase text;
+    old_qcode varchar;
+    num_rows_jrnx integer;
+    num_rows_predef integer;
+    n_count integer;
+begin
+    n_count := 0;
+    -- get current value
+    select ad_value into old_qcode from fiche_detail where jft_id=njft_id;
+    -- av_text didn't change so no update
+    if tav_text = upper( trim(old_qcode)) then
+        raise notice 'nothing to change % %' , tav_text,old_qcode;
+        return 0;
+    end if;
+
+    tText := comptaproc.format_quickcode(tav_text);
+
+    if length ( tText) = 0 or tText is null then
+        return 0;
+    end if;
+
+    ns := njft_id;
+    tBase := tText;
+    loop
+        -- av_text already used ?
+        select count(*) into nExist
+        from fiche_detail
+        where
+                ad_id=23 and ad_value=tText
+          and jft_id <> njft_id;
+
+        if nExist = 0 then
+            exit;
+        end if;
+        tText := tBase || n_count::text;
+        n_count := n_count + 1 ;
+
+    end loop;
+    update fiche_detail set ad_value = tText where jft_id=njft_id;
+
+    -- update also the contact
+    update fiche_detail set ad_value = tText
+    where jft_id in
+          ( select jft_id
+            from fiche_detail
+            where ad_id in (select ad_id from attr_def where ad_type='card') 
and ad_value=old_qcode);
+
+
+    return ns;
+end;
+$function$
+    LANGUAGE plpgsql;
+
+COMMENT ON FUNCTION comptaproc.update_quick_code(int4,text) IS 'update the 
qcode + related columns in other cards';
+CREATE OR REPLACE FUNCTION comptaproc.fiche_detail_check()
+ RETURNS trigger
+AS $function$
+       BEGIN
+               if new.ad_id = 23 and coalesce (new.ad_value,'') = '' then 
+                       raise exception  'QUICKCODE can not be empty';
+               end if;
+       if new.ad_id = 1 and coalesce (new.ad_value,'') = '' then 
+                       raise exception  'NAME can not be empty';
+               end if;
+       return new;
+       END;
+
+$function$
+LANGUAGE plpgsql;
+
+create trigger fiche_detail_check_trg before update or insert 
+    on
+    public.fiche_detail for each row execute function 
comptaproc.fiche_detail_check() ;
+
+
+insert into version (val,v_description) values (166,'Fix bug for card with 
empty name or quickcode');
+commit ;
diff --git a/unit-test/include/class/fiche.Test.php 
b/unit-test/include/class/fiche.Test.php
index cabd2d0..c46ecb3 100644
--- a/unit-test/include/class/fiche.Test.php
+++ b/unit-test/include/class/fiche.Test.php
@@ -23,7 +23,7 @@ class FicheTest extends TestCase
     protected function setUp()
     {
         include 'global.php';
-        $this->object=new Fiche($g_connection);
+        $this->object = new Fiche($g_connection);
     }
 
     /**
@@ -32,13 +32,13 @@ class FicheTest extends TestCase
      */
     protected function tearDown()
     {
-        
+
     }
 
     static function tearDownAfterClass()
     {
         include 'global.php';
-        $fiche_def=new Fiche_Def($g_connection, 5);
+        $fiche_def = new Fiche_Def($g_connection, 5);
         // prepare test , clean 
         $fiche_def->RemoveAttribut([20, 21, 22, 51, 52, 53]);
     }
@@ -49,8 +49,8 @@ class FicheTest extends TestCase
     public function testCmp_name()
     {
         global $g_connection;
-        $fiche=new \Fiche($g_connection, 21);
-        $fiche_2=new \Fiche($g_connection, 25);
+        $fiche = new \Fiche($g_connection, 21);
+        $fiche_2 = new \Fiche($g_connection, 25);
         $this->assertGreaterThan(\Fiche::cmp_name($fiche, $fiche_2), 0);
     }
 
@@ -60,10 +60,10 @@ class FicheTest extends TestCase
     public function testGet_bk_account()
     {
         include 'global.php';
-        $this->object=new Fiche($g_connection);
-        $result=$this->object->get_bk_account();
+        $this->object = new Fiche($g_connection);
+        $result = $this->object->get_bk_account();
         $this->assertEquals(gettype($result), 'array');
-        $count=count($result);
+        $count = count($result);
         $this->assertGreaterThan(0, $count);
     }
 
@@ -73,25 +73,22 @@ class FicheTest extends TestCase
     public function testGet_row()
     {
         include 'global.php';
-        $card_count=$g_connection->get_array("select count(*),f_id ".
-                " from jrnx ".
-                " where ".
-                " f_id is not null ".
-                "group by f_id order by count(*) desc");
-        $a=new Fiche($g_connection, $card_count[0]['f_id']);
-        try
-        {
+        $card_count = $g_connection->get_array("select count(*),f_id " .
+            " from jrnx " .
+            " where " .
+            " f_id is not null " .
+            "group by f_id order by count(*) desc");
+        $a = new Fiche($g_connection, $card_count[0]['f_id']);
+        try {
             $a->get_row(235, 238);
             $this->assertFalse(TRUE, "Exception periode invalide");
-        }
-        catch (\Exception $e)
-        {
+        } catch (\Exception $e) {
             $this->assertTrue(TRUE);
         }
-        $a_result=$a->get_row(92, 131);
+        $a_result = $a->get_row(92, 131);
         // Size == 25
 
-        $nb_result=count($a_result);
+        $nb_result = count($a_result);
         $this->assertEquals($nb_result, 3, "Size array not correct ");
         $this->assertEquals(198.74, $a_result[0][13]["deb_montant"], "Debit 
from operation 12");
     }
@@ -102,26 +99,23 @@ class FicheTest extends TestCase
     public function testCount_by_modele()
     {
 
-        $nb=$this->object->count_by_modele(1, "", "");
+        $nb = $this->object->count_by_modele(1, "", "");
         $this->assertEquals(4, $nb, "number of Sales Card ");
-        $nb=$this->object->count_by_modele(3, "eau", "");
+        $nb = $this->object->count_by_modele(3, "eau", "");
         $this->assertEquals(1, $nb, "Purchase card water ");
-        $nb=$this->object->count_by_modele(3, "EAU", "");
+        $nb = $this->object->count_by_modele(3, "EAU", "");
         $this->assertEquals(1, $nb, "Purchase card water ");
-        $nb=$this->object->count_by_modele(3, "ZZ", "");
+        $nb = $this->object->count_by_modele(3, "ZZ", "");
         $this->assertEquals(0, $nb, "no  card  found");
-        $nb=$this->object->count_by_modele(3000, "", "");
+        $nb = $this->object->count_by_modele(3000, "", "");
         $this->assertEquals(0, $nb, "no  card found");
-        $nb=$this->object->count_by_modele(3, "", "");
+        $nb = $this->object->count_by_modele(3, "", "");
         $this->assertEquals(7, $nb, "Purchase cards ");
         // attempt to inject SQL command, you must get an error
-        try
-        {
-            $nb=@$this->object->count_by_modele(3, "", " ;delete from jrn;");
+        try {
+            $nb = @$this->object->count_by_modele(3, "", " ;delete from jrn;");
             $this->assertFalse(true, "Inject SQL command not found");
-        }
-        catch (Exception $e)
-        {
+        } catch (Exception $e) {
             $this->assertTrue(true, "Inject SQL command found");
         }
     }
@@ -133,26 +127,27 @@ class FicheTest extends TestCase
      */
     public function testSummary()
     {
-        $_REQUEST['ac']="CARD";
-        $this->object->fiche_def_ref=-1;
-        $r=$this->object->summary();
+        $_REQUEST['ac'] = "CARD";
+        $this->object->fiche_def_ref = -1;
+        $r = $this->object->summary();
         $this->assertEquals('', $r);
-        $this->object->fiche_def_ref=9;
-        $r=$this->object->summary();
+        $this->object->fiche_def_ref = 9;
+        $r = $this->object->summary();
         $this->assertContains('</TABLE>', $r);
     }
+
     /**
-     * 
+     *
      *
      */
     function testFicheDefInsertAttribut()
     {
         global $g_connection;
-        $fiche_def=new Fiche_Def($g_connection, 5);
+        $fiche_def = new Fiche_Def($g_connection, 5);
         // prepare test , clean 
         $fiche_def->RemoveAttribut([20, 21, 22, 51, 52, 53]);
         $this->assertEquals(35,
-                $g_connection->get_value("select count(*) from fiche_detail 
join fiche using (f_id)
+            $g_connection->get_value("select count(*) from fiche_detail join 
fiche using (f_id)
                 where fd_id=5"), "Efface 6 attributs");
 
         // percent deductible
@@ -167,24 +162,24 @@ class FicheTest extends TestCase
 
         // check that all card has these attributes
         $this->assertEquals(77,
-                $g_connection->get_value("select count(*) from fiche_detail 
join fiche using (f_id)
+            $g_connection->get_value("select count(*) from fiche_detail join 
fiche using (f_id)
                 where fd_id=5"), "Ajout 6 attributs");
     }
 
     /**
-     * @covers Fiche_Def::RemoveAttribut
+     * @covers  Fiche_Def::RemoveAttribut
      * @depends testFicheDefInsertAttribut
      */
     function testFicheDefRemoveAttribut()
     {
         global $g_connection;
-        $fiche_def=new Fiche_Def($g_connection, 5);
+        $fiche_def = new Fiche_Def($g_connection, 5);
 
         // prepare test , clean 
         $fiche_def->RemoveAttribut([20, 21, 22, 51, 52, 53]);
 
         $this->assertEquals(35,
-                $g_connection->get_value("select count(*) from fiche_detail 
join fiche using (f_id)
+            $g_connection->get_value("select count(*) from fiche_detail join 
fiche using (f_id)
                 where fd_id=5"), "Efface 6 attributs");
     }
 
@@ -202,40 +197,37 @@ class FicheTest extends TestCase
         // modify attribute for card category , add VAT non ded, Tax non ded , 
VAT completely non ded 0%
         // category Misc Services & goods (5)
         //-- modify card 29 : ELECTR
-        $fiche=new Fiche($g_connection, 29);
+        $fiche = new Fiche($g_connection, 29);
         $fiche->set_f_enable("1");
         $fiche->setAttribut(20, "33.33");
-        $a_attribut=$fiche->to_array();
+        $a_attribut = $fiche->to_array();
         $this->assertEquals($a_attribut['av_text20'], 33.33, "Attribut 20 set 
to 33%");
 
         $fiche->update($a_attribut);
 
         $this->assertEquals("33.33",
-                $g_connection->get_value("select ad_value from fiche_detail 
where f_id=$1 and ad_id=$2", [29, 20]),
-                "Attribut ad_id 20 inserted");
+            $g_connection->get_value("select ad_value from fiche_detail where 
f_id=$1 and ad_id=$2", [29, 20]),
+            "Attribut ad_id 20 inserted");
 
         $this->assertEquals("33.33", $fiche->strAttribut(20), "retrieve 
attribute 20");
         $fiche->setAttribut(20, "0.05");
         Card_Property::update($fiche);
-         $this->assertEquals("0.05",
-                $g_connection->get_value("select ad_value from fiche_detail 
where f_id=$1 and ad_id=$2", [29, 20]),
-                "Attribut ad_id 20 updated");
+        $this->assertEquals("0.05",
+            $g_connection->get_value("select ad_value from fiche_detail where 
f_id=$1 and ad_id=$2", [29, 20]),
+            "Attribut ad_id 20 updated");
     }
 
     function testInexistantCard()
     {
         global $g_connection;
-        $last_card=$g_connection->get_next_seq('s_fiche');
-        $last_card=$g_connection->get_next_seq('s_fiche');
-        $inexistant_fiche=new Fiche($g_connection, $last_card+2000);
-        $_POST['av_text1']='not exist';
-        try
-        {
+        $last_card = $g_connection->get_next_seq('s_fiche');
+        $last_card = $g_connection->get_next_seq('s_fiche');
+        $inexistant_fiche = new Fiche($g_connection, $last_card + 2000);
+        $_POST['av_text1'] = 'not exist';
+        try {
             $inexistant_fiche->update();
             $this->assertTrue(false, 'Inexistant card not detected');
-        }
-        catch (Exception $e)
-        {
+        } catch (Exception $e) {
             $this->assertTrue(true, "Inexistant card properly detected");
         }
     }
@@ -247,7 +239,7 @@ class FicheTest extends TestCase
     {
         global $g_connection;
 
-        $fiche_goods=new Fiche($g_connection);
+        $fiche_goods = new Fiche($g_connection);
         $fiche_goods->get_by_qcode("MARCHA");
         $this->assertEquals($fiche_goods->id, 23, "retrieve card by qcode");
         $this->assertEquals($fiche_goods->strAttribut(ATTR_DEF_NAME), 
"Marchandise1", "Retrieve name");
@@ -260,44 +252,262 @@ class FicheTest extends TestCase
     public function testInsertAndRemoveCard()
     {
         global $g_connection;
-        $last_card=$g_connection->get_next_seq('s_fiche');
-        $nb_fiche=$g_connection->get_value("select count(*) from fiche");
-        $new_fiche=new Fiche($g_connection);
-        $aProperty=array('av_text1'=>'Nom', 'av_text23'=>'ZZZTEST');
+        $last_card = $g_connection->get_next_seq('s_fiche');
+        $nb_fiche = $g_connection->get_value("select count(*) from fiche");
+        $new_fiche = new Fiche($g_connection);
+        $aProperty = array('av_text1' => 'Nom', 'av_text23' => 'ZZZTEST');
         $new_fiche->insert(5, $aProperty);
-        $this->assertFalse(empty($new_fiche->attribut)," attributes array is 
empty");
-        
+        $this->assertFalse(empty($new_fiche->attribut), " attributes array is 
empty");
+
         $this->assertGreaterThan($last_card, $new_fiche->id, 'card created');
-        $nb_fiche_after=$g_connection->get_value("select count(*) from fiche");
+        $nb_fiche_after = $g_connection->get_value("select count(*) from 
fiche");
         $this->assertGreaterThan($nb_fiche, $nb_fiche_after, 'card created');
         $this->assertEquals("1", $new_fiche->get_f_enable(), "By default 
enable");
         $new_fiche->remove();
-        $nb_fiche_after=$g_connection->get_value("select count(*) from fiche");
+        $nb_fiche_after = $g_connection->get_value("select count(*) from 
fiche");
         $this->assertEquals($nb_fiche, $nb_fiche_after, 'card removed');
     }
+
     /**
      * @testdox fiche->display
      */
     public function testDisplay()
     {
         global $g_connection;
-        $last_card=$g_connection->get_next_seq('s_fiche');
-        $nb_fiche=$g_connection->get_value("select count(*) from fiche");
-        
-        $new_fiche=new Fiche($g_connection);
-        $aProperty=array('av_text1'=>'Nom', 'av_text23'=>'ZZZTEST2');
+        $last_card = $g_connection->get_next_seq('s_fiche');
+        $nb_fiche = $g_connection->get_value("select count(*) from fiche");
+
+        $new_fiche = new Fiche($g_connection);
+        $aProperty = array('av_text1' => 'Nom', 'av_text23' => 'ZZZTEST2');
         $new_fiche->insert(5, $aProperty);
-        
-        $this->assertFalse(empty($new_fiche->attribut)," attributes array is 
empty");
+
+        $this->assertFalse(empty($new_fiche->attribut), " attributes array is 
empty");
         $this->assertGreaterThan($last_card, $new_fiche->id, 'card  not 
created');
         $result = $new_fiche->display(true);
-        
-        \Noalyss\Facility::save_file(__DIR__.'/file', 'testDisplay.txt', 
$result);
-        $this->assertNotEquals('FNT',$result,'card not found 
'.print_r($new_fiche,true));
-        
+
+        \Noalyss\Facility::save_file(__DIR__ . '/file', 'testDisplay.txt', 
$result);
+        $this->assertNotEquals('FNT', $result, 'card not found ' . 
print_r($new_fiche, true));
+
         $new_fiche->remove();
-        
-        $nb_fiche_after=$g_connection->get_value("select count(*) from fiche");
+
+        $nb_fiche_after = $g_connection->get_value("select count(*) from 
fiche");
         $this->assertEquals($nb_fiche, $nb_fiche_after, 'card not removed');
     }
+
+    public function dataEmptyQuickCode()
+    {
+        return array(
+            array("none", ""),
+            array("", ""),
+            array(",,,,", ""),
+            array("####", ""),
+            array("àz-5","AZ-5"),
+            array("àz-5&é","AZ-5E"),
+            array("é####àz-5","EAZ-5"),
+        );
+    }
+
+    /**
+     * @testdox Test if it is possible to insert e a empty quickcode
+     * @dataProvider dataEmptyQuickCode
+     */
+    public function testInsertEmptyQuickCode($name, $quick_code)
+    {
+        global $g_connection;
+        //insert
+        $fiche = new Fiche($g_connection);
+        $fiche->insert(2, ['av_text13' => $quick_code, "av_text1" => $name]);
+        $fiche->load();
+        $this->assertTrue($fiche->id > 0 && 
!empty(trim($fiche->strAttribut(23))), 'error : card created with empty 
quickcode');
+        $fiche->remove();
+
+    }
+    function dataUpdateQuickCode()
+    {
+        return array(
+            array('a+z+1-5', 'AZ1-5'),
+            array('///a+z+1-5', 'AZ1-5'),
+            array('/*/a+z+1-5', 'AZ1-5'),
+            array('.2a+z+1-5', '.2AZ1-5'),
+            array('a+z+1-5', 'AZ1-5'),
+            array('ÀÉ@Ê', 'AE@E'),
+            array('ça&"', 'CA'),
+            array("", "QC"),
+            array(",,,,", "QC"),
+            array("####", "QC")
+        );
+    }
+
+    /**
+     * @testdox Test if it is possible to update a empty quickcode
+     * @dataProvider dataUpdateQuickCode
+     */
+    public function testUpdateEmptyQuickCode($name, $quick_code)
+    {
+        global $g_connection;
+
+        //insert
+        $fiche = new Fiche($g_connection);
+        $fiche->insert(2, ['av_text23' => 'QC', "av_text1" => $name]);
+        $this->assertTrue($fiche->id > 0 && !empty($fiche->strAttribut(23)), 
'error : card created with empty quickcode');
+
+
+        // add attributes of type card , and for another card, set it to this 
quickcode
+        $fiche_def_id=$g_connection->get_value("select fd_id from fiche where 
f_id=$1 ",[$fiche->id]);
+        $fiche_def=new Fiche_Def($g_connection,$fiche_def_id);
+        $aAttribute=$g_connection->get_array("
+select ad_id from attr_def a1
+where 
+      a1.ad_id not 
+            in (select j1.ad_id from jnt_fic_attr j1 where j1.fd_id=$1)
+    and a1.ad_type = $2",[$fiche_def_id,'card']);
+        foreach ($aAttribute as $nAttribute) {
+            var_dump($nAttribute);
+
+            $fiche_def->insertAttribut($nAttribute['ad_id']);
+        }
+        // related cards
+        $fiche_related1=new Fiche($g_connection);
+        $aAttributeRelated=array(
+            "av_text23"=>"FR1",
+            'av_text1'=>'Fiche reliée'
+        );
+        foreach ($aAttribute as $nAttribute) {
+            $aAttributeRelated['av_text'.$nAttribute['ad_id']]='QC';
+        }
+        $fiche_related1->insert(2,$aAttributeRelated);
+        $aAttributeRelated['av_text1']='Fiche reliée 2';
+        $aAttributeRelated['av_text23']='FR2';
+        $fiche_related2=new Fiche($g_connection);
+        $fiche_related2->insert(2,$aAttributeRelated);
+
+        // ----
+        // check that related cards are properly created
+        // ----
+        $fiche_related1->load();
+        $fiche_related2->load();
+        $this->assertTrue($fiche_related1->id > 0 &&  
!empty($fiche_related1->strAttribut(23)),'error related card 
+        not created properly');
+        $this->assertTrue($fiche_related2->id > 0 &&  
!empty($fiche_related2->strAttribut(23)),'error related card 
+        not created properly');
+        // ----
+        // check card attribute
+        // ----
+        foreach ($aAttribute as $nAttribute) {
+            
$this->assertEquals('QC',$fiche_related1->strAttribut($nAttribute['ad_id']),'Attribute
 QC is not set');
+            
$this->assertEquals('QC',$fiche_related2->strAttribut($nAttribute['ad_id']),'Attribute
 QC is not set');
+        }
+
+        $fiche->setAttribut(23, $name);
+
+
+        $fiche->update($fiche->to_array());
+        $compare = new Fiche($g_connection, $fiche->id);
+        $this->assertEquals($quick_code, $compare->get_quick_code(), 'Error : 
quickcode not correct');
+
+        // check that the other attributes of type card have been updated
+        foreach ($aAttribute as $nAttribute) {
+            
$this->assertEquals($fiche->get_quick_code(),$fiche_related1->strAttribut($nAttribute['ad_id']),'Attribute
 QC is not set');
+            
$this->assertEquals($fiche->get_quick_code(),$fiche_related2->strAttribut($nAttribute['ad_id']),'Attribute
 QC is not set');
+        }
+        $fiche->remove();
+        $fiche_related1->remove();
+        $fiche_related2->remove();
+        foreach ($aAttribute as $nAttribute) {
+            $fiche_def->removeAttribut($nAttribute['ad_id']);
+        }
+
+
+    }
+    function dataFormat_QuickCode()
+    {
+        return array(
+            array('a+z+1-5','AZ1-5'),
+            array('///a+z+1-5','AZ1-5'),
+            array('/*/a+z+1-5','AZ1-5'),
+            array('.2a+z+1-5','.2AZ1-5'),
+            array('a+z+1-5','AZ1-5'),
+            array('ÀÉ@Ê','AE@E'),
+            array('ça&"','CA'),
+              array("", ""),
+            array(",,,,", ""),
+            array("####", ""),
+        );
+    }
+    /**
+     * @testdox test comptaproc.format_quickcode
+     * @dataProvider dataFormat_Quickcode
+     */
+    public function testFormat_QuickCode($p_qcode,$p_result)
+    {
+        global $g_connection;
+        $this->assertEquals($p_result,$g_connection->get_value( "select 
comptaproc.format_quickcode($1)",
+        [$p_result]),'quickcode is not transformed correctly');
+    }
+    function dataInsertName()
+    {
+        return array(
+            ['',"Nom vide"],
+            ['Eléphant','Eléphant'],
+            ["L'alphabet","L'alphabet"]
+        );
+    }
+    /**
+     * @testdox Test if it is possible to insert or update a empty  name
+     * @dataProvider dataInsertName
+     */
+    public function testInsertName($p_name,$p_result)
+    {
+        global $g_connection;
+
+        //insert
+        $fiche = new Fiche($g_connection);
+        $fiche->insert(2, ['av_text23' => 'QC', "av_text1" => $p_name]);
+        $fiche->load();
+        
$this->assertEquals($p_result,$fiche->strAttribut(ATTR_DEF_NAME),'incorrect 
name');
+
+    }
+    /**
+     * @testdox Test if it is possible to insert or update a empty  name
+     * @dataProvider dataInsertName
+     */
+    public function testUpdateName($p_name,$p_result)
+    {
+        global $g_connection;
+
+        //insert
+        $fiche = new Fiche($g_connection);
+        $fiche->insert(2, ['av_text23' => 'QC', "av_text1" => $p_name]);
+        $fiche->load();
+        
$this->assertEquals($p_result,$fiche->strAttribut(ATTR_DEF_NAME),'incorrect 
name');
+        $fiche->remove();
+    }
+    /**
+     * @testdox quickcode is generated and get a number if already exists
+     */
+    public function testQuickCodeNumbering()
+    {
+        global $g_connection;
+        $fiche=new Fiche($g_connection);
+        $fiche->insert(2,array("av_text1"=>'Base Card','av_text23'=>'DUP'));
+        $this->assertTrue($fiche->id > 0 && 'DUP'==$fiche->strAttribut(23),
+            'error : card created with wrong 
quickcode'.$fiche->strAttribut(23));
+        for ($i=0;$i<100;$i++) {
+            $fiche_duplicate = new Fiche($g_connection);
+            $fiche_duplicate->insert(2, array("av_text1" => 'Base Card' . $i, 
'av_text23' => 'DUP'));
+         /*   $this->assertTrue($fiche_duplicate->id > $fiche->id && 'DUP' . 
$i == $fiche_duplicate->strAttribut(23),
+                'error : card created with empty 
quickcode'.$fiche_duplicate->strAttribut(23));
+         */
+        }
+        $a_fiche_clean=$g_connection->get_array("select f_id from fiche where 
f_id >= $1",
+                    [$fiche->id]);
+        foreach($a_fiche_clean as $fiche_clean) {
+            $fiche=new Fiche($g_connection,$fiche_clean['f_id']);
+            $fiche->remove();
+            $this->assertEquals(0,
+                $g_connection->get_value("select count(*) from fiche where 
f_id=$1",
+                    array($fiche_clean['f_id']))
+                ,"Card not removed");
+        }
+    }
 }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]