<commit_before><?php

namespace Rhubarb\Scaffolds\Authentication\Tests;

use Rhubarb\Crown\Tests\Fixtures\TestCases\RhubarbTestCase;
use Rhubarb\Scaffolds\Authentication\LoginProviders\LoggedLoginProvider;

class LoggedLoginProviderTest extends RhubarbTestCase
{

}<commit_msg>Adding unit tests for LoggedLoginProvider<commit_after><?php

namespace Rhubarb\Scaffolds\Authentication\Tests;

use Rhubarb\Crown\Encryption\HashProvider;
use Rhubarb\Crown\Encryption\Sha512HashProvider;
use Rhubarb\Crown\LoginProviders\LoginProvider;
use Rhubarb\Crown\Tests\Fixtures\TestCases\RhubarbTestCase;
use Rhubarb\Scaffolds\Authentication\DatabaseSchema;
use Rhubarb\Scaffolds\Authentication\LoginProviders\LoggedLoginProvider;
use Rhubarb\Scaffolds\Authentication\User;
use Rhubarb\Scaffolds\Authentication\UserLoginAttempt;
use Rhubarb\Stem\Schema\SolutionSchema;

class LoggedLoginProviderTest extends RhubarbTestCase
{
    public function testFailedLoginAttemptBeingRecorded()
    {
        $this->assertEquals(0, UserLoginAttempt::find()->count());

        LoginProvider::setProviderClassName(LoggedLoginProvider::class);
        SolutionSchema::registerSchema('Authentication', DatabaseSchema::class);

        try {
            $loggedLoginProvider = LoginProvider::getProvider();
            $loggedLoginProvider->login('test', 'test');
        } catch (\Exception $exception) {
        }

        $this->assertEquals(1, UserLoginAttempt::find()->count());
        $userLoginAttempt = UserLoginAttempt::find()[0];

        $this->assertFalse($userLoginAttempt->Successful);
        $this->assertNotEmpty($userLoginAttempt->ExceptionMessage);
    }

    public function testSuccessfulLoginAttemptBeingRecorded()
    {
        $this->assertEquals(0, UserLoginAttempt::find()->count());

        LoginProvider::setProviderClassName(LoggedLoginProvider::class);
        HashProvider::setProviderClassName(Sha512HashProvider::class);
        SolutionSchema::registerSchema('Authentication', DatabaseSchema::class);

        $user = new User();
        $user->setNewPassword("<PASSWORD>");
        $user->Username = "test";
        $user->Forename = "test";
        $user->Enabled = 1;
        $user->save();

        try {
            $loggedLoginProvider = LoginProvider::getProvider();
            $loggedLoginProvider->login('test', 'abc<PASSWORD>');
        } catch (\Exception $exception) {
        }

        $this->assertEquals(1, UserLoginAttempt::find()->count());
        $userLoginAttempt = UserLoginAttempt::find()[0];

        $this->assertTrue($userLoginAttempt->Successful);
        $this->assertEmpty($userLoginAttempt->ExceptionMessage);
    }
}
{'fraction_non_alphanumeric': 0.1239638281838734, 'fraction_numerical': 0.0048982667671439335, 'mean_word_length': 4.374493927125506, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '808110', 'n_tokens_mistral': 751, 'n_tokens_neox': 718, 'n_words': 96}