After a few tests based on the code (for a different problem), found in an old posting here, I can conclude that “Test Mode” is currently broken in 1.7.2.
Example similar to above linked old posting:
1) Create a mysql table called Customers with 3 columns CustomerID, CustomerEmail and CustomerName. Make sure the table is InnoDB (if using mySQL). [‘db_debug’] can be true or false, does not matter, if set to off, you’ll see the echo lines better.
Create table Customers (
CustomerID Int NOT NULL AUTO_INCREMENT,
CustomerName Char(20) NOT NULL,
CustomerEmail Char(100) NOT NULL,
UNIQUE (CustomerID),
Primary Key (CustomerID)) ENGINE = InnoDB;
2) copy the code below to your controllers directory.
test.php
<?php
class Test extends Controller {
function Test()
{
parent::Controller();
}
function dbtest($rollback)
{
$this->db->trans_begin();
$this->db->insert('customers',array('CustomerEmail' => 'e1',
'CustomerName' => 'n1'));
$this->db->insert('customers',array('CustomerEmail' => 'e2',
'CustomerName' => 'n2'));
if ($rollback) {
$this->db->insert('customers',array('CAUSE ROLLBACK' => 'e2',
'CustomerName' => 'n2'));
}
if ($this->db->trans_status()===FALSE)
{
$this->db->trans_rollback();
echo "rollback";
return True;
} else {
$this->db->trans_commit();
echo "commit";
return False;
}
}
// ciKD
function dbtest_auto($rollback)
{
$this->db->trans_start();
$this->db->insert('customers',array('CustomerEmail' => 'auto_e1_' . $rollback,
'CustomerName' => 'auto_n1_' . $rollback));
$this->db->insert('customers',array('CustomerEmail' => 'auto_e2_' . $rollback,
'CustomerName' => 'auto_n2_' . $rollback));
if ($rollback) {
$this->db->insert('customers',array('CAUSE ROLLBACK' => 'auto_e2_' . $rollback,
'CustomerName' => 'auto_n2_' . $rollback));
}
$this->db->trans_complete();
// if setting $db['default']['db_debug'] = TRUE; then we actually never get here
// because $rollback causes db_error display, but the transaction itself works as
// expected, rollback of first two is obviously done fine
echo "auto…
";
if ($this->db->trans_status()===FALSE)
{
echo "auto rollback";
return True;
} else {
echo "auto commit";
return False;
}
}
// ciKD
function dbtest_testmode($testmode)
{
$this->db->trans_start(($testmode == 1) ? TRUE : FALSE);
$this->db->insert('customers',array('CustomerEmail' => 'testmode_e1_' .$testmode,
'CustomerName' => 'testmode_n1_' . $testmode));
$this->db->insert('customers',array('CustomerEmail' => 'testmode_e2_' . $testmode,
'CustomerName' => 'testmode_n2_' . $testmode));
$this->db->trans_complete();
echo "testmode $testmode ... ";
if ($testmode) {
echo "all should be rolled back now, is it? check your table… ";
}
if ($this->db->trans_status()===FALSE)
{
echo "trans_status=FALSE";
return True;
} else {
echo "trans_status=TRUE";
return False;
}
}
}
?>
3) Try calling the controller http://localhost/.../index.php/test/dbtest/1 nothing should be inserted. Then try http://localhost/.../index.php/test/dbtest/0 and two row should be inserted, trans_begin() and manual trans_rollback() and trans_commit() is used here.
4) Try calling the controller http://localhost/.../index.php/test/dbtest_auto/1 nothing should be inserted. Then try http://localhost/.../index.php/test/dbtest_auto/0 and two row should be inserted, trans_start() and trans_complete() is used here.
5) Try calling the controller http://localhost/.../index.php/test/dbtest_testmode/1 nothing should be inserted. Then try http://localhost/.../index.php/test/dbtest_testmode/0 and two row should be inserted, trans_start() and trans_complete() is used here.
With current 1.7.2 case 5) fails, rows are inserted and not rolled back!
A very old open bug-report from (11/29/2008) exists for 1.7.0 with a solution, which obviously would also work with 1.7.2 (if inserted starting line 528), but as this report is very old, I will report a new bug now. Edit: done
Edit: @WanWizard, thanks for your confirmation!