4 * Class CRM_Core_TransactionTest
6 class CRM_Core_TransactionTest
extends CiviUnitTestCase
{
14 * @var array (int $idx => int $contactId) list of contact IDs that have been created (in order of creation)
16 * Note that ID this is all IDs created by the test-case -- even if the creation was subsequently rolled back
20 public function setUp() {
22 $this->quickCleanup(array('civicrm_contact', 'civicrm_activity'));
23 $this->callbackLog
= array();
24 $this->cids
= array();
30 public function dataCreateStyle() {
40 public function dataCreateAndCommitStyles() {
42 array('sql-insert', 'implicit-commit'),
43 array('sql-insert', 'explicit-commit'),
44 array('bao-create', 'implicit-commit'),
45 array('bao-create', 'explicit-commit'),
50 * @param string $createStyle
51 * 'sql-insert'|'bao-create'.
52 * @param string $commitStyle
53 * 'implicit-commit'|'explicit-commit'.
54 * @dataProvider dataCreateAndCommitStyles
56 public function testBasicCommit($createStyle, $commitStyle) {
57 $this->createContactWithTransaction('reuse-tx', $createStyle, $commitStyle);
58 $this->assertCount(1, $this->cids
);
59 $this->assertContactsExistByOffset(array(0 => TRUE));
63 * @dataProvider dataCreateStyle
66 public function testBasicRollback($createStyle) {
67 $this->createContactWithTransaction('reuse-tx', $createStyle, 'rollback');
68 $this->assertCount(1, $this->cids
);
69 $this->assertContactsExistByOffset(array(0 => FALSE));
73 * Test in which an outer function makes multiple calls to inner functions.
74 * but then rolls back the entire set.
76 * @param string $createStyle
77 * 'sql-insert'|'bao-create'.
78 * @param string $commitStyle
79 * 'implicit-commit'|'explicit-commit'.
80 * @dataProvider dataCreateAndCommitStyles
82 public function testBatchRollback($createStyle, $commitStyle) {
86 array('reuse-tx', $createStyle, $commitStyle), // cid 0
87 array('reuse-tx', $createStyle, $commitStyle), // cid 1
89 array(0 => TRUE, 1 => TRUE),
92 $this->assertCount(2, $this->cids
);
93 $this->assertContactsExistByOffset(array(0 => FALSE, 1 => FALSE));
97 * Test in which runBatch makes multiple calls to
98 * createContactWithTransaction using a mix of rollback/commit.
99 * createContactWithTransaction use nesting (savepoints), so the
100 * batch is able to commit.
102 * @param string $createStyle
103 * 'sql-insert'|'bao-create'.
104 * @param string $commitStyle
105 * 'implicit-commit'|'explicit-commit'.
106 * @dataProvider dataCreateAndCommitStyles
108 public function testMixedBatchCommit_nesting($createStyle, $commitStyle) {
112 array('nest-tx', $createStyle, $commitStyle), // cid 0
113 array('nest-tx', $createStyle, 'rollback'), // cid 1
114 array('nest-tx', $createStyle, $commitStyle), // cid 2
116 array(0 => TRUE, 1 => FALSE, 2 => TRUE),
119 $this->assertCount(3, $this->cids
);
120 $this->assertContactsExistByOffset(array(0 => TRUE, 1 => FALSE, 2 => TRUE));
124 * Test in which runBatch makes multiple calls to
125 * createContactWithTransaction using a mix of rollback/commit.
126 * createContactWithTransaction reuses the main transaction,
127 * so the overall batch must rollback.
129 * @param string $createStyle
130 * 'sql-insert'|'bao-create'.
131 * @param string $commitStyle
132 * 'implicit-commit'|'explicit-commit'.
133 * @dataProvider dataCreateAndCommitStyles
135 public function testMixedBatchCommit_reuse($createStyle, $commitStyle) {
139 array('reuse-tx', $createStyle, $commitStyle), // cid 0
140 array('reuse-tx', $createStyle, 'rollback'), // cid 1
141 array('reuse-tx', $createStyle, $commitStyle), // cid 2
143 array(0 => TRUE, 1 => TRUE, 2 => TRUE),
146 $this->assertCount(3, $this->cids
);
147 $this->assertContactsExistByOffset(array(0 => FALSE, 1 => FALSE, 2 => FALSE));
151 * Test in which runBatch makes multiple calls to
152 * createContactWithTransaction using a mix of rollback/commit.
153 * The overall batch is rolled back.
155 * @param string $createStyle
156 * 'sql-insert'|'bao-create'.
157 * @param string $commitStyle
158 * 'implicit-commit'|'explicit-commit'.
159 * @dataProvider dataCreateAndCommitStyles
161 public function testMixedBatchRollback_nesting($createStyle, $commitStyle) {
162 $this->assertFalse(CRM_Core_Transaction
::isActive());
166 array('nest-tx', $createStyle, $commitStyle), // cid 0
167 array('nest-tx', $createStyle, 'rollback'), // cid 1
168 array('nest-tx', $createStyle, $commitStyle), // cid 2
170 array(0 => TRUE, 1 => FALSE, 2 => TRUE),
173 $this->assertFalse(CRM_Core_Transaction
::isActive());
174 $this->assertCount(3, $this->cids
);
175 $this->assertContactsExistByOffset(array(0 => FALSE, 1 => FALSE, 2 => FALSE));
178 public function testIsActive() {
179 $this->assertEquals(FALSE, CRM_Core_Transaction
::isActive());
180 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
181 $tx = new CRM_Core_Transaction();
182 $this->assertEquals(TRUE, CRM_Core_Transaction
::isActive());
183 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
185 $this->assertEquals(FALSE, CRM_Core_Transaction
::isActive());
186 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
189 public function testIsActive_rollback() {
190 $this->assertEquals(FALSE, CRM_Core_Transaction
::isActive());
191 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
193 $tx = new CRM_Core_Transaction();
194 $this->assertEquals(TRUE, CRM_Core_Transaction
::isActive());
195 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
198 $this->assertEquals(TRUE, CRM_Core_Transaction
::isActive());
199 $this->assertEquals(FALSE, CRM_Core_Transaction
::willCommit());
202 $this->assertEquals(FALSE, CRM_Core_Transaction
::isActive());
203 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
206 public function testCallback_commit() {
207 $tx = new CRM_Core_Transaction();
209 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_PRE_COMMIT
, array($this, '_preCommit'), array(
213 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_POST_COMMIT
, array($this, '_postCommit'), array(
217 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_PRE_ROLLBACK
, array(
220 ), array('asd', 'fgh'));
221 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_POST_ROLLBACK
, array(
224 ), array('jkl', ';'));
226 CRM_Core_DAO
::executeQuery('UPDATE civicrm_contact SET id = 100 WHERE id = 100');
228 $this->assertEquals(array(), $this->callbackLog
);
230 $this->assertEquals(array('_preCommit', 'qwe', 'rty'), $this->callbackLog
[0]);
231 $this->assertEquals(array('_postCommit', 'uio', 'p[]'), $this->callbackLog
[1]);
234 public function testCallback_rollback() {
235 $tx = new CRM_Core_Transaction();
237 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_PRE_COMMIT
, array($this, '_preCommit'), array(
241 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_POST_COMMIT
, array($this, '_postCommit'), array(
245 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_PRE_ROLLBACK
, array(
248 ), array('dsa', 'hgf'));
249 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_POST_ROLLBACK
, array(
252 ), array('lkj', ';'));
254 CRM_Core_DAO
::executeQuery('UPDATE civicrm_contact SET id = 100 WHERE id = 100');
257 $this->assertEquals(array(), $this->callbackLog
);
259 $this->assertEquals(array('_preRollback', 'dsa', 'hgf'), $this->callbackLog
[0]);
260 $this->assertEquals(array('_postRollback', 'lkj', ';'), $this->callbackLog
[1]);
264 * @param string $createStyle
265 * 'sql-insert'|'bao-create'.
266 * @param string $commitStyle
267 * 'implicit-commit'|'explicit-commit'.
268 * @dataProvider dataCreateAndCommitStyles
270 public function testRun_ok($createStyle, $commitStyle) {
272 CRM_Core_Transaction
::create(TRUE)->run(function ($tx) use (&$test, $createStyle, $commitStyle) {
273 $test->createContactWithTransaction('nest-tx', $createStyle, $commitStyle);
274 $test->assertContactsExistByOffset(array(0 => TRUE));
276 $this->assertContactsExistByOffset(array(0 => TRUE));
280 * @param string $createStyle
281 * 'sql-insert'|'bao-create'.
282 * @param string $commitStyle
283 * 'implicit-commit'|'explicit-commit'.
284 * @dataProvider dataCreateAndCommitStyles
286 public function testRun_exception($createStyle, $commitStyle) {
287 $tx = new CRM_Core_Transaction();
289 $e = NULL; // Exception
291 CRM_Core_Transaction
::create(TRUE)->run(function ($tx) use (&$test, $createStyle, $commitStyle) {
292 $test->createContactWithTransaction('nest-tx', $createStyle, $commitStyle);
293 $test->assertContactsExistByOffset(array(0 => TRUE));
294 throw new Exception("Ruh-roh");
297 catch (Exception
$ex) {
299 if (get_class($e) != 'Exception' ||
$e->getMessage() != 'Ruh-roh') {
303 $this->assertTrue($e instanceof Exception
);
304 $this->assertContactsExistByOffset(array(0 => FALSE));
311 public function assertContactsExist($cids, $exist = TRUE) {
312 foreach ($cids as $cid) {
313 $this->assertTrue(is_numeric($cid));
314 $this->assertDBQuery($exist ?
1 : 0, 'SELECT count(*) FROM civicrm_contact WHERE id = %1', array(
315 1 => array($cid, 'Integer'),
321 * @param array $existsByOffset
322 * Array(int $cidOffset => bool $expectExists).
323 * @param int $generalOffset
325 public function assertContactsExistByOffset($existsByOffset, $generalOffset = 0) {
326 foreach ($existsByOffset as $offset => $expectExists) {
327 $this->assertTrue(isset($this->cids
[$generalOffset +
$offset]), "Find cid at offset($generalOffset + $offset)");
328 $cid = $this->cids
[$generalOffset +
$offset];
329 $this->assertTrue(is_numeric($cid));
330 $this->assertDBQuery($expectExists ?
1 : 0, 'SELECT count(*) FROM civicrm_contact WHERE id = %1', array(
331 1 => array($cid, 'Integer'),
332 ), "Check contact at offset($generalOffset + $offset)");
337 * Use SQL to INSERT a contact and assert success. Perform
338 * work within a transaction.
340 * @param string $nesting
341 * 'reuse-tx'|'nest-tx' how to construct transaction.
342 * @param string $insert
343 * 'sql-insert'|'bao-create' how to add the example record.
344 * @param string $outcome
345 * 'rollback'|'implicit-commit'|'explicit-commit' how to finish transaction.
349 public function createContactWithTransaction($nesting, $insert, $outcome) {
350 if ($nesting != 'reuse-tx' && $nesting != 'nest-tx') {
351 throw new RuntimeException('Bad test data: reuse=' . $nesting);
353 if ($insert != 'sql-insert' && $insert != 'bao-create') {
354 throw new RuntimeException('Bad test data: insert=' . $insert);
356 if ($outcome != 'rollback' && $outcome != 'implicit-commit' && $outcome != 'explicit-commit') {
357 throw new RuntimeException('Bad test data: outcome=' . $outcome);
360 $tx = new CRM_Core_Transaction($nesting === 'nest-tx');
362 if ($insert == 'sql-insert') {
363 $r = CRM_Core_DAO
::executeQuery("INSERT INTO civicrm_contact(first_name,last_name) VALUES ('ff', 'll')");
364 $cid = mysql_insert_id();
366 elseif ($insert == 'bao-create') {
368 'contact_type' => 'Individual',
369 'first_name' => 'FF',
372 $r = CRM_Contact_BAO_Contact
::create($params);
376 $this->cids
[] = $cid;
378 $this->assertContactsExist(array($cid), TRUE);
380 if ($outcome == 'rollback') {
383 elseif ($outcome == 'explicit-commit') {
385 } // else: implicit-commit
391 * Perform a series of operations within smaller transactions.
393 * @param string $nesting
394 * 'reuse-tx'|'nest-tx' how to construct transaction.
395 * @param array $callbacks
396 * See createContactWithTransaction.
397 * @param array $existsByOffset
398 * See assertContactsMix.
399 * @param string $outcome
400 * 'rollback'|'implicit-commit'|'explicit-commit' how to finish transaction.
403 public function runBatch($nesting, $callbacks, $existsByOffset, $outcome) {
404 if ($nesting != 'reuse-tx' && $nesting != 'nest-tx') {
405 throw new RuntimeException('Bad test data: nesting=' . $nesting);
407 if ($outcome != 'rollback' && $outcome != 'implicit-commit' && $outcome != 'explicit-commit') {
408 throw new RuntimeException('Bad test data: outcome=' . $nesting);
411 $tx = new CRM_Core_Transaction($nesting === 'reuse-tx');
413 $generalOffset = count($this->cids
);
414 foreach ($callbacks as $callback) {
415 list ($cbNesting, $cbInsert, $cbOutcome) = $callback;
416 $this->createContactWithTransaction($cbNesting, $cbInsert, $cbOutcome);
419 $this->assertContactsExistByOffset($existsByOffset, $generalOffset);
421 if ($outcome == 'rollback') {
424 elseif ($outcome == 'explicit-commit') {
426 } // else: implicit-commit
433 public function _preCommit($arg1, $arg2) {
434 $this->callbackLog
[] = array('_preCommit', $arg1, $arg2);
441 public function _postCommit($arg1, $arg2) {
442 $this->callbackLog
[] = array('_postCommit', $arg1, $arg2);
449 public function _preRollback($arg1, $arg2) {
450 $this->callbackLog
[] = array('_preRollback', $arg1, $arg2);
457 public function _postRollback($arg1, $arg2) {
458 $this->callbackLog
[] = array('_postRollback', $arg1, $arg2);