Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- added new method to metadata (Metadata::get_namespace)

 - User class completed!
  • Loading branch information...
commit ae25028c4f2e907bfffb02e045d18b08141bfccd 1 parent 3242144
@arunoda authored
View
5 .buildpath
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<buildpath>
+ <buildpathentry kind="src" path=""/>
+ <buildpathentry kind="con" path="org.eclipse.php.core.LANGUAGE"/>
+</buildpath>
View
22 .project
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>git</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.wst.validation.validationbuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.dltk.core.scriptbuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.php.core.PHPNature</nature>
+ </natures>
+</projectDescription>
View
3  .settings/org.eclipse.php.core.prefs
@@ -0,0 +1,3 @@
+#Tue Nov 17 12:18:06 IST 2009
+eclipse.preferences.version=1
+include_path=0;/trunk
View
20 application/helpers/metadata_helper.php
@@ -14,6 +14,11 @@ public static function set($key,$value,$namespace='global'){
}
+ /**
+ wildcards
+ % for all
+ _ for single char
+ */
public static function get($key,$namespace='global'){
$CI= get_instance();
$db=$CI->db;
@@ -37,4 +42,19 @@ public static function delete($key,$namespace='global'){
return $db->query($sql);
}
+ public static function get_namespace($key,$value,$namespace="%"){
+ $CI= get_instance();
+ $db=$CI->db;
+ $sql="SELECT namespace FROM metadata WHERE `key` LIKE '$key' AND `value` ".
+ "LIKE '$value' AND `namespace` LIKE '$namespace'";
+ $query= $db->query($sql);
+
+ if($query->result()==null) return null;
+ $rtn=array();
+ foreach($query->result() as $val){
+ $rtn[]=$val->namespace;
+ }
+ return $rtn;
+ }
+
}
View
102 application/helpers/user_helper.php
@@ -1,5 +1,107 @@
<?php
class User{
+ private $id;
+ var $username;
+ var $email;
+ var $fullname;
+ private $password;
+ private $salt;
+ function __construct($id="",$username="",$email="",$fullname="",$password="",$salt=""){
+ $this->id=$id;
+ $this->username=$username;
+ $this->email=$email;
+ $this->fullname=$fullname;
+ $this->password=$password;
+ $this->salt=$salt;
+ }
+
+ public function save(){
+ $count=$this->id;
+ if(!$count){
+ $count=Metadata::get('user_count');
+ $count=($count)?$count['global']['user_count']:0;
+ $count++;
+ $this->id=$count;
+ Metadata::set('user_count',$count);
+ }
+
+ Metadata::set('username',$this->username,"user.$count");
+ Metadata::set('id',$this->id,"user.$count");
+ Metadata::set('email',$this->email,"user.$count");
+ Metadata::set('fullname',$this->fullname,"user.$count");
+ Metadata::set('password',$this->password,"user.$count");
+ Metadata::set('salt',$this->salt,"user.$count");
+
+ }
+
+ public function get_id(){
+ return $this->id;
+ }
+
+ public function set_password($password){
+ $this->salt=substr(md5(rand()),0,25);
+ $this->password=md5($password.$this->salt);
+ Metadata::set('password',$this->password,"user.{$this->id}");
+ Metadata::set('salt',$this->salt,"user.{$this->id}");
+ }
+
+ public function check_password($password){
+ return $this->password==md5($password.$this->salt);
+ }
+
+
+ public static function by_id($id){
+ $res=Metadata::get('%',"user.$id");
+ if(!$res) return null;
+
+ return new User(
+ $res["user.$id"]['id'],
+ $res["user.$id"]['username'],
+ $res["user.$id"]['email'],
+ $res["user.$id"]['fullname'],
+ $res["user.$id"]['password'],
+ $res["user.$id"]['salt']
+ );
+
+ }
+
+ public static function by_username($username){
+ $res=Metadata::get_namespace('username',$username,'user.%');
+ if(!$res) return null;
+ $namespace=$res[0];
+ $res=Metadata::get('%',$namespace);
+ if(!$res) return null;
+
+ return new User(
+ $res[$namespace]['id'],
+ $res[$namespace]['username'],
+ $res[$namespace]['email'],
+ $res[$namespace]['fullname'],
+ $res[$namespace]['password'],
+ $res[$namespace]['salt']
+ );
+ }
+
+ public static function by_email($email){
+ $res=Metadata::get_namespace('email',$email,'user.%');
+ if(!$res) return null;
+ $namespace=$res[0];
+ $res=Metadata::get('%',$namespace);
+ if(!$res) return null;
+
+ return new User(
+ $res[$namespace]['id'],
+ $res[$namespace]['username'],
+ $res[$namespace]['email'],
+ $res[$namespace]['fullname'],
+ $res[$namespace]['password'],
+ $res[$namespace]['salt']
+ );
+ }
+
+ public static function by_session($session){
+ throw new Exception("Not Implemented Yet!");
+ }
}
View
38 application/tests/helpers/metadata_helper.php
@@ -1,6 +1,12 @@
<?php
class Metadata_helper extends CodeIgniterUnitTestCase{
+ function setUp(){
+ Metadata::set('email','a@abc.com','**user.4');
+ Metadata::set('email','a@abc.com','**man.2');
+ Metadata::set('username','arunoda','**user.1');
+ }
+
function test_included(){
$this->assertTrue(class_exists('Metadata'));
}
@@ -10,7 +16,7 @@ function test_set(){
}
function test_get(){
- $users=Metadata::get('email','user._');
+ $users=Metadata::get('email','**user._');
$usernames=array();
foreach ($users as $user){
$usernames[]=$user['email'];
@@ -18,16 +24,34 @@ function test_get(){
}
function test_delete(){
- $this->assertTrue(Metadata::delete('email','user.1'));
+ $this->assertTrue(Metadata::delete('email','**user.1'));
}
function test_all1(){
- Metadata::set('username','arunoda','user.1');
- $res=Metadata::get('username','user.1');
- $this->assertEqual('arunoda',$res['user.1']['username']);
+ $res=Metadata::get('username','**user.1');
+ $this->assertEqual('arunoda',$res['**user.1']['username']);
- Metadata::delete('user%','user.%');
- $res=Metadata::get('username','user.1');
+ Metadata::delete('user%','**user.%');
+ $res=Metadata::get('username','**user.1');
$this->assertNull($res,"found $res");
}
+
+ function test_get_metadata(){
+
+ $res=Metadata::get_namespace('email','a@abc.com');
+ $this->assertTrue(count($res)>0);
+
+ $res=Metadata::get_namespace("email",'a@abc.com',"**user%");
+ $this->assertEqual($res[0],"**user.4");
+
+ $res=Metadata::get_namespace("email",'a@abc.com',"**man._");
+ $this->assertEqual($res[0],"**man.2");
+ }
+
+ function test_get_null(){
+ $res=Metadata::get("ppp",'gfd.gt');
+ $this->assertNull($res);
+ }
+
+
}
View
90 application/tests/helpers/user_helper.php
@@ -5,18 +5,80 @@ function test_included(){
$this->assertTrue(class_exists('User'));
}
- /**
- * Functionalities we expect from the POC
- * --------------------------------------
- * 1.fields (id,username,email,fullname,salt,password)
- * 2.save()
- * 3.get_id()
- * 4.set_password()
- * 5.set_session()
- * 5.check_password()
- * 6.::by_id()
- * 7.::by_username()
- * 8.::by_email()
- * 9.::by_session()
- */
+
+
+ function test_save(){
+ //just to check and get
+ $user=new User();
+ $rand=md5(rand());
+ $user->username='user_'.$rand;
+ $user->fullname='This is the name';
+ $user->email="$rand@email.com";
+ $user->save();
+
+
+ $user_g=User::by_id($user->get_id());
+ $this->assertNotNull($user_g);
+ $this->assertEqual($user_g->username,$user->username);
+ $this->assertEqual($user_g->email,$user->email);
+ $this->assertEqual($user_g->fullname,$user->fullname);
+ $this->assertEqual($user_g->get_id(),$user->get_id());
+
+ }
+
+ function test_get_username(){
+ //just to check and get
+ $user=new User();
+ $rand=md5(rand());
+ $user->username='user_'.$rand;
+ $user->fullname='This is the name';
+ $user->email="$rand@email.com";
+ $user->save();
+
+
+ $user_g=User::by_username($user->username);
+ $this->assertNotNull($user_g);
+ $this->assertEqual($user_g->username,$user->username);
+ $this->assertEqual($user_g->email,$user->email);
+ $this->assertEqual($user_g->fullname,$user->fullname);
+ $this->assertEqual($user_g->get_id(),$user->get_id());
+
+ }
+
+ function test_get_by_email(){
+ //just to check and get
+ $user=new User();
+ $rand=md5(rand());
+ $user->username='user_'.$rand;
+ $user->fullname='This is the name';
+ $user->email="$rand@email.com";
+ $user->save();
+
+
+ $user_g=User::by_email($user->email);
+ $this->assertNotNull($user_g);
+ $this->assertEqual($user_g->username,$user->username);
+ $this->assertEqual($user_g->email,$user->email);
+ $this->assertEqual($user_g->fullname,$user->fullname);
+ $this->assertEqual($user_g->get_id(),$user->get_id());
+
+ }
+
+ function test_password(){
+ $user=new User();
+ $rand=md5(rand());
+ $user->username='user_'.$rand;
+ $user->fullname='This is the name';
+ $user->email="$rand@email.com";
+ $user->save();
+
+ $user->set_password('abc.com');
+ $this->assertTrue($user->check_password('abc.com'));
+ $this->assertFalse($user->check_password('abc.codffdm'));
+
+ //to check with the DB
+ $user_g=User::by_email($user->email);
+ $this->assertTrue($user_g->check_password('abc.com'));
+ $this->assertFalse($user_g->check_password('abc.codffdm'));
+ }
}
View
3  index.php
@@ -101,9 +101,10 @@
$application_folder = 'application';
}
- define('APPPATH', BASEPATH.$application_folder.'/');
+ define('APPPATH', realpath(dirname(__FILE__)).'/'.$application_folder.'/');
}
+
/*
|---------------------------------------------------------------
| LOAD THE FRONT CONTROLLER
View
4 test.php
@@ -39,7 +39,7 @@ function add_test($file, &$test) {
elseif (preg_match('/_helper/', $file)) {
$view = preg_replace('#' . ROOT . 'application/tests/helpers/([a-zA-Z0-9_\-])_helper.php#', '$1', $file);
$view = implode('/', explode('_', $view));
- $implementation = ROOT . 'application/helpers/'.$view.'.php';
+ $implementation = ROOT . 'application/helpers/'.$view.'_helper.php';
}
if (file_exists($implementation)) {
@@ -109,7 +109,7 @@ function prepare_array($value, $key) {
if (preg_match('/_controller/', $value)) { $files[$key] = ROOT . 'application/tests/controllers/' . $value; }
if (preg_match('/_view/', $value)) { $files[$key] = ROOT . 'application/tests/views/' . $value; }
if (preg_match('/_library/', $value)) { $files[$key] = ROOT . 'application/tests/libraries/' . $value; }
- if (preg_match('/_helper/', $value)) { $files[$key] = ROOT . 'application/tests/helpers/' . $value; }
+ if (preg_match('/_helper/', $value)) { $files[$key] = ROOT . 'application/tests/helpers/' . $value; }
}
array_walk($files, 'prepare_array');
Please sign in to comment.
Something went wrong with that request. Please try again.