test: add login

This commit is contained in:
grandeljay 2023-09-25 12:15:51 +02:00
parent f0b67a0529
commit b75ef54a51
2 changed files with 54 additions and 1 deletions

View file

@ -315,7 +315,13 @@ class User
*/
public function logIn(string $email = '', string $password = '', bool $user_login_is_persistent = false): bool
{
global $database;
$database = new Database(
DATABASE_HOST,
DATABASE_NAME,
DATABASE_USER,
DATABASE_PASSWORD
);
$database->connect();
$login_was_successful = false;

47
tests/LogInTest.php Normal file
View file

@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace wishthis\Tests;
use PHPUnit\Framework\TestCase;
use wishthis\{Database, User};
final class LogInTest extends TestCase
{
public function testLogIn(): void
{
\define('DEFAULT_LOCALE', 'en_GB');
\define('ROOT', \dirname(__DIR__));
require __DIR__ . '/../src/classes/wishthis/User.php';
require __DIR__ . '/../src/classes/wishthis/Database.php';
require __DIR__ . '/../src/functions/gettext.php';
require __DIR__ . '/../src/config/config.php';
$user = User::getCurrent();
$userEmpty = new User();
$userEmail = 'email@domain.tld';
$userPassword = '1234isnotarealpassword';
$this->assertEquals($user, $userEmpty);
$this->assertFalse($user->isLoggedIn());
$database = new Database(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
$database->connect();
$database->query(
'REPLACE INTO `users`
(`email`, `password`) VALUES
(:userEmail, :userPassword)',
array(
'userEmail' => $userEmail,
'userPassword' => $userPassword,
)
);
$userLoginSuccessful = $user->logIn($userEmail, $userPassword);
$this->assertTrue($userLoginSuccessful);
$this->assertEquals($userEmail, $_SESSION['user']->getEmail());
}
}