<commit_before><?php
    require_once 'models/user.php';
    require_once 'models/country.php';
    require_once 'models/grader/bot.php';

    class UserTest extends UnitTestWithFixtures {
        public function testCreate() {
            $user = new User();
            $user->username = 'pkakelas';
            $user->password = '<PASSWORD>';
            $user->email = '<EMAIL>';
            $user->save();
            $passwordSuccess = $user->authenticatesWithPassword( '<PASSWORD>' );
            $this->assertTrue( $passwordSuccess, 'Password must be the one associated during creation' );
            $this->assertEquals( 'pkakelas', $user->username, 'Username must be the one associated during creation' );
            $this->assertEquals( '<EMAIL>', $user->email, 'Email must be the one associated during creation' );
        }
        public function testAuthenticatesWithPassword() {
            $user = $this->buildUser( 'pkakelas' );
            $this->assertFalse( $user->authenticatesWithPassword( '<PASSWORD>' ), 'User must not be authenticated with a wrong password' );
        }
        public function testDelete() {
            $user = $this->buildUser( 'pkakelas' );
            $user->delete();
            try {
                $user = User::findByUsername( 'pkakelas' );
                $success = false;
            }
            catch ( ModelNotFoundException $e ) {
                $success = true;
            }
            $this->assertTrue( $success, 'The user must be deleted when the $user->delete() function runs' );
        }
        public function testPasswordChange() {
            $user = $this->buildUser( 'pkakelas' );
            $user->password = '<PASSWORD>';
            $user->save();
            $success = $user->authenticatesWithPassword( '<PASSWORD>' );
            $this->assertTrue( $success, 'Password must be the one associated during update' );
        }
        public function testEmailChange() {
            $user = $this->buildUser( 'pkakelas' );
            $user->email = '<EMAIL>';
            $user->save();
            $this->assertEquals( '<EMAIL>', $user->email, 'Email must be the one associated during update' );
        }
        public function testSetCountry() {
            $country = $this->buildCountry( 'Greece', 'GR' );

            $user = $this->buildUser( 'pkakelas' );
            $user->country = $country;
            $this->assertEquals( 1, $user->country->id, 'Country must be the one associated during update' );
        }
        public function testDuplicateUsername() {
            $caught = false;
            $user1 = $this->buildUser( 'pkakelas' );
            try {
                $user2 = $this->buildUser( 'pkakelas' );
            }
            catch ( ModelValidationException $e ) {
                $caught = true;
                $this->assertEquals(
                    'username_used',
                    $e->error,
                    "If the username is used we must get a 'username_used' error"
                );
            }
            $this->assertTrue( $caught, 'A ModelValidationException must be caught if we try to make a duplicate username' );
        }
        public function testDuplicateEmail() {
            $caught = false;
            $user1 = new User();
            $user2 = new User();
            $user1->username = 'pkakelas';
            $user2->username = 'dionyziz';
            $user1->password = $user2->password = '<PASSWORD>';
            $user1->email = $user2->email = '<EMAIL>';
            $user1->save();
            try {
                $user2->save();
            }
            catch ( ModelValidationException $e ) {
                $caught = true;
                $this->assertEquals(
                    'email_used',
                    $e->error,
                    "If the email is used we must get a 'email_used' error"
                );
            }
            $this->assertTrue( $caught, 'A ModelValidationException must be caught if we try to make a duplicate email' );
        }
        public function testFindNonExistentUser() {
            $caught = false;
            try {
                $user = new User( 1 );
            }
            catch ( ModelNotFoundException $e ) {
                $caught = true;
            }
            $this->assertTrue( $caught, 'When we try to find a non existent user we must get a ModelNotFoundException' );
        }
        public function testFindById() {
            $user = $this->buildUser( 'dionyziz' );
            $dbUser = new User( 1 );
            $this->assertEquals( $user->username, $dbUser->username, "User's username must be correctly stored in the database" );
            $this->assertEquals( $user->email, $dbUser->email, "User's email must be correctly stored in the database" );
            $this->assertEquals( $user->boturl, $dbUser->boturl, "User's boturl must be correctly stored in the database" );
        }
        public function testFindByUsername() {
            $user = $this->buildUser( 'pkakelas' );
            $dbUser = User::findByUsername( 'pkakelas' );
            $this->assertEquals( $user->id, intval( $dbUser->id ), "findByUsername must find the correct user" );
        }
        public function testAuthenticationAfterRenewSessionId() {
            $user = $this->buildUser( 'pkakelas' );

            $user->renewSessionId();
            $passwordSuccess = $user->authenticatesWithPassword( '<PASSWORD>' );
            $this->assertTrue( $passwordSuccess, 'Password must not be changed after "renewSessionId" is run' );
        }
        public function testCreateForgotPasswordLink() {
            $user = $this->buildUser( 'pkakelas' );
            
            $link = $user->createForgotPasswordLink();
            $this->assertTrue( isset( $user->forgotpasswordtoken ), 'CreateForgotPasswordLink must save the token to $user->forgotpasswordtoken' );
            $this->assertTrue( !empty( $user->forgotpasswordtoken ), '$user->forgotpasswordtokeni must not be empty' );
            $this->assertTrue( isset( $user->forgotpasswordrequestcreated ), 'CreateForgotPasswordLink must save the time it created the link to $user->forgotpasswordrequestcreated' );
        }
        public function testRevokePasswordCheck() {
            $user = $this->buildUser( 'pkakelas' );
            
            $user->createForgotPasswordLink();
            try {
                $user->revokePasswordCheck( $user->forgotpasswordtoken );
                $trueSuccess = true;
            } 
            catch ( ForgotPasswordModelInvalidTokenException $e ) {
                $trueSuccess = false; 
            }
            try {
                $user->revokePasswordCheck( '<PASSWORD>' );
                $falseSuccess = false;
            } 
            catch ( ForgotPasswordModelInvalidTokenException $e ) {
                $falseSuccess = true; 
            }
            try {
                $user->revokePasswordCheck( '' );
                $emptySuccess = false;
            } 
            catch ( ForgotPasswordModelInvalidTokenException $e ) {
                $emptySuccess = true; 
            }
            $oldToken = $user->forgotpasswordtoken;
            $user->createforgotpasswordlink();
            try {
                $user->revokePasswordCheck( $oldToken );
                $oldTokenSuccess = false;
            } 
            catch ( ForgotPasswordModelInvalidTokenException $e ) {
                $oldTokenSuccess = true; 
            }
            $user->forgotpasswordrequestcreated = time() - 60 * 24 * 2; 
            try {
                $user->revokePasswordCheck( $user->forgotpasswordtoken );
                $expiredSuccess = false;
            }
            catch ( ModelValidationException $e ) {
                $expiredSuccess = true;
            }
            $this->assertTrue( $trueSuccess, 'revokePasswordCheck should validate correct tokens' );
            $this->assertTrue( $falseSuccess, 'revokePasswordCheck should not validate invalid tokens' );
            $this->assertTrue( $emptySuccess, 'revokePasswordCheck should not validate empty tokens' );
            $this->assertTrue( $oldTokenSuccess, 'revokePasswordCheck should not validate with old tokens' );<commit_msg>Fix the timing on UserTest/revokePasswordCheck()<commit_after><?php
    require_once 'models/user.php';
    require_once 'models/country.php';
    require_once 'models/grader/bot.php';

    class UserTest extends UnitTestWithFixtures {
        public function testCreate() {
            $user = new User();
            $user->username = 'pkakelas';
            $user->password = '<PASSWORD>';
            $user->email = '<EMAIL>';
            $user->save();
            $passwordSuccess = $user->authenticatesWithPassword( '<PASSWORD>' );
            $this->assertTrue( $passwordSuccess, 'Password must be the one associated during creation' );
            $this->assertEquals( 'pkakelas', $user->username, 'Username must be the one associated during creation' );
            $this->assertEquals( '<EMAIL>', $user->email, 'Email must be the one associated during creation' );
        }
        public function testAuthenticatesWithPassword() {
            $user = $this->buildUser( 'pkakelas' );
            $this->assertFalse( $user->authenticatesWithPassword( 'wrongsecret' ), 'User must not be authenticated with a wrong password' );
        }
        public function testDelete() {
            $user = $this->buildUser( 'pkakelas' );
            $user->delete();
            try {
                $user = User::findByUsername( 'pkakelas' );
                $success = false;
            }
            catch ( ModelNotFoundException $e ) {
                $success = true;
            }
            $this->assertTrue( $success, 'The user must be deleted when the $user->delete() function runs' );
        }
        public function testPasswordChange() {
            $user = $this->buildUser( 'pkakelas' );
            $user->password = '<PASSWORD>';
            $user->save();
            $success = $user->authenticatesWithPassword( '<PASSWORD>' );
            $this->assertTrue( $success, 'Password must be the one associated during update' );
        }
        public function testEmailChange() {
            $user = $this->buildUser( 'pkakelas' );
            $user->email = '<EMAIL>';
            $user->save();
            $this->assertEquals( '<EMAIL>', $user->email, 'Email must be the one associated during update' );
        }
        public function testSetCountry() {
            $country = $this->buildCountry( 'Greece', 'GR' );

            $user = $this->buildUser( 'pkakelas' );
            $user->country = $country;
            $this->assertEquals( 1, $user->country->id, 'Country must be the one associated during update' );
        }
        public function testDuplicateUsername() {
            $caught = false;
            $user1 = $this->buildUser( 'pkakelas' );
            try {
                $user2 = $this->buildUser( 'pkakelas' );
            }
            catch ( ModelValidationException $e ) {
                $caught = true;
                $this->assertEquals(
                    'username_used',
                    $e->error,
                    "If the username is used we must get a 'username_used' error"
                );
            }
            $this->assertTrue( $caught, 'A ModelValidationException must be caught if we try to make a duplicate username' );
        }
        public function testDuplicateEmail() {
            $caught = false;
            $user1 = new User();
            $user2 = new User();
            $user1->username = 'pkakelas';
            $user2->username = 'dionyziz';
            $user1->password = $user2->password = '<PASSWORD>';
            $user1->email = $user2->email = '<EMAIL>';
            $user1->save();
            try {
                $user2->save();
            }
            catch ( ModelValidationException $e ) {
                $caught = true;
                $this->assertEquals(
                    'email_used',
                    $e->error,
                    "If the email is used we must get a 'email_used' error"
                );
            }
            $this->assertTrue( $caught, 'A ModelValidationException must be caught if we try to make a duplicate email' );
        }
        public function testFindNonExistentUser() {
            $caught = false;
            try {
                $user = new User( 1 );
            }
            catch ( ModelNotFoundException $e ) {
                $caught = true;
            }
            $this->assertTrue( $caught, 'When we try to find a non existent user we must get a ModelNotFoundException' );
        }
        public function testFindById() {
            $user = $this->buildUser( 'dionyziz' );
            $dbUser = new User( 1 );
            $this->assertEquals( $user->username, $dbUser->username, "User's username must be correctly stored in the database" );
            $this->assertEquals( $user->email, $dbUser->email, "User's email must be correctly stored in the database" );
            $this->assertEquals( $user->boturl, $dbUser->boturl, "User's boturl must be correctly stored in the database" );
        }
        public function testFindByUsername() {
            $user = $this->buildUser( 'pkakelas' );
            $dbUser = User::findByUsername( 'pkakelas' );
            $this->assertEquals( $user->id, intval( $dbUser->id ), "findByUsername must find the correct user" );
        }
        public function testAuthenticationAfterRenewSessionId() {
            $user = $this->buildUser( 'pkakelas' );

            $user->renewSessionId();
            $passwordSuccess = $user->authenticatesWithPassword( '<PASSWORD>' );
            $this->assertTrue( $passwordSuccess, 'Password must not be changed after "renewSessionId" is run' );
        }
        public function testCreateForgotPasswordLink() {
            $user = $this->buildUser( 'pkakelas' );
            
            $link = $user->createForgotPasswordLink();
            $this->assertTrue( isset( $user->forgotpasswordtoken ), 'CreateForgotPasswordLink must save the token to $user->forgotpasswordtoken' );
            $this->assertTrue( !empty( $user->forgotpasswordtoken ), '$user->forgotpasswordtokeni must not be empty' );
            $this->assertTrue( isset( $user->forgotpasswordrequestcreated ), 'CreateForgotPasswordLink must save the time it created the link to $user->forgotpasswordrequestcreated' );
        }
        public function testRevokePasswordCheck() {
            $user = $this->buildUser( 'pkakelas' );
            
            $user->createForgotPasswordLink();
            try {
                $user->revokePasswordCheck( $user->forgotpasswordtoken );
                $trueSuccess = true;
            } 
            catch ( ForgotPasswordModelInvalidTokenException $e ) {
                $trueSuccess = false; 
            }
            try {
                $user->revokePasswordCheck( '<PASSWORD>' );
                $falseSuccess = false;
            } 
            catch ( ForgotPasswordModelInvalidTokenException $e ) {
                $falseSuccess = true; 
            }
            try {
                $user->revokePasswordCheck( '' );
                $emptySuccess = false;
            } 
            catch ( ForgotPasswordModelInvalidTokenException $e ) {
                $emptySuccess = true; 
            }
            $oldToken = $user->forgotpasswordtoken;
            $user->createforgotpasswordlink();
            try {
                $user->revokePasswordCheck( $oldToken );
                $oldTokenSuccess = false;
            } 
            catch ( ForgotPasswordModelInvalidTokenException $e ) {
                $oldTokenSuccess = true; 
            }
            $user->forgotpasswordrequestcreated = time() - 60 * 60 * 24 * 2;
            try {
                $user->revokePasswordCheck( $user->forgotpasswordtoken );
                $expiredSuccess = false;
            }
            catch ( ModelValidationException $e ) {
                $expiredSuccess = true;
            }
            $this->assertTrue( $trueSuccess, 'revokePasswordCheck should validate correct tokens' );
            $this->assertTrue( $falseSuccess, 'revokePasswordCheck should not validate invalid tokens' );
            $this->assertTrue( $emptySuccess, 'revokePasswordCheck should not validate empty tokens' );
            $this->assertTrue( $oldTokenSuccess, 'revokePasswordCheck should not validate with old tokens' );
{'fraction_non_alphanumeric': 0.11735557167029248, 'fraction_numerical': 0.0025380710659898475, 'mean_word_length': 1.8478747203579418, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 218, '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': '3355953', 'n_tokens_mistral': 4268, 'n_tokens_neox': 4159, 'n_words': 1063}