Browse Source

added GroupVar functions & tests

v3-dev
REJack 6 years ago
parent
commit
c3786bd365
No known key found for this signature in database
GPG Key ID: 4A44B48700429F46
  1. 125
      app/Libraries/Aauth.php
  2. 107
      tests/Aauth/Libraries/Aauth/GroupVariablesTest.php

125
app/Libraries/Aauth.php

@ -27,6 +27,7 @@ use \App\Models\Aauth\PermToUserModel;
use \App\Models\Aauth\LoginAttemptModel; use \App\Models\Aauth\LoginAttemptModel;
use \App\Models\Aauth\LoginTokenModel; use \App\Models\Aauth\LoginTokenModel;
use \App\Models\Aauth\UserVariableModel; use \App\Models\Aauth\UserVariableModel;
use \App\Models\Aauth\GroupVariableModel;
/** /**
* Aauth Library * Aauth Library
@ -1146,7 +1147,7 @@ class Aauth
$userModel = new UserModel(); $userModel = new UserModel();
if (! @$userModel->existsById($userId)) if (! $userModel->existsById($userId))
{ {
return false; return false;
} }
@ -1736,6 +1737,128 @@ class Aauth
]; ];
} }
/**
* Set Group Variable as key value
*
* if variable not set before, it will be set
* if set, overwrites the value
*
* @param string $key
* @param string $value
* @param integer $groupId Group id
*
* @return boolean
*/
public function setGroupVar(string $key, string $value, int $groupId)
{
$groupModel = new GroupModel();
if (! $groupModel->existsById($groupId))
{
return false;
}
$groupVariableModel = new GroupVariableModel();
return $groupVariableModel->save($groupId, $key, $value);
}
/**
* Unset Group Variable as key value
*
* @param string $key
* @param integer $groupId Group id
*
* @return boolean
*/
public function unsetGroupVar(string $key, int $groupId)
{
$groupModel = new GroupModel();
if (! $groupModel->existsById($groupId))
{
return false;
}
$groupVariableModel = new GroupVariableModel();
return $groupVariableModel->delete($groupId, $key);
}
/**
* Get Group Variable by key
*
* @param string $key Variable Key
* @param integer $groupId Group id
*
* @return boolean|string false if var is not set, the value of var if set
*/
public function getGroupVar(string $key, int $groupId)
{
$groupModel = new GroupModel();
if (! $groupModel->existsById($groupId))
{
return false;
}
$groupVariableModel = new GroupVariableModel();
if (! $variable = $groupVariableModel->find($groupId, $key))
{
return false;
}
return $variable;
}
/**
* Get Group Variables by group id
*
* Return array with all group keys & variables
*
* @param integer $groupId Group id
*
* @return array
*/
public function getGroupVars(int $groupId = null)
{
$groupModel = new GroupModel();
if (! $groupModel->existsById($groupId))
{
return false;
}
$groupVariableModel = new GroupVariableModel();
return $groupVariableModel->findAll($groupId);
}
/**
* List Group Variable Keys by GroupId
*
* Return array of variable keys or false
*
* @param integer $groupId Group id
*
* @return boolean|array
*/
public function listGroupVarKeys(int $groupId = null)
{
$groupModel = new GroupModel();
if (! $groupModel->existsById($groupId))
{
return false;
}
$groupVariableModel = new GroupVariableModel();
$groupVariableModel->select('data_key as key');
return $groupVariableModel->findAll($groupId);
}
//-------------------------------------------------------------------- //--------------------------------------------------------------------
// Perm Functions // Perm Functions
//-------------------------------------------------------------------- //--------------------------------------------------------------------

107
tests/Aauth/Libraries/Aauth/GroupVariablesTest.php

@ -0,0 +1,107 @@
<?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\GroupVariableModel;
class GroupVariablesTest 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();
}
//--------------------------------------------------------------------
public function testSetGroupVar()
{
$this->assertTrue($this->library->setGroupVar('test_var', 'test', 1));
$this->seeInDatabase($this->config->dbTableGroupVariables, [
'group_id' => 1,
'data_key' => 'test_var',
'data_value' => 'test',
]);
$this->assertFalse($this->library->setGroupVar('test_var', 'test', 99));
}
public function testUnsetGroupVar()
{
$this->hasInDatabase($this->config->dbTableGroupVariables, [
'group_id' => 1,
'data_key' => 'test_var',
'data_value' => 'test',
]);
$this->assertTrue($this->library->unsetGroupVar('test_var', 1));
$this->assertFalse($this->library->unsetGroupVar('test_var', 99));
}
public function testGetGroupVar()
{
$this->hasInDatabase($this->config->dbTableGroupVariables, [
'group_id' => 1,
'data_key' => 'test_var',
'data_value' => 'test',
]);
$this->assertEquals('test', $this->library->getGroupVar('test_var', 1));
$this->assertFalse($this->library->getGroupVar('test_var_99', 1));
$this->assertFalse($this->library->getGroupVar('test_var', 99));
}
public function testGetGroupVars()
{
$this->hasInDatabase($this->config->dbTableGroupVariables, [
'group_id' => 1,
'data_key' => 'test_var',
'data_value' => 'test',
]);
$this->hasInDatabase($this->config->dbTableGroupVariables, [
'group_id' => 1,
'data_key' => 'test_var2',
'data_value' => 'test2',
]);
$this->assertCount(2, $this->library->getGroupVars(1));
$this->assertFalse($this->library->getGroupVars(99));
}
public function testListGroupVarKeys()
{
$this->hasInDatabase($this->config->dbTableGroupVariables, [
'group_id' => 1,
'data_key' => 'test_var',
'data_value' => 'test',
]);
$this->hasInDatabase($this->config->dbTableGroupVariables, [
'group_id' => 1,
'data_key' => 'test_var2',
'data_value' => 'test2',
]);
$this->assertCount(2, $this->library->listGroupVarKeys(1));
$this->assertEquals([['key' => 'test_var'], ['key' => 'test_var2']], $this->library->listGroupVarKeys(1));
$this->assertFalse($this->library->listGroupVarKeys(99));
}
}
Loading…
Cancel
Save