* @license PHP License * @package WB * @subpackage unittest */ WBClass::load('WBUnitTest_SQLUte'); /** * Unit Test * * TestBaseClass * * @version 0.2.0 * @package WB * @subpackage unittest */ class TestCaseUser extends UnitTestCase { /** * user storage * * @var WBUser_Storage_Table */ protected $table; /** * list of users * @var unknown_type */ protected $users = array(); /** * table options * @var array */ protected $options = array(); /** * current user id * @var strong */ protected $id; /** * constructor */ public function __construct() { WBClass::load('WBUnitTest_SQLUte'); WBUnitTest_SQLUte::source(__FILE__, 'construct'); $this->table = WBClass::create('WBUser_Storage_Table', $this->options); } /** * destructor */ public function __destruct() { // WBUnitTest_SQLUte::source( __FILE__, 'destruct' ); } /** * check user storage object */ public function testStorageTableBegin() { $this->assertTrue(class_exists( 'WBUser_Storage_Table', false)); $this->assertIsA($this->table, 'WBUser_Storage_Table'); } /** * check creating valid user accounts */ public function testStorageTableAddUser() { $new = array(); $new[] = array( 'nickname' => 'truss', 'enabled' => 1, 'approved' => 1, 'forename' => 'gERD', 'surname' => 'Schaufelberger', 'password' => 'gerd123', 'email' => 'gerd@php-tools.net' ); $new[] = array( 'nickname' => 'foobar', 'enabled' => 1, 'approved' => 1, 'forename' => 'Foo', 'surname' => 'F Bar', 'password' => 'foo123', 'email' => 'foobar@example.com' ); $new[] = array( 'nickname' => 'joedoe', 'enabled' => 1, 'approved' => 1, 'forename' => 'Joe', 'surname' => 'Doe', 'password' => 'joe123', 'email' => 'jodoe@example.com' ); $new[] = array( 'nickname' => 'tux2008', 'enabled' => 1, 'approved' => 1, 'forename' => 'Tux', 'surname' => '', 'password' => 'tux123', 'email' => 'tux@example.com' ); foreach( $new as $i => $n ) { WBParam::set('wb/user/password/algo', 'md5-wombat1'); if (0 == $i % 2) { WBParam::set('wb/user/password/algo', 'sha256'); } $id = $this->table->set( $n, true ); $this->assertNotNull( $id ); $this->users[$id] = $n; } } public function testStorageLoadUser() { $this->table->clear(); foreach( $this->users as $id => $data ) { $loadId = $this->table->load($id); $this->assertEqual($id, $loadId); } } /** * login user by Nickname */ public function testStorageTableFindUserByNickname() { $this->table->clear(); foreach( $this->users as $id => $data ) { $cred = array( 'nickname' => $data['nickname'], 'password' => $data['password'] ); $loginId = $this->table->find($cred); $this->assertEqual($id, $loginId); $userData = $this->table->get(); } } /** * login user by Email */ public function testStorageTableFindUserByEmail() { $this->table->clear(); foreach( $this->users as $id => $data ) { $cred = array( 'email' => $data['email'], 'password' => $data['password'] ); $loginId = $this->table->find($cred); $this->assertEqual($id, $loginId); $userData = $this->table->get(); } } /** * load User_Auth */ public function TestUserAuthLoad() { WBClass::load('WBUser'); $this->assertTrue(class_exists('WBUser', false)); $auth = WBUser::getCurrent(); $this->assertIsA($auth, 'WBUser_Auth'); } /** * use Auth object to login using nickname */ public function TestAuthLoginByNickname() { $auth = WBUser::getCurrent(); if ($auth->isAuthenticated()) { $auth->logout(); } foreach ($this->users as $id => $data) { $cred = array( 'nickname' => $data['nickname'], 'password' => $data['password'] ); $loginId = $auth->login($cred); $this->assertEqual($id, $loginId); $this->assertTrue($auth->isAuthenticated()); $auth->logout(); } } /** * use Auth object to login using email address */ public function testAuthLoginByEmail() { $auth = WBUser::getCurrent(); if ($auth->isAuthenticated()) { $auth->logout(); } foreach ($this->users as $id => $data) { $cred = array( 'email' => $data['email'], 'password' => $data['password'] ); $loginId = $auth->login($cred); $this->assertEqual($id, $loginId); $this->assertTrue($auth->isAuthenticated()); $auth->logout(); } } public function testUserRegisterInit() { $reg = WBClass::create('WBUser_Register'); $this->assertIsA($reg, 'WBUser_Register'); $this->assertTrue($reg->load(1)); $this->assertFalse($reg->load(1232435)); $this->assertTrue($reg->load(1)); } public function testUserRegisterRegister() { $reg = WBClass::create('WBUser_Register'); // try to register empty data $data = array(); $this->assertFalse($reg->register($data)); // try to register useless data $data = array( 'foo' => 'bar' ); $this->assertFalse($reg->register($data)); // try to register incomplete data $data = array( 'nickname' => 'nickname', 'email' => 'nickname@example.com' ); $this->assertFalse($reg->register($data)); $data = array( 'nickname' => 'nickname', 'email' => 'nickname@example.com', 'forename' => 'Nick', 'surname' => 'Name' ); $this->assertFalse($reg->register($data)); $data = array( 'nickname' => 'nickname', 'email' => 'nickname@example.com', 'password' => 'nickname123', 'forename' => 'Nick', 'surname' => 'Name' ); $this->id = $reg->register($data); $this->assertNotNull($this->id); } public function testUserRegisterApprove() { $reg = WBClass::create('WBUser_Register'); $code = '123'; $this->assertFalse($reg->approve($code)); $code = 'asd123'; $this->assertFalse($reg->approve($code)); $code = '123 sdfgw45dsfg'; $this->assertFalse($reg->approve($code)); $code = '123asd4356sadf'; $this->assertFalse($reg->approve($code)); $code = $this->id . 'asd67saf'; $this->assertFalse($reg->approve($code)); $oc = WBClass::create('WBDatasource_ObscureCode'); /** @var $oc WBDatasource_ObscureCode */ $oc->setNamespace(get_class($reg) . ':register')->setId($this->id); $code = $this->id . $oc->get(); $this->assertTrue($reg->approve($code)); } }