4 * Class CRM_Core_TransactionTest
7 class CRM_Core_TransactionTest
extends CiviUnitTestCase
{
15 * @var array (int $idx => int $contactId) list of contact IDs that have been created (in order of creation)
17 * Note that ID this is all IDs created by the test-case -- even if the creation was subsequently rolled back
21 public function setUp() {
23 $this->quickCleanup(array('civicrm_contact', 'civicrm_activity'));
24 $this->callbackLog
= array();
25 $this->cids
= array();
31 public function dataCreateStyle() {
41 public function dataCreateAndCommitStyles() {
43 array('sql-insert', 'implicit-commit'),
44 array('sql-insert', 'explicit-commit'),
45 array('bao-create', 'implicit-commit'),
46 array('bao-create', 'explicit-commit'),
51 * @param string $createStyle
52 * 'sql-insert'|'bao-create'.
53 * @param string $commitStyle
54 * 'implicit-commit'|'explicit-commit'.
55 * @dataProvider dataCreateAndCommitStyles
57 public function testBasicCommit($createStyle, $commitStyle) {
58 $this->createContactWithTransaction('reuse-tx', $createStyle, $commitStyle);
59 $this->assertCount(1, $this->cids
);
60 $this->assertContactsExistByOffset(array(0 => TRUE));
64 * @dataProvider dataCreateStyle
67 public function testBasicRollback($createStyle) {
68 $this->createContactWithTransaction('reuse-tx', $createStyle, 'rollback');
69 $this->assertCount(1, $this->cids
);
70 $this->assertContactsExistByOffset(array(0 => FALSE));
74 * Test in which an outer function makes multiple calls to inner functions.
75 * but then rolls back the entire set.
77 * @param string $createStyle
78 * 'sql-insert'|'bao-create'.
79 * @param string $commitStyle
80 * 'implicit-commit'|'explicit-commit'.
81 * @dataProvider dataCreateAndCommitStyles
83 public function testBatchRollback($createStyle, $commitStyle) {
87 array('reuse-tx', $createStyle, $commitStyle), // cid 0
88 array('reuse-tx', $createStyle, $commitStyle), // cid 1
90 array(0 => TRUE, 1 => TRUE),
93 $this->assertCount(2, $this->cids
);
94 $this->assertContactsExistByOffset(array(0 => FALSE, 1 => FALSE));
98 * Test in which runBatch makes multiple calls to
99 * createContactWithTransaction using a mix of rollback/commit.
100 * createContactWithTransaction use nesting (savepoints), so the
101 * batch is able to commit.
103 * @param string $createStyle
104 * 'sql-insert'|'bao-create'.
105 * @param string $commitStyle
106 * 'implicit-commit'|'explicit-commit'.
107 * @dataProvider dataCreateAndCommitStyles
109 public function testMixedBatchCommit_nesting($createStyle, $commitStyle) {
113 array('nest-tx', $createStyle, $commitStyle), // cid 0
114 array('nest-tx', $createStyle, 'rollback'), // cid 1
115 array('nest-tx', $createStyle, $commitStyle), // cid 2
117 array(0 => TRUE, 1 => FALSE, 2 => TRUE),
120 $this->assertCount(3, $this->cids
);
121 $this->assertContactsExistByOffset(array(0 => TRUE, 1 => FALSE, 2 => TRUE));
125 * Test in which runBatch makes multiple calls to
126 * createContactWithTransaction using a mix of rollback/commit.
127 * createContactWithTransaction reuses the main transaction,
128 * so the overall batch must rollback.
130 * @param string $createStyle
131 * 'sql-insert'|'bao-create'.
132 * @param string $commitStyle
133 * 'implicit-commit'|'explicit-commit'.
134 * @dataProvider dataCreateAndCommitStyles
136 public function testMixedBatchCommit_reuse($createStyle, $commitStyle) {
140 array('reuse-tx', $createStyle, $commitStyle), // cid 0
141 array('reuse-tx', $createStyle, 'rollback'), // cid 1
142 array('reuse-tx', $createStyle, $commitStyle), // cid 2
144 array(0 => TRUE, 1 => TRUE, 2 => TRUE),
147 $this->assertCount(3, $this->cids
);
148 $this->assertContactsExistByOffset(array(0 => FALSE, 1 => FALSE, 2 => FALSE));
152 * Test in which runBatch makes multiple calls to
153 * createContactWithTransaction using a mix of rollback/commit.
154 * The overall batch is rolled back.
156 * @param string $createStyle
157 * 'sql-insert'|'bao-create'.
158 * @param string $commitStyle
159 * 'implicit-commit'|'explicit-commit'.
160 * @dataProvider dataCreateAndCommitStyles
162 public function testMixedBatchRollback_nesting($createStyle, $commitStyle) {
163 $this->assertFalse(CRM_Core_Transaction
::isActive());
167 array('nest-tx', $createStyle, $commitStyle), // cid 0
168 array('nest-tx', $createStyle, 'rollback'), // cid 1
169 array('nest-tx', $createStyle, $commitStyle), // cid 2
171 array(0 => TRUE, 1 => FALSE, 2 => TRUE),
174 $this->assertFalse(CRM_Core_Transaction
::isActive());
175 $this->assertCount(3, $this->cids
);
176 $this->assertContactsExistByOffset(array(0 => FALSE, 1 => FALSE, 2 => FALSE));
179 public function testIsActive() {
180 $this->assertEquals(FALSE, CRM_Core_Transaction
::isActive());
181 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
182 $tx = new CRM_Core_Transaction();
183 $this->assertEquals(TRUE, CRM_Core_Transaction
::isActive());
184 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
186 $this->assertEquals(FALSE, CRM_Core_Transaction
::isActive());
187 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
190 public function testIsActive_rollback() {
191 $this->assertEquals(FALSE, CRM_Core_Transaction
::isActive());
192 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
194 $tx = new CRM_Core_Transaction();
195 $this->assertEquals(TRUE, CRM_Core_Transaction
::isActive());
196 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
199 $this->assertEquals(TRUE, CRM_Core_Transaction
::isActive());
200 $this->assertEquals(FALSE, CRM_Core_Transaction
::willCommit());
203 $this->assertEquals(FALSE, CRM_Core_Transaction
::isActive());
204 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
207 public function testCallback_commit() {
208 $tx = new CRM_Core_Transaction();
210 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_PRE_COMMIT
, array($this, '_preCommit'), array(
214 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_POST_COMMIT
, array($this, '_postCommit'), array(
218 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_PRE_ROLLBACK
, array(
221 ), array('asd', 'fgh'));
222 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_POST_ROLLBACK
, array(
225 ), array('jkl', ';'));
227 CRM_Core_DAO
::executeQuery('UPDATE civicrm_contact SET id = 100 WHERE id = 100');
229 $this->assertEquals(array(), $this->callbackLog
);
231 $this->assertEquals(array('_preCommit', 'qwe', 'rty'), $this->callbackLog
[0]);
232 $this->assertEquals(array('_postCommit', 'uio', 'p[]'), $this->callbackLog
[1]);
235 public function testCallback_rollback() {
236 $tx = new CRM_Core_Transaction();
238 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_PRE_COMMIT
, array($this, '_preCommit'), array(
242 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_POST_COMMIT
, array($this, '_postCommit'), array(
246 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_PRE_ROLLBACK
, array(
249 ), array('dsa', 'hgf'));
250 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_POST_ROLLBACK
, array(
253 ), array('lkj', ';'));
255 CRM_Core_DAO
::executeQuery('UPDATE civicrm_contact SET id = 100 WHERE id = 100');
258 $this->assertEquals(array(), $this->callbackLog
);
260 $this->assertEquals(array('_preRollback', 'dsa', 'hgf'), $this->callbackLog
[0]);
261 $this->assertEquals(array('_postRollback', 'lkj', ';'), $this->callbackLog
[1]);
265 * @param string $createStyle
266 * 'sql-insert'|'bao-create'.
267 * @param string $commitStyle
268 * 'implicit-commit'|'explicit-commit'.
269 * @dataProvider dataCreateAndCommitStyles
271 public function testRun_ok($createStyle, $commitStyle) {
273 CRM_Core_Transaction
::create(TRUE)->run(function ($tx) use (&$test, $createStyle, $commitStyle) {
274 $test->createContactWithTransaction('nest-tx', $createStyle, $commitStyle);
275 $test->assertContactsExistByOffset(array(0 => TRUE));
277 $this->assertContactsExistByOffset(array(0 => TRUE));
281 * @param string $createStyle
282 * 'sql-insert'|'bao-create'.
283 * @param string $commitStyle
284 * 'implicit-commit'|'explicit-commit'.
285 * @dataProvider dataCreateAndCommitStyles
287 public function testRun_exception($createStyle, $commitStyle) {
288 $tx = new CRM_Core_Transaction();
290 $e = NULL; // Exception
292 CRM_Core_Transaction
::create(TRUE)->run(function ($tx) use (&$test, $createStyle, $commitStyle) {
293 $test->createContactWithTransaction('nest-tx', $createStyle, $commitStyle);
294 $test->assertContactsExistByOffset(array(0 => TRUE));
295 throw new Exception("Ruh-roh");
298 catch (Exception
$ex) {
300 if (get_class($e) != 'Exception' ||
$e->getMessage() != 'Ruh-roh') {
304 $this->assertTrue($e instanceof Exception
);
305 $this->assertContactsExistByOffset(array(0 => FALSE));
312 public function assertContactsExist($cids, $exist = TRUE) {
313 foreach ($cids as $cid) {
314 $this->assertTrue(is_numeric($cid));
315 $this->assertDBQuery($exist ?
1 : 0, 'SELECT count(*) FROM civicrm_contact WHERE id = %1', array(
316 1 => array($cid, 'Integer'),
322 * @param array $existsByOffset
323 * Array(int $cidOffset => bool $expectExists).
324 * @param int $generalOffset
326 public function assertContactsExistByOffset($existsByOffset, $generalOffset = 0) {
327 foreach ($existsByOffset as $offset => $expectExists) {
328 $this->assertTrue(isset($this->cids
[$generalOffset +
$offset]), "Find cid at offset($generalOffset + $offset)");
329 $cid = $this->cids
[$generalOffset +
$offset];
330 $this->assertTrue(is_numeric($cid));
331 $this->assertDBQuery($expectExists ?
1 : 0, 'SELECT count(*) FROM civicrm_contact WHERE id = %1', array(
332 1 => array($cid, 'Integer'),
333 ), "Check contact at offset($generalOffset + $offset)");
338 * Use SQL to INSERT a contact and assert success. Perform
339 * work within a transaction.
341 * @param string $nesting
342 * 'reuse-tx'|'nest-tx' how to construct transaction.
343 * @param string $insert
344 * 'sql-insert'|'bao-create' how to add the example record.
345 * @param string $outcome
346 * 'rollback'|'implicit-commit'|'explicit-commit' how to finish transaction.
350 public function createContactWithTransaction($nesting, $insert, $outcome) {
351 if ($nesting != 'reuse-tx' && $nesting != 'nest-tx') {
352 throw new RuntimeException('Bad test data: reuse=' . $nesting);
354 if ($insert != 'sql-insert' && $insert != 'bao-create') {
355 throw new RuntimeException('Bad test data: insert=' . $insert);
357 if ($outcome != 'rollback' && $outcome != 'implicit-commit' && $outcome != 'explicit-commit') {
358 throw new RuntimeException('Bad test data: outcome=' . $outcome);
361 $tx = new CRM_Core_Transaction($nesting === 'nest-tx');
363 if ($insert == 'sql-insert') {
364 $r = CRM_Core_DAO
::executeQuery("INSERT INTO civicrm_contact(first_name,last_name) VALUES ('ff', 'll')");
365 $cid = $r->getConnection()->lastInsertId();
367 elseif ($insert == 'bao-create') {
369 'contact_type' => 'Individual',
370 'first_name' => 'FF',
373 $r = CRM_Contact_BAO_Contact
::create($params);
377 $this->cids
[] = $cid;
379 $this->assertContactsExist(array($cid), TRUE);
381 if ($outcome == 'rollback') {
384 elseif ($outcome == 'explicit-commit') {
386 } // else: implicit-commit
392 * Perform a series of operations within smaller transactions.
394 * @param string $nesting
395 * 'reuse-tx'|'nest-tx' how to construct transaction.
396 * @param array $callbacks
397 * See createContactWithTransaction.
398 * @param array $existsByOffset
399 * See assertContactsMix.
400 * @param string $outcome
401 * 'rollback'|'implicit-commit'|'explicit-commit' how to finish transaction.
404 public function runBatch($nesting, $callbacks, $existsByOffset, $outcome) {
405 if ($nesting != 'reuse-tx' && $nesting != 'nest-tx') {
406 throw new RuntimeException('Bad test data: nesting=' . $nesting);
408 if ($outcome != 'rollback' && $outcome != 'implicit-commit' && $outcome != 'explicit-commit') {
409 throw new RuntimeException('Bad test data: outcome=' . $nesting);
412 $tx = new CRM_Core_Transaction($nesting === 'reuse-tx');
414 $generalOffset = count($this->cids
);
415 foreach ($callbacks as $callback) {
416 list ($cbNesting, $cbInsert, $cbOutcome) = $callback;
417 $this->createContactWithTransaction($cbNesting, $cbInsert, $cbOutcome);
420 $this->assertContactsExistByOffset($existsByOffset, $generalOffset);
422 if ($outcome == 'rollback') {
425 elseif ($outcome == 'explicit-commit') {
427 } // else: implicit-commit
434 public function _preCommit($arg1, $arg2) {
435 $this->callbackLog
[] = array('_preCommit', $arg1, $arg2);
442 public function _postCommit($arg1, $arg2) {
443 $this->callbackLog
[] = array('_postCommit', $arg1, $arg2);
450 public function _preRollback($arg1, $arg2) {
451 $this->callbackLog
[] = array('_preRollback', $arg1, $arg2);
458 public function _postRollback($arg1, $arg2) {
459 $this->callbackLog
[] = array('_postRollback', $arg1, $arg2);