2022-11-16 11:12:34 +01:00
< ? php
/* Copyright ( C ) 2007 - 2017 Laurent Destailleur < eldy @ users . sourceforge . net >
2023-03-23 11:10:47 +01:00
* Copyright ( C ) 2023 - Thomas Negre - contact @ open - dsi . fr
2023-04-14 09:45:44 +02:00
* Copyright ( C ) 2023 Alexandre Janniaux < alexandre . janniaux @ gmail . com >
2022-11-16 11:12:34 +01: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
* 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
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
/**
2022-11-16 11:44:46 +01:00
* \file test / unit / ODFTest . php
* \ingroup odf
* \brief PHPUnit test for odf class .
2022-11-16 11:12:34 +01:00
*/
global $conf , $user , $langs , $db ;
//define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
//require_once 'PHPUnit/Autoload.php';
require_once dirname ( __FILE__ ) . '/../../htdocs/master.inc.php' ;
require_once dirname ( __FILE__ ) . '/../../htdocs/includes/odtphp/odf.php' ;
if ( empty ( $user -> id )) {
print " Load permissions for admin user nb 1 \n " ;
$user -> fetch ( 1 );
$user -> getrights ();
}
$conf -> global -> MAIN_DISABLE_ALL_MAILS = 1 ;
$langs -> load ( " main " );
/**
* Class for PHPUnit tests
*
* @ backupGlobals disabled
* @ backupStaticAttributes enabled
* @ remarks backupGlobals must be disabled to have db , conf , user and lang not erased .
*/
class ODFTest extends PHPUnit\Framework\TestCase
{
protected $savconf ;
protected $savuser ;
protected $savlangs ;
protected $savdb ;
/**
* Constructor
* We save global variables into local variables
*
2023-05-17 12:27:46 +02:00
* @ param string $name Name
* @ return ODFTest
2022-11-16 11:12:34 +01:00
*/
2023-05-07 14:31:35 +02:00
public function __construct ( $name = '' )
2022-11-16 11:12:34 +01:00
{
2023-05-07 14:31:35 +02:00
parent :: __construct ( $name );
2022-11-16 11:12:34 +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 " ;
}
/**
* setUpBeforeClass
*
* @ return void
*/
2023-04-14 09:45:44 +02:00
public static function setUpBeforeClass () : void
2022-11-16 11:12:34 +01:00
{
global $conf , $user , $langs , $db ;
$db -> begin (); // This is to have all actions inside a transaction even if test launched without suite.
print __METHOD__ . " \n " ;
}
/**
* tearDownAfterClass
*
* @ return void
*/
2023-04-14 09:45:44 +02:00
public static function tearDownAfterClass () : void
2022-11-16 11:12:34 +01:00
{
global $conf , $user , $langs , $db ;
$db -> rollback ();
print __METHOD__ . " \n " ;
}
/**
* Init phpunit tests
*
* @ return void
*/
2023-04-14 09:45:44 +02:00
protected function setUp () : void
2022-11-16 11:12:34 +01:00
{
global $conf , $user , $langs , $db ;
$conf = $this -> savconf ;
$user = $this -> savuser ;
$langs = $this -> savlangs ;
$db = $this -> savdb ;
print __METHOD__ . " \n " ;
}
/**
* End phpunit tests
*
* @ return void
*/
2023-04-14 09:45:44 +02:00
protected function tearDown () : void
2022-11-16 11:12:34 +01:00
{
print __METHOD__ . " \n " ;
}
/**
* test ODF convertVarToOdf
*
* @ return int
*/
public function testODFconvertVarToOdf ()
{
global $conf , $user , $langs , $db ;
$conf = $this -> savconf ;
$user = $this -> savuser ;
$langs = $this -> savlangs ;
$db = $this -> savdb ;
// we test using template_invoice, it does not matter, we just need a valid odt.
$filename = '../../htdocs/install/doctemplates/invoices/template_invoice.odt' ;
$config = [
'PATH_TO_TMP' => " /tmp " ,
'ZIP_PROXY' => " PclZipProxy " ,
'DELIMITER_LEFT' => " { " ,
'DELIMITER_RIGHT' => " } " ,
];
$to_test = [
/** No HTML **/
// Simple strings
1 => [
'to_convert' => 'Simple string' ,
'encode' => true ,
'charset' => null ,
2023-03-24 12:11:39 +01:00
'expected' => 'Simple string' ,
2022-11-16 11:12:34 +01:00
],
2 => [
'to_convert' => 'Simple string' ,
'encode' => false ,
'charset' => null ,
2023-03-24 12:11:39 +01:00
'expected' => 'Simple string' ,
2022-11-16 11:12:34 +01:00
],
3 => [
'to_convert' => " Simple string \n with line break " ,
'encode' => true ,
'charset' => null ,
2023-03-24 12:11:39 +01:00
'expected' => " Simple string<text:line-break/>with line break " ,
2022-11-16 11:12:34 +01:00
],
4 => [
'to_convert' => " Simple string \n with line break " ,
'encode' => false ,
'charset' => null ,
2023-03-24 12:11:39 +01:00
'expected' => " Simple string<text:line-break/>with line break " ,
2022-11-16 11:12:34 +01:00
],
// Special chars
5 => [
'to_convert' => 'One&two' ,
'encode' => true ,
'charset' => null ,
2023-03-24 12:11:39 +01:00
'expected' => 'One&two' ,
2022-11-16 11:12:34 +01:00
],
6 => [
'to_convert' => 'One&two' ,
'encode' => false ,
'charset' => null ,
2023-03-24 12:11:39 +01:00
'expected' => 'One&two' ,
2022-11-16 11:12:34 +01:00
],
7 => [
'to_convert' => " /a&él'èàüöç€Ğ~<> " ,
'encode' => true ,
'charset' => null ,
2023-12-14 14:51:31 +01:00
'expected' => mb_convert_encoding ( " /a&él'èàüöç€Ğ~<> " , 'UTF-8' , 'ISO-8859-1' ),
2022-11-16 11:12:34 +01:00
],
8 => [
'to_convert' => " /a&él'èàüöç€Ğ~<> " ,
'encode' => false ,
'charset' => null ,
2023-12-14 14:51:31 +01:00
'expected' => mb_convert_encoding ( " /a&él'èàüöç€Ğ~<> " , 'UTF-8' , 'ISO-8859-1' ),
2022-11-16 11:12:34 +01:00
],
// special chars with non-default charset
9 => [
'to_convert' => " /a&él'èàüöç€Ğ~<> " ,
'encode' => true ,
'charset' => 'UTF-16' ,
2023-03-24 12:11:39 +01:00
'expected' => " /a&él'èàüöç€Ğ~<> " ,
2022-11-16 11:12:34 +01:00
],
10 => [
'to_convert' => " /a&él'èàüöç€Ğ~<> " ,
'encode' => false ,
'charset' => 'UTF-16' , // When the charset differs from ISO-8859 string is not converted.
2023-03-24 12:11:39 +01:00
'expected' => " /a&él'èàüöç€Ğ~<> " ,
2022-11-16 11:12:34 +01:00
],
11 => [
'to_convert' => " Greater > than " ,
'encode' => true ,
'charset' => null ,
2023-12-14 14:51:31 +01:00
'expected' => mb_convert_encoding ( " Greater > than " , 'UTF-8' , 'ISO-8859-1' ),
2022-11-16 11:12:34 +01:00
],
12 => [
'to_convert' => " Greater > than " ,
'encode' => false ,
'charset' => null ,
2023-12-14 14:51:31 +01:00
'expected' => mb_convert_encoding ( " Greater > than " , 'UTF-8' , 'ISO-8859-1' ),
2022-11-16 11:12:34 +01:00
],
13 => [
'to_convert' => " Smaller < than " ,
'encode' => true ,
'charset' => null ,
2023-12-14 14:51:31 +01:00
'expected' => mb_convert_encoding ( " Smaller < than " , 'UTF-8' , 'ISO-8859-1' ),
2022-11-16 11:12:34 +01:00
],
14 => [
'to_convert' => " Smaller < than " ,
'encode' => false ,
'charset' => null ,
2023-12-14 14:51:31 +01:00
'expected' => mb_convert_encoding ( " Smaller < than " , 'UTF-8' , 'ISO-8859-1' ),
2022-11-16 11:12:34 +01:00
],
/** HTML **/
// break lines
15 => [
'to_convert' => " Break<br>line " ,
'encode' => true ,
'charset' => null ,
2023-12-14 14:51:31 +01:00
'expected' => mb_convert_encoding ( " Break<text:line-break/>line " , 'UTF-8' , 'ISO-8859-1' ),
2022-11-16 11:12:34 +01:00
],
16 => [
'to_convert' => " Break<br>line " ,
'encode' => false ,
'charset' => null ,
2023-12-14 14:51:31 +01:00
'expected' => mb_convert_encoding ( " Break<text:line-break/>line " , 'UTF-8' , 'ISO-8859-1' ),
2022-11-16 11:12:34 +01:00
],
17 => [
'to_convert' => " Break<br />line " ,
'encode' => true ,
'charset' => null ,
2023-12-14 14:51:31 +01:00
'expected' => mb_convert_encoding ( " Break<text:line-break/>line " , 'UTF-8' , 'ISO-8859-1' ),
2022-11-16 11:12:34 +01:00
],
18 => [
'to_convert' => " Break<br />line " ,
'encode' => false ,
'charset' => null ,
2023-12-14 14:51:31 +01:00
'expected' => mb_convert_encoding ( " Break<text:line-break/>line " , 'UTF-8' , 'ISO-8859-1' ),
2022-11-16 11:12:34 +01:00
],
// HTML tags
19 => [
'to_convert' => " text with <strong>strong, </strong><em>emphasis</em> and <u>underlined</u> words with <i>it@lic sp&ciàlchärs éè l'</i> " ,
'encode' => false ,
'charset' => 'UTF-8' ,
2023-03-24 12:11:39 +01:00
'expected' => 'text with <text:span text:style-name="boldText">strong, </text:span><text:span text:style-name="italicText">emphasis</text:span> and <text:span text:style-name="underlineText">underlined</text:span> words with <text:span text:style-name="italicText">it@lic sp&ciàlchärs éè l\'</text:span>' ,
2022-11-16 11:12:34 +01:00
],
20 => [
'to_convert' => " text with <strong>strong, </strong><em>emphasis</em> and <u>underlined</u> words with <i>it@lic sp&ciàlchärs éè l'</i> " ,
'encode' => true ,
'charset' => 'UTF-8' ,
2023-03-24 12:11:39 +01:00
'expected' => 'text with <text:span text:style-name="boldText">strong, </text:span><text:span text:style-name="italicText">emphasis</text:span> and <text:span text:style-name="underlineText">underlined</text:span> words with <text:span text:style-name="italicText">it@lic sp&ciàlchärs éè l'</text:span>' ,
2022-11-16 11:12:34 +01:00
],
21 => [
'to_convert' => " text with <strong>strong, </strong><em>emphasis</em> and <u>underlined</u> words with <i>it@lic sp&ciàlchärs éè l'</i> " ,
'encode' => false ,
'charset' => null ,
2023-12-14 14:51:31 +01:00
'expected' => mb_convert_encoding ( 'text with <text:span text:style-name="boldText">strong, </text:span><text:span text:style-name="italicText">emphasis</text:span> and <text:span text:style-name="underlineText">underlined</text:span> words with <text:span text:style-name="italicText">it@lic sp&ciàlchärs éè l\'</text:span>' , 'UTF-8' , 'ISO-8859-1' ),
2022-11-16 11:12:34 +01:00
],
22 => [
'to_convert' => " text with <strong>strong, </strong><em>emphasis</em> and <u>underlined</u> words with <i>it@lic sp&ciàlchärs éè l'</i> " ,
'encode' => true ,
'charset' => null ,
2023-12-14 14:51:31 +01:00
'expected' => mb_convert_encoding ( 'text with <text:span text:style-name="boldText">strong, </text:span><text:span text:style-name="italicText">emphasis</text:span> and <text:span text:style-name="underlineText">underlined</text:span> words with <text:span text:style-name="italicText">it@lic sp&ciàlchärs éè l'</text:span>' , 'UTF-8' , 'ISO-8859-1' ),
2022-11-16 11:12:34 +01:00
],
23 => [
'to_convert' => " text with <strong>intricated<u>tags</u></strong> " ,
'encode' => true ,
'charset' => null ,
2023-12-14 14:51:31 +01:00
'expected' => mb_convert_encoding ( 'text with <text:span text:style-name="boldText">intricated<text:span text:style-name="underlineText">tags</text:span></text:span>' , 'UTF-8' , 'ISO-8859-1' ),
2022-11-16 11:12:34 +01:00
],
2025-01-03 10:08:56 +01:00
24 => [
'to_convert' => " text with <strong>two</strong> (strong) <strong>tags</strong> " ,
'encode' => true ,
'charset' => null ,
'expected' => utf8_encode ( 'text with <text:span text:style-name="boldText">two</text:span> (strong) <text:span text:style-name="boldText">tags</text:span>' ),
],
2025-01-09 11:05:45 +01:00
25 => [
'to_convert' => " text with <strong class= \" whatever \" >two</strong> (strong) <strong class= \" the weather \" >tags and <u>intricated</u> underline </strong> " ,
'encode' => true ,
'charset' => null ,
'expected' => utf8_encode ( 'text with <text:span text:style-name="boldText">two</text:span> (strong) <text:span text:style-name="boldText">tags and <text:span text:style-name="underlineText">intricated</text:span> underline </text:span>' ),
],
2022-11-16 11:12:34 +01:00
// One can also pass html-encoded string to the method
2025-01-09 11:05:45 +01:00
26 => [
2022-11-16 11:12:34 +01:00
'to_convert' => 'One&two' ,
'encode' => true ,
'charset' => null ,
2023-03-24 12:11:39 +01:00
'expected' => 'One&two' ,
2022-11-16 11:12:34 +01:00
],
2025-01-09 11:05:45 +01:00
27 => [
2022-11-16 11:12:34 +01:00
'to_convert' => " text with <strong>strong, </strong><em>emphasis</em> and <u>underlined</u> words with <i>it@lic sp&ciàlchärs éè l'</i> " ,
'encode' => false ,
'charset' => 'UTF-8' ,
2023-03-24 12:11:39 +01:00
'expected' => 'text with <text:span text:style-name="boldText">strong, </text:span><text:span text:style-name="italicText">emphasis</text:span> and <text:span text:style-name="underlineText">underlined</text:span> words with <text:span text:style-name="italicText">it@lic sp&ciàlchärs éè l\'</text:span>' ,
2022-11-16 11:12:34 +01:00
],
2025-01-09 11:05:45 +01:00
28 => [
2022-11-16 11:12:34 +01:00
'to_convert' => " text with <strong>strong, </strong><em>emphasis</em> and <u>underlined</u> words with <i>it@lic sp&ciàlchärs éè l'</i> " ,
'encode' => true ,
'charset' => 'UTF-8' ,
2023-03-24 12:11:39 +01:00
'expected' => 'text with <text:span text:style-name="boldText">strong, </text:span><text:span text:style-name="italicText">emphasis</text:span> and <text:span text:style-name="underlineText">underlined</text:span> words with <text:span text:style-name="italicText">it@lic sp&ciàlchärs éè l'</text:span>' ,
2022-11-16 11:12:34 +01:00
],
2025-01-09 11:05:45 +01:00
29 => [
2022-11-16 11:12:34 +01:00
'to_convert' => " text with <strong>strong, </strong><em>emphasis</em> and <u>underlined</u> words with <i>it@lic sp&ciàlchärs éè l'</i> " ,
'encode' => false ,
'charset' => null ,
2023-12-14 14:51:31 +01:00
'expected' => mb_convert_encoding ( 'text with <text:span text:style-name="boldText">strong, </text:span><text:span text:style-name="italicText">emphasis</text:span> and <text:span text:style-name="underlineText">underlined</text:span> words with <text:span text:style-name="italicText">it@lic sp&ciàlchärs éè l\'</text:span>' , 'UTF-8' , 'ISO-8859-1' ),
2022-11-16 11:12:34 +01:00
],
2025-01-09 11:05:45 +01:00
30 => [
2022-11-16 11:12:34 +01:00
'to_convert' => " text with <strong>strong, </strong><em>emphasis</em> and <u>underlined</u> words with <i>it@lic sp&ciàlchärs éè l'</i> " ,
'encode' => true ,
'charset' => null ,
2023-12-14 14:51:31 +01:00
'expected' => mb_convert_encoding ( 'text with <text:span text:style-name="boldText">strong, </text:span><text:span text:style-name="italicText">emphasis</text:span> and <text:span text:style-name="underlineText">underlined</text:span> words with <text:span text:style-name="italicText">it@lic sp&ciàlchärs éè l'</text:span>' , 'UTF-8' , 'ISO-8859-1' ),
2022-11-16 11:12:34 +01:00
],
// // TODO custom styles are not tested for now : the custom style have a custom ID based on time. Not random, but hard to mock or predict. generated in _replaceHtmlWithOdtTag() case 'span'.
// [
// 'to_convert' => '123 <span style="color:#e74c3c">trucmachin > truc < troc > trac</span>bla bla',
// 'encode' => true,
// 'charset' => 'UTF-8',
2023-03-24 12:11:39 +01:00
// 'expected' => "123 <text:span text:style-name="customStyle1668592427018">trucmachin > truc < troc > trac</text:span>bla bla'",
2022-11-16 11:12:34 +01:00
// ],
2022-11-23 12:01:21 +01:00
/* Tests that can evolve */
// Following tests reflect the current behavior. They may evolve if the method behavior changes.
2022-11-16 11:12:34 +01:00
// The method removes hyperlinks and tags that are not dealt with.
2025-01-09 11:05:45 +01:00
31 => [
2022-11-16 11:12:34 +01:00
'to_convert' => '123 <a href="/test.php">trucmachin > truc < troc > trac</a>bla bla' ,
'encode' => true ,
'charset' => null ,
2023-03-24 12:11:39 +01:00
'expected' => " 123 trucmachin > truc < troc > tracbla bla " ,
2022-11-16 11:12:34 +01:00
],
2025-01-09 11:05:45 +01:00
32 => [
2022-11-23 12:01:21 +01:00
'to_convert' => '123 <h3>Title</h3> bla' ,
'encode' => true ,
'charset' => null ,
2023-03-24 12:11:39 +01:00
'expected' => " 123 Title bla " ,
2022-11-23 12:01:21 +01:00
],
// HTML should not take \n into account, but only <br />.
2025-01-09 11:05:45 +01:00
33 => [
2022-11-16 14:08:41 +01:00
'to_convert' => " text with <strong>strong text </strong>, a line \n break and <u>underlined</u> words with <i>it@lic sp&ciàlchärs éè l'</i> " ,
'encode' => false ,
'charset' => 'UTF-8' ,
2023-03-24 12:11:39 +01:00
'expected' => 'text with <text:span text:style-name="boldText">strong text </text:span>, a line' . " \n " . 'break and <text:span text:style-name="underlineText">underlined</text:span> words with <text:span text:style-name="italicText">it@lic sp&ciàlchärs éè l\'</text:span>' ,
2022-11-16 14:08:41 +01:00
],
2022-11-16 11:12:34 +01:00
];
$odf = new Odf ( $filename , array ());
2023-12-04 11:22:28 +01:00
if ( is_object ( $odf )) {
$result = 1 ;
} // Just to test
2022-11-16 11:12:34 +01:00
foreach ( $to_test as $case ) {
if ( $case [ 'charset' ] !== null ) {
$res = $odf -> convertVarToOdf ( $case [ 'to_convert' ], $case [ 'encode' ], $case [ 'charset' ]);
} else {
$res = $odf -> convertVarToOdf ( $case [ 'to_convert' ], $case [ 'encode' ]);
}
2025-01-03 10:50:30 +01:00
$this -> assertEquals ( $case [ 'expected' ], $res );
2022-11-16 11:12:34 +01:00
}
print __METHOD__ . " result= " . $result . " \n " ;
return $result ;
}
}