noalyss-commit
[Top][All Lists]
Advanced

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

[Noalyss-commit] [noalyss] 05/46: Document : add phpunit test


From: Dany De Bontridder
Subject: [Noalyss-commit] [noalyss] 05/46: Document : add phpunit test
Date: Tue, 13 Jul 2021 05:01:48 -0400 (EDT)

sparkyx pushed a commit to branch master
in repository noalyss.

commit e942a0b0ef6e50ffe739ad808c1130769458c5d5
Author: sparkyx <danydb@noalyss.eu>
AuthorDate: Mon Jun 7 15:01:00 2021 +0200

    Document : add phpunit test
---
 include/class/document.class.php          |  26 +++--
 include/class/document_modele.class.php   |   6 +-
 include/document_modele.inc.php           |   3 +-
 include/lib/database_core.class.php       |  12 +--
 unit-test/global.php                      |   2 +
 unit-test/include/class/data/all_tags.odt | Bin 0 -> 18310 bytes
 unit-test/include/class/document.Test.php | 158 ++++++++++++++++++++++++++++++
 7 files changed, 187 insertions(+), 20 deletions(-)

diff --git a/include/class/document.class.php b/include/class/document.class.php
index b87fb36..7e24b98 100644
--- a/include/class/document.class.php
+++ b/include/class/document.class.php
@@ -84,17 +84,24 @@ class Document
      */
     function compute_filename($pj, $filename)
     {
-        foreach (array('/', '*', '<', '>', ';', ',', '\\', '.', ':', '(', ')', 
' ', '[', ']') as $i)
-        {
-            $pj=str_replace($i, "-", $pj);
-        }
-        // save the suffix
         $pos_prefix=strrpos($filename, ".");
         if ($pos_prefix==0)
             $pos_prefix=strlen($filename);
         $filename_no=substr($filename, 0, $pos_prefix);
         $filename_suff=substr($filename, $pos_prefix, strlen($filename));
+        
+        foreach (array('/', '*', '<', '>', ';', ',', '\\', '.', ':', '(', ')', 
' ', '[', ']',"'") as $i)
+        {
+            $pj=str_replace($i, "-", $pj);
+            $filename_no=str_replace($i,"-",$filename_no);
+        }
+        
+       
         $new_filename=strtolower($filename_no."-".$pj.$filename_suff);
+        $pj=str_replace("---","-",$pj);
+        $pj=str_replace("--","-",$pj);
+        $new_filename=str_replace("---","-",$new_filename);
+        $new_filename=str_replace("--","-",$new_filename);
         return $new_filename;
     }
 
@@ -596,8 +603,8 @@ class Document
 
     function get()
     {
-        $sql="select * from document where d_id=".$this->d_id;
-        $ret=$this->db->exec_sql($sql);
+        $sql="select * from document where d_id=$1";
+        $ret=$this->db->exec_sql($sql,[$this->d_id]);
         if (Database::num_row($ret)==0)
         {
             return;
@@ -1848,7 +1855,7 @@ class Document
      */
     function transform2pdf()
     {
-        if (GENERATE_PDF == 'YES' && $pdf == 'on') {
+        if (GENERATE_PDF == 'NO' ) {
             \record_log(__FILE__."DOC37 : PDF not available");
             throw new \Exception("Cannot not transform to PDF");
         }
@@ -1859,10 +1866,11 @@ class Document
         mkdir($dirname);
         $destination_file=$dirname."/".$this->d_filename;
         $this->export_file($destination_file);
+        
         passthru(OFFICE . escapeshellarg($destination_file), $status);
         if ($status != 0) {
             \record_log(__FILE__."DOC45 : Error  cannot transform into PDF");
-            throw new \Exception("Cannot not transform to PDF");
+            throw new \Exception("DOC45 Cannot not transform to PDF");
         }
         // remove extension
         $ext = strrpos($this->d_filename, ".");
diff --git a/include/class/document_modele.class.php 
b/include/class/document_modele.class.php
index a1722a9..42367ac 100644
--- a/include/class/document_modele.class.php
+++ b/include/class/document_modele.class.php
@@ -186,16 +186,16 @@ class Document_modele
                 }
                 else
                 {
-                    echo "<H1>Error</H1>";
+                    echo "<H1>"._("Erreur opération annulée")."</H1>";
                     $this->cn->rollback();
-                   throw new Exception("Erreur".__FILE__.__LINE__);
+                   throw new Exception("DM191 : error loading  
template".__FILE__.__LINE__);
                 }
             }
         }
         catch (Exception $e)
         {
               record_log($e);
-            rollback($this->cn);
+            $this->cn->rollback();
             return ;
         }
     }
diff --git a/include/document_modele.inc.php b/include/document_modele.inc.php
index 8e0ec87..5372654 100644
--- a/include/document_modele.inc.php
+++ b/include/document_modele.inc.php
@@ -23,7 +23,6 @@
 
 if ( ! defined ('ALLOWED') ) die('Appel direct ne sont pas permis');
 require_once NOALYSS_INCLUDE.'/class/document_modele.class.php';
-$sub_action=(isset ($_REQUEST['sa']))?$_REQUEST['sa']:"";
 
 $http=new HttpInput();
 
@@ -81,7 +80,7 @@ if ( $sub_action == 'mod_template')
 // Default action : Show the list
 //-----------------------------------------------------
 echo $doc->myList();
-echo '<div id="add_modele" class="inner_box" style="display:none">';
+echo '<div id="add_modele" class="inner_box" 
style="position:fixed;top:120px;display:none">';
 echo HtmlInput::title_box(_("Ajout d'un modèle"), "add_modele", "hide");
 echo $doc->form('');
 echo '</div>';
diff --git a/include/lib/database_core.class.php 
b/include/lib/database_core.class.php
index 29ef8a1..1b47b24 100644
--- a/include/lib/database_core.class.php
+++ b/include/lib/database_core.class.php
@@ -801,24 +801,24 @@ class DatabaseCore
     /**
      * \brief wrapper for the function pg_lo_export
      * \param $p_oid is the oid of the log
-     * \param $tmp  is the file
+     * \param $tmp_file  is the file
      * \return result of the operation
      */
 
-    function lo_export($p_oid, $tmp)
+    function lo_export($p_oid, $tmp_file)
     {
-        return pg_lo_export($this->db, $p_oid, $tmp);
+        return pg_lo_export($this->db, $p_oid, $tmp_file);
     }
 
     /**\brief wrapper for the function pg_lo_export
-     * \param $p_oid is the filename
+     * \param $p_filename is the filename
      * \param $tmp  is the file
      * \return result of the operation
      */
 
-    function lo_import($p_oid)
+    function lo_import($p_filename)
     {
-        return pg_lo_import($this->db, $p_oid);
+        return pg_lo_import($this->db, $p_filename);
     }
 
     /**\brief wrapper for the function pg_escape_string
diff --git a/unit-test/global.php b/unit-test/global.php
index ae2dc71..96911f4 100644
--- a/unit-test/global.php
+++ b/unit-test/global.php
@@ -29,6 +29,8 @@ if (!defined("DOSSIER"))define ("DOSSIER",25);
 $_REQUEST['gDossier'] = DOSSIER;
 $g_connection=new Database(DOSSIER);
 $g_parameter = new Noalyss_Parameter_Folder($g_connection);
+$_SESSION[SESSION_KEY.'use_name']='unit test';
+$_SESSION[SESSION_KEY.'use_first_name']='automatic';
 $_SESSION[SESSION_KEY.'g_user']='admin';
 $_SESSION[SESSION_KEY.'g_pass']='phpcompta';
 $_SESSION[SESSION_KEY.'g_pagesize']='50';
diff --git a/unit-test/include/class/data/all_tags.odt 
b/unit-test/include/class/data/all_tags.odt
new file mode 100644
index 0000000..2305131
Binary files /dev/null and b/unit-test/include/class/data/all_tags.odt differ
diff --git a/unit-test/include/class/document.Test.php 
b/unit-test/include/class/document.Test.php
new file mode 100644
index 0000000..54ccadd
--- /dev/null
+++ b/unit-test/include/class/document.Test.php
@@ -0,0 +1,158 @@
+<?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  test \Document
+ */
+require_once 'global.php';
+
+class DocumentTest extends TestCase
+{
+
+    /**
+     * Document 
+     */
+    private $document;
+
+    public static function setUpBeforeClass()
+    {
+        // insert a document_modele
+        $cn=Dossier::connect();
+        $cn->start();
+        $md_id=$cn->get_next_seq('document_modele_md_id_seq');
+        $sql="insert into 
document_modele(md_id,md_name,md_filename,md_mimetype,md_type,md_affect)
+                  values ($1,$2,$3,$4,$5,$6)";
+        $cn->exec_sql($sql, array($md_id, "Balise", "all-tags.odt", 
"application/vnd.oasis.opendocument.text", "4", "GES"));
+        $oid=$cn->lo_import(__DIR__."/data/all_tags.odt");
+        $cn->exec_sql("update document_modele set md_lob = $1 where md_id=$2", 
[$oid, $md_id]);
+        $cn->commit();
+    }
+
+    public static function tearDownAfterClass()
+    {
+        // clean database
+        $cn=Dossier::connect();
+        $cn->start();
+        $md_id=$cn->get_value("select max(md_id) from document_modele where 
md_name='Balise'");
+        $oid=$cn->get_value("select md_lob from document_modele where 
md_id=$1", [$md_id]);
+        if ($oid!="")
+        {
+            $cn->lo_unlink($oid);
+        }
+        $cn->exec_sql("delete from document_modele where md_id=$1", [$md_id]);
+        $cn->exec_sql("delete from document where ag_id=1");
+        $cn->commit();
+    }
+
+    /**
+     * Sets up the fixture, for example, opens a network connection.
+     * This method is called before a test is executed.
+     */
+    protected function setUp()
+    {
+        
+    }
+
+    /**
+     * Tears down the fixture, for example, closes a network connection.
+     * This method is called after a test is executed.
+     */
+    protected function tearDown()
+    {
+        
+    }
+    function testBlank()
+    {
+        $cn=Dossier::connect();
+        $document=new Document($cn);
+        $document->ag_id=1;
+        $document->md_type=4;
+        $document->blank();
+        $this->assertTrue($document->d_id>-1,"Document inserted")       ;
+        $this->assertTrue(isset($document->d_number) 
&&($document->d_number>0),"Document inserted")       ;
+                
+    }
+    function testCompute_filename()
+    {
+        $cn=Dossier::connect();
+        $document=new Document($cn);
+        $filename="N<ew file name (21-01-01).odt";
+        $new_filename=$document->compute_filename("ACH'->42", $filename);
+        
$this->assertEquals("n-ew-file-name-21-01-01-ach-42.odt",$new_filename,"Sanitize
 filename");
+    }
+    function testReplace()
+    {
+        $cn=Dossier::connect();
+        $document=new Document($cn);
+        $array=[];
+        $array['e_client']='CLIENT';
+        $cn->exec_sql("update  parameter set pr_value='Dossier Test' where 
pr_id=$1",
+                ['MY_NAME']);
+        $cn->exec_sql("update  parameter set pr_value='BE00112233' where 
pr_id=$1",
+                ['MY_TVA']);
+        $cn->exec_sql("update  parameter set pr_value='My street' where 
pr_id=$1",
+                ['MY_STREET']);
+      
+        $this->assertTrue ( $document->replace('MY_NAME',array()) == 'Dossier 
Test','MY_NAME');
+        $this->assertTrue ( $document->replace('MY_TVA',array()) == 
'BE00112233','MY_TVA');
+        $this->assertTrue ( $document->replace('MY_STREET',array()) == 'My 
street','MY_STREET');
+        $this->assertTrue ( $document->replace('CUST_NAME',$array) == 'Client 
1','CUST_NAME');
+    }
+    /**
+     * @covers Document::generate(), 
Document::parseDocument(),Document::replace();
+     */
+    function testGenerate()
+    {
+        $cn=Dossier::connect();
+        $md_id=$cn->get_value('select max(md_id) md_id from document_modele 
where md_name=$1',['Balise']);
+        
+        $array['e_client']='CLIENT';
+        $array['e_date']='21.03.2020';
+        $document=new Document($cn,$md_id);
+        $document->ag_id=2;
+        $document->md_id=$md_id;
+        $cnt_before=$cn->get_value("select count(*) from document");
+        $document->generate($array);
+        $this->assertEquals($document->d_filename ,'all-tags.odt','Generated 
File ');
+        $cnt_after=$cn->get_value("select count(*) from document");
+        $this->assertTrue ($cnt_after == $cnt_before+1,"One file generated");
+    }
+    /**
+     * @depends DocumentTest::testGenerate
+     * @covers Document::export_pdf, Document::transform2pdf()
+     */
+    function testExportPDF()
+    {
+        $cn=Dossier::connect();
+        $d_id=$cn->get_value('select max(d_id) d_id from document');
+        $document=new Document($cn);
+        $document->d_id=$d_id;
+        $document->get();
+        $this->assertEquals($document->d_filename,'all-tags.odt','get document 
row');
+        $file=$document->transform2pdf();
+        echo "Please check $file ";
+        $this->assertTrue (file_exists($file),"File exist");
+        
+    }
+}



reply via email to

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