Skip to content

Instantly share code, notes, and snippets.

@arleighdickerson
Created July 22, 2015 13:49
Show Gist options
  • Save arleighdickerson/f0c220944a8131c016a3 to your computer and use it in GitHub Desktop.
Save arleighdickerson/f0c220944a8131c016a3 to your computer and use it in GitHub Desktop.
Instance Authorization Test
<?php
namespace tests\codeception\unit\models;
use Yii;
use yii\codeception\TestCase;
use Codeception\Specify;
use yii\rbac\PhpManager;
class MockRule extends \yii\rbac\Rule {
const NAME = 'mock';
public function init() {
parent::init();
$this->name = self::NAME;
}
public static $PASS = true;
public function execute($user, $item, $params) {
return self::$PASS;
}
}
class InstanceAuthorizationTest extends TestCase {
use Specify;
private $auth;
protected function setUp() {
parent::setUp();
$this->auth = new PhpManager;
$this->auth->removeAll();
}
public function testSanity() {
expect($this->auth->add(new MockRule))->true();
$base = $this->auth->createRole("base");
expect($this->auth->add($base))->true();
$implied = $this->auth->createRole("implied");
$implied->ruleName = MockRule::NAME;
$this->auth->add($implied);
$this->auth->addChild($implied, $base);
expect($this->auth->getChildren($base->name))->greaterThan(0);
$this->auth->assign($implied, 0);
expect($this->auth->checkAccess(0, 'derp'))->false();
expect($this->auth->checkAccess(0, $implied->name))->true();
expect($this->auth->checkAccess(0, $base->name))->true();
MockRule::$PASS = false;
expect($this->auth->checkAccess(0, $implied->name))->false();
expect($this->auth->checkAccess(0, $base->name))->false();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment