From 33c7378d2d1428d76e7b491010e90a70dbd9296f Mon Sep 17 00:00:00 2001 From: ldestailleur Date: Wed, 26 Mar 2025 21:01:46 +0100 Subject: [PATCH] Add phpunit test --- htdocs/core/lib/functions.lib.php | 5 ++-- test/phpunit/FunctionsLibTest.php | 46 +++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index c5aea6e8167..358fe41e38e 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -11960,7 +11960,7 @@ function dolExplodeIntoArray($string, $delimiter = ';', $kv = '=') } /** - * Explode a search string into an array but do not explode with keys are inside quotes. + * Explode a search string into an array but do not explode when keys are inside quotes. * For example "a 'b c'" will be array("a", "b c"). * * @param string $input String to explode @@ -12037,7 +12037,7 @@ function dol_getmypid() * 1=value is a numeric test (Example ">5.5 <10"), * 2=value is a list of ID separated with comma (Example '1,3,4'), -2 is for exclude list, * 3=value is list of string separated with comma (Example 'text 1,text 2'), -3 if for exclude list, - * 4=value is a list of ID separated with comma (Example '2,7') to be used to search into a multiselect string '1,2,3,4' + * 4=value is a list of ID separated with comma (Example '2,7') to be used to search inside a string '1,2,3,4' * @param integer $nofirstand 1=Do not output the first 'AND' * @return string $res The statement to append to the SQL query * @see dolSqlDateFilter() @@ -12076,6 +12076,7 @@ function natural_search($fields, $value, $mode = 0, $nofirstand = 0) $crit = trim($crit); $i2 = 0; // count the nb of valid criteria added for this this first criteria $newres = ''; + foreach ($fields as $field) { if ($mode == 1) { $tmpcrits = explode('|', $crit); diff --git a/test/phpunit/FunctionsLibTest.php b/test/phpunit/FunctionsLibTest.php index 67c3e13b816..632f2a1e223 100644 --- a/test/phpunit/FunctionsLibTest.php +++ b/test/phpunit/FunctionsLibTest.php @@ -1881,7 +1881,7 @@ class FunctionsLibTest extends CommonClassTest /** * testFetchObjectByElement * - * @return boolean; + * @return boolean */ public function testFetchObjectByElement() { @@ -1897,7 +1897,7 @@ class FunctionsLibTest extends CommonClassTest /** * testRoundUpToNextMultiple * - * @return void; + * @return void */ public function testRoundUpToNextMultiple() { @@ -1921,7 +1921,7 @@ class FunctionsLibTest extends CommonClassTest /** * testNaturalSearch * - * @return void; + * @return void */ public function testNaturalSearch() { @@ -1957,4 +1957,44 @@ class FunctionsLibTest extends CommonClassTest $s = natural_search("t.field", "KØB", 3); // mode 3 is to provide a list of string separated with coma $this->assertEquals(" AND (t.field IN ('KØB'))", $s); } + + /** + * testDolExplodeKeepIfQuotes + * + * @return void + */ + public function testDolExplodeKeepIfQuotes() + { + global $db; + + $result = dolExplodeKeepIfQuotes("a b"); + $this->assertEquals("a", $result[0]); + $this->assertEquals("b", $result[1]); + + $result = dolExplodeKeepIfQuotes("'a' 'b'"); + $this->assertEquals("a", $result[0]); + $this->assertEquals("b", $result[1]); + + $result = dolExplodeKeepIfQuotes("a 'b' c"); + $this->assertEquals("a", $result[0]); + $this->assertEquals("b", $result[1]); + $this->assertEquals("c", $result[2]); + + $result = dolExplodeKeepIfQuotes("a b'c"); + $this->assertEquals("a", $result[0]); + $this->assertEquals("b'c", $result[1]); + + $result = dolExplodeKeepIfQuotes("a 'b'c"); + $this->assertEquals("a", $result[0]); + $this->assertEquals("b", $result[1]); + $this->assertEquals("c", $result[2]); + + $result = dolExplodeKeepIfQuotes("a 'b c'"); + $this->assertEquals("a", $result[0]); + $this->assertEquals("b c", $result[1]); + + //$result = dolExplodeKeepIfQuotes("1 0"); + //$this->assertEquals("1", $result[0]); + //$this->assertEquals("0", $result[1]); + } }