1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133:
<?php
namespace Nethgui\Test\Unit\Nethgui\Authorization;
class UserTest extends \PHPUnit_Framework_TestCase
{
protected $object;
private $log;
protected function setUp()
{
$this->session = $this->getMock('Nethgui\Utility\Session', array('retrieve'));
$this->log = $this->getMock('Nethgui\Log\LogInterface');
$this->object = new \Nethgui\Authorization\User($this->session, $this->log);
}
public function testSetAuthenticationProcedure()
{
$this->assertSame($this->object, $this->object->setAuthenticationProcedure(function () {
return FALSE;
}));
}
public function testGetLanguageCode()
{
$this->session->expects($this->once())
->method('retrieve')
->with(get_class($this->object))
->will($this->returnValue(new \ArrayObject(array(
'credentials' => array(),
'preferences' => array('lang' => 'it'),
'authenticated' => array()
))));
$this->assertEquals('it', $this->object->getLanguageCode());
$this->object->setPreference('lang', 'fr');
$this->assertEquals('fr', $this->object->getLanguageCode());
}
public function testSetLanguageCode()
{
$this->assertSame($this->object, $this->object->setLanguageCode('fr'));
}
public function testAuthenticate()
{
$this->assertNull($this->object->getCredential('undefined'));
$this->assertFalse($this->object->isAuthenticated());
$this->assertFalse($this->object->hasCredential('groups'));
$this->assertEquals(FALSE, $this->object->getAuthorizationAttribute('authenticated'));
$this->object->setAuthenticationProcedure(function ($uname, $pw, &$credentials) {
$credentials['groups'] = array('g1', 'g2');
$credentials['username'] = $uname;
return TRUE;
});
$this->assertTrue($this->object->authenticate('usr1', 'pass'));
$this->assertTrue($this->object->isAuthenticated());
$this->assertTrue($this->object->hasCredential('groups'));
$this->assertEquals('usr1', $this->object->getCredential('username'));
$this->assertEquals('usr1', $this->object->asAuthorizationString());
$this->assertEquals(array('g1', 'g2'), $this->object->getAuthorizationAttribute('groups'));
$this->assertEquals(TRUE, $this->object->getAuthorizationAttribute('authenticated'));
}
public function testSerialize()
{
$ser = $this->object->serialize();
$this->assertInternalType('string', $ser);
}
public function testUnserialize()
{
$ser = $this->object->serialize();
$state = unserialize($ser);
$this->assertInternalType('array', $state);
$object = unserialize(serialize($this->object));
$this->assertInstanceOf('Nethgui\Authorization\User', $object);
}
public function testSetPreference()
{
$this->assertSame($this->object, $this->object->setPreference('lang', 'fr'));
}
public function testGetPreference()
{
$this->assertNull($this->object->getPreference('nopref'));
$this->object->setPreference('lang', 'fr');
$this->assertEquals('fr', $this->object->getPreference('lang'));
}
}