|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpRss\Tests\Unit; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use PhpRss\Auth; |
| 7 | +use PhpRss\Database; |
| 8 | +use PDO; |
| 9 | + |
| 10 | +class AuthTest extends TestCase |
| 11 | +{ |
| 12 | + protected function setUp(): void |
| 13 | + { |
| 14 | + // Initialize database for testing |
| 15 | + Database::init(); |
| 16 | + Database::setup(); |
| 17 | + |
| 18 | + // Start session for Auth tests |
| 19 | + if (session_status() === PHP_SESSION_NONE) { |
| 20 | + session_start(); |
| 21 | + } |
| 22 | + |
| 23 | + // Clear session |
| 24 | + $_SESSION = []; |
| 25 | + } |
| 26 | + |
| 27 | + protected function tearDown(): void |
| 28 | + { |
| 29 | + // Clean up: remove test users |
| 30 | + $db = Database::getConnection(); |
| 31 | + $db->exec("DELETE FROM users WHERE username LIKE 'test_%'"); |
| 32 | + |
| 33 | + // Clear session |
| 34 | + $_SESSION = []; |
| 35 | + } |
| 36 | + |
| 37 | + public function testCheckReturnsFalseWhenNotLoggedIn(): void |
| 38 | + { |
| 39 | + $this->assertFalse(Auth::check()); |
| 40 | + } |
| 41 | + |
| 42 | + public function testCheckReturnsTrueWhenLoggedIn(): void |
| 43 | + { |
| 44 | + // Create a test user and login |
| 45 | + $this->createTestUser('test_user', 'test@example.com', 'password123'); |
| 46 | + $this->assertTrue(Auth::login('test_user', 'password123')); |
| 47 | + $this->assertTrue(Auth::check()); |
| 48 | + } |
| 49 | + |
| 50 | + public function testUserReturnsNullWhenNotAuthenticated(): void |
| 51 | + { |
| 52 | + $user = Auth::user(); |
| 53 | + $this->assertNull($user); |
| 54 | + } |
| 55 | + |
| 56 | + public function testUserReturnsUserDataWhenAuthenticated(): void |
| 57 | + { |
| 58 | + $username = 'test_user'; |
| 59 | + $email = 'test@example.com'; |
| 60 | + $this->createTestUser($username, $email, 'password123'); |
| 61 | + |
| 62 | + Auth::login($username, 'password123'); |
| 63 | + $user = Auth::user(); |
| 64 | + |
| 65 | + $this->assertNotNull($user); |
| 66 | + $this->assertEquals($username, $user['username']); |
| 67 | + $this->assertEquals($email, $user['email']); |
| 68 | + $this->assertArrayHasKey('id', $user); |
| 69 | + } |
| 70 | + |
| 71 | + public function testLoginWithValidCredentials(): void |
| 72 | + { |
| 73 | + $username = 'test_user'; |
| 74 | + $this->createTestUser($username, 'test@example.com', 'password123'); |
| 75 | + |
| 76 | + $result = Auth::login($username, 'password123'); |
| 77 | + $this->assertTrue($result); |
| 78 | + $this->assertTrue(Auth::check()); |
| 79 | + $this->assertEquals($username, $_SESSION['username'] ?? null); |
| 80 | + } |
| 81 | + |
| 82 | + public function testLoginWithInvalidPassword(): void |
| 83 | + { |
| 84 | + $username = 'test_user'; |
| 85 | + $this->createTestUser($username, 'test@example.com', 'password123'); |
| 86 | + |
| 87 | + $result = Auth::login($username, 'wrong_password'); |
| 88 | + $this->assertFalse($result); |
| 89 | + $this->assertFalse(Auth::check()); |
| 90 | + } |
| 91 | + |
| 92 | + public function testLoginWithInvalidUsername(): void |
| 93 | + { |
| 94 | + $result = Auth::login('nonexistent_user', 'password123'); |
| 95 | + $this->assertFalse($result); |
| 96 | + $this->assertFalse(Auth::check()); |
| 97 | + } |
| 98 | + |
| 99 | + public function testLoginWithEmail(): void |
| 100 | + { |
| 101 | + $email = 'test@example.com'; |
| 102 | + $this->createTestUser('test_user', $email, 'password123'); |
| 103 | + |
| 104 | + $result = Auth::login($email, 'password123'); |
| 105 | + $this->assertTrue($result); |
| 106 | + $this->assertTrue(Auth::check()); |
| 107 | + } |
| 108 | + |
| 109 | + public function testRegisterWithNewUser(): void |
| 110 | + { |
| 111 | + $result = Auth::register('test_new_user', 'newuser@example.com', 'password123'); |
| 112 | + $this->assertTrue($result); |
| 113 | + |
| 114 | + // Verify user was created |
| 115 | + $db = Database::getConnection(); |
| 116 | + $stmt = $db->prepare("SELECT * FROM users WHERE username = ?"); |
| 117 | + $stmt->execute(['test_new_user']); |
| 118 | + $user = $stmt->fetch(); |
| 119 | + $this->assertNotFalse($user); |
| 120 | + $this->assertEquals('newuser@example.com', $user['email']); |
| 121 | + } |
| 122 | + |
| 123 | + public function testRegisterWithDuplicateUsername(): void |
| 124 | + { |
| 125 | + $username = 'test_duplicate'; |
| 126 | + $this->createTestUser($username, 'first@example.com', 'password123'); |
| 127 | + |
| 128 | + // Try to register with same username |
| 129 | + $result = Auth::register($username, 'second@example.com', 'password456'); |
| 130 | + $this->assertFalse($result); |
| 131 | + } |
| 132 | + |
| 133 | + public function testRegisterWithDuplicateEmail(): void |
| 134 | + { |
| 135 | + $email = 'duplicate@example.com'; |
| 136 | + $this->createTestUser('test_first', $email, 'password123'); |
| 137 | + |
| 138 | + // Try to register with same email |
| 139 | + $result = Auth::register('test_second', $email, 'password456'); |
| 140 | + $this->assertFalse($result); |
| 141 | + } |
| 142 | + |
| 143 | + public function testLogout(): void |
| 144 | + { |
| 145 | + // Login first |
| 146 | + $this->createTestUser('test_user', 'test@example.com', 'password123'); |
| 147 | + Auth::login('test_user', 'password123'); |
| 148 | + $this->assertTrue(Auth::check()); |
| 149 | + |
| 150 | + // Logout |
| 151 | + Auth::logout(); |
| 152 | + $this->assertFalse(Auth::check()); |
| 153 | + } |
| 154 | + |
| 155 | + public function testUserLoadsPreferencesIntoSession(): void |
| 156 | + { |
| 157 | + $username = 'test_user'; |
| 158 | + $this->createTestUser($username, 'test@example.com', 'password123'); |
| 159 | + |
| 160 | + Auth::login($username, 'password123'); |
| 161 | + Auth::user(); // Load preferences |
| 162 | + |
| 163 | + // Check that preferences are in session |
| 164 | + $this->assertArrayHasKey('hide_read_items', $_SESSION); |
| 165 | + $this->assertArrayHasKey('dark_mode', $_SESSION); |
| 166 | + $this->assertArrayHasKey('timezone', $_SESSION); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Helper method to create a test user in the database. |
| 171 | + */ |
| 172 | + private function createTestUser(string $username, string $email, string $password): void |
| 173 | + { |
| 174 | + $db = Database::getConnection(); |
| 175 | + $passwordHash = password_hash($password, PASSWORD_DEFAULT); |
| 176 | + $stmt = $db->prepare("INSERT INTO users (username, email, password_hash) VALUES (?, ?, ?)"); |
| 177 | + $stmt->execute([$username, $email, $passwordHash]); |
| 178 | + } |
| 179 | +} |
0 commit comments