Permalink
Browse files

'I know... I should not have committed all this in the same branch. a…

…dded user tests and other stuff'
  • Loading branch information...
bioshazard committed Mar 18, 2017
1 parent 462bd5a commit c7f99a91b625b97c5795ffed1f86abde487dca3e
View
@@ -1,5 +1,4 @@
<?php
// Bootstrap the littlefoot libraries
require_once('lf/system/bootstrap.php');
@@ -18,7 +18,7 @@ class users
public function main()
{
$args = \lf\requestGet('Param'); // backward compatibility
$users = \lf\orm::q('lf_users')->order()->getAll();
$users = (new \LfUsers)->order()->getAll();
$usercount = count($users);
include 'view/users.main.php';
}
View
@@ -13,7 +13,7 @@ public function rest()
// test for API token, expand on this later
$api_key = $headers['X-Api-Key'];
if($api_key != '612e648bf9594adb50844cad6895f2cf')
if(true || 'COMING SOON!')
return $this;
if( count($action) < 2 )
@@ -42,7 +42,10 @@ public function rest()
// display result as JSON
header('Content-Type: application/json');
return json_encode(
[ "result" => (new $class)->$method($id) ]
[
"data" => (new $class)->$method($id)
, "self" => "?"
]
);
}
}
View
@@ -64,7 +64,7 @@ public function updateprofile()
$update = array_intersect_key($_POST, $filter);
(new \LfUsers)
(new \LfUsers) // ORM Exmaple: this could be shortened up here to ->updateById($id, $update);
->byId($id)
->setArray($update)
->debug()
View
@@ -156,12 +156,12 @@ class cms
*/
public function run()
{
// load plugins from `lf_plugins` table, execute plugins hooked to 'pre cms run'
(new \lf\plugin)->run('pre cms run');
// Start the '->run()' timer
startTimer(__METHOD__);
// load plugins from `lf_plugins` table, execute plugins hooked to 'pre cms run'
(new \lf\plugin)->run('pre cms run');
// test the installation. can we connect to MySQL, etc?
//(new install)->test();
(new orm)->init();
View
@@ -437,6 +437,16 @@ public function getIterator() {
return new \ArrayIterator( $return );
}
/**
*
*/
public function getSchema() {
$schema = array();
foreach( $this->fetchall('desc '.$this->table) as $row )
$schema[$row['Field']] = $row;
return $schema;
}
/**
* Free last database result
@@ -564,17 +574,20 @@ function escape($str)
* Run query, return SQL result, increment SQL counter
*
* `$sqlResult = (new orm)->query('SELECT * FROM lf_users');`
*
*
* @param string $q MySQL Query
* @param bool $big If the request is big
*/
function query($q, $big = false)
{
$method_name = __METHOD__.' '.md5($q); // makes the query timer label unique to the query run
startTimer($method_name);
$this->mysqli_result = $this->mysqli->query($q);
$this->query_count++;
if($this->mysqli->error)
$this->error[] = $this->mysqli->connect_errno.": " .$this->mysqli->connect_error;
endTimer($method_name);
return $this->mysqli_result;
}
@@ -706,6 +719,7 @@ public function __call($magicMethod, $args)
if($magicPrefix == 'setAs')
return $this->setAsMagic($magicSuffix, $args);
// Run a find after the by()
if($magicPrefix == 'findBy')
return $this
@@ -856,6 +870,24 @@ public function find($by = NULL)
return $this;
}
public function search($field, $value)
{
$search_result = array();
$index = 1;
foreach($this->result as $row)
{
foreach($row as $testfield => $testvalue)
{
if($testfield == $field && $testvalue == $value)
{
$search_result[$index][$testfield] = $testvalue;
}
}
$index++;
}
return $search_result;
}
/**
* push current result row into data value
@@ -1452,6 +1484,7 @@ public function updateById($id, $data)
}
$page->save();
return $this->mysqli->info;
}
public function rawResult()
@@ -0,0 +1,64 @@
<?php
use PHPUnit\Framework\TestCase;
class UserTest extends TestCase
{
public function testResolveAnon()
{
$expectedOut = 'TEST|Anonymous|TSET';
$replaceIn = 'TEST|{user:0}|TSET';
$resolvedIdString = (new \lf\user)->resolveIds($replaceIn);
$this->assertEquals($resolvedIdString, $expectedOut);
}
public function testCreateAnon()
{
$anonUser = (new \lf\user);
stderr($anonUser->getuser());
$display_name = $anonUser->getdisplay_name();
$this->assertEquals($display_name, 'Anonymous');
}
public function testCreateNewUser()
{
// I use this exact process to create a user at first installation
// Making this test finally leaves me certain that it works lol
$newUser = (new \lf\user)
->setDisplay_name('First L.')
->setEmail('fak3@j098gj[0349hg.com')
->setUser('zer0cool')
->setPass('hunter2')
->setStatus('valid')
->setAccess('admin')
->save()
->toSession(); // and auto login as that new user
// Test `->save()`
$savedUser = (new \orm\lf_users)->getById( $newUser->getId() );
$savedName = $savedUser['display_name'];
$newUserName = $newUser->getDisplay_name();
$this->assertEquals($newUserName, $savedName);
$sessionUser = (new \lf\user)->fromSession();
stderr($_SESSION);
// clean up after yourself
(new \LfUsers)->deleteById($newUser->getId());
// $display_name = $user->getdisplay_name();
// $this->assertEquals($display_name, 'Anonymous');
}
public function testCreateddAnon()
{
$user = new \lf\user();
$display_name = $user->getdisplay_name();
$this->assertEquals($display_name, 'Anonymous');
}
}
View
@@ -32,6 +32,13 @@ public function idFromSession()
return $this->fromSession()->getId();
}
// this makes me want to separate session handling into a new `sessions` class
// maybe merge with the auth class?
public function isLoggedIn()
{
return ($this->fromSession()->getId() != 0);
}
// resolve {user:34} to user 34's display_name. {user:0} resolves to "Anonymous".
public function resolveIds($out, $wholeLastName = false)
{

0 comments on commit c7f99a9

Please sign in to comment.