<?php

require_once("fakemail/fakemail.php");
require_once(
"fakemail/simplemail.php");

class
TestDoubleOptIn extends WebTestCase {

    var
$_fakemail;
    var
$emailaddress;
    var
$password;
    
    function
setUp(){
        
        
$this->emailaddress = TEST_EMAIL;
        
$this->password = TEST_PASSWORD;
    
        
$this->_fakemail=new FakeMailDaemon(NULL,NULL,10025);
        
$this->_fakemail->start();
    }
    
    function
tearDown(){
        
$this->_fakemail->stop();
    }
    
    function
testDoubleOptIn(){
        
        
$this->get(TEST_SERVER_HTTP);
        
$this->clickLink("register");
        
        
// form should exist with email address box:

        
$this->assertWantedPattern('#Please provide your email address#i');
        
$this->setField("email",$this->emailaddress);
        
        if(!
MAIL_TEST_MODE){
            
$this->fail("Server must be in test mode to run this test.");
            return
false;
        }
        
        
$this->clickSubmit('register');
        
        
$this->assertWantedPattern("#An email was sent to "
                
. preg_quote(trim($this->emailaddress))
                .
"#i"
        
);
        
        
// Go and check email
        
        
$simplemail = new SimpleMail($this->_fakemail);
        
$simplemail->login($this->emailaddress);
        
$simplemail->loadMessages();

        
$msg =& $simplemail->getLatestBySubject('#^New customer#i');

        if(
$msg===false){
            
$this->fail("Couldn't find 'new customer' email for ".$this->emailaddress);
            return
false;
        }                            

        
$url = $msg->findLinkAfterText("Please click the link below to confirm");

        if(
$url===false){
            
$this->fail("Couldn't find link in email: ".$msg->error);
            return
false;
        }

        if(!
preg_match("#^".preg_quote(TEST_SERVER_HTTPS)."/reg2.php\\?key=#",$url)){
            
$this->fail("Email link doesn't match expected: $url");
            return
false;
        }
        
        
// Click the email link...
        
        
$this->get($url);
        
$this->assertTitle("Enter your contact details");
        
        
// Fill in customer details form
        
        
$this->setField("title","Mr");
        
$this->setField("firstname","Sam");
        
$this->setField("lastname","Tiger");
        
$this->setField("address","77 Domestic Bliss St");
        
$this->setField("city","Sydney");
        if(!
$this->setField("country","Australia")){
            
$this->fail("Unable to set 'country' via selectbox");
            return
false;
        }
        
$this->setField("telno","999999");
        
$this->setField("password1",$this->password);
        
$this->setField("password2",$this->password);
        
        
$this->clickSubmit("continue");
        
        
$this->assertWantedPattern("#<h1>Thanks for Registering</h1>#i");
    }
}

?>