2014-03-11 23:39:26 +01:00
< ? php
/* Copyright ( C ) 2013 Laurent Destailleur < eldy @ users . sourceforge . net >
*
* 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
* the Free Software Foundation ; either version 3 of the License , or
* ( 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 .
*
* 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 /
2014-03-11 23:39:26 +01:00
*/
/**
* \file test / phpunit / LangTest . php
* \ingroup test
* \brief PHPUnit test
* \remarks To run this script as CLI : phpunit filename . php
*/
global $conf , $user , $langs , $db ;
//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';
2014-03-11 23:39:26 +01:00
require_once dirname ( __FILE__ ) . '/../../htdocs/master.inc.php' ;
require_once dirname ( __FILE__ ) . '/../../htdocs/core/lib/security.lib.php' ;
require_once dirname ( __FILE__ ) . '/../../htdocs/core/lib/security2.lib.php' ;
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)
}
if ( empty ( $user -> id )) {
print " Load permissions for admin user nb 1 \n " ;
$user -> fetch ( 1 );
$user -> getrights ();
2014-03-11 23:39:26 +01:00
}
$conf -> global -> MAIN_DISABLE_ALL_MAILS = 1 ;
/**
* Class for PHPUnit tests
*
* @ backupGlobals disabled
* @ backupStaticAttributes enabled
* @ remarks backupGlobals must be disabled to have db , conf , user and lang not erased .
*/
2019-07-05 21:28:27 +02:00
class LangTest extends PHPUnit\Framework\TestCase
2014-03-11 23:39:26 +01:00
{
protected $savconf ;
protected $savuser ;
protected $savlangs ;
protected $savdb ;
/**
* Constructor
* We save global variables into local variables
*
* @ return SecurityTest
*/
2019-02-25 00:56:48 +01:00
public function __construct ()
2014-03-11 23:39:26 +01:00
{
2018-09-02 14:10:06 +02:00
parent :: __construct ();
2014-03-11 23:39:26 +01:00
//$this->sharedFixture
global $conf , $user , $langs , $db ;
$this -> savconf = $conf ;
$this -> savuser = $user ;
$this -> savlangs = $langs ;
$this -> savdb = $db ;
print __METHOD__ . " db->type= " . $db -> type . " user->id= " . $user -> id ;
//print " - db ".$db->db;
print " \n " ;
}
2020-05-03 22:47:43 +02:00
/**
2021-01-14 15:09:08 +01:00
* setUpBeforeClass
*
* @ return void
*/
public static function setUpBeforeClass ()
{
global $conf , $user , $langs , $db ;
2014-03-11 23:39:26 +01:00
$db -> begin (); // This is to have all actions inside a transaction even if test launched without suite.
2021-01-14 15:09:08 +01:00
print __METHOD__ . " \n " ;
}
/**
* tearDownAfterClass
*
* @ return void
*/
public static function tearDownAfterClass ()
{
global $conf , $user , $langs , $db ;
2014-03-11 23:39:26 +01:00
$db -> rollback ();
print __METHOD__ . " \n " ;
2021-01-14 15:09:08 +01:00
}
2014-03-11 23:39:26 +01:00
/**
* Init phpunit tests
*
* @ return void
*/
2021-01-14 15:09:08 +01:00
protected function setUp ()
{
global $conf , $user , $langs , $db ;
2014-03-11 23:39:26 +01:00
$conf = $this -> savconf ;
$user = $this -> savuser ;
$langs = $this -> savlangs ;
$db = $this -> savdb ;
print __METHOD__ . " \n " ;
2021-01-14 15:09:08 +01:00
}
2014-03-11 23:39:26 +01:00
/**
* End phpunit tests
*
* @ return void
*/
2021-01-14 15:09:08 +01:00
protected function tearDown ()
{
print __METHOD__ . " \n " ;
}
/**
* testLang
*
* @ return string
*/
public function testLang ()
{
global $conf , $user , $langs , $db ;
2014-03-11 23:39:26 +01:00
$conf = $this -> savconf ;
$user = $this -> savuser ;
$langs = $this -> savlangs ;
$db = $this -> savdb ;
2021-01-14 15:09:08 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/class/translate.class.php' ;
2014-03-11 23:39:26 +01:00
$filesarray = scandir ( DOL_DOCUMENT_ROOT . '/langs' );
2021-01-14 15:09:08 +01:00
foreach ( $filesarray as $key => $code ) {
if ( ! preg_match ( '/^[a-z]+_[A-Z]+$/' , $code )) {
continue ;
}
2014-03-11 23:39:26 +01:00
2014-07-19 16:03:55 +02:00
print 'Check language file for lang code=' . $code . " \n " ;
2019-01-27 13:07:22 +01:00
$tmplangs = new Translate ( '' , $conf );
2021-01-14 15:09:08 +01:00
$langcode = $code ;
$tmplangs -> setDefaultLang ( $langcode );
2014-03-11 23:39:26 +01:00
$tmplangs -> load ( " main " );
2021-09-28 16:47:09 +02:00
$result = $tmplangs -> transnoentitiesnoconv ( " FONTFORPDF " );
print __METHOD__ . " FONTFORPDF= " . $result . " \n " ;
$this -> assertTrue ( in_array ( $result , array ( 'msungstdlight' , 'stsongstdlight' , 'helvetica' , 'DejaVuSans' , 'cid0jp' , 'cid0kr' , 'freemono' )), 'Error bad value ' . $result . ' for FONTFORPDF in main.lang file ' . $code );
2021-05-03 15:24:19 +02:00
$result = $tmplangs -> transnoentitiesnoconv ( " DIRECTION " );
print __METHOD__ . " DIRECTION= " . $result . " \n " ;
2021-09-28 16:47:09 +02:00
$this -> assertTrue ( in_array ( $result , array ( 'rtl' , 'ltr' )), 'Error bad value for DIRECTION in main.lang file ' . $code );
2021-05-03 15:24:19 +02:00
2015-12-18 20:53:04 +01:00
$result = $tmplangs -> transnoentitiesnoconv ( " SeparatorDecimal " );
2014-03-11 23:39:26 +01:00
print __METHOD__ . " SeparatorDecimal= " . $result . " \n " ;
2021-09-28 16:47:09 +02:00
$this -> assertContains ( $result , array ( '.' , ',' , '/' , ' ' , '' , 'None' ), 'Error on decimal separator for lang code ' . $code ); // Note that ، that is coma for RTL languages is not supported
2014-03-11 23:39:26 +01:00
2015-12-18 20:53:04 +01:00
$result = $tmplangs -> transnoentitiesnoconv ( " SeparatorThousand " );
2014-03-11 23:39:26 +01:00
print __METHOD__ . " SeparatorThousand= " . $result . " \n " ;
2021-09-28 16:47:09 +02:00
$this -> assertContains ( $result , array ( '.' , ',' , '/' , ' ' , '' , '\'' , 'None' , 'Space' ), 'Error on thousand separator for lang code ' . $code ); // Note that ، that is coma for RTL languages is not supported
2014-03-11 23:39:26 +01:00
// Test java string contains only d,M,y,/,-,. and not m,...
2015-12-18 20:53:04 +01:00
$result = $tmplangs -> transnoentitiesnoconv ( " FormatDateShortJava " );
2014-03-11 23:39:26 +01:00
print __METHOD__ . " FormatDateShortJava= " . $result . " \n " ;
2019-01-27 13:07:22 +01:00
$this -> assertRegExp ( '/^[dMy\/\-\.]+$/' , $result , 'FormatDateShortJava KO for lang code ' . $code );
2014-03-11 23:39:26 +01:00
$result = $tmplangs -> trans ( " FormatDateShortJavaInput " );
print __METHOD__ . " FormatDateShortJavaInput= " . $result . " \n " ;
2019-01-27 13:07:22 +01:00
$this -> assertRegExp ( '/^[dMy\/\-\.]+$/' , $result , 'FormatDateShortJavaInput KO for lang code ' . $code );
2014-03-11 23:39:26 +01:00
unset ( $tmplangs );
2020-05-23 22:01:17 +02:00
$filesarray2 = scandir ( DOL_DOCUMENT_ROOT . '/langs/' . $code );
2020-05-26 01:16:18 +02:00
foreach ( $filesarray2 as $key => $file ) {
2020-05-23 22:01:17 +02:00
if ( ! preg_match ( '/\.lang$/' , $file )) {
continue ;
}
print 'Check lang file ' . $file . " \n " ;
$filecontent = file_get_contents ( DOL_DOCUMENT_ROOT . '/langs/' . $code . '/' . $file );
2020-11-19 14:57:36 +01:00
$result = preg_match ( '/=--$/m' , $filecontent ); // A special % char we don't want. We want the common one.
//print __METHOD__." Result for checking we don't have bad percent char = ".$result."\n";
$this -> assertTrue ( $result == 0 , 'Found a translation KEY=-- into file ' . $code . '/' . $file . '. We probably want Key=- instead.' );
2020-11-05 19:32:14 +01:00
$result = strpos ( $filecontent , '% ' ); // A special % char we don't want. We want the common one.
//print __METHOD__." Result for checking we don't have bad percent char = ".$result."\n";
$this -> assertTrue ( $result === false , 'Found a bad percent char % instead of % into file ' . $code . '/' . $file );
2020-05-23 22:01:17 +02:00
}
2014-03-11 23:39:26 +01:00
}
2021-01-14 15:09:08 +01:00
return ;
}
/**
* testTrans
*
* @ return string
*/
public function testTrans ()
{
global $conf , $user , $langs , $db ;
$conf = $this -> savconf ;
$user = $this -> savuser ;
$langs = $this -> savlangs ;
$db = $this -> savdb ;
$tmplangs = new Translate ( '' , $conf );
$langcode = 'en_US' ;
$tmplangs -> setDefaultLang ( $langcode );
$tmplangs -> load ( " main " );
$result = $tmplangs -> trans ( " FilterOnInto " , " <input autofocus onfocus='alert(1337)' <--! " );
print __METHOD__ . " result trans FilterOnInto = " . $result . " \n " ;
$this -> assertEquals ( $result , " Search criteria '<b><input autofocus onfocus='alert(1337)' <--!</b>' into fields " , 'Result of lang->trans must have original translation string with its original HTML tag, but inserted values must be fully encoded.' );
}
2014-03-11 23:39:26 +01:00
}