Source for file db.class.php
Documentation is available at db.class.php
* test case for database wrapper
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* @author m2mtech <tech@m2m.at>
* @copyright 2007 m2m server software gmbh
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License Version 2
* @version $Id: db.class.php 140 2007-08-29 22:01:56Z m2mtech $
* @link http://www.ea-geier.at/
require_once('code/base/db.class.php');
* helper class - input validator
require_once('code/base/input.class.php');
Mock::generate('eaInput');
* helper class - create sql tables from schemas
require_once(eaADODB_DIR . 'adodb-xmlschema03.inc.php');
* tast case for db wrapper
* enable debugging of ADOdb
if (isset ($GLOBALS['ADODB_OUTP'])) unset ($GLOBALS['ADODB_OUTP']);
$this->UnitTestCase('Test Database Wrapper');
* checks connection to database
$GLOBALS['ADODB_OUTP'] = 'eaHideADOdbErrors';
$conf = $GLOBALS['conf'];
$this->assertTrue($db->error());
$conf['dsn'] = $GLOBALS['conf']['dsn'] . 'wrong';
$this->assertTrue($db->error());
$conf['dsn'] = 'wrong' . $GLOBALS['conf']['dsn'];
$this->assertTrue($db->error());
$conf['dsn'] = str_replace('//', '//wrong', $GLOBALS['conf']['dsn']);
$this->assertTrue($db->error());
$conf['dsn'] = str_replace('@', 'wrong@', $GLOBALS['conf']['dsn']);
$this->assertTrue($db->error());
// wrong serer ... very very strange ... some setups do not care
// about the name you give for the server
$conf['dsn'] = str_replace('@', '@wrong', $GLOBALS['conf']['dsn']);
// $this->assertTrue($db->error()); // should be normal but isn't
echo 'Note: The used database does not care about the setting for the server value<br />';
unset ($GLOBALS['ADODB_OUTP']);
$conf = $GLOBALS['conf'];
$this->assertFalse($db->error());
* checks connection to database
$conf = $GLOBALS['conf'];
if ($conf['dsnNewClientDB']) $testNewDB = true;
$data['txtNewClientName'] = 'nameTest';
$this->assertTrue($clientID = $db->createClient($data));
$this->assertTrue($db->getClients($in));
$this->assertTrue($oldTables = $db->_db->MetaTables());
$oldPrefix = $db->_prefix;
$this->assertEqual($db->switchToClientDB($clientID), $testNewDB);
// no way to detect if session has been written :(
$newTables = $db->_db->MetaTables();
// tables should have changed, because we use another database
$this->assertNotEqual($newTables, $oldTables);
// because we switched, we have to move back
// tables should not have changed, because we use the same database
$this->assertEqual($newTables, $oldTables);
$db->_prefix = $oldPrefix;
$this->assertTrue($db->deleteClient($clientID));
* checks for not logged in, wrong logindata, valid logindata, valid session
$conf = $GLOBALS['conf'];
$this->assertFalse($db->checkLogin($in));
$in->setReturnValue('post', 'testmail', array('mailLogin'));
$in->setReturnValue('post', 'testpass', array('passLogin'));
$this->assertFalse($db->checkLogin($in));
$this->assertTrue($db->createUser('testmail', 'testpass'));
$this->assertTrue($user = $db->checkLogin($in));
$this->assertTrue($db->updateUserConf($user['id'], array('testvar' => 'testvalue')));
$what = array('name' => 'testvar', 'context' => 'user', 'lastModified' => $db->now());
$this->assertTrue($db->insert($what, $db->table('config')));
$in = new MockeaInput(); // delete old return values;
$this->assertTrue($db->checkLogin($in));
$this->assertTrue(isset ($conf['testvar']));
$this->assertEqual($conf['testvar'], 'testvalue');
$this->assertTrue($db->deleteUser('testmail'));
$this->assertTrue($db->delete($db->table('config'), "name = 'testvar'"));
$conf = $GLOBALS['conf'];
$in->setReturnValue('post', 'testmail', array('mailLogin'));
$in->setReturnValue('post', 'testpass', array('passLogin'));
$this->assertTrue($db->createUser('testmail', 'testpass'));
$this->assertTrue($user = $db->checkLogin($in));
$in = new MockeaInput(); // delete old return values;
$this->assertTrue($db->checkLogin($in));
$this->assertTrue($db->logout());
$this->assertFalse($db->checkLogin($in));
$this->assertTrue($db->deleteUser('testmail'));
* checks non existing & existing user
$conf = $GLOBALS['conf'];
$this->assertFalse($db->isUser($user));
$this->assertTrue($db->createUser($user, 'testpass'));
$this->assertTrue($db->isUser($user));
// returns true because no name is set
$this->assertTrue($result = $db->isUser($user, 'name'));
$this->assertEqual($result, true);
$this->assertTrue($result = $db->isUser($user, 'id'));
$this->assertTrue($result > 1);
$this->assertTrue($db->deleteUser($user));
$conf = $GLOBALS['conf'];
//user does not exist already
$this->assertFalse($db->isUser($user));
$this->assertTrue($db->createUser($user, 'testpass'));
// user successfully created
$this->assertTrue($db->isUser($user));
$this->assertTrue($db->deleteUser($user));
* checks function & result
$conf = $GLOBALS['conf'];
$this->assertTrue($db->createUser($user, 'testpass'));
$this->assertTrue($db->setPW($user, 'newpassword'));
$this->assertTrue($db->selectOne('id', $db->table('users'), array('mail' => 'testmail', 'pw' => md5('newpassword'))));
$this->assertTrue($db->deleteUser($user));
* tests update of user data
* checks function & result
$conf = $GLOBALS['conf'];
$where = array('mail' => $user, 'pw' => md5('testpass'), 'name' => 'dummy');
$this->assertTrue($db->createUser($user, 'testpass'));
$this->assertTrue($db->selectOne('id', $db->table('users'), $where));
$what = array('name' => 'newname');
$where['name'] = 'newname';
$this->assertTrue($db->updateUser($user, $what));
$this->assertTrue($db->selectOne('id', $db->table('users'), $where));
$what = array('pw' => 'newpass');
$where['pw'] = md5('newpass');
$this->assertTrue($db->updateUser($user, $what));
$this->assertTrue($db->selectOne('id', $db->table('users'), $where));
$what = array('mail' => 'newmail');
$where['mail'] = 'newmail';
$this->assertTrue($db->updateUser($user, $what));
$this->assertTrue($db->selectOne('id', $db->table('users'), $where));
$this->assertFalse($db->deleteUser('testmail'));
$this->assertTrue($db->deleteUser('newmail'));
* tests deleting of user data
* checks function & result
$conf = $GLOBALS['conf'];
$where = array('mail' => $user, 'pw' => md5('testpass'), 'name' => 'dummy');
$this->assertTrue($db->createUser($user, 'testpass'));
$this->assertTrue($user = $db->selectOne('id, mail', $db->table('users'), $where));
$vars = array('var1' => 'val1', 'var2' => 'val2');
$this->assertTrue($db->updateUserConf($user['id'], $vars));
$data['txtNewClientName'] = 'nameTest';
$db->user['id'] = $user['id'];
$this->assertTrue($clientID = $db->createClient($data));
$this->assertTrue($db->updateClientConf($clientID, $vars));
$this->assertTrue($db->deleteUser($user['mail']));
// everything should be deleted
$this->assertFalse($db->isUser($user['mail']));
$this->assertFalse($db->select('*', $db->table('userconfig'), array('userID' => $user['id'])));
$this->assertFalse($db->select('*', $db->table('clients'), array('owner' => $user['id'])));
$this->assertFalse($db->select('*', $db->table('clientconfig'), array('clientID' => $clientID)));
* tests fetching user configuration data
$conf = $GLOBALS['conf'];
$table = $db->table('config');
$where = "(context = 'user')";
$result1 = $db->getUserConf();
$result2 = $db->select('id', $table, $where);
$this->assertEqual(count($result1), count($result2));
$db->user['mail'] = 'testdummy';
$where .= " and (rights = 'user')";
$result1 = $db->getUserConf();
$result2 = $db->select('id', $table, $where);
$this->assertEqual(count($result1), count($result2));
* tests update of user configuration data
* - old variables with old values
* - old variables with new values
$conf = $GLOBALS['conf'];
$this->assertTrue($db->createUser('testmail', 'testpass'));
$this->assertTrue($user = $db->selectOne('id', $db->table('users'), array('mail' => 'testmail')));
$table = $db->table('userconfig');
$where = array('userID' => $user['id']);
$vars = array('var1' => 'val1', 'var2' => 'val2');
$this->assertTrue($db->updateUserConf($user['id'], $vars));
$this->assertTrue($result = $db->select('*', $table, $where));
foreach ($result as $item) {
$this->assertTrue(isset ($vars[$item['name']]));
$this->assertEqual($vars[$item['name']], $item['value']);
// old variables, old values
$vars = array('var1' => 'val1', 'var2' => 'val2');
$this->assertTrue($db->updateUserConf($user['id'], $vars));
$this->assertTrue($result = $db->select('*', $table, $where));
foreach ($result as $item) {
$this->assertTrue(isset ($vars[$item['name']]));
$this->assertEqual($vars[$item['name']], $item['value']);
// old variables, new values
$vars = array('var1' => 'newval1', 'var2' => 'newval2');
$this->assertTrue($db->updateUserConf($user['id'], $vars));
$this->assertTrue($result = $db->select('*', $table, $where));
foreach ($result as $item) {
$this->assertTrue(isset ($vars[$item['name']]));
$this->assertEqual($vars[$item['name']], $item['value']);
$this->assertTrue($db->deleteUser('testmail'));
$conf = $GLOBALS['conf'];
$data['txtNewClientName'] = 'nameTest';
$conf['dsnNewClientDB'] = 'dsnTest';
// create client with default user
$this->assertTrue($id = $db->createClient($data));
$table = $db->table('clients');
$where = array('id' => $id);
$this->assertTrue($result = $db->selectOne('*', $table, $where));
$this->assertEqual($result['name'], $data['txtNewClientName']);
$this->assertEqual($result['owner'], $user['id']);
$this->assertEqual($db->decrypt($result['db']), $conf['dsnNewClientDB']);
//$rTime = $db->_db->UnixTimeStamp($result['lastModified']);
//$this->assertTrue($time - 100 < $rTime && $time + 100 > $rTime);
$this->assertTrue($db->deleteClient($id));
// create client with specific user
$this->assertTrue($id = $db->createClient($data, $user));
$table = $db->table('clients');
$where = array('id' => $id);
$this->assertTrue($result = $db->selectOne('*', $table, $where));
$this->assertEqual($result['name'], $data['txtNewClientName']);
$this->assertEqual($result['owner'], $user['id']);
$this->assertEqual($db->decrypt($result['db']), $conf['dsnNewClientDB']);
//$rTime = $db->_db->UnixTimeStamp($result['lastModified']);
//$this->assertTrue($time - 100 < $rTime && $time + 100 > $rTime);
$this->assertTrue($db->deleteClient($id));
* tests deleting of client data
* checks function & result
$conf = $GLOBALS['conf'];
$vars = array('var1' => 'val1', 'var2' => 'val2');
$data['txtNewClientName'] = 'nameTest';
$this->assertTrue($id = $db->createClient($data));
$this->assertTrue($db->updateClientConf($id, $vars));
$this->assertTrue($db->deleteClient($id));
// everything should be deleted
$this->assertFalse($db->select('*', $db->table('clients'), array('id' => $id)));
$this->assertFalse($db->select('*', $db->table('clientconfig'), array('clientID' => $id)));
//$this->_dbDebug = true;
$conf = $GLOBALS['conf'];
// no clients created yet
$this->assertFalse($db->getClients($in));
// create first client ('2' to check database sorting)
$data['txtNewClientName'] = 'nameTest2';
$conf['dsnNewClientDB'] = 'dsnTest2';
$this->assertTrue($id = $db->createClient($data));
$data['txtNewClientName'] = 'nameTest1';
$conf['dsnNewClientDB'] = 'dsnTest1';
$this->assertTrue($id = $db->createClient($data));
$this->assertTrue($db->getClients($in));
// right number of clients
$opt = $in->options['clients'];
$this->assertEqual(count($opt), 2);
$this->assertEqual(count($clients), 2);
$this->assertEqual($first, 'nameTest1');
$first = reset($clients);
$this->assertEqual($first['name'], 'nameTest1');
$this->assertEqual($first['db'], 'dsnTest1');
foreach ($clients as $client)
$this->assertTrue($db->deleteClient($client['id']));
* tests update of client data
* checks function & result
$conf = $GLOBALS['conf'];
$data['txtNewClientName'] = 'nameTest';
$conf['dsnNewClientDB'] = 'dsnTest';
$this->assertTrue($id = $db->createClient($data));
$newData['name'] = 'newTestName';
$this->assertTrue($db->updateClient($id, $newData));
$table = $db->table('clients');
$where = array('id' => $id);
$this->assertTrue($result = $db->selectOne('*', $table, $where));
$this->assertEqual($result['name'], $newData['name']);
$this->assertTrue($db->deleteClient($id));
* tests fetching client configuration data
$conf = $GLOBALS['conf'];
$table = $db->table('config');
$where = "(context = 'client')";
$result2 = $db->select('id', $table, $where);
$this->assertEqual(count($result1), count($result2));
$where .= " and (rights = 'owner')";
$result1 = $db->getClientConf('testdummy');
$result2 = $db->select('id', $table, $where);
$this->assertEqual(count($result1), count($result2));
* tests loading client configuration data into configuration
$conf = $GLOBALS['conf'];
// no client produces no result
$this->assertFalse($db->loadClientConf(false));
$data['txtNewClientName'] = 'nameTest';
$this->assertTrue($id = $db->createClient($data));
$vars = array('var1' => 'val1', 'var2' => 'val2');
$this->assertTrue($db->updateClientConf($id, $vars));
$this->assertTrue($db->loadClientConf($id));
foreach ($vars as $key => $val) {
$this->assertTrue(isset ($conf[$key]));
$this->assertEqual($conf[$key], $val);
$this->assertTrue($db->deleteClient($id));
* tests update of client configuration data
* - old variables with old values
* - old variables with new values
$conf = $GLOBALS['conf'];
$data['txtNewClientName'] = 'nameTest';
$this->assertTrue($id = $db->createClient($data));
$table = $db->table('clientconfig');
$where = array('clientID' => $id);
$vars = array('var1' => 'val1', 'var2' => 'val2');
$this->assertTrue($db->updateClientConf($id, $vars));
$this->assertTrue($result = $db->select('*', $table, $where));
foreach ($result as $item) {
$this->assertTrue(isset ($vars[$item['name']]));
$this->assertEqual($vars[$item['name']], $item['value']);
// old variables, old values
$vars = array('var1' => 'val1', 'var2' => 'val2');
$this->assertTrue($db->updateClientConf($id, $vars));
$this->assertTrue($result = $db->select('*', $table, $where));
foreach ($result as $item) {
$this->assertTrue(isset ($vars[$item['name']]));
$this->assertEqual($vars[$item['name']], $item['value']);
// old variables, new values
$vars = array('var1' => 'newval1', 'var2' => 'newval2');
$this->assertTrue($db->updateClientConf($id, $vars));
$this->assertTrue($result = $db->select('*', $table, $where));
foreach ($result as $item) {
$this->assertTrue(isset ($vars[$item['name']]));
$this->assertEqual($vars[$item['name']], $item['value']);
$this->assertTrue($db->deleteClient($id));
* tests adding, checking, deleteing users assigned to clients & owner check
$conf = $GLOBALS['conf'];
$data['txtNewClientName'] = 'nameTest';
$this->assertTrue($id = $db->createClient($data));
$users = array('newUser1', 'newUser2', 'newUser3', 'newUser4');
foreach ($users as $user)
$this->assertTrue($db->createUser($user, 'testpass'));
$this->assertFalse($db->addUserToClient($id, 'notexisting'));
foreach ($users as $user)
$this->assertTrue($db->addUserToClient($id, $user));
$this->assertTrue($result = $db->getClientUsers($id));
$this->assertEqual(count($result), count($users));
foreach ($users as $user)
$this->assertTrue(in_array($user, $result));
foreach ($result as $key => $user) {
$this->assertFalse($db->isOwner($id));
$db->clients[$id]['owner'] = $key;
$this->assertTrue($db->isOwner($id));
$this->assertTrue($db->isOwner(0));
// double entries are not entered
foreach ($users as $user)
$this->assertTrue($db->addUserToClient($id, $user));
$this->assertTrue($result = $db->getClientUsers($id));
$this->assertEqual(count($result), count($users));
// remove users & check if successful
foreach ($result as $key => $user)
$this->assertTrue($db->delUserClient($id, $key));
$this->assertFalse($db->getClientUsers($id));
foreach ($users as $user)
$this->assertTrue($db->deleteUser($user));
$this->assertTrue($db->deleteClient($id));
* tests fetching system configuration data
$conf = $GLOBALS['conf'];
$table = $db->table('config');
$where = "(context = 'system')";
$result1 = $db->getSysConf();
$result2 = $db->select('id', $table, $where);
$this->assertEqual(count($result1), count($result2));
$db->user['mail'] = 'testdummy';
$this->assertFalse($db->getSysConf());
* tests update of system configuration data
* - old variables with old values
* - old variables with new values
$conf = $GLOBALS['conf'];
$this->assertFalse($db->updateSysConf('wrong'));
$this->assertFalse($db->updateSysConf(array()));
$table = $db->table('config');
$vars = array('txtMailServer', 'numMailPort');
foreach ($vars as $var) {
$old[$var] = $conf[$var];
$where .= $next . "(name = '" . $var . "')";
$this->assertTrue($db->updateSysConf($old));
$this->assertTrue($result = $db->select('name, value', $table, $where));
foreach ($result as $item) {
$this->assertTrue(isset ($checked[$item['name']]));
$this->assertEqual($item['value'], $checked[$item['name']]);
unset ($checked[$item['name']]);
$this->assertFalse($checked);
foreach ($old as $key => $val) $new[$key] = $val . '123';
$db->user['mail'] = 'testdummy';
$this->assertFalse($db->updateSysConf($new));
$this->assertTrue($db->updateSysConf($new));
$this->assertTrue($result = $db->select('name, value', $table, $where));
foreach ($result as $item) {
$this->assertTrue(isset ($checked[$item['name']]));
$this->assertEqual($item['value'], $checked[$item['name']]);
unset ($checked[$item['name']]);
$this->assertFalse($checked);
$this->assertTrue($db->updateSysConf($old));
$this->assertTrue($result = $db->select('name, value', $table, $where));
foreach ($result as $item) {
$this->assertTrue(isset ($checked[$item['name']]));
$this->assertEqual($item['value'], $checked[$item['name']]);
unset ($checked[$item['name']]);
$this->assertFalse($checked);
* checks verious types of selects
$conf = $GLOBALS['conf'];
$this->assertTrue($db->select($what));
// if db is empty prepare it
$schema = new adoSchema($db->_db);
$schema->ParseSchema('admin/sql/simpletest.sql');
$schema->ContinueOnError(true);
$schema->sqlArray = str_replace('simpletest', $db->_prefix . 'simpletest', $schema->sqlArray);
$schema->ExecuteSchema();
$table = $db->table('simpletest');
$this->assertTrue($result = $db->select($what, $table));
$this->assertEqual(count($result), 2);
$result = array('id' => 1, 'name' => 'Test1');
$this->assertTrue($result = $db->select($what, $table));
foreach ($result as $item)
$this->assertTrue(isset ($item['name']));
$expected = array(0 => array('id' => 2, 'name' => 'Test2'));
$this->assertTrue($result = $db->select($what, $table, $where));
foreach ($expected as $key1 => $val1) foreach ($val1 as $key2 => $val2)
$this->assertEqual($result[$key1][$key2], $val2);
$this->assertTrue($result = $db->select($what, $table, '', $sort));
foreach ($result as $item)
if ($i === false) $i = $item['id'];
$this->assertTrue($i > $item['id']);
* checks verious types of selects
$conf = $GLOBALS['conf'];
$this->assertTrue($db->selectOne($what), 'what only');
$table = $db->table('simpletest');
$result = array('id' => 1);
$this->assertEqual($db->selectOne($what, $table), $result, 'no where');
$expected = array('id' => 1, 'name' => 'Test1');
$this->assertTrue($result = $db->selectOne($what, $table));
foreach ($expected as $key1 => $val1)
$this->assertEqual($result[$key1], $val1);
$this->assertTrue(isset ($result['lastModified']));
$expected = array('id' => 2, 'name' => 'Test2');
$this->assertTrue($result = $db->selectOne($what, $table, $where));
foreach ($expected as $key1 => $val1)
$this->assertEqual($result[$key1], $val1);
$result = array('id' => 2, 'name' => 'Test2');
$expected = array('id' => 2, 'name' => 'Test2');
$this->assertTrue($result = $db->selectOne($what, $table, $where, $sort));
foreach ($expected as $key1 => $val1)
$this->assertEqual($result[$key1], $val1);
* compares escaped string with mysql escaped string
$conf = $GLOBALS['conf'];
$string = "\x00, \n, \r, \, ', \" and \x1a.";
$this->assertEqual($db->escape($string), "'" . $result . "'");
$this->assertEqual($db->escape($string, true), $result);
* tests encryption & decryption function
* checks encryption and decryption of string with default, wrong & valid keys
$conf = $GLOBALS['conf'];
$this->assertTrue($result = $db->encrypt($string, $key));
$this->assertNotEqual($result, $string);
$this->assertEqual($string, $db->decrypt($result, $key));
$this->assertNotEqual($string, $db->decrypt($result, 'wrong'));
$this->assertTrue($result = $db->encrypt($string));
$this->assertNotEqual($result, $string);
$this->assertEqual($string, $db->decrypt($result));
$this->assertNotEqual($string, $db->decrypt($result, $key));
$conf = $GLOBALS['conf'];
$this->assertEqual($table, $db->table($table));
$db->_prefix = 'testPrefix';
$this->assertEqual($db->_prefix . $table, $db->table($table));
* checks valid insert, dublicate entry, nonexsiting table
$conf = $GLOBALS['conf'];
$table = $db->table('simpletest');
$what = array('id' => 3, 'name' => 'inserttest', 'lastModified' => $db->now());
$this->assertTrue($db->insert($what, $table), 'insert');
$GLOBALS['ADODB_OUTP'] = 'eaHideADOdbErrors';
$what = array('id' => 3, 'name' => 'inserttest', 'lastModified' => $db->now());
$this->assertFalse($db->insert($what, $table), 'dublicated id');
$this->assertFalse($db->insert($what, $table . 'wrong'), 'nonexisting table');
unset ($GLOBALS['ADODB_OUTP']);
$this->assertTrue($result = $db->selectOne($whatS, $table, $where));
foreach ($what as $key => $val)
$this->assertEqual($result[$key], $val);
$db->delete($table, $where);
* checks valid & invalid updates
$conf = $GLOBALS['conf'];
$table = $db->table('simpletest');
$what = array('id' => 3, 'name' => 'testname', 'lastModified' => $db->now());
$this->assertTrue($db->insert($what, $table));
$what['name'] = 'newname';
$this->assertTrue($db->update($what, $table, $where));
$this->assertTrue($db->selectOne('id', $table, $what));
$this->assertTrue($db->update("name = 'newname2'", $table, 'id = 3'));
$where['name'] = 'newname2';
$this->assertTrue($db->selectOne('id', $table, $where));
$this->assertFalse($db->update("namewrong = 'newname2'", $table, 'id = 3'));
$where['name'] = 'newname2';
$this->assertTrue($db->selectOne('id', $table, $where));
$db->delete($table, $where);
* checks valid delete, double delete, nonexsiting table
$conf = $GLOBALS['conf'];
$table = $db->table('simpletest');
$what = array('id' => 3, 'name' => 'inserttest', 'lastModified' => $db->now());
$this->assertTrue($db->insert($what, $table));
$this->assertTrue($db->delete($table, $where), 'deleted');
$this->assertFalse($db->selectOne('*', $table, $where), 'really deleted');
$this->assertTrue($db->delete($table, $where), 'deleted twice');
$GLOBALS['ADODB_OUTP'] = 'eaHideADOdbErrors';
$this->assertFalse($db->delete($table . 'wrong', $where), 'wrong table');
unset ($GLOBALS['ADODB_OUTP']);
* checks autoincrement functionality of database and lastId function
$conf = $GLOBALS['conf'];
$table = $db->table('simpletest');
$what = array('name' => 'lastIDtest', 'lastModified' => $db->now());
$db->insert($what, $table);
$this->assertTrue($id1 = $db->lastID());
$db->insert($what, $table);
$this->assertTrue($id2 = $db->lastID());
$this->assertTrue($id1 < $id2);
$db->delete($table, $what);
* checks dropping the table
$conf = $GLOBALS['conf'];
$table = $db->table('simpletest');
$this->assertTrue($db->selectOne($what, $table, $where));
$this->assertTrue($db->drop($table));
// table not there any more
$this->assertFalse($db->selectOne($what, $table, $where));
// create the table again
$schema = new adoSchema($db->_db);
$schema->ParseSchema('admin/sql/simpletest.sql');
$schema->ContinueOnError(true);
$schema->sqlArray = str_replace($table, $db->_prefix . 'simpletest', $schema->sqlArray);
$schema->ExecuteSchema();
* checks inserting time fields
$conf = $GLOBALS['conf'];
$table = $db->table('simpletest');
$where = array('id' => 3);
$this->assertTrue($what['lastModified'] = $db->now());
$this->assertTrue($db->insert($what, $table));
$this->assertTrue($result = $db->selectOne('lastModified', $table, $where));
$this->assertEqual($result['lastModified'], $what['lastModified']);
$this->assertTrue($what['lastModified'] = $db->time(time()));
$this->assertTrue($db->update($what, $table, $where));
$this->assertTrue($result = $db->selectOne('lastModified', $table, $where));
$this->assertEqual($result['lastModified'], $what['lastModified']);
$this->assertTrue($what['lastModified'] = $db->time('1803-02-12 12:03:15'));
$this->assertTrue($db->update($what, $table, $where));
$this->assertTrue($result = $db->selectOne('lastModified', $table, $where));
$this->assertEqual($result['lastModified'], $what['lastModified']);
$this->assertTrue($db->delete($table, $where));
* checks inserting time fields
$conf = $GLOBALS['conf'];
$table = $db->table('simpletest');
$what['maxField'] = str_pad('y', 1024 * 128, 'x');
$db->insert($what, $table);
$result = $db->selectOne('*', $table, $where);
echo strlen($result['maxField']);
* we trigger them, so we do not need to log them
* tast case for db wrapper (sessions)
* enable debugging of ADOdb
$this->WebTestCase('Test Database Wrapper (Session)');
// some servers (windows) have problems finding localhost
if ($_SERVER['SERVER_NAME'] == 'localhost') $server = '127.0.0.1';
else $server = $_SERVER['SERVER_NAME'];
$this->_dir = 'http://' . $server . ':' . $_SERVER['SERVER_PORT'] . dirname($_SERVER['PHP_SELF']) . '/base/files/';
* open first page set value
* open second page check value
* open second page but change session name
$this->assertTrue($this->get($this->_dir . 'sessionStart.php'));
$this->assertTitle('session start');
// using get forces cookies
$this->assertTrue($this->get($this->_dir . 'sessionCheck.php'), 'page loaded');
$this->assertTitle('session check', 'correct page');
$this->assertText('var1=value1', 'variable delivered');
$this->assertTrue($this->get($this->_dir . 'sessionCheck.php?wrongSession=true'), 'page loaded');
$this->assertTitle('session check', 'correct page');
$this->assertNoText('var1=value1', 'variable not delivered');
* tests session without cookies
* open first page set value
* click on link for second page without SID -> fail
* click on link for second page with SID -> ok
$this->assertTrue($this->get($this->_dir . 'sessionStart.php'), 'page loaded');
$this->assertTitle('session start', 'correct page');
$this->assertTrue($this->clickLinkById('sessionCheck'), 'page loaded');
$this->assertTitle('session check', 'correct page');
$this->assertNoText('var1=value1', 'variable not delivered');
$this->assertTrue($this->clickLinkById('sessionCheckSID'), 'page loaded');
$this->assertTitle('session check', 'correct page');
$this->assertText('var1=value1', 'variable delivered');
|