From 6d189756b3fcb309a884e5ca5b17437f4daa2c30 Mon Sep 17 00:00:00 2001 From: Geoff Munn Date: Thu, 1 Oct 2009 21:04:18 +0000 Subject: [PATCH] Transaction test created git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@87896 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- tests/TransactionTest.php | 89 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tests/TransactionTest.php diff --git a/tests/TransactionTest.php b/tests/TransactionTest.php new file mode 100644 index 000000000..a77c0c31e --- /dev/null +++ b/tests/TransactionTest.php @@ -0,0 +1,89 @@ +supportsTransactions()==true){ + DB::getConn()->startTransaction(); + $page=new Page(); + $page->Title='First page'; + $page->write(); + + $page=new Page(); + $page->Title='Second page'; + $page->write(); + + //Create a savepoint here: + DB::getConn()->transactionSavepoint('rollback'); + + $page=new Page(); + $page->Title='Third page'; + $page->write(); + + $page=new Page(); + $page->Title='Forth page'; + $page->write(); + + //Revert to a savepoint: + DB::getConn()->transactionRollback('rollback'); + + DB::getConn()->endTransaction(); + + $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'"); + + //These pages should be in the system + $this->assertTrue(is_object($first) && $first->exists()); + $this->assertTrue(is_object($second) && $second->exists()); + + //These pages should NOT exist, we reverted to a savepoint: + $this->assertFalse(is_object($third) && $third->exists()); + $this->assertFalse(is_object($forth) && $forth->exists()); + } + } + + function testReadOnlyTransaction(){ + + if(DB::getConn()->supportsTransactions()==true){ + + $page=new Page(); + $page->Title='Read only success'; + $page->write(); + + DB::getConn()->startTransaction('READ ONLY'); + + 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(); + } + + $success=DataObject::get('Page', "\"Title\"='Read only success'"); + + DB::getConn()->endTransaction(); + + $fail=DataObject::get('Page', "\"Title\"='Read only failed'"); + + //This page should be in the system + $this->assertTrue(is_object($success) && $success->exists()); + + //This page should NOT exist, we had 'read only' permissions + $this->assertFalse(is_object($fail) && $fail->exists()); + + } + + } + +} + +?>