Skip to content

Commit efb8445

Browse files
author
igor-chepurnoi
committed
add tests for AssignmentModel
1 parent f576df1 commit efb8445

File tree

3 files changed

+220
-1
lines changed

3 files changed

+220
-1
lines changed

tests/TestCase.php

+26-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected function tearDown()
2929
* @param array $config The application configuration, if needed
3030
* @param string $appClass name of the application class to create
3131
*/
32-
protected function mockApplication($config = [], $appClass = '\yii\console\Application')
32+
protected function mockApplication($config = [], $appClass = '\yii\web\Application')
3333
{
3434
new $appClass(ArrayHelper::merge([
3535
'id' => 'testapp',
@@ -48,6 +48,13 @@ protected function mockApplication($config = [], $appClass = '\yii\console\Appli
4848
'assignmentTable' => 'AuthAssignment',
4949
'ruleTable' => 'AuthRule',
5050
],
51+
'user' => [
52+
'identityClass' => 'yii2mod\rbac\tests\data\User',
53+
],
54+
'request' => [
55+
'hostInfo' => 'http://domain.com',
56+
'scriptUrl' => 'index.php'
57+
],
5158
],
5259
], $config));
5360
}
@@ -74,6 +81,7 @@ protected function destroyApplication()
7481
protected function setupTestDbData()
7582
{
7683
$db = Yii::$app->getDb();
84+
7785
// Structure :
7886

7987
$db->createCommand()->createTable('AuthRule', [
@@ -110,5 +118,22 @@ protected function setupTestDbData()
110118
'PRIMARY KEY (item_name, user_id)',
111119
'FOREIGN KEY (item_name) REFERENCES ' . '{{%AuthItem}}' . ' (name) ON DELETE CASCADE ON UPDATE CASCADE',
112120
])->execute();
121+
122+
$db->createCommand()->createTable('User', [
123+
'id' => 'pk',
124+
'username' => 'string not null unique',
125+
'authKey' => 'string(32) not null',
126+
'passwordHash' => 'string not null',
127+
'email' => 'string not null unique',
128+
])->execute();
129+
130+
// Data :
131+
132+
$db->createCommand()->insert('User', [
133+
'username' => 'demo',
134+
'authKey' => Yii::$app->getSecurity()->generateRandomString(),
135+
'passwordHash' => Yii::$app->getSecurity()->generatePasswordHash('password'),
136+
'email' => '[email protected]'
137+
])->execute();
113138
}
114139
}

tests/data/User.php

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace yii2mod\rbac\tests\data;
4+
5+
use yii\db\ActiveRecord;
6+
use yii\web\IdentityInterface;
7+
8+
/**
9+
* @property integer $id
10+
* @property string $username
11+
* @property string $email
12+
*/
13+
class User extends ActiveRecord implements IdentityInterface
14+
{
15+
/**
16+
* @inheritdoc
17+
*/
18+
public static function tableName()
19+
{
20+
return 'User';
21+
}
22+
23+
/**
24+
* @inheritdoc
25+
*/
26+
public function rules()
27+
{
28+
return [
29+
[['username', 'email'], 'required'],
30+
];
31+
}
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
public static function findIdentity($id)
37+
{
38+
return static::findOne($id);
39+
}
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
public static function findIdentityByAccessToken($token, $type = null)
45+
{
46+
// TODO: Implement findIdentityByAccessToken() method.
47+
}
48+
49+
/**
50+
* @inheritdoc
51+
*/
52+
public function getId()
53+
{
54+
return $this->getPrimaryKey();
55+
}
56+
57+
/**
58+
* @inheritdoc
59+
*/
60+
public function getAuthKey()
61+
{
62+
// TODO: Implement getAuthKey() method.
63+
}
64+
65+
/**
66+
* @inheritdoc
67+
*/
68+
public function validateAuthKey($authKey)
69+
{
70+
// TODO: Implement validateAuthKey() method.
71+
}
72+
}

tests/models/AssignmentTest.php

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
namespace yii2mod\rbac\tests\models;
4+
5+
use Yii;
6+
use yii\base\Exception;
7+
use yii\rbac\Item;
8+
use yii2mod\rbac\models\AssignmentModel;
9+
use yii2mod\rbac\models\AuthItemModel;
10+
use yii2mod\rbac\tests\data\User;
11+
use yii2mod\rbac\tests\TestCase;
12+
13+
/**
14+
* Class AssignmentTest
15+
* @package yii2mod\rbac\tests\models
16+
*/
17+
class AssignmentTest extends TestCase
18+
{
19+
/**
20+
* @var string
21+
*/
22+
private $_roleName = 'admin';
23+
24+
/**
25+
* @var string
26+
*/
27+
private $_permissionName = 'viewArticles';
28+
29+
// Tests :
30+
31+
public function testAssignRole()
32+
{
33+
$this->createRole();
34+
35+
$user = User::find()->one();
36+
$model = new AssignmentModel($user);
37+
38+
$this->assertTrue($model->assign([$this->_roleName]));
39+
$this->assertArrayHasKey($this->_roleName, Yii::$app->authManager->getAssignments($user->id));
40+
41+
return $model;
42+
}
43+
44+
public function testAssignPermission()
45+
{
46+
$this->createPermission();
47+
48+
$user = User::find()->one();
49+
$model = new AssignmentModel($user);
50+
51+
$this->assertTrue($model->assign([$this->_permissionName]));
52+
$this->assertArrayHasKey($this->_permissionName, Yii::$app->authManager->getAssignments($user->id));
53+
54+
return $model;
55+
}
56+
57+
/**
58+
* @depends testAssignRole
59+
* @depends testAssignPermission
60+
* @param AssignmentModel $role
61+
* @param AssignmentModel $permission
62+
*/
63+
public function testGetItems(AssignmentModel $role, AssignmentModel $permission)
64+
{
65+
$this->assertArrayHasKey($this->_roleName, $role->getItems()['assigned']);
66+
$this->assertArrayHasKey($this->_permissionName, $permission->getItems()['assigned']);
67+
}
68+
69+
/**
70+
* @depends testAssignRole
71+
* @param AssignmentModel $model
72+
*/
73+
public function testRevokeRole(AssignmentModel $model)
74+
{
75+
$this->assertTrue($model->revoke([$this->_roleName]));
76+
}
77+
78+
/**
79+
* @depends testAssignPermission
80+
* @param AssignmentModel $model
81+
*/
82+
public function testRevokePermission(AssignmentModel $model)
83+
{
84+
$this->assertTrue($model->revoke([$this->_permissionName]));
85+
}
86+
87+
/**
88+
* Create role for testing purposes
89+
*
90+
* @return void
91+
*
92+
* @throws Exception
93+
*/
94+
private function createRole()
95+
{
96+
$model = new AuthItemModel();
97+
$model->type = Item::TYPE_ROLE;
98+
$model->name = $this->_roleName;
99+
100+
if (!$model->save()) {
101+
throw new Exception("A Role '{$this->_roleName}' has not been created.");
102+
}
103+
}
104+
105+
/**
106+
* Create permission for testing purposes
107+
*
108+
* @return void
109+
*
110+
* @throws Exception
111+
*/
112+
private function createPermission()
113+
{
114+
$model = new AuthItemModel();
115+
$model->type = Item::TYPE_ROLE;
116+
$model->name = $this->_permissionName;
117+
118+
if (!$model->save()) {
119+
throw new Exception("A Permission '{$this->_permissionName}' has not been created.");
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)