Subversion Repositories oidplus

Compare Revisions

No changes between revisions

Regard whitespace Rev 373 → Rev 374

/trunk/dev/test_database_plugins
0,0 → 1,275
#!/usr/bin/php
<?php
 
/*
* OIDplus 2.0
* Copyright 2019 Daniel Marschall, ViaThinkSoft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
// This script is used to detect problems with your database plugin
// ATTENTION: PLEASE DO NOT USE THIS SCRIPT ON A PRODUCTIVE DATABASE,
// BECAUSE IT ADDS AND CHANGES DATA DURING THE TESTING.
 
# ---
 
require_once __DIR__ . '/../includes/oidplus.inc.php';
 
$num_errs = 0;
$num_succ = 0;
 
echo "OIDplus Database plugin testcases\n\n";
 
OIDplus::init(true, true);
 
# Test MySQL
OIDplus::baseConfig()->setValue('DATABASE_PLUGIN', 'MySQL');
OIDplus::init(true, true);
$db = OIDplus::db();
OIDplus::init(true, true);
if (function_exists('mysqli_fetch_all')) {
OIDplus::baseConfig()->setValue('MYSQL_FORCE_MYSQLND_SUPPLEMENT', false);
echo "[With MySQLnd support] ";
dotest($db);
OIDplus::baseConfig()->setValue('MYSQL_FORCE_MYSQLND_SUPPLEMENT', true);
}
echo "[Without MySQLnd support] ";
dotest($db);
 
# Test PDO
OIDplus::baseConfig()->setValue('DATABASE_PLUGIN', 'PDO');
OIDplus::init(true, true);
$db = OIDplus::db();
dotest($db);
 
# Test ODBC
OIDplus::baseConfig()->setValue('DATABASE_PLUGIN', 'ODBC');
OIDplus::init(true, true);
$db = OIDplus::db();
dotest($db);
 
# Test PgSQL
OIDplus::baseConfig()->setValue('DATABASE_PLUGIN', 'PgSQL');
OIDplus::init(true, true);
$db = OIDplus::db();
dotest($db);
 
# Test SQLite3
OIDplus::baseConfig()->setValue('DATABASE_PLUGIN', 'SQLite');
OIDplus::init(true, true);
$db = OIDplus::db();
dotest($db);
 
# ---
 
exit($num_errs > 0 ? 1 : 0);
 
# ---
 
function dotest($db) {
echo "Database: " . get_class($db) . "\n";
try {
$db->connect();
} catch (Exception $e) {
echo "Connection ".redtext("FAILED")." (check userdata/baseconfig/config.inc.php): ".$e->getMessage()."\n\n";
return;
}
echo "Detected slang: " . $db->getSlang()::id()."\n";
$db->query("delete from ###objects where parent = 'test:1'");
$db->query("insert into ###objects (id, parent, title, description, confidential) values ('test:1.1', 'test:1', '', '', '0')");
$db->query("insert into ###objects (id, parent, title, description, confidential) values ('test:1.2', 'test:1', '', '', '0')");
try {
// --- "SQL Date" handling
 
try {
$res = $db->query("update ###objects set created = ".$db->sqlDate()." where id = 'test:1.1'");
echo "SQLDate (".$db->sqlDate().') '.greentext('PASSED')."\n";
} catch (Exception $e) {
echo "SQLDate (".$db->sqlDate().') '.redtext('FAILED')."\n";
}
 
// --- "Num rows" handling
 
$res = $db->query("select id from ###objects where parent = ? order by id", array('test:1'));
 
$num_rows = $res->num_rows();
echo "Num rows: " . ($num_rows===2 ? greentext('PASSED') : redtext('FAILED'))."\n";
 
$res->fetch_array();
$num_rows = $res->num_rows();
echo "Num rows after something fetched: " . ($num_rows===2 ? greentext('PASSED') : redtext('FAILED'))."\n";
 
$nextid = $res->fetch_array()['id'];
echo "Num rows does not change cursor: " . ($nextid == 'test:1.2' ? greentext('PASSED') : redtext('FAILED'))."\n";
 
$next = $res->fetch_array();
echo "Fetch after EOF gives null: " . (is_null($next) ? greentext('PASSED') : redtext('FAILED'))."\n";
 
// --- Simultanous prepared statements
 
$res = $db->query("select id from ###objects where parent = ? order by id", array('test:1'));
 
$passed = false;
while ($row = $res->fetch_array()) {
$res2 = $db->query("select id from ###objects where parent = ? order by id", array($row['id']));
while ($row2 = $res2->fetch_array()) {
}
if ($row['id'] == 'test:1.2') {
$passed = true;
}
}
echo "Simultanous prepared statements: ".($passed ? greentext('PASSED') : redtext('FAILED'))."\n";
 
// --- Exception handling
 
try {
$db->query("ABCDEF");
echo "Exception for DirectQuery: ".redtext('FAILED').", no Exception thrown\n";
} catch (Exception $e) {
if (strpos($e->getMessage(), 'ABCDEF') !== false) {
echo "Exception for DirectQuery: ".greentext('PASSED')."\n";
} else {
echo "Exception for DirectQuery: ".redtext('FAILED').", does probably not contain DBMS error string\n";
}
}
 
$msg = $db->error();
if (strpos($msg, 'ABCDEF') !== false) {
echo "Error-Function after failed direct query: ".greentext('PASSED')."\n";
} else {
echo "Error-Function after failed direct query: ".redtext('FAILED').", does probably not contain DBMS error string ($msg)\n";
}
 
try {
$db->query("FEDCBA", array(''));
echo "Exception for PreparedQuery: ".redtext('FAILED').", no Exception thrown\n";
} catch (Exception $e) {
if (strpos($e->getMessage(), 'FEDCBA') !== false) {
echo "Exception for PreparedQuery: ".greentext('PASSED')."\n";
} else {
echo "Exception for PreparedQuery: ".redtext('FAILED').", does probably not contain DBMS error string\n";
}
}
 
$msg = $db->error();
if (strpos($msg, 'FEDCBA') !== false) {
echo "Error-Function after failed prepared query: ".greentext('PASSED')."\n";
} else {
echo "Error-Function after failed prepared query: ".redtext('FAILED').", does probably not contain DBMS error string ($msg)\n";
}
 
$db->query("select 1");
$msg = $db->error();
if (!$msg) {
echo "Error-Function gets cleared after non-failed query: ".greentext('PASSED')."\n";
} else {
echo "Error-Function gets cleared after non-failed query: ".redtext('FAILED').", does probably not contain DBMS error string\n";
}
 
// --- Boolean handling
 
$db->query("update ###objects set confidential = ? where id = 'test:1.1'", array(true));
$res = $db->query("select confidential from ###objects where id = 'test:1.1'");
$val = $res->fetch_object()->confidential;
echo "Boolean handling TRUE with prepared statement: " . ($val ? greentext('PASSED') : redtext('FAILED'))."\n";
 
$db->query("update ###objects set confidential = ? where id = 'test:1.1'", array(false));
$res = $db->query("select confidential from ###objects where id = 'test:1.1'");
$val = $res->fetch_object()->confidential;
echo "Boolean handling FALSE with prepared statement: " . (!$val ? greentext('PASSED') : redtext('FAILED'))."\n";
 
$db->query("update ###objects set confidential = '1' where id = 'test:1.1'");
$res = $db->query("select confidential from ###objects where id = 'test:1.1'");
$val = $res->fetch_object()->confidential;
echo "Boolean handling TRUE with normal statement: " . ($val ? greentext('PASSED') : redtext('FAILED'))."\n";
 
$db->query("update ###objects set confidential = '0' where id = 'test:1.1'");
$res = $db->query("select confidential from ###objects where id = 'test:1.1'");
$val = $res->fetch_object()->confidential;
echo "Boolean handling FALSE with normal statement: " . (!$val ? greentext('PASSED') : redtext('FAILED'))."\n";
 
// --- Check if transactions work
 
$db->query("update ###objects set title = 'A' where id = 'test:1.1'");
$db->transaction_begin();
$db->query("update ###objects set title = 'B' where id = 'test:1.1'");
$db->transaction_rollback();
$res = $db->query("select title from ###objects where id = 'test:1.1'");
$val = $res->fetch_object()->title;
echo "Transaction rollback: " . ($val == 'A' ? greentext('PASSED') : redtext('FAILED'))."\n";
 
$db->query("update ###objects set title = 'A' where id = 'test:1.1'");
$db->transaction_begin();
$db->query("update ###objects set title = 'B' where id = 'test:1.1'");
$db->transaction_commit();
$res = $db->query("select title from ###objects where id = 'test:1.1'");
$val = $res->fetch_object()->title;
echo "Transaction commit: " . ($val == 'B' ? greentext('PASSED') : redtext('FAILED'))."\n";
 
// --- Check natOrder feature
 
$db->query("delete from ###objects where parent = 'test:1'");
$db->query("insert into ###objects (id, parent, title, description, confidential) values ('oid:3.1.10', 'test:1', '', '', '0')");
$db->query("insert into ###objects (id, parent, title, description, confidential) values ('oid:3.1.2', 'test:1', '', '', '0')");
$res = $db->query("select id from ###objects where parent = ? order by ".$db->natOrder('id'), array('test:1'));
$val = $res->fetch_object()->id;
echo "Natural OID Sorting (< 16 Bit): " . ($val == 'oid:3.1.2' ? greentext('PASSED') : redtext('FAILED'))."\n";
 
$db->query("delete from ###objects where parent = 'test:1'");
$db->query("insert into ###objects (id, parent, title, description, confidential) values ('oid:2.25.317919736312109525688528068157180855579', 'test:1', '', '', '0')");
$db->query("insert into ###objects (id, parent, title, description, confidential) values ('oid:2.25.67919736312109525688528068157180855579', 'test:1', '', '', '0')");
$res = $db->query("select id from ###objects where parent = ? order by ".$db->natOrder('id'), array('test:1'));
$val = $res->fetch_object()->id;
echo "Natural OID Sorting (128 Bit): " . ($val == 'oid:2.25.67919736312109525688528068157180855579' ? greentext('PASSED') : redtext('FAILED'))."\n";
 
$db->query("delete from ###objects where parent = 'test:1'");
$db->query("insert into ###objects (id, parent, title, description, confidential) values ('abc:3.1.10', 'test:1', '', '', '0')");
$db->query("insert into ###objects (id, parent, title, description, confidential) values ('abc:3.1.2', 'test:1', '', '', '0')");
$res = $db->query("select id from ###objects where parent = ? order by ".$db->natOrder('id'), array('test:1'));
$val = $res->fetch_object()->id;
echo "Non-Natural Sorting for Non-OIDs: " . ($val == 'abc:3.1.10' ? greentext('PASSED') : redtext('FAILED'))."\n";
 
// --- Test insert_id()
 
$db->query("delete from ###log_object where object = 'test:1'");
$cur = $db->insert_id();
echo "Insert ID on non-insert: " . ($cur == 0 ? greentext('PASSED') : redtext('FAILED'))." ($cur)\n";
$db->query("insert into ###log_object (log_id, object) values (1000, 'test:1')");
$prev = $db->insert_id();
$db->query("insert into ###log_object (log_id, object) values (2000, 'test:1')");
$cur = $db->insert_id();
echo "Insert ID on actual inserts: " . ($cur == $prev+1 ? greentext('PASSED') : redtext('FAILED'))." ($prev => $cur)\n";
if ($cur != $prev+1);
$db->query("delete from ###log_object where object = 'test:1'");
$cur = $db->insert_id();
echo "Non-Insert query will reset insert ID: " . ($cur == 0 ? greentext('PASSED') : redtext('FAILED'))." ($cur)\n";
 
} finally {
$db->query("delete from ###objects where parent = 'test:1'");
}
$db->disconnect();
echo "\n";
}
 
function redtext($str) {
global $num_errs;
$num_errs++;
return "\033[31m$str\033[0m";
}
 
function greentext($str) {
global $num_succ;
$num_succ++;
return "\033[32m$str\033[0m";
}
Property changes:
Added: svn:executable
+*
\ No newline at end of property