2010-10-26 21:47:19 +02:00
< ? php
/* Copyright ( C ) 2010 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
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
2011-12-17 21:58:44 +01:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
* or see http :// 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
*/
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';
2010-10-26 21:47:19 +02:00
require_once dirname ( __FILE__ ) . '/../../htdocs/master.inc.php' ;
2011-10-24 09:29:17 +02:00
require_once dirname ( __FILE__ ) . '/../../htdocs/core/class/CMailFile.class.php' ;
2010-10-26 21:47:19 +02:00
2015-01-06 17:54:36 +01:00
if ( empty ( $user -> id )) {
print " Load permissions for admin user nb 1 \n " ;
$user -> fetch ( 1 );
$user -> getrights ();
2010-10-26 21:47:19 +02:00
}
$conf -> global -> MAIN_DISABLE_ALL_MAILS = 1 ;
/**
2011-09-23 14:21:00 +02:00
* Class for PHPUnit tests
2010-10-26 21:47:19 +02:00
*
* @ backupGlobals disabled
* @ backupStaticAttributes enabled
* @ remarks backupGlobals must be disabled to have db , conf , user and lang not erased .
*/
class CMailFileTest extends PHPUnit_Framework_TestCase
{
2015-01-06 17:54:36 +01:00
protected $savconf ;
protected $savuser ;
protected $savlangs ;
protected $savdb ;
/**
* Constructor
* We save global variables into local variables
*
* @ return CMailFile
*/
function __construct ()
{
//$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 " ;
}
// Static methods
public static function setUpBeforeClass ()
2010-10-26 21:47:19 +02:00
{
2015-01-06 17:54:36 +01:00
global $conf , $user , $langs , $db ;
$db -> begin (); // This is to have all actions inside a transaction even if test launched without suite.
2010-10-26 21:47:19 +02:00
2015-01-06 17:54:36 +01:00
print __METHOD__ . " \n " ;
2010-10-26 21:47:19 +02:00
}
2015-01-06 17:54:36 +01:00
// tear down after class
2010-10-26 21:47:19 +02:00
public static function tearDownAfterClass ()
{
2015-01-06 17:54:36 +01:00
global $conf , $user , $langs , $db ;
$db -> rollback ();
2010-10-26 21:47:19 +02:00
2015-01-06 17:54:36 +01:00
print __METHOD__ . " \n " ;
2010-10-26 21:47:19 +02:00
}
2015-01-06 17:54:36 +01:00
/**
* Init phpunit tests
*
* @ return void
*/
2010-10-26 21:47:19 +02:00
protected function setUp ()
{
2015-01-06 17:54:36 +01:00
global $conf , $user , $langs , $db ;
$conf = $this -> savconf ;
$user = $this -> savuser ;
$langs = $this -> savlangs ;
$db = $this -> savdb ;
2010-10-26 21:47:19 +02:00
2015-10-14 23:50:36 +02: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.
2015-01-06 17:54:36 +01:00
print __METHOD__ . " \n " ;
2010-10-26 21:47:19 +02:00
}
2015-01-06 17:54:36 +01:00
/**
* End phpunit tests
*
* @ return void
*/
2010-10-26 21:47:19 +02:00
protected function tearDown ()
{
2015-01-06 17:54:36 +01:00
print __METHOD__ . " \n " ;
2010-10-26 21:47:19 +02:00
}
/**
2012-02-15 13:41:05 +01:00
* testCMailFileText
*
* @ return void
2010-10-26 21:47:19 +02:00
*/
public function testCMailFileText ()
{
2015-01-06 17:54:36 +01:00
global $conf , $user , $langs , $db ;
$conf = $this -> savconf ;
$user = $this -> savuser ;
$langs = $this -> savlangs ;
$db = $this -> savdb ;
2010-10-26 21:47:19 +02:00
2015-01-06 17:54:36 +01:00
$localobject = new CMailFile ( 'Test' , 'test@test.com' , 'from@from.com' , 'Message txt' , array (), array (), array (), '' , '' , 1 , 0 );
2010-10-26 21:47:19 +02:00
2015-01-06 17:54:36 +01:00
$result = $localobject -> sendfile ();
2010-10-26 21:47:19 +02:00
print __METHOD__ . " result= " . $result . " \n " ;
2015-01-06 17:54:36 +01:00
$this -> assertFalse ( $result ); // False because mail send disabled
2010-10-26 21:47:19 +02:00
2015-01-06 17:54:36 +01:00
return $result ;
2010-10-26 21:47:19 +02:00
}
/**
2012-02-15 13:41:05 +01:00
* testCMailFileStatic
*
* @ return string
2010-10-26 21:47:19 +02:00
*/
public function testCMailFileStatic ()
{
global $conf , $user , $langs , $db ;
$conf = $this -> savconf ;
$user = $this -> savuser ;
$langs = $this -> savlangs ;
$db = $this -> savdb ;
$localobject = new CMailFile ( '' , '' , '' , '' );
2012-02-02 10:22:04 +01:00
$src = 'John Doe <john@doe.com>' ;
$result = $localobject -> getValidAddress ( $src , 0 );
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( $result , 'John Doe <john@doe.com>' );
2010-10-26 21:47:19 +02:00
$src = 'John Doe <john@doe.com>' ;
$result = $localobject -> getValidAddress ( $src , 1 );
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( $result , '<john@doe.com>' );
$src = 'John Doe <john@doe.com>' ;
$result = $localobject -> getValidAddress ( $src , 2 );
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( $result , 'john@doe.com' );
2012-02-02 10:22:04 +01:00
$src = 'John Doe <john@doe.com>' ;
$result = $localobject -> getValidAddress ( $src , 3 , 0 );
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( $result , '"John Doe" <john@doe.com>' );
$src = 'John Doe <john@doe.com>' ;
$result = $localobject -> getValidAddress ( $src , 3 , 1 );
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( $result , '"=?UTF-8?B?Sm9obiBEb2U=?=" <john@doe.com>' );
2016-10-24 02:02:30 +02:00
$src = 'John Doe <john@doe.com>' ;
$result = $localobject -> getValidAddress ( $src , 4 );
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( $result , 'John Doe' );
$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 " ;
$this -> assertEquals ( $result , 'John Doe,John Doe2,John Doe3' );
$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 " ;
$this -> assertEquals ( $result , 'John Doe,John Doe2...' );
2010-10-26 21:47:19 +02:00
return $result ;
}
}