diff --git a/include/lib/database_core.class.php b/include/lib/database_core.class.php index 165a0aec4..6cd6f30f2 100644 --- a/include/lib/database_core.class.php +++ b/include/lib/database_core.class.php @@ -969,9 +969,11 @@ class DatabaseCore /** * @brief with the handle of a successull query, echo each row into CSV and - * send it directly - * @param type $ret handle to a query - * @param type $aheader double array, each item of the array contains + * send it directly to output. To save it into a file, it is needed to send first the HEADER as CSV. If there are + *less column than column defined in $aheader, then the columns not included in the header are not displaid + * + * @param handle $ret handle to a query + * @param array $aheader double array, each item of the array contains (idx : title, type) * a key type (num) and a key title */ function query_to_csv($ret, $aheader) @@ -990,6 +992,7 @@ class DatabaseCore // for each rows, for each value $e=0; foreach ($row as $row_item){ + if ( $e >= count($a_header)) break; switch ($aheader[$e]['type']) { case 'num': $csv->add($row_item, "number"); diff --git a/unit-test/include/lib/databaseCore.Test.php b/unit-test/include/lib/databaseCoreTest.php similarity index 50% rename from unit-test/include/lib/databaseCore.Test.php rename to unit-test/include/lib/databaseCoreTest.php index 370f29a45..246097350 100644 --- a/unit-test/include/lib/databaseCore.Test.php +++ b/unit-test/include/lib/databaseCoreTest.php @@ -69,6 +69,61 @@ class DatabaseCoreTest extends TestCase } + /** + * @brief test the Query_to_CSV function + * @testDox query_to_csv + * @return void + */ + public function testQuery_to_csv() + { + $aheader=array(); + $aheader[]=array("title"=>"date","type"=>"date"); + $aheader[]=array("title"=>"Montant","type"=>"num"); + $aheader[]=array("title"=>"Label","type"=>"string"); + + $ret=$this->object->exec_sql("select j_id,j_montant,j_text from jrnx where coalesce(j_text ,'') != '' order by j_id limit 10"); + ob_start(); + $this->object->query_to_csv($ret, $aheader); + $p_content=ob_get_contents(); + ob_end_clean(); + $filename=__FUNCTION__."_result.txt"; + \Noalyss\Facility::save_file(__DIR__, $filename, $p_content); + $this->assertFileExists(__DIR__."/$filename"); + $filesize=filesize(__DIR__."/$filename"); + $this->assertEquals(284, $filesize,"$filename has not 284 bytes"); + $this->assertStringContainsString("Documentation", $p_content,"$filename invalid content"); + + } + /** + * @brief test the Query_to_CSV function + * @testDox query_to_csv + * @return void + */ + public function testQuery_to_csvSmallHeader() + { + $aheader=array(); + $aheader[]=array("title"=>"date","type"=>"date"); + $aheader[]=array("title"=>"Montant","type"=>"num"); + $aheader[]=array("title"=>"Label","type"=>"string"); + + $ret=$this->object->exec_sql(" + select j_id,j_montant,j_text,j_poste + from + jrnx + where + coalesce(j_text ,'') != '' order by j_id limit 10"); + ob_start(); + $this->object->query_to_csv($ret, $aheader); + $p_content=ob_get_contents(); + ob_end_clean(); + $filename=__FUNCTION__."_result.txt"; + \Noalyss\Facility::save_file(__DIR__, $filename, $p_content); + $this->assertFileExists(__DIR__."/$filename"); + $filesize=filesize(__DIR__."/$filename"); + $this->assertEquals(284, $filesize,"$filename has not 284 bytes"); + $this->assertStringContainsString("Documentation", $p_content,"$filename invalid content"); + + } } \ No newline at end of file