|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace phpMyFAQ; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use phpMyFAQ\Database\Sqlite3; |
| 7 | +use phpMyFAQ\User\UserData; |
| 8 | +use PHPUnit\Framework\MockObject\MockObject; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use ReflectionClass; |
| 11 | + |
| 12 | +class UserTest extends TestCase |
| 13 | +{ |
| 14 | + private Configuration|MockObject $configuration; |
| 15 | + private Sqlite3|MockObject $database; |
| 16 | + private User $user; |
| 17 | + private UserData|MockObject $userData; |
| 18 | + |
| 19 | + protected function setUp(): void |
| 20 | + { |
| 21 | + $this->configuration = $this->createMock(Configuration::class); |
| 22 | + $this->database = $this->createMock(Sqlite3::class); |
| 23 | + $this->userData = $this->createMock(UserData::class); |
| 24 | + |
| 25 | + $this->configuration->method('getDb')->willReturn($this->database); |
| 26 | + $this->configuration->method('get')->willReturnMap([ |
| 27 | + ['security.permLevel', 'basic'], |
| 28 | + ]); |
| 29 | + |
| 30 | + $this->user = new User($this->configuration); |
| 31 | + $this->user->userdata = $this->userData; |
| 32 | + } |
| 33 | + |
| 34 | + public function testIsEmailAddressReturnsTrueForValidEmail(): void |
| 35 | + { |
| 36 | + $reflection = new ReflectionClass($this->user); |
| 37 | + $method = $reflection->getMethod('isEmailAddress'); |
| 38 | + |
| 39 | + $result = $method->invoke($this->user, 'test@example.com'); |
| 40 | + $this->assertTrue($result); |
| 41 | + } |
| 42 | + |
| 43 | + public function testIsEmailAddressReturnsFalseForInvalidEmail(): void |
| 44 | + { |
| 45 | + $reflection = new ReflectionClass($this->user); |
| 46 | + $method = $reflection->getMethod('isEmailAddress'); |
| 47 | + |
| 48 | + $result = $method->invoke($this->user, 'invalid-email'); |
| 49 | + $this->assertFalse($result); |
| 50 | + } |
| 51 | + |
| 52 | + public function testCreateUserThrowsExceptionWhenEmailAlreadyExistsAsLogin(): void |
| 53 | + { |
| 54 | + $this->expectException(Exception::class); |
| 55 | + $this->expectExceptionMessage(User::ERROR_USER_EMAIL_NOT_UNIQUE); |
| 56 | + |
| 57 | + // Mock that login is valid |
| 58 | + $this->database->method('escape')->willReturn('test@example.com'); |
| 59 | + |
| 60 | + // Mock that getUserByLogin returns false (login doesn't exist as login) |
| 61 | + $this->database->method('query')->willReturn(true); |
| 62 | + $this->database->method('numRows')->willReturn(0); |
| 63 | + |
| 64 | + // Mock that email exists in userdata |
| 65 | + $this->userData->method('emailExists')->willReturn(true); |
| 66 | + |
| 67 | + $this->user->createUser('test@example.com'); |
| 68 | + } |
| 69 | + |
| 70 | + public function testCreateUserDoesNotCheckEmailWhenLoginIsNotEmail(): void |
| 71 | + { |
| 72 | + // Mock database operations to simulate login not existing |
| 73 | + $this->database->method('escape')->willReturn('username'); |
| 74 | + $this->database->method('query')->willReturn(true); |
| 75 | + $this->database->method('numRows')->willReturn(0); |
| 76 | + |
| 77 | + // emailExists should never be called for non-email logins |
| 78 | + $this->userData->expects($this->never())->method('emailExists'); |
| 79 | + |
| 80 | + // This will throw an exception because no auth container is set up, |
| 81 | + // but that's expected - we just want to verify emailExists wasn't called |
| 82 | + try { |
| 83 | + $this->user->createUser('username'); |
| 84 | + } catch (Exception $e) { |
| 85 | + // Expected - ignore this exception as we're only testing the email check logic |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public function testCreateUserThrowsExceptionWhenLoginNotUnique(): void |
| 90 | + { |
| 91 | + $this->expectException(Exception::class); |
| 92 | + $this->expectExceptionMessage(User::ERROR_USER_LOGIN_NOT_UNIQUE); |
| 93 | + |
| 94 | + // Mock that login exists |
| 95 | + $this->database->method('escape')->willReturn('existinguser'); |
| 96 | + $this->database->method('query')->willReturn(true); |
| 97 | + $this->database->method('numRows')->willReturn(1); |
| 98 | + $this->database->method('fetchArray')->willReturn([ |
| 99 | + 'user_id' => 1, |
| 100 | + 'login' => 'existinguser', |
| 101 | + 'account_status' => 'active', |
| 102 | + 'is_superadmin' => false, |
| 103 | + 'auth_source' => 'local' |
| 104 | + ]); |
| 105 | + |
| 106 | + $this->user->createUser('existinguser'); |
| 107 | + } |
| 108 | +} |
0 commit comments