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();
33 public function dataCreateStyle() {
43 public function dataCreateAndCommitStyles() {
45 array('sql-insert', 'implicit-commit'),
46 array('sql-insert', 'explicit-commit'),
47 array('bao-create', 'implicit-commit'),
48 array('bao-create', 'explicit-commit'),
53 * @param string $createStyle
54 * 'sql-insert'|'bao-create'.
55 * @param string $commitStyle
56 * 'implicit-commit'|'explicit-commit'.
57 * @dataProvider dataCreateAndCommitStyles
59 public function testBasicCommit($createStyle, $commitStyle) {
60 $this->createContactWithTransaction('reuse-tx', $createStyle, $commitStyle);
61 $this->assertCount(1, $this->cids
);
62 $this->assertContactsExistByOffset(array(0 => TRUE));
66 * @dataProvider dataCreateStyle
69 public function testBasicRollback($createStyle) {
70 $this->createContactWithTransaction('reuse-tx', $createStyle, 'rollback');
71 $this->assertCount(1, $this->cids
);
72 $this->assertContactsExistByOffset(array(0 => FALSE));
76 * Test in which an outer function makes multiple calls to inner functions.
77 * but then rolls back the entire set.
79 * @param string $createStyle
80 * 'sql-insert'|'bao-create'.
81 * @param string $commitStyle
82 * 'implicit-commit'|'explicit-commit'.
83 * @dataProvider dataCreateAndCommitStyles
85 public function testBatchRollback($createStyle, $commitStyle) {
89 array('reuse-tx', $createStyle, $commitStyle), // cid 0
90 array('reuse-tx', $createStyle, $commitStyle), // cid 1
92 array(0 => TRUE, 1 => TRUE),
95 $this->assertCount(2, $this->cids
);
96 $this->assertContactsExistByOffset(array(0 => FALSE, 1 => FALSE));
100 * Test in which runBatch makes multiple calls to
101 * createContactWithTransaction using a mix of rollback/commit.
102 * createContactWithTransaction use nesting (savepoints), so the
103 * batch is able to commit.
105 * @param string $createStyle
106 * 'sql-insert'|'bao-create'.
107 * @param string $commitStyle
108 * 'implicit-commit'|'explicit-commit'.
109 * @dataProvider dataCreateAndCommitStyles
111 public function testMixedBatchCommit_nesting($createStyle, $commitStyle) {
115 array('nest-tx', $createStyle, $commitStyle), // cid 0
116 array('nest-tx', $createStyle, 'rollback'), // cid 1
117 array('nest-tx', $createStyle, $commitStyle), // cid 2
119 array(0 => TRUE, 1 => FALSE, 2 => TRUE),
122 $this->assertCount(3, $this->cids
);
123 $this->assertContactsExistByOffset(array(0 => TRUE, 1 => FALSE, 2 => TRUE));
127 * Test in which runBatch makes multiple calls to
128 * createContactWithTransaction using a mix of rollback/commit.
129 * createContactWithTransaction reuses the main transaction,
130 * so the overall batch must rollback.
132 * @param string $createStyle
133 * 'sql-insert'|'bao-create'.
134 * @param string $commitStyle
135 * 'implicit-commit'|'explicit-commit'.
136 * @dataProvider dataCreateAndCommitStyles
138 public function testMixedBatchCommit_reuse($createStyle, $commitStyle) {
142 array('reuse-tx', $createStyle, $commitStyle), // cid 0
143 array('reuse-tx', $createStyle, 'rollback'), // cid 1
144 array('reuse-tx', $createStyle, $commitStyle), // cid 2
146 array(0 => TRUE, 1 => TRUE, 2 => TRUE),
149 $this->assertCount(3, $this->cids
);
150 $this->assertContactsExistByOffset(array(0 => FALSE, 1 => FALSE, 2 => FALSE));
154 * Test in which runBatch makes multiple calls to
155 * createContactWithTransaction using a mix of rollback/commit.
156 * The overall batch is rolled back.
158 * @param string $createStyle
159 * 'sql-insert'|'bao-create'.
160 * @param string $commitStyle
161 * 'implicit-commit'|'explicit-commit'.
162 * @dataProvider dataCreateAndCommitStyles
164 public function testMixedBatchRollback_nesting($createStyle, $commitStyle) {
165 $this->assertFalse(CRM_Core_Transaction
::isActive());
169 array('nest-tx', $createStyle, $commitStyle), // cid 0
170 array('nest-tx', $createStyle, 'rollback'), // cid 1
171 array('nest-tx', $createStyle, $commitStyle), // cid 2
173 array(0 => TRUE, 1 => FALSE, 2 => TRUE),
176 $this->assertFalse(CRM_Core_Transaction
::isActive());
177 $this->assertCount(3, $this->cids
);
178 $this->assertContactsExistByOffset(array(0 => FALSE, 1 => FALSE, 2 => FALSE));
181 public function testIsActive() {
182 $this->assertEquals(FALSE, CRM_Core_Transaction
::isActive());
183 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
184 $tx = new CRM_Core_Transaction();
185 $this->assertEquals(TRUE, CRM_Core_Transaction
::isActive());
186 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
188 $this->assertEquals(FALSE, CRM_Core_Transaction
::isActive());
189 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
192 public function testIsActive_rollback() {
193 $this->assertEquals(FALSE, CRM_Core_Transaction
::isActive());
194 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
196 $tx = new CRM_Core_Transaction();
197 $this->assertEquals(TRUE, CRM_Core_Transaction
::isActive());
198 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
201 $this->assertEquals(TRUE, CRM_Core_Transaction
::isActive());
202 $this->assertEquals(FALSE, CRM_Core_Transaction
::willCommit());
205 $this->assertEquals(FALSE, CRM_Core_Transaction
::isActive());
206 $this->assertEquals(TRUE, CRM_Core_Transaction
::willCommit());
209 public function testCallback_commit() {
210 $tx = new CRM_Core_Transaction();
212 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_PRE_COMMIT
, array($this, '_preCommit'), array(
216 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_POST_COMMIT
, array($this, '_postCommit'), array(
220 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_PRE_ROLLBACK
, array(
223 ), array('asd', 'fgh'));
224 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_POST_ROLLBACK
, array(
227 ), array('jkl', ';'));
229 CRM_Core_DAO
::executeQuery('UPDATE civicrm_contact SET id = 100 WHERE id = 100');
231 $this->assertEquals(array(), $this->callbackLog
);
233 $this->assertEquals(array('_preCommit', 'qwe', 'rty'), $this->callbackLog
[0]);
234 $this->assertEquals(array('_postCommit', 'uio', 'p[]'), $this->callbackLog
[1]);
237 public function testCallback_rollback() {
238 $tx = new CRM_Core_Transaction();
240 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_PRE_COMMIT
, array($this, '_preCommit'), array(
244 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_POST_COMMIT
, array($this, '_postCommit'), array(
248 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_PRE_ROLLBACK
, array(
251 ), array('dsa', 'hgf'));
252 CRM_Core_Transaction
::addCallback(CRM_Core_Transaction
::PHASE_POST_ROLLBACK
, array(
255 ), array('lkj', ';'));
257 CRM_Core_DAO
::executeQuery('UPDATE civicrm_contact SET id = 100 WHERE id = 100');
260 $this->assertEquals(array(), $this->callbackLog
);
262 $this->assertEquals(array('_preRollback', 'dsa', 'hgf'), $this->callbackLog
[0]);
263 $this->assertEquals(array('_postRollback', 'lkj', ';'), $this->callbackLog
[1]);
267 * @param string $createStyle
268 * 'sql-insert'|'bao-create'.
269 * @param string $commitStyle
270 * 'implicit-commit'|'explicit-commit'.
271 * @dataProvider dataCreateAndCommitStyles
273 public function testRun_ok($createStyle, $commitStyle) {
275 CRM_Core_Transaction
::create(TRUE)->run(function ($tx) use (&$test, $createStyle, $commitStyle) {
276 $test->createContactWithTransaction('nest-tx', $createStyle, $commitStyle);
277 $test->assertContactsExistByOffset(array(0 => TRUE));
279 $this->assertContactsExistByOffset(array(0 => TRUE));
283 * @param string $createStyle
284 * 'sql-insert'|'bao-create'.
285 * @param string $commitStyle
286 * 'implicit-commit'|'explicit-commit'.
287 * @dataProvider dataCreateAndCommitStyles
289 public function testRun_exception($createStyle, $commitStyle) {
290 $tx = new CRM_Core_Transaction();
292 $e = NULL; // Exception
294 CRM_Core_Transaction
::create(TRUE)->run(function ($tx) use (&$test, $createStyle, $commitStyle) {
295 $test->createContactWithTransaction('nest-tx', $createStyle, $commitStyle);
296 $test->assertContactsExistByOffset(array(0 => TRUE));
297 throw new Exception("Ruh-roh");
300 catch (Exception
$ex) {
302 if (get_class($e) != 'Exception' ||
$e->getMessage() != 'Ruh-roh') {
306 $this->assertTrue($e instanceof Exception
);
307 $this->assertContactsExistByOffset(array(0 => FALSE));
314 public function assertContactsExist($cids, $exist = TRUE) {
315 foreach ($cids as $cid) {
316 $this->assertTrue(is_numeric($cid));
317 $this->assertDBQuery($exist ?
1 : 0, 'SELECT count(*) FROM civicrm_contact WHERE id = %1', array(
318 1 => array($cid, 'Integer'),
324 * @param array $existsByOffset
325 * Array(int $cidOffset => bool $expectExists).
326 * @param int $generalOffset
328 public function assertContactsExistByOffset($existsByOffset, $generalOffset = 0) {
329 foreach ($existsByOffset as $offset => $expectExists) {
330 $this->assertTrue(isset($this->cids
[$generalOffset +
$offset]), "Find cid at offset($generalOffset + $offset)");
331 $cid = $this->cids
[$generalOffset +
$offset];
332 $this->assertTrue(is_numeric($cid));
333 $this->assertDBQuery($expectExists ?
1 : 0, 'SELECT count(*) FROM civicrm_contact WHERE id = %1', array(
334 1 => array($cid, 'Integer'),
335 ), "Check contact at offset($generalOffset + $offset)");
340 * Use SQL to INSERT a contact and assert success. Perform
341 * work within a transaction.
343 * @param string $nesting
344 * 'reuse-tx'|'nest-tx' how to construct transaction.
345 * @param string $insert
346 * 'sql-insert'|'bao-create' how to add the example record.
347 * @param string $outcome
348 * 'rollback'|'implicit-commit'|'explicit-commit' how to finish transaction.
352 public function createContactWithTransaction($nesting, $insert, $outcome) {
353 if ($nesting != 'reuse-tx' && $nesting != 'nest-tx') {
354 throw new RuntimeException('Bad test data: reuse=' . $nesting);
356 if ($insert != 'sql-insert' && $insert != 'bao-create') {
357 throw new RuntimeException('Bad test data: insert=' . $insert);
359 if ($outcome != 'rollback' && $outcome != 'implicit-commit' && $outcome != 'explicit-commit') {
360 throw new RuntimeException('Bad test data: outcome=' . $outcome);
363 $tx = new CRM_Core_Transaction($nesting === 'nest-tx');
365 if ($insert == 'sql-insert') {
366 $r = CRM_Core_DAO
::executeQuery("INSERT INTO civicrm_contact(first_name,last_name) VALUES ('ff', 'll')");
367 $cid = mysql_insert_id();
369 elseif ($insert == 'bao-create') {
371 'contact_type' => 'Individual',
372 'first_name' => 'FF',
375 $r = CRM_Contact_BAO_Contact
::create($params);
379 $this->cids
[] = $cid;
381 $this->assertContactsExist(array($cid), TRUE);
383 if ($outcome == 'rollback') {
386 elseif ($outcome == 'explicit-commit') {
388 } // else: implicit-commit
394 * Perform a series of operations within smaller transactions.
396 * @param string $nesting
397 * 'reuse-tx'|'nest-tx' how to construct transaction.
398 * @param array $callbacks
399 * See createContactWithTransaction.
400 * @param array $existsByOffset
401 * See assertContactsMix.
402 * @param string $outcome
403 * 'rollback'|'implicit-commit'|'explicit-commit' how to finish transaction.
406 public function runBatch($nesting, $callbacks, $existsByOffset, $outcome) {
407 if ($nesting != 'reuse-tx' && $nesting != 'nest-tx') {
408 throw new RuntimeException('Bad test data: nesting=' . $nesting);
410 if ($outcome != 'rollback' && $outcome != 'implicit-commit' && $outcome != 'explicit-commit') {
411 throw new RuntimeException('Bad test data: outcome=' . $nesting);
414 $tx = new CRM_Core_Transaction($nesting === 'reuse-tx');
416 $generalOffset = count($this->cids
);
417 foreach ($callbacks as $callback) {
418 list ($cbNesting, $cbInsert, $cbOutcome) = $callback;
419 $this->createContactWithTransaction($cbNesting, $cbInsert, $cbOutcome);
422 $this->assertContactsExistByOffset($existsByOffset, $generalOffset);
424 if ($outcome == 'rollback') {
427 elseif ($outcome == 'explicit-commit') {
429 } // else: implicit-commit
436 public function _preCommit($arg1, $arg2) {
437 $this->callbackLog
[] = array('_preCommit', $arg1, $arg2);
444 public function _postCommit($arg1, $arg2) {
445 $this->callbackLog
[] = array('_postCommit', $arg1, $arg2);
452 public function _preRollback($arg1, $arg2) {
453 $this->callbackLog
[] = array('_preRollback', $arg1, $arg2);
460 public function _postRollback($arg1, $arg2) {
461 $this->callbackLog
[] = array('_postRollback', $arg1, $arg2);