PHP Classes

File: tests/test_registration_system.php

Recommend this page to a friend!
  Packages of Matthew Asham   Binkterm PHP   tests/test_registration_system.php   Download  
File: tests/test_registration_system.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Binkterm PHP
Bulletin board system based on the Web
Author: By
Last change:
Date: 5 days ago
Size: 10,065 bytes
 

Contents

Class file image Download
<?php

require_once __DIR__ . '/../vendor/autoload.php';

use
BinktermPHP\MessageHandler;
use
BinktermPHP\Database;

// Initialize database
Database::getInstance();

echo
"Testing User Registration System\n";
echo
"===============================\n\n";

try {
   
$db = Database::getInstance()->getPdo();
   
$handler = new MessageHandler();
   
   
// Check if pending_users table exists
   
echo "1. Checking database schema:\n";
   
$stmt = $db->query("SELECT name FROM sqlite_master WHERE type='table' AND name='pending_users'");
   
$table = $stmt->fetch();
   
    if (
$table) {
        echo
" ? pending_users table exists\n";
       
       
// Show table structure
       
$pragma = $db->query("PRAGMA table_info(pending_users)");
       
$columns = $pragma->fetchAll();
        echo
" Table columns:\n";
        foreach (
$columns as $col) {
            echo
" - {$col['name']} ({$col['type']})\n";
        }
    } else {
        echo
" ? pending_users table does not exist\n";
        echo
" Run the database schema to create it.\n";
        exit(
1);
    }
    echo
"\n";
   
   
// Test registration notification
   
echo "2. Testing registration notification system:\n";
    try {
       
// Create a test pending user entry
       
$insertStmt = $db->prepare("
            INSERT INTO pending_users (username, password_hash, email, real_name, reason, ip_address, user_agent)
            VALUES (?, ?, ?, ?, ?, ?, ?)
        "
);
       
       
$insertStmt->execute([
           
'testuser_' . time(),
           
password_hash('testpassword', PASSWORD_DEFAULT),
           
'test@example.com',
           
'Test User',
           
'Testing the registration system',
           
'127.0.0.1',
           
'Test User Agent'
       
]);
       
       
$pendingUserId = $db->lastInsertId();
        echo
" ? Created test pending user (ID: $pendingUserId)\n";
       
       
// Test sending notification
       
$handler->sendRegistrationNotification(
           
$pendingUserId,
           
'testuser_' . time(),
           
'Test User',
           
'test@example.com',
           
'Testing the registration system',
           
'127.0.0.1'
       
);
       
        echo
" ? Registration notification sent successfully\n";
       
       
// Check if netmail was created
       
$netmailStmt = $db->prepare("SELECT * FROM netmail WHERE subject LIKE '%Registration Request%' ORDER BY id DESC LIMIT 1");
       
$netmailStmt->execute();
       
$netmail = $netmailStmt->fetch();
       
        if (
$netmail) {
            echo
" ? Netmail notification created in database\n";
            echo
" Subject: {$netmail['subject']}\n";
            echo
" To: {$netmail['to_name']}\n";
            echo
" From: {$netmail['from_name']}\n";
        } else {
            echo
" ? No netmail notification found\n";
        }
       
    } catch (
Exception $e) {
        echo
" ? Registration notification failed: " . $e->getMessage() . "\n";
    }
    echo
"\n";
   
   
// Test approval functionality
   
echo "3. Testing user approval system:\n";
    try {
       
// Get the pending user we just created
       
$pendingStmt = $db->prepare("SELECT * FROM pending_users WHERE id = ? AND status = 'pending'");
       
$pendingStmt->execute([$pendingUserId]);
       
$pendingUser = $pendingStmt->fetch();
       
        if (
$pendingUser) {
            echo
" ? Found pending user for approval test\n";
           
           
// Test approval
           
$adminStmt = $db->query("SELECT id FROM users WHERE is_admin = 1 LIMIT 1");
           
$admin = $adminStmt->fetch();
           
$adminId = $admin ? $admin['id'] : 1;
           
           
$newUserId = $handler->approveUserRegistration($pendingUserId, $adminId, 'Test approval');
            echo
" ? User approved successfully (new user ID: $newUserId)\n";
           
           
// Verify user was created
           
$userStmt = $db->prepare("SELECT * FROM users WHERE id = ?");
           
$userStmt->execute([$newUserId]);
           
$newUser = $userStmt->fetch();
           
            if (
$newUser && $newUser['username'] === $pendingUser['username']) {
                echo
" ? User account created with correct data\n";
                echo
" Username: {$newUser['username']}\n";
                echo
" Real Name: {$newUser['real_name']}\n";
                echo
" Email: {$newUser['email']}\n";
                echo
" Active: " . ($newUser['is_active'] ? 'Yes' : 'No') . "\n";
            } else {
                echo
" ? User account not created properly\n";
            }
           
           
// Check if user settings were created
           
$settingsStmt = $db->prepare("SELECT * FROM user_settings WHERE user_id = ?");
           
$settingsStmt->execute([$newUserId]);
           
$settings = $settingsStmt->fetch();
           
            if (
$settings) {
                echo
" ? User settings created with messages_per_page: {$settings['messages_per_page']}\n";
            } else {
                echo
" ? User settings not created\n";
            }
           
           
// Check if welcome message was sent
           
$welcomeStmt = $db->prepare("SELECT * FROM netmail WHERE user_id = ? AND subject LIKE '%Welcome%' ORDER BY id DESC LIMIT 1");
           
$welcomeStmt->execute([$newUserId]);
           
$welcome = $welcomeStmt->fetch();
           
            if (
$welcome) {
                echo
" ? Welcome message sent to new user\n";
                echo
" Subject: {$welcome['subject']}\n";
            } else {
                echo
" ? Welcome message not sent\n";
            }
           
           
// Verify pending user status was updated
           
$updatedPendingStmt = $db->prepare("SELECT status, reviewed_by, admin_notes FROM pending_users WHERE id = ?");
           
$updatedPendingStmt->execute([$pendingUserId]);
           
$updatedPending = $updatedPendingStmt->fetch();
           
            if (
$updatedPending && $updatedPending['status'] === 'approved') {
                echo
" ? Pending user status updated to approved\n";
                echo
" Reviewed by admin ID: {$updatedPending['reviewed_by']}\n";
                echo
" Notes: {$updatedPending['admin_notes']}\n";
            } else {
                echo
" ? Pending user status not updated\n";
            }
           
        } else {
            echo
" ? No pending user found for approval test\n";
        }
       
    } catch (
Exception $e) {
        echo
" ? Approval test failed: " . $e->getMessage() . "\n";
    }
    echo
"\n";
   
   
// Test rejection functionality
   
echo "4. Testing user rejection system:\n";
    try {
       
// Create another test pending user
       
$insertStmt = $db->prepare("
            INSERT INTO pending_users (username, password_hash, email, real_name, reason, ip_address, user_agent)
            VALUES (?, ?, ?, ?, ?, ?, ?)
        "
);
       
       
$rejectTestUsername = 'rejecttest_' . time();
       
$insertStmt->execute([
           
$rejectTestUsername,
           
password_hash('testpassword', PASSWORD_DEFAULT),
           
'reject@example.com',
           
'Reject Test User',
           
'Testing the rejection system',
           
'127.0.0.1',
           
'Test User Agent'
       
]);
       
       
$rejectPendingUserId = $db->lastInsertId();
        echo
" ? Created test user for rejection (ID: $rejectPendingUserId)\n";
       
       
// Test rejection
       
$handler->rejectUserRegistration($rejectPendingUserId, $adminId, 'Test rejection - automated test');
        echo
" ? User rejected successfully\n";
       
       
// Verify rejection status
       
$rejectedStmt = $db->prepare("SELECT status, reviewed_by, admin_notes FROM pending_users WHERE id = ?");
       
$rejectedStmt->execute([$rejectPendingUserId]);
       
$rejected = $rejectedStmt->fetch();
       
        if (
$rejected && $rejected['status'] === 'rejected') {
            echo
" ? Pending user status updated to rejected\n";
            echo
" Notes: {$rejected['admin_notes']}\n";
        } else {
            echo
" ? Pending user status not updated to rejected\n";
        }
       
       
// Verify no user account was created
       
$noUserStmt = $db->prepare("SELECT id FROM users WHERE username = ?");
       
$noUserStmt->execute([$rejectTestUsername]);
       
$noUser = $noUserStmt->fetch();
       
        if (!
$noUser) {
            echo
" ? No user account created for rejected user (correct)\n";
        } else {
            echo
" ? User account was created for rejected user (incorrect)\n";
        }
       
    } catch (
Exception $e) {
        echo
" ? Rejection test failed: " . $e->getMessage() . "\n";
    }
    echo
"\n";
   
   
// Test getPendingUsers functionality
   
echo "5. Testing getPendingUsers functionality:\n";
    try {
       
$pendingUsers = $handler->getPendingUsers();
        echo
" ? Retrieved pending users: " . count($pendingUsers) . " found\n";
       
        foreach (
$pendingUsers as $user) {
            echo
" - {$user['username']} ({$user['status']}) - {$user['requested_at']}\n";
        }
       
    } catch (
Exception $e) {
        echo
" ? getPendingUsers failed: " . $e->getMessage() . "\n";
    }
    echo
"\n";
   
   
// Summary
   
echo "Registration System Test Summary\n";
    echo
"==============================\n";
    echo
"? Database schema is properly configured\n";
    echo
"? Registration notification system works\n";
    echo
"? User approval workflow functions correctly\n";
    echo
"? User rejection workflow functions correctly\n";
    echo
"? Pending users can be retrieved for admin interface\n";
    echo
"\nThe user registration system is fully functional!\n";
   
} catch (
Exception $e) {
    echo
"Test failed: " . $e->getMessage() . "\n";
    echo
"Trace: " . $e->getTraceAsString() . "\n";
}

echo
"\nTest completed.\n";