2 require_once 'CiviTest/CiviUnitTestCase.php';
3 require_once 'CiviTest/Contact.php';
4 require_once 'CiviTest/Custom.php';
7 * Class CRM_Core_TransactionTest
9 class CRM_Core_TransactionTest
extends CiviUnitTestCase
{
17 * @var array (int $idx => int $contactId) list of contact IDs that have been created (in order of creation)
19 * Note that ID this is all IDs created by the test-case -- even if the creation was subsequently rolled back
23 public function setUp() {
25 $this->quickCleanup(array('civicrm_contact', 'civicrm_activity'));
26 $this->callbackLog
= array();
27 $this->cids
= array();
30 public function dataCreateStyle() {
37 public function dataCreateAndCommitStyles() {
39 array('sql-insert', 'implicit-commit'),
40 array('sql-insert', 'explicit-commit'),
41 array('bao-create', 'implicit-commit'),
42 array('bao-create', 'explicit-commit'),
47 * @param string $createStyle
48 * 'sql-insert'|'bao-create'.
49 * @param string $commitStyle
50 * 'implicit-commit'|'explicit-commit'.
51 * @dataProvider dataCreateAndCommitStyles
53 public function testBasicCommit($createStyle, $commitStyle) {
54 $this->createContactWithTransaction('reuse-tx', $createStyle, $commitStyle);
55 $this->assertCount(1, $this->cids
);
56 $this->assertContactsExistByOffset(array(0 => TRUE));
60 * @dataProvider dataCreateStyle
62 public function testBasicRollback($createStyle) {
63 $this->createContactWithTransaction('reuse-tx', $createStyle, 'rollback');
64 $this->assertCount(1, $this->cids
);
65 $this->assertContactsExistByOffset(array(0 => FALSE));
69 * Test in which an outer function makes multiple calls to inner functions
70 * but then rolls back the entire set.
72 * @param string $createStyle
73 * 'sql-insert'|'bao-create'.
74 * @param string $commitStyle
75 * 'implicit-commit'|'explicit-commit'.
76 * @dataProvider dataCreateAndCommitStyles
78 public function testBatchRollback($createStyle, $commitStyle) {
82 array('reuse-tx', $createStyle, $commitStyle), // cid 0
83 array('reuse-tx', $createStyle, $commitStyle), // cid 1
85 array(0 => TRUE, 1 => TRUE),
88 $this->assertCount(2, $this->cids
);
89 $this->assertContactsExistByOffset(array(0 => FALSE, 1 => FALSE));
93 * Test in which runBatch makes multiple calls to
94 * createContactWithTransaction using a mix of rollback/commit.
95 * createContactWithTransaction use nesting (savepoints), so the
96 * batch is able to commit.
98 * @param string $createStyle
99 * 'sql-insert'|'bao-create'.
100 * @param string $commitStyle
101 * 'implicit-commit'|'explicit-commit'.
102 * @dataProvider dataCreateAndCommitStyles
104 public function testMixedBatchCommit_nesting($createStyle, $commitStyle) {
108 array('nest-tx', $createStyle, $commitStyle), // cid 0
109 array('nest-tx', $createStyle, 'rollback'), // cid 1
110 array('nest-tx', $createStyle, $commitStyle), // cid 2
112 array(0 => TRUE, 1 => FALSE, 2 => TRUE),
115 $this->assertCount(3, $this->cids
);
116 $this->assertContactsExistByOffset(array(0 => TRUE, 1 => FALSE, 2 => TRUE));
120 * Test in which runBatch makes multiple calls to
121 * createContactWithTransaction using a mix of rollback/commit.
122 * createContactWithTransaction reuses the main transaction,
123 * so the overall batch must rollback.
125 * @param string $createStyle
126 * 'sql-insert'|'bao-create'.
127 * @param string $commitStyle
128 * 'implicit-commit'|'explicit-commit'.
129 * @dataProvider dataCreateAndCommitStyles
131 public function testMixedBatchCommit_reuse($createStyle, $commitStyle) {
135 array('reuse-tx', $createStyle, $commitStyle), // cid 0
136 array('reuse-tx', $createStyle, 'rollback'), // cid 1
137 array('reuse-tx', $createStyle, $commitStyle), // cid 2
139 array(0 => TRUE, 1 => TRUE, 2 => TRUE),
142 $this->assertCount(3, $this->cids
);
143 $this->assertContactsExistByOffset(array(0 => FALSE, 1 => FALSE, 2 => FALSE));
147 * Test in which runBatch makes multiple calls to
148 * createContactWithTransaction using a mix of rollback/commit.
149 * The overall batch is rolled back.
151 * @param string $createStyle
152 * 'sql-insert'|'bao-create'.
153 * @param string $commitStyle
154 * 'implicit-commit'|'explicit-commit'.
155 * @dataProvider dataCreateAndCommitStyles
157 public function testMixedBatchRollback_nesting($createStyle, $commitStyle) {
158 $this->assertFalse(CRM_Core_Transaction
::isActive());
162 array('nest-tx', $createStyle, $commitStyle), // cid 0
163 array('nest-tx', $createStyle, 'rollback'), // cid 1
164 array('nest-tx', $createStyle, $commitStyle), // cid 2
166 array(0 => TRUE, 1 => FALSE, 2 => TRUE),
169 $this->assertFalse(CRM_Core_Transaction
::isActive());
170 $this->assertCount(3, $this->cids
);
171 $this->assertContactsExistByOffset(array(0 => FALSE, 1 => FALSE, 2 => FALSE));
174 public function testIsActive() {
175 $this->assertEquals(FALSE, CRM_Core_Transaction
::isActive());
176 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
177 $tx = new CRM_Core_Transaction();
178 $this->assertEquals(TRUE, CRM_Core_Transaction
::isActive());
179 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
181 $this->assertEquals(FALSE, CRM_Core_Transaction
::isActive());
182 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
185 public function testIsActive_rollback() {
186 $this->assertEquals(FALSE, CRM_Core_Transaction
::isActive());
187 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
189 $tx = new CRM_Core_Transaction();
190 $this->assertEquals(TRUE, CRM_Core_Transaction
::isActive());
191 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
194 $this->assertEquals(TRUE, CRM_Core_Transaction
::isActive());
195 $this->assertEquals(FALSE, CRM_Core_Transaction
::willCommit());
198 $this->assertEquals(FALSE, CRM_Core_Transaction
::isActive());
199 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
202 public function testCallback_commit() {
203 $tx = new CRM_Core_Transaction();
205 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_PRE_COMMIT
, array($this, '_preCommit'), array('qwe', 'rty'));
206 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_POST_COMMIT
, array($this, '_postCommit'), array('uio', 'p[]'));
207 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_PRE_ROLLBACK
, array($this, '_preRollback'), array('asd', 'fgh'));
208 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_POST_ROLLBACK
, array($this, '_postRollback'), array('jkl', ';'));
210 CRM_Core_DAO
::executeQuery('UPDATE civicrm_contact SET id = 100 WHERE id = 100');
212 $this->assertEquals(array(), $this->callbackLog
);
214 $this->assertEquals(array('_preCommit', 'qwe', 'rty'), $this->callbackLog
[0]);
215 $this->assertEquals(array('_postCommit', 'uio', 'p[]'), $this->callbackLog
[1]);
218 public function testCallback_rollback() {
219 $tx = new CRM_Core_Transaction();
221 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_PRE_COMMIT
, array($this, '_preCommit'), array('ewq', 'ytr'));
222 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_POST_COMMIT
, array($this, '_postCommit'), array('oiu', '][p'));
223 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_PRE_ROLLBACK
, array($this, '_preRollback'), array('dsa', 'hgf'));
224 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_POST_ROLLBACK
, array($this, '_postRollback'), array('lkj', ';'));
226 CRM_Core_DAO
::executeQuery('UPDATE civicrm_contact SET id = 100 WHERE id = 100');
229 $this->assertEquals(array(), $this->callbackLog
);
231 $this->assertEquals(array('_preRollback', 'dsa', 'hgf'), $this->callbackLog
[0]);
232 $this->assertEquals(array('_postRollback', 'lkj', ';'), $this->callbackLog
[1]);
236 * @param string $createStyle
237 * 'sql-insert'|'bao-create'.
238 * @param string $commitStyle
239 * 'implicit-commit'|'explicit-commit'.
240 * @dataProvider dataCreateAndCommitStyles
242 public function testRun_ok($createStyle, $commitStyle) {
244 CRM_Core_Transaction
::create(TRUE)->run(function($tx) use (&$test, $createStyle, $commitStyle) {
245 $test->createContactWithTransaction('nest-tx', $createStyle, $commitStyle);
246 $test->assertContactsExistByOffset(array(0 => TRUE));
248 $this->assertContactsExistByOffset(array(0 => TRUE));
252 * @param string $createStyle
253 * 'sql-insert'|'bao-create'.
254 * @param string $commitStyle
255 * 'implicit-commit'|'explicit-commit'.
256 * @dataProvider dataCreateAndCommitStyles
258 public function testRun_exception($createStyle, $commitStyle) {
259 $tx = new CRM_Core_Transaction();
261 $e = NULL; // Exception
263 CRM_Core_Transaction
::create(TRUE)->run(function($tx) use (&$test, $createStyle, $commitStyle) {
264 $test->createContactWithTransaction('nest-tx', $createStyle, $commitStyle);
265 $test->assertContactsExistByOffset(array(0 => TRUE));
266 throw new Exception("Ruh-roh");
269 catch (Exception
$ex) {
271 if (get_class($e) != 'Exception' ||
$e->getMessage() != 'Ruh-roh') {
275 $this->assertTrue($e instanceof Exception
);
276 $this->assertContactsExistByOffset(array(0 => FALSE));
283 public function assertContactsExist($cids, $exist = TRUE) {
284 foreach ($cids as $cid) {
285 $this->assertTrue(is_numeric($cid));
286 $this->assertDBQuery($exist ?
1 : 0, 'SELECT count(*) FROM civicrm_contact WHERE id = %1', array(
287 1 => array($cid, 'Integer'),
293 * @param array $existsByOffset
294 * Array(int $cidOffset => bool $expectExists).
295 * @param int $generalOffset
297 public function assertContactsExistByOffset($existsByOffset, $generalOffset = 0) {
298 foreach ($existsByOffset as $offset => $expectExists) {
299 $this->assertTrue(isset($this->cids
[$generalOffset +
$offset]), "Find cid at offset($generalOffset + $offset)");
300 $cid = $this->cids
[$generalOffset +
$offset];
301 $this->assertTrue(is_numeric($cid));
302 $this->assertDBQuery($expectExists ?
1 : 0, 'SELECT count(*) FROM civicrm_contact WHERE id = %1', array(
303 1 => array($cid, 'Integer'),
304 ), "Check contact at offset($generalOffset + $offset)");
309 * Use SQL to INSERT a contact and assert success. Perform
310 * work within a transaction.
312 * @param string $nesting
313 * 'reuse-tx'|'nest-tx' how to construct transaction.
314 * @param string $insert
315 * 'sql-insert'|'bao-create' how to add the example record.
316 * @param string $outcome
317 * 'rollback'|'implicit-commit'|'explicit-commit' how to finish transaction.
321 public function createContactWithTransaction($nesting, $insert, $outcome) {
322 if ($nesting != 'reuse-tx' && $nesting != 'nest-tx') {
323 throw new RuntimeException('Bad test data: reuse=' . $nesting);
325 if ($insert != 'sql-insert' && $insert != 'bao-create') {
326 throw new RuntimeException('Bad test data: insert=' . $insert);
328 if ($outcome != 'rollback' && $outcome != 'implicit-commit' && $outcome != 'explicit-commit') {
329 throw new RuntimeException('Bad test data: outcome=' . $outcome);
332 $tx = new CRM_Core_Transaction($nesting === 'nest-tx');
334 if ($insert == 'sql-insert') {
335 $r = CRM_Core_DAO
::executeQuery("INSERT INTO civicrm_contact(first_name,last_name) VALUES ('ff', 'll')");
336 $cid = mysql_insert_id();
338 elseif ($insert == 'bao-create') {
340 'contact_type' => 'Individual',
341 'first_name' => 'FF',
344 $r = CRM_Contact_BAO_Contact
::create($params);
348 $this->cids
[] = $cid;
350 $this->assertContactsExist(array($cid), TRUE);
352 if ($outcome == 'rollback') {
355 elseif ($outcome == 'explicit-commit') {
357 } // else: implicit-commit
363 * Perform a series of operations within smaller transactions
365 * @param string $nesting
366 * 'reuse-tx'|'nest-tx' how to construct transaction.
367 * @param array $callbacks
368 * See createContactWithTransaction.
369 * @param array $existsByOffset
370 * See assertContactsMix.
371 * @param string $outcome
372 * 'rollback'|'implicit-commit'|'explicit-commit' how to finish transaction.
376 public function runBatch($nesting, $callbacks, $existsByOffset, $outcome) {
377 if ($nesting != 'reuse-tx' && $nesting != 'nest-tx') {
378 throw new RuntimeException('Bad test data: nesting=' . $nesting);
380 if ($outcome != 'rollback' && $outcome != 'implicit-commit' && $outcome != 'explicit-commit') {
381 throw new RuntimeException('Bad test data: outcome=' . $nesting);
384 $tx = new CRM_Core_Transaction($nesting === 'reuse-tx');
386 $generalOffset = count($this->cids
);
387 foreach ($callbacks as $callback) {
388 list ($cbNesting, $cbInsert, $cbOutcome) = $callback;
389 $this->createContactWithTransaction($cbNesting, $cbInsert, $cbOutcome);
392 $this->assertContactsExistByOffset($existsByOffset, $generalOffset);
394 if ($outcome == 'rollback') {
397 elseif ($outcome == 'explicit-commit') {
399 } // else: implicit-commit
402 public function _preCommit($arg1, $arg2) {
403 $this->callbackLog
[] = array('_preCommit', $arg1, $arg2);
406 public function _postCommit($arg1, $arg2) {
407 $this->callbackLog
[] = array('_postCommit', $arg1, $arg2);
410 public function _preRollback($arg1, $arg2) {
411 $this->callbackLog
[] = array('_preRollback', $arg1, $arg2);
414 public function _postRollback($arg1, $arg2) {
415 $this->callbackLog
[] = array('_postRollback', $arg1, $arg2);