Subversion Repositories oidplus

Rev

Rev 277 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. // This script is used to detect problems with your database plugin
  21.  
  22. require_once __DIR__ . '/../../includes/oidplus.inc.php';
  23.  
  24. echo '<h1>OIDplus Database plugin testcases</h1>';
  25.  
  26. # Test MySQL
  27. $db = new OIDplusDatabasePluginMySQLi();
  28. if (function_exists('mysqli_fetch_all')) {
  29.         OIDplus::baseConfig()->setValue('MYSQL_FORCE_MYSQLND_SUPPLEMENT', false);
  30.         echo "[With MySQLnd support] ";
  31.         dotest($db);
  32.         OIDplus::baseConfig()->setValue('MYSQL_FORCE_MYSQLND_SUPPLEMENT', true);
  33. }
  34. echo "[Without MySQLnd support] ";
  35. dotest($db);
  36.  
  37. # Test PDO
  38. $db = new OIDplusDatabasePluginPDO();
  39. dotest($db);
  40.  
  41. # Test ODBC
  42. $db = new OIDplusDatabasePluginODBC();
  43. dotest($db);
  44.  
  45. # Test PgSQL
  46. $db = new OIDplusDatabasePluginPgSQL();
  47. dotest($db);
  48.  
  49. # Test SQLite3
  50. $db = new OIDplusDatabasePluginSQLite3();
  51. dotest($db);
  52.  
  53. # ---
  54.  
  55. function dotest($db) {
  56.         echo "Database: " . $db::id()."<br>";
  57.         try {
  58.                 $db->connect();
  59.         } catch (Exception $e) {
  60.                 echo "Connection <font color=\"red\">FAILED</font> (check userdata/baseconfig/config.inc.php): ".$e->getMessage()."<br><br>";
  61.                 return;
  62.         }
  63.         echo "Detected slang: " . $db->getSlang()::id()."<br>";
  64.         $db->query("delete from ###objects where parent = 'test:1'");
  65.         $db->query("insert into ###objects (id, parent, title, description, confidential) values ('test:1.1', 'test:1', '', '', '0')");
  66.         $db->query("insert into ###objects (id, parent, title, description, confidential) values ('test:1.2', 'test:1', '', '', '0')");
  67.         try {
  68.                 // --- "SQL Date" handling
  69.  
  70.                 try {
  71.                         $res = $db->query("update ###objects set created = ".$db->sqlDate()." where id = 'test:1.1'");
  72.                         echo "SQLDate (".$db->sqlDate().") PASSED<br>";
  73.                 } catch (Exception $e) {
  74.                         echo "SQLDate (".$db->sqlDate().") <font color=\"red\">FAILED</font><br>";
  75.                 }
  76.  
  77.                 // --- "Num rows" handling
  78.  
  79.                 $res = $db->query("select id from ###objects where parent = ? order by id", array('test:1'));
  80.  
  81.                 $num_rows = $res->num_rows();
  82.                 echo "Num rows: " . ($num_rows===2 ? 'PASSED' : '<font color="red">FAILED</font>')."<br>";
  83.  
  84.                 $res->fetch_array();
  85.                 $num_rows = $res->num_rows();
  86.                 echo "Num rows after something fetched: " . ($num_rows===2 ? 'PASSED' : '<font color="red">FAILED</font>')."<br>";
  87.  
  88.                 $nextid = $res->fetch_array()['id'];
  89.                 echo "Num rows does not change cursor: " . ($nextid == 'test:1.2' ? 'PASSED' : '<font color="red">FAILED</font>')."<br>";
  90.  
  91.                 $next = $res->fetch_array();
  92.                 echo "Fetch after EOF gives null: " . (is_null($next) ? 'PASSED' : '<font color="red">FAILED</font>')."<br>";
  93.  
  94.                 // --- Simultanous prepared statements
  95.  
  96.                 $res = $db->query("select id from ###objects where parent = ? order by id", array('test:1'));
  97.  
  98.                 $passed = false;
  99.                 while ($row = $res->fetch_array()) {
  100.                         $res2 = $db->query("select id from ###objects where parent = ? order by id", array($row['id']));
  101.                         while ($row2 = $res2->fetch_array()) {
  102.                         }
  103.                         if ($row['id'] == 'test:1.2') {
  104.                                 $passed = true;
  105.                         }
  106.                 }
  107.                 echo "Simultanous prepared statements: ".($passed ? 'PASSED' : '<font color="red">FAILED</font>')."<br>";
  108.  
  109.                 // --- Exception handling
  110.  
  111.                 try {
  112.                         $db->query("ABCDEF");
  113.                         echo "Exception for DirectQuery: <font color=\"red\">FAILED</font>, no Exception thrown<br>";
  114.                 } catch (Exception $e) {
  115.                         if (strpos($e->getMessage(), 'ABCDEF') !== false) {
  116.                                 echo "Exception for DirectQuery: PASSED<br>";
  117.                         } else {
  118.                                 echo "Exception for DirectQuery: <font color=\"red\">FAILED</font>, does probably not contain DBMS error string<br>";
  119.                         }
  120.                 }
  121.  
  122.                 $msg = $db->error();
  123.                 if (strpos($msg, 'ABCDEF') !== false) {
  124.                         echo "Error-Function after failed direct query: PASSED<br>";
  125.                 } else {
  126.                         echo "Error-Function after failed direct query: <font color=\"red\">FAILED</font>, does probably not contain DBMS error string ($msg)<br>";
  127.                 }
  128.  
  129.                 try {
  130.                         $db->query("FEDCBA", array(''));
  131.                         echo "Exception for PreparedQuery: <font color=\"red\">FAILED</font>, no Exception thrown<br>";
  132.                 } catch (Exception $e) {
  133.                         if (strpos($e->getMessage(), 'FEDCBA') !== false) {
  134.                                 echo "Exception for PreparedQuery: PASSED<br>";
  135.                         } else {
  136.                                 echo "Exception for PreparedQuery: <font color=\"red\">FAILED</font>, does probably not contain DBMS error string<br>";
  137.                         }
  138.                 }
  139.  
  140.                 $msg = $db->error();
  141.                 if (strpos($msg, 'FEDCBA') !== false) {
  142.                         echo "Error-Function after failed prepared query: PASSED<br>";
  143.                 } else {
  144.                         echo "Error-Function after failed prepared query: <font color=\"red\">FAILED</font>, does probably not contain DBMS error string ($msg)<br>";
  145.                 }
  146.  
  147.                 $db->query("select 1");
  148.                 $msg = $db->error();
  149.                 if (!$msg) {
  150.                         echo "Error-Function gets cleared after non-failed query: PASSED<br>";
  151.                 } else {
  152.                         echo "Error-Function gets cleared after non-failed query: <font color=\"red\">FAILED</font>, does probably not contain DBMS error string<br>";
  153.                 }
  154.  
  155.                 // --- Boolean handling
  156.  
  157.                 $db->query("update ###objects set confidential = ? where id = 'test:1.1'", array(true));
  158.                 $res = $db->query("select confidential from ###objects where id = 'test:1.1'");
  159.                 $val = $res->fetch_object()->confidential;
  160.                 echo "Boolean handling TRUE with prepared statement: " . ($val ? 'PASSED' : '<font color="red">FAILED</font>')."<br>";
  161.  
  162.                 $db->query("update ###objects set confidential = ? where id = 'test:1.1'", array(false));
  163.                 $res = $db->query("select confidential from ###objects where id = 'test:1.1'");
  164.                 $val = $res->fetch_object()->confidential;
  165.                 echo "Boolean handling FALSE with prepared statement: " . (!$val ? 'PASSED' : '<font color="red">FAILED</font>')."<br>";
  166.  
  167.                 $db->query("update ###objects set confidential = '1' where id = 'test:1.1'");
  168.                 $res = $db->query("select confidential from ###objects where id = 'test:1.1'");
  169.                 $val = $res->fetch_object()->confidential;
  170.                 echo "Boolean handling TRUE with normal statement: " . ($val ? 'PASSED' : '<font color="red">FAILED</font>')."<br>";
  171.  
  172.                 $db->query("update ###objects set confidential = '0' where id = 'test:1.1'");
  173.                 $res = $db->query("select confidential from ###objects where id = 'test:1.1'");
  174.                 $val = $res->fetch_object()->confidential;
  175.                 echo "Boolean handling FALSE with normal statement: " . (!$val ? 'PASSED' : '<font color="red">FAILED</font>')."<br>";
  176.  
  177.                 // --- Check if transactions work
  178.  
  179.                 $db->query("update ###objects set title = 'A' where id = 'test:1.1'");
  180.                 $db->transaction_begin();
  181.                 $db->query("update ###objects set title = 'B' where id = 'test:1.1'");
  182.                 $db->transaction_rollback();
  183.                 $res = $db->query("select title from ###objects where id = 'test:1.1'");
  184.                 $val = $res->fetch_object()->title;
  185.                 echo "Transaction rollback: " . ($val == 'A' ? 'PASSED' : '<font color="red">FAILED</font>')."<br>";
  186.  
  187.                 $db->query("update ###objects set title = 'A' where id = 'test:1.1'");
  188.                 $db->transaction_begin();
  189.                 $db->query("update ###objects set title = 'B' where id = 'test:1.1'");
  190.                 $db->transaction_commit();
  191.                 $res = $db->query("select title from ###objects where id = 'test:1.1'");
  192.                 $val = $res->fetch_object()->title;
  193.                 echo "Transaction commit: " . ($val == 'B' ? 'PASSED' : '<font color="red">FAILED</font>')."<br>";
  194.  
  195.                 // --- Check natOrder feature
  196.  
  197.                 $db->query("delete from ###objects where parent = 'test:1'");
  198.                 $db->query("insert into ###objects (id, parent, title, description, confidential) values ('oid:3.1.10', 'test:1', '', '', '0')");
  199.                 $db->query("insert into ###objects (id, parent, title, description, confidential) values ('oid:3.1.2', 'test:1', '', '', '0')");
  200.                 $res = $db->query("select id from ###objects where parent = ? order by ".$db->natOrder('id'), array('test:1'));
  201.                 $val = $res->fetch_object()->id;
  202.                 echo "Natural OID Sorting (< 16 Bit): " . ($val == 'oid:3.1.2' ? 'PASSED' : '<font color="red">FAILED</font>')."<br>";
  203.  
  204.                 $db->query("delete from ###objects where parent = 'test:1'");
  205.                 $db->query("insert into ###objects (id, parent, title, description, confidential) values ('oid:2.25.317919736312109525688528068157180855579', 'test:1', '', '', '0')");
  206.                 $db->query("insert into ###objects (id, parent, title, description, confidential) values ('oid:2.25.67919736312109525688528068157180855579', 'test:1', '', '', '0')");
  207.                 $res = $db->query("select id from ###objects where parent = ? order by ".$db->natOrder('id'), array('test:1'));
  208.                 $val = $res->fetch_object()->id;
  209.                 echo "Natural OID Sorting (128 Bit): " . ($val == 'oid:2.25.67919736312109525688528068157180855579' ? 'PASSED' : '<font color="red">FAILED</font>')."<br>";
  210.  
  211.                 $db->query("delete from ###objects where parent = 'test:1'");
  212.                 $db->query("insert into ###objects (id, parent, title, description, confidential) values ('abc:3.1.10', 'test:1', '', '', '0')");
  213.                 $db->query("insert into ###objects (id, parent, title, description, confidential) values ('abc:3.1.2', 'test:1', '', '', '0')");
  214.                 $res = $db->query("select id from ###objects where parent = ? order by ".$db->natOrder('id'), array('test:1'));
  215.                 $val = $res->fetch_object()->id;
  216.                 echo "Non-Natural Sorting for Non-OIDs: " . ($val == 'abc:3.1.10' ? 'PASSED' : '<font color="red">FAILED</font>')."<br>";
  217.  
  218.                 // --- Test insert_id()
  219.  
  220.                 $db->query("delete from ###log_object where object = 'test:1'");
  221.                 $cur = $db->insert_id();
  222.                 echo "Insert ID on non-insert: " . ($cur == 0 ? 'PASSED' : '<font color="red">FAILED</font>')." ($cur)<br>";
  223.                 $db->query("insert into ###log_object (log_id, object) values (1000, 'test:1')");
  224.                 $prev = $db->insert_id();
  225.                 $db->query("insert into ###log_object (log_id, object) values (2000, 'test:1')");
  226.                 $cur = $db->insert_id();
  227.                 echo "Insert ID on actual inserts: " . ($cur == $prev+1 ? 'PASSED' : '<font color="red">FAILED</font>')." ($prev => $cur)<br>";
  228.                 if ($cur != $prev+1);
  229.                 $db->query("delete from ###log_object where object = 'test:1'");
  230.                 $cur = $db->insert_id();
  231.                 echo "Non-Insert query will reset insert ID: " . ($cur == 0 ? 'PASSED' : '<font color="red">FAILED</font>')." ($cur)<br>";
  232.  
  233.         } finally {
  234.                 $db->query("delete from ###objects where parent = 'test:1'");
  235.         }
  236.         $db->disconnect();
  237.         echo "<br>";
  238. }
  239.