Example #1
0
File: user.php Project: cmsx/auth
 function testRole()
 {
     $u = new User();
     $this->assertFalse($u->hasRole(User::ROLE_ADMIN), 'Не админ 1');
     $this->assertFalse($u->isAdmin(), 'Не админ 2');
     $u->grantRole(User::ROLE_ADMIN);
     $this->assertTrue($u->hasRole(User::ROLE_ADMIN), 'Админ 1');
     $this->assertTrue($u->isAdmin(), 'Админ 2');
     $u = new TestUser();
     try {
         $u->grantRole(TestUser::ROLE_ADMIN);
         $this->fail('Роль Администратора не включена');
     } catch (Exception $e) {
         $this->assertEquals(User::ERR_WRONG_ROLE, $e->getCode(), 'Код ошибки 1');
     }
     $u->grantRole(TestUser::ROLE_SUPEREVIL);
     $u->grantRole(TestUser::ROLE_SUPERHERO);
     $this->assertTrue($u->hasRole(TestUser::ROLE_SUPERHERO), 'Супергерой 1');
     $this->assertTrue($u->hasRole(TestUser::ROLE_SUPEREVIL), 'Суперзлодей 1');
     $u->rejectRole(TestUser::ROLE_SUPEREVIL);
     $this->assertTrue($u->hasRole(TestUser::ROLE_SUPERHERO), 'Супергерой 2');
     $this->assertFalse($u->hasRole(TestUser::ROLE_SUPEREVIL), 'Суперзлодей 2');
     $u->switchRole(TestUser::ROLE_SUPEREVIL);
     $this->assertFalse($u->hasRole(TestUser::ROLE_SUPERHERO), 'Супергерой 3');
     $this->assertTrue($u->hasRole(TestUser::ROLE_SUPEREVIL), 'Суперзлодей 3');
     try {
         $u->switchRole(TestUser::ROLE_SUPEREVIL | TestUser::ROLE_SUPERHERO);
         $this->fail('Можно установить только одну роль, но не на группу');
     } catch (Exception $e) {
         $this->assertEquals(User::ERR_WRONG_ROLE, $e->getCode(), 'Код ошибки 2');
     }
 }