69 changed files with 3071 additions and 454 deletions
@ -0,0 +1,242 @@ |
|||||||
|
<?php namespace Tests\Aauth\Libraries\Aauth; |
||||||
|
|
||||||
|
use Config\Aauth as AauthConfig; |
||||||
|
use Config\App; |
||||||
|
use Config\Logger; |
||||||
|
use Tests\Support\Log\TestLogger; |
||||||
|
use Tests\Support\HTTP\MockResponse; |
||||||
|
use Tests\Support\Session\MockSession; |
||||||
|
use CodeIgniter\Config\Services; |
||||||
|
use CodeIgniter\HTTP\IncomingRequest; |
||||||
|
use CodeIgniter\HTTP\URI; |
||||||
|
use CodeIgniter\HTTP\UserAgent; |
||||||
|
use CodeIgniter\Session\Handlers\FileHandler; |
||||||
|
use CodeIgniter\Test\CIDatabaseTestCase; |
||||||
|
use App\Libraries\Aauth; |
||||||
|
|
||||||
|
class AccessTest extends CIDatabaseTestCase |
||||||
|
{ |
||||||
|
protected $refresh = true; |
||||||
|
|
||||||
|
protected $basePath = TESTPATH . '../application' . 'Database/Migrations'; |
||||||
|
|
||||||
|
protected $namespace = 'App'; |
||||||
|
|
||||||
|
public function setUp() |
||||||
|
{ |
||||||
|
parent::setUp(); |
||||||
|
|
||||||
|
Services::injectMock('response', new MockResponse(new App())); |
||||||
|
$this->response = service('response'); |
||||||
|
$this->request = new IncomingRequest(new App(), new URI(), null, new UserAgent()); |
||||||
|
Services::injectMock('request', $this->request); |
||||||
|
|
||||||
|
$this->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; |
||||||
|
} |
||||||
|
|
||||||
|
//-------------------------------------------------------------------- |
||||||
|
|
||||||
|
/** |
||||||
|
* @runInSeparateProcess |
||||||
|
* @preserveGlobalState disabled |
||||||
|
*/ |
||||||
|
public function testIsLoggedIn() |
||||||
|
{ |
||||||
|
$session = $this->getInstance(); |
||||||
|
$this->library = new Aauth(null, $session); |
||||||
|
$session->set('user', [ |
||||||
|
'loggedIn' => true, |
||||||
|
]); |
||||||
|
$this->assertTrue($this->library->isLoggedIn()); |
||||||
|
$session->remove('user'); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @runInSeparateProcess |
||||||
|
* @preserveGlobalState disabled |
||||||
|
*/ |
||||||
|
public function testIsMember() |
||||||
|
{ |
||||||
|
$config = new AauthConfig(); |
||||||
|
$this->assertTrue($this->library->isMember($config->groupDefault, 1)); |
||||||
|
|
||||||
|
$session = $this->getInstance(); |
||||||
|
$this->library = new Aauth(null, $session); |
||||||
|
$session->set('user', [ |
||||||
|
'id' => 1, |
||||||
|
'loggedIn' => true, |
||||||
|
]); |
||||||
|
$this->assertTrue($this->library->isMember($config->groupDefault)); |
||||||
|
$session->remove('user'); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @runInSeparateProcess |
||||||
|
* @preserveGlobalState disabled |
||||||
|
*/ |
||||||
|
public function testIsAdmin() |
||||||
|
{ |
||||||
|
$this->assertTrue($this->library->isAdmin(1)); |
||||||
|
|
||||||
|
$session = $this->getInstance(); |
||||||
|
$this->library = new Aauth(null, $session); |
||||||
|
$session->set('user', [ |
||||||
|
'id' => 1, |
||||||
|
'loggedIn' => true, |
||||||
|
]); |
||||||
|
$this->assertTrue($this->library->isAdmin()); |
||||||
|
$session->remove('user'); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @runInSeparateProcess |
||||||
|
* @preserveGlobalState disabled |
||||||
|
*/ |
||||||
|
public function testIsAllowed() |
||||||
|
{ |
||||||
|
$config = new AauthConfig(); |
||||||
|
$this->hasInDatabase($config->dbTablePerms, [ |
||||||
|
'id' => 1, |
||||||
|
'name' => 'testPerm1', |
||||||
|
'definition' => 'Test Perm 1', |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertTrue($this->library->isAllowed('testPerm1', 1)); |
||||||
|
$this->assertFalse($this->library->isAllowed('testPerm1', 2)); |
||||||
|
|
||||||
|
$this->hasInDatabase($config->dbTablePermToGroup, [ |
||||||
|
'perm_id' => 1, |
||||||
|
'group_id' => 2, |
||||||
|
]); |
||||||
|
$this->assertTrue($this->library->isAllowed('testPerm1', 2)); |
||||||
|
|
||||||
|
$this->hasInDatabase($config->dbTablePermToUser, [ |
||||||
|
'perm_id' => 1, |
||||||
|
'user_id' => 2, |
||||||
|
]); |
||||||
|
$this->assertTrue($this->library->isAllowed('testPerm1', 2)); |
||||||
|
|
||||||
|
$session = $this->getInstance(); |
||||||
|
$this->library = new Aauth(null, $session); |
||||||
|
$session->set('user', [ |
||||||
|
'id' => 1, |
||||||
|
'loggedIn' => true, |
||||||
|
]); |
||||||
|
$this->assertTrue($this->library->isAllowed('testPerm1')); |
||||||
|
$session->remove('user'); |
||||||
|
|
||||||
|
$this->assertFalse($this->library->isAllowed('testPerm99', 2)); |
||||||
|
$this->assertFalse($this->library->isAllowed('testPerm1', 99)); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @runInSeparateProcess |
||||||
|
* @preserveGlobalState disabled |
||||||
|
*/ |
||||||
|
public function testIsGroupAllowed() |
||||||
|
{ |
||||||
|
$config = new AauthConfig(); |
||||||
|
$this->hasInDatabase($config->dbTablePerms, [ |
||||||
|
'id' => 1, |
||||||
|
'name' => 'testPerm1', |
||||||
|
'definition' => 'Test Perm 1', |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertTrue($this->library->isGroupAllowed('testPerm1', $config->groupAdmin)); |
||||||
|
|
||||||
|
$session = $this->getInstance(); |
||||||
|
$this->library = new Aauth(null, $session); |
||||||
|
|
||||||
|
$session->set('user', [ |
||||||
|
'id' => 2, |
||||||
|
'loggedIn' => true, |
||||||
|
]); |
||||||
|
$this->assertFalse($this->library->isGroupAllowed('testPerm1')); |
||||||
|
$session->remove('user'); |
||||||
|
|
||||||
|
$this->hasInDatabase($config->dbTablePermToGroup, [ |
||||||
|
'perm_id' => 1, |
||||||
|
'group_id' => 2, |
||||||
|
]); |
||||||
|
$this->assertTrue($this->library->isGroupAllowed('testPerm1', 2)); |
||||||
|
|
||||||
|
$session->set('user', [ |
||||||
|
'id' => 1, |
||||||
|
'loggedIn' => true, |
||||||
|
]); |
||||||
|
$this->assertTrue($this->library->isGroupAllowed('testPerm1')); |
||||||
|
$session->remove('user'); |
||||||
|
|
||||||
|
$session->set('user', [ |
||||||
|
'id' => 2, |
||||||
|
'loggedIn' => true, |
||||||
|
]); |
||||||
|
$this->assertTrue($this->library->isGroupAllowed('testPerm1')); |
||||||
|
$session->remove('user'); |
||||||
|
|
||||||
|
$this->assertFalse($this->library->isGroupAllowed('testPerm1')); |
||||||
|
$this->assertFalse($this->library->isGroupAllowed('testPerm1', 3)); |
||||||
|
$this->assertFalse($this->library->isGroupAllowed('testPerm99', 2)); |
||||||
|
$this->assertFalse($this->library->isGroupAllowed('testPerm1', 99)); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @runInSeparateProcess |
||||||
|
* @preserveGlobalState disabled |
||||||
|
*/ |
||||||
|
public function testIsGroupAllowedSubgroup() |
||||||
|
{ |
||||||
|
$config = new AauthConfig(); |
||||||
|
$this->hasInDatabase($config->dbTablePerms, [ |
||||||
|
'id' => 1, |
||||||
|
'name' => 'testPerm1', |
||||||
|
'definition' => 'Test Perm 1', |
||||||
|
]); |
||||||
|
$this->hasInDatabase($config->dbTableGroups, [ |
||||||
|
'id' => 4, |
||||||
|
'name' => 'testGroups1', |
||||||
|
'definition' => 'Test Group 1', |
||||||
|
]); |
||||||
|
|
||||||
|
$this->hasInDatabase($config->dbTableGroupToGroup, [ |
||||||
|
'group_id' => 2, |
||||||
|
'subgroup_id' => 4, |
||||||
|
]); |
||||||
|
$this->hasInDatabase($config->dbTablePermToGroup, [ |
||||||
|
'perm_id' => 1, |
||||||
|
'group_id' => 4, |
||||||
|
]); |
||||||
|
$this->assertTrue($this->library->isGroupAllowed('testPerm1', 2)); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,342 @@ |
|||||||
|
<?php namespace Tests\Aauth\Libraries\Aauth; |
||||||
|
|
||||||
|
use Config\Aauth as AauthConfig; |
||||||
|
use Config\App; |
||||||
|
use Config\Logger; |
||||||
|
use Tests\Support\Log\TestLogger; |
||||||
|
use Tests\Support\HTTP\MockResponse; |
||||||
|
use Tests\Support\Session\MockSession; |
||||||
|
use CodeIgniter\Config\Services; |
||||||
|
use CodeIgniter\HTTP\IncomingRequest; |
||||||
|
use CodeIgniter\HTTP\URI; |
||||||
|
use CodeIgniter\HTTP\UserAgent; |
||||||
|
use CodeIgniter\Session\Handlers\FileHandler; |
||||||
|
use CodeIgniter\Test\CIDatabaseTestCase; |
||||||
|
use App\Libraries\Aauth; |
||||||
|
|
||||||
|
class GroupTest extends CIDatabaseTestCase |
||||||
|
{ |
||||||
|
protected $refresh = true; |
||||||
|
|
||||||
|
protected $basePath = TESTPATH . '../application' . 'Database/Migrations'; |
||||||
|
|
||||||
|
protected $namespace = 'App'; |
||||||
|
|
||||||
|
public function setUp() |
||||||
|
{ |
||||||
|
parent::setUp(); |
||||||
|
|
||||||
|
Services::injectMock('response', new MockResponse(new App())); |
||||||
|
$this->response = service('response'); |
||||||
|
$this->request = new IncomingRequest(new App(), new URI(), null, new UserAgent()); |
||||||
|
Services::injectMock('request', $this->request); |
||||||
|
|
||||||
|
$this->config = new AauthConfig(); |
||||||
|
$this->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 testCreateGroup() |
||||||
|
{ |
||||||
|
$this->library->createGroup('testGroup1', 'Test Group 1'); |
||||||
|
$this->seeInDatabase($this->config->dbTableGroups, [ |
||||||
|
'name' => 'testGroup1', |
||||||
|
'definition' => 'Test Group 1', |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertFalse($this->library->createGroup('admin')); |
||||||
|
$this->assertEquals(lang('Aauth.existsAlreadyGroup'), $this->library->getErrorsArray()[0]); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->createGroup('')); |
||||||
|
$this->assertEquals(lang('Aauth.requiredGroupName'), $this->library->getErrorsArray()[0]); |
||||||
|
} |
||||||
|
|
||||||
|
public function testUpdateGroup() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTableGroups, [ |
||||||
|
'id' => 4, |
||||||
|
'name' => 'testGroup1', |
||||||
|
'definition' => 'Test Group 1', |
||||||
|
]); |
||||||
|
$this->library->updateGroup('testGroup1', 'testGroup1N', 'Test Group 1 New'); |
||||||
|
$this->seeInDatabase($this->config->dbTableGroups, [ |
||||||
|
'id' => 4, |
||||||
|
'name' => 'testGroup1N', |
||||||
|
'definition' => 'Test Group 1 New', |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertFalse($this->library->updateGroup($this->config->groupAdmin, $this->config->groupDefault)); |
||||||
|
$this->assertEquals(lang('Aauth.existsAlreadyGroup'), $this->library->getErrorsArray()[0]); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->updateGroup($this->config->groupAdmin)); |
||||||
|
$this->assertCount(0, $this->library->getErrorsArray()); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->updateGroup(99, '')); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundGroup'), $this->library->getErrorsArray()[0]); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->updateGroup('testGroup99 ', '')); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundGroup'), $this->library->getErrorsArray()[0]); |
||||||
|
} |
||||||
|
|
||||||
|
public function testDeleteGroup() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTableGroups, [ |
||||||
|
'id' => 4, |
||||||
|
'name' => 'testGroup1', |
||||||
|
'definition' => 'Test Group 1', |
||||||
|
]); |
||||||
|
$this->assertTrue($this->library->deleteGroup('testGroup1')); |
||||||
|
$this->dontSeeInDatabase($this->config->dbTableGroups, [ |
||||||
|
'id' => 4, |
||||||
|
'name' => 'testGroup1N', |
||||||
|
'definition' => 'Test Group 1 New', |
||||||
|
]); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->deleteGroup(99, '')); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundGroup'), $this->library->getErrorsArray()[0]); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->deleteGroup('testGroup99 ', '')); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundGroup'), $this->library->getErrorsArray()[0]); |
||||||
|
} |
||||||
|
|
||||||
|
public function testAddMember() |
||||||
|
{ |
||||||
|
$this->assertTrue($this->library->addMember(1, 2)); |
||||||
|
$this->seeInDatabase($this->config->dbTableGroupToUser, [ |
||||||
|
'group_id' => 1, |
||||||
|
'user_id' => 2, |
||||||
|
]); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertTrue($this->library->addMember(2, 2)); |
||||||
|
$this->assertEquals(lang('Aauth.alreadyMemberGroup'), $this->library->getInfosArray()[0]); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->addMember(99, 2)); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundGroup'), $this->library->getErrorsArray()[0]); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->addMember(2, 99)); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); |
||||||
|
} |
||||||
|
|
||||||
|
public function testRemoveMember() |
||||||
|
{ |
||||||
|
$this->assertTrue($this->library->removeMember(1, 1)); |
||||||
|
$this->dontSeeInDatabase($this->config->dbTableGroupToUser, [ |
||||||
|
'group_id' => 1, |
||||||
|
'user_id' => 1, |
||||||
|
]); |
||||||
|
} |
||||||
|
|
||||||
|
public function testAddSubgroup() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTableGroups, [ |
||||||
|
'id' => 4, |
||||||
|
'name' => 'testGroup1', |
||||||
|
'definition' => 'Test Group 1', |
||||||
|
]); |
||||||
|
$this->hasInDatabase($this->config->dbTableGroups, [ |
||||||
|
'id' => 5, |
||||||
|
'name' => 'testGroup2', |
||||||
|
'definition' => 'Test Group 2', |
||||||
|
]); |
||||||
|
$this->hasInDatabase($this->config->dbTableGroups, [ |
||||||
|
'id' => 6, |
||||||
|
'name' => 'testGroup3', |
||||||
|
'definition' => 'Test Group 3', |
||||||
|
]); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertTrue($this->library->addSubgroup('testGroup1', 'testGroup2')); |
||||||
|
$this->assertTrue($this->library->addSubgroup('testGroup1', 'testGroup3')); |
||||||
|
$this->assertFalse($this->library->addSubgroup('testGroup2', 'testGroup1')); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertTrue($this->library->addSubgroup(4, 5)); |
||||||
|
$this->assertEquals(lang('Aauth.alreadyMemberSubgroup'), $this->library->getInfosArray()[0]); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->addSubgroup(99, 1)); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundGroup'), $this->library->getErrorsArray()[0]); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->addSubgroup(1, 99)); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundSubgroup'), $this->library->getErrorsArray()[0]); |
||||||
|
} |
||||||
|
|
||||||
|
public function testRemoveSubgroup() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTableGroupToGroup, [ |
||||||
|
'group_id' => 1, |
||||||
|
'subgroup_id' => 2, |
||||||
|
]); |
||||||
|
$this->assertTrue($this->library->removeSubgroup(1, 2)); |
||||||
|
$this->dontSeeInDatabase($this->config->dbTableGroupToGroup, [ |
||||||
|
'group_id' => 1, |
||||||
|
'subgroup_id' => 2, |
||||||
|
]); |
||||||
|
} |
||||||
|
|
||||||
|
public function testRemoveMemberFromAll() |
||||||
|
{ |
||||||
|
$this->assertTrue($this->library->removeMemberFromAll(1)); |
||||||
|
$this->dontSeeInDatabase($this->config->dbTableGroupToUser, [ |
||||||
|
'group_id' => 1, |
||||||
|
'user_id' => 1, |
||||||
|
]); |
||||||
|
$this->dontSeeInDatabase($this->config->dbTableGroupToUser, [ |
||||||
|
'group_id' => 2, |
||||||
|
'user_id' => 1, |
||||||
|
]); |
||||||
|
} |
||||||
|
|
||||||
|
public function testListGroups() |
||||||
|
{ |
||||||
|
$groups = $this->library->listGroups(); |
||||||
|
$this->assertCount(3, $groups); |
||||||
|
$this->assertEquals($this->config->groupAdmin, $groups[0]['name']); |
||||||
|
$this->assertEquals($this->config->groupDefault, $groups[1]['name']); |
||||||
|
} |
||||||
|
|
||||||
|
public function testListGroupsPaginated() |
||||||
|
{ |
||||||
|
$groups = $this->library->listGroupsPaginated(); |
||||||
|
$this->assertTrue(isset($groups['pager'])); |
||||||
|
$this->assertCount(3, $groups['groups']); |
||||||
|
$this->assertEquals($this->config->groupAdmin, $groups['groups'][0]['name']); |
||||||
|
$this->assertEquals($this->config->groupDefault, $groups['groups'][1]['name']); |
||||||
|
|
||||||
|
$groupsOrderBy = $this->library->listGroupsPaginated(10, 'id DESC'); |
||||||
|
$this->assertEquals($this->config->groupPublic, $groupsOrderBy['groups'][0]['name']); |
||||||
|
$this->assertEquals($this->config->groupDefault, $groupsOrderBy['groups'][1]['name']); |
||||||
|
} |
||||||
|
|
||||||
|
public function testListUserGroups() |
||||||
|
{ |
||||||
|
$groups = $this->library->listUserGroups(1); |
||||||
|
$this->assertCount(2, $groups); |
||||||
|
$this->assertEquals($this->config->groupAdmin, $groups[0]['name']); |
||||||
|
$this->assertEquals($this->config->groupDefault, $groups[1]['name']); |
||||||
|
|
||||||
|
$this->assertFalse($this->library->listUserGroups(99)); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @runInSeparateProcess |
||||||
|
* @preserveGlobalState disabled |
||||||
|
*/ |
||||||
|
public function testListUserGroupsPaginated() |
||||||
|
{ |
||||||
|
$groups = $this->library->listUserGroupsPaginated(1); |
||||||
|
$this->assertTrue(isset($groups['pager'])); |
||||||
|
$this->assertCount(2, $groups['groups']); |
||||||
|
$this->assertEquals($this->config->groupAdmin, $groups['groups'][0]['name']); |
||||||
|
$this->assertEquals($this->config->groupDefault, $groups['groups'][1]['name']); |
||||||
|
|
||||||
|
$groupsOrderBy = $this->library->listUserGroupsPaginated(1, 10, 'id DESC'); |
||||||
|
$this->assertEquals($this->config->groupDefault, $groupsOrderBy['groups'][0]['name']); |
||||||
|
$this->assertEquals($this->config->groupAdmin, $groupsOrderBy['groups'][1]['name']); |
||||||
|
|
||||||
|
$this->assertFalse($this->library->listUserGroupsPaginated(99)); |
||||||
|
|
||||||
|
$session = $this->getInstance(); |
||||||
|
$this->library = new Aauth(null, $session); |
||||||
|
$session->set('user', [ |
||||||
|
'id' => 1, |
||||||
|
'loggedIn' => true, |
||||||
|
]); |
||||||
|
$groups = $this->library->listUserGroupsPaginated(); |
||||||
|
$this->assertCount(2, $groups['groups']); |
||||||
|
} |
||||||
|
|
||||||
|
public function testGetGroupName() |
||||||
|
{ |
||||||
|
$this->assertEquals($this->config->groupAdmin, $this->library->getGroupName(1)); |
||||||
|
$this->assertFalse($this->library->getGroupName(99)); |
||||||
|
} |
||||||
|
|
||||||
|
public function testGetGroupId() |
||||||
|
{ |
||||||
|
$this->assertEquals(1, $this->library->getGroupId($this->config->groupAdmin)); |
||||||
|
$this->assertEquals(1, $this->library->getGroupId(1)); |
||||||
|
$this->assertFalse($this->library->getGroupId('testGroup99')); |
||||||
|
} |
||||||
|
|
||||||
|
public function testGetGroup() |
||||||
|
{ |
||||||
|
$group = $this->library->getGroup($this->config->groupAdmin); |
||||||
|
$this->assertEquals(1, $group['id']); |
||||||
|
$group = $this->library->getGroup(1); |
||||||
|
$this->assertEquals($this->config->groupAdmin, $group['name']); |
||||||
|
$this->assertFalse($this->library->getGroup('testGroup99')); |
||||||
|
$this->assertFalse($this->library->getGroup(99)); |
||||||
|
} |
||||||
|
|
||||||
|
public function testGetSubgroups() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTableGroups, [ |
||||||
|
'id' => 4, |
||||||
|
'name' => 'testGroup1', |
||||||
|
'definition' => 'Test Group 1', |
||||||
|
]); |
||||||
|
$this->hasInDatabase($this->config->dbTableGroups, [ |
||||||
|
'id' => 5, |
||||||
|
'name' => 'testGroup2', |
||||||
|
'definition' => 'Test Group 2', |
||||||
|
]); |
||||||
|
$this->hasInDatabase($this->config->dbTableGroups, [ |
||||||
|
'id' => 6, |
||||||
|
'name' => 'testGroup3', |
||||||
|
'definition' => 'Test Group 3', |
||||||
|
]); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertTrue($this->library->addSubgroup('testGroup1', 'testGroup2')); |
||||||
|
$this->assertTrue($this->library->addSubgroup('testGroup1', 'testGroup3')); |
||||||
|
|
||||||
|
$subgroups = $this->library->getSubgroups(4); |
||||||
|
$this->assertCount(2, $subgroups); |
||||||
|
$this->assertEquals([['subgroup_id' => '5'], ['subgroup_id' => '6']], $subgroups); |
||||||
|
$this->assertFalse($this->library->getSubgroups('testGroup99')); |
||||||
|
$this->assertFalse($this->library->getSubgroups(99)); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,451 @@ |
|||||||
|
<?php namespace Tests\Aauth\Libraries\Aauth; |
||||||
|
|
||||||
|
use Config\Aauth as AauthConfig; |
||||||
|
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 App\Libraries\Aauth; |
||||||
|
use App\Models\Aauth\UserVariableModel; |
||||||
|
|
||||||
|
class PermTest extends CIDatabaseTestCase |
||||||
|
{ |
||||||
|
protected $refresh = true; |
||||||
|
|
||||||
|
protected $basePath = TESTPATH . '../application' . 'Database/Migrations'; |
||||||
|
|
||||||
|
protected $namespace = 'App'; |
||||||
|
|
||||||
|
public function setUp() |
||||||
|
{ |
||||||
|
parent::setUp(); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->config = new AauthConfig(); |
||||||
|
$_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 testCreatePerm() |
||||||
|
{ |
||||||
|
$this->library->createPerm('testPerm1', 'Test Perm 1'); |
||||||
|
$this->seeInDatabase($this->config->dbTablePerms, [ |
||||||
|
'name' => 'testPerm1', |
||||||
|
'definition' => 'Test Perm 1', |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertFalse($this->library->createPerm('testPerm1')); |
||||||
|
$this->assertEquals(lang('Aauth.existsAlreadyPerm'), $this->library->getErrorsArray()[0]); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->createPerm('')); |
||||||
|
$this->assertEquals(lang('Aauth.requiredPermName'), $this->library->getErrorsArray()[0]); |
||||||
|
} |
||||||
|
|
||||||
|
public function testUpdatePerm() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'id' => 1, |
||||||
|
'name' => 'testPerm1', |
||||||
|
'definition' => 'Test Perm 1', |
||||||
|
]); |
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'id' => 2, |
||||||
|
'name' => 'testPerm2', |
||||||
|
'definition' => 'Test Perm 2', |
||||||
|
]); |
||||||
|
$this->library->updatePerm('testPerm1', 'testPerm1N', 'Test Perm 1 New'); |
||||||
|
$this->seeInDatabase($this->config->dbTablePerms, [ |
||||||
|
'id' => 1, |
||||||
|
'name' => 'testPerm1N', |
||||||
|
'definition' => 'Test Perm 1 New', |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertFalse($this->library->updatePerm('testPerm1N', 'testPerm2')); |
||||||
|
$this->assertEquals(lang('Aauth.existsAlreadyPerm'), $this->library->getErrorsArray()[0]); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->updatePerm('testPerm1')); |
||||||
|
$this->assertCount(0, $this->library->getErrorsArray()); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->updatePerm(99, '')); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundPerm'), $this->library->getErrorsArray()[0]); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->updatePerm('testPerm99', '')); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundPerm'), $this->library->getErrorsArray()[0]); |
||||||
|
} |
||||||
|
|
||||||
|
public function testDeletePerm() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'name' => 'testPerm1', |
||||||
|
'definition' => 'Test Perm 1', |
||||||
|
]); |
||||||
|
$this->assertTrue($this->library->deletePerm('testPerm1')); |
||||||
|
$this->dontSeeInDatabase($this->config->dbTablePerms, [ |
||||||
|
'name' => 'testPerm1', |
||||||
|
'definition' => 'Test Perm 1', |
||||||
|
'deleted' => 0, |
||||||
|
]); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->deletePerm(99)); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundPerm'), $this->library->getErrorsArray()[0]); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->deletePerm('testPerm99')); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundPerm'), $this->library->getErrorsArray()[0]); |
||||||
|
} |
||||||
|
|
||||||
|
public function testListPerms() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'id' => 1, |
||||||
|
'name' => 'testPerm1', |
||||||
|
'definition' => 'Test Perm 1', |
||||||
|
]); |
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'id' => 2, |
||||||
|
'name' => 'testPerm2', |
||||||
|
'definition' => 'Test Perm 2', |
||||||
|
]); |
||||||
|
$perms = $this->library->listPerms(); |
||||||
|
$this->assertCount(2, $perms); |
||||||
|
$this->assertEquals('testPerm1', $perms[0]['name']); |
||||||
|
$this->assertEquals('testPerm2', $perms[1]['name']); |
||||||
|
} |
||||||
|
|
||||||
|
public function testListPermsPaginated() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'id' => 1, |
||||||
|
'name' => 'testPerm1', |
||||||
|
'definition' => 'Test Perm 1', |
||||||
|
]); |
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'id' => 2, |
||||||
|
'name' => 'testPerm2', |
||||||
|
'definition' => 'Test Perm 2', |
||||||
|
]); |
||||||
|
|
||||||
|
$perms = $this->library->listPermsPaginated(); |
||||||
|
$this->assertTrue(isset($perms['pager'])); |
||||||
|
$this->assertCount(2, $perms['perms']); |
||||||
|
$this->assertEquals('testPerm1', $perms['perms'][0]['name']); |
||||||
|
$this->assertEquals('testPerm2', $perms['perms'][1]['name']); |
||||||
|
|
||||||
|
$permsOrderBy = $this->library->listPermsPaginated(10, 'id DESC'); |
||||||
|
$this->assertEquals('testPerm2', $permsOrderBy['perms'][0]['name']); |
||||||
|
$this->assertEquals('testPerm1', $permsOrderBy['perms'][1]['name']); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @runInSeparateProcess |
||||||
|
* @preserveGlobalState disabled |
||||||
|
*/ |
||||||
|
public function testListUserPerms() |
||||||
|
{ |
||||||
|
$perms = $this->library->listUserPerms(1); |
||||||
|
$this->assertCount(0, $perms); |
||||||
|
|
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'id' => 1, |
||||||
|
'name' => 'testPerm1', |
||||||
|
'definition' => 'Test Perm 1', |
||||||
|
]); |
||||||
|
$this->hasInDatabase($this->config->dbTablePermToUser, [ |
||||||
|
'perm_id' => 1, |
||||||
|
'user_id' => 1, |
||||||
|
'state' => 1, |
||||||
|
]); |
||||||
|
|
||||||
|
$perms = $this->library->listUserPerms(1); |
||||||
|
$this->assertCount(1, $perms); |
||||||
|
$this->assertEquals('testPerm1', $perms[0]['name']); |
||||||
|
|
||||||
|
$this->assertFalse($this->library->listUserPerms(99)); |
||||||
|
|
||||||
|
$session = $this->getInstance(); |
||||||
|
$this->library = new Aauth(null, $session); |
||||||
|
|
||||||
|
$session->set('user', [ |
||||||
|
'id' => 1, |
||||||
|
'loggedIn' => true, |
||||||
|
]); |
||||||
|
|
||||||
|
$perms = $this->library->listUserPerms(); |
||||||
|
$this->assertCount(1, $perms); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @runInSeparateProcess |
||||||
|
* @preserveGlobalState disabled |
||||||
|
*/ |
||||||
|
public function testListUserPermsPaginated() |
||||||
|
{ |
||||||
|
$perms = $this->library->listUserPermsPaginated(1); |
||||||
|
$this->assertCount(0, $perms['perms']); |
||||||
|
|
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'id' => 1, |
||||||
|
'name' => 'testPerm1', |
||||||
|
'definition' => 'Test Perm 1', |
||||||
|
]); |
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'id' => 2, |
||||||
|
'name' => 'testPerm2', |
||||||
|
'definition' => 'Test Perm 2', |
||||||
|
]); |
||||||
|
$this->hasInDatabase($this->config->dbTablePermToUser, [ |
||||||
|
'perm_id' => 1, |
||||||
|
'user_id' => 1, |
||||||
|
'state' => 1, |
||||||
|
]); |
||||||
|
$this->hasInDatabase($this->config->dbTablePermToUser, [ |
||||||
|
'perm_id' => 2, |
||||||
|
'user_id' => 1, |
||||||
|
'state' => 1, |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertFalse($this->library->listUserPermsPaginated(99)); |
||||||
|
|
||||||
|
$perms = $this->library->listUserPermsPaginated(1); |
||||||
|
$this->assertTrue(isset($perms['pager'])); |
||||||
|
$this->assertCount(2, $perms['perms']); |
||||||
|
$this->assertEquals('testPerm1', $perms['perms'][0]['name']); |
||||||
|
$this->assertEquals('testPerm2', $perms['perms'][1]['name']); |
||||||
|
|
||||||
|
$permsOrderBy = $this->library->listUserPermsPaginated(1, 10, 'id DESC'); |
||||||
|
$this->assertEquals('testPerm2', $permsOrderBy['perms'][0]['name']); |
||||||
|
$this->assertEquals('testPerm1', $permsOrderBy['perms'][1]['name']); |
||||||
|
|
||||||
|
$session = $this->getInstance(); |
||||||
|
$this->library = new Aauth(null, $session); |
||||||
|
$session->set('user', [ |
||||||
|
'id' => 1, |
||||||
|
'loggedIn' => true, |
||||||
|
]); |
||||||
|
$perms = $this->library->listUserPermsPaginated(); |
||||||
|
$this->assertCount(2, $perms['perms']); |
||||||
|
} |
||||||
|
|
||||||
|
public function testGetPermId() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'name' => 'testPerm1', |
||||||
|
'definition' => 'Test Perm 1', |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertEquals(1, $this->library->getPermId('testPerm1')); |
||||||
|
$this->assertEquals(1, $this->library->getPermId(1)); |
||||||
|
$this->assertFalse($this->library->getPermId('testPerm99')); |
||||||
|
} |
||||||
|
|
||||||
|
public function testGetPerm() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'name' => 'testPerm1', |
||||||
|
'definition' => 'Test Perm 1', |
||||||
|
]); |
||||||
|
|
||||||
|
$perm = $this->library->getPerm('testPerm1'); |
||||||
|
$this->assertEquals(1, $perm['id']); |
||||||
|
$perm = $this->library->getPerm(1); |
||||||
|
$this->assertEquals('testPerm1', $perm['name']); |
||||||
|
$this->assertFalse($this->library->getPerm('testPerm99')); |
||||||
|
$this->assertFalse($this->library->getPerm(99)); |
||||||
|
} |
||||||
|
|
||||||
|
public function testAllowUser() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'id' => 1, |
||||||
|
'name' => 'testPerm1', |
||||||
|
'definition' => 'Test Perm 1', |
||||||
|
]); |
||||||
|
$this->assertTrue($this->library->allowUser(1, 1)); |
||||||
|
$this->seeInDatabase($this->config->dbTablePermToUser, [ |
||||||
|
'perm_id' => 1, |
||||||
|
'user_id' => 1, |
||||||
|
'state' => 1, |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertTrue($this->library->allowUser(1, 1)); |
||||||
|
$this->assertFalse($this->library->allowUser(99, 1)); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundPerm'), $this->library->getErrorsArray()[0]); |
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->allowUser(1, 99)); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); |
||||||
|
} |
||||||
|
|
||||||
|
public function testDenyUser() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'id' => 1, |
||||||
|
'name' => 'testPerm1', |
||||||
|
'definition' => 'Test Perm 1', |
||||||
|
]); |
||||||
|
$this->assertTrue($this->library->denyUser(1, 1)); |
||||||
|
$this->seeInDatabase($this->config->dbTablePermToUser, [ |
||||||
|
'perm_id' => 1, |
||||||
|
'user_id' => 1, |
||||||
|
'state' => 0, |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertTrue($this->library->denyUser(1, 1)); |
||||||
|
$this->assertFalse($this->library->denyUser(99, 1)); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundPerm'), $this->library->getErrorsArray()[0]); |
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->denyUser(1, 99)); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); |
||||||
|
} |
||||||
|
|
||||||
|
public function testAllowGroup() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'id' => 1, |
||||||
|
'name' => 'testPerm1', |
||||||
|
'definition' => 'Test Perm 1', |
||||||
|
]); |
||||||
|
$this->assertTrue($this->library->allowGroup(1, 1)); |
||||||
|
$this->seeInDatabase($this->config->dbTablePermToGroup, [ |
||||||
|
'perm_id' => 1, |
||||||
|
'group_id' => 1, |
||||||
|
'state' => 1, |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertTrue($this->library->allowGroup(1, 1)); |
||||||
|
$this->assertFalse($this->library->allowGroup(99, 1)); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundPerm'), $this->library->getErrorsArray()[0]); |
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->allowGroup(1, 99)); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundGroup'), $this->library->getErrorsArray()[0]); |
||||||
|
} |
||||||
|
|
||||||
|
public function testDenyGroup() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'id' => 1, |
||||||
|
'name' => 'testPerm1', |
||||||
|
'definition' => 'Test Perm 1', |
||||||
|
]); |
||||||
|
$this->assertTrue($this->library->denyGroup(1, 1)); |
||||||
|
$this->seeInDatabase($this->config->dbTablePermToGroup, [ |
||||||
|
'perm_id' => 1, |
||||||
|
'group_id' => 1, |
||||||
|
'state' => 0, |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertTrue($this->library->denyGroup(1, 1)); |
||||||
|
$this->assertFalse($this->library->denyGroup(99, 1)); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundPerm'), $this->library->getErrorsArray()[0]); |
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->assertFalse($this->library->denyGroup(1, 99)); |
||||||
|
$this->assertEquals(lang('Aauth.notFoundGroup'), $this->library->getErrorsArray()[0]); |
||||||
|
} |
||||||
|
|
||||||
|
public function testListGroupPerms() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'id' => 1, |
||||||
|
'name' => 'testPerm1', |
||||||
|
'definition' => 'Test Perm 1', |
||||||
|
]); |
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'id' => 2, |
||||||
|
'name' => 'testPerm2', |
||||||
|
'definition' => 'Test Perm 2', |
||||||
|
]); |
||||||
|
$this->hasInDatabase($this->config->dbTablePermToGroup, [ |
||||||
|
'perm_id' => 1, |
||||||
|
'group_id' => 2, |
||||||
|
'state' => 0, |
||||||
|
]); |
||||||
|
$this->hasInDatabase($this->config->dbTablePermToGroup, [ |
||||||
|
'perm_id' => 2, |
||||||
|
'group_id' => 2, |
||||||
|
'state' => 1, |
||||||
|
]); |
||||||
|
|
||||||
|
$groupPerms = $this->library->listGroupPerms($this->config->groupDefault); |
||||||
|
|
||||||
|
$this->assertCount(2, $groupPerms); |
||||||
|
$this->assertEquals('testPerm1', $groupPerms[0]['name']); |
||||||
|
$this->assertEquals('0', $groupPerms[0]['state']); |
||||||
|
$this->assertEquals('testPerm2', $groupPerms[1]['name']); |
||||||
|
$this->assertEquals('1', $groupPerms[1]['state']); |
||||||
|
|
||||||
|
$this->assertFalse($this->library->listGroupPerms(99)); |
||||||
|
} |
||||||
|
|
||||||
|
public function testListGroupPermsPaginated() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'id' => 1, |
||||||
|
'name' => 'testPerm1', |
||||||
|
'definition' => 'Test Perm 1', |
||||||
|
]); |
||||||
|
$this->hasInDatabase($this->config->dbTablePerms, [ |
||||||
|
'id' => 2, |
||||||
|
'name' => 'testPerm2', |
||||||
|
'definition' => 'Test Perm 2', |
||||||
|
]); |
||||||
|
$this->hasInDatabase($this->config->dbTablePermToGroup, [ |
||||||
|
'perm_id' => 1, |
||||||
|
'group_id' => 2, |
||||||
|
'state' => 0, |
||||||
|
]); |
||||||
|
$this->hasInDatabase($this->config->dbTablePermToGroup, [ |
||||||
|
'perm_id' => 2, |
||||||
|
'group_id' => 2, |
||||||
|
'state' => 1, |
||||||
|
]); |
||||||
|
|
||||||
|
$groupPerms = $this->library->listGroupPermsPaginated(2); |
||||||
|
$this->assertTrue(isset($groupPerms['pager'])); |
||||||
|
$this->assertCount(2, $groupPerms['perms']); |
||||||
|
$this->assertEquals('testPerm1', $groupPerms['perms'][0]['name']); |
||||||
|
$this->assertEquals('0', $groupPerms['perms'][0]['state']); |
||||||
|
$this->assertEquals('testPerm2', $groupPerms['perms'][1]['name']); |
||||||
|
$this->assertEquals('1', $groupPerms['perms'][1]['state']); |
||||||
|
|
||||||
|
$groupPermsOrderBy = $this->library->listGroupPermsPaginated(2, 10, 'id DESC'); |
||||||
|
$this->assertEquals('testPerm2', $groupPermsOrderBy['perms'][0]['name']); |
||||||
|
$this->assertEquals('testPerm1', $groupPermsOrderBy['perms'][1]['name']); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,187 @@ |
|||||||
|
<?php namespace Tests\Aauth\Libraries\Aauth; |
||||||
|
|
||||||
|
use Config\Aauth as AauthConfig; |
||||||
|
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 App\Libraries\Aauth; |
||||||
|
use App\Models\Aauth\UserVariableModel; |
||||||
|
|
||||||
|
/** |
||||||
|
* @runTestsInSeparateProcesses |
||||||
|
* @preserveGlobalState disabled |
||||||
|
*/ |
||||||
|
class UserVariablesTest extends CIDatabaseTestCase |
||||||
|
{ |
||||||
|
protected $refresh = true; |
||||||
|
|
||||||
|
protected $basePath = TESTPATH . '../application' . 'Database/Migrations'; |
||||||
|
|
||||||
|
protected $namespace = 'App'; |
||||||
|
|
||||||
|
public function setUp() |
||||||
|
{ |
||||||
|
parent::setUp(); |
||||||
|
|
||||||
|
$this->library = new Aauth(null, true); |
||||||
|
$this->config = new AauthConfig(); |
||||||
|
$_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 testSetUserVar() |
||||||
|
{ |
||||||
|
$this->assertTrue($this->library->setUserVar('test_var', 'test', 1)); |
||||||
|
$this->seeInDatabase($this->config->dbTableUserVariables, [ |
||||||
|
'user_id' => 1, |
||||||
|
'data_key' => 'test_var', |
||||||
|
'data_value' => 'test', |
||||||
|
]); |
||||||
|
|
||||||
|
$session = $this->getInstance(); |
||||||
|
$this->library = new Aauth(null, $session); |
||||||
|
$session->set('user', [ |
||||||
|
'id' => 1, |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertTrue($this->library->setUserVar('test_var', 'test2')); |
||||||
|
$this->seeInDatabase($this->config->dbTableUserVariables, [ |
||||||
|
'user_id' => 1, |
||||||
|
'data_key' => 'test_var', |
||||||
|
'data_value' => 'test2', |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertFalse($this->library->setUserVar('test_var', 'test', 99)); |
||||||
|
} |
||||||
|
|
||||||
|
public function testUnsetUserVar() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTableUserVariables, [ |
||||||
|
'user_id' => 1, |
||||||
|
'data_key' => 'test_var', |
||||||
|
'data_value' => 'test', |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertTrue($this->library->unsetUserVar('test_var', 1)); |
||||||
|
|
||||||
|
$session = $this->getInstance(); |
||||||
|
$this->library = new Aauth(null, $session); |
||||||
|
$session->set('user', [ |
||||||
|
'id' => 1, |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertTrue($this->library->unsetUserVar('test_var')); |
||||||
|
|
||||||
|
$this->assertFalse($this->library->unsetUserVar('test_var', 99)); |
||||||
|
} |
||||||
|
|
||||||
|
public function testGetUserVar() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTableUserVariables, [ |
||||||
|
'user_id' => 1, |
||||||
|
'data_key' => 'test_var', |
||||||
|
'data_value' => 'test', |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertEquals('test', $this->library->getUserVar('test_var', 1)); |
||||||
|
|
||||||
|
$session = $this->getInstance(); |
||||||
|
$this->library = new Aauth(null, $session); |
||||||
|
$session->set('user', [ |
||||||
|
'id' => 1, |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertEquals('test', $this->library->getUserVar('test_var')); |
||||||
|
|
||||||
|
$this->assertFalse($this->library->getUserVar('test_var_99', 1)); |
||||||
|
|
||||||
|
$this->assertFalse($this->library->getUserVar('test_var', 99)); |
||||||
|
} |
||||||
|
|
||||||
|
public function testGetUserVars() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTableUserVariables, [ |
||||||
|
'user_id' => 1, |
||||||
|
'data_key' => 'test_var', |
||||||
|
'data_value' => 'test', |
||||||
|
]); |
||||||
|
$this->hasInDatabase($this->config->dbTableUserVariables, [ |
||||||
|
'user_id' => 1, |
||||||
|
'data_key' => 'test_var2', |
||||||
|
'data_value' => 'test2', |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertCount(2, $this->library->getUserVars(1)); |
||||||
|
|
||||||
|
$session = $this->getInstance(); |
||||||
|
$this->library = new Aauth(null, $session); |
||||||
|
$session->set('user', [ |
||||||
|
'id' => 1, |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertCount(2, $this->library->getUserVars()); |
||||||
|
|
||||||
|
$this->assertFalse($this->library->getUserVars(99)); |
||||||
|
} |
||||||
|
|
||||||
|
public function testListUserVarKeys() |
||||||
|
{ |
||||||
|
$this->hasInDatabase($this->config->dbTableUserVariables, [ |
||||||
|
'user_id' => 1, |
||||||
|
'data_key' => 'test_var', |
||||||
|
'data_value' => 'test', |
||||||
|
]); |
||||||
|
$this->hasInDatabase($this->config->dbTableUserVariables, [ |
||||||
|
'user_id' => 1, |
||||||
|
'data_key' => 'test_var2', |
||||||
|
'data_value' => 'test2', |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertCount(2, $this->library->listUserVarKeys(1)); |
||||||
|
$this->assertEquals([['key' => 'test_var'], ['key' => 'test_var2']], $this->library->listUserVarKeys(1)); |
||||||
|
|
||||||
|
$session = $this->getInstance(); |
||||||
|
$this->library = new Aauth(null, $session); |
||||||
|
$session->set('user', [ |
||||||
|
'id' => 1, |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertCount(2, $this->library->listUserVarKeys()); |
||||||
|
|
||||||
|
$this->assertFalse($this->library->listUserVarKeys(99)); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue