Browse Source

updated Libraries/Aauth, Aauth/ErrorsTest and Aauth/UserTest

v3-dev
REJack 6 years ago
parent
commit
adca6004b3
No known key found for this signature in database
GPG Key ID: 4A44B48700429F46
  1. 16
      application/Libraries/Aauth.php
  2. 5
      tests/Aauth/Libraries/Aauth/ErrorsTest.php
  3. 76
      tests/Aauth/Libraries/Aauth/UserTest.php

16
application/Libraries/Aauth.php

@ -261,7 +261,7 @@ class Aauth
if (! is_null($orderBy)) if (! is_null($orderBy))
{ {
$user->orderBy($orderBy[0], $orderBy[1]); $user->orderBy($orderBy);
} }
return $user->findAll(); return $user->findAll();
@ -349,7 +349,7 @@ class Aauth
if (! $userId) if (! $userId)
{ {
$userId = $this->session->id; $userId = $this->session->user['id'];
} }
if ($user = $userModel->find($userId)) if ($user = $userModel->find($userId))
@ -385,7 +385,7 @@ class Aauth
if (! $email) if (! $email)
{ {
$where = ['id' => $this->session->id]; $where = ['id' => $this->session->user['id']];
} }
else else
{ {
@ -413,7 +413,7 @@ class Aauth
if (! $userId) if (! $userId)
{ {
$userId = $this->session->id; $userId = $this->session->user['id'];
} }
if (! $userModel->existsById($userId)) if (! $userModel->existsById($userId))
@ -439,7 +439,7 @@ class Aauth
if (! $userId) if (! $userId)
{ {
$userId = $this->session->id; $userId = $this->session->user['id'];
} }
if (! $userModel->existsById($userId)) if (! $userModel->existsById($userId))
@ -465,7 +465,7 @@ class Aauth
if (! $userId) if (! $userId)
{ {
$userId = $this->session->id; $userId = $this->session->user['id'];
} }
if (! $userModel->existsById($userId)) if (! $userModel->existsById($userId))
@ -740,7 +740,7 @@ class Aauth
$data['username'] = $user['username']; $data['username'] = $user['username'];
$data['email'] = $user['email']; $data['email'] = $user['email'];
$data['loggedIn'] = true; $data['loggedIn'] = true;
$this->session->set($data); $this->session->set('user', $data);
if ($remember) if ($remember)
{ {
@ -813,7 +813,7 @@ class Aauth
if ($user = $userModel->get()->getFirstRow()) if ($user = $userModel->get()->getFirstRow())
{ {
$this->session->set([ $this->session->set('user', [
'id' => $user->id, 'id' => $user->id,
'username' => $user->username, 'username' => $user->username,
'email' => $user->email, 'email' => $user->email,

5
tests/Aauth/Libraries/Aauth/ErrorsTest.php

@ -17,6 +17,7 @@ class ErrorsTest extends \CIUnitTestCase
{ {
parent::setUp(); parent::setUp();
$this->library = new Aauth(null, true);
$_COOKIE = []; $_COOKIE = [];
$_SESSION = []; $_SESSION = [];
} }
@ -55,7 +56,6 @@ class ErrorsTest extends \CIUnitTestCase
public function testErrors() public function testErrors()
{ {
$this->library = new Aauth(NULL, TRUE);
$this->assertCount(0, $this->library->getErrorsArray()); $this->assertCount(0, $this->library->getErrorsArray());
$this->library->error('test message 1'); $this->library->error('test message 1');
$this->assertEquals(['test message 1'], $this->library->getErrorsArray()); $this->assertEquals(['test message 1'], $this->library->getErrorsArray());
@ -63,7 +63,6 @@ class ErrorsTest extends \CIUnitTestCase
public function testErrorsArray() public function testErrorsArray()
{ {
$this->library = new Aauth(NULL, TRUE);
$this->assertCount(0, $this->library->getErrorsArray()); $this->assertCount(0, $this->library->getErrorsArray());
$this->library->error(['test message 1','test message 2']); $this->library->error(['test message 1','test message 2']);
$this->assertEquals(['test message 1','test message 2'], $this->library->getErrorsArray()); $this->assertEquals(['test message 1','test message 2'], $this->library->getErrorsArray());
@ -71,7 +70,6 @@ class ErrorsTest extends \CIUnitTestCase
public function testPrintErrorsReturn() public function testPrintErrorsReturn()
{ {
$this->library = new Aauth(NULL, TRUE);
$this->library->error('test message 1'); $this->library->error('test message 1');
$this->assertEquals('test message 1', $this->library->printErrors('<br />', true)); $this->assertEquals('test message 1', $this->library->printErrors('<br />', true));
$this->library->error('test message 2'); $this->library->error('test message 2');
@ -80,7 +78,6 @@ class ErrorsTest extends \CIUnitTestCase
public function testPrintErrorsEcho() public function testPrintErrorsEcho()
{ {
$this->library = new Aauth(NULL, TRUE);
$this->library->error('test message 1'); $this->library->error('test message 1');
$this->library->printErrors('<br />'); $this->library->printErrors('<br />');
$this->expectOutputString('test message 1'); $this->expectOutputString('test message 1');

76
tests/Aauth/Libraries/Aauth/UserTest.php

@ -1,5 +1,10 @@
<?php namespace Tests\Aauth\Libraries\Aauth; <?php namespace Tests\Aauth\Libraries\Aauth;
use Config\Logger;
use Config\Services;
use Tests\Support\Log\TestLogger;
use Tests\Support\Session\MockSession;
use CodeIgniter\Session\Handlers\FileHandler;
use CodeIgniter\Test\CIDatabaseTestCase; use CodeIgniter\Test\CIDatabaseTestCase;
use App\Libraries\Aauth; use App\Libraries\Aauth;
@ -19,18 +24,45 @@ class UserTest extends CIDatabaseTestCase
{ {
parent::setUp(); parent::setUp();
$this->library = new Aauth(null, true);
$_COOKIE = [];
$_SESSION = [];
} }
public function tearDown() 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() public function testUpdateUser()
{ {
$this->library = new Aauth(null, true);
$userPre = $this->library->getUser(2); $userPre = $this->library->getUser(2);
$this->library->updateUser(2, 'user1@example.com', 'password987654', 'user1'); $this->library->updateUser(2, 'user1@example.com', 'password987654', 'user1');
$user = $this->library->getUser(2); $user = $this->library->getUser(2);
@ -39,11 +71,12 @@ class UserTest extends CIDatabaseTestCase
$this->library->updateUser(2, null, null, 'user1'); $this->library->updateUser(2, null, null, 'user1');
$userAfter = $this->library->getUser(2); $userAfter = $this->library->getUser(2);
$this->assertEquals($user['username'], $userAfter['username']); $this->assertEquals($user['username'], $userAfter['username']);
$this->assertFalse($this->library->updateUser(2, 'asasdfasd'));
$this->assertFalse($this->library->updateUser(2));
} }
public function testDeleteUser() public function testDeleteUser()
{ {
$this->library = new Aauth(null, true);
$users = $this->library->listUsers(); $users = $this->library->listUsers();
$this->assertCount(2, $users); $this->assertCount(2, $users);
$this->library->deleteUser(2); $this->library->deleteUser(2);
@ -54,39 +87,51 @@ class UserTest extends CIDatabaseTestCase
public function testListUsers() public function testListUsers()
{ {
$this->library = new Aauth(null, true);
$users = $this->library->listUsers(); $users = $this->library->listUsers();
$this->assertCount(2, $users); $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() public function testGetUser()
{ {
$this->library = new Aauth(null, true);
$user = $this->library->getUser(1); $user = $this->library->getUser(1);
$this->assertEquals('1', $user['id']); $this->assertEquals('1', $user['id']);
$this->assertEquals('admin', $user['username']); $this->assertEquals('admin', $user['username']);
$this->assertEquals('admin@example.com', $user['email']); $this->assertEquals('admin@example.com', $user['email']);
} $this->assertFalse($this->library->getUser(99));
public function testGetUserUserVars() $userVar = $this->library->getUser(1, true);
{ $this->assertInternalType('array', $userVar['variables']);
$this->library = new Aauth(null, true);
$user = $this->library->getUser(1, true); $session = $this->getInstance();
$this->assertInternalType('array', $user['variables']); $this->library = new Aauth(NULL, $session);
$session->set('user', [
'id' => 1,
]);
$userIdNone = $this->library->getUser();
$this->assertEquals('admin', $userIdNone['username']);
} }
public function testGetUserId() public function testGetUserId()
{ {
$this->library = new Aauth(null, true);
$userIdEmail = $this->library->getUserId('admin@example.com'); $userIdEmail = $this->library->getUserId('admin@example.com');
$this->assertEquals('1', $userIdEmail); $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() public function testBanUser()
{ {
$this->library = new Aauth(null, true);
$this->assertFalse($this->library->isBanned(1)); $this->assertFalse($this->library->isBanned(1));
$this->library->banUser(1); $this->library->banUser(1);
$this->assertTrue($this->library->isBanned(1)); $this->assertTrue($this->library->isBanned(1));
@ -94,7 +139,6 @@ class UserTest extends CIDatabaseTestCase
public function testUnbanUser() public function testUnbanUser()
{ {
$this->library = new Aauth(null, true);
$this->library->banUser(1); $this->library->banUser(1);
$this->assertTrue($this->library->isBanned(1)); $this->assertTrue($this->library->isBanned(1));
$this->library->unbanUser(1); $this->library->unbanUser(1);

Loading…
Cancel
Save