diff --git a/application/Libraries/Aauth.php b/application/Libraries/Aauth.php index 199e058..96f6f93 100644 --- a/application/Libraries/Aauth.php +++ b/application/Libraries/Aauth.php @@ -261,7 +261,7 @@ class Aauth if (! is_null($orderBy)) { - $user->orderBy($orderBy[0], $orderBy[1]); + $user->orderBy($orderBy); } return $user->findAll(); @@ -349,7 +349,7 @@ class Aauth if (! $userId) { - $userId = $this->session->id; + $userId = $this->session->user['id']; } if ($user = $userModel->find($userId)) @@ -385,7 +385,7 @@ class Aauth if (! $email) { - $where = ['id' => $this->session->id]; + $where = ['id' => $this->session->user['id']]; } else { @@ -413,7 +413,7 @@ class Aauth if (! $userId) { - $userId = $this->session->id; + $userId = $this->session->user['id']; } if (! $userModel->existsById($userId)) @@ -439,7 +439,7 @@ class Aauth if (! $userId) { - $userId = $this->session->id; + $userId = $this->session->user['id']; } if (! $userModel->existsById($userId)) @@ -465,7 +465,7 @@ class Aauth if (! $userId) { - $userId = $this->session->id; + $userId = $this->session->user['id']; } if (! $userModel->existsById($userId)) @@ -740,7 +740,7 @@ class Aauth $data['username'] = $user['username']; $data['email'] = $user['email']; $data['loggedIn'] = true; - $this->session->set($data); + $this->session->set('user', $data); if ($remember) { @@ -813,7 +813,7 @@ class Aauth if ($user = $userModel->get()->getFirstRow()) { - $this->session->set([ + $this->session->set('user', [ 'id' => $user->id, 'username' => $user->username, 'email' => $user->email, diff --git a/tests/Aauth/Libraries/Aauth/ErrorsTest.php b/tests/Aauth/Libraries/Aauth/ErrorsTest.php index 63cd029..7d9b096 100644 --- a/tests/Aauth/Libraries/Aauth/ErrorsTest.php +++ b/tests/Aauth/Libraries/Aauth/ErrorsTest.php @@ -17,6 +17,7 @@ class ErrorsTest extends \CIUnitTestCase { parent::setUp(); + $this->library = new Aauth(null, true); $_COOKIE = []; $_SESSION = []; } @@ -55,7 +56,6 @@ class ErrorsTest extends \CIUnitTestCase public function testErrors() { - $this->library = new Aauth(NULL, TRUE); $this->assertCount(0, $this->library->getErrorsArray()); $this->library->error('test message 1'); $this->assertEquals(['test message 1'], $this->library->getErrorsArray()); @@ -63,7 +63,6 @@ class ErrorsTest extends \CIUnitTestCase public function testErrorsArray() { - $this->library = new Aauth(NULL, TRUE); $this->assertCount(0, $this->library->getErrorsArray()); $this->library->error(['test message 1','test message 2']); $this->assertEquals(['test message 1','test message 2'], $this->library->getErrorsArray()); @@ -71,7 +70,6 @@ class ErrorsTest extends \CIUnitTestCase public function testPrintErrorsReturn() { - $this->library = new Aauth(NULL, TRUE); $this->library->error('test message 1'); $this->assertEquals('test message 1', $this->library->printErrors('
', true)); $this->library->error('test message 2'); @@ -80,7 +78,6 @@ class ErrorsTest extends \CIUnitTestCase public function testPrintErrorsEcho() { - $this->library = new Aauth(NULL, TRUE); $this->library->error('test message 1'); $this->library->printErrors('
'); $this->expectOutputString('test message 1'); diff --git a/tests/Aauth/Libraries/Aauth/UserTest.php b/tests/Aauth/Libraries/Aauth/UserTest.php index cf0d357..4f82aa0 100644 --- a/tests/Aauth/Libraries/Aauth/UserTest.php +++ b/tests/Aauth/Libraries/Aauth/UserTest.php @@ -1,5 +1,10 @@ library = new Aauth(null, true); + $_COOKIE = []; + $_SESSION = []; } public function tearDown() { + } + + protected function getInstance($options=[]) + { + $defaults = [ + 'sessionDriver' => 'CodeIgniter\Session\Handlers\FileHandler', + 'sessionCookieName' => 'ci_session', + 'sessionExpiration' => 7200, + 'sessionSavePath' => 'null', + 'sessionMatchIP' => false, + 'sessionTimeToUpdate' => 300, + 'sessionRegenerateDestroy' => false, + 'cookieDomain' => '', + 'cookiePrefix' => '', + 'cookiePath' => '/', + 'cookieSecure' => false, + ]; + + $config = (object)$defaults; + + $session = new MockSession(new FileHandler($config, Services::request()->getIPAddress()), $config); + $session->setLogger(new TestLogger(new Logger())); + $session->start(); + + return $session; } //-------------------------------------------------------------------- public function testUpdateUser() { - $this->library = new Aauth(null, true); $userPre = $this->library->getUser(2); $this->library->updateUser(2, 'user1@example.com', 'password987654', 'user1'); $user = $this->library->getUser(2); @@ -39,11 +71,12 @@ class UserTest extends CIDatabaseTestCase $this->library->updateUser(2, null, null, 'user1'); $userAfter = $this->library->getUser(2); $this->assertEquals($user['username'], $userAfter['username']); + $this->assertFalse($this->library->updateUser(2, 'asasdfasd')); + $this->assertFalse($this->library->updateUser(2)); } public function testDeleteUser() { - $this->library = new Aauth(null, true); $users = $this->library->listUsers(); $this->assertCount(2, $users); $this->library->deleteUser(2); @@ -54,39 +87,51 @@ class UserTest extends CIDatabaseTestCase public function testListUsers() { - $this->library = new Aauth(null, true); $users = $this->library->listUsers(); $this->assertCount(2, $users); + $this->assertEquals('admin', $users[0]['username']); + $this->assertEquals('user', $users[1]['username']); + $usersOrderBy = $this->library->listUsers(0, 0, null, 'id DESC'); + $this->assertEquals('user', $usersOrderBy[0]['username']); + $this->assertEquals('admin', $usersOrderBy[1]['username']); } public function testGetUser() { - $this->library = new Aauth(null, true); $user = $this->library->getUser(1); $this->assertEquals('1', $user['id']); $this->assertEquals('admin', $user['username']); $this->assertEquals('admin@example.com', $user['email']); - } - - public function testGetUserUserVars() - { - $this->library = new Aauth(null, true); - $user = $this->library->getUser(1, true); - $this->assertInternalType('array', $user['variables']); + $this->assertFalse($this->library->getUser(99)); + + $userVar = $this->library->getUser(1, true); + $this->assertInternalType('array', $userVar['variables']); + + $session = $this->getInstance(); + $this->library = new Aauth(NULL, $session); + $session->set('user', [ + 'id' => 1, + ]); + $userIdNone = $this->library->getUser(); + $this->assertEquals('admin', $userIdNone['username']); } public function testGetUserId() { - $this->library = new Aauth(null, true); $userIdEmail = $this->library->getUserId('admin@example.com'); $this->assertEquals('1', $userIdEmail); - // $userIdNone = $this->library->getUserId(); - // $this->assertEquals('1', $userIdNone); + + $session = $this->getInstance(); + $this->library = new Aauth(NULL, $session); + $session->set('user', [ + 'id' => 1, + ]); + $userIdNone = $this->library->getUserId(); + $this->assertEquals('1', $userIdNone); } public function testBanUser() { - $this->library = new Aauth(null, true); $this->assertFalse($this->library->isBanned(1)); $this->library->banUser(1); $this->assertTrue($this->library->isBanned(1)); @@ -94,7 +139,6 @@ class UserTest extends CIDatabaseTestCase public function testUnbanUser() { - $this->library = new Aauth(null, true); $this->library->banUser(1); $this->assertTrue($this->library->isBanned(1)); $this->library->unbanUser(1);