Skip to content

Commit

Permalink
test: added tests for CurrentUser class
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Mar 23, 2024
1 parent 7b0aa5c commit 29746c1
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 6 deletions.
2 changes: 1 addition & 1 deletion phpmyfaq/src/phpMyFAQ/Database/Sqlite3.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function fetchArray(mixed $result): ?array
*/
public function fetchRow(mixed $result): mixed
{
return $result->fetchAssoc();
return $result->fetchArray(SQLITE3_ASSOC);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions phpmyfaq/src/phpMyFAQ/User/CurrentUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,13 +596,13 @@ public function setAuthSource(string $authSource): bool
$this->getUserId()
);

return $this->configuration->getDb()->query($update);
return (bool) $this->configuration->getDb()->query($update);
}

/**
* Saves remember me token in the database.
*/
protected function setRememberMe(string $rememberMe): bool
public function setRememberMe(string $rememberMe): bool
{
$update = sprintf(
"UPDATE %sfaquser SET remember_me = '%s' WHERE user_id = %d",
Expand All @@ -611,7 +611,7 @@ protected function setRememberMe(string $rememberMe): bool
$this->getUserId()
);

return $this->configuration->getDb()->query($update);
return (bool) $this->configuration->getDb()->query($update);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" bootstrap="tests/bootstrap.php" executionOrder="depends,defects" beStrictAboutOutputDuringTests="true" failOnRisky="true" failOnWarning="true" testdox="true" cacheDirectory=".phpunit.cache" requireCoverageMetadata="false" beStrictAboutCoverageMetadata="true" displayDetailsOnTestsThatTriggerWarnings="true">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" bootstrap="tests/bootstrap.php" executionOrder="depends,defects" beStrictAboutOutputDuringTests="true" failOnRisky="true" failOnWarning="true" testdox="true" cacheDirectory=".phpunit.cache" requireCoverageMetadata="false" beStrictAboutCoverageMetadata="true" displayDetailsOnTestsThatTriggerWarnings="true" displayDetailsOnTestsThatTriggerNotices="true">
<testsuites>
<testsuite name="phpMyFAQ Test Suite">
<directory>./tests/phpMyFAQ</directory>
Expand Down
2 changes: 1 addition & 1 deletion tests/phpMyFAQ/ExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function setUp(): void
*/
public function testCreatePdf(): void
{
$pdf = Export::create($this->faq, $this->category, $this->configuration, 'pdf');
$pdf = Export::create($this->faq, $this->category, $this->configuration);
$this->assertInstanceOf(Pdf::class, $pdf);
}

Expand Down
103 changes: 103 additions & 0 deletions tests/phpMyFAQ/User/CurrentUserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace phpMyFAQ\User;

use phpMyFAQ\Configuration;
use phpMyFAQ\Core\Exception;
use phpMyFAQ\Database\Sqlite3;
use phpMyFAQ\Strings;
use PHPUnit\Framework\TestCase;

class CurrentUserTest extends TestCase
{
private CurrentUser $currentUser;

/**
* @throws Exception
*/
protected function setUp(): void
{
session_start();

parent::setUp();

Strings::init();

$dbHandle = new Sqlite3();
$dbHandle->connect(PMF_TEST_DIR . '/test.db', '', '');
$configuration = new Configuration($dbHandle);

$this->currentUser = new CurrentUser($configuration);
}

protected function tearDown(): void
{
parent::tearDown();
session_destroy();
}

public function testIsLoggedInReturnsTrueWhenLoggedIn(): void
{
$this->currentUser->setLoggedIn(true);
$this->assertTrue($this->currentUser->isLoggedIn());
}

public function testIsLoggedInReturnsFalseWhenNotLoggedIn(): void
{
$this->currentUser->setLoggedIn(false);
$this->assertFalse($this->currentUser->isLoggedIn());
}

public function testSessionIsTimedOutReturnsFalseWhenNotTimedOut(): void
{
$this->currentUser->setSessionTimeout(3600); // 1 hour
$this->assertFalse($this->currentUser->sessionIsTimedOut());
}

/**
* @throws Exception
*/
public function testLoginFailureWithInvalidCredentials(): void
{
$this->expectException(Exception::class);
$this->currentUser->login('invalidLogin', 'invalidPassword');
}

/**
* @throws Exception
*/
public function testLoginSuccessWithValidCredentials(): void
{
$this->currentUser->login('admin', 'password');
$this->assertTrue($this->currentUser->isLoggedIn());
}

/**
* @throws Exception
*/
public function testLoginSuccessWithValidCredentialsAndRememberMe(): void
{
$this->currentUser->setRememberMe(true);
$this->currentUser->login('admin', 'password');
$this->assertTrue($this->currentUser->isLoggedIn());
}

/**
* @throws Exception
*/
public function testIsLocalUser(): void
{
$this->currentUser->login('admin', 'password');
$this->assertTrue($this->currentUser->isLocalUser());
}

/**
* @throws Exception
*/
public function testDeleteFromSession(): void
{
$this->currentUser->login('admin', 'password');
$this->currentUser->deleteFromSession();
$this->assertFalse($this->currentUser->isLoggedIn());
}
}

0 comments on commit 29746c1

Please sign in to comment.