2010-10-26 21:47:19 +02:00
< ? php
/* Copyright ( C ) 2010 Laurent Destailleur < eldy @ users . sourceforge . net >
2023-05-07 14:31:35 +02:00
* Copyright ( C ) 2023 Alexandre Janniaux < alexandre . janniaux @ gmail . com >
2010-10-26 21:47:19 +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
2010-10-26 21:47:19 +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 .
*
* 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 /
2010-10-26 21:47:19 +02:00
*/
/**
* \file test / phpunit / CMailFileTest . php
2015-01-06 17:54:36 +01:00
* \ingroup test
2010-10-26 21:47:19 +02:00
* \brief PHPUnit test
2015-01-06 17:54:36 +01:00
* \remarks To run this script as CLI : phpunit filename . php
2010-10-26 21:47:19 +02:00
*/
2023-12-13 11:31:42 +01:00
global $conf , $user , $langs , $db ;
2010-10-26 21:47:19 +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';
2023-12-13 11:47:25 +01:00
require_once dirname ( __FILE__ ) . '/../../htdocs/master.inc.php' ;
require_once dirname ( __FILE__ ) . '/../../htdocs/core/class/CMailFile.class.php' ;
require_once dirname ( __FILE__ ) . '/../../htdocs/core/lib/files.lib.php' ;
2010-10-26 21:47:19 +02:00
2015-01-06 17:54:36 +01:00
if ( empty ( $user -> id )) {
2021-01-14 15:09:08 +01:00
print " Load permissions for admin user nb 1 \n " ;
$user -> fetch ( 1 );
$user -> getrights ();
2010-10-26 21:47:19 +02:00
}
2023-12-13 11:31:42 +01:00
$conf -> global -> MAIN_DISABLE_ALL_MAILS = 1 ;
2010-10-26 21:47:19 +02:00
/**
2011-09-23 14:21:00 +02:00
* Class for PHPUnit tests
2010-10-26 21:47:19 +02:00
*
* @ backupGlobals disabled
* @ backupStaticAttributes enabled
2023-12-13 11:31:42 +01:00
* @ remarks backupGlobals must be disabled to have db , conf , user and lang not erased .
2010-10-26 21:47:19 +02:00
*/
2019-07-05 21:28:27 +02:00
class CMailFileTest extends PHPUnit\Framework\TestCase
2010-10-26 21:47:19 +02:00
{
2021-01-14 15:09:08 +01:00
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
2021-01-14 15:09:08 +01:00
* @ return CMailFile
*/
2023-05-07 14:31:35 +02:00
public function __construct ( $name = '' )
2021-01-14 15:09:08 +01:00
{
2023-05-07 14:31:35 +02:00
parent :: __construct ( $name );
2021-01-14 15:09:08 +01:00
//$this->sharedFixture
2023-12-13 11:31:42 +01:00
global $conf , $user , $langs , $db ;
$this -> savconf = $conf ;
$this -> savuser = $user ;
$this -> savlangs = $langs ;
$this -> savdb = $db ;
2021-01-14 15:09:08 +01:00
2023-12-13 11:31:42 +01:00
print __METHOD__ . " db->type= " . $db -> type . " user->id= " . $user -> id ;
2021-01-14 15:09:08 +01:00
//print " - db ".$db->db;
print " \n " ;
}
/**
* setUpBeforeClass
*
* @ return void
*/
2022-09-21 17:55:04 +02:00
public static function setUpBeforeClass () : void
2021-01-14 15:09:08 +01:00
{
2023-12-13 11:31:42 +01:00
global $conf , $user , $langs , $db ;
2021-01-14 15:09:08 +01:00
$db -> begin (); // This is to have all actions inside a transaction even if test launched without suite.
2023-12-13 11:31:42 +01:00
print __METHOD__ . " \n " ;
2021-01-14 15:09:08 +01:00
}
/**
* tearDownAfterClass
*
2023-12-13 11:31:42 +01:00
* @ return void
2021-01-14 15:09:08 +01:00
*/
2022-09-21 17:55:04 +02:00
public static function tearDownAfterClass () : void
2021-01-14 15:09:08 +01:00
{
2023-12-13 11:31:42 +01:00
global $conf , $user , $langs , $db ;
2021-01-14 15:09:08 +01:00
$db -> rollback ();
2023-12-13 11:31:42 +01:00
print __METHOD__ . " \n " ;
2021-01-14 15:09:08 +01:00
}
/**
* Init phpunit tests
*
* @ return void
*/
2022-09-21 17:55:04 +02:00
protected function setUp () : void
2021-01-14 15:09:08 +01:00
{
2023-12-13 11:31:42 +01:00
global $conf , $user , $langs , $db ;
$conf = $this -> savconf ;
$user = $this -> savuser ;
$langs = $this -> savlangs ;
$db = $this -> savdb ;
2021-01-14 15:09:08 +01:00
2023-12-13 11:31:42 +01:00
$conf -> global -> MAIN_DISABLE_ALL_MAILS = 1 ; // If I comment/remove this lien, unit test still works alone but failed when ran from AllTest. Don't know why.
2021-01-14 15:09:08 +01:00
2023-12-13 11:31:42 +01:00
print __METHOD__ . " \n " ;
2021-01-14 15:09:08 +01:00
}
2023-12-13 11:31:42 +01:00
2021-01-14 15:09:08 +01:00
/**
* End phpunit tests
*
2023-12-13 11:31:42 +01:00
* @ return void
2021-01-14 15:09:08 +01:00
*/
2022-09-21 17:55:04 +02:00
protected function tearDown () : void
2021-01-14 15:09:08 +01:00
{
2023-12-13 11:31:42 +01:00
print __METHOD__ . " \n " ;
2021-01-14 15:09:08 +01:00
}
/**
* testCMailFileText
*
* @ return void
*/
public function testCMailFileText ()
{
2023-12-13 11:31:42 +01:00
global $conf , $user , $langs , $db ;
$conf = $this -> savconf ;
$user = $this -> savuser ;
$langs = $this -> savlangs ;
$db = $this -> savdb ;
2021-01-14 15:09:08 +01:00
2023-12-13 11:31:42 +01:00
$localobject = new CMailFile ( 'Test' , 'test@test.com' , 'from@from.com' , 'Message txt' , array (), array (), array (), '' , '' , 1 , 0 );
2021-01-14 15:09:08 +01:00
2023-12-13 11:31:42 +01:00
$result = $localobject -> sendfile ();
print __METHOD__ . " result= " . $result . " \n " ;
2021-01-14 15:09:08 +01:00
$this -> assertFalse ( $result ); // False because mail send disabled
return $result ;
}
/**
* testCMailFileStatic
*
* @ return string
*/
public function testCMailFileStatic ()
{
2023-12-13 11:31:42 +01:00
global $conf , $user , $langs , $db ;
$conf = $this -> savconf ;
$user = $this -> savuser ;
$langs = $this -> savlangs ;
$db = $this -> savdb ;
2021-01-14 15:09:08 +01:00
2023-12-13 11:31:42 +01:00
$localobject = new CMailFile ( '' , '' , '' , '' );
2021-01-14 15:09:08 +01:00
2023-12-13 11:31:42 +01:00
$src = 'John Doe <john@doe.com>' ;
$result = $localobject -> getValidAddress ( $src , 0 );
print __METHOD__ . " result= " . $result . " \n " ;
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( $result , 'John Doe <john@doe.com>' );
2023-12-13 11:31:42 +01:00
$src = 'John Doe <john@doe.com>' ;
$result = $localobject -> getValidAddress ( $src , 1 );
print __METHOD__ . " result= " . $result . " \n " ;
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( $result , '<john@doe.com>' );
2023-12-13 11:31:42 +01:00
$src = 'John Doe <john@doe.com>' ;
$result = $localobject -> getValidAddress ( $src , 2 );
print __METHOD__ . " result= " . $result . " \n " ;
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( $result , 'john@doe.com' );
2023-12-13 11:31:42 +01:00
$src = 'John Doe <john@doe.com>' ;
$result = $localobject -> getValidAddress ( $src , 3 , 0 );
print __METHOD__ . " result= " . $result . " \n " ;
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( $result , '"John Doe" <john@doe.com>' );
2023-12-13 11:31:42 +01:00
$src = 'John Doe <john@doe.com>' ;
$result = $localobject -> getValidAddress ( $src , 3 , 1 );
print __METHOD__ . " result= " . $result . " \n " ;
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( $result , '"=?UTF-8?B?Sm9obiBEb2U=?=" <john@doe.com>' );
2023-12-13 11:31:42 +01:00
$src = 'John Doe <john@doe.com>' ;
$result = $localobject -> getValidAddress ( $src , 4 );
print __METHOD__ . " result= " . $result . " \n " ;
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( $result , 'John Doe' );
2023-12-13 11:31:42 +01:00
$src = 'John Doe <john@doe.com>, John Doe2 <john@doe3.com>, John Doe3 <john@doe2.com>' ;
$result = $localobject -> getValidAddress ( $src , 4 );
print __METHOD__ . " result= " . $result . " \n " ;
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( $result , 'John Doe,John Doe2,John Doe3' );
2023-12-13 11:31:42 +01:00
$src = 'John Doe <john@doe.com>, John Doe2 <john@doe3.com>, John Doe3 <john@doe2.com>' ;
$result = $localobject -> getValidAddress ( $src , 4 , 0 , 2 );
print __METHOD__ . " result= " . $result . " \n " ;
2021-01-14 15:09:08 +01:00
$this -> assertEquals ( $result , 'John Doe,John Doe2...' );
return $result ;
}
2023-12-13 11:31:42 +01:00
2023-12-29 16:51:05 +01:00
/**
* testCMailFileHTMLWithImage
*
2023-12-30 13:09:41 +01:00
* @ return int
2023-12-29 16:51:05 +01:00
*/
2023-12-13 11:31:42 +01:00
public function testCMailFileHTMLWithImage ()
{
global $conf , $user , $langs , $db ;
2023-12-29 16:51:05 +01:00
2023-12-13 11:31:42 +01:00
$conf = $this -> savconf ;
$user = $this -> savuser ;
$langs = $this -> savlangs ;
$db = $this -> savdb ;
2023-12-30 13:09:41 +01:00
2023-12-13 11:31:42 +01:00
$conf -> global -> MAIN_MAIL_ADD_INLINE_IMAGES_IF_IN_MEDIAS = 1 ;
$conf -> global -> MAIN_MAIL_ADD_INLINE_IMAGES_IF_DATA = 1 ;
dol_delete_dir_recursive ( DOL_DATA_ROOT . '/medias/image' );
dol_mkdir ( DOL_DATA_ROOT . '/medias/image' );
dol_copy ( dirname ( __FILE__ ) . '/img250x20.png' , DOL_DATA_ROOT . '/medias/image/img250x20.png' );
$msg = '<html><body>' ;
$msg .= '<img alt="" src="/viewimage.php?modulepart=medias&entity=1&file=image/img250x20.png" style="height:39px; width:150px" />' ;
$msg .= ' <p>From wikipedia</p> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />' ;
$msg .= '</body></html>' ;
2023-12-13 11:47:25 +01:00
$localobject = new CMailFile ( 'Test' , 'test@test.com' , 'from@from.com' , $msg , array (), array (), array (), '' , '' , 0 , - 1 , '' , '' , '' , '' , 'standard' , '' , '/tmp' );
2023-12-13 11:31:42 +01:00
$result = count ( $localobject -> html_images );
print __METHOD__ . " result count image detected in the mail= " . $result . " \n " ;
$this -> assertEquals ( $result , 2 );
foreach ( $localobject -> html_images as $i => $val )
2023-12-29 16:51:05 +01:00
if ( $localobject -> html_images [ $i ]) {
if ( preg_match ( '/img250x20\.png/i' , $localobject -> html_images [ $i ][ 'fullpath' ])) {
print __METHOD__ . " content type must be image png = " . $localobject -> html_images [ $i ][ 'content_type' ] . " \n " ;
$this -> assertEquals ( $localobject -> html_images [ $i ][ 'content_type' ], 'image/png' );
print __METHOD__ . " type must be cidfromurl = " . $localobject -> html_images [ $i ][ 'type' ] . " \n " ;
$this -> assertEquals ( $localobject -> html_images [ $i ][ 'type' ], 'cidfromurl' );
$fileSize = 9744 ;
print __METHOD__ . " File size must be = " . $fileSize . " \n " ;
$this -> assertEquals ( dol_filesize ( $localobject -> html_images [ $i ][ 'fullpath' ]), $fileSize );
} elseif ( preg_match ( '/\.png/i' , $localobject -> html_images [ $i ][ 'fullpath' ])) {
print __METHOD__ . " content type must be image png = " . $localobject -> html_images [ $i ][ 'content_type' ] . " \n " ;
$this -> assertEquals ( $localobject -> html_images [ $i ][ 'content_type' ], 'image/png' );
print __METHOD__ . " type must be cidfromdata = " . $localobject -> html_images [ $i ][ 'type' ] . " \n " ;
$this -> assertEquals ( $localobject -> html_images [ $i ][ 'type' ], 'cidfromdata' );
$fileSize = 85 ;
print __METHOD__ . " File size must be = " . $fileSize . " \n " ;
$this -> assertEquals ( dol_filesize ( $localobject -> html_images [ $i ][ 'fullpath' ]), $fileSize );
2023-12-13 11:31:42 +01:00
}
2023-12-29 16:51:05 +01:00
}
2023-12-13 11:31:42 +01:00
return $result ;
}
2010-10-26 21:47:19 +02:00
}