Register.php.bak

<?php

namespace Tlf\User\Gui;

///////////////////////
//
//
//
//
// I made this backup file because i had an implementation of mail() testing that used /var/spool/mail/user to verify email content & headers. 
//
// I have since switched to namespaced mail() function for easier testing & less hinkyness even though it is a less thorough test
//
//
//
//
//
//
//
///////////////////////////////

class Register extends \Tlf\User\GuiTester {


// Paths
// 1: view register form
// 2: submit register form, create new user
// 3: submit register form, user exists
// 4: submit register form, invalid confirmation email
// 5: submit register form, invalid password
// 6: submit register form, invalid confirm password
// 7: submit register form, invalid email 
// 8: What happens if a user registered but didn't finish registration & now their reg code is expired?
    // Maybe reset password is sufficient for getting them a new code? Hmmm.
    // Or do they need to re-register? Hmmm.

    public function testInvalidEmail(){
        $email = 'reed-invalid-email';
        $password = 'Abcd1234!@';
        $lib = new \Tlf\User\Lib($this->pdo());
    
        $response = $this->post('/user/register/',
            ['email'=>$email,
            'email_confirm'=>$email,
            'password'=>$password,
            'password_confirm'=>$password
            ]
        );

        $this->str_contains(
            $response,
            'Your email does not appear to be a real email address. Please try again.',
            '<form method="POST" action="/user/register/">',
            // include a message to "let us know if your email is, in fact REAL & we have a bug"
                // bc php filter_var() may have issues with rfc compatability,
                    // as noted on the php website by some users
        );

        $user = $lib->user_from_email($email);
        $this->is_false($user->is_registered());
        $this->is_false($user->is_active());
        $ldb = new \Tlf\LilDb($this->pdo());
        $rows = $ldb->select('code', ['user_id'=>$user->id]);
        $this->test("Ensure a new code was not created during POST.");
        $this->is_true(count($rows)==0);
    }
    
    public function testInvalidConfirmationPassword(){
        $email = 'reed@register.passwordmismatch';
        $password = 'Abcd1234!@';
        $lib = new \Tlf\User\Lib($this->pdo());
    
        $response = $this->post('/user/register/',
            ['email'=>$email,
            'email_confirm'=>$email,
            'password'=>$password,
            'password_confirm'=>$password.'O'
            ]
        );

        $this->str_contains(
            $response,
            'The passwords you entered did not match. Please try again.',
            '<form method="POST" action="/user/register/">',
        );

        $user = $lib->user_from_email($email);
        $this->is_false($user->is_registered());
        $this->is_false($user->is_active());
        $ldb = new \Tlf\LilDb($this->pdo());
        $rows = $ldb->select('code', ['user_id'=>$user->id]);
        $this->test("Ensure a new code was not created during POST.");
        $this->is_true(count($rows)==0);

    }

    public function testInvalidPassword(){
        $email = 'reed@register.passwordinvalid';
        $password = 'abcd123456';
        $lib = new \Tlf\User\Lib($this->pdo());
    
        $response = $this->post('/user/register/',
            ['email'=>$email,
            'email_confirm'=>$email,
            'password'=>$password,
            'password_confirm'=>$password
            ]
        );

        $this->str_contains(
            $response,
            'The password you entered does not meet our minimum security requirements. Please try again.',
            '<form method="POST" action="/user/register/">',
        );

        $user = $lib->user_from_email($email);
        $this->is_false($user->is_registered());
        $this->is_false($user->is_active());
        $ldb = new \Tlf\LilDb($this->pdo());
        $rows = $ldb->select('code', ['user_id'=>$user->id]);
        $this->test("Ensure a new code was not created during POST.");
        $this->is_true(count($rows)==0);
    }

    public function testInvalidConfirmationEmail(){

        $email = 'reed@register.emailconfirmissue';
        $password = 'Abcd1234!@';
        $lib = new \Tlf\User\Lib($this->pdo());

        $response = $this->post('/user/register/',
            ['email'=>$email,
            'email_confirm'=>'reed@wrongconfirm.email',
            'password'=>$password,
            'password_confirm'=>$password
            ]
        );

        $this->str_contains(
            $response,
            'The email addresses you entered did not match. Please try again.',
            '<form method="POST" action="/user/register/">',
        );

        $user = $lib->user_from_email($email);
        $this->is_false($user->is_registered());
        $this->is_false($user->is_active());
        $ldb = new \Tlf\LilDb($this->pdo());
        $rows = $ldb->select('code', ['user_id'=>$user->id]);
        $this->test("Ensure a new code was not created during POST.");
        $this->is_true(count($rows)==0);
    }

    public function testUserExists(){
        $email = 'reed@register.exists';
        $password = 'Abcd1234!@';
        $lib = new \Tlf\User\Lib($this->pdo());
    
        $user = $lib->user_from_email($email);
        $code = $user->register($password);

        $response = $this->post('/user/register/',
            ['email'=>$email,
            'email_confirm'=>$email,
            'password'=>$password,
            'password_confirm'=>$password
            ]
        );

        $this->str_contains(
            $response,
            "An email has been sent to $email. Please check your email to complete registration."
        );

        $this->str_not_contains(
            $response,
            '<form method="POST" action="/user/register/">',
        );

        $user = $lib->user_from_email($email);
        $this->is_true($user->is_registered());
        $this->is_false($user->is_active());
        $ldb = new \Tlf\LilDb($this->pdo());
        $rows = $ldb->select('code', ['user_id'=>$user->id]);
        $this->test("Ensure a new code was not created during POST.");
        $this->is_true(count($rows)==0);



        $this->is_false(true);
        echo "this test is not complete. it needs to validate email.";
    }

    public function testCreateUser(){
        // $fakemail = dirname(__DIR__,2).'/sendmail';
        // system('export PATH='.$fakemail.':$PATH');
        // var_dump();
        // ini_set('sendmail_path', dirname(__DIR__,2).'/sendmail');

        // $success = mail('reed@localhost.test', 'subject', 'message', []);
        // var_dump($success);

        // exit;
        ///////////////////
        //
        // this test sucks, because it's validating email
        // it uses /var/spool/mail/get_current_user()
        //
        // which basically requires /usr/sbin/sendmail to fail
        // & store the output
        //
        // I'm doing this on Fedora 35 & I don't know if it will work on other systems.
        //
        //
        ////////////////////




        // prepare email 
        $user = get_current_user();
        $mail_out = "/var/spool/mail/$user";
        echo "\nFile '$mail_out' must exist, and be readable & writeable.\n";
        file_put_contents($mail_out, '');
        $this->compare('',file_get_contents($mail_out));

        // POST /user/register
        $email = $user.'@test.localhost';
        $password = 'Abcd1234!@';

        $response = $this->post('/user/register/',
            ['email'=>$email,
             'email_confirm'=>$email,
             'password'=>$password,
             'password_confirm'=>$password,
            ]
        );


        // echo $response;
        // exit;

        ////////
        // validate response
        ////////
        $this->str_contains(
            $response,
            "An email has been sent to $email. Please check your email to complete registration.",
        );
        $this->str_not_contains(
            $response,
            '<form method="POST" action="/user/register/">',
        );


        ////////
        // validate user state
        ////////
        $lib = new \Tlf\User\Lib($this->pdo());
        $ldb = new \Tlf\LilDb($this->pdo());

        // then i need to get the user & make sure there is a valid registration code
        $user = $lib->user_from_email($email);
        $this->is_true($user->is_registered());
        $this->is_false($user->is_active());

        $code_rows = $ldb->select('code',['user_id'=>$user->id]);
        $code = $code_rows[0]['code'];
        $this->is_true($user->activate($code));
        $this->is_true($user->is_active());
        $this->is_string($user->password_login('Abcd1234!@'));


        ////////
        // validate sent email
        ////////
        $target_url = 'http://create.localhost/user/complete_registration?code='.$code;
        echo "\nThis test requires /usr/bin/sendmail to be functional & to store localhost mails at $mail_out. The email address used was $email.";

        // sleep(2);
        $sent_mail = file_get_contents($mail_out);
        $this->str_contains(
            $sent_mail,
            'To: reed@test.localhost',
            'Subject: Account Registration',
            'From: test@tlf.userlib',
            'Reply-To: test@tlf.userlib',
            "To complete registration, visit <a href=\"$target_url\">$target_url</a>",
        );
// echo "\n\n\n-----------\n\n";
        // echo $sent_mail;
// echo "\n\n\n-----------\n\n";
        // exit;



    }

    public function testViewForm(){
        $response = $this->get('/user/register/');
        $this->str_contains(
            $response,
            '<form method="POST" action="/user/register/">',
        );

        echo $response;
    }

}