HEX
Server: Apache
System: Linux WWW 6.1.0-40-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.153-1 (2025-09-20) x86_64
User: web11 (1011)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: /var/www/apklausos/tests/unit/api/AuthTokenSimpleTest.php
<?php

namespace ls\tests\unit\api;

use ls\tests\TestBaseClass;
use LimeSurvey\Api\Authentication\AuthenticationTokenSimple;
use LimeSurvey\Api\Authentication\SessionUtil;
use LimeSurvey\Api\Transformer\Formatter\FormatterDateTimeToJson;


/**
 * @testdox Authentication Token Simple
 */
class AuthenticationTokenSimpleTest extends TestBaseClass
{
    public static function tearDownAfterClass(): void
    {
        $_SESSION = [];
        $_POST    = [];
    }

    /**
     * @testdox doLogin() Returns boolean token if login successful.
     */
    public function testDoLogin()
    {
        $username = getenv('ADMINUSERNAME');
        if (!$username) {
            $username = 'admin';
        }

        $password = getenv('PASSWORD');
        if (!$password) {
            $password = 'password';
        }

        $authTokenSimple = new AuthenticationTokenSimple(
            new SessionUtil,
            new FormatterDateTimeToJson
        );
        $result = $authTokenSimple->login(
            $username,
            $password
        );

        $this->assertNotEmpty($result);
        $this->assertIsString($result['token']);
        $this->assertIsString($result['expires']);
        $this->assertIsInt($result['userId']);
    }

    /**
     * @testdox checkKey() returns false on valid key.
     */
    public function testCheckKeySessionNotFound()
    {
        $authTokenSimple = new AuthenticationTokenSimple(
            new SessionUtil,
            new FormatterDateTimeToJson
        );
        $result = $authTokenSimple->isAuthenticated('invalid-key');
        $this->assertFalse($result);
    }
}