2009-10-01 23:04:18 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class TransactionTest extends SapphireTest {
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
function testCreateWithTransaction() {
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
if(DB::getConn()->supportsTransactions()==true){
|
2011-01-12 00:01:42 +01:00
|
|
|
DB::getConn()->transactionStart();
|
2009-10-01 23:04:18 +02:00
|
|
|
$page=new Page();
|
|
|
|
$page->Title='First page';
|
|
|
|
$page->write();
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
$page=new Page();
|
|
|
|
$page->Title='Second page';
|
|
|
|
$page->write();
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
//Create a savepoint here:
|
|
|
|
DB::getConn()->transactionSavepoint('rollback');
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
$page=new Page();
|
|
|
|
$page->Title='Third page';
|
|
|
|
$page->write();
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
$page=new Page();
|
|
|
|
$page->Title='Forth page';
|
|
|
|
$page->write();
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
//Revert to a savepoint:
|
|
|
|
DB::getConn()->transactionRollback('rollback');
|
2011-01-12 00:01:42 +01:00
|
|
|
|
|
|
|
DB::getConn()->transactionEnd();
|
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
$first=DataObject::get('Page', "\"Title\"='First page'");
|
|
|
|
$second=DataObject::get('Page', "\"Title\"='Second page'");
|
|
|
|
$third=DataObject::get('Page', "\"Title\"='Third page'");
|
|
|
|
$forth=DataObject::get('Page', "\"Title\"='Forth page'");
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
//These pages should be in the system
|
|
|
|
$this->assertTrue(is_object($first) && $first->exists());
|
|
|
|
$this->assertTrue(is_object($second) && $second->exists());
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
//These pages should NOT exist, we reverted to a savepoint:
|
|
|
|
$this->assertFalse(is_object($third) && $third->exists());
|
2011-01-12 00:01:42 +01:00
|
|
|
$this->assertFalse(is_object($forth) && $forth->exists());
|
2009-10-01 23:04:18 +02:00
|
|
|
}
|
|
|
|
}
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
function testReadOnlyTransaction(){
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
if(DB::getConn()->supportsTransactions()==true){
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
$page=new Page();
|
|
|
|
$page->Title='Read only success';
|
|
|
|
$page->write();
|
2011-01-12 00:01:42 +01:00
|
|
|
|
|
|
|
DB::getConn()->transactionStart('READ ONLY');
|
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
try {
|
|
|
|
$page=new Page();
|
|
|
|
$page->Title='Read only page failed';
|
|
|
|
$page->write();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
//could not write this record
|
|
|
|
//We need to do a rollback or a commit otherwise we'll get error messages
|
|
|
|
DB::getConn()->transactionRollback();
|
|
|
|
}
|
2011-01-12 00:01:42 +01:00
|
|
|
|
|
|
|
DB::getConn()->transactionEnd();
|
|
|
|
|
|
|
|
DataObject::flush_and_destroy_cache();
|
|
|
|
|
2010-10-13 02:49:57 +02:00
|
|
|
$success=DataObject::get('Page', "\"Title\"='Read only success'");
|
2011-01-12 00:01:42 +01:00
|
|
|
$fail=DataObject::get('Page', "\"Title\"='Read only page failed'");
|
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
//This page should be in the system
|
|
|
|
$this->assertTrue(is_object($success) && $success->exists());
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
//This page should NOT exist, we had 'read only' permissions
|
|
|
|
$this->assertFalse(is_object($fail) && $fail->exists());
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
}
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
}
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
}
|
|
|
|
|
2011-01-12 00:01:42 +01:00
|
|
|
?>
|