2011-05-01 11:28:46 +02:00
< ? php
2014-03-13 09:20:05 +01:00
/* Copyright ( C ) 2010 - 2014 Laurent Destailleur < eldy @ users . sourceforge . net >
2015-04-23 10:35:08 +02:00
* Copyright ( C ) 2015 Juanjo Menent < jmenent @ 2 byte . es >
2023-05-07 14:31:35 +02:00
* Copyright ( C ) 2023 Alexandre Janniaux < alexandre . janniaux @ gmail . com >
2011-05-01 11:28:46 +02:00
*
2012-03-25 14:25:32 +02:00
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2012-03-25 14:25:32 +02:00
* ( at your option ) any later version .
*
* This program 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 .
*
2011-12-17 21:58:44 +01:00
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
* or see https :// www . gnu . org /
2012-03-25 14:25:32 +02:00
*/
2011-05-01 11:28:46 +02:00
/**
2014-03-11 23:39:26 +01:00
* \file test / phpunit / FunctionsLibTest . php
2011-05-01 11:28:46 +02:00
* \ingroup test
* \brief PHPUnit test
* \remarks To run this script as CLI : phpunit filename . php
*/
2023-12-19 14:03:17 +01:00
global $conf , $user , $langs , $db , $mysoc ;
2011-05-01 11:28:46 +02:00
//define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
2014-05-01 19:57:53 +02:00
//require_once 'PHPUnit/Autoload.php';
2015-02-25 16:47:34 +01:00
require_once dirname ( __FILE__ ) . '/../../htdocs/master.inc.php' ;
require_once dirname ( __FILE__ ) . '/../../htdocs/core/lib/date.lib.php' ;
2022-09-20 21:13:51 +02:00
require_once dirname ( __FILE__ ) . '/../../htdocs/product/class/product.class.php' ;
2024-02-16 23:26:32 +01:00
require_once dirname ( __FILE__ ) . '/CommonClassTest.class.php' ;
2015-02-25 16:47:34 +01:00
2021-01-14 15:09:08 +01:00
if ( ! defined ( 'NOREQUIREUSER' )) {
define ( 'NOREQUIREUSER' , '1' );
}
if ( ! defined ( 'NOREQUIREDB' )) {
define ( 'NOREQUIREDB' , '1' );
}
if ( ! defined ( 'NOREQUIRESOC' )) {
define ( 'NOREQUIRESOC' , '1' );
}
if ( ! defined ( 'NOREQUIRETRAN' )) {
define ( 'NOREQUIRETRAN' , '1' );
}
if ( ! defined ( 'NOCSRFCHECK' )) {
define ( 'NOCSRFCHECK' , '1' );
}
if ( ! defined ( 'NOTOKENRENEWAL' )) {
define ( 'NOTOKENRENEWAL' , '1' );
}
if ( ! defined ( 'NOREQUIREMENU' )) {
define ( 'NOREQUIREMENU' , '1' ); // If there is no menu to show
}
if ( ! defined ( 'NOREQUIREHTML' )) {
define ( 'NOREQUIREHTML' , '1' ); // If we don't need to load the html.form.class.php
}
if ( ! defined ( 'NOREQUIREAJAX' )) {
define ( 'NOREQUIREAJAX' , '1' );
}
if ( ! defined ( " NOLOGIN " )) {
define ( " NOLOGIN " , '1' ); // If this page is public (can be called outside logged session)
}
2011-05-01 11:28:46 +02:00
2021-05-04 13:33:18 +02:00
print " \n " . $langs -> trans ( " CurrentTimeZone " ) . ' : ' . getServerTimeZoneString ();
print " \n " . $langs -> trans ( " CurrentHour " ) . ' : ' . dol_print_date ( dol_now ( 'gmt' ), 'dayhour' , 'tzserver' );
print " \n " ;
2011-05-01 11:28:46 +02:00
/**
2011-09-23 14:21:00 +02:00
* Class for PHPUnit tests
2011-09-10 14:28:00 +02:00
*
2011-05-01 11:28:46 +02:00
* @ backupGlobals disabled
* @ backupStaticAttributes enabled
* @ remarks backupGlobals must be disabled to have db , conf , user and lang not erased .
*/
2024-02-16 23:26:32 +01:00
class FunctionsLibTest extends CommonClassTest
2011-05-01 11:28:46 +02:00
{
2021-01-14 15:09:08 +01:00
/**
* setUpBeforeClass
*
* @ return void
*/
2022-09-21 17:55:04 +02:00
public static function setUpBeforeClass () : void
2021-01-14 15:09:08 +01:00
{
global $conf , $user , $langs , $db ;
//$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
if ( ! function_exists ( 'mb_substr' )) {
2023-12-04 11:22:28 +01:00
print " \n " . __METHOD__ . " function mb_substr must be enabled. \n " ;
die ( 1 );
2021-03-03 12:05:43 +01:00
}
2024-10-30 19:53:40 +01:00
if ( getDolGlobalInt ( 'MAIN_MAX_DECIMALS_UNIT' ) != 5 ) {
2023-12-04 11:22:28 +01:00
print " \n " . __METHOD__ . " bad setup for number of digits for unit amount. Must be 5 for this test. \n " ;
die ( 1 );
2021-03-03 12:05:43 +01:00
}
2024-10-30 19:53:40 +01:00
if ( getDolGlobalInt ( 'MAIN_MAX_DECIMALS_TOT' ) != 2 ) {
2023-12-04 11:22:28 +01:00
print " \n " . __METHOD__ . " bad setup for number of digits for unit amount. Must be 2 for this test. \n " ;
die ( 1 );
2021-01-14 15:09:08 +01:00
}
print __METHOD__ . " \n " ;
}
2023-02-25 19:48:33 +01:00
2024-02-16 01:19:53 +01:00
/**
* testDolCheckFilters
*
* @ return boolean
*/
public function testDolCheckFilters ()
{
global $conf , $langs , $db ;
// A sql with global parenthesis at level 2
$error = '' ;
$parenthesislevel = 0 ;
$sql = '(( ... (a:=:1) .éééé. (b:=:1) ... ))' ;
$result = dolCheckFilters ( $sql , $error , $parenthesislevel );
$this -> assertEquals ( 2 , $parenthesislevel );
$this -> assertTrue ( $result );
// A sql with global parenthesis at level 2
$error = '' ;
$parenthesislevel = 0 ;
$sql = '(((((a:=:1) ... ) .éééé.. (b:=:1) ..) ... ))' ;
$result = dolCheckFilters ( $sql , $error , $parenthesislevel );
$this -> assertEquals ( 2 , $parenthesislevel );
$this -> assertTrue ( $result );
// A sql with global parenthesis at level 2
$error = '' ;
$parenthesislevel = 0 ;
$sql = '((... (((a:=:1) ... ( .éééé.. (b:=:1) ..)))))' ;
$result = dolCheckFilters ( $sql , $error , $parenthesislevel );
$this -> assertEquals ( 2 , $parenthesislevel );
$this -> assertTrue ( $result );
// A sql with global parenthesis at level 0
$error = '' ;
$parenthesislevel = 0 ;
$sql = '(a:=:1) ... (b:=:1) éééé ...' ;
$result = dolCheckFilters ( $sql , $error , $parenthesislevel );
$this -> assertEquals ( 0 , $parenthesislevel );
$this -> assertTrue ( $result );
// A sql with bad balance
$error = '' ;
$parenthesislevel = 0 ;
$sql = '((((a:=:1) ... (b:=:1) éééé ..))' ;
$result = dolCheckFilters ( $sql , $error , $parenthesislevel );
$this -> assertEquals ( 0 , $parenthesislevel );
$this -> assertFalse ( $result );
// A sql with bad balance
$error = '' ;
$parenthesislevel = 0 ;
$sql = '(((a:=:1) ... (b:=:1) éééé ..)))' ;
$result = dolCheckFilters ( $sql , $error , $parenthesislevel );
$this -> assertEquals ( 0 , $parenthesislevel );
$this -> assertFalse ( $result );
2024-02-16 01:50:03 +01:00
return true ;
}
/**
* testDolForgeExplodeAnd
*
* @ return boolean
*/
public function testDolForgeExplodeAnd ()
{
$tmp = dolForgeExplodeAnd ( '' );
$this -> assertEquals ( 0 , count ( $tmp ));
$tmp = dolForgeExplodeAnd ( '(a:=:1)' );
$this -> assertEquals ( '(a:=:1)' , $tmp [ 0 ]);
$tmp = dolForgeExplodeAnd ( '(a:=:1) AND (b:=:2)' );
$this -> assertEquals ( '(a:=:1)' , $tmp [ 0 ]);
$this -> assertEquals ( '(b:=:2)' , $tmp [ 1 ]);
$tmp = dolForgeExplodeAnd ( '(a:=:1) AND ((b:=:2) OR (c:=:3))' );
$this -> assertEquals ( '(a:=:1)' , $tmp [ 0 ]);
$this -> assertEquals ( '((b:=:2) OR (c:=:3))' , $tmp [ 1 ]);
$tmp = dolForgeExplodeAnd ( '(a:=:1) AND (b:=:2) OR (c:=:3)' );
$this -> assertEquals ( '(a:=:1)' , $tmp [ 0 ]);
$this -> assertEquals ( '(b:=:2) OR (c:=:3)' , $tmp [ 1 ]);
$tmp = dolForgeExplodeAnd ( '(a:=:1) OR (b:=:2) AND (c:=:3)' );
$this -> assertEquals ( '(a:=:1) OR (b:=:2)' , $tmp [ 0 ]);
$this -> assertEquals ( '(c:=:3)' , $tmp [ 1 ]);
$tmp = dolForgeExplodeAnd ( '(a:=:1) OR ((b:=:2) AND (c:=:3))' );
$this -> assertEquals ( '(a:=:1) OR ((b:=:2) AND (c:=:3))' , $tmp [ 0 ]);
$tmp = dolForgeExplodeAnd ( '((y:=:1) AND (p:=:8))' );
$this -> assertEquals ( '(y:=:1)' , $tmp [ 0 ]);
$this -> assertEquals ( '(p:=:8)' , $tmp [ 1 ]);
return true ;
2024-02-16 01:19:53 +01:00
}
2023-02-25 19:48:33 +01:00
/**
2024-11-24 14:58:40 +01:00
* testDolForgeSQLCriteriaCallback
2023-02-25 19:48:33 +01:00
*
* @ return boolean
*/
2024-11-24 14:58:40 +01:00
public function testDolForgeSQLCriteriaCallback ()
2023-02-25 19:48:33 +01:00
{
2023-12-19 15:10:59 +01:00
global $conf , $langs , $db ;
2023-02-25 19:48:33 +01:00
2024-05-08 15:41:13 +02:00
// Test using like
$filter = " (lastname:like:'%aaa%') OR (firstname:like:'%bbb%') " ;
$sql = forgeSQLFromUniversalSearchCriteria ( $filter );
$this -> assertEquals ( " AND ((lastname LIKE '%aaa%') OR (firstname LIKE '%bbb%')) " , $sql );
2024-03-25 23:51:09 +01:00
// Test on NOW
$filter = " (client:!=:8) AND (datefin:>=:'__NOW__') " ;
$sql = forgeSQLFromUniversalSearchCriteria ( $filter );
$this -> assertStringContainsStringIgnoringCase ( " AND ((client <> 8) AND (datefin >= ' " , $sql );
2023-02-25 19:48:33 +01:00
// An attempt for SQL injection
2024-02-19 15:28:21 +01:00
$filter = 'if(now()=sysdate()%2Csleep(6)%2C0)' ;
2023-02-25 19:48:33 +01:00
$sql = forgeSQLFromUniversalSearchCriteria ( $filter );
2024-02-17 15:19:06 +01:00
$this -> assertEquals ( 'Filter error - Bad syntax of the search string' , $sql );
2023-02-25 19:48:33 +01:00
// A real search string
2024-02-19 15:28:21 +01:00
$filter = '(((statut:=:1) or (entity:in:__AAA__)) and (abc:<:2.0) and (abc:!=:1.23))' ;
2023-02-25 19:48:33 +01:00
$sql = forgeSQLFromUniversalSearchCriteria ( $filter );
2023-12-01 19:48:26 +01:00
$this -> assertEquals ( ' AND ((((statut = 1) or (entity IN (__AAA__))) and (abc < 2) and (abc <> 1.23)))' , $sql );
2023-02-25 19:48:33 +01:00
2023-12-19 15:01:21 +01:00
// A real search string
2024-02-19 15:28:21 +01:00
$filter = " (t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.date_creation:<:'2016-01-01 12:30:00') or (t.nature:is:NULL) " ;
2023-02-25 19:48:33 +01:00
$sql = forgeSQLFromUniversalSearchCriteria ( $filter );
2024-08-23 15:27:10 +02:00
$this -> assertEquals ( " AND ((t.ref LIKE 'SO-%') or (t.date_creation < '20160101') or (t.date_creation < '2016-01-01 12:30:00') or (t.nature IS NULL)) " , $sql );
2023-02-25 19:48:33 +01:00
2023-12-19 15:01:21 +01:00
// A real search string
$filter = " (t.fieldstring:=:'aaa ttt') " ;
$sql = forgeSQLFromUniversalSearchCriteria ( $filter );
$this -> assertEquals ( " AND ((t.fieldstring = 'aaa ttt')) " , $sql );
// Check that parenthesis are NOT allowed inside the last operand. Very important.
$filter = " (t.fieldint:=:(1,2)) " ;
$sql = forgeSQLFromUniversalSearchCriteria ( $filter );
2024-02-17 15:19:06 +01:00
$this -> assertEquals ( " Filter error - Bad syntax of the search string " , $sql );
2023-12-19 15:01:21 +01:00
// Check that ' is escaped into the last operand
$filter = " (t.fieldstring:=:'aaa'ttt') " ;
$sql = forgeSQLFromUniversalSearchCriteria ( $filter );
2023-12-19 15:10:59 +01:00
if ( $db -> type == 'mysqli' ) {
$this -> assertEquals ( " AND ((t.fieldstring = 'aaa \ 'ttt')) " , $sql ); // with mysql
} else {
$this -> assertEquals ( " AND ((t.fieldstring = 'aaa''ttt')) " , $sql ); // with pgsql
}
2023-12-19 15:01:21 +01:00
$filter = " (t.fk_soc:IN:1,2) " ;
$sql = forgeSQLFromUniversalSearchCriteria ( $filter );
$this -> assertEquals ( " AND ((t.fk_soc IN (1,2))) " , $sql );
$filter = " (t.fk_soc:IN:'1','2=b') " ;
$sql = forgeSQLFromUniversalSearchCriteria ( $filter );
$this -> assertEquals ( " AND ((t.fk_soc IN ('1','2=b'))) " , $sql );
2023-12-19 15:10:59 +01:00
$filter = " (t.fk_soc:IN:SELECT rowid FROM llx_societe WHERE fournisseur = 1) " ;
2023-12-19 14:03:17 +01:00
$sql = forgeSQLFromUniversalSearchCriteria ( $filter );
2023-12-19 15:10:59 +01:00
$this -> assertEquals ( " AND ((t.fk_soc IN (SELECT rowid FROM llx_societe WHERE fournisseur = 1))) " , $sql );
2023-12-19 14:03:17 +01:00
2023-02-25 19:48:33 +01:00
return true ;
}
2022-09-20 21:13:51 +02:00
/**
* testDolClone
*
* @ return void
*/
public function testDolClone ()
{
2023-04-13 20:25:07 +02:00
global $db ;
2022-09-20 21:13:51 +02:00
2023-04-13 20:25:07 +02:00
$newproduct1 = new Product ( $db );
print __METHOD__ . " this->savdb has type " . ( is_resource ( $db -> db ) ? get_resource_type ( $db -> db ) : ( is_object ( $db -> db ) ? 'object' : 'unknown' )) . " \n " ;
2022-09-20 21:13:51 +02:00
print __METHOD__ . " newproduct1->db->db has type " . ( is_resource ( $newproduct1 -> db -> db ) ? get_resource_type ( $newproduct1 -> db -> db ) : ( is_object ( $newproduct1 -> db -> db ) ? 'object' : 'unknown' )) . " \n " ;
2023-04-13 20:25:07 +02:00
$this -> assertEquals ( $db -> connected , 1 , 'Savdb is connected' );
2022-09-20 21:13:51 +02:00
$this -> assertNotNull ( $newproduct1 -> db -> db , 'newproduct1->db is not null' );
$newproductcloned1 = dol_clone ( $newproduct1 );
2023-04-13 20:25:07 +02:00
print __METHOD__ . " this->savdb has type " . ( is_resource ( $db -> db ) ? get_resource_type ( $db -> db ) : ( is_object ( $db -> db ) ? 'object' : 'unknown' )) . " \n " ;
2022-09-20 21:13:51 +02:00
print __METHOD__ . " newproduct1->db->db has type " . ( is_resource ( $newproduct1 -> db -> db ) ? get_resource_type ( $newproduct1 -> db -> db ) : ( is_object ( $newproduct1 -> db -> db ) ? 'object' : 'unknown' )) . " \n " ;
2023-04-13 20:25:07 +02:00
$this -> assertEquals ( $db -> connected , 1 , 'Savdb is connected' );
2022-09-20 21:13:51 +02:00
$this -> assertNotNull ( $newproduct1 -> db -> db , 'newproduct1->db is not null' );
2023-12-19 15:01:21 +01:00
//$newproductcloned2 = dol_clone($newproduct1, 2);
//var_dump($newproductcloned2);
2022-09-20 21:13:51 +02:00
//print __METHOD__." newproductcloned1->db must be null\n";
//$this->assertNull($newproductcloned1->db, 'newproductcloned1->db is null');
}
2022-07-08 14:11:13 +02:00
2022-05-31 18:17:43 +02:00
/**
* testNum2Alpha
*
* @ return void
*/
public function testNum2Alpha ()
{
$result = num2Alpha ( 0 );
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( $result , 'A' , 'Check num2Alpha 0' );
$result = num2Alpha ( 5 );
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( $result , 'F' , 'Check num2Alpha 5' );
$result = num2Alpha ( 26 );
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( $result , 'AA' , 'Check num2Alpha 26' );
}
2021-01-14 15:09:08 +01:00
/**
* testIsValidEmail
*
* @ return void
*/
public function testIsValidEmail ()
{
// Nb of line is same than entry text
2024-02-19 15:28:21 +01:00
$input = " bidon@bademail " ;
$result = isValidEmail ( $input );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertFalse ( $result , 'Check isValidEmail ' . $input );
2024-02-19 15:28:21 +01:00
$input = " test@yahoo.com " ;
$result = isValidEmail ( $input );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertTrue ( $result , 'Check isValidEmail ' . $input );
2024-02-19 15:28:21 +01:00
$input = " The name of sender <test@yahoo.com> " ;
$result = isValidEmail ( $input );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertFalse ( $result , 'Check isValidEmail ' . $input );
2024-02-19 15:28:21 +01:00
$input = " 1234.abcdefg@domainame.com.br " ;
$result = isValidEmail ( $input );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertTrue ( $result , 'Check isValidEmail ' . $input );
2024-09-15 11:24:34 +02:00
$input = " 1234.abcdefg@domainame.entreprises " ;
$result = isValidEmail ( $input );
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertTrue ( $result , 'Check isValidEmail ' . $input );
2021-01-14 15:09:08 +01:00
}
/**
* testIsValidMXRecord
*
* @ return void
*/
public function testIsValidMXRecord ()
{
// Nb of line is same than entry text
2024-02-19 15:28:21 +01:00
$input = " yahoo.com " ;
$result = isValidMXRecord ( $input );
2025-01-27 11:41:59 +01:00
print __METHOD__ . " " . $input . " result= " . $result . " \n " ;
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 1 , $result );
2024-02-19 15:28:21 +01:00
$input = " yhaoo.com " ;
$result = isValidMXRecord ( $input );
2025-01-27 11:41:59 +01:00
print __METHOD__ . " " . $input . " result= " . $result . " \n " ;
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 0 , $result );
2024-02-19 15:28:21 +01:00
$input = " dolibarr.fr " ;
$result = isValidMXRecord ( $input );
2025-01-27 11:41:59 +01:00
print __METHOD__ . " " . $input . " result= " . $result . " \n " ;
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 0 , $result );
2025-01-27 11:41:59 +01:00
$input = " usace.army.mil " ;
$result = isValidMXRecord ( $input );
print __METHOD__ . " " . $input . " result= " . $result . " \n " ;
$this -> assertEquals ( 1 , $result );
2021-01-14 15:09:08 +01:00
}
/**
* testDolGetFirstLineOfText
*
* @ return void
*/
public function testDolGetFirstLineOfText ()
{
// Nb of line is same than entry text
2024-02-19 15:28:21 +01:00
$input = " aaaa " ;
$result = dolGetFirstLineOfText ( $input );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( " aaaa " , $result );
2024-02-19 15:28:21 +01:00
$input = " aaaa \n bbbbbbbbbbbb \n " ;
$result = dolGetFirstLineOfText ( $input , 2 );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( " aaaa \n bbbbbbbbbbbb " , $result );
2024-02-19 15:28:21 +01:00
$input = " aaaa<br>bbbbbbbbbbbb<br> " ;
$result = dolGetFirstLineOfText ( $input , 2 );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( " aaaa<br> \n bbbbbbbbbbbb " , $result );
// Nb of line is lower
2024-02-19 15:28:21 +01:00
$input = " aaaa \n bbbbbbbbbbbb \n cccccc \n " ;
$result = dolGetFirstLineOfText ( $input );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( " aaaa... " , $result );
2024-02-19 15:28:21 +01:00
$input = " aaaa<br>bbbbbbbbbbbb<br>cccccc<br> " ;
$result = dolGetFirstLineOfText ( $input );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( " aaaa... " , $result );
2024-02-19 15:28:21 +01:00
$input = " aaaa \n bbbbbbbbbbbb \n cccccc \n " ;
$result = dolGetFirstLineOfText ( $input , 2 );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( " aaaa \n bbbbbbbbbbbb... " , $result );
2024-02-19 15:28:21 +01:00
$input = " aaaa<br>bbbbbbbbbbbb<br>cccccc<br> " ;
$result = dolGetFirstLineOfText ( $input , 2 );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( " aaaa<br> \n bbbbbbbbbbbb... " , $result );
// Nb of line is higher
2024-02-19 15:28:21 +01:00
$input = " aaaa<br>bbbbbbbbbbbb<br>cccccc " ;
$result = dolGetFirstLineOfText ( $input , 100 );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( " aaaa<br> \n bbbbbbbbbbbb<br> \n cccccc " , $result , 'dolGetFirstLineOfText with nb 100 a' );
2024-02-19 15:28:21 +01:00
$input = " aaaa<br>bbbbbbbbbbbb<br>cccccc<br> " ;
$result = dolGetFirstLineOfText ( $input , 100 );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( " aaaa<br> \n bbbbbbbbbbbb<br> \n cccccc " , $result , 'dolGetFirstLineOfText with nb 100 b' );
2024-02-19 15:28:21 +01:00
$input = " aaaa<br>bbbbbbbbbbbb<br>cccccc<br> \n " ;
$result = dolGetFirstLineOfText ( $input , 100 );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( " aaaa<br> \n bbbbbbbbbbbb<br> \n cccccc " , $result , 'dolGetFirstLineOfText with nb 100 c' );
}
/**
* testDolBuildPath
*
* @ return void
*/
public function testDolBuildPath ()
{
/* $tmp = dol_buildpath ( '/google/oauth2callback.php' , 0 );
var_dump ( $tmp );
*/
/* $tmp = dol_buildpath ( '/google/oauth2callback.php' , 1 );
var_dump ( $tmp );
*/
2024-02-19 15:28:21 +01:00
$result = dol_buildpath ( '/google/oauth2callback.php' , 2 );
2023-12-19 14:03:17 +01:00
print __METHOD__ . " dol_buildpath result= " . $result . " \n " ;
2021-01-14 15:09:08 +01:00
$this -> assertStringStartsWith ( 'http' , $result );
2024-02-19 15:28:21 +01:00
$result = dol_buildpath ( '/google/oauth2callback.php' , 3 );
2023-12-19 14:03:17 +01:00
print __METHOD__ . " dol_buildpath result= " . $result . " \n " ;
2021-01-14 15:09:08 +01:00
$this -> assertStringStartsWith ( 'http' , $result );
}
/**
* testGetBrowserInfo
*
* @ return void
*/
public function testGetBrowserInfo ()
{
// MSIE 5.0
2024-02-19 15:28:21 +01:00
$user_agent = 'Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt; KITV4 Wanadoo; KITV5 Wanadoo)' ;
$tmp = getBrowserInfo ( $user_agent );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 'ie' , $tmp [ 'browsername' ]);
$this -> assertEquals ( '5.0' , $tmp [ 'browserversion' ]);
$this -> assertEmpty ( $tmp [ 'phone' ]);
$this -> assertFalse ( $tmp [ 'tablet' ]);
$this -> assertEquals ( 'classic' , $tmp [ 'layout' ]);
// Firefox 0.9.1
2024-02-19 15:28:21 +01:00
$user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.5a) Gecko/20030728 Mozilla Firefox/0.9.1' ;
$tmp = getBrowserInfo ( $user_agent );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 'firefox' , $tmp [ 'browsername' ]);
$this -> assertEquals ( '0.9.1' , $tmp [ 'browserversion' ]);
$this -> assertEmpty ( $tmp [ 'phone' ]);
$this -> assertFalse ( $tmp [ 'tablet' ]);
$this -> assertEquals ( 'classic' , $tmp [ 'layout' ]);
2024-02-19 15:28:21 +01:00
$user_agent = 'Mozilla/3.0 (Windows 98; U) Opera 6.03 [en]' ;
$tmp = getBrowserInfo ( $user_agent );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 'opera' , $tmp [ 'browsername' ]);
$this -> assertEquals ( '6.03' , $tmp [ 'browserversion' ]);
$this -> assertEmpty ( $tmp [ 'phone' ]);
$this -> assertFalse ( $tmp [ 'tablet' ]);
$this -> assertEquals ( 'classic' , $tmp [ 'layout' ]);
2024-02-19 15:28:21 +01:00
$user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21' ;
$tmp = getBrowserInfo ( $user_agent );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 'chrome' , $tmp [ 'browsername' ]);
$this -> assertEquals ( '19.0.1042.0' , $tmp [ 'browserversion' ]);
$this -> assertEmpty ( $tmp [ 'phone' ]);
$this -> assertFalse ( $tmp [ 'tablet' ]);
$this -> assertEquals ( 'classic' , $tmp [ 'layout' ]);
2024-02-19 15:28:21 +01:00
$user_agent = 'chrome (Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11)' ;
$tmp = getBrowserInfo ( $user_agent );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 'chrome' , $tmp [ 'browsername' ]);
$this -> assertEquals ( '17.0.963.56' , $tmp [ 'browserversion' ]);
$this -> assertEmpty ( $tmp [ 'phone' ]);
$this -> assertFalse ( $tmp [ 'tablet' ]);
$this -> assertEquals ( 'classic' , $tmp [ 'layout' ]);
2024-02-19 15:28:21 +01:00
$user_agent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1' ;
$tmp = getBrowserInfo ( $user_agent );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 'safari' , $tmp [ 'browsername' ]);
$this -> assertEquals ( '533.21.1' , $tmp [ 'browserversion' ]);
$this -> assertEmpty ( $tmp [ 'phone' ]);
$this -> assertFalse ( $tmp [ 'tablet' ]);
$this -> assertEquals ( 'classic' , $tmp [ 'layout' ]);
2024-10-09 00:13:56 +02:00
// Internet Explorer 11
2021-01-14 15:09:08 +01:00
$user_agent = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko' ;
2024-02-19 15:28:21 +01:00
$tmp = getBrowserInfo ( $user_agent );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 'ie' , $tmp [ 'browsername' ]);
$this -> assertEquals ( '11.0' , $tmp [ 'browserversion' ]);
$this -> assertEmpty ( $tmp [ 'phone' ]);
$this -> assertFalse ( $tmp [ 'tablet' ]);
$this -> assertEquals ( 'classic' , $tmp [ 'layout' ]);
2024-10-09 00:13:56 +02:00
// Internet Explorer 11 bis
2021-01-14 15:09:08 +01:00
$user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; NP06; rv:11.0) like Gecko' ;
2024-02-19 15:28:21 +01:00
$tmp = getBrowserInfo ( $user_agent );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 'ie' , $tmp [ 'browsername' ]);
$this -> assertEquals ( '11.0' , $tmp [ 'browserversion' ]);
$this -> assertEmpty ( $tmp [ 'phone' ]);
$this -> assertFalse ( $tmp [ 'tablet' ]);
$this -> assertEquals ( 'classic' , $tmp [ 'layout' ]);
2024-10-09 00:13:56 +02:00
// iPad
2021-01-14 15:09:08 +01:00
$user_agent = 'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25' ;
2024-02-19 15:28:21 +01:00
$tmp = getBrowserInfo ( $user_agent );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 'safari' , $tmp [ 'browsername' ]);
$this -> assertEquals ( '8536.25' , $tmp [ 'browserversion' ]);
$this -> assertEquals ( 'ios' , $tmp [ 'browseros' ]);
$this -> assertEquals ( 'tablet' , $tmp [ 'layout' ]);
$this -> assertEquals ( 'iphone' , $tmp [ 'phone' ]);
2022-12-31 13:44:11 +01:00
2024-10-09 00:13:56 +02:00
// Lynx
2022-12-31 13:44:11 +01:00
$user_agent = 'Lynx/2.8.8dev.3 libwww‑ FM/2.14 SSL‑ MM/1.4.1' ;
2024-02-19 15:28:21 +01:00
$tmp = getBrowserInfo ( $user_agent );
2024-10-09 00:13:56 +02:00
$this -> assertEquals ( 'textbrowser' , $tmp [ 'browsername' ]);
2022-12-31 16:48:40 +01:00
$this -> assertEquals ( '2.8.8' , $tmp [ 'browserversion' ]);
2022-12-31 13:44:11 +01:00
$this -> assertEquals ( 'unknown' , $tmp [ 'browseros' ]);
$this -> assertEquals ( 'classic' , $tmp [ 'layout' ]);
2024-10-09 00:13:56 +02:00
// W3M
$user_agent = 'w3m/1.2.3-git123456' ;
$tmp = getBrowserInfo ( $user_agent );
$this -> assertEquals ( 'textbrowser' , $tmp [ 'browsername' ]);
$this -> assertEquals ( '1.2.3' , $tmp [ 'browserversion' ]);
$this -> assertEquals ( 'unknown' , $tmp [ 'browseros' ]);
$this -> assertEquals ( 'classic' , $tmp [ 'layout' ]);
2021-01-14 15:09:08 +01:00
}
/**
* testGetLanguageCodeFromCountryCode
*
* @ return void
*/
public function testGetLanguageCodeFromCountryCode ()
{
global $mysoc ;
$language = getLanguageCodeFromCountryCode ( 'US' );
$this -> assertEquals ( 'en_US' , $language , 'US' );
$language = getLanguageCodeFromCountryCode ( 'ES' );
$this -> assertEquals ( 'es_ES' , $language , 'ES' );
$language = getLanguageCodeFromCountryCode ( 'CL' );
$this -> assertEquals ( 'es_CL' , $language , 'CL' );
$language = getLanguageCodeFromCountryCode ( 'CA' );
$this -> assertEquals ( 'en_CA' , $language , 'CA' );
$language = getLanguageCodeFromCountryCode ( 'MQ' );
$this -> assertEquals ( 'fr_CA' , $language );
$language = getLanguageCodeFromCountryCode ( 'FR' );
$this -> assertEquals ( 'fr_FR' , $language );
$language = getLanguageCodeFromCountryCode ( 'BE' );
$this -> assertEquals ( 'fr_BE' , $language );
$mysoc -> country_code = 'FR' ;
$language = getLanguageCodeFromCountryCode ( 'CH' );
$this -> assertEquals ( 'fr_CH' , $language );
$mysoc -> country_code = 'DE' ;
$language = getLanguageCodeFromCountryCode ( 'CH' );
$this -> assertEquals ( 'de_CH' , $language );
$language = getLanguageCodeFromCountryCode ( 'DE' );
$this -> assertEquals ( 'de_DE' , $language );
$language = getLanguageCodeFromCountryCode ( 'SA' );
$this -> assertEquals ( 'ar_SA' , $language );
$language = getLanguageCodeFromCountryCode ( 'SE' );
$this -> assertEquals ( 'sv_SE' , $language );
$language = getLanguageCodeFromCountryCode ( 'DK' );
$this -> assertEquals ( 'da_DK' , $language );
}
/**
* testDolTextIsHtml
*
* @ return void
*/
public function testDolTextIsHtml ()
{
// True
2024-02-19 15:28:21 +01:00
$input = '<html>xxx</html>' ;
$after = dol_textishtml ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertTrue ( $after , 'Test with html tag' );
2024-02-19 15:28:21 +01:00
$input = '<body>xxx</body>' ;
$after = dol_textishtml ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertTrue ( $after , 'Test with body tag' );
2024-02-19 15:28:21 +01:00
$input = 'xxx <b>yyy</b> zzz' ;
$after = dol_textishtml ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertTrue ( $after , 'Test with b tag' );
2024-02-19 15:28:21 +01:00
$input = 'xxx <u>yyy</u> zzz' ;
$after = dol_textishtml ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertTrue ( $after , 'Test with u tag' );
2024-02-19 15:28:21 +01:00
$input = 'text with <div>some div</div>' ;
$after = dol_textishtml ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertTrue ( $after , 'Test with div tag' );
2024-02-19 15:28:21 +01:00
$input = 'text with HTML entities' ;
$after = dol_textishtml ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertTrue ( $after , 'Test with entities tag' );
2024-02-19 15:28:21 +01:00
$input = 'xxx<br>' ;
$after = dol_textishtml ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertTrue ( $after , 'Test with entities br' );
2024-02-19 15:28:21 +01:00
$input = 'xxx<br >' ;
$after = dol_textishtml ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertTrue ( $after , 'Test with entities br' );
2024-02-19 15:28:21 +01:00
$input = 'xxx<br style="eee">' ;
$after = dol_textishtml ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertTrue ( $after , 'Test with entities br and attributes' );
2024-02-19 15:28:21 +01:00
$input = 'xxx<br style="eee" >' ;
$after = dol_textishtml ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertTrue ( $after , 'Test with entities br and attributes bis' );
2024-02-19 15:28:21 +01:00
$input = '<h2>abc</h2>' ;
$after = dol_textishtml ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertTrue ( $after , 'Test with entities h2' );
2024-02-19 15:28:21 +01:00
$input = '<img id="abc" src="https://xxx.com/aaa/image.png" />' ;
$after = dol_textishtml ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertTrue ( $after , 'Test with img tag' );
2024-02-19 15:28:21 +01:00
$input = '<a class="azerty" href="https://xxx.com/aaa/image.png" />' ;
$after = dol_textishtml ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertTrue ( $after , 'Test with a tag' );
2024-02-19 15:28:21 +01:00
$input = 'This is a text with html spaces' ;
$after = dol_textishtml ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertTrue ( $after , 'Test with a ' );
2024-02-19 15:28:21 +01:00
$input = 'This is a text with accent é' ;
$after = dol_textishtml ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertTrue ( $after , 'Test with a é' );
2024-02-19 15:28:21 +01:00
$input = '<i class="abc">xxx</i>' ;
$after = dol_textishtml ( $input );
2023-03-21 14:03:55 +01:00
$this -> assertTrue ( $after , 'Test with i tag and class;' );
2021-01-14 15:09:08 +01:00
// False
2024-02-19 15:28:21 +01:00
$input = 'xxx < br>' ;
$after = dol_textishtml ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertFalse ( $after );
2024-02-19 15:28:21 +01:00
$input = 'xxx <email@email.com>' ; // <em> is html, <em... is not
$after = dol_textishtml ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertFalse ( $after );
2024-02-19 15:28:21 +01:00
$input = 'xxx <brstyle="ee">' ;
$after = dol_textishtml ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertFalse ( $after );
2024-02-19 15:28:21 +01:00
$input = 'This is a text with html comments <!-- comment -->' ; // we suppose this is not enough to be html content
$after = dol_textishtml ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertFalse ( $after );
2022-07-08 14:11:13 +02:00
2024-02-19 15:28:21 +01:00
$input = " A text \n with a link https://aaa?param=abc&param2=def " ;
$after = dol_textishtml ( $input );
2022-07-08 14:11:13 +02:00
$this -> assertFalse ( $after );
2021-01-14 15:09:08 +01:00
}
/**
* testDolHtmlCleanLastBr
*
* @ return boolean
*/
public function testDolHtmlCleanLastBr ()
{
2024-02-19 15:28:21 +01:00
$input = " A string \n " ;
$after = dol_htmlcleanlastbr ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string " , $after );
2024-02-19 15:28:21 +01:00
$input = " A string first \n A string second \n " ;
$after = dol_htmlcleanlastbr ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string first \n A string second " , $after );
2024-02-19 15:28:21 +01:00
$input = " A string \n \n \n " ;
$after = dol_htmlcleanlastbr ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string " , $after );
2024-02-19 15:28:21 +01:00
$input = " A string<br> " ;
$after = dol_htmlcleanlastbr ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string " , $after );
2024-02-19 15:28:21 +01:00
$input = " A string first<br> \n A string second<br> " ;
$after = dol_htmlcleanlastbr ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string first<br> \n A string second " , $after );
2024-02-19 15:28:21 +01:00
$input = " A string \n <br type= \" _moz \" /> \n " ;
$after = dol_htmlcleanlastbr ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string " , $after );
2024-02-19 15:28:21 +01:00
$input = " A string \n <br><br /> \n \n " ;
$after = dol_htmlcleanlastbr ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string " , $after );
return true ;
}
/**
* testDolConcat
*
* @ return boolean
*/
public function testDolConcat ()
{
2024-02-19 15:28:21 +01:00
$text1 = " A string 1 " ;
$text2 = " A string 2 " ; // text 1 and 2 are text, concat need only \n
$after = dol_concatdesc ( $text1 , $text2 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string 1 \n A string 2 " , $after );
2024-02-19 15:28:21 +01:00
$text1 = " A<br>string 1 " ;
$text2 = " A string 2 " ; // text 1 is html, concat need <br>\n
$after = dol_concatdesc ( $text1 , $text2 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A<br>string 1<br> \n A string 2 " , $after );
2024-02-19 15:28:21 +01:00
$text1 = " A string 1 " ;
$text2 = " A <b>string</b> 2 " ; // text 2 is html, concat need <br>\n
$after = dol_concatdesc ( $text1 , $text2 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string 1<br> \n A <b>string</b> 2 " , $after );
return true ;
}
2023-04-26 18:25:56 +02:00
/**
* testDolStringNoSpecial
*
* @ return boolean
*/
public function testDolStringNoSpecial ()
{
2023-11-06 20:35:41 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/functions2.lib.php' ;
$text = " A string with space and special char like ' or ° and more... \n " ;
$after = dol_string_nospecial ( $text , '_' , '' , '' , 0 );
2023-04-26 18:25:56 +02:00
$this -> assertEquals ( " A_string_with_space_and_special_char_like___or___and_more... \n " , $after , " testDolStringNoSpecial 1 " );
2023-11-06 20:35:41 +01:00
$text = " A string with space and special char like ' or ° and more... \n " ;
$after = dol_string_nospecial ( $text , '_' , '' , '' , 1 );
2023-04-26 18:25:56 +02:00
$this -> assertEquals ( " A string with space and special char like _ or _ and more... \n " , $after , " testDolStringNoSpecial 2 " );
2023-11-06 20:35:41 +01:00
$text = 'Bahnhofstraße' ;
$after = dolEscapeXML ( dol_trunc ( dol_string_nospecial ( dol_string_unaccent ( $text ), ' ' ), 70 , 'right' , 'UTF-8' , 1 ));
$this -> assertEquals ( " Bahnhofstraße " , $after , " testDolStringNoSpecial with german char " );
2023-04-26 18:25:56 +02:00
return true ;
}
2021-01-14 15:09:08 +01:00
/**
* testDolStringNohtmltag
*
* @ return boolean
*/
public function testDolStringNohtmltag ()
{
2024-02-19 15:28:21 +01:00
$text = " A \n string \n \n and more \n " ;
$after = dol_string_nohtmltag ( $text , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A \n string \n \n and more " , $after , " test1a " );
2024-02-19 15:28:21 +01:00
$text = " A <b>string<b><br> \n <br> \n \n with html tag<br> \n " ;
$after = dol_string_nohtmltag ( $text , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string \n \n \n \n \n with html tag " , $after , 'test2a 2 br and 3 \n give 5 \n' );
2024-02-19 15:28:21 +01:00
$text = " A <b>string<b><br> \n <br> \n \n with html tag<br> \n " ;
$after = dol_string_nohtmltag ( $text , 1 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string with html tag " , $after , 'test2b 2 br and 3 \n give 1 space' );
2024-02-19 15:28:21 +01:00
$text = " A <b>string<b><br> \n <br> \n \n with html tag<br> \n " ;
$after = dol_string_nohtmltag ( $text , 2 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string \n \n with html tag " , $after , 'test2c 2 br and 3 \n give 2 \n' );
2024-02-19 15:28:21 +01:00
$text = " A <b>string<b><br> \r \n <br> \r \n \r \n with html tag<br> \n " ;
$after = dol_string_nohtmltag ( $text , 2 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string \n \n with html tag " , $after , 'test2c 2 br and 3 \r\n give 2 \n' );
2024-02-19 15:28:21 +01:00
$text = " A string<br>Another string " ;
$after = dol_string_nohtmltag ( $text , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string \n Another string " , $after , " test4 " );
2024-02-19 15:28:21 +01:00
$text = " A string<br>Another string " ;
$after = dol_string_nohtmltag ( $text , 1 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string Another string " , $after , " test5 " );
2024-02-19 15:28:21 +01:00
$text = '<a href="/myurl" title="<u>Afficher projet</u>">ABC</a>' ;
$after = dol_string_nohtmltag ( $text , 1 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " ABC " , $after , " test6 " );
2024-02-19 15:28:21 +01:00
$text = '<a href="/myurl" title="<u>Afficher projet</u>">DEF</a>' ;
$after = dol_string_nohtmltag ( $text , 1 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " DEF " , $after , " test7 " );
2024-02-19 15:28:21 +01:00
$text = '<a href="/myurl" title="<u>A title</u>">HIJ</a>' ;
$after = dol_string_nohtmltag ( $text , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " HIJ " , $after , " test8 " );
2024-02-19 15:28:21 +01:00
$text = " A <b>string<b> \n \n with html tag and '<' chars<br> \n " ;
$after = dol_string_nohtmltag ( $text , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string \n \n with html tag and '<' chars " , $after , " test9 " );
2024-02-19 15:28:21 +01:00
$text = " A <b>string<b> \n \n with tag with < chars<br> \n " ;
$after = dol_string_nohtmltag ( $text , 1 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string with tag with < chars " , $after , " test10 " );
return true ;
}
/**
* testDolHtmlEntitiesBr
*
* @ return boolean
*/
public function testDolHtmlEntitiesBr ()
{
// Text not already HTML
2024-02-19 15:28:21 +01:00
$input = " A string \n with a é, &, < and >. " ;
$after = dol_htmlentitiesbr ( $input , 0 ); // Add <br> before \n
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string<br> \n with a é, &, < and >. " , $after );
2024-02-19 15:28:21 +01:00
$input = " A string \n with a é, &, < and >. " ;
$after = dol_htmlentitiesbr ( $input , 1 ); // Replace \n with <br>
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string<br>with a é, &, < and >. " , $after );
2024-02-19 15:28:21 +01:00
$input = " A string \n with a é, &, < and >. \n \n " ; // With some \n at end that should be cleaned
$after = dol_htmlentitiesbr ( $input , 0 ); // Add <br> before \n
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string<br> \n with a é, &, < and >. " , $after );
2024-02-19 15:28:21 +01:00
$input = " A string \n with a é, &, < and >. \n \n " ; // With some \n at end that should be cleaned
$after = dol_htmlentitiesbr ( $input , 1 ); // Replace \n with <br>
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string<br>with a é, &, < and >. " , $after );
// Text already HTML, so &,<,> should not be converted
2024-02-19 15:28:21 +01:00
$input = " A string<br> \n with a é, &, < and >. " ;
$after = dol_htmlentitiesbr ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string<br> \n with a é, &, < and >. " , $after );
2024-02-19 15:28:21 +01:00
$input = " <li> \n A string with a é, &, < and >.</li> \n Another string " ;
$after = dol_htmlentitiesbr ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " <li> \n A string with a é, &, < and >.</li> \n Another string " , $after );
2024-02-19 15:28:21 +01:00
$input = " A string<br> \n with a é, &, < and >.<br> " ; // With some <br> at end that should be cleaned
$after = dol_htmlentitiesbr ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string<br> \n with a é, &, < and >. " , $after );
2024-02-19 15:28:21 +01:00
$input = " <li> \n A string with a é, &, < and >.</li> \n Another string<br> " ; // With some <br> at end that should be cleaned
$after = dol_htmlentitiesbr ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " <li> \n A string with a é, &, < and >.</li> \n Another string " , $after );
// TODO Add test with param $removelasteolbr = 0
return true ;
}
/**
* testDolNbOfLinesBis
*
* @ return boolean
*/
public function testDolNbOfLinesBis ()
{
// This is not a html string so nb of lines depends on \n
2024-02-19 15:28:21 +01:00
$input = " A string \n with a é, &, < and > and bold tag. \n Third line " ;
$after = dol_nboflines_bis ( $input , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( $after , 3 );
// This is a html string so nb of lines depends on <br>
2024-02-19 15:28:21 +01:00
$input = " A string \n with a é, &, < and > and <b>bold</b> tag. \n Third line " ;
$after = dol_nboflines_bis ( $input , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( $after , 1 );
// This is a html string so nb of lines depends on <br>
2024-02-19 15:28:21 +01:00
$input = " A string<br>with a é, &, < and > and <b>bold</b> tag.<br>Third line " ;
$after = dol_nboflines_bis ( $input , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( $after , 3 );
return true ;
}
/**
* testDolUnaccent
*
* @ return boolean
*/
public function testDolUnaccent ()
{
// Text not already HTML
2024-02-19 15:28:21 +01:00
$input = " A string \n with a à ä é è ë ï ü ö ÿ, &, < and >. " ;
$after = dol_string_unaccent ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " A string \n with a a a e e e i u o y, &, < and >. " , $after );
}
/**
* testDolUtf8Check
*
* @ return void
*/
public function testDolUtf8Check ()
{
// True
2024-02-19 15:28:21 +01:00
$result = utf8_check ( 'azerty' );
2021-01-14 15:09:08 +01:00
$this -> assertTrue ( $result );
2024-02-19 15:28:21 +01:00
$file = dirname ( __FILE__ ) . '/textutf8.txt' ;
$filecontent = file_get_contents ( $file );
$result = utf8_check ( $filecontent );
2021-01-14 15:09:08 +01:00
$this -> assertTrue ( $result );
2024-02-19 15:28:21 +01:00
$file = dirname ( __FILE__ ) . '/textiso.txt' ;
$filecontent = file_get_contents ( $file );
$result = utf8_check ( $filecontent );
2021-01-14 15:09:08 +01:00
$this -> assertFalse ( $result );
}
/**
* testDolAsciiCheck
*
* @ return void
*/
public function testDolAsciiCheck ()
{
// True
2024-02-19 15:28:21 +01:00
$result = ascii_check ( 'azerty' );
2021-01-14 15:09:08 +01:00
$this -> assertTrue ( $result );
2024-02-19 15:28:21 +01:00
$result = ascii_check ( 'é' );
2021-01-14 15:09:08 +01:00
$this -> assertFalse ( $result );
2024-02-19 15:28:21 +01:00
$file = dirname ( __FILE__ ) . '/textutf8.txt' ;
$filecontent = file_get_contents ( $file );
$result = ascii_check ( $filecontent );
2021-01-14 15:09:08 +01:00
$this -> assertFalse ( $result );
}
/**
* testDolTrunc
*
* @ return boolean
*/
public function testDolTrunc ()
{
2021-02-21 13:32:54 +01:00
// Default trunc (will add … if truncation truncation or keep last char if only one char)
2024-02-19 15:28:21 +01:00
$input = " éeéeéeàa " ;
$after = dol_trunc ( $input , 3 );
2021-02-21 13:32:54 +01:00
$this -> assertEquals ( " éeé… " , $after , 'Test A1' );
2024-02-19 15:28:21 +01:00
$after = dol_trunc ( $input , 2 );
2021-02-21 13:32:54 +01:00
$this -> assertEquals ( " ée… " , $after , 'Test A2' );
2024-02-19 15:28:21 +01:00
$after = dol_trunc ( $input , 1 );
2021-02-21 13:32:54 +01:00
$this -> assertEquals ( " é… " , $after , 'Test A3' );
2024-02-19 15:28:21 +01:00
$input = " éeée " ;
$after = dol_trunc ( $input , 3 );
2021-02-21 15:40:03 +01:00
$this -> assertEquals ( " éeée " , $after , 'Test B1' );
2024-02-19 15:28:21 +01:00
$after = dol_trunc ( $input , 2 );
2021-02-21 15:40:03 +01:00
$this -> assertEquals ( " ée… " , $after , 'Test B2' );
2024-02-19 15:28:21 +01:00
$after = dol_trunc ( $input , 1 );
2021-02-21 13:32:54 +01:00
$this -> assertEquals ( " é… " , $after , 'Test B3' );
2024-02-19 15:28:21 +01:00
$input = " éeée " ;
$after = dol_trunc ( $input , 3 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " éeée " , $after , 'Test C1' );
2024-02-19 15:28:21 +01:00
$after = dol_trunc ( $input , 2 );
2021-02-21 16:05:59 +01:00
$this -> assertEquals ( " ée… " , $after , 'Test C2' );
2024-02-19 15:28:21 +01:00
$after = dol_trunc ( $input , 1 );
2021-02-21 16:05:59 +01:00
$this -> assertEquals ( " é… " , $after , 'Test C3' );
2024-02-19 15:28:21 +01:00
$input = " éeé " ;
$after = dol_trunc ( $input , 3 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " éeé " , $after , 'Test C' );
2024-02-19 15:28:21 +01:00
$after = dol_trunc ( $input , 2 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " éeé " , $after , 'Test D' );
2024-02-19 15:28:21 +01:00
$after = dol_trunc ( $input , 1 );
2021-02-21 16:10:35 +01:00
$this -> assertEquals ( " é… " , $after , 'Test E' );
2021-02-21 13:32:54 +01:00
// Trunc with no …
2024-02-19 15:28:21 +01:00
$input = " éeéeéeàa " ;
$after = dol_trunc ( $input , 3 , 'right' , 'UTF-8' , 1 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " éeé " , $after , 'Test F' );
2024-02-19 15:28:21 +01:00
$after = dol_trunc ( $input , 2 , 'right' , 'UTF-8' , 1 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " ée " , $after , 'Test G' );
2024-02-19 15:28:21 +01:00
$input = " éeé " ;
$after = dol_trunc ( $input , 3 , 'right' , 'UTF-8' , 1 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " éeé " , $after , 'Test H' );
2024-02-19 15:28:21 +01:00
$after = dol_trunc ( $input , 2 , 'right' , 'UTF-8' , 1 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " ée " , $after , 'Test I' );
2024-02-19 15:28:21 +01:00
$after = dol_trunc ( $input , 1 , 'right' , 'UTF-8' , 1 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " é " , $after , 'Test J' );
2024-02-19 15:28:21 +01:00
$input = " éeéeéeàa " ;
$after = dol_trunc ( $input , 4 , 'middle' );
2021-02-21 13:32:54 +01:00
$this -> assertEquals ( " ée…àa " , $after , 'Test K' );
2021-01-14 15:09:08 +01:00
return true ;
}
/**
* testDolMkTime
*
* @ return void
*/
public function testDolMkTime ()
{
global $conf ;
2024-02-19 15:28:21 +01:00
$savtz = date_default_timezone_get ();
2021-01-14 15:09:08 +01:00
// Some test for UTC TZ
date_default_timezone_set ( 'UTC' );
// Check bad hours
2024-02-19 15:28:21 +01:00
$result = dol_mktime ( 25 , 0 , 0 , 1 , 1 , 1970 , 1 , 1 ); // Error (25 hours)
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( '' , $result );
2024-02-19 15:28:21 +01:00
$result = dol_mktime ( 2 , 61 , 0 , 1 , 1 , 1970 , 1 , 1 ); // Error (61 minutes)
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( '' , $result );
2024-02-19 15:28:21 +01:00
$result = dol_mktime ( 2 , 1 , 61 , 1 , 1 , 1970 , 1 , 1 ); // Error (61 seconds)
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( '' , $result );
2024-02-19 15:28:21 +01:00
$result = dol_mktime ( 2 , 1 , 1 , 1 , 32 , 1970 , 1 , 1 ); // Error (day 32)
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( '' , $result );
2024-02-19 15:28:21 +01:00
$result = dol_mktime ( 2 , 1 , 1 , 13 , 1 , 1970 , 1 , 1 ); // Error (month 13)
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( '' , $result );
2024-02-19 15:28:21 +01:00
$result = dol_mktime ( 2 , 1 , 1 , 1 , 1 , 1970 , 1 ); // 1970-01-01 02:01:01 in GMT area -> 7261
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( 7261 , $result );
2024-02-19 15:28:21 +01:00
$result = dol_mktime ( 2 , 0 , 0 , 1 , 1 , 1970 , 0 ); // 1970-01-01 02:00:00 = 7200 in local area Europe/Paris = 3600 GMT
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
2024-02-19 15:28:21 +01:00
$tz = getServerTimeZoneInt ( 'winter' ); // +1 in Europe/Paris at this time (this time is winter)
$this -> assertEquals ( 7200 - ( $tz * 3600 ), $result ); // 7200 if we are at greenwich winter, 7200-($tz*3600) at local winter
2021-01-14 15:09:08 +01:00
// Some test for local TZ Europe/Paris
date_default_timezone_set ( 'Europe/Paris' );
// Check that tz for paris in winter is used
2024-02-19 15:28:21 +01:00
$result = dol_mktime ( 2 , 0 , 0 , 1 , 1 , 1970 , 'server' ); // 1970-01-01 02:00:00 = 7200 in local area Europe/Paris = 3600 GMT
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( 3600 , $result ); // 7200 if we are at greenwich winter, 3600 at Europe/Paris
// Check that daylight saving time is used
2024-02-19 15:28:21 +01:00
$result = dol_mktime ( 2 , 0 , 0 , 6 , 1 , 2014 , 0 ); // 2014-06-01 02:00:00 = 1401588000-3600(location)-3600(daylight) in local area Europe/Paris = 1401588000 GMT
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
2024-02-19 15:28:21 +01:00
$this -> assertEquals ( 1401588000 - 3600 - 3600 , $result ); // 1401588000 are at greenwich summer, 1401588000-3600(location)-3600(daylight) at Europe/Paris summer
2021-01-14 15:09:08 +01:00
date_default_timezone_set ( $savtz );
}
/**
* testDolEscapeJs
*
* @ return void
*/
public function testDolEscapeJs ()
{
2024-02-19 15:28:21 +01:00
$input = " x&<b>#</b>, \" ' " ; // " will be converted into '
$result = dol_escape_js ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " x&<b>#</b>, \ ' \ ' " , $result , " Test mode=0 " );
2024-02-19 15:28:21 +01:00
$result = dol_escape_js ( $input , 1 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " x&<b>#</b>, \" \ ' " , $result , " Test mode=1 " );
2024-02-19 15:28:21 +01:00
$result = dol_escape_js ( $input , 2 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " x&<b>#</b>, \\ \" ' " , $result , " Test mode=2 " );
}
/**
2024-07-22 20:12:35 +02:00
* testDolEscapeHtmlTag
*
* @ return void
*/
2021-01-14 15:09:08 +01:00
public function testDolEscapeHtmlTag ()
{
2024-02-19 15:28:21 +01:00
$input = 'x&<b>#</b>,"' ; // & and " are converted into html entities, <b> are removed
$result = dol_escape_htmltag ( $input );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 'x&#,"' , $result );
2024-02-19 15:28:21 +01:00
$input = 'x&<b>#</b>,"' ; // & and " are converted into html entities, <b> are not removed
$result = dol_escape_htmltag ( $input , 1 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 'x&<b>#</b>,"' , $result );
2024-06-08 14:51:32 +02:00
2025-01-27 10:16:28 +01:00
$input = '<img alt="" src="https://github.githubassets.com/assets/GitHub%20Mark-ea2971cee799.png">' ; // & and " are converted into html entities, <b> are not removed
$result = dol_escape_htmltag ( $input , 1 , 1 , 'common' , 0 , 1 );
$this -> assertEquals ( '<img alt="" src="https://github.githubassets.com/assets/GitHub%20Mark-ea2971cee799.png">' , $result );
$input = '<img src="data:image/png;base64, 123/456+789==" style="height: 123px; width:456px">' ; // & and " are converted into html entities, <b> are not removed
$result = dol_escape_htmltag ( $input , 1 , 1 , 'common' );
$this -> assertEquals ( '<img src="data:image/png;base64, 123/456+789==" style="height: 123px; width:456px">' , $result );
$input = '<img src="data:image/png;base64, 123/456+789==" style="height: 123px; width:456px">' ; // & and " are converted into html entities, <b> are not removed
$result = dol_escape_htmltag ( $input , 1 );
$this -> assertEquals ( '<img src="data:image/png;base64, 123/456+789==" style="height: 123px; width:456px">' , $result );
2024-06-08 14:51:32 +02:00
$input = '<img alt="" src="https://github.githubassets.com/assets/GitHub-Mark-ea2971cee799.png">' ; // & and " are converted into html entities, <b> are not removed
$result = dol_escape_htmltag ( $input , 1 , 1 , 'common' , 0 , 1 );
$this -> assertEquals ( '<img alt="" src="https://github.githubassets.com/assets/GitHub-Mark-ea2971cee799.png">' , $result );
2024-07-22 20:12:35 +02:00
$input = ' < div style = " float:left; margin-left:0px; margin-right:5px " >
< img id = " sigPhoto " src = " https://www.domain.com/aaa.png " style = " height:65px; width:65px " />
</ div >
< div style = " margin-left:74px " >< strong > A text here </ strong > and more < br >
< a href = " mailto:abc+def@domain.com " id = " sigEmail " style = " color:#428BCA; " > abc + def @ domain . com </ a >< br >
< a href = " https://www.another-domain.com " id = " sigWebsite " style = " color:#428BCA; " > https :// www . another - domain . com </ a >< br >
</ div > ' ;
$result = dol_escape_htmltag ( $input , 1 , 1 , 'common' );
$this -> assertEquals ( $input , $result );
2021-01-14 15:09:08 +01:00
}
/**
* testDolFormatAddress
*
* @ return void
*/
public function testDolFormatAddress ()
{
global $conf , $user , $langs , $db ;
2024-02-19 15:28:21 +01:00
$conf = $this -> savconf ;
$user = $this -> savuser ;
$langs = $this -> savlangs ;
$db = $this -> savdb ;
2021-01-14 15:09:08 +01:00
2024-02-19 15:28:21 +01:00
$object = new Societe ( $db );
2021-01-14 15:09:08 +01:00
$object -> initAsSpecimen ();
2024-02-19 15:28:21 +01:00
$object -> country_code = 'FR' ;
$address = dol_format_address ( $object );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " 21 jump street \n 99999 MyTown " , $address );
2024-02-19 15:28:21 +01:00
$object -> country_code = 'GB' ;
$address = dol_format_address ( $object );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " 21 jump street \n MyTown, MyState \n 99999 " , $address );
2024-02-19 15:28:21 +01:00
$object -> country_code = 'US' ;
$address = dol_format_address ( $object );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " 21 jump street \n MyTown, MyState, 99999 " , $address );
2024-02-19 15:28:21 +01:00
$object -> country_code = 'AU' ;
$address = dol_format_address ( $object );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( " 21 jump street \n MyTown, MyState, 99999 " , $address );
2021-09-02 10:39:56 +02:00
2024-02-19 15:28:21 +01:00
$object -> country_code = 'JP' ;
$address = dol_format_address ( $object );
2021-09-02 10:39:56 +02:00
$this -> assertEquals ( " 21 jump street \n MyState, MyTown 99999 " , $address );
2021-01-14 15:09:08 +01:00
}
/**
2023-09-29 16:53:54 +02:00
* testDolPrintPhone
2021-01-14 15:09:08 +01:00
*
* @ return void
*/
public function testDolPrintPhone ()
{
global $conf , $user , $langs , $db ;
2024-02-19 15:28:21 +01:00
$conf = $this -> savconf ;
$user = $this -> savuser ;
$langs = $this -> savlangs ;
$db = $this -> savdb ;
2021-01-14 15:09:08 +01:00
2024-02-19 15:28:21 +01:00
$object = new Societe ( $db );
2021-01-14 15:09:08 +01:00
$object -> initAsSpecimen ();
2024-02-19 15:28:21 +01:00
$object -> country_code = 'FR' ;
$phone = dol_print_phone ( '1234567890' , $object -> country_code );
2024-07-26 04:10:10 +02:00
$this -> assertEquals ( '<span class="paddingright">12 34 56 78 90</span>' , $phone , 'Phone for FR 1' );
2021-01-14 15:09:08 +01:00
2024-02-19 15:28:21 +01:00
$object -> country_code = 'FR' ;
$phone = dol_print_phone ( '1234567890' , $object -> country_code , 0 , 0 , 0 , '' );
2024-07-26 04:10:10 +02:00
$this -> assertEquals ( '<span class="paddingright">1234567890</span>' , $phone , 'Phone for FR 2' );
2021-01-14 15:09:08 +01:00
2024-02-19 15:28:21 +01:00
$object -> country_code = 'FR' ;
$phone = dol_print_phone ( '1234567890' , $object -> country_code , 0 , 0 , 0 , ' ' );
2024-07-26 04:10:10 +02:00
$this -> assertEquals ( '<span class="paddingright">12 34 56 78 90</span>' , $phone , 'Phone for FR 3' );
2021-01-14 15:09:08 +01:00
2024-02-19 15:28:21 +01:00
$object -> country_code = 'CA' ;
$phone = dol_print_phone ( '1234567890' , $object -> country_code , 0 , 0 , 0 , ' ' );
2024-07-26 04:10:10 +02:00
$this -> assertEquals ( '<span class="paddingright">(123) 456-7890</span>' , $phone , 'Phone for CA 1' );
2021-01-14 15:09:08 +01:00
}
/**
* testImgPicto
*
* @ return void
*/
public function testImgPicto ()
{
2024-02-19 15:28:21 +01:00
$s = img_picto ( 'title' , 'user' );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " s= " . $s . " \n " ;
2023-09-10 15:16:46 +02:00
$this -> assertStringContainsStringIgnoringCase ( 'fa-user' , $s , 'testImgPicto1' );
2021-01-14 15:09:08 +01:00
2024-02-19 15:28:21 +01:00
$s = img_picto ( 'title' , 'img.png' , 'style="float: right"' , 0 );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " s= " . $s . " \n " ;
2023-09-10 15:16:46 +02:00
$this -> assertStringContainsStringIgnoringCase ( 'theme' , $s , 'testImgPicto2' );
$this -> assertStringContainsStringIgnoringCase ( 'style="float: right"' , $s , 'testImgPicto2' );
2021-01-14 15:09:08 +01:00
2024-02-19 15:28:21 +01:00
$s = img_picto ( 'title' , '/fullpath/img.png' , '' , 1 );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " s= " . $s . " \n " ;
$this -> assertEquals ( '<img src="/fullpath/img.png" alt="" title="title" class="inline-block">' , $s , 'testImgPicto3' );
2024-02-19 15:28:21 +01:00
$s = img_picto ( 'title' , '/fullpath/img.png' , '' , true );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " s= " . $s . " \n " ;
$this -> assertEquals ( '<img src="/fullpath/img.png" alt="" title="title" class="inline-block">' , $s , 'testImgPicto4' );
2024-02-19 15:28:21 +01:00
$s = img_picto ( 'title' , 'delete' , '' , 0 , 1 );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " s= " . $s . " \n " ;
$this -> assertEquals ( DOL_URL_ROOT . '/theme/eldy/img/delete.png' , $s , 'testImgPicto5' );
}
/**
* testDolNow
*
* @ return void
*/
public function testDolNow ()
{
2024-02-19 15:28:21 +01:00
$now = dol_now ( 'gmt' );
$nowtzserver = dol_now ( 'tzserver' );
print __METHOD__ . " getServerTimeZoneInt= " . ( getServerTimeZoneInt ( 'now' ) * 3600 ) . " \n " ;
$this -> assertEquals ( getServerTimeZoneInt ( 'now' ) * 3600 , ( $nowtzserver - $now ));
2021-01-14 15:09:08 +01:00
}
2024-03-09 01:27:01 +01:00
/**
* Data provider for testVerifCond
*
* @ return array < string , array { 0 : string , 1 : bool } >
*/
public function verifCondDataProvider () : array
{
return [
'Test a true comparison' => [ '1==1' , true ,],
'Test a false comparison' => [ '1==2' , false ,],
'Test that the conf property of a module reports true when enabled' => [ 'isModEnabled("facture")' , true ,],
'Test that the conf property of a module reports false when disabled' => [ 'isModEnabled("moduledummy")' , false ,],
'Test that verifConf(0) returns false' => [ 0 , false ,],
'Test that verifConf("0") returns false' => [ " 0 " , false ,],
'Test that verifConf("") returns false (special case)' => [ '' , true ,],
];
}
2021-01-14 15:09:08 +01:00
/**
* testVerifCond
*
2024-03-09 01:27:01 +01:00
* @ dataProvider verifCondDataProvider
*
* @ param string $cond Condition to test using verifCond
* @ param string $expected Expected outcome of verifCond
*
2021-01-14 15:09:08 +01:00
* @ return void
*/
2024-03-09 01:27:01 +01:00
public function testVerifCond ( $cond , $expected )
2021-01-14 15:09:08 +01:00
{
2024-03-09 01:27:01 +01:00
if ( $expected ) {
$this -> assertTrue ( verifCond ( $cond ));
} else {
$this -> assertFalse ( verifCond ( $cond ));
}
2021-01-14 15:09:08 +01:00
}
/**
* testGetDefaultTva
*
* @ return void
*/
public function testGetDefaultTva ()
{
global $conf , $user , $langs , $db ;
2024-02-19 15:28:21 +01:00
$this -> savconf = $conf ;
$this -> savuser = $user ;
$this -> savlangs = $langs ;
$this -> savdb = $db ;
2021-01-14 15:09:08 +01:00
// Sellers
2024-02-19 15:28:21 +01:00
$companyfrnovat = new Societe ( $db );
$companyfrnovat -> country_code = 'FR' ;
$companyfrnovat -> tva_assuj = 0 ;
2021-01-14 15:09:08 +01:00
2024-02-19 15:28:21 +01:00
$companyfr = new Societe ( $db );
$companyfr -> country_code = 'FR' ;
$companyfr -> tva_assuj = 1 ;
$companyfr -> tva_intra = 'FR9999' ;
2021-01-14 15:09:08 +01:00
// Buyers
2024-02-19 15:28:21 +01:00
$companymc = new Societe ( $db );
$companymc -> country_code = 'MC' ;
$companymc -> tva_assuj = 1 ;
$companyfr -> tva_intra = 'MC9999' ;
2021-01-14 15:09:08 +01:00
2024-02-19 15:28:21 +01:00
$companyit = new Societe ( $db );
$companyit -> country_code = 'IT' ;
$companyit -> tva_assuj = 1 ;
$companyit -> tva_intra = 'IT99999' ;
2021-01-14 15:09:08 +01:00
2024-02-19 15:28:21 +01:00
$companyde = new Societe ( $db );
$companyde -> country_code = 'DE' ;
$companyde -> tva_assuj = 1 ;
$companyde -> tva_intra = 'DE99999' ;
2021-01-14 15:09:08 +01:00
2024-02-19 15:28:21 +01:00
$notcompanyde = new Societe ( $db );
$notcompanyde -> country_code = 'DE' ;
$notcompanyde -> tva_assuj = 0 ;
$notcompanyde -> tva_intra = '' ;
$notcompanyde -> typent_code = 'TE_PRIVATE' ;
2021-01-14 15:09:08 +01:00
2024-02-19 15:28:21 +01:00
$companyus = new Societe ( $db );
$companyus -> country_code = 'US' ;
$companyus -> tva_assuj = 1 ;
$companyus -> tva_intra = '' ;
2021-01-14 15:09:08 +01:00
// Test RULE 0 (FR-DE)
// Not tested
// Test RULE 1
2024-02-19 15:28:21 +01:00
$vat = get_default_tva ( $companyfrnovat , $companymc , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 0 , $vat , 'RULE 1' );
// Test RULE 2 (FR-FR)
2024-02-19 15:28:21 +01:00
$vat = get_default_tva ( $companyfr , $companyfr , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 20 , $vat , 'RULE 2' );
// Test RULE 2 (FR-MC)
2024-02-19 15:28:21 +01:00
$vat = get_default_tva ( $companyfr , $companymc , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 20 , $vat , 'RULE 2' );
// Test RULE 3 (FR-DE company)
2024-02-19 15:28:21 +01:00
$vat = get_default_tva ( $companyfr , $companyit , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 0 , $vat , 'RULE 3' );
// Test RULE 4 (FR-DE not a company)
2024-02-19 15:28:21 +01:00
$vat = get_default_tva ( $companyfr , $notcompanyde , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 20 , $vat , 'RULE 4' );
// Test RULE 5 (FR-US)
2024-02-19 15:28:21 +01:00
$vat = get_default_tva ( $companyfr , $companyus , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 0 , $vat , 'RULE 5' );
// We do same tests but with option SERVICE_ARE_ECOMMERCE_200238EC on.
$conf -> global -> SERVICE_ARE_ECOMMERCE_200238EC = 1 ;
// Test RULE 1 (FR-US)
2024-02-19 15:28:21 +01:00
$vat = get_default_tva ( $companyfr , $companyus , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 0 , $vat , 'RULE 1 ECOMMERCE_200238EC' );
// Test RULE 2 (FR-FR)
2024-02-19 15:28:21 +01:00
$vat = get_default_tva ( $companyfr , $companyfr , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 20 , $vat , 'RULE 2 ECOMMERCE_200238EC' );
// Test RULE 3 (FR-DE company)
2024-02-19 15:28:21 +01:00
$vat = get_default_tva ( $companyfr , $companyde , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 0 , $vat , 'RULE 3 ECOMMERCE_200238EC' );
// Test RULE 4 (FR-DE not a company)
2024-02-19 15:28:21 +01:00
$vat = get_default_tva ( $companyfr , $notcompanyde , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 19 , $vat , 'RULE 4 ECOMMERCE_200238EC' );
// Test RULE 5 (FR-US)
2024-02-19 15:28:21 +01:00
$vat = get_default_tva ( $companyfr , $companyus , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 0 , $vat , 'RULE 5 ECOMMERCE_200238EC' );
}
/**
2023-03-18 10:48:09 +01:00
* testGetDefaultLocalTax
2021-01-14 15:09:08 +01:00
*
* @ return void
*/
public function testGetDefaultLocalTax ()
{
global $conf , $user , $langs , $db ;
2024-02-19 15:28:21 +01:00
$this -> savconf = $conf ;
$this -> savuser = $user ;
$this -> savlangs = $langs ;
$this -> savdb = $db ;
$companyfrnovat = new Societe ( $db );
$companyfrnovat -> country_code = 'FR' ;
$companyfrnovat -> tva_assuj = 0 ;
$companyfrnovat -> localtax1_assuj = 0 ;
$companyfrnovat -> localtax2_assuj = 0 ;
$companyes = new Societe ( $db );
$companyes -> country_code = 'ES' ;
$companyes -> tva_assuj = 1 ;
$companyes -> localtax1_assuj = 1 ;
$companyes -> localtax2_assuj = 1 ;
$companymc = new Societe ( $db );
$companymc -> country_code = 'MC' ;
$companymc -> tva_assuj = 1 ;
$companymc -> localtax1_assuj = 0 ;
$companymc -> localtax2_assuj = 0 ;
$companyit = new Societe ( $db );
$companyit -> country_code = 'IT' ;
$companyit -> tva_assuj = 1 ;
$companyit -> tva_intra = 'IT99999' ;
$companyit -> localtax1_assuj = 0 ;
$companyit -> localtax2_assuj = 0 ;
$notcompanyit = new Societe ( $db );
$notcompanyit -> country_code = 'IT' ;
$notcompanyit -> tva_assuj = 1 ;
$notcompanyit -> tva_intra = '' ;
$notcompanyit -> typent_code = 'TE_PRIVATE' ;
$notcompanyit -> localtax1_assuj = 0 ;
$notcompanyit -> localtax2_assuj = 0 ;
$companyus = new Societe ( $db );
$companyus -> country_code = 'US' ;
$companyus -> tva_assuj = 1 ;
$companyus -> tva_intra = '' ;
$companyus -> localtax1_assuj = 0 ;
$companyus -> localtax2_assuj = 0 ;
2021-01-14 15:09:08 +01:00
// Test RULE FR-MC
2024-02-19 15:28:21 +01:00
$vat1 = get_default_localtax ( $companyfrnovat , $companymc , 1 , 0 );
$vat2 = get_default_localtax ( $companyfrnovat , $companymc , 2 , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 0 , $vat1 );
$this -> assertEquals ( 0 , $vat2 );
// Test RULE ES-ES
2024-02-19 15:28:21 +01:00
$vat1 = get_default_localtax ( $companyes , $companyes , 1 , 0 );
$vat2 = get_default_localtax ( $companyes , $companyes , 2 , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( $vat1 , 5.2 );
$this -> assertStringStartsWith (( string ) $vat2 , '-19:-15:-9' ); // Can be -19 (old version) or '-19:-15:-9' (new setup)
// Test RULE ES-IT
2024-02-19 15:28:21 +01:00
$vat1 = get_default_localtax ( $companyes , $companyit , 1 , 0 );
$vat2 = get_default_localtax ( $companyes , $companyit , 2 , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 0 , $vat1 );
$this -> assertEquals ( 0 , $vat2 );
// Test RULE ES-IT
2024-02-19 15:28:21 +01:00
$vat1 = get_default_localtax ( $companyes , $notcompanyit , 1 , 0 );
$vat2 = get_default_localtax ( $companyes , $notcompanyit , 2 , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 0 , $vat1 );
$this -> assertEquals ( 0 , $vat2 );
// Test RULE FR-IT
// Not tested
// Test RULE ES-US
2024-02-19 15:28:21 +01:00
$vat1 = get_default_localtax ( $companyes , $companyus , 1 , 0 );
$vat2 = get_default_localtax ( $companyes , $companyus , 2 , 0 );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 0 , $vat1 );
$this -> assertEquals ( 0 , $vat2 );
}
2023-03-18 10:48:09 +01:00
/**
* testGetLocalTaxByThird
*
* @ return void
*/
public function testGetLocalTaxByThird ()
{
global $mysoc ;
$mysoc -> country_code = 'ES' ;
$result = get_localtax_by_third ( 1 );
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( '5.2' , $result );
$result = get_localtax_by_third ( 2 );
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( '-19:-15:-9' , $result );
}
2021-01-14 15:09:08 +01:00
/**
* testDolExplodeIntoArray
*
* @ return void
*/
public function testDolExplodeIntoArray ()
{
2024-02-19 15:28:21 +01:00
$stringtoexplode = 'AA=B/B.CC=.EE=FF.HH=GG;.' ;
$tmp = dolExplodeIntoArray ( $stringtoexplode , '.' , '=' );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " tmp= " . json_encode ( $tmp ) . " \n " ;
$this -> assertEquals ( '{"AA":"B\/B","CC":"","EE":"FF","HH":"GG;"}' , json_encode ( $tmp ));
2023-04-07 16:21:51 +02:00
2024-02-19 15:28:21 +01:00
$stringtoexplode = " AA=B/B;CC= \n \r EE=FF \n HH=GG;;; \n II=JJ \n " ;
$tmp = dolExplodeIntoArray ( $stringtoexplode , " ( \r \n | \n | \r |;) " , '=' );
2023-04-07 16:21:51 +02:00
print __METHOD__ . " tmp= " . json_encode ( $tmp ) . " \n " ;
$this -> assertEquals ( '{"AA":"B\/B","CC":"","EE":"FF","HH":"GG","II":"JJ"}' , json_encode ( $tmp ));
2021-01-14 15:09:08 +01:00
}
/**
* dol_nl2br
*
* @ return void
*/
public function testDolNl2Br ()
{
//String to encode
$string = " a \n a " ;
$this -> assertEquals ( dol_nl2br ( $string ), " a<br> \n a " );
//With $forxml parameter
$this -> assertEquals ( dol_nl2br ( $string , 0 , 1 ), " a<br /> \n a " );
//Replacing \n by br
$this -> assertEquals ( dol_nl2br ( $string , 1 ), " a<br>a " );
//With $forxml parameter
$this -> assertEquals ( dol_nl2br ( $string , 1 , 1 ), " a<br />a " );
}
/**
* testDolPrice2Num
*
* @ return boolean
*/
public function testDolPrice2Num ()
{
global $langs , $conf ;
$oldlangs = $langs ;
$newlangs = new Translate ( '' , $conf );
$newlangs -> setDefaultLang ( 'en_US' );
$newlangs -> load ( " main " );
$langs = $newlangs ;
$this -> assertEquals ( 150 , price2num ( '(SELECT/**/CASE/**/WHEN/**/(0<1)/**/THEN/**/SLEEP(5)/**/ELSE/**/SLEEP(0)/**/END)' ));
$this -> assertEquals ( 1000 , price2num ( '1 000.0' ));
$this -> assertEquals ( 1000 , price2num ( '1 000' , 'MT' ));
$this -> assertEquals ( 1000 , price2num ( '1 000' , 'MU' ));
$this -> assertEquals ( 1000.123456 , price2num ( '1 000.123456' ));
// Round down
2021-03-03 12:05:43 +01:00
$this -> assertEquals ( 1000.12 , price2num ( '1 000.123452' , 'MT' ), 'Error in round down with MT' );
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 1000.12345 , price2num ( '1 000.123452' , 'MU' ), " Test MU " );
// Round up
$this -> assertEquals ( 1000.13 , price2num ( '1 000.125456' , 'MT' ));
$this -> assertEquals ( 1000.12546 , price2num ( '1 000.125456' , 'MU' ), " Test MU " );
$this -> assertEquals ( 1 , price2num ( '1.000' ), 'Test 1.000 give 1 with english language' );
// Text can't be converted
2021-11-11 18:34:04 +01:00
$this -> assertEquals ( '12.4' , price2num ( '12.4$' ));
$this -> assertEquals ( '12.4' , price2num ( '12r.4$' ));
2021-01-14 15:09:08 +01:00
// For spanish language
$newlangs2 = new Translate ( '' , $conf );
$newlangs2 -> setDefaultLang ( 'es_ES' );
$newlangs2 -> load ( " main " );
$langs = $newlangs2 ;
// Test with 3 chars after . or ,
// If a . is used and there is 3 digits after, it is a thousand separator
$this -> assertEquals ( 1234 , price2num ( '1.234' , '' , 2 ), 'Test 1.234 give 1234 with spanish language if user input' );
$this -> assertEquals ( 1.234 , price2num ( '1,234' , '' , 2 ), 'Test 1,234 give 1234 with spanish language if user input' );
$this -> assertEquals ( 1234 , price2num ( '1 234' , '' , 2 ), 'Test 1 234 give 1234 with spanish language if user input' );
$this -> assertEquals ( - 1.234 , price2num ( '-1.234' ), 'Test 1.234 give 1.234 with spanish language' );
$this -> assertEquals ( - 1.234 , price2num ( '-1,234' ), 'Test 1,234 give 1234 with spanish language' );
$this -> assertEquals ( - 1234 , price2num ( '-1 234' ), 'Test 1 234 give 1234 with spanish language' );
$this -> assertEquals ( 21500123 , price2num ( '21.500.123' ), 'Test 21.500.123 give 21500123 with spanish language' );
$this -> assertEquals ( 21500123 , price2num ( '21500.123' , 0 , 2 ), 'Test 21500.123 give 21500123 with spanish language if user input' );
$this -> assertEquals ( 21500.123 , price2num ( '21500.123' ), 'Test 21500.123 give 21500123 with spanish language' );
$this -> assertEquals ( 21500.123 , price2num ( '21500,123' ), 'Test 21500,123 give 21500.123 with spanish language' );
// Test with 2 digits
$this -> assertEquals ( 21500.12 , price2num ( '21500.12' ), 'Test 21500.12 give 21500.12 with spanish language' );
$this -> assertEquals ( 21500.12 , price2num ( '21500,12' ), 'Test 21500,12 give 21500.12 with spanish language' );
// Test with 3 digits
$this -> assertEquals ( 12123 , price2num ( '12.123' , '' , 2 ), 'Test 12.123 give 12123 with spanish language if user input' );
$this -> assertEquals ( 12.123 , price2num ( '12,123' , '' , 2 ), 'Test 12,123 give 12.123 with spanish language if user input' );
$this -> assertEquals ( 12.123 , price2num ( '12.123' ), 'Test 12.123 give 12.123 with spanish language' );
$this -> assertEquals ( 12.123 , price2num ( '12,123' ), 'Test 12,123 give 12.123 with spanish language' );
// For french language
$newlangs3 = new Translate ( '' , $conf );
$newlangs3 -> setDefaultLang ( 'fr_FR' );
$newlangs3 -> load ( " main " );
$langs = $newlangs3 ;
$this -> assertEquals ( 1 , price2num ( '1.000' , '' , 2 ), 'Test 1.000 give 1 with french language if user input' );
$this -> assertEquals ( 1 , price2num ( '1.000' ), 'Test 1.000 give 1 with french language' );
$this -> assertEquals ( 1000 , price2num ( '1 000' ), 'Test 1.000 give 1 with french language' );
$this -> assertEquals ( 1.234 , price2num ( '1.234' , '' , 2 ), 'Test 1.234 give 1.234 with french language if user input' );
$this -> assertEquals ( 1.234 , price2num ( '1.234' ), 'Test 1.234 give 1.234 with french language' );
$this -> assertEquals ( 1.234 , price2num ( '1,234' , '' , 2 ), 'Test 1,234 give 1.234 with french language if user input' );
$this -> assertEquals ( 1.234 , price2num ( '1,234' ), 'Test 1,234 give 1.234 with french language' );
$this -> assertEquals ( 21500000 , price2num ( '21500 000' ), 'Test 21500 000 give 21500000 with french language' );
$this -> assertEquals ( 21500000 , price2num ( '21 500 000' ), 'Test 21 500 000 give 21500000 with french language' );
$this -> assertEquals ( 21500 , price2num ( '21500.00' ), 'Test 21500.00 give 21500 with french language' );
$this -> assertEquals ( 21500 , price2num ( '21500,00' ), 'Test 21500,00 give 21500 with french language' );
$langs = $oldlangs ;
return true ;
}
/**
* testDolGetDate
*
* @ return boolean
*/
public function testDolGetDate ()
{
global $conf ;
$conf -> global -> MAIN_START_WEEK = 0 ;
2024-02-19 15:28:21 +01:00
$tmp = dol_getdate ( 24 * 60 * 60 + 1 , false , 'UTC' ); // 2/1/1970 and 1 second = friday
2021-05-04 13:33:18 +02:00
$this -> assertEquals ( 5 , $tmp [ 'wday' ], 'Bad value of day in week' );
2021-01-14 15:09:08 +01:00
$conf -> global -> MAIN_START_WEEK = 1 ;
2024-02-19 15:28:21 +01:00
$tmp = dol_getdate ( 1 , false , 'UTC' ); // 1/1/1970 and 1 second = thirday
2021-05-04 13:33:18 +02:00
$this -> assertEquals ( 4 , $tmp [ 'wday' ], 'Bad value of day in week' );
2021-01-14 15:09:08 +01:00
2024-02-19 15:28:21 +01:00
$tmp = dol_getdate ( 24 * 60 * 60 + 1 , false , 'UTC' ); // 2/1/1970 and 1 second = friday
2021-05-04 13:33:18 +02:00
$this -> assertEquals ( 5 , $tmp [ 'wday' ], 'Bad value of day in week' );
2021-01-14 15:09:08 +01:00
2024-02-19 15:28:21 +01:00
$tmp = dol_getdate ( 1 , false , " Europe/Paris " ); // 1/1/1970 and 1 second = thirday
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 1970 , $tmp [ 'year' ]);
$this -> assertEquals ( 1 , $tmp [ 'mon' ]);
$this -> assertEquals ( 1 , $tmp [ 'mday' ]);
$this -> assertEquals ( 4 , $tmp [ 'wday' ]);
$this -> assertEquals ( 0 , $tmp [ 'yday' ]);
$this -> assertEquals ( 1 , $tmp [ 'hours' ]); // We are winter, so we are GMT+1 even during summer
$this -> assertEquals ( 0 , $tmp [ 'minutes' ]);
$this -> assertEquals ( 1 , $tmp [ 'seconds' ]);
2024-02-19 15:28:21 +01:00
$tmp = dol_getdate ( 15638401 , false , " Europe/Paris " ); // 1/7/1970 and 1 second = wednesday
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 1970 , $tmp [ 'year' ]);
$this -> assertEquals ( 7 , $tmp [ 'mon' ]);
$this -> assertEquals ( 1 , $tmp [ 'mday' ]);
$this -> assertEquals ( 3 , $tmp [ 'wday' ]);
$this -> assertEquals ( 181 , $tmp [ 'yday' ]);
$this -> assertEquals ( 1 , $tmp [ 'hours' ]); // There is no daylight in 1970, so we are GMT+1 even during summer
$this -> assertEquals ( 0 , $tmp [ 'minutes' ]);
$this -> assertEquals ( 1 , $tmp [ 'seconds' ]);
2024-02-19 15:28:21 +01:00
$tmp = dol_getdate ( 1593561601 , false , " Europe/Paris " ); // 1/7/2020 and 1 second = wednesday
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 2020 , $tmp [ 'year' ]);
$this -> assertEquals ( 7 , $tmp [ 'mon' ]);
$this -> assertEquals ( 1 , $tmp [ 'mday' ]);
$this -> assertEquals ( 3 , $tmp [ 'wday' ]);
$this -> assertEquals ( 182 , $tmp [ 'yday' ]); // 182 and not 181, due to the 29th february
$this -> assertEquals ( 2 , $tmp [ 'hours' ]); // There is a daylight, so we are GMT+2
$this -> assertEquals ( 0 , $tmp [ 'minutes' ]);
$this -> assertEquals ( 1 , $tmp [ 'seconds' ]);
2024-02-19 15:28:21 +01:00
$tmp = dol_getdate ( 1 , false , 'UTC' ); // 1/1/1970 and 1 second = thirday
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 1970 , $tmp [ 'year' ]);
$this -> assertEquals ( 1 , $tmp [ 'mon' ]);
$this -> assertEquals ( 1 , $tmp [ 'mday' ]);
$this -> assertEquals ( 4 , $tmp [ 'wday' ]);
$this -> assertEquals ( 0 , $tmp [ 'yday' ]);
// We must disable this because on CI, timezone is may be UTC or something else
2021-05-04 13:33:18 +02:00
//$this->assertEquals(1, $tmp['hours']); // We are winter, so we are GMT+1 even during summer
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 0 , $tmp [ 'minutes' ]);
$this -> assertEquals ( 1 , $tmp [ 'seconds' ]);
2024-02-19 15:28:21 +01:00
$tmp = dol_getdate ( 15638401 , false , 'UTC' ); // 1/7/1970 and 1 second = wednesday
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 1970 , $tmp [ 'year' ]);
$this -> assertEquals ( 7 , $tmp [ 'mon' ]);
$this -> assertEquals ( 1 , $tmp [ 'mday' ]);
$this -> assertEquals ( 3 , $tmp [ 'wday' ]);
$this -> assertEquals ( 181 , $tmp [ 'yday' ]);
// We must disable this because on CI, timezone is may be UTC or something else
2021-05-04 13:33:18 +02:00
//$this->assertEquals(1, $tmp['hours']); // There is no daylight in 1970, so we are GMT+1 even during summer
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 0 , $tmp [ 'minutes' ]);
$this -> assertEquals ( 1 , $tmp [ 'seconds' ]);
2024-02-19 15:28:21 +01:00
$tmp = dol_getdate ( 1593561601 , false , 'UTC' ); // 1/7/2020 and 1 second = wednesday
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 2020 , $tmp [ 'year' ]);
$this -> assertEquals ( 7 , $tmp [ 'mon' ]);
$this -> assertEquals ( 1 , $tmp [ 'mday' ]);
$this -> assertEquals ( 3 , $tmp [ 'wday' ]);
$this -> assertEquals ( 182 , $tmp [ 'yday' ]); // 182 and not 181, due to the 29th february
// We must disable this because on CI, timezone is may be UTC or something else
2021-05-04 13:33:18 +02:00
//$this->assertEquals(2, $tmp['hours']); // There is a daylight, so we are GMT+2
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( 0 , $tmp [ 'minutes' ]);
$this -> assertEquals ( 1 , $tmp [ 'seconds' ]);
return true ;
}
/**
* testMakeSubstitutions
*
* @ return boolean
*/
public function testMakeSubstitutions ()
{
2021-05-04 13:33:18 +02:00
global $conf , $langs , $mysoc ;
2021-01-14 15:09:08 +01:00
$langs -> load ( " main " );
2021-05-04 13:33:18 +02:00
// Try simple replacement
2024-02-19 15:28:21 +01:00
$substit = array ( " __AAA__ " => 'Not used' , " __BBB__ " => 'Not used' , " __CCC__ " => " C instead " , " DDD " => " D instead " );
2021-05-04 13:33:18 +02:00
$substit += getCommonSubstitutionArray ( $langs );
$chaine = 'This is a string with theme constant __[MAIN_THEME]__ and __(DIRECTION)__ and __CCC__ and DDD and __MYCOMPANY_NAME__ and __YEAR__' ;
2021-01-14 15:09:08 +01:00
$newstring = make_substitutions ( $chaine , $substit );
2021-05-04 13:33:18 +02:00
print __METHOD__ . " " . $newstring . " \n " ;
2023-12-19 14:03:17 +01:00
$this -> assertEquals ( $newstring , 'This is a string with theme constant eldy and ltr and C instead and D instead and ' . $mysoc -> name . ' and ' . dol_print_date ( dol_now (), '%Y' , 'gmt' ));
2021-05-04 13:33:18 +02:00
// Try mix HTML not HTML, no change on initial text
2024-02-19 15:28:21 +01:00
$substit = array ( " __NOHTML__ " => 'No html' , " __HTML__ " => '<b>HTML</b>' );
2021-05-04 13:33:18 +02:00
$chaine = " This is a text with \n New line \n Then \n __NOHTML__ \n Then \n __HTML__ " ;
$newstring = make_substitutions ( $chaine , $substit , $langs );
print __METHOD__ . " " . $newstring . " \n " ;
$this -> assertEquals ( $newstring , " This is a text with \n New line \n Then \n No html \n Then \n <b>HTML</b> " , 'Test on make_substitutions with conversion of inserted values only' );
// Try mix HTML not HTML, accept to change initial text
2024-02-19 15:28:21 +01:00
$substit = array ( " __NOHTML__ " => 'No html' , " __HTML__ " => '<b>HTML</b>' );
2021-05-04 13:33:18 +02:00
$chaine = " This is a text with \n New line \n Then \n __NOHTML__ \n Then \n __HTML__ " ;
$newstring = make_substitutions ( $chaine , $substit , $langs , 1 );
print __METHOD__ . " " . $newstring . " \n " ;
$this -> assertEquals ( $newstring , " This is a text with<br> \n New line<br> \n Then<br> \n No html<br> \n Then<br> \n <b>HTML</b> " , 'Test on make_substitutions with full conversion of text accepted' );
2021-01-14 15:09:08 +01:00
return true ;
}
/**
* testDolStringIsGoodIso
*
* @ return boolean
*/
public function testDolStringIsGoodIso ()
{
global $conf , $langs ;
2024-02-19 15:28:21 +01:00
$chaine = 'This is an ISO string' ;
2021-01-14 15:09:08 +01:00
$result = dol_string_is_good_iso ( $chaine );
$this -> assertEquals ( $result , 1 );
2024-02-19 15:28:21 +01:00
$chaine = 'This is a not ISO string ' . chr ( 0 );
2021-01-14 15:09:08 +01:00
$result = dol_string_is_good_iso ( $chaine );
$this -> assertEquals ( $result , 0 );
return true ;
}
2023-11-30 21:22:20 +01:00
/**
* testUtf8Check
*
* @ return boolean
*/
public function testUtf8Check ()
{
global $conf , $langs ;
$chaine = 'This is an UTF8 string with a é.' ;
$result = utf8_check ( $chaine );
$this -> assertEquals ( true , $result );
$chaine = mb_convert_encoding ( 'This is an UTF8 with a é.' , 'ISO-8859-1' , 'UTF-8' );
$result = utf8_check ( $chaine );
$this -> assertEquals ( false , $result );
return true ;
}
/**
* testUtf8Valid
*
* @ return boolean
*/
public function testUtf8Valid ()
{
global $conf , $langs ;
$chaine = 'This is an UTF8 string with a é.' ;
$result = utf8_valid ( $chaine );
$this -> assertEquals ( true , $result );
$chaine = mb_convert_encoding ( 'This is an UTF8 with a é.' , 'ISO-8859-1' , 'UTF-8' );
$result = utf8_valid ( $chaine );
$this -> assertEquals ( false , $result );
return true ;
}
2021-01-14 15:09:08 +01:00
/**
* testGetUserRemoteIP
*
* @ return boolean
*/
public function testGetUserRemoteIP ()
{
global $conf , $langs ;
2024-02-19 15:28:21 +01:00
$_SERVER [ 'HTTP_X_FORWARDED_FOR' ] = '1.2.3.4' ;
$_SERVER [ 'HTTP_CLIENT_IP' ] = '5.6.7.8' ;
2021-01-14 15:09:08 +01:00
$result = getUserRemoteIP ();
$this -> assertEquals ( $result , '1.2.3.4' );
2024-02-19 15:28:21 +01:00
$_SERVER [ 'HTTP_X_FORWARDED_FOR' ] = '1.2.3.4<corrupted>' ;
$_SERVER [ 'HTTP_CLIENT_IP' ] = '5.6.7.8' ;
2021-01-14 15:09:08 +01:00
$result = getUserRemoteIP ();
$this -> assertEquals ( $result , '5.6.7.8' );
2024-02-19 15:28:21 +01:00
$_SERVER [ 'HTTP_X_FORWARDED_FOR' ] = '[1:2:3:4]' ;
$_SERVER [ 'HTTP_CLIENT_IP' ] = '5.6.7.8' ;
2021-01-14 15:09:08 +01:00
$result = getUserRemoteIP ();
$this -> assertEquals ( $result , '[1:2:3:4]' );
return true ;
}
2023-12-27 13:09:25 +01:00
/**
* testFetchObjectByElement
*
* @ return boolean ;
*/
public function testFetchObjectByElement ()
{
global $conf , $langs ;
$result = fetchObjectByElement ( 0 , 'product' );
$this -> assertTrue ( is_object ( $result ));
return true ;
}
2024-01-30 17:49:58 +01:00
/**
* testRoundUpToNextMultiple
*
* @ return void ;
*/
public function testRoundUpToNextMultiple ()
{
$this -> assertEquals ( roundUpToNextMultiple ( 39.5 ), 40 );
$this -> assertEquals ( roundUpToNextMultiple ( 40 ), 40 );
$this -> assertEquals ( roundUpToNextMultiple ( 40.4 ), 45 );
$this -> assertEquals ( roundUpToNextMultiple ( 40.5 ), 45 );
$this -> assertEquals ( roundUpToNextMultiple ( 44.5 ), 45 );
$this -> assertEquals ( roundUpToNextMultiple ( 39.5 , 10 ), 40 );
$this -> assertEquals ( roundUpToNextMultiple ( 40 , 10 ), 40 );
$this -> assertEquals ( roundUpToNextMultiple ( 40.5 , 10 ), 50 );
$this -> assertEquals ( roundUpToNextMultiple ( 44.5 , 10 ), 50 );
$this -> assertEquals ( roundUpToNextMultiple ( 39.5 , 6 ), 42 );
$this -> assertEquals ( roundUpToNextMultiple ( 40 , 6 ), 42 );
$this -> assertEquals ( roundUpToNextMultiple ( 40.5 , 6 ), 42 );
$this -> assertEquals ( roundUpToNextMultiple ( 44.5 , 6 ), 48 );
}
2011-05-01 11:28:46 +02:00
}