Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
Working on the AuthHelper tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Krämer committed Apr 24, 2015
1 parent 12b47f7 commit ee34c90
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ php:

matrix:
include:
- php: 5.5
- php: 5.5
- php: 5.6
env:
- COVERALLS=1
- php: 7.0

allow_failures:
- php: 7.0
Expand Down
19 changes: 12 additions & 7 deletions src/View/Helper/AuthHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace Burzum\UserTools\View\Helper;

use Cake\Event\Event;
use Cake\Utility\Hash;
use Cake\View\Helper;
use Cake\View\View;
Expand Down Expand Up @@ -54,11 +55,13 @@ public function __construct(\Cake\View\View $View, $settings = []) {
*/
protected function _setupUserData() {
if (is_string($this->_config['session'])) {
$this->_userData = CakeSession::read($this->_config['session']);
$this->_userData = $this->_View->request->session()->read($this->_config['session']);
} else {
if (!isset($this->_View->viewVars[$this->_config['viewVar']]) && $this->_View->viewVars[$this->_config['viewVar']] !== null) {
if ($this->_config['viewVarException'] === true) {
throw new \RuntimeException(__d('user_tools', 'View var `{0}` not present!', $this->_config['viewVar']));
throw new \RuntimeException(sprintf('View var `%s` not present!', $this->_config['viewVar']));
} else {
$this->_userData = [];
}
} else {
$this->_userData = $this->_View->viewVars[$this->_config['viewVar']];
Expand Down Expand Up @@ -118,20 +121,22 @@ public function user($key = null) {
}

/**
* Role check
* Role check.
*
* @param string
* @return boolean
* @param string String of the role identifier.
* @return boolean True if the role is in the set of roles for the active user data.
*/
public function hasRole($role) {
$roles = $this->user($this->_config['roleField']);
if (!is_string($role)) {
throw new \InvalidArgumentException('Role must be a string!');
}
$roles = $this->user($this->config('roleField'));
if (is_string($roles)) {
return ($role === $roles);
}
if (is_array($roles)) {
return (in_array($role, $roles));
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* ]@copyright 2013 - 2014 Florian Krämer
* @license MIT
*/
class UserToolComponent extends TestCase {
class UserToolComponentTest extends TestCase {

/**
* Fixtures
Expand Down
25 changes: 25 additions & 0 deletions tests/TestCase/View/Helper/AuthHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public function testUser() {
$Auth = new AuthHelper($this->View);
$result = $Auth->user('something');
$this->assertEquals($result, 'some value');

$result = $Auth->user();
$this->assertEquals($result, $this->View->viewVars['userData']);
}

/**
Expand All @@ -87,6 +90,14 @@ public function testHasRole() {
$Auth = new AuthHelper($this->View);
$this->assertTrue($Auth->hasRole('manager'));
$this->assertFalse($Auth->hasRole('doesnotexist'));

try {
$object = new \stdClass();
$Auth->hasRole($object);
$this->fail('No \InvalidArgumentException thrown!');
} catch (\InvalidArgumentException $e) {
// Pass
}
}

/**
Expand Down Expand Up @@ -114,4 +125,18 @@ public function testIsLoggedIn() {
$this->assertFalse($Auth->isLoggedin());
}

/**
* testSetupUserData
*
* @return void
*/
public function testSetupUserData() {
try {
$this->View->viewVars = [];
$Auth = new AuthHelper($this->View);
$this->fail('No \RuntimeException thrown!');
} catch (\RuntimeException $e) {
// Pass
}
}
}

0 comments on commit ee34c90

Please sign in to comment.