<?php
/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */

include_once 'Services/Mail/classes/class.ilMimeMail.php';

/** 
 * Unit tests for the mail system
 *
 * @version $Id$
 * @ingroup ServicesMail
 * @author	Michael Jansen <mjansen@databay.de>
 * 
 */
class ilMailTest extends PHPUnit_Framework_TestCase
{
	protected $backupGlobals = false;
	
	protected function setUp()
	{
		include_once 'Services/PHPUnit/classes/class.ilUnitUtil.php';
		ilUnitUtil::performInitialisation();
	}
	
	public function testEmpty()
	{
		$this->assertEquals('foo', 'foo');
	}
	
	public function testIdentifiedUserIsSender()
	{
		global $ilUser;
		
		$mail = new ilMail($ilUser->getId());
		$sender = $mail->getMimeMailSender();

		$this->assertEquals($sender, ilMimeMail::_mimeEncode($ilUser->getFullname()).'<'.$ilUser->getEmail().'>');
	}
	
	public function testMissingNoReplyAddressAutogenerated()
	{
		global $ilSetting;
		
		$tmp = $ilSetting->get('mail_external_sender_noreply');
		
		$ilSetting->set('mail_external_sender_noreply', '');
			        
		$mail = new ilMail(ANONYMOUS_USER_ID);
		$sender = $mail->getMimeMailSender();
		
		$ilSetting->set('mail_external_sender_noreply', $tmp);
		
		$this->assertEquals($sender, ilMimeMail::_mimeEncode(ilMail::_getAnonymousName()).'<noreply@'.$_SERVER['SERVER_NAME'].'>');
	}
	
	public function testInvalidNoReplyAddressAutogenerated()
	{	
		global $ilSetting;
		
		$tmp = $ilSetting->get('mail_external_sender_noreply');
		
		$ilSetting->set('mail_external_sender_noreply', 'host.de');
	        
		$mail = new ilMail(ANONYMOUS_USER_ID);
		$sender = $mail->getMimeMailSender();
		
		$ilSetting->set('mail_external_sender_noreply', $tmp);
		
		$this->assertEquals($sender, ilMimeMail::_mimeEncode(ilMail::_getAnonymousName()).'<noreply@host.de>');
	}
	
	public function testValidNoReplyAddressAutogenerated()
	{	        
		global $ilSetting;
		
		$tmp = $ilSetting->get('mail_external_sender_noreply');
		
		$ilSetting->set('mail_external_sender_noreply', 'doNotReply@host.de');
	
		$mail = new ilMail(ANONYMOUS_USER_ID);
		$sender = $mail->getMimeMailSender();
		
		$ilSetting->set('mail_external_sender_noreply', $tmp);
		
		$this->assertEquals($sender, ilMimeMail::_mimeEncode(ilMail::_getAnonymousName()).'<doNotReply@host.de>');
	}
}
?>