You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
3.6 KiB
143 lines
3.6 KiB
11 years ago
|
<?php
|
||
|
|
||
10 years ago
|
namespace yii\rbac;
|
||
|
|
||
|
/**
|
||
|
* Mock for the filemtime() function for rbac classes. Avoid random test fails.
|
||
|
* @return int
|
||
|
*/
|
||
|
function filemtime($file)
|
||
|
{
|
||
|
return \yiiunit\framework\rbac\PhpManagerTest::$filemtime ?: \filemtime($file);
|
||
|
}
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Mock for the time() function for rbac classes. Avoid random test fails.
|
||
|
* @return int
|
||
|
*/
|
||
|
function time()
|
||
|
{
|
||
|
return \yiiunit\framework\rbac\PhpManagerTest::$time ?: \time();
|
||
|
}
|
||
|
|
||
11 years ago
|
namespace yiiunit\framework\rbac;
|
||
|
|
||
|
use Yii;
|
||
|
|
||
|
/**
|
||
|
* @group rbac
|
||
10 years ago
|
* @property ExposedPhpManager $auth
|
||
11 years ago
|
*/
|
||
10 years ago
|
class PhpManagerTest extends ManagerTestCase
|
||
11 years ago
|
{
|
||
10 years ago
|
public static $filemtime;
|
||
10 years ago
|
public static $time;
|
||
10 years ago
|
|
||
10 years ago
|
protected function getItemFile()
|
||
10 years ago
|
{
|
||
|
return Yii::$app->getRuntimePath() . '/rbac-items.php';
|
||
|
}
|
||
|
|
||
10 years ago
|
protected function getAssignmentFile()
|
||
10 years ago
|
{
|
||
|
return Yii::$app->getRuntimePath() . '/rbac-assignments.php';
|
||
|
}
|
||
|
|
||
10 years ago
|
protected function getRuleFile()
|
||
10 years ago
|
{
|
||
|
return Yii::$app->getRuntimePath() . '/rbac-rules.php';
|
||
|
}
|
||
|
|
||
|
protected function removeDataFiles()
|
||
|
{
|
||
10 years ago
|
@unlink($this->getItemFile());
|
||
|
@unlink($this->getAssignmentFile());
|
||
|
@unlink($this->getRuleFile());
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
/**
|
||
|
* @inheritdoc
|
||
|
*/
|
||
10 years ago
|
protected function createManager()
|
||
|
{
|
||
|
return new ExposedPhpManager([
|
||
10 years ago
|
'itemFile' => $this->getItemFile(),
|
||
|
'assignmentFile' => $this->getAssignmentFile(),
|
||
|
'ruleFile' => $this->getRuleFile(),
|
||
10 years ago
|
]);
|
||
|
}
|
||
|
|
||
11 years ago
|
protected function setUp()
|
||
|
{
|
||
10 years ago
|
static::$filemtime = null;
|
||
10 years ago
|
static::$time = null;
|
||
11 years ago
|
parent::setUp();
|
||
10 years ago
|
|
||
|
if (defined('HHVM_VERSION')) {
|
||
|
$this->markTestSkipped('PhpManager is not compatible with HHVM.');
|
||
|
}
|
||
|
|
||
11 years ago
|
$this->mockApplication();
|
||
10 years ago
|
$this->removeDataFiles();
|
||
|
$this->auth = $this->createManager();
|
||
11 years ago
|
}
|
||
|
|
||
|
protected function tearDown()
|
||
|
{
|
||
10 years ago
|
$this->removeDataFiles();
|
||
10 years ago
|
static::$filemtime = null;
|
||
10 years ago
|
static::$time = null;
|
||
11 years ago
|
parent::tearDown();
|
||
|
}
|
||
|
|
||
|
public function testSaveLoad()
|
||
|
{
|
||
10 years ago
|
static::$time = static::$filemtime = \time();
|
||
10 years ago
|
|
||
10 years ago
|
$this->prepareData();
|
||
10 years ago
|
$items = $this->auth->items;
|
||
|
$children = $this->auth->children;
|
||
|
$assignments = $this->auth->assignments;
|
||
|
$rules = $this->auth->rules;
|
||
11 years ago
|
$this->auth->save();
|
||
10 years ago
|
|
||
|
$this->auth = $this->createManager();
|
||
11 years ago
|
$this->auth->load();
|
||
|
|
||
10 years ago
|
$this->assertEquals($items, $this->auth->items);
|
||
|
$this->assertEquals($children, $this->auth->children);
|
||
|
$this->assertEquals($assignments, $this->auth->assignments);
|
||
|
$this->assertEquals($rules, $this->auth->rules);
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
public function testUpdateItemName()
|
||
10 years ago
|
{
|
||
|
$this->prepareData();
|
||
|
|
||
|
$name = 'readPost';
|
||
|
$permission = $this->auth->getPermission($name);
|
||
|
$permission->name = 'UPDATED-NAME';
|
||
10 years ago
|
$this->assertTrue($this->auth->update($name, $permission), 'You should be able to update name.');
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
public function testUpdateDescription() {
|
||
|
$this->prepareData();
|
||
|
$name = 'readPost';
|
||
|
$permission = $this->auth->getPermission($name);
|
||
|
$permission->description = 'UPDATED-DESCRIPTION';
|
||
|
$this->assertTrue($this->auth->update($name, $permission), 'You should be able to save w/o changing name.');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @expectedException \yii\base\InvalidParamException
|
||
|
*/
|
||
|
public function testOverwriteName()
|
||
|
{
|
||
|
$this->prepareData();
|
||
|
$name = 'readPost';
|
||
|
$permission = $this->auth->getPermission($name);
|
||
|
$permission->name = 'createPost';
|
||
|
$this->auth->update($name, $permission);
|
||
|
}
|
||
10 years ago
|
}
|